Files @ r28800:381a234fb097
Branch filter:

Location: cpp/openttd-patchpack/source/src/gfx_layout.cpp - annotation

translators
Update: Translations from eints
english (au): 2 changes by krysclarke
spanish (mexican): 149 changes by Can202
estonian: 11 changes by RM87
chinese (simplified): 18 changes by WenSimEHRP
hungarian: 2 changes by PstasDev
italian: 195 changes by Rivarossi
serbian: 42 changes by nkrs
german: 2 changes by Wuzzy2
belarusian: 537 changes by KorneySan
russian: 25 changes by KorneySan
ukrainian: 21 changes by StepanIvasyn
turkish: 14 changes by jnmbk
latvian: 2 changes by lexuslatvia
dutch: 1 change by iamthedutchdude
spanish: 15 changes by MontyMontana
french: 2 changes by ottdfevr
portuguese: 2 changes by jcteotonio, 2 changes by azulcosta
portuguese (brazilian): 149 changes by pasantoro
polish: 2 changes by pAter-exe
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r27400:742251af6ade
r20450:002c8057d672
r20450:002c8057d672
r20952:c84424569e57
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r27220:3b810953455c
r27220:3b810953455c
r27221:3735cc2cef6b
r27220:3b810953455c
r27221:3735cc2cef6b
r20455:4851057db176
r22897:749f465f2c95
r22897:749f465f2c95
r22897:749f465f2c95
r22897:749f465f2c95
r23077:4219883ebf65
r23077:4219883ebf65
r23077:4219883ebf65
r23077:4219883ebf65
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r20543:89cc7ba70ae8
r20544:29afe601d5e7
r20546:b6881fee3922
r20544:29afe601d5e7
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20952:c84424569e57
r20952:c84424569e57
r23607:36c15679007d
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r27288:52957325547f
r20952:c84424569e57
r23607:36c15679007d
r20952:c84424569e57
r27536:b1a7922dd16b
r27536:b1a7922dd16b
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r23517:1a32c3c14728
r20952:c84424569e57
r27288:52957325547f
r27288:52957325547f
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r27288:52957325547f
r27737:728d55b97775
r20952:c84424569e57
r27288:52957325547f
r27288:52957325547f
r20952:c84424569e57
r20952:c84424569e57
r22818:dc0401fd5990
r22818:dc0401fd5990
r22818:dc0401fd5990
r22818:dc0401fd5990
r22816:fb91bcdce3b1
r22816:fb91bcdce3b1
r20952:c84424569e57
r24111:2dabe397dfc5
r24111:2dabe397dfc5
r20952:c84424569e57
r27221:3735cc2cef6b
r27221:3735cc2cef6b
r20952:c84424569e57
r22896:063f71e2d5c3
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r27384:b919ab98bb53
r27384:b919ab98bb53
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r20952:c84424569e57
r27384:b919ab98bb53
r27384:b919ab98bb53
r20952:c84424569e57
r22896:063f71e2d5c3
r20952:c84424569e57
r20952:c84424569e57
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r27288:52957325547f
r20450:002c8057d672
r20541:b3cc03065e98
r20467:466e71240619
r27288:52957325547f
r27288:52957325547f
r27288:52957325547f
r20544:29afe601d5e7
r27288:52957325547f
r23607:36c15679007d
r20544:29afe601d5e7
r20953:64db27cf67a7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20963:a2701a65356f
r20963:a2701a65356f
r27221:3735cc2cef6b
r23607:36c15679007d
r27288:52957325547f
r27221:3735cc2cef6b
r27221:3735cc2cef6b
r20952:c84424569e57
r22897:749f465f2c95
r22897:749f465f2c95
r22897:749f465f2c95
r22897:749f465f2c95
r23607:36c15679007d
r27288:52957325547f
r23607:36c15679007d
r22897:749f465f2c95
r22897:749f465f2c95
r22897:749f465f2c95
r22897:749f465f2c95
r22897:749f465f2c95
r23077:4219883ebf65
r23607:36c15679007d
r27288:52957325547f
r23607:36c15679007d
r23077:4219883ebf65
r23077:4219883ebf65
r23077:4219883ebf65
r23077:4219883ebf65
r23077:4219883ebf65
r23607:36c15679007d
r27288:52957325547f
r20952:c84424569e57
r20450:002c8057d672
r20450:002c8057d672
r23599:b4bf61c1f787
r23599:b4bf61c1f787
r23599:b4bf61c1f787
r23607:36c15679007d
r23599:b4bf61c1f787
r20467:466e71240619
r27288:52957325547f
r27288:52957325547f
r27288:52957325547f
r27288:52957325547f
r27288:52957325547f
r27288:52957325547f
r27288:52957325547f
r27288:52957325547f
r27288:52957325547f
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r23599:b4bf61c1f787
r24597:afde5721a3b6
r23536:ce42deb0b32d
r20450:002c8057d672
r20450:002c8057d672
r20450:002c8057d672
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r27650:0cae268be5b0
r27650:0cae268be5b0
r27737:728d55b97775
r27650:0cae268be5b0
r27650:0cae268be5b0
r27650:0cae268be5b0
r27650:0cae268be5b0
r27650:0cae268be5b0
r27650:0cae268be5b0
r27650:0cae268be5b0
r27650:0cae268be5b0
r27650:0cae268be5b0
r27650:0cae268be5b0
r20613:8e39d5a2a85b
r27288:52957325547f
r20613:8e39d5a2a85b
r20613:8e39d5a2a85b
r20613:8e39d5a2a85b
r27288:52957325547f
r20613:8e39d5a2a85b
r27289:abeec00a698a
r27289:abeec00a698a
r27289:abeec00a698a
r27289:abeec00a698a
r27289:abeec00a698a
r27289:abeec00a698a
r27289:abeec00a698a
r27289:abeec00a698a
r20613:8e39d5a2a85b
r20613:8e39d5a2a85b
r20613:8e39d5a2a85b
r27288:52957325547f
r27289:abeec00a698a
r27737:728d55b97775
r27650:0cae268be5b0
r27289:abeec00a698a
r27289:abeec00a698a
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r27289:abeec00a698a
r28526:5a401541c043
r20613:8e39d5a2a85b
r27289:abeec00a698a
r20613:8e39d5a2a85b
r27289:abeec00a698a
r27289:abeec00a698a
r27289:abeec00a698a
r28448:e444dae22974
r28448:e444dae22974
r20613:8e39d5a2a85b
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r28526:5a401541c043
r20613:8e39d5a2a85b
r20613:8e39d5a2a85b
r20613:8e39d5a2a85b
r28526:5a401541c043
r28526:5a401541c043
r20613:8e39d5a2a85b
r20613:8e39d5a2a85b
r20613:8e39d5a2a85b
r27603:9ca7ab7bd8aa
r20653:cbfc7f1af4ff
r27901:d0a1e7017e52
r27603:9ca7ab7bd8aa
r20653:cbfc7f1af4ff
r27901:d0a1e7017e52
r20653:cbfc7f1af4ff
r27901:d0a1e7017e52
r27901:d0a1e7017e52
r27901:d0a1e7017e52
r20653:cbfc7f1af4ff
r20953:64db27cf67a7
r23598:7da4fa077a86
r28448:e444dae22974
r28448:e444dae22974
r28448:e444dae22974
r20653:cbfc7f1af4ff
r23598:7da4fa077a86
r20653:cbfc7f1af4ff
r28448:e444dae22974
r20653:cbfc7f1af4ff
r28511:2d7264916867
r28511:2d7264916867
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r28448:e444dae22974
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r27603:9ca7ab7bd8aa
r27603:9ca7ab7bd8aa
r20653:cbfc7f1af4ff
r27737:728d55b97775
r27650:0cae268be5b0
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r27288:52957325547f
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r20653:cbfc7f1af4ff
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r27384:b919ab98bb53
r27385:dea7924e367a
r20543:89cc7ba70ae8
r27385:dea7924e367a
r27385:dea7924e367a
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r28584:d5de6992fa2a
r28584:d5de6992fa2a
r28584:d5de6992fa2a
r28584:d5de6992fa2a
r28584:d5de6992fa2a
r28584:d5de6992fa2a
r28584:d5de6992fa2a
r28584:d5de6992fa2a
r28584:d5de6992fa2a
r28584:d5de6992fa2a
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r20543:89cc7ba70ae8
r23517:1a32c3c14728
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r22897:749f465f2c95
r22897:749f465f2c95
r22897:749f465f2c95
r22897:749f465f2c95
r23077:4219883ebf65
r23077:4219883ebf65
r23077:4219883ebf65
r20543:89cc7ba70ae8
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r27288:52957325547f
r20544:29afe601d5e7
r23607:36c15679007d
r20546:b6881fee3922
r20546:b6881fee3922
r20546:b6881fee3922
r20546:b6881fee3922
r27288:52957325547f
r25898:e57f843dc921
r25898:e57f843dc921
r25898:e57f843dc921
r25898:e57f843dc921
r25898:e57f843dc921
r20544:29afe601d5e7
r20544:29afe601d5e7
r27288:52957325547f
r20546:b6881fee3922
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r23607:36c15679007d
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r20544:29afe601d5e7
r23607:36c15679007d
r20546:b6881fee3922
r20546:b6881fee3922
r20546:b6881fee3922
r20544:29afe601d5e7
/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file gfx_layout.cpp Handling of laying out text. */

