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