OpenTTD Source  20240917-master-g9ab0a47812
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 <http://www.gnu.org/licenses/>.
6  */
7 
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 "debug.h"
16 
17 #include "table/control_codes.h"
18 
19 #include "gfx_layout_fallback.h"
20 
21 #if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
22 #include "gfx_layout_icu.h"
23 #endif /* WITH_ICU_I18N && WITH_HARFBUZZ */
24 
25 #ifdef WITH_UNISCRIBE
27 #endif /* WITH_UNISCRIBE */
28 
29 #ifdef WITH_COCOA
30 #include "os/macosx/string_osx.h"
31 #endif
32 
33 #include "safeguards.h"
34 
35 
37 Layouter::LineCache *Layouter::linecache;
38 
40 Layouter::FontColourMap Layouter::fonts[FS_END];
41 
42 
49  fc(FontCache::Get(size)), colour(colour)
50 {
51  assert(size < FS_END);
52 }
53 
63 template <typename T>
64 static inline void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
65 {
66  if (line.buffer != nullptr) free(line.buffer);
67 
68  typename T::CharType *buff_begin = MallocT<typename T::CharType>(str.size() + 1);
69  const typename T::CharType *buffer_last = buff_begin + str.size() + 1;
70  typename T::CharType *buff = buff_begin;
71  FontMap &fontMapping = line.runs;
72  Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
73 
74  line.buffer = buff_begin;
75  fontMapping.clear();
76 
77  auto cur = str.begin();
78 
79  /*
80  * Go through the whole string while adding Font instances to the font map
81  * whenever the font changes, and convert the wide characters into a format
82  * usable by ParagraphLayout.
83  */
84  for (; buff < buffer_last && cur != str.end();) {
85  char32_t c = Utf8Consume(cur);
86  if (c == '\0' || c == '\n') {
87  /* Caller should already have filtered out these characters. */
88  NOT_REACHED();
89  } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
90  state.SetColour((TextColour)(c - SCC_BLUE));
91  } else if (c == SCC_PUSH_COLOUR) {
92  state.PushColour();
93  } else if (c == SCC_POP_COLOUR) {
94  state.PopColour();
95  } else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
96  state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
97  } else {
98  /* Filter out non printable characters */
99  if (!IsPrintable(c)) continue;
100  /* Filter out text direction characters that shouldn't be drawn, and
101  * will not be handled in the fallback case because they are mostly
102  * needed for RTL languages which need more proper shaping support. */
103  if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
104  buff += T::AppendToBuffer(buff, buffer_last, c);
105  continue;
106  }
107 
108  if (fontMapping.count(buff - buff_begin) == 0) {
109  fontMapping[buff - buff_begin] = f;
110  }
111  f = Layouter::GetFont(state.fontsize, state.cur_colour);
112  }
113 
114  /* Better safe than sorry. */
115  *buff = '\0';
116 
117  if (fontMapping.count(buff - buff_begin) == 0) {
118  fontMapping[buff - buff_begin] = f;
119  }
120  line.layout = T::GetParagraphLayout(buff_begin, buff, fontMapping);
121  line.state_after = state;
122 }
123 
130 Layouter::Layouter(std::string_view str, int maxw, FontSize fontsize) : string(str)
131 {
132  FontState state(TC_INVALID, fontsize);
133 
134  while (true) {
135  auto line_length = str.find_first_of('\n');
136  auto str_line = str.substr(0, line_length);
137 
138  LineCacheItem &line = GetCachedParagraphLayout(str_line, state);
139  if (line.layout != nullptr) {
140  state = line.state_after;
141  line.layout->Reflow();
142  } else {
143  /* Line is new, layout it */
144  FontState old_state = state;
145 
146 #if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
147  if (line.layout == nullptr) {
148  GetLayouter<ICUParagraphLayoutFactory>(line, str_line, state);
149  if (line.layout == nullptr) {
150  state = old_state;
151  }
152  }
153 #endif
154 
155 #ifdef WITH_UNISCRIBE
156  if (line.layout == nullptr) {
157  GetLayouter<UniscribeParagraphLayoutFactory>(line, str_line, state);
158  if (line.layout == nullptr) {
159  state = old_state;
160  }
161  }
162 #endif
163 
164 #ifdef WITH_COCOA
165  if (line.layout == nullptr) {
166  GetLayouter<CoreTextParagraphLayoutFactory>(line, str_line, state);
167  if (line.layout == nullptr) {
168  state = old_state;
169  }
170  }
171 #endif
172 
173  if (line.layout == nullptr) {
174  GetLayouter<FallbackParagraphLayoutFactory>(line, str_line, state);
175  }
176  }
177 
178  /* Move all lines into a local cache so we can reuse them later on more easily. */
179  for (;;) {
180  auto l = line.layout->NextLine(maxw);
181  if (l == nullptr) break;
182  this->push_back(std::move(l));
183  }
184 
185  /* Break out if this was the last line. */
186  if (line_length == std::string_view::npos) {
187  break;
188  }
189 
190  /* Go to the next line. */
191  str.remove_prefix(line_length + 1);
192  }
193 }
194 
200 {
201  Dimension d = { 0, 0 };
202  for (const auto &l : *this) {
203  d.width = std::max<uint>(d.width, l->GetWidth());
204  d.height += l->GetLeading();
205  }
206  return d;
207 }
208 
212 static bool IsConsumedFormattingCode(char32_t ch)
213 {
214  if (ch >= SCC_BLUE && ch <= SCC_BLACK) return true;
215  if (ch == SCC_PUSH_COLOUR) return true;
216  if (ch == SCC_POP_COLOUR) return true;
217  if (ch >= SCC_FIRST_FONT && ch <= SCC_LAST_FONT) return true;
218  // All other characters defined in Unicode standard are assumed to be non-consumed.
219  return false;
220 }
221 
228 ParagraphLayouter::Position Layouter::GetCharPosition(std::string_view::const_iterator ch) const
229 {
230  const auto &line = this->front();
231 
232  /* Pointer to the end-of-string marker? Return total line width. */
233  if (ch == this->string.end()) {
234  Point p = {_current_text_dir == TD_LTR ? line->GetWidth() : 0, 0};
235  return p;
236  }
237 
238  /* Find the code point index which corresponds to the char
239  * pointer into our UTF-8 source string. */
240  size_t index = 0;
241  auto str = this->string.begin();
242  while (str < ch) {
243  char32_t c = Utf8Consume(str);
244  if (!IsConsumedFormattingCode(c)) index += line->GetInternalCharLength(c);
245  }
246 
247  /* Initial position, returned if character not found. */
248  const ParagraphLayouter::Position initial_position = Point{_current_text_dir == TD_LTR ? 0 : line->GetWidth(), 0};
249  const ParagraphLayouter::Position *position = &initial_position;
250 
251  /* We couldn't find the code point index. */
252  if (str != ch) return *position;
253 
254  /* Valid character. */
255 
256  /* Scan all runs until we've found our code point index. */
257  size_t best_index = SIZE_MAX;
258  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
259  const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
260  const auto &positions = run.GetPositions();
261  const auto &charmap = run.GetGlyphToCharMap();
262 
263  auto itp = positions.begin();
264  for (auto it = charmap.begin(); it != charmap.end(); ++it, ++itp) {
265  const size_t cur_index = static_cast<size_t>(*it);
266  /* Found exact character match? */
267  if (cur_index == index) return *itp;
268 
269  /* If the character we are looking for has been combined with other characters to form a ligature then
270  * we may not be able to find an exact match. We don't actually know if our character is part of a
271  * ligature. In this case we will aim to select the first character of the ligature instead, so the best
272  * index is the index nearest to but lower than the desired index. */
273  if (cur_index < index && (best_index < cur_index || best_index == SIZE_MAX)) {
274  best_index = cur_index;
275  position = &*itp;
276  }
277  }
278  }
279 
280  /* At the end of the run but still didn't find our character so probably a trailing ligature, use the last found position. */
281  return *position;
282 }
283 
290 ptrdiff_t Layouter::GetCharAtPosition(int x, size_t line_index) const
291 {
292  if (line_index >= this->size()) return -1;
293 
294  const auto &line = this->at(line_index);
295 
296  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
297  const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
298  const auto &glyphs = run.GetGlyphs();
299  const auto &positions = run.GetPositions();
300  const auto &charmap = run.GetGlyphToCharMap();
301 
302  for (int i = 0; i < run.GetGlyphCount(); i++) {
303  /* Not a valid glyph (empty). */
304  if (glyphs[i] == 0xFFFF) continue;
305 
306  int begin_x = positions[i].left;
307  int end_x = positions[i].right + 1;
308 
309  if (IsInsideMM(x, begin_x, end_x)) {
310  /* Found our glyph, now convert to UTF-8 string index. */
311  size_t index = charmap[i];
312 
313  size_t cur_idx = 0;
314  for (auto str = this->string.begin(); str != this->string.end();) {
315  if (cur_idx == index) return str - this->string.begin();
316 
317  char32_t c = Utf8Consume(str);
318  if (!IsConsumedFormattingCode(c)) cur_idx += line->GetInternalCharLength(c);
319  }
320  }
321  }
322  }
323 
324  return -1;
325 }
326 
331 {
332  FontColourMap::iterator it = fonts[size].find(colour);
333  if (it != fonts[size].end()) return it->second.get();
334 
335  fonts[size][colour] = std::make_unique<Font>(size, colour);
336  return fonts[size][colour].get();
337 }
338 
343 {
344 #if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
346 #endif /* WITH_ICU_I18N && WITH_HARFBUZZ */
347 }
348 
354 {
355  fonts[size].clear();
356 
357  /* We must reset the linecache since it references the just freed fonts */
358  ResetLineCache();
359 
360 #if defined(WITH_UNISCRIBE)
361  UniscribeResetScriptCache(size);
362 #endif
363 #if defined(WITH_COCOA)
364  MacOSResetScriptCache(size);
365 #endif
366 }
367 
376 {
377  if (linecache == nullptr) {
378  /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
379  linecache = new LineCache();
380  }
381 
382  if (auto match = linecache->find(LineCacheQuery{state, str});
383  match != linecache->end()) {
384  return match->second;
385  }
386 
387  /* Create missing entry */
388  LineCacheKey key;
389  key.state_before = state;
390  key.str.assign(str);
391  return (*linecache)[std::move(key)];
392 }
393 
398 {
399  if (linecache != nullptr) linecache->clear();
400 }
401 
406 {
407  if (linecache != nullptr) {
408  /* TODO LRU cache would be fancy, but not exactly necessary */
409  if (linecache->size() > 4096) ResetLineCache();
410  }
411 }
412 
421 ParagraphLayouter::Position GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize)
422 {
423  /* Ensure "ch" is inside "str" or at the exact end. */
424  assert(ch >= str.data() && (ch - str.data()) <= static_cast<ptrdiff_t>(str.size()));
425  auto it_ch = str.begin() + (ch - str.data());
426 
427  Layouter layout(str, INT32_MAX, start_fontsize);
428  return layout.GetCharPosition(it_ch);
429 }
430 
438 ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
439 {
440  if (x < 0) return -1;
441 
442  Layouter layout(str, INT32_MAX, start_fontsize);
443  return layout.GetCharAtPosition(x, 0);
444 }
Layouter::LineCacheKey
Key into the linecache.
Definition: gfx_layout.h:142
Layouter::LineCacheItem
Item in the linecache.
Definition: gfx_layout.h:168
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
IsInsideMM
constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition: math_func.hpp:268
TD_LTR
@ TD_LTR
Text is written left-to-right by default.
Definition: strings_type.h:23
FontState::PopColour
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:47
GetCharPosInString
ParagraphLayouter::Position GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize)
Get the leading corner of a character in a single-line string relative to the start of the string.
Definition: gfx_layout.cpp:421
math_func.hpp
Layouter::GetCharPosition
ParagraphLayouter::Position GetCharPosition(std::string_view::const_iterator ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:228
Layouter::linecache
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:181
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:260
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:353
gfx_layout_icu.h
Layouter::GetCachedParagraphLayout
static LineCacheItem & GetCachedParagraphLayout(std::string_view str, const FontState &state)
Get reference to cache item.
Definition: gfx_layout.cpp:375
Layouter::LineCacheItem::layout
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:174
control_codes.h
string_osx.h
Layouter::LineCacheKey::str
std::string str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:144
Layouter::Layouter
Layouter(std::string_view str, int maxw=INT32_MAX, FontSize fontsize=FS_NORMAL)
Create a new layouter.
Definition: gfx_layout.cpp:130
Layouter::fonts
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition: gfx_layout.h:186
Layouter::LineCacheKey::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:143
Layouter::LineCacheItem::runs
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:171
FontState::fontsize
FontSize fontsize
Current font size.
Definition: gfx_layout.h:25
gfx_layout_fallback.h
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:334
IsTextDirectionChar
bool IsTextDirectionChar(char32_t c)
Is the given character a text direction character.
Definition: string_func.h:217
GetCharAtPosition
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.
Definition: gfx_layout.cpp:438
Layouter::GetBounds
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:199
Layouter
The layouter performs all the layout work.
Definition: gfx_layout.h:138
Font::Font
Font(FontSize size, TextColour colour)
Construct a new font.
Definition: gfx_layout.cpp:48
safeguards.h
Layouter::LineCacheQuery
Definition: gfx_layout.h:147
Layouter::GetFont
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition: gfx_layout.cpp:330
gfx_layout.h
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
stdafx.h
Font
Container with information about a font.
Definition: gfx_layout.h:75
Layouter::ReduceLineCache
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
Definition: gfx_layout.cpp:405
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:107
FontState::cur_colour
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:26
string_func.h
strings_func.h
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
ICUParagraphLayoutFactory::InitializeLayouter
static void InitializeLayouter()
Initialize data needed for the ICU layouter.
Definition: gfx_layout_icu.cpp:385
ParagraphLayouter::Position
Position of a glyph within a VisualRun.
Definition: gfx_layout.h:94
GetLayouter
static void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
Helper for getting a ParagraphLayouter of the given type.
Definition: gfx_layout.cpp:64
FontState::SetColour
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:37
Layouter::LineCacheItem::state_after
FontState state_after
Font state after the line.
Definition: gfx_layout.h:173
FontState::SetFontSize
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:66
FontState
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:24
Layouter::ResetLineCache
static void ResetLineCache()
Clear line cache.
Definition: gfx_layout.cpp:397
IsConsumedFormattingCode
static bool IsConsumedFormattingCode(char32_t ch)
Test whether a character is a non-printable formatting code.
Definition: gfx_layout.cpp:212
MacOSResetScriptCache
void MacOSResetScriptCache(FontSize size)
Delete CoreText font reference for a specific font size.
Definition: string_osx.cpp:297
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:208
FontMap
std::map< int, Font * > FontMap
Mapping from index to font.
Definition: gfx_layout.h:84
Layouter::Initialize
static void Initialize()
Perform initialization of layout engine.
Definition: gfx_layout.cpp:342
FontState::PushColour
void PushColour()
Push the current colour on to the stack.
Definition: gfx_layout.h:57
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:56
Layouter::LineCacheItem::buffer
void * buffer
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:170
debug.h
string_uniscribe.h
Layouter::GetCharAtPosition
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.
Definition: gfx_layout.cpp:290