#include "stdafx.h"
#include "core/math_func.hpp"
#include "gfx_layout.h"
#include "string_func.h"
#include "debug.h"

#include "table/control_codes.h"

#include "gfx_layout_fallback.h"

#if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
#include "gfx_layout_icu.h"
#endif /* WITH_ICU_I18N && WITH_HARFBUZZ */

#ifdef WITH_UNISCRIBE
#include "os/windows/string_uniscribe.h"
#endif /* WITH_UNISCRIBE */

#ifdef WITH_COCOA
#include "os/macosx/string_osx.h"
#endif

#include "safeguards.h"


/** Cache of ParagraphLayout lines. */
Layouter::LineCache *Layouter::linecache;

/** Cache of Font instances. */
Layouter::FontColourMap Layouter::fonts[FS_END];


/**
 * Construct a new font.
 * @param size   The font size to use for this font.
 * @param colour The colour to draw this font in.
 */
Font::Font(FontSize size, TextColour colour) :
		fc(FontCache::Get(size)), colour(colour)
{
	assert(size < FS_END);
}

/**
 * Helper for getting a ParagraphLayouter of the given type.
 *
 * @note In case no ParagraphLayouter could be constructed, line.layout will be nullptr.
 * @param line The cache item to store our layouter in.
 * @param str The string to create a layouter for.
 * @param state The state of the font and color.
 * @tparam T The type of layouter we want.
 */
