OpenTTD Source: src/os/windows/string_uniscribe.cpp Source File

OpenTTD Source 12.2

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