OpenTTD Source: src/gfx_layout.cpp Source File

OpenTTD Source 14.0-beta3

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 "debug.h"
15
16 #include "table/control_codes.h"
17
18 #include "gfx_layout_fallback.h"
19
20 #if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
21 #include "gfx_layout_icu.h"
22 #endif /* WITH_ICU_I18N && WITH_HARFBUZZ */
23
24 #ifdef WITH_UNISCRIBE
25 #include "os/windows/string_uniscribe.h"
26 #endif /* WITH_UNISCRIBE */
27
28 #ifdef WITH_COCOA
29 #include "os/macosx/string_osx.h"
30 #endif
31
32 #include "safeguards.h"
33
34
36 Layouter::LineCache *Layouter::linecache;
37
39 Layouter::FontColourMap Layouter::fonts[FS_END];
40
41
47 Font::Font(FontSize size, TextColour colour) :
48 fc(FontCache::Get(size)), colour(colour)
49 {
50 assert(size < FS_END);
51 }
52
62 template <typename T>
63 static inline void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
64 {
65 if (line.buffer != nullptr) free(line.buffer);
66
67 typename T::CharType *buff_begin = MallocT<typename T::CharType>(str.size() + 1);
68 const typename T::CharType *buffer_last = buff_begin + str.size() + 1;
69 typename T::CharType *buff = buff_begin;
70 FontMap &fontMapping = line.runs;
71 Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
72
73 line.buffer = buff_begin;
74 fontMapping.clear();
75
76 auto cur = str.begin();
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 (; buff < buffer_last && cur != str.end();) {
84 char32_t c = Utf8Consume(cur);
85 if (c == '\0' || c == '\n') {
86 /* Caller should already have filtered out these characters. */
87 NOT_REACHED();
88 } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
89 state.SetColour((TextColour)(c - SCC_BLUE));
90 } else if (c == SCC_PUSH_COLOUR) {
91 state.PushColour();
92 } else if (c == SCC_POP_COLOUR) {
93 state.PopColour();
94 } else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
95 state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
96 } else {
97 /* Filter out non printable characters */
98 if (!IsPrintable(c)) continue;
99 /* Filter out text direction characters that shouldn't be drawn, and
100 * will not be handled in the fallback case because they are mostly
101 * needed for RTL languages which need more proper shaping support. */
102 if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
103 buff += T::AppendToBuffer(buff, buffer_last, c);
104 continue;
105 }
106
107 if (fontMapping.count(buff - buff_begin) == 0) {
108 fontMapping[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 (fontMapping.count(buff - buff_begin) == 0) {
117 fontMapping[buff - buff_begin] = f;
118 }
119 line.layout = T::GetParagraphLayout(buff_begin, buff, fontMapping);
120 line.state_after = state;
121 }
122
130 Layouter::Layouter(std::string_view str, int maxw, TextColour colour, FontSize fontsize) : string(str)
131 {
132 FontState state(colour, 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
199 Dimension Layouter::GetBounds()
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 Point 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 = { line->GetWidth(), 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 static const std::vector<Point> zero = { {0, 0} };
249 auto position = zero.begin();
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 for (int 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 /* Run starts after our character, use the last found position. */
263 if ((size_t)charmap.front() > index) return *position;
264
265 position = positions.begin();
266 for (auto it = charmap.begin(); it != charmap.end(); /* nothing */) {
267 /* Plain honest-to-$deity match. */
268 if ((size_t)*it == index) return *position;
269 ++it;
270 if (it == charmap.end()) break;
271
272 /* We just passed our character, it's probably a ligature, use the last found position. */
273 if ((size_t)*it > index) return *position;
274 ++position;
275 }
276 }
277
278 /* At the end of the run but still didn't find our character so probably a trailing ligature, use the last found position. */
279 return *position;
280 }
281
288 ptrdiff_t Layouter::GetCharAtPosition(int x, size_t line_index) const
289 {
290 if (line_index >= this->size()) return -1;
291
292 const auto &line = this->at(line_index);
293
294 for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
295 const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
296 const auto &glyphs = run.GetGlyphs();
297 const auto &positions = run.GetPositions();
298 const auto &charmap = run.GetGlyphToCharMap();
299
300 for (int i = 0; i < run.GetGlyphCount(); i++) {
301 /* Not a valid glyph (empty). */
302 if (glyphs[i] == 0xFFFF) continue;
303
304 int begin_x = positions[i].x;
305 int end_x = positions[i + 1].x;
306
307 if (IsInsideMM(x, begin_x, end_x)) {
308 /* Found our glyph, now convert to UTF-8 string index. */
309 size_t index = charmap[i];
310
311 size_t cur_idx = 0;
312 for (auto str = this->string.begin(); str != this->string.end();) {
313 if (cur_idx == index) return str - this->string.begin();
314
315 char32_t c = Utf8Consume(str);
316 if (!IsConsumedFormattingCode(c)) cur_idx += line->GetInternalCharLength(c);
317 }
318 }
319 }
320 }
321
322 return -1;
323 }
324
328 Font *Layouter::GetFont(FontSize size, TextColour colour)
329 {
330 FontColourMap::iterator it = fonts[size].find(colour);
331 if (it != fonts[size].end()) return it->second.get();
332
333 fonts[size][colour] = std::make_unique<Font>(size, colour);
334 return fonts[size][colour].get();
335 }
336
340 void Layouter::Initialize()
341 {
342 #if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
343 ICUParagraphLayoutFactory::InitializeLayouter();
344 #endif /* WITH_ICU_I18N && WITH_HARFBUZZ */
345 }
346
351 void Layouter::ResetFontCache(FontSize size)
352 {
353 fonts[size].clear();
354
355 /* We must reset the linecache since it references the just freed fonts */
356 ResetLineCache();
357
358 #if defined(WITH_UNISCRIBE)
359 UniscribeResetScriptCache(size);
360 #endif
361 #if defined(WITH_COCOA)
362 MacOSResetScriptCache(size);
363 #endif
364 }
365
373 Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(std::string_view str, const FontState &state)
374 {
375 if (linecache == nullptr) {
376 /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
377 linecache = new LineCache();
378 }
379
380 if (auto match = linecache->find(LineCacheQuery{state, str});
381 match != linecache->end()) {
382 return match->second;
383 }
384
385 /* Create missing entry */
386 LineCacheKey key;
387 key.state_before = state;
388 key.str.assign(str);
389 return (*linecache)[key];
390 }
391
395 void Layouter::ResetLineCache()
396 {
397 if (linecache != nullptr) linecache->clear();
398 }
399
403 void Layouter::ReduceLineCache()
404 {
405 if (linecache != nullptr) {
406 /* TODO LRU cache would be fancy, but not exactly necessary */
407 if (linecache->size() > 4096) ResetLineCache();
408 }
409 }
Layouter::LineCacheKey
Key into the linecache.
Definition: gfx_layout.h:129
Layouter::LineCacheItem
Item in the linecache.
Definition: gfx_layout.h:155
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
FontState::PopColour
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:47
math_func.hpp
Layouter::linecache
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:168
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:253
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:351
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:373
Layouter::LineCacheItem::layout
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:161
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:131
Layouter::fonts
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition: gfx_layout.h:173
Layouter::LineCacheKey::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:130
Layouter::LineCacheItem::runs
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:158
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:379
IsTextDirectionChar
bool IsTextDirectionChar(char32_t c)
Is the given character a text direction character.
Definition: string_func.h:216
Layouter::GetBounds
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:199
Font::Font
Font(FontSize size, TextColour colour)
Construct a new font.
Definition: gfx_layout.cpp:47
safeguards.h
Layouter::LineCacheQuery
Definition: gfx_layout.h:134
Layouter::GetFont
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition: gfx_layout.cpp:328
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:403
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:94
FontState::cur_colour
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:26
string_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:389
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:63
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:160
Layouter::Layouter
Layouter(std::string_view str, int maxw=INT32_MAX, TextColour colour=TC_FROMSTRING, FontSize fontsize=FS_NORMAL)
Create a new layouter.
Definition: gfx_layout.cpp:130
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:395
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:296
Layouter::GetCharPosition
Point GetCharPosition(std::string_view::const_iterator ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:228
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:202
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:340
FontState::PushColour
void PushColour()
Push the current colour on to the stack.
Definition: gfx_layout.h:57
Layouter::LineCacheItem::buffer
void * buffer
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:157
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:288