template <typename T>
static inline void GetLayouter(Layouter::LineCacheItem &line, std::string_view str, FontState &state)
{
	if (line.buffer != nullptr) free(line.buffer);

	typename T::CharType *buff_begin = MallocT<typename T::CharType>(str.size() + 1);
	const typename T::CharType *buffer_last = buff_begin + str.size() + 1;
	typename T::CharType *buff = buff_begin;
	FontMap &fontMapping = line.runs;
	Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);

	line.buffer = buff_begin;
	fontMapping.clear();

	auto cur = str.begin();

	/*
	 * Go through the whole string while adding Font instances to the font map
	 * whenever the font changes, and convert the wide characters into a format
	 * usable by ParagraphLayout.
	 */
	for (; buff < buffer_last && cur != str.end();) {
		char32_t c = Utf8Consume(cur);
		if (c == '\0' || c == '\n') {
			/* Caller should already have filtered out these characters. */
			NOT_REACHED();
		} else if (c >= SCC_BLUE && c <= SCC_BLACK) {
			state.SetColour((TextColour)(c - SCC_BLUE));
		} else if (c == SCC_PUSH_COLOUR) {
			state.PushColour();
		} else if (c == SCC_POP_COLOUR) {
			state.PopColour();
		} else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
			state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
		} else {
			/* Filter out non printable characters */
			if (!IsPrintable(c)) continue;
			/* Filter out text direction characters that shouldn't be drawn, and
			 * will not be handled in the fallback case because they are mostly
			 * needed for RTL languages which need more proper shaping support. */
			if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
			buff += T::AppendToBuffer(buff, buffer_last, c);
			continue;
		}

		if (fontMapping.count(buff - buff_begin) == 0) {
			fontMapping[buff - buff_begin] = f;
		}
		f = Layouter::GetFont(state.fontsize, state.cur_colour);
	}

	/* Better safe than sorry. */
	*buff = '\0';

	if (fontMapping.count(buff - buff_begin) == 0) {
		fontMapping[buff - buff_begin] = f;
	}
	line.layout = T::GetParagraphLayout(buff_begin, buff, fontMapping);
	line.state_after = state;
}

