OpenTTD Source 20260218-master-g2123fca5ea
gfx_layout.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "core/math_func.hpp"
12#include "gfx_layout.h"
13#include "string_func.h"
14#include "strings_func.h"
15#include "core/utf8.hpp"
16#include "debug.h"
17
18#include "table/control_codes.h"
19
20#include "gfx_layout_fallback.h"
21
22#if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
23#include "gfx_layout_icu.h"
24#endif /* WITH_ICU_I18N && WITH_HARFBUZZ */
25
26#ifdef WITH_UNISCRIBE
28#endif /* WITH_UNISCRIBE */
29
30#ifdef WITH_COCOA
32#endif
33
34#include "safeguards.h"
35
36
38std::unique_ptr<Layouter::LineCache> Layouter::linecache;
39
41Layouter::FontColourMap Layouter::fonts[FS_END];
42
43
50 fc(FontCache::Get(size)), colour(colour)
51{
52 assert(size < FS_END);
53}
54
64template <typename T>
65static inline void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
66{
67 typename T::CharType *buff_begin = new typename T::CharType[str.size() + 1];
68 /* Move ownership of buff_begin into the Buffer/unique_ptr. */
69 line.buffer = Layouter::LineCacheItem::Buffer(buff_begin, [](void *p) { delete[] reinterpret_cast<T::CharType *>(p); });
70
71 const typename T::CharType *buffer_last = buff_begin + str.size() + 1;
72 typename T::CharType *buff = buff_begin;
73 FontMap &font_mapping = line.runs;
74 Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
75
76 font_mapping.clear();
77
78 /*
79 * Go through the whole string while adding Font instances to the font map
80 * whenever the font changes, and convert the wide characters into a format
81 * usable by ParagraphLayout.
82 */
83 for (char32_t c : Utf8View(str)) {
84 if (c == '\0' || c == '\n') {
85 /* Caller should already have filtered out these characters. */
86 NOT_REACHED();
87 } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
88 state.SetColour((TextColour)(c - SCC_BLUE));
89 } else if (c == SCC_PUSH_COLOUR) {
90 state.PushColour();
91 } else if (c == SCC_POP_COLOUR) {
92 state.PopColour();
93 } else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
94 state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
95 } else {
96 /* Filter out non printable characters */
97 if (!IsPrintable(c)) continue;
98 /* Filter out text direction characters that shouldn't be drawn, and
99 * will not be handled in the fallback case because they are mostly
100 * needed for RTL languages which need more proper shaping support. */
101 if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
102 buff += T::AppendToBuffer(buff, buffer_last, c);
103 if (buff >= buffer_last) break;
104 continue;
105 }
106
107 if (font_mapping.empty() || font_mapping.back().first != buff - buff_begin) {
108 font_mapping.emplace_back(buff - buff_begin, f);
109 }
110 f = Layouter::GetFont(state.fontsize, state.cur_colour);
111 }
112
113 /* Better safe than sorry. */
114 *buff = '\0';
115
116 if (font_mapping.empty() || font_mapping.back().first != buff - buff_begin) {
117 font_mapping.emplace_back(buff - buff_begin, f);
118 }
119 line.layout = T::GetParagraphLayout(buff_begin, buff, font_mapping);
120 line.state_after = state;
121}
122
129Layouter::Layouter(std::string_view str, int maxw, FontSize fontsize) : string(str)
130{
131 FontState state(TC_INVALID, fontsize);
132
133 while (true) {
134 auto line_length = str.find_first_of('\n');
135 auto str_line = str.substr(0, line_length);
136
137 LineCacheItem &line = GetCachedParagraphLayout(str_line, state);
138 if (line.layout != nullptr) {
139 state = line.state_after;
140 line.layout->Reflow();
141 } else {
142 /* Line is new, layout it */
143 FontState old_state = state;
144
145#if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
146 if (line.layout == nullptr) {
147 GetLayouter<ICUParagraphLayoutFactory>(line, str_line, state);
148 if (line.layout == nullptr) {
149 state = std::move(old_state);
150 }
151 }
152#endif
153
154#ifdef WITH_UNISCRIBE
155 if (line.layout == nullptr) {
157 if (line.layout == nullptr) {
158 state = std::move(old_state);
159 }
160 }
161#endif
162
163#ifdef WITH_COCOA
164 if (line.layout == nullptr) {
165 GetLayouter<CoreTextParagraphLayoutFactory>(line, str_line, state);
166 if (line.layout == nullptr) {
167 state = std::move(old_state);
168 }
169 }
170#endif
171
172 if (line.layout == nullptr) {
173 GetLayouter<FallbackParagraphLayoutFactory>(line, str_line, state);
174 }
175 }
176
177 if (line.cached_width != maxw) {
178 /* First run or width has changed, so we need to go through the layouter. Lines are moved into a cache to
179 * be reused if the width is not changed. */
180 line.cached_layout.clear();
181 line.cached_width = maxw;
182 for (;;) {
183 auto l = line.layout->NextLine(maxw);
184 if (l == nullptr) break;
185 line.cached_layout.push_back(std::move(l));
186 }
187 }
188
189 /* Retrieve layout from the cache. */
190 for (const auto &l : line.cached_layout) {
191 this->push_back(l.get());
192 }
193
194 /* Break out if this was the last line. */
195 if (line_length == std::string_view::npos) {
196 break;
197 }
198
199 /* Go to the next line. */
200 str.remove_prefix(line_length + 1);
201 }
202}
203
209{
210 Dimension d = { 0, 0 };
211 for (const auto &l : *this) {
212 d.width = std::max<uint>(d.width, l->GetWidth());
213 d.height += l->GetLeading();
214 }
215 return d;
216}
217
223static bool IsConsumedFormattingCode(char32_t ch)
224{
225 if (ch >= SCC_BLUE && ch <= SCC_BLACK) return true;
226 if (ch == SCC_PUSH_COLOUR) return true;
227 if (ch == SCC_POP_COLOUR) return true;
228 if (ch >= SCC_FIRST_FONT && ch <= SCC_LAST_FONT) return true;
229 /* All other characters defined in Unicode standard are assumed to be non-consumed. */
230 return false;
231}
232
239ParagraphLayouter::Position Layouter::GetCharPosition(std::string_view::const_iterator ch) const
240{
241 const auto &line = this->front();
242
243 /* Pointer to the end-of-string marker? Return total line width. */
244 if (ch == this->string.end()) {
245 Point p = {_current_text_dir == TD_LTR ? line->GetWidth() : 0, 0};
246 return p;
247 }
248
249 /* Initial position, returned if character not found. */
250 const ParagraphLayouter::Position initial_position = Point{_current_text_dir == TD_LTR ? 0 : line->GetWidth(), 0};
251
252 /* Find the code point index which corresponds to the char
253 * pointer into our UTF-8 source string. */
254 size_t index = 0;
255 {
256 Utf8View view(this->string);
257 const size_t offset = ch - this->string.begin();
258 const auto pos = view.GetIterAtByte(offset);
259
260 /* We couldn't find the code point index. */
261 if (pos.GetByteOffset() != offset) return initial_position;
262
263 for (auto it = view.begin(); it < pos; ++it) {
264 char32_t c = *it;
265 if (!IsConsumedFormattingCode(c)) index += line->GetInternalCharLength(c);
266 }
267 }
268
269 const ParagraphLayouter::Position *position = &initial_position;
270
271 /* Scan all runs until we've found our code point index. */
272 size_t best_index = SIZE_MAX;
273 for (size_t run_index = 0; run_index < line->CountRuns(); run_index++) {
274 const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
275 const auto &positions = run.GetPositions();
276 const auto &charmap = run.GetGlyphToCharMap();
277
278 auto itp = positions.begin();
279 for (auto it = charmap.begin(); it != charmap.end(); ++it, ++itp) {
280 const size_t cur_index = static_cast<size_t>(*it);
281 /* Found exact character match? */
282 if (cur_index == index) return *itp;
283
284 /* If the character we are looking for has been combined with other characters to form a ligature then
285 * we may not be able to find an exact match. We don't actually know if our character is part of a
286 * ligature. In this case we will aim to select the first character of the ligature instead, so the best
287 * index is the index nearest to but lower than the desired index. */
288 if (cur_index < index && (best_index < cur_index || best_index == SIZE_MAX)) {
289 best_index = cur_index;
290 position = &*itp;
291 }
292 }
293 }
294
295 /* At the end of the run but still didn't find our character so probably a trailing ligature, use the last found position. */
296 return *position;
297}
298
305ptrdiff_t Layouter::GetCharAtPosition(int x, size_t line_index) const
306{
307 if (line_index >= this->size()) return -1;
308
309 const auto &line = this->at(line_index);
310
311 for (size_t run_index = 0; run_index < line->CountRuns(); run_index++) {
312 const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
313 const auto &glyphs = run.GetGlyphs();
314 const auto &positions = run.GetPositions();
315 const auto &charmap = run.GetGlyphToCharMap();
316
317 for (size_t i = 0; i < run.GetGlyphCount(); i++) {
318 /* Not a valid glyph (empty). */
319 if (glyphs[i] == 0xFFFF) continue;
320
321 int begin_x = positions[i].left;
322 int end_x = positions[i].right + 1;
323
324 if (IsInsideMM(x, begin_x, end_x)) {
325 /* Found our glyph, now convert to UTF-8 string index. */
326 size_t index = charmap[i];
327
328 size_t cur_idx = 0;
329 Utf8View view(this->string);
330 for (auto it = view.begin(), end = view.end(); it != end; ++it) {
331 if (cur_idx == index) return it.GetByteOffset();
332
333 char32_t c = *it;
334 if (!IsConsumedFormattingCode(c)) cur_idx += line->GetInternalCharLength(c);
335 }
336 }
337 }
338 }
339
340 return -1;
341}
342
350{
351 FontColourMap::iterator it = fonts[size].find(colour);
352 if (it != fonts[size].end()) return it->second.get();
353
354 fonts[size][colour] = std::make_unique<Font>(size, colour);
355 return fonts[size][colour].get();
356}
357
362{
363#if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
365#endif /* WITH_ICU_I18N && WITH_HARFBUZZ */
366}
367
373{
374 fonts[size].clear();
375
376 /* We must reset the linecache since it references the just freed fonts */
378
379#if defined(WITH_UNISCRIBE)
380 UniscribeResetScriptCache(size);
381#endif
382#if defined(WITH_COCOA)
384#endif
385}
386
395{
396 if (linecache == nullptr) {
397 /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
398 linecache = std::make_unique<LineCache>(4096);
399 }
400
401 if (auto match = linecache->GetIfValid(LineCacheQuery{state, str});
402 match != nullptr) {
403 return *match;
404 }
405
406 /* Create missing entry */
407 LineCacheKey key;
408 key.state_before = state;
409 key.str.assign(str);
410 linecache->Insert(key, {});
411 return *linecache->GetIfValid(key);
412}
413
418{
419 if (linecache != nullptr) linecache->Clear();
420}
421
430ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize)
431{
432 assert(pos <= str.size());
433 auto it_ch = str.begin() + pos;
434
435 Layouter layout(str, INT32_MAX, start_fontsize);
436 return layout.GetCharPosition(it_ch);
437}
438
446ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
447{
448 if (x < 0) return -1;
449
450 Layouter layout(str, INT32_MAX, start_fontsize);
451 return layout.GetCharAtPosition(x, 0);
452}
Font cache for basic fonts.
Definition fontcache.h:22
Container with information about a font.
Definition gfx_layout.h:97
Font(FontSize size, TextColour colour)
Construct a new font.
FontCache * fc
The font we are using.
Definition gfx_layout.h:99
TextColour colour
The colour this font has to be.
Definition gfx_layout.h:100
static void InitializeLayouter()
Initialize data needed for the ICU layouter.
The layouter performs all the layout work.
Definition gfx_layout.h:230
static void Initialize()
Perform initialization of layout engine.
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition gfx_layout.h:279
Layouter(std::string_view str, int maxw=INT32_MAX, FontSize fontsize=FS_NORMAL)
Create a new layouter.
ptrdiff_t GetCharAtPosition(int x, size_t line_index) const
Get the character that is at a pixel position in the first line of the layouted text.
static void ResetFontCache(FontSize size)
Reset cached font information.
ParagraphLayouter::Position GetCharPosition(std::string_view::const_iterator ch) const
Get the position of a character in the layout.
static std::unique_ptr< LineCache > linecache
Cache of ParagraphLayout lines.
Definition gfx_layout.h:274
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
std::string_view string
Pointer to the original string.
Definition gfx_layout.h:231
static void ResetLineCache()
Clear line cache.
Dimension GetBounds()
Get the boundaries of this paragraph.
static LineCacheItem & GetCachedParagraphLayout(std::string_view str, const FontState &state)
Get reference to cache item.
Position of a glyph within a VisualRun.
Definition gfx_layout.h:116
Visual run contains data about the bit of text with the same font.
Definition gfx_layout.h:132
virtual std::span< const GlyphID > GetGlyphs() const =0
Get the glyphs to draw.
virtual std::span< const int > GetGlyphToCharMap() const =0
The offset for each of the glyphs to the character run that was passed to the Layouter.
virtual std::span< const Position > GetPositions() const =0
Get the positions for each of the glyphs.
virtual size_t GetGlyphCount() const =0
Get the number of glyphs.
Constant span of UTF-8 encoded data.
Definition utf8.hpp:28
iterator GetIterAtByte(size_t offset) const
Create iterator pointing at codepoint, which occupies the byte position "offset".
Definition utf8.cpp:81
Control codes that are embedded in the translation strings.
Functions related to debugging.
ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize)
Get the leading corner of a character in a single-line string relative to the start of the string.
static bool IsConsumedFormattingCode(char32_t ch)
Test whether a character is a non-printable formatting code.
static void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
Helper for getting a ParagraphLayouter of the given type.
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
Get the character from a string that is drawn at a specific position.
Functions related to laying out the texts.
std::vector< std::pair< int, Font * > > FontMap
Mapping from index to font.
Definition gfx_layout.h:106
Functions related to laying out the texts as fallback.
Functions related to laying out the texts with ICU.
FontSize
Available font sizes.
Definition gfx_type.h:248
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
#define Point
Macro that prevents name conflicts between included headers.
Integer math functions.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
bool IsTextDirectionChar(char32_t c)
Is the given character a text direction character.
void MacOSResetScriptCache(FontSize size)
Delete CoreText font reference for a specific font size.
Functions related to localized text support on OSX.
Functions related to laying out text on Win32.
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
Functions related to OTTD's strings.
@ TD_LTR
Text is written left-to-right by default.
Dimensions (a width and height) of a rectangle in 2D.
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition gfx_layout.h:24
void PushColour()
Push the current colour on to the stack.
Definition gfx_layout.h:58
void SetColour(TextColour c)
Switch to new colour c.
Definition gfx_layout.h:38
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition gfx_layout.h:48
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition gfx_layout.h:67
FontSize fontsize
Current font size.
Definition gfx_layout.h:25
TextColour cur_colour
Current text colour.
Definition gfx_layout.h:26
Item in the linecache.
Definition gfx_layout.h:259
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:264
Buffer buffer
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:263
int cached_width
Width used for the cached layout.
Definition gfx_layout.h:270
std::vector< std::unique_ptr< const ParagraphLayouter::Line > > cached_layout
Cached results of line layouting.
Definition gfx_layout.h:269
FontState state_after
Font state after the line.
Definition gfx_layout.h:266
std::unique_ptr< ParagraphLayouter > layout
Layout of the line.
Definition gfx_layout.h:267
Key into the linecache.
Definition gfx_layout.h:234
FontState state_before
Font state at the beginning of the line.
Definition gfx_layout.h:235
std::string str
Source string of the line (including colour and font size codes).
Definition gfx_layout.h:236
Handling of UTF-8 encoded data.