OpenTTD
gfx_layout.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "stdafx.h"
11 #include "gfx_layout.h"
12 #include "string_func.h"
13 #include "strings_func.h"
14 #include "debug.h"
15 
16 #include "table/control_codes.h"
17 
18 #ifdef WITH_ICU_LX
19 #include <unicode/ustring.h>
20 #endif /* WITH_ICU_LX */
21 
22 #ifdef WITH_UNISCRIBE
24 #endif /* WITH_UNISCRIBE */
25 
26 #ifdef WITH_COCOA
27 #include "os/macosx/string_osx.h"
28 #endif
29 
30 #include "safeguards.h"
31 
32 
34 Layouter::LineCache *Layouter::linecache;
35 
38 
39 
45 Font::Font(FontSize size, TextColour colour) :
46  fc(FontCache::Get(size)), colour(colour)
47 {
48  assert(size < FS_END);
49 }
50 
51 #ifdef WITH_ICU_LX
52 /* Implementation details of LEFontInstance */
53 
54 le_int32 Font::getUnitsPerEM() const
55 {
56  return this->fc->GetUnitsPerEM();
57 }
58 
59 le_int32 Font::getAscent() const
60 {
61  return this->fc->GetAscender();
62 }
63 
64 le_int32 Font::getDescent() const
65 {
66  return -this->fc->GetDescender();
67 }
68 
69 le_int32 Font::getLeading() const
70 {
71  return this->fc->GetHeight();
72 }
73 
74 float Font::getXPixelsPerEm() const
75 {
76  return (float)this->fc->GetHeight();
77 }
78 
79 float Font::getYPixelsPerEm() const
80 {
81  return (float)this->fc->GetHeight();
82 }
83 
84 float Font::getScaleFactorX() const
85 {
86  return 1.0f;
87 }
88 
89 float Font::getScaleFactorY() const
90 {
91  return 1.0f;
92 }
93 
94 const void *Font::getFontTable(LETag tableTag) const
95 {
96  size_t length;
97  return this->getFontTable(tableTag, length);
98 }
99 
100 const void *Font::getFontTable(LETag tableTag, size_t &length) const
101 {
102  return this->fc->GetFontTable(tableTag, length);
103 }
104 
105 LEGlyphID Font::mapCharToGlyph(LEUnicode32 ch) const
106 {
107  if (IsTextDirectionChar(ch)) return 0;
108  return this->fc->MapCharToGlyph(ch);
109 }
110 
111 void Font::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
112 {
113  advance.fX = glyph == 0xFFFF ? 0 : this->fc->GetGlyphWidth(glyph);
114  advance.fY = 0;
115 }
116 
117 le_bool Font::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
118 {
119  return FALSE;
120 }
121 
126  icu::ParagraphLayout *p;
127 public:
130  const icu::ParagraphLayout::VisualRun *vr;
131 
132  public:
133  ICUVisualRun(const icu::ParagraphLayout::VisualRun *vr) : vr(vr) { }
134 
135  const Font *GetFont() const override { return (const Font*)vr->getFont(); }
136  int GetGlyphCount() const override { return vr->getGlyphCount(); }
137  const GlyphID *GetGlyphs() const override { return vr->getGlyphs(); }
138  const float *GetPositions() const override { return vr->getPositions(); }
139  int GetLeading() const override { return vr->getLeading(); }
140  const int *GetGlyphToCharMap() const override { return vr->getGlyphToCharMap(); }
141  };
142 
144  class ICULine : public std::vector<ICUVisualRun>, public ParagraphLayouter::Line {
145  icu::ParagraphLayout::Line *l;
146 
147  public:
148  ICULine(icu::ParagraphLayout::Line *l) : l(l)
149  {
150  for (int i = 0; i < l->countRuns(); i++) {
151  this->emplace_back(l->getVisualRun(i));
152  }
153  }
154  ~ICULine() override { delete l; }
155 
156  int GetLeading() const override { return l->getLeading(); }
157  int GetWidth() const override { return l->getWidth(); }
158  int CountRuns() const override { return l->countRuns(); }
159  const ParagraphLayouter::VisualRun &GetVisualRun(int run) const override { return this->at(run); }
160 
161  int GetInternalCharLength(WChar c) const override
162  {
163  /* ICU uses UTF-16 internally which means we need to account for surrogate pairs. */
164  return Utf8CharLen(c) < 4 ? 1 : 2;
165  }
166  };
167 
168  ICUParagraphLayout(icu::ParagraphLayout *p) : p(p) { }
169  ~ICUParagraphLayout() override { delete p; }
170  void Reflow() override { p->reflow(); }
171 
172  std::unique_ptr<const Line> NextLine(int max_width) override
173  {
174  icu::ParagraphLayout::Line *l = p->nextLine(max_width);
175  return std::unique_ptr<const Line>(l == nullptr ? nullptr : new ICULine(l));
176  }
177 };
178 
183 public:
185  typedef UChar CharType;
187  static const bool SUPPORTS_RTL = true;
188 
189  static ParagraphLayouter *GetParagraphLayout(UChar *buff, UChar *buff_end, FontMap &fontMapping)
190  {
191  int32 length = buff_end - buff;
192 
193  if (length == 0) {
194  /* ICU's ParagraphLayout cannot handle empty strings, so fake one. */
195  buff[0] = ' ';
196  length = 1;
197  fontMapping.back().first++;
198  }
199 
200  /* Fill ICU's FontRuns with the right data. */
201  icu::FontRuns runs(fontMapping.size());
202  for (auto &pair : fontMapping) {
203  runs.add(pair.second, pair.first);
204  }
205 
206  LEErrorCode status = LE_NO_ERROR;
207  /* ParagraphLayout does not copy "buff", so it must stay valid.
208  * "runs" is copied according to the ICU source, but the documentation does not specify anything, so this might break somewhen. */
209  icu::ParagraphLayout *p = new icu::ParagraphLayout(buff, length, &runs, nullptr, nullptr, nullptr, _current_text_dir == TD_RTL ? 1 : 0, false, status);
210  if (status != LE_NO_ERROR) {
211  delete p;
212  return nullptr;
213  }
214 
215  return new ICUParagraphLayout(p);
216  }
217 
218  static size_t AppendToBuffer(UChar *buff, const UChar *buffer_last, WChar c)
219  {
220  /* Transform from UTF-32 to internal ICU format of UTF-16. */
221  int32 length = 0;
222  UErrorCode err = U_ZERO_ERROR;
223  u_strFromUTF32(buff, buffer_last - buff, &length, (UChar32*)&c, 1, &err);
224  return length;
225  }
226 };
227 #endif /* WITH_ICU_LX */
228 
229 /*** Paragraph layout ***/
249 public:
252  Font *font;
254  float *positions;
257 
258  public:
259  FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x);
260  FallbackVisualRun(FallbackVisualRun &&other) noexcept;
261  ~FallbackVisualRun() override;
262  const Font *GetFont() const override;
263  int GetGlyphCount() const override;
264  const GlyphID *GetGlyphs() const override;
265  const float *GetPositions() const override;
266  int GetLeading() const override;
267  const int *GetGlyphToCharMap() const override;
268  };
269 
271  class FallbackLine : public std::vector<FallbackVisualRun>, public ParagraphLayouter::Line {
272  public:
273  int GetLeading() const override;
274  int GetWidth() const override;
275  int CountRuns() const override;
276  const ParagraphLayouter::VisualRun &GetVisualRun(int run) const override;
277 
278  int GetInternalCharLength(WChar c) const override { return 1; }
279  };
280 
282  const WChar *buffer;
284 
285  FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs);
286  void Reflow() override;
287  std::unique_ptr<const Line> NextLine(int max_width) override;
288 };
289 
294 public:
296  typedef WChar CharType;
298  static const bool SUPPORTS_RTL = false;
299 
307  static ParagraphLayouter *GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
308  {
309  return new FallbackParagraphLayout(buff, buff_end - buff, fontMapping);
310  }
311 
319  static size_t AppendToBuffer(WChar *buff, const WChar *buffer_last, WChar c)
320  {
321  *buff = c;
322  return 1;
323  }
324 };
325 
333 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(Font *font, const WChar *chars, int char_count, int x) :
334  font(font), glyph_count(char_count)
335 {
336  this->glyphs = MallocT<GlyphID>(this->glyph_count);
337  this->glyph_to_char = MallocT<int>(this->glyph_count);
338 
339  /* Positions contains the location of the begin of each of the glyphs, and the end of the last one. */
340  this->positions = MallocT<float>(this->glyph_count * 2 + 2);
341  this->positions[0] = x;
342  this->positions[1] = 0;
343 
344  for (int i = 0; i < this->glyph_count; i++) {
345  this->glyphs[i] = font->fc->MapCharToGlyph(chars[i]);
346  this->positions[2 * i + 2] = this->positions[2 * i] + font->fc->GetGlyphWidth(this->glyphs[i]);
347  this->positions[2 * i + 3] = 0;
348  this->glyph_to_char[i] = i;
349  }
350 }
351 
354 {
355  this->positions = other.positions;
356  this->glyph_to_char = other.glyph_to_char;
357  this->glyphs = other.glyphs;
358 
359  other.positions = nullptr;
360  other.glyph_to_char = nullptr;
361  other.glyphs = nullptr;
362 }
363 
366 {
367  free(this->positions);
368  free(this->glyph_to_char);
369  free(this->glyphs);
370 }
371 
377 {
378  return this->font;
379 }
380 
386 {
387  return this->glyph_count;
388 }
389 
395 {
396  return this->glyphs;
397 }
398 
404 {
405  return this->positions;
406 }
407 
413 {
414  return this->glyph_to_char;
415 }
416 
422 {
423  return this->GetFont()->fc->GetHeight();
424 }
425 
431 {
432  int leading = 0;
433  for (const auto &run : *this) {
434  leading = max(leading, run.GetLeading());
435  }
436 
437  return leading;
438 }
439 
445 {
446  if (this->size() == 0) return 0;
447 
448  /*
449  * The last X position of a run contains is the end of that run.
450  * Since there is no left-to-right support, taking this value of
451  * the last run gives us the end of the line and thus the width.
452  */
453  const auto &run = this->GetVisualRun(this->CountRuns() - 1);
454  return (int)run.GetPositions()[run.GetGlyphCount() * 2];
455 }
456 
462 {
463  return (uint)this->size();
464 }
465 
471 {
472  return this->at(run);
473 }
474 
481 FallbackParagraphLayout::FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs) : buffer_begin(buffer), buffer(buffer), runs(runs)
482 {
483  assert(runs.End()[-1].first == length);
484 }
485 
490 {
491  this->buffer = this->buffer_begin;
492 }
493 
499 std::unique_ptr<const ParagraphLayouter::Line> FallbackParagraphLayout::NextLine(int max_width)
500 {
501  /* Simple idea:
502  * - split a line at a newline character, or at a space where we can break a line.
503  * - split for a visual run whenever a new line happens, or the font changes.
504  */
505  if (this->buffer == nullptr) return nullptr;
506 
507  std::unique_ptr<FallbackLine> l(new FallbackLine());
508 
509  if (*this->buffer == '\0') {
510  /* Only a newline. */
511  this->buffer = nullptr;
512  l->emplace_back(this->runs.front().second, this->buffer, 0, 0);
513  return l;
514  }
515 
516  int offset = this->buffer - this->buffer_begin;
517  FontMap::iterator iter = this->runs.data();
518  while (iter->first <= offset) {
519  iter++;
520  assert(iter != this->runs.End());
521  }
522 
523  const FontCache *fc = iter->second->fc;
524  const WChar *next_run = this->buffer_begin + iter->first;
525 
526  const WChar *begin = this->buffer;
527  const WChar *last_space = nullptr;
528  const WChar *last_char;
529  int width = 0;
530  for (;;) {
531  WChar c = *this->buffer;
532  last_char = this->buffer;
533 
534  if (c == '\0') {
535  this->buffer = nullptr;
536  break;
537  }
538 
539  if (this->buffer == next_run) {
540  int w = l->GetWidth();
541  l->emplace_back(iter->second, begin, this->buffer - begin, w);
542  iter++;
543  assert(iter != this->runs.End());
544 
545  next_run = this->buffer_begin + iter->first;
546  begin = this->buffer;
547 
548  last_space = nullptr;
549  }
550 
551  if (IsWhitespace(c)) last_space = this->buffer;
552 
553  if (IsPrintable(c) && !IsTextDirectionChar(c)) {
554  int char_width = GetCharacterWidth(fc->GetSize(), c);
555  width += char_width;
556  if (width > max_width) {
557  /* The string is longer than maximum width so we need to decide
558  * what to do with it. */
559  if (width == char_width) {
560  /* The character is wider than allowed width; don't know
561  * what to do with this case... bail out! */
562  this->buffer = nullptr;
563  return l;
564  }
565 
566  if (last_space == nullptr) {
567  /* No space has been found. Just terminate at our current
568  * location. This usually happens for languages that do not
569  * require spaces in strings, like Chinese, Japanese and
570  * Korean. For other languages terminating mid-word might
571  * not be the best, but terminating the whole string instead
572  * of continuing the word at the next line is worse. */
573  last_char = this->buffer;
574  } else {
575  /* A space is found; perfect place to terminate */
576  this->buffer = last_space + 1;
577  last_char = last_space;
578  }
579  break;
580  }
581  }
582 
583  this->buffer++;
584  }
585 
586  if (l->size() == 0 || last_char - begin != 0) {
587  int w = l->GetWidth();
588  l->emplace_back(iter->second, begin, last_char - begin, w);
589  }
590  return l;
591 }
592 
602 template <typename T>
603 static inline void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
604 {
605  if (line.buffer != nullptr) free(line.buffer);
606 
607  typename T::CharType *buff_begin = MallocT<typename T::CharType>(DRAW_STRING_BUFFER);
608  const typename T::CharType *buffer_last = buff_begin + DRAW_STRING_BUFFER;
609  typename T::CharType *buff = buff_begin;
610  FontMap &fontMapping = line.runs;
611  Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
612 
613  line.buffer = buff_begin;
614  fontMapping.clear();
615 
616  /*
617  * Go through the whole string while adding Font instances to the font map
618  * whenever the font changes, and convert the wide characters into a format
619  * usable by ParagraphLayout.
620  */
621  for (; buff < buffer_last;) {
622  WChar c = Utf8Consume(const_cast<const char **>(&str));
623  if (c == '\0' || c == '\n') {
624  break;
625  } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
626  state.SetColour((TextColour)(c - SCC_BLUE));
627  } else if (c == SCC_PUSH_COLOUR) {
628  state.PushColour();
629  } else if (c == SCC_POP_COLOUR) {
630  state.PopColour();
631  } else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
632  state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
633  } else {
634  /* Filter out text direction characters that shouldn't be drawn, and
635  * will not be handled in the fallback non ICU case because they are
636  * mostly needed for RTL languages which need more ICU support. */
637  if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
638  buff += T::AppendToBuffer(buff, buffer_last, c);
639  continue;
640  }
641 
642  if (!fontMapping.Contains(buff - buff_begin)) {
643  fontMapping.Insert(buff - buff_begin, f);
644  }
645  f = Layouter::GetFont(state.fontsize, state.cur_colour);
646  }
647 
648  /* Better safe than sorry. */
649  *buff = '\0';
650 
651  if (!fontMapping.Contains(buff - buff_begin)) {
652  fontMapping.Insert(buff - buff_begin, f);
653  }
654  line.layout = T::GetParagraphLayout(buff_begin, buff, fontMapping);
655  line.state_after = state;
656 }
657 
665 Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsize) : string(str)
666 {
667  FontState state(colour, fontsize);
668  WChar c = 0;
669 
670  do {
671  /* Scan string for end of line */
672  const char *lineend = str;
673  for (;;) {
674  size_t len = Utf8Decode(&c, lineend);
675  if (c == '\0' || c == '\n') break;
676  lineend += len;
677  }
678 
679  LineCacheItem& line = GetCachedParagraphLayout(str, lineend - str, state);
680  if (line.layout != nullptr) {
681  /* Line is in cache */
682  str = lineend + 1;
683  state = line.state_after;
684  line.layout->Reflow();
685  } else {
686  /* Line is new, layout it */
687  FontState old_state = state;
688 #if defined(WITH_ICU_LX) || defined(WITH_UNISCRIBE) || defined(WITH_COCOA)
689  const char *old_str = str;
690 #endif
691 
692 #ifdef WITH_ICU_LX
693  GetLayouter<ICUParagraphLayoutFactory>(line, str, state);
694  if (line.layout == nullptr) {
695  static bool warned = false;
696  if (!warned) {
697  DEBUG(misc, 0, "ICU layouter bailed on the font. Falling back to the fallback layouter");
698  warned = true;
699  }
700 
701  state = old_state;
702  str = old_str;
703  }
704 #endif
705 
706 #ifdef WITH_UNISCRIBE
707  if (line.layout == nullptr) {
708  GetLayouter<UniscribeParagraphLayoutFactory>(line, str, state);
709  if (line.layout == nullptr) {
710  state = old_state;
711  str = old_str;
712  }
713  }
714 #endif
715 
716 #ifdef WITH_COCOA
717  if (line.layout == nullptr) {
718  GetLayouter<CoreTextParagraphLayoutFactory>(line, str, state);
719  if (line.layout == nullptr) {
720  state = old_state;
721  str = old_str;
722  }
723  }
724 #endif
725 
726  if (line.layout == nullptr) {
727  GetLayouter<FallbackParagraphLayoutFactory>(line, str, state);
728  }
729  }
730 
731  /* Move all lines into a local cache so we can reuse them later on more easily. */
732  for (;;) {
733  auto l = line.layout->NextLine(maxw);
734  if (l == nullptr) break;
735  this->push_back(std::move(l));
736  }
737  } while (c != '\0');
738 }
739 
745 {
746  Dimension d = { 0, 0 };
747  for (const auto &l : *this) {
748  d.width = max<uint>(d.width, l->GetWidth());
749  d.height += l->GetLeading();
750  }
751  return d;
752 }
753 
760 Point Layouter::GetCharPosition(const char *ch) const
761 {
762  /* Find the code point index which corresponds to the char
763  * pointer into our UTF-8 source string. */
764  size_t index = 0;
765  const char *str = this->string;
766  while (str < ch) {
767  WChar c;
768  size_t len = Utf8Decode(&c, str);
769  if (c == '\0' || c == '\n') break;
770  str += len;
771  index += this->front()->GetInternalCharLength(c);
772  }
773 
774  if (str == ch) {
775  /* Valid character. */
776  const auto &line = this->front();
777 
778  /* Pointer to the end-of-string/line marker? Return total line width. */
779  if (*ch == '\0' || *ch == '\n') {
780  Point p = { line->GetWidth(), 0 };
781  return p;
782  }
783 
784  /* Scan all runs until we've found our code point index. */
785  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
786  const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
787 
788  for (int i = 0; i < run.GetGlyphCount(); i++) {
789  /* Matching glyph? Return position. */
790  if ((size_t)run.GetGlyphToCharMap()[i] == index) {
791  Point p = { (int)run.GetPositions()[i * 2], (int)run.GetPositions()[i * 2 + 1] };
792  return p;
793  }
794  }
795  }
796  }
797 
798  Point p = { 0, 0 };
799  return p;
800 }
801 
807 const char *Layouter::GetCharAtPosition(int x) const
808 {
809  const auto &line = this->front();
810 
811  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
812  const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
813 
814  for (int i = 0; i < run.GetGlyphCount(); i++) {
815  /* Not a valid glyph (empty). */
816  if (run.GetGlyphs()[i] == 0xFFFF) continue;
817 
818  int begin_x = (int)run.GetPositions()[i * 2];
819  int end_x = (int)run.GetPositions()[i * 2 + 2];
820 
821  if (IsInsideMM(x, begin_x, end_x)) {
822  /* Found our glyph, now convert to UTF-8 string index. */
823  size_t index = run.GetGlyphToCharMap()[i];
824 
825  size_t cur_idx = 0;
826  for (const char *str = this->string; *str != '\0'; ) {
827  if (cur_idx == index) return str;
828 
829  WChar c = Utf8Consume(&str);
830  cur_idx += line->GetInternalCharLength(c);
831  }
832  }
833  }
834  }
835 
836  return nullptr;
837 }
838 
843 {
844  FontColourMap::iterator it = fonts[size].Find(colour);
845  if (it != fonts[size].End()) return it->second;
846 
847  Font *f = new Font(size, colour);
848  fonts[size].emplace_back(colour, f);
849  return f;
850 }
851 
857 {
858  for (auto &pair : fonts[size]) {
859  delete pair.second;
860  }
861  fonts[size].clear();
862 
863  /* We must reset the linecache since it references the just freed fonts */
864  ResetLineCache();
865 
866 #if defined(WITH_UNISCRIBE)
867  UniscribeResetScriptCache(size);
868 #endif
869 #if defined(WITH_COCOA)
870  MacOSResetScriptCache(size);
871 #endif
872 }
873 
882 Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
883 {
884  if (linecache == nullptr) {
885  /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
886  linecache = new LineCache();
887  }
888 
889  LineCacheKey key;
890  key.state_before = state;
891  key.str.assign(str, len);
892  return (*linecache)[key];
893 }
894 
899 {
900  if (linecache != nullptr) linecache->clear();
901 }
902 
907 {
908  if (linecache != nullptr) {
909  /* TODO LRU cache would be fancy, but not exactly necessary */
910  if (linecache->size() > 4096) ResetLineCache();
911  }
912 }
Functions related to OTTD&#39;s strings.
Functions related to laying out text on Win32.
UChar CharType
Helper for GetLayouter, to get the right type.
Definition: gfx_layout.cpp:185
int GetWidth() const override
Get the width of this line.
Definition: gfx_layout.cpp:444
Control codes that are embedded in the translation strings.
FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x)
Create the visual run.
Definition: gfx_layout.cpp:333
Helper class to construct a new FallbackParagraphLayout.
Definition: gfx_layout.cpp:293
static void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
Helper for getting a ParagraphLayouter of the given type.
Definition: gfx_layout.cpp:603
const icu::ParagraphLayout::VisualRun * vr
The actual ICU vr.
Definition: gfx_layout.cpp:130
Functions related to debugging.
void * buffer
Accessed by both ICU&#39;s and our ParagraphLayout::nextLine.
Definition: gfx_layout.h:172
FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs)
Create a new paragraph layouter.
Definition: gfx_layout.cpp:481
std::vector< Pair >::const_iterator Find(const T &key) const
Finds given key in this map.
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:183
const WChar * buffer_begin
Begin of the buffer.
Definition: gfx_layout.cpp:281
static bool IsWhitespace(WChar c)
Check whether UNICODE character is whitespace or not, i.e.
Definition: string_func.h:240
Implementation of simple mapping class.
std::string str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:157
Key into the linecache.
Definition: gfx_layout.h:155
int * glyph_to_char
The char index of the glyphs.
Definition: gfx_layout.cpp:255
byte GetCharacterWidth(FontSize size, WChar key)
Return width of character glyph.
Definition: gfx.cpp:1152
int CountRuns() const override
Get the number of runs in this line.
Definition: gfx_layout.cpp:461
static bool IsTextDirectionChar(WChar c)
Is the given character a text direction character.
Definition: string_func.h:208
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:83
const char * GetCharAtPosition(int x) const
Get the character that is at a position.
Definition: gfx_layout.cpp:807
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:446
void Reflow() override
Reset the position to the start of the paragraph.
Definition: gfx_layout.cpp:489
const Font * GetFont() const override
Get the font associated with this run.
Definition: gfx_layout.cpp:376
int glyph_count
The number of glyphs.
Definition: gfx_layout.cpp:256
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:120
void MacOSResetScriptCache(FontSize size)
Delete CoreText font reference for a specific font size.
Definition: string_osx.cpp:260
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:24
const float * GetPositions() const override
Get the positions of this run.
Definition: gfx_layout.cpp:403
Functions related to laying out the texts.
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:173
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition: gfx_layout.cpp:842
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:74
icu::ParagraphLayout::Line * l
The actual ICU line.
Definition: gfx_layout.cpp:145
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:264
Font * font
The font used to layout these.
Definition: gfx_layout.cpp:252
Functions related to low-level strings.
A single line worth of VisualRuns.
Definition: gfx_layout.h:132
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.cpp:129
Simple pair of data.
Interface to glue fallback and normal layouter into one.
Definition: gfx_layout.h:115
FontMap & runs
The fonts we have to use for this paragraph.
Definition: gfx_layout.cpp:283
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:245
WChar CharType
Helper for GetLayouter, to get the right type.
Definition: gfx_layout.cpp:296
Functions related to localized text support on OSX.
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:46
static int8 Utf8CharLen(WChar c)
Return the length of a UTF-8 encoded character.
Definition: string_func.h:97
Class handling the splitting of a paragraph of text into lines and visual runs.
Definition: gfx_layout.cpp:248
static size_t AppendToBuffer(WChar *buff, const WChar *buffer_last, WChar c)
Append a wide character to the internal buffer.
Definition: gfx_layout.cpp:319
bool Insert(const T &key, const U &data)
Adds new item to this map.
int GetLeading() const override
Get the height of the line.
Definition: gfx_layout.cpp:430
GlyphID * glyphs
The glyphs we&#39;re drawing.
Definition: gfx_layout.cpp:253
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:856
Font cache for basic fonts.
Definition: fontcache.h:21
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:744
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
float * positions
The positions of the glyphs.
Definition: gfx_layout.cpp:254
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:33
int GetGlyphCount() const override
Get the number of glyphs in this run.
Definition: gfx_layout.cpp:385
~FallbackVisualRun() override
Free all data.
Definition: gfx_layout.cpp:365
int GetLeading() const override
Get the height of this font.
Definition: gfx_layout.cpp:421
const ParagraphLayouter::VisualRun & GetVisualRun(int run) const override
Get a specific visual run.
Definition: gfx_layout.cpp:470
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:55
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:35
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:48
FontSize fontsize
Current font size.
Definition: gfx_layout.h:34
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition: gfx_layout.h:188
FontSize
Available font sizes.
Definition: gfx_type.h:201
FontState state_after
Font state after the line.
Definition: gfx_layout.h:175
A single line worth of VisualRuns.
Definition: gfx_layout.cpp:144
Wrapper for doing layouts with ICU.
Definition: gfx_layout.cpp:125
void PushColour()
Push the current colour on to the stack.
Definition: gfx_layout.h:65
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:156
Coordinates of a point in 2D.
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
Definition: gfx_layout.cpp:906
static void ResetLineCache()
Clear line cache.
Definition: gfx_layout.cpp:898
A single line worth of VisualRuns.
Definition: gfx_layout.cpp:271
const int * GetGlyphToCharMap() const override
Get the glyph-to-character map for this visual run.
Definition: gfx_layout.cpp:412
static LineCacheItem & GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
Get reference to cache item.
Definition: gfx_layout.cpp:882
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.cpp:251
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:129
icu::ParagraphLayout * p
The actual ICU paragraph layout.
Definition: gfx_layout.cpp:126
Text is written right-to-left by default.
Definition: strings_type.h:24
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
Helper class to construct a new ICUParagraphLayout.
Definition: gfx_layout.cpp:182
std::unique_ptr< const Line > NextLine(int max_width) override
Construct a new line with a maximum width.
Definition: gfx_layout.cpp:499
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:176
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
const WChar * buffer
The current location in the buffer.
Definition: gfx_layout.cpp:282
Layouter(const char *str, int maxw=INT32_MAX, TextColour colour=TC_FROMSTRING, FontSize fontsize=FS_NORMAL)
Create a new layouter.
Definition: gfx_layout.cpp:665
Dimensions (a width and height) of a rectangle in 2D.
const GlyphID * GetGlyphs() const override
Get the glyphs of this run.
Definition: gfx_layout.cpp:394
Point GetCharPosition(const char *ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:760
const char * string
Pointer to the original string.
Definition: gfx_layout.h:152
static ParagraphLayouter * GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
Get the actual ParagraphLayout for the given buffer.
Definition: gfx_layout.cpp:307
Item in the linecache.
Definition: gfx_layout.h:170