/**
 * Create a new layouter.
 * @param str      The string to create the layout for.
 * @param maxw     The maximum width.
 * @param colour   The colour of the font.
 * @param fontsize The size of font to use.
 */
Layouter::Layouter(std::string_view str, int maxw, TextColour colour, FontSize fontsize) : string(str)
{
	FontState state(colour, fontsize);

	while (true) {
		auto line_length = str.find_first_of('\n');
		auto str_line = str.substr(0, line_length);

		LineCacheItem &line = GetCachedParagraphLayout(str_line, state);
		if (line.layout != nullptr) {
			state = line.state_after;
			line.layout->Reflow();
		} else {
			/* Line is new, layout it */
			FontState old_state = state;

#if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
			if (line.layout == nullptr) {
				GetLayouter<ICUParagraphLayoutFactory>(line, str_line, state);
				if (line.layout == nullptr) {
					state = old_state;
				}
			}
#endif

#ifdef WITH_UNISCRIBE
			if (line.layout == nullptr) {
				GetLayouter<UniscribeParagraphLayoutFactory>(line, str_line, state);
				if (line.layout == nullptr) {
					state = old_state;
				}
			}
#endif

#ifdef WITH_COCOA
			if (line.layout == nullptr) {
				GetLayouter<CoreTextParagraphLayoutFactory>(line, str_line, state);
				if (line.layout == nullptr) {
					state = old_state;
				}
			}
#endif

			if (line.layout == nullptr) {
				GetLayouter<FallbackParagraphLayoutFactory>(line, str_line, state);
			}
		}

		/* Move all lines into a local cache so we can reuse them later on more easily. */
		for (;;) {
			auto l = line.layout->NextLine(maxw);
			if (l == nullptr) break;
			this->push_back(std::move(l));
		}

		/* Break out if this was the last line. */
		if (line_length == std::string_view::npos) {
			break;
		}

		/* Go to the next line. */
		str.remove_prefix(line_length + 1);
	}
}

/**
 * Get the boundaries of this paragraph.
 * @return The boundaries.
 */
Dimension Layouter::GetBounds()
{
	Dimension d = { 0, 0 };
	for (const auto &l : *this) {
		d.width = std::max<uint>(d.width, l->GetWidth());
		d.height += l->GetLeading();
	}
	return d;
}

/**
 * Test whether a character is a non-printable formatting code
 */
static bool IsConsumedFormattingCode(char32_t ch)
{
	if (ch >= SCC_BLUE && ch <= SCC_BLACK) return true;
	if (ch == SCC_PUSH_COLOUR) return true;
	if (ch == SCC_POP_COLOUR) return true;
	if (ch >= SCC_FIRST_FONT && ch <= SCC_LAST_FONT) return true;
	// All other characters defined in Unicode standard are assumed to be non-consumed.
	return false;
}

/**
 * Get the position of a character in the layout.
 * @param ch Character to get the position of. Must be an iterator of the string passed to the constructor.
 * @return Upper left corner of the character relative to the start of the string.
 * @note Will only work right for single-line strings.
 */
Point Layouter::GetCharPosition(std::string_view::const_iterator ch) const
{
	const auto &line = this->front();

	/* Pointer to the end-of-string marker? Return total line width. */
	if (ch == this->string.end()) {
		Point p = { line->GetWidth(), 0 };
		return p;
	}

	/* Find the code point index which corresponds to the char
	 * pointer into our UTF-8 source string. */
	size_t index = 0;
	auto str = this->string.begin();
	while (str < ch) {
		char32_t c = Utf8Consume(str);
		if (!IsConsumedFormattingCode(c)) index += line->GetInternalCharLength(c);
	}

	/* Initial position, returned if character not found. */
	static const std::vector<Point> zero = { {0, 0} };
	auto position = zero.begin();

	/* We couldn't find the code point index. */
	if (str != ch) return *position;

	/* Valid character. */

	/* Scan all runs until we've found our code point index. */
	for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
		const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
		const auto &positions = run.GetPositions();
		const auto &charmap = run.GetGlyphToCharMap();

		/* Run starts after our character, use the last found position. */
		if ((size_t)charmap.front() > index) return *position;

		position = positions.begin();
		for (auto it = charmap.begin(); it != charmap.end(); /* nothing */) {
			/* Plain honest-to-$deity match. */
			if ((size_t)*it == index) return *position;
			++it;
			if (it == charmap.end()) break;

			/* We just passed our character, it's probably a ligature, use the last found position. */
			if ((size_t)*it > index) return *position;
			++position;
		}
	}

	/* At the end of the run but still didn't find our character so probably a trailing ligature, use the last found position. */
	return *position;
}

/**
 * Get the character that is at a pixel position in the first line of the layouted text.
 * @param x Position in the string.
 * @param line_index Which line of the layout to search
 * @return String offset of the position (bytes) or -1 if no character is at the position.
 */
ptrdiff_t Layouter::GetCharAtPosition(int x, size_t line_index) const
{
	if (line_index >= this->size()) return -1;

	const auto &line = this->at(line_index);

	for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
		const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
		const auto &glyphs = run.GetGlyphs();
		const auto &positions = run.GetPositions();
		const auto &charmap = run.GetGlyphToCharMap();

		for (int i = 0; i < run.GetGlyphCount(); i++) {
			/* Not a valid glyph (empty). */
			if (glyphs[i] == 0xFFFF) continue;

			int begin_x = positions[i].x;
			int end_x   = positions[i + 1].x;

			if (IsInsideMM(x, begin_x, end_x)) {
				/* Found our glyph, now convert to UTF-8 string index. */
				size_t index = charmap[i];

				size_t cur_idx = 0;
				for (auto str = this->string.begin(); str != this->string.end();) {
					if (cur_idx == index) return str - this->string.begin();

					char32_t c = Utf8Consume(str);
					if (!IsConsumedFormattingCode(c)) cur_idx += line->GetInternalCharLength(c);
				}
			}
		}
	}

	return -1;
}

/**
 * Get a static font instance.
 */
Font *Layouter::GetFont(FontSize size, TextColour colour)
{
	FontColourMap::iterator it = fonts[size].find(colour);
	if (it != fonts[size].end()) return it->second.get();

	fonts[size][colour] = std::make_unique<Font>(size, colour);
	return fonts[size][colour].get();
}

/**
 * Perform initialization of layout engine.
 */
void Layouter::Initialize()
{
#if defined(WITH_ICU_I18N) && defined(WITH_HARFBUZZ)
	ICUParagraphLayoutFactory::InitializeLayouter();
#endif /* WITH_ICU_I18N && WITH_HARFBUZZ */
}

/**
 * Reset cached font information.
 * @param size Font size to reset.
 */
void Layouter::ResetFontCache(FontSize size)
{
	fonts[size].clear();

	/* We must reset the linecache since it references the just freed fonts */
	ResetLineCache();

#if defined(WITH_UNISCRIBE)
	UniscribeResetScriptCache(size);
#endif
#if defined(WITH_COCOA)
	MacOSResetScriptCache(size);
#endif
}

/**
 * Get reference to cache item.
 * If the item does not exist yet, it is default constructed.
 * @param str Source string of the line (including colour and font size codes).
 * @param state State of the font at the beginning of the line.
 * @return Reference to cache item.
 */
Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(std::string_view str, const FontState &state)
{
	if (linecache == nullptr) {
		/* Create linecache on first access to avoid trouble with initialisation order of static variables. */
		linecache = new LineCache();
	}

	if (auto match = linecache->find(LineCacheQuery{state, str});
		match != linecache->end()) {
		return match->second;
	}

	/* Create missing entry */
	LineCacheKey key;
	key.state_before = state;
	key.str.assign(str);
	return (*linecache)[key];
}

/**
 * Clear line cache.
 */
void Layouter::ResetLineCache()
{
	if (linecache != nullptr) linecache->clear();
}

/**
 * Reduce the size of linecache if necessary to prevent infinite growth.
 */
void Layouter::ReduceLineCache()
{
	if (linecache != nullptr) {
		/* TODO LRU cache would be fancy, but not exactly necessary */
		if (linecache->size() > 4096) ResetLineCache();
	}
}