OpenTTD Source 20261112-master-gbcbec5e63f
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
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
31#endif
32
33#include "safeguards.h"
34
35
37std::unique_ptr<Layouter::LineCache> Layouter::linecache;
38
48template <typename T>
49static inline void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
50{
51 typename T::CharType *buff_begin = new typename T::CharType[str.size() + 1];
52 /* Move ownership of buff_begin into the Buffer/unique_ptr. */
53 line.buffer = Layouter::LineCacheItem::Buffer(buff_begin, [](void *p) { delete[] reinterpret_cast<T::CharType *>(p); });
54
55 const typename T::CharType *buffer_last = buff_begin + str.size() + 1;
56 typename T::CharType *buff = buff_begin;
57 FontMap &font_mapping = line.runs;
58 Font f{state};
59
60 font_mapping.clear();
61
62 /*
63 * Go through the whole string while adding Font instances to the font map
64 * whenever the font changes, and convert the wide characters into a format
65 * usable by ParagraphLayout.
66 */
67 for (char32_t c : Utf8View(str)) {
68 if (c == '\0' || c == '\n') {
69 /* Caller should already have filtered out these characters. */
70 NOT_REACHED();
71 } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
72 state.SetColour((TextColour)(c - SCC_BLUE));
73 } else if (c == SCC_PUSH_COLOUR) {
74 state.PushColour();
75 } else if (c == SCC_POP_COLOUR) {
76 state.PopColour();
77 } else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
78 state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
79 } else {
80 /* Filter out non printable characters */
81 if (!IsPrintable(c)) continue;
82 /* Filter out text direction characters that shouldn't be drawn, and
83 * will not be handled in the fallback case because they are mostly
84 * needed for RTL languages which need more proper shaping support. */
85 if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
86 buff += T::AppendToBuffer(buff, buffer_last, c);
87 if (buff >= buffer_last) break;
88 continue;
89 }
90
91 if (font_mapping.empty() || font_mapping.back().first != buff - buff_begin) {
92 font_mapping.emplace_back(buff - buff_begin, f);
93 }
94 f = state;
95 }
96
97 /* Better safe than sorry. */
98 *buff = '\0';
99
100 if (font_mapping.empty() || font_mapping.back().first != buff - buff_begin) {
101 font_mapping.emplace_back(buff - buff_begin, f);
102 }
103 line.layout = T::GetParagraphLayout(buff_begin, buff, font_mapping);
104 line.state_after = state;
105}
106
113Layouter::Layouter(std::string_view str, int maxw, FontSize fontsize) : string(str)
114{
115 FontState state(fontsize, TextColour::Invalid);
116
117 while (true) {
118 auto line_length = str.find_first_of('\n');
119 auto str_line = str.substr(0, line_length);
120
121 LineCacheItem &line = GetCachedParagraphLayout(str_line, state);
122 if (line.layout != nullptr) {
123 state = line.state_after;
124 line.layout->Reflow();
125 } else {
126 /* Line is new, layout it */
127 FontState old_state = state;
128
129#if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
130 if (line.layout == nullptr) {
131 GetLayouter<ICUParagraphLayoutFactory>(line, str_line, state);
132 if (line.layout == nullptr) {
133 state = std::move(old_state);
134 }
135 }
136#endif
137
138#ifdef WITH_UNISCRIBE
139 if (line.layout == nullptr) {
141 if (line.layout == nullptr) {
142 state = std::move(old_state);
143 }
144 }
145#endif
146
147#ifdef WITH_COCOA
148 if (line.layout == nullptr) {
149 GetLayouter<CoreTextParagraphLayoutFactory>(line, str_line, state);
150 if (line.layout == nullptr) {
151 state = std::move(old_state);
152 }
153 }
154#endif
155
156 if (line.layout == nullptr) {
157 GetLayouter<FallbackParagraphLayoutFactory>(line, str_line, state);
158 }
159 }
160
161 if (line.cached_width != maxw) {
162 /* First run or width has changed, so we need to go through the layouter. Lines are moved into a cache to
163 * be reused if the width is not changed. */
164 line.cached_layout.clear();
165 line.cached_width = maxw;
166 for (;;) {
167 auto l = line.layout->NextLine(maxw);
168 if (l == nullptr) break;
169 line.cached_layout.push_back(std::move(l));
170 }
171 }
172
173 /* Retrieve layout from the cache. */
174 for (const auto &l : line.cached_layout) {
175 this->push_back(l.get());
176 }
177
178 /* Break out if this was the last line. */
179 if (line_length == std::string_view::npos) {
180 break;
181 }
182
183 /* Go to the next line. */
184 str.remove_prefix(line_length + 1);
185 }
186}
187
193{
194 Dimension d = { 0, 0 };
195 for (const auto &l : *this) {
196 d.width = std::max<uint>(d.width, l->GetWidth());
197 d.height += l->GetLeading();
198 }
199 return d;
200}
201
207static bool IsConsumedFormattingCode(char32_t ch)
208{
209 if (ch >= SCC_BLUE && ch <= SCC_BLACK) return true;
210 if (ch == SCC_PUSH_COLOUR) return true;
211 if (ch == SCC_POP_COLOUR) return true;
212 if (ch >= SCC_FIRST_FONT && ch <= SCC_LAST_FONT) return true;
213 /* All other characters defined in Unicode standard are assumed to be non-consumed. */
214 return false;
215}
216
223ParagraphLayouter::Position Layouter::GetCharPosition(std::string_view::const_iterator ch) const
224{
225 const auto &line = this->front();
226
227 /* Pointer to the end-of-string marker? Return total line width. */
228 if (ch == this->string.end()) {
229 Point p = {_current_text_dir == TD_LTR ? line->GetWidth() : 0, 0};
230 return p;
231 }
232
233 /* Initial position, returned if character not found. */
234 const ParagraphLayouter::Position initial_position = Point{_current_text_dir == TD_LTR ? 0 : line->GetWidth(), 0};
235
236 /* Find the code point index which corresponds to the char
237 * pointer into our UTF-8 source string. */
238 size_t index = 0;
239 {
240 Utf8View view(this->string);
241 const size_t offset = ch - this->string.begin();
242 const auto pos = view.GetIterAtByte(offset);
243
244 /* We couldn't find the code point index. */
245 if (pos.GetByteOffset() != offset) return initial_position;
246
247 for (auto it = view.begin(); it < pos; ++it) {
248 char32_t c = *it;
249 if (!IsConsumedFormattingCode(c)) index += line->GetInternalCharLength(c);
250 }
251 }
252
253 const ParagraphLayouter::Position *position = &initial_position;
254
255 /* Scan all runs until we've found our code point index. */
256 size_t best_index = SIZE_MAX;
257 for (size_t run_index = 0; run_index < line->CountRuns(); run_index++) {
258 const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
259 const auto &positions = run.GetPositions();
260 const auto &charmap = run.GetGlyphToCharMap();
261
262 auto itp = positions.begin();
263 for (auto it = charmap.begin(); it != charmap.end(); ++it, ++itp) {
264 const size_t cur_index = static_cast<size_t>(*it);
265 /* Found exact character match? */
266 if (cur_index == index) return *itp;
267
268 /* If the character we are looking for has been combined with other characters to form a ligature then
269 * we may not be able to find an exact match. We don't actually know if our character is part of a
270 * ligature. In this case we will aim to select the first character of the ligature instead, so the best
271 * index is the index nearest to but lower than the desired index. */
272 if (cur_index < index && (best_index < cur_index || best_index == SIZE_MAX)) {
273 best_index = cur_index;
274 position = &*itp;
275 }
276 }
277 }
278
279 /* At the end of the run but still didn't find our character so probably a trailing ligature, use the last found position. */
280 return *position;
281}
282
289ptrdiff_t Layouter::GetCharAtPosition(int x, size_t line_index) const
290{
291 if (line_index >= this->size()) return -1;
292
293 const auto &line = this->at(line_index);
294
295 for (size_t run_index = 0; run_index < line->CountRuns(); run_index++) {
296 const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
297 const auto &glyphs = run.GetGlyphs();
298 const auto &positions = run.GetPositions();
299 const auto &charmap = run.GetGlyphToCharMap();
300
301 for (size_t i = 0; i < run.GetGlyphCount(); i++) {
302 /* Not a valid glyph (empty). */
303 if (glyphs[i] == 0xFFFF) continue;
304
305 int begin_x = positions[i].left;
306 int end_x = positions[i].right + 1;
307
308 if (IsInsideMM(x, begin_x, end_x)) {
309 /* Found our glyph, now convert to UTF-8 string index. */
310 size_t index = charmap[i];
311
312 size_t cur_idx = 0;
313 Utf8View view(this->string);
314 for (auto it = view.begin(), end = view.end(); it != end; ++it) {
315 if (cur_idx == index) return it.GetByteOffset();
316
317 char32_t c = *it;
318 if (!IsConsumedFormattingCode(c)) cur_idx += line->GetInternalCharLength(c);
319 }
320 }
321 }
322 }
323
324 return -1;
325}
326
331{
332#if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
334#endif /* WITH_ICU_I18N && WITH_HARFBUZZ */
335}
336
341void Layouter::ResetFontCache([[maybe_unused]] FontSize size)
342{
343 /* We must reset the linecache since it references the just freed fonts */
345
346#if defined(WITH_UNISCRIBE)
347 UniscribeResetScriptCache(size);
348#endif
349}
350
359{
360 if (linecache == nullptr) {
361 /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
362 linecache = std::make_unique<LineCache>(4096);
363 }
364
365 if (auto match = linecache->GetIfValid(LineCacheQuery{state, str});
366 match != nullptr) {
367 return *match;
368 }
369
370 /* Create missing entry */
371 LineCacheKey key;
372 key.state_before = state;
373 key.str.assign(str);
374 linecache->Insert(key, {});
375 return *linecache->GetIfValid(key);
376}
377
382{
383 if (linecache != nullptr) linecache->Clear();
384}
385
394ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize)
395{
396 assert(pos <= str.size());
397 auto it_ch = str.begin() + pos;
398
399 Layouter layout(str, INT32_MAX, start_fontsize);
400 return layout.GetCharPosition(it_ch);
401}
402
410ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize)
411{
412 if (x < 0) return -1;
413
414 Layouter layout(str, INT32_MAX, start_fontsize);
415 return layout.GetCharAtPosition(x, 0);
416}
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition gfx_layout.h:43
void PushColour()
Push the current colour on to the stack.
Definition gfx_layout.h:87
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition gfx_layout.h:77
void SetColour(ExtendedTextColour c)
Switch to new colour c.
Definition gfx_layout.h:67
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition gfx_layout.h:96
Container with information about a font.
Definition gfx_layout.h:21
static void InitializeLayouter()
Initialize data needed for the ICU layouter.
The layouter performs all the layout work.
Definition gfx_layout.h:266
Layouter(std::string_view str, int maxw=INT32_MAX, FontSize fontsize=FontSize::Normal)
Create a new layouter.
static void Initialize()
Perform initialization of layout engine.
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:310
std::string_view string
Pointer to the original string.
Definition gfx_layout.h:267
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:150
Visual run contains data about the bit of text with the same font.
Definition gfx_layout.h:166
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.
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:139
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:315
@ Invalid
Invalid colour.
Definition gfx_type.h:336
#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.
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:55
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.
value_type height
The height of the rectangle.
value_type width
The width of the rectangle.
Item in the linecache.
Definition gfx_layout.h:295
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:300
Buffer buffer
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:299
int cached_width
Width used for the cached layout.
Definition gfx_layout.h:306
std::vector< std::unique_ptr< const ParagraphLayouter::Line > > cached_layout
Cached results of line layouting.
Definition gfx_layout.h:305
FontState state_after
Font state after the line.
Definition gfx_layout.h:302
std::unique_ptr< void, void(*)(void *)> Buffer
Due to the type of data in the buffer differing depending on the Layouter, we need to pass our own de...
Definition gfx_layout.h:297
std::unique_ptr< ParagraphLayouter > layout
Layout of the line.
Definition gfx_layout.h:303
Key into the linecache.
Definition gfx_layout.h:270
FontState state_before
Font state at the beginning of the line.
Definition gfx_layout.h:271
std::string str
Source string of the line (including colour and font size codes).
Definition gfx_layout.h:272
Handling of UTF-8 encoded data.