OpenTTD Source 20261112-master-gbcbec5e63f
string_uniscribe.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 "../../debug.h"
12#include "string_uniscribe.h"
13#include "../../language.h"
14#include "../../strings_func.h"
15#include "../../string_func.h"
16#include "../../core/utf8.hpp"
18#include "../../zoom_func.h"
19#include "win32.h"
20
21#include <windows.h>
22#include <usp10.h>
23
24#include "../../safeguards.h"
25
26#ifdef _MSC_VER
27# pragma comment(lib, "usp10")
28#endif
29
30
33
39 int pos;
40 int len;
41 Font font;
42
43 std::vector<GlyphID> ft_glyphs;
44
45 SCRIPT_ANALYSIS sa;
46 std::vector<WORD> char_to_glyph;
47
48 std::vector<SCRIPT_VISATTR> vis_attribs;
49 std::vector<WORD> glyphs;
50 std::vector<int> advances;
51 std::vector<GOFFSET> offsets;
52 int total_advance;
53
61 UniscribeRun(int pos, int len, const Font &font, SCRIPT_ANALYSIS &sa) : pos(pos), len(len), font(font), sa(sa) {}
62};
63
64static std::vector<SCRIPT_ITEM> UniscribeItemizeString(UniscribeParagraphLayoutFactory::CharType *buff, int32_t length);
66
70class UniscribeParagraphLayout : public ParagraphLayouter {
71private:
73
74 std::vector<UniscribeRun> ranges;
75 std::vector<UniscribeRun>::iterator cur_range;
77
78public:
80 class UniscribeVisualRun : public ParagraphLayouter::VisualRun {
81 private:
82 std::vector<GlyphID> glyphs;
83 std::vector<Position> positions;
84 std::vector<WORD> char_to_glyph;
85
86 int start_pos;
87 int total_advance;
88 Font font;
89
90 mutable std::vector<int> glyph_to_char;
91
92 public:
93 UniscribeVisualRun(const UniscribeRun &range, int x);
94 UniscribeVisualRun(UniscribeVisualRun &&other) noexcept;
95
96 std::span<const GlyphID> GetGlyphs() const override { return this->glyphs; }
97 std::span<const Position> GetPositions() const override { return this->positions; }
98 std::span<const int> GetGlyphToCharMap() const override;
99
100 const Font &GetFont() const override { return this->font; }
101 int GetLeading() const override { return this->font.GetFontCache().GetHeight(); }
102 size_t GetGlyphCount() const override { return this->glyphs.size(); }
103 int GetAdvance() const { return this->total_advance; }
104 };
105
107 class UniscribeLine : public std::vector<UniscribeVisualRun>, public ParagraphLayouter::Line {
108 public:
109 int GetLeading() const override;
110 int GetWidth() const override;
111 size_t CountRuns() const override { return this->size(); }
112 const VisualRun &GetVisualRun(size_t run) const override { return this->at(run); }
113
114 int GetInternalCharLength(char32_t c) const override
115 {
116 /* Uniscribe uses UTF-16 internally which means we need to account for surrogate pairs. */
117 return c >= 0x010000U ? 2 : 1;
118 }
119 };
120
121 UniscribeParagraphLayout(std::vector<UniscribeRun> &&ranges, const UniscribeParagraphLayoutFactory::CharType *buffer) : text_buffer(buffer), ranges(std::move(ranges))
122 {
123 this->Reflow();
124 }
125
126 ~UniscribeParagraphLayout() override = default;
127
128 void Reflow() override
129 {
130 this->cur_range = this->ranges.begin();
131 this->cur_range_offset = 0;
132 }
133
134 std::unique_ptr<const Line> NextLine(int max_width) override;
135};
136
137void UniscribeResetScriptCache(FontSize size)
138{
139 if (_script_cache[size] != nullptr) {
140 ScriptFreeCache(&_script_cache[size]);
141 _script_cache[size] = nullptr;
142 }
143}
144
150static HFONT HFontFromFont(const Font &font)
151{
152 FontCache &fc = font.GetFontCache();
153 assert(fc.GetOSHandle() != nullptr);
154 return CreateFontIndirect(reinterpret_cast<PLOGFONT>(const_cast<void *>(fc.GetOSHandle())));
155}
156
164{
165 /* Initial size guess for the number of glyphs recommended by Uniscribe. */
166 range.glyphs.resize(range.len * 3 / 2 + 16);
167 range.vis_attribs.resize(range.glyphs.size());
168
169 /* The char-to-glyph array is the same size as the input. */
170 range.char_to_glyph.resize(range.len);
171
172 HDC temp_dc = nullptr;
173 HFONT old_font = nullptr;
174 HFONT cur_font = nullptr;
175
176 while (true) {
177 /* Shape the text run by determining the glyphs needed for display. */
178 int glyphs_used = 0;
179 HRESULT hr = ScriptShape(temp_dc, &_script_cache[range.font.fontsize], buff + range.pos, range.len, (int)range.glyphs.size(), &range.sa, &range.glyphs[0], &range.char_to_glyph[0], &range.vis_attribs[0], &glyphs_used);
180
181 if (SUCCEEDED(hr)) {
182 range.glyphs.resize(glyphs_used);
183 range.vis_attribs.resize(glyphs_used);
184
185 /* Calculate the glyph positions. */
186 ABC abc;
187 range.advances.resize(range.glyphs.size());
188 range.offsets.resize(range.glyphs.size());
189 hr = ScriptPlace(temp_dc, &_script_cache[range.font.fontsize], &range.glyphs[0], (int)range.glyphs.size(), &range.vis_attribs[0], &range.sa, &range.advances[0], &range.offsets[0], &abc);
190 if (SUCCEEDED(hr)) {
191 /* We map our special sprite chars to values that don't fit into a WORD. Copy the glyphs
192 * into a new vector and query the real glyph to use for these special chars. */
193 range.ft_glyphs.resize(range.glyphs.size());
194 for (size_t g_id = 0; g_id < range.glyphs.size(); g_id++) {
195 range.ft_glyphs[g_id] = range.glyphs[g_id];
196 }
197 for (int i = 0; i < range.len; i++) {
198 if (buff[range.pos + i] >= SCC_SPRITE_START && buff[range.pos + i] <= SCC_SPRITE_END) {
199 auto pos = range.char_to_glyph[i];
200 if (range.ft_glyphs[pos] == 0) { // Font doesn't have our special glyph, so remap.
201 FontCache &fc = range.font.GetFontCache();
202 range.ft_glyphs[pos] = fc.MapCharToGlyph(buff[range.pos + i]);
203 range.offsets[pos].dv = (fc.GetHeight() - ScaleSpriteTrad(FontCache::GetDefaultFontHeight(fc.GetSize()))) / 2; // Align sprite font to centre
204 range.advances[pos] = fc.GetGlyphWidth(range.ft_glyphs[pos]);
205 }
206 }
207 }
208
209 range.total_advance = 0;
210 for (size_t i = 0; i < range.advances.size(); i++) {
211#ifdef WITH_FREETYPE
212 /* FreeType and GDI/Uniscribe seems to occasionally disagree over the width of a glyph. */
213 if (range.advances[i] > 0 && range.ft_glyphs[i] != 0xFFFF) range.advances[i] = range.font.GetFontCache().GetGlyphWidth(range.ft_glyphs[i]);
214#endif
215 range.total_advance += range.advances[i];
216 }
217 break;
218 }
219 }
220
221 if (hr == E_OUTOFMEMORY) {
222 /* The glyph buffer needs to be larger. Just double it every time. */
223 range.glyphs.resize(range.glyphs.size() * 2);
224 range.vis_attribs.resize(range.vis_attribs.size() * 2);
225 } else if (hr == E_PENDING) {
226 /* Glyph data is not in cache, load native font. */
227 cur_font = HFontFromFont(range.font);
228 if (cur_font == nullptr) return false; // Sorry, no dice.
229
230 temp_dc = CreateCompatibleDC(nullptr);
231 SetMapMode(temp_dc, MM_TEXT);
232 old_font = (HFONT)SelectObject(temp_dc, cur_font);
233 } else if (hr == USP_E_SCRIPT_NOT_IN_FONT && range.sa.eScript != SCRIPT_UNDEFINED) {
234 /* Try again with the generic shaping engine. */
235 range.sa.eScript = SCRIPT_UNDEFINED;
236 } else {
237 /* Some unknown other error. */
238 if (temp_dc != nullptr) {
239 SelectObject(temp_dc, old_font);
240 DeleteObject(cur_font);
241 ReleaseDC(nullptr, temp_dc);
242 }
243 return false;
244 }
245 }
246
247 if (temp_dc != nullptr) {
248 SelectObject(temp_dc, old_font);
249 DeleteObject(cur_font);
250 ReleaseDC(nullptr, temp_dc);
251 }
252
253 return true;
254}
255
262static std::vector<SCRIPT_ITEM> UniscribeItemizeString(UniscribeParagraphLayoutFactory::CharType *buff, int32_t length)
263{
264 /* Itemize text. */
265 SCRIPT_CONTROL control{};
266 control.uDefaultLanguage = _current_language->winlangid;
267
268 SCRIPT_STATE state{};
269 state.uBidiLevel = _current_text_dir == TD_RTL ? 1 : 0;
270
271 std::vector<SCRIPT_ITEM> items(16);
272 while (true) {
273 /* We subtract one from max_items to work around a buffer overflow on some older versions of Windows. */
274 int generated = 0;
275 HRESULT hr = ScriptItemize(buff, length, (int)items.size() - 1, &control, &state, &items[0], &generated);
276
277 if (SUCCEEDED(hr)) {
278 /* Resize the item buffer. Note that Uniscribe will always add an additional end sentinel item. */
279 items.resize(generated + 1);
280 break;
281 }
282 /* Some kind of error except item buffer too small. */
283 if (hr != E_OUTOFMEMORY) return std::vector<SCRIPT_ITEM>();
284
285 items.resize(items.size() * 2);
286 }
287
288 return items;
289}
290
291/* static */ std::unique_ptr<ParagraphLayouter> UniscribeParagraphLayoutFactory::GetParagraphLayout(CharType *buff, CharType *buff_end, FontMap &font_mapping)
292{
293 int32_t length = buff_end - buff;
294 /* Can't layout an empty string. */
295 if (length == 0) return nullptr;
296
297 /* Can't layout our in-built sprite fonts. */
298 for (auto const &[position, font] : font_mapping) {
299 if (font.GetFontCache().IsBuiltInFont()) return nullptr;
300 }
301
302 /* Itemize text. */
303 std::vector<SCRIPT_ITEM> items = UniscribeItemizeString(buff, length);
304 if (items.empty()) return nullptr;
305
306 /* Build ranges from the items and the font map. A range is a run of text
307 * that is part of a single item and formatted using a single font style. */
308 std::vector<UniscribeRun> ranges;
309
310 int cur_pos = 0;
311 std::vector<SCRIPT_ITEM>::iterator cur_item = items.begin();
312 for (auto const &[position, font] : font_mapping) {
313 while (cur_pos < position && cur_item != items.end() - 1) {
314 /* Add a range that spans the intersection of the remaining item and font run. */
315 int stop_pos = std::min(position, (cur_item + 1)->iCharPos);
316 assert(stop_pos - cur_pos > 0);
317 ranges.emplace_back(cur_pos, stop_pos - cur_pos, font, cur_item->a);
318
319 /* Shape the range. */
320 if (!UniscribeShapeRun(buff, ranges.back())) {
321 return nullptr;
322 }
323
324 /* If we are at the end of the current item, advance to the next item. */
325 if (stop_pos == (cur_item + 1)->iCharPos) cur_item++;
326 cur_pos = stop_pos;
327 }
328 }
329
330 return std::make_unique<UniscribeParagraphLayout>(std::move(ranges), buff);
331}
332
333/* virtual */ std::unique_ptr<const ParagraphLayouter::Line> UniscribeParagraphLayout::NextLine(int max_width)
334{
335 std::vector<UniscribeRun>::iterator start_run = this->cur_range;
336 std::vector<UniscribeRun>::iterator last_run = this->cur_range;
337
338 if (start_run == this->ranges.end()) return nullptr;
339
340 /* Add remaining width of the first run if it is a broken run. */
341 int cur_width = 0;
342 if (this->cur_range_offset != 0) {
343 std::vector<int> dx(start_run->len);
344 ScriptGetLogicalWidths(&start_run->sa, start_run->len, (int)start_run->glyphs.size(), &start_run->advances[0], &start_run->char_to_glyph[0], &start_run->vis_attribs[0], &dx[0]);
345
346 for (std::vector<int>::const_iterator c = dx.begin() + this->cur_range_offset; c != dx.end(); c++) {
347 cur_width += *c;
348 }
349 ++last_run;
350 }
351
352 /* Gather runs until the line is full. */
353 while (last_run != this->ranges.end() && cur_width <= max_width) {
354 cur_width += last_run->total_advance;
355 ++last_run;
356 }
357
358 /* If the text does not fit into the available width, find a suitable breaking point. */
359 int remaining_offset = (last_run - 1)->len + 1;
360 int whitespace_count = 0;
361 if (cur_width > max_width) {
362 std::vector<SCRIPT_LOGATTR> log_attribs;
363
364 /* Get word break information. */
365 int width_avail = max_width;
366 int num_chars = this->cur_range_offset;
367 int start_offs = this->cur_range_offset;
368 int last_cluster = this->cur_range_offset + 1;
369 for (std::vector<UniscribeRun>::iterator r = start_run; r != last_run; r++) {
370 log_attribs.resize(r->pos - start_run->pos + r->len);
371 if (FAILED(ScriptBreak(this->text_buffer + r->pos + start_offs, r->len - start_offs, &r->sa, &log_attribs[r->pos - start_run->pos + start_offs]))) return nullptr;
372
373 std::vector<int> dx(r->len);
374 ScriptGetLogicalWidths(&r->sa, r->len, (int)r->glyphs.size(), &r->advances[0], &r->char_to_glyph[0], &r->vis_attribs[0], &dx[0]);
375
376 /* Count absolute max character count on the line. */
377 for (int c = start_offs; c < r->len && width_avail > 0; c++, num_chars++) {
378 if (c > start_offs && log_attribs[num_chars].fCharStop) last_cluster = num_chars;
379 width_avail -= dx[c];
380 }
381
382 start_offs = 0;
383 }
384
385 /* Walk backwards to find the last suitable breaking point. */
386 while (--num_chars > this->cur_range_offset && !log_attribs[num_chars].fSoftBreak && !log_attribs[num_chars].fWhiteSpace) {}
387
388 if (num_chars == this->cur_range_offset) {
389 /* Didn't find any suitable word break point, just break on the last cluster boundary. */
390 num_chars = last_cluster;
391 }
392
393 /* Eat any whitespace characters before the breaking point. */
394 while (num_chars - 1 > this->cur_range_offset && log_attribs[num_chars - 1].fWhiteSpace) num_chars--;
395 /* Count whitespace after the breaking point. */
396 while (num_chars + whitespace_count < (int)log_attribs.size() && log_attribs[num_chars + whitespace_count].fWhiteSpace) whitespace_count++;
397
398 /* Get last run that corresponds to the number of characters to show. */
399 for (std::vector<UniscribeRun>::iterator run = start_run; run != last_run; run++) {
400 num_chars -= run->len;
401
402 if (num_chars <= 0) {
403 remaining_offset = num_chars + run->len + 1;
404 last_run = run + 1;
405 assert(remaining_offset - 1 > 0);
406 break;
407 }
408 }
409 }
410
411 /* Build display order from the runs. */
412 std::vector<BYTE> bidi_level;
413 for (std::vector<UniscribeRun>::iterator r = start_run; r != last_run; r++) {
414 bidi_level.push_back(r->sa.s.uBidiLevel);
415 }
416 std::vector<INT> vis_to_log(bidi_level.size());
417 if (FAILED(ScriptLayout((int)bidi_level.size(), &bidi_level[0], &vis_to_log[0], nullptr))) return nullptr;
418
419 /* Create line. */
420 std::unique_ptr<UniscribeLine> line = std::make_unique<UniscribeLine>();
421
422 int cur_pos = 0;
423 for (std::vector<INT>::iterator l = vis_to_log.begin(); l != vis_to_log.end(); l++) {
424 std::vector<UniscribeRun>::iterator i_run = start_run + *l;
425 UniscribeRun run = *i_run;
426
427 /* Partial run after line break (either start or end)? Reshape run to get the first/last glyphs right. */
428 if (i_run == last_run - 1 && remaining_offset <= (last_run - 1)->len) {
429 run.len = remaining_offset - 1;
430
431 if (!UniscribeShapeRun(this->text_buffer, run)) return nullptr;
432 }
433 if (i_run == start_run && this->cur_range_offset > 0) {
434 assert(run.len - this->cur_range_offset > 0);
435 run.pos += this->cur_range_offset;
436 run.len -= this->cur_range_offset;
437
438 if (!UniscribeShapeRun(this->text_buffer, run)) return nullptr;
439 }
440
441 line->emplace_back(run, cur_pos);
442 cur_pos += run.total_advance;
443 }
444
445 if (remaining_offset + whitespace_count - 1 < (last_run - 1)->len) {
446 /* We didn't use up all of the last run, store remainder for the next line. */
447 this->cur_range_offset = remaining_offset + whitespace_count - 1;
448 this->cur_range = last_run - 1;
449 assert(this->cur_range->len > this->cur_range_offset);
450 } else {
451 this->cur_range_offset = 0;
452 this->cur_range = last_run;
453 }
454
455 return line;
456}
457
463{
464 int leading = 0;
465 for (const auto &run : *this) {
466 leading = std::max(leading, run.GetLeading());
467 }
468
469 return leading;
470}
471
477{
478 int length = 0;
479 for (const auto &run : *this) {
480 length += run.GetAdvance();
481 }
482
483 return length;
484}
485
486UniscribeParagraphLayout::UniscribeVisualRun::UniscribeVisualRun(const UniscribeRun &range, int x) : glyphs(range.ft_glyphs), char_to_glyph(range.char_to_glyph), start_pos(range.pos), total_advance(range.total_advance), font(range.font)
487{
488 this->positions.reserve(this->GetGlyphCount());
489
490 int advance = x;
491 for (size_t i = 0; i < this->GetGlyphCount(); i++) {
492 int x_advance = range.advances[i];
493 this->positions.emplace_back(range.offsets[i].du + advance, range.offsets[i].du + advance + x_advance - 1, range.offsets[i].dv);
494
495 advance += x_advance;
496 }
497}
498
499UniscribeParagraphLayout::UniscribeVisualRun::UniscribeVisualRun(UniscribeVisualRun&& other) noexcept
500 : glyphs(std::move(other.glyphs)), positions(std::move(other.positions)), char_to_glyph(std::move(other.char_to_glyph)),
501 start_pos(other.start_pos), total_advance(other.total_advance), font(other.font),
502 glyph_to_char(std::move(other.glyph_to_char))
503{
504}
505
507{
508 if (this->glyph_to_char.empty()) {
509 this->glyph_to_char.resize(this->GetGlyphCount());
510
511 /* The char to glyph array contains the first glyph index of the cluster that is associated
512 * with each character. It is possible for a cluster to be formed of several chars. */
513 for (int c = 0; c < (int)this->char_to_glyph.size(); c++) {
514 /* If multiple chars map to one glyph, only refer back to the first character. */
515 if (this->glyph_to_char[this->char_to_glyph[c]] == 0) this->glyph_to_char[this->char_to_glyph[c]] = c + this->start_pos;
516 }
517
518 /* We only marked the first glyph of each cluster in the loop above. Fill the gaps. */
519 int last_char = this->glyph_to_char[0];
520 for (size_t g = 0; g < this->GetGlyphCount(); g++) {
521 if (this->glyph_to_char[g] != 0) last_char = this->glyph_to_char[g];
522 this->glyph_to_char[g] = last_char;
523 }
524 }
525
526 return this->glyph_to_char;
527}
528
529
530/* virtual */ void UniscribeStringIterator::SetString(std::string_view s)
531{
532 this->utf16_to_utf8.clear();
533 this->str_info.clear();
534 this->cur_pos = 0;
535
536 /* Uniscribe operates on UTF-16, thus we have to convert the input string.
537 * To be able to return proper offsets, we have to create a mapping at the same time. */
538 std::vector<wchar_t> utf16_str;
539 Utf8View view(s);
540 for (auto it = view.begin(), end = view.end(); it != end; ++it) {
541 size_t idx = it.GetByteOffset();
542 char32_t c = *it;
543 if (c < 0x10000) {
544 utf16_str.push_back((wchar_t)c);
545 } else {
546 /* Make a surrogate pair. */
547 utf16_str.push_back((wchar_t)(0xD800 + ((c - 0x10000) >> 10)));
548 utf16_str.push_back((wchar_t)(0xDC00 + ((c - 0x10000) & 0x3FF)));
549 this->utf16_to_utf8.push_back(idx);
550 }
551 this->utf16_to_utf8.push_back(idx);
552 }
553 this->utf16_to_utf8.push_back(s.size());
554
555 /* Query Uniscribe for word and cluster break information. */
556 this->str_info.resize(utf16_to_utf8.size());
557
558 if (!utf16_str.empty()) {
559 /* Itemize string into language runs. */
560 std::vector<SCRIPT_ITEM> runs = UniscribeItemizeString(&utf16_str[0], (int32_t)utf16_str.size());
561
562 for (std::vector<SCRIPT_ITEM>::const_iterator run = runs.begin(); !runs.empty() && run != runs.end() - 1; run++) {
563 /* Get information on valid word and character break.s */
564 int len = (run + 1)->iCharPos - run->iCharPos;
565 std::vector<SCRIPT_LOGATTR> attr(len);
566 ScriptBreak(&utf16_str[run->iCharPos], len, &run->a, &attr[0]);
567
568 /* Extract the information we're interested in. */
569 for (size_t c = 0; c < attr.size(); c++) {
570 /* First character of a run is always a valid word break. */
571 this->str_info[c + run->iCharPos].word_stop = attr[c].fWordStop || c == 0;
572 this->str_info[c + run->iCharPos].char_stop = attr[c].fCharStop;
573 }
574 }
575 }
576
577 /* End-of-string is always a valid stopping point. */
578 this->str_info.back().char_stop = true;
579 this->str_info.back().word_stop = true;
580}
581
582/* virtual */ size_t UniscribeStringIterator::SetCurPosition(size_t pos)
583{
584 /* Convert incoming position to an UTF-16 string index. */
585 size_t utf16_pos = 0;
586 for (size_t i = 0; i < this->utf16_to_utf8.size(); i++) {
587 if (this->utf16_to_utf8[i] == pos) {
588 utf16_pos = i;
589 break;
590 }
591 }
592
593 /* Sanitize in case we get a position inside a grapheme cluster. */
594 while (utf16_pos > 0 && !this->str_info[utf16_pos].char_stop) utf16_pos--;
595 this->cur_pos = utf16_pos;
596
597 return this->utf16_to_utf8[this->cur_pos];
598}
599
600/* virtual */ size_t UniscribeStringIterator::Next(IterType what)
601{
602 assert(this->cur_pos <= this->utf16_to_utf8.size());
604
605 if (this->cur_pos == this->utf16_to_utf8.size()) return END;
606
607 do {
608 this->cur_pos++;
609 } while (this->cur_pos < this->utf16_to_utf8.size() && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
610
611 return this->cur_pos == this->utf16_to_utf8.size() ? END : this->utf16_to_utf8[this->cur_pos];
612}
613
615{
616 assert(this->cur_pos <= this->utf16_to_utf8.size());
618
619 if (this->cur_pos == 0) return END;
620
621 do {
622 this->cur_pos--;
623 } while (this->cur_pos > 0 && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
624
625 return this->utf16_to_utf8[this->cur_pos];
626}
Font cache for basic fonts.
Definition fontcache.h:23
int GetHeight() const
Get the height of the font.
Definition fontcache.h:65
virtual const void * GetOSHandle()
Get the native OS font handle, if there is one.
Definition fontcache.h:120
virtual GlyphID MapCharToGlyph(char32_t key, bool fallback=true)=0
Map a character into a glyph.
virtual uint GetGlyphWidth(GlyphID key)=0
Get the width of the glyph with the given key.
FontSize GetSize() const
Get the FontSize of the font.
Definition fontcache.h:59
Container with information about a font.
Definition gfx_layout.h:21
FontCache & GetFontCache() const
Get the font cache of this font.
Definition gfx_layout.h:36
FontSize fontsize
The font size.
Definition gfx_layout.h:23
A single line worth of VisualRuns.
Definition gfx_layout.h:209
Visual run contains data about the bit of text with the same font.
Definition gfx_layout.h:166
Interface to glue fallback and normal layouter into one.
Definition gfx_layout.h:144
static const size_t END
Sentinel to indicate end-of-iteration.
Definition string_base.h:23
IterType
Type of the iterator.
Definition string_base.h:17
@ ITER_WORD
Iterate over words.
Definition string_base.h:19
@ ITER_CHARACTER
Iterate over characters (or more exactly grapheme clusters).
Definition string_base.h:18
static std::unique_ptr< ParagraphLayouter > GetParagraphLayout(CharType *buff, CharType *buff_end, FontMap &font_mapping)
Get the actual ParagraphLayout for the given buffer.
wchar_t CharType
Helper for GetLayouter, to get the right type.
A single line worth of VisualRuns.
int GetWidth() const override
Get the width of this line.
size_t CountRuns() const override
Get the number of runs in this line.
int GetInternalCharLength(char32_t c) const override
Get the number of elements the given character occupies in the underlying text buffer of the Layouter...
int GetLeading() const override
Get the height of the line.
const VisualRun & GetVisualRun(size_t run) const override
Get a reference to the given run.
Visual run contains data about the bit of text with the same font.
std::span< const GlyphID > GetGlyphs() const override
Get the glyphs to draw.
size_t GetGlyphCount() const override
Get the number of glyphs.
std::span< const int > GetGlyphToCharMap() const override
The offset for each of the glyphs to the character run that was passed to the Layouter.
std::span< const Position > GetPositions() const override
Get the positions for each of the glyphs.
const Font & GetFont() const override
Get the font.
int GetLeading() const override
Get the font leading, or distance between the baselines of consecutive lines.
Wrapper for doing layouts with Uniscribe.
int cur_range_offset
Offset from the start of the current run from where to output.
void Reflow() override
Reset the position to the start of the paragraph.
std::vector< UniscribeRun > ranges
All runs of the text.
std::unique_ptr< const Line > NextLine(int max_width) override
Construct a new line with a maximum width.
std::vector< UniscribeRun >::iterator cur_range
The next run to be output.
size_t Prev(IterType what) override
Move the cursor back by one iteration unit.
std::vector< CharInfo > str_info
Break information for each code point.
void SetString(std::string_view s) override
Set a new iteration string.
size_t cur_pos
Current iteration position.
std::vector< size_t > utf16_to_utf8
Mapping from UTF-16 code point position to index in the UTF-8 source string.
size_t Next(IterType what) override
Advance the cursor by one iteration unit.
size_t SetCurPosition(size_t pos) override
Change the current string cursor.
Constant span of UTF-8 encoded data.
Definition utf8.hpp:28
Control codes that are embedded in the translation strings.
Functions related to debugging.
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
std::vector< std::pair< int, Font > > FontMap
Mapping from index to font.
Definition gfx_layout.h:139
FontSize
Available font sizes.
Definition gfx_type.h:248
Information about languages and their files.
const LanguageMetadata * _current_language
The currently loaded language.
Definition strings.cpp:53
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.
static EnumIndexArray< SCRIPT_CACHE, FontSize, FontSize::End > _script_cache
Uniscribe cache for internal font information, cleared when OTTD changes fonts.
static std::vector< SCRIPT_ITEM > UniscribeItemizeString(UniscribeParagraphLayoutFactory::CharType *buff, int32_t length)
Break a string into language formatting ranges.
static HFONT HFontFromFont(const Font &font)
Load the matching native Windows font.
static bool UniscribeShapeRun(const UniscribeParagraphLayoutFactory::CharType *buff, UniscribeRun &range)
Determine the glyph positions for a run.
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_RTL
Text is written right-to-left by default.
Contains all information about a run of characters.
UniscribeRun(int pos, int len, const Font &font, SCRIPT_ANALYSIS &sa)
Construct a new Uniscribe Run object.
Handling of UTF-8 encoded data.
Declarations of functions for MS windows systems.
Functions related to zooming.
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition zoom_func.h:107