OpenTTD Source  13.0-beta2
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 
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  const bool isbuiltin = font->fc->IsBuiltInFont();
337 
338  this->glyphs = MallocT<GlyphID>(this->glyph_count);
339  this->glyph_to_char = MallocT<int>(this->glyph_count);
340 
341  /* Positions contains the location of the begin of each of the glyphs, and the end of the last one. */
342  this->positions = MallocT<float>(this->glyph_count * 2 + 2);
343  this->positions[0] = x;
344 
345  for (int i = 0; i < this->glyph_count; i++) {
346  this->glyphs[i] = font->fc->MapCharToGlyph(chars[i]);
347  if (isbuiltin) {
348  this->positions[2 * i + 1] = font->fc->GetAscender(); // Apply sprite font's ascender.
349  } else if (chars[i] >= SCC_SPRITE_START && chars[i] <= SCC_SPRITE_END) {
350  this->positions[2 * i + 1] = font->fc->GetAscender() - font->fc->GetGlyph(this->glyphs[i])->height - 1; // Align sprite glyphs to font baseline.
351  } else {
352  this->positions[2 * i + 1] = 0; // No ascender adjustment.
353  }
354  this->positions[2 * i + 2] = this->positions[2 * i] + font->fc->GetGlyphWidth(this->glyphs[i]);
355  this->glyph_to_char[i] = i;
356  }
357 }
358 
360 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(FallbackVisualRun &&other) noexcept : font(other.font), glyph_count(other.glyph_count)
361 {
362  this->positions = other.positions;
363  this->glyph_to_char = other.glyph_to_char;
364  this->glyphs = other.glyphs;
365 
366  other.positions = nullptr;
367  other.glyph_to_char = nullptr;
368  other.glyphs = nullptr;
369 }
370 
373 {
374  free(this->positions);
375  free(this->glyph_to_char);
376  free(this->glyphs);
377 }
378 
384 {
385  return this->font;
386 }
387 
393 {
394  return this->glyph_count;
395 }
396 
402 {
403  return this->glyphs;
404 }
405 
411 {
412  return this->positions;
413 }
414 
420 {
421  return this->glyph_to_char;
422 }
423 
429 {
430  return this->GetFont()->fc->GetHeight();
431 }
432 
438 {
439  int leading = 0;
440  for (const auto &run : *this) {
441  leading = std::max(leading, run.GetLeading());
442  }
443 
444  return leading;
445 }
446 
452 {
453  if (this->size() == 0) return 0;
454 
455  /*
456  * The last X position of a run contains is the end of that run.
457  * Since there is no left-to-right support, taking this value of
458  * the last run gives us the end of the line and thus the width.
459  */
460  const auto &run = this->GetVisualRun(this->CountRuns() - 1);
461  return (int)run.GetPositions()[run.GetGlyphCount() * 2];
462 }
463 
469 {
470  return (uint)this->size();
471 }
472 
478 {
479  return this->at(run);
480 }
481 
489 {
490  assert(runs.End()[-1].first == length);
491 }
492 
497 {
498  this->buffer = this->buffer_begin;
499 }
500 
506 std::unique_ptr<const ParagraphLayouter::Line> FallbackParagraphLayout::NextLine(int max_width)
507 {
508  /* Simple idea:
509  * - split a line at a newline character, or at a space where we can break a line.
510  * - split for a visual run whenever a new line happens, or the font changes.
511  */
512  if (this->buffer == nullptr) return nullptr;
513 
514  std::unique_ptr<FallbackLine> l(new FallbackLine());
515 
516  if (*this->buffer == '\0') {
517  /* Only a newline. */
518  this->buffer = nullptr;
519  l->emplace_back(this->runs.front().second, this->buffer, 0, 0);
520  return l;
521  }
522 
523  int offset = this->buffer - this->buffer_begin;
524  FontMap::iterator iter = this->runs.data();
525  while (iter->first <= offset) {
526  iter++;
527  assert(iter != this->runs.End());
528  }
529 
530  const FontCache *fc = iter->second->fc;
531  const WChar *next_run = this->buffer_begin + iter->first;
532 
533  const WChar *begin = this->buffer;
534  const WChar *last_space = nullptr;
535  const WChar *last_char;
536  int width = 0;
537  for (;;) {
538  WChar c = *this->buffer;
539  last_char = this->buffer;
540 
541  if (c == '\0') {
542  this->buffer = nullptr;
543  break;
544  }
545 
546  if (this->buffer == next_run) {
547  int w = l->GetWidth();
548  l->emplace_back(iter->second, begin, this->buffer - begin, w);
549  iter++;
550  assert(iter != this->runs.End());
551 
552  next_run = this->buffer_begin + iter->first;
553  begin = this->buffer;
554 
555  last_space = nullptr;
556  }
557 
558  if (IsWhitespace(c)) last_space = this->buffer;
559 
560  if (IsPrintable(c) && !IsTextDirectionChar(c)) {
561  int char_width = GetCharacterWidth(fc->GetSize(), c);
562  width += char_width;
563  if (width > max_width) {
564  /* The string is longer than maximum width so we need to decide
565  * what to do with it. */
566  if (width == char_width) {
567  /* The character is wider than allowed width; don't know
568  * what to do with this case... bail out! */
569  this->buffer = nullptr;
570  return l;
571  }
572 
573  if (last_space == nullptr) {
574  /* No space has been found. Just terminate at our current
575  * location. This usually happens for languages that do not
576  * require spaces in strings, like Chinese, Japanese and
577  * Korean. For other languages terminating mid-word might
578  * not be the best, but terminating the whole string instead
579  * of continuing the word at the next line is worse. */
580  last_char = this->buffer;
581  } else {
582  /* A space is found; perfect place to terminate */
583  this->buffer = last_space + 1;
584  last_char = last_space;
585  }
586  break;
587  }
588  }
589 
590  this->buffer++;
591  }
592 
593  if (l->size() == 0 || last_char - begin != 0) {
594  int w = l->GetWidth();
595  l->emplace_back(iter->second, begin, last_char - begin, w);
596  }
597  return l;
598 }
599 
609 template <typename T>
610 static inline void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
611 {
612  if (line.buffer != nullptr) free(line.buffer);
613 
614  typename T::CharType *buff_begin = MallocT<typename T::CharType>(DRAW_STRING_BUFFER);
615  const typename T::CharType *buffer_last = buff_begin + DRAW_STRING_BUFFER;
616  typename T::CharType *buff = buff_begin;
617  FontMap &fontMapping = line.runs;
618  Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
619 
620  line.buffer = buff_begin;
621  fontMapping.clear();
622 
623  /*
624  * Go through the whole string while adding Font instances to the font map
625  * whenever the font changes, and convert the wide characters into a format
626  * usable by ParagraphLayout.
627  */
628  for (; buff < buffer_last;) {
629  WChar c = Utf8Consume(const_cast<const char **>(&str));
630  if (c == '\0' || c == '\n') {
631  break;
632  } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
633  state.SetColour((TextColour)(c - SCC_BLUE));
634  } else if (c == SCC_PUSH_COLOUR) {
635  state.PushColour();
636  } else if (c == SCC_POP_COLOUR) {
637  state.PopColour();
638  } else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
639  state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
640  } else {
641  /* Filter out non printable characters */
642  if (!IsPrintable(c)) continue;
643  /* Filter out text direction characters that shouldn't be drawn, and
644  * will not be handled in the fallback non ICU case because they are
645  * mostly needed for RTL languages which need more ICU support. */
646  if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
647  buff += T::AppendToBuffer(buff, buffer_last, c);
648  continue;
649  }
650 
651  if (!fontMapping.Contains(buff - buff_begin)) {
652  fontMapping.Insert(buff - buff_begin, f);
653  }
654  f = Layouter::GetFont(state.fontsize, state.cur_colour);
655  }
656 
657  /* Better safe than sorry. */
658  *buff = '\0';
659 
660  if (!fontMapping.Contains(buff - buff_begin)) {
661  fontMapping.Insert(buff - buff_begin, f);
662  }
663  line.layout = T::GetParagraphLayout(buff_begin, buff, fontMapping);
664  line.state_after = state;
665 }
666 
674 Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsize) : string(str)
675 {
676  FontState state(colour, fontsize);
677  WChar c = 0;
678 
679  do {
680  /* Scan string for end of line */
681  const char *lineend = str;
682  for (;;) {
683  size_t len = Utf8Decode(&c, lineend);
684  if (c == '\0' || c == '\n') break;
685  lineend += len;
686  }
687 
688  LineCacheItem& line = GetCachedParagraphLayout(str, lineend - str, state);
689  if (line.layout != nullptr) {
690  /* Line is in cache */
691  str = lineend + 1;
692  state = line.state_after;
693  line.layout->Reflow();
694  } else {
695  /* Line is new, layout it */
696  FontState old_state = state;
697 #if defined(WITH_ICU_LX) || defined(WITH_UNISCRIBE) || defined(WITH_COCOA)
698  const char *old_str = str;
699 #endif
700 
701 #ifdef WITH_ICU_LX
702  GetLayouter<ICUParagraphLayoutFactory>(line, str, state);
703  if (line.layout == nullptr) {
704  static bool warned = false;
705  if (!warned) {
706  Debug(misc, 0, "ICU layouter bailed on the font. Falling back to the fallback layouter");
707  warned = true;
708  }
709 
710  state = old_state;
711  str = old_str;
712  }
713 #endif
714 
715 #ifdef WITH_UNISCRIBE
716  if (line.layout == nullptr) {
717  GetLayouter<UniscribeParagraphLayoutFactory>(line, str, state);
718  if (line.layout == nullptr) {
719  state = old_state;
720  str = old_str;
721  }
722  }
723 #endif
724 
725 #ifdef WITH_COCOA
726  if (line.layout == nullptr) {
727  GetLayouter<CoreTextParagraphLayoutFactory>(line, str, state);
728  if (line.layout == nullptr) {
729  state = old_state;
730  str = old_str;
731  }
732  }
733 #endif
734 
735  if (line.layout == nullptr) {
736  GetLayouter<FallbackParagraphLayoutFactory>(line, str, state);
737  }
738  }
739 
740  /* Move all lines into a local cache so we can reuse them later on more easily. */
741  for (;;) {
742  auto l = line.layout->NextLine(maxw);
743  if (l == nullptr) break;
744  this->push_back(std::move(l));
745  }
746  } while (c != '\0');
747 }
748 
754 {
755  Dimension d = { 0, 0 };
756  for (const auto &l : *this) {
757  d.width = std::max<uint>(d.width, l->GetWidth());
758  d.height += l->GetLeading();
759  }
760  return d;
761 }
762 
769 Point Layouter::GetCharPosition(const char *ch) const
770 {
771  /* Find the code point index which corresponds to the char
772  * pointer into our UTF-8 source string. */
773  size_t index = 0;
774  const char *str = this->string;
775  while (str < ch) {
776  WChar c;
777  size_t len = Utf8Decode(&c, str);
778  if (c == '\0' || c == '\n') break;
779  str += len;
780  index += this->front()->GetInternalCharLength(c);
781  }
782 
783  if (str == ch) {
784  /* Valid character. */
785  const auto &line = this->front();
786 
787  /* Pointer to the end-of-string/line marker? Return total line width. */
788  if (*ch == '\0' || *ch == '\n') {
789  Point p = { line->GetWidth(), 0 };
790  return p;
791  }
792 
793  /* Scan all runs until we've found our code point index. */
794  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
795  const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
796 
797  for (int i = 0; i < run.GetGlyphCount(); i++) {
798  /* Matching glyph? Return position. */
799  if ((size_t)run.GetGlyphToCharMap()[i] == index) {
800  Point p = { (int)run.GetPositions()[i * 2], (int)run.GetPositions()[i * 2 + 1] };
801  return p;
802  }
803  }
804  }
805  }
806 
807  Point p = { 0, 0 };
808  return p;
809 }
810 
816 const char *Layouter::GetCharAtPosition(int x) const
817 {
818  const auto &line = this->front();
819 
820  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
821  const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
822 
823  for (int i = 0; i < run.GetGlyphCount(); i++) {
824  /* Not a valid glyph (empty). */
825  if (run.GetGlyphs()[i] == 0xFFFF) continue;
826 
827  int begin_x = (int)run.GetPositions()[i * 2];
828  int end_x = (int)run.GetPositions()[i * 2 + 2];
829 
830  if (IsInsideMM(x, begin_x, end_x)) {
831  /* Found our glyph, now convert to UTF-8 string index. */
832  size_t index = run.GetGlyphToCharMap()[i];
833 
834  size_t cur_idx = 0;
835  for (const char *str = this->string; *str != '\0'; ) {
836  if (cur_idx == index) return str;
837 
838  WChar c = Utf8Consume(&str);
839  cur_idx += line->GetInternalCharLength(c);
840  }
841  }
842  }
843  }
844 
845  return nullptr;
846 }
847 
852 {
853  FontColourMap::iterator it = fonts[size].Find(colour);
854  if (it != fonts[size].End()) return it->second;
855 
856  Font *f = new Font(size, colour);
857  fonts[size].emplace_back(colour, f);
858  return f;
859 }
860 
866 {
867  for (auto &pair : fonts[size]) {
868  delete pair.second;
869  }
870  fonts[size].clear();
871 
872  /* We must reset the linecache since it references the just freed fonts */
873  ResetLineCache();
874 
875 #if defined(WITH_UNISCRIBE)
876  UniscribeResetScriptCache(size);
877 #endif
878 #if defined(WITH_COCOA)
879  MacOSResetScriptCache(size);
880 #endif
881 }
882 
891 Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
892 {
893  if (linecache == nullptr) {
894  /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
895  linecache = new LineCache();
896  }
897 
898  if (auto match = linecache->find(LineCacheQuery{state, std::string_view{str, len}});
899  match != linecache->end()) {
900  return match->second;
901  }
902 
903  /* Create missing entry */
904  LineCacheKey key;
905  key.state_before = state;
906  key.str.assign(str, len);
907  return (*linecache)[key];
908 }
909 
914 {
915  if (linecache != nullptr) linecache->clear();
916 }
917 
922 {
923  if (linecache != nullptr) {
924  /* TODO LRU cache would be fancy, but not exactly necessary */
925  if (linecache->size() > 4096) ResetLineCache();
926  }
927 }
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
IsInsideMM
static constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
Definition: math_func.hpp:204
Layouter::LineCacheItem
Item in the linecache.
Definition: gfx_layout.h:183
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:36
FontCache::GetSize
FontSize GetSize() const
Get the FontSize of the font.
Definition: fontcache.h:42
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
ICUParagraphLayout::ICUVisualRun::vr
const icu::ParagraphLayout::VisualRun * vr
The actual ICU vr.
Definition: gfx_layout.cpp:130
FontState::PopColour
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:57
ICUParagraphLayoutFactory::SUPPORTS_RTL
static const bool SUPPORTS_RTL
Helper for GetLayouter, to get whether the layouter supports RTL.
Definition: gfx_layout.cpp:187
Utf8CharLen
static int8 Utf8CharLen(WChar c)
Return the length of a UTF-8 encoded character.
Definition: string_func.h:116
Layouter::string
const char * string
Pointer to the original string.
Definition: gfx_layout.h:154
Layouter::linecache
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:196
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
Layouter::GetCharPosition
Point GetCharPosition(const char *ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:769
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:865
FallbackParagraphLayoutFactory
Helper class to construct a new FallbackParagraphLayout.
Definition: gfx_layout.cpp:293
FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun
FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x)
Create the visual run.
Definition: gfx_layout.cpp:333
SmallMap::Insert
bool Insert(const T &key, const U &data)
Adds new item to this map.
Definition: smallmap_type.hpp:127
FallbackParagraphLayout::runs
FontMap & runs
The fonts we have to use for this paragraph.
Definition: gfx_layout.cpp:283
ICUParagraphLayout::ICUVisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.cpp:129
ParagraphLayouter
Interface to glue fallback and normal layouter into one.
Definition: gfx_layout.h:117
SmallMap
Implementation of simple mapping class.
Definition: smallmap_type.hpp:26
Layouter::LineCacheItem::layout
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:189
control_codes.h
FallbackParagraphLayout::buffer_begin
const WChar * buffer_begin
Begin of the buffer.
Definition: gfx_layout.cpp:281
string_osx.h
ParagraphLayouter::Line
A single line worth of VisualRuns.
Definition: gfx_layout.h:134
ICUParagraphLayoutFactory::CharType
UChar CharType
Helper for GetLayouter, to get the right type.
Definition: gfx_layout.cpp:185
FallbackParagraphLayout::FallbackLine::GetWidth
int GetWidth() const override
Get the width of this line.
Definition: gfx_layout.cpp:451
DRAW_STRING_BUFFER
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:86
Layouter::fonts
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition: gfx_layout.h:201
FallbackParagraphLayout::FallbackVisualRun::GetPositions
const float * GetPositions() const override
Get the positions of this run.
Definition: gfx_layout.cpp:410
FallbackParagraphLayout::Reflow
void Reflow() override
Reset the position to the start of the paragraph.
Definition: gfx_layout.cpp:496
GetLayouter
static void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
Helper for getting a ParagraphLayouter of the given type.
Definition: gfx_layout.cpp:610
Layouter::LineCacheItem::runs
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:186
FallbackParagraphLayout::FallbackVisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.cpp:251
FontState::fontsize
FontSize fontsize
Current font size.
Definition: gfx_layout.h:36
Layouter::GetBounds
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:753
Layouter::Layouter
Layouter(const char *str, int maxw=INT32_MAX, TextColour colour=TC_FROMSTRING, FontSize fontsize=FS_NORMAL)
Create a new layouter.
Definition: gfx_layout.cpp:674
FallbackParagraphLayout::FallbackVisualRun::glyphs
GlyphID * glyphs
The glyphs we're drawing.
Definition: gfx_layout.cpp:253
safeguards.h
FallbackParagraphLayout::FallbackVisualRun::positions
float * positions
The positions of the glyphs.
Definition: gfx_layout.cpp:254
Layouter::LineCacheQuery
Definition: gfx_layout.h:162
Layouter::GetFont
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition: gfx_layout.cpp:851
FallbackParagraphLayout::FallbackVisualRun::GetGlyphCount
int GetGlyphCount() const override
Get the number of glyphs in this run.
Definition: gfx_layout.cpp:392
FallbackParagraphLayout::FallbackLine::CountRuns
int CountRuns() const override
Get the number of runs in this line.
Definition: gfx_layout.cpp:468
FallbackParagraphLayoutFactory::SUPPORTS_RTL
static const bool SUPPORTS_RTL
Helper for GetLayouter, to get whether the layouter supports RTL.
Definition: gfx_layout.cpp:298
FallbackParagraphLayout::FallbackVisualRun::~FallbackVisualRun
~FallbackVisualRun() override
Free all data.
Definition: gfx_layout.cpp:372
gfx_layout.h
FallbackParagraphLayout::FallbackVisualRun::GetLeading
int GetLeading() const override
Get the height of this font.
Definition: gfx_layout.cpp:428
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
stdafx.h
FallbackParagraphLayout::FallbackLine::GetVisualRun
const ParagraphLayouter::VisualRun & GetVisualRun(int run) const override
Get a specific visual run.
Definition: gfx_layout.cpp:477
FallbackParagraphLayoutFactory::AppendToBuffer
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
FallbackParagraphLayout
Class handling the splitting of a paragraph of text into lines and visual runs.
Definition: gfx_layout.cpp:248
Utf8Decode
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:593
Layouter::ReduceLineCache
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
Definition: gfx_layout.cpp:921
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:122
FallbackParagraphLayout::FallbackVisualRun::GetGlyphToCharMap
const int * GetGlyphToCharMap() const override
Get the glyph-to-character map for this visual run.
Definition: gfx_layout.cpp:419
FontState::cur_colour
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:37
string_func.h
strings_func.h
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
FallbackParagraphLayout::FallbackVisualRun::GetGlyphs
const GlyphID * GetGlyphs() const override
Get the glyphs of this run.
Definition: gfx_layout.cpp:401
FallbackParagraphLayout::FallbackLine::GetLeading
int GetLeading() const override
Get the height of the line.
Definition: gfx_layout.cpp:437
Layouter::GetCharAtPosition
const char * GetCharAtPosition(int x) const
Get the character that is at a position.
Definition: gfx_layout.cpp:816
FallbackParagraphLayout::FallbackVisualRun::glyph_count
int glyph_count
The number of glyphs.
Definition: gfx_layout.cpp:256
FontState::SetColour
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:48
Layouter::LineCacheItem::state_after
FontState state_after
Font state after the line.
Definition: gfx_layout.h:188
ICUParagraphLayout::p
icu::ParagraphLayout * p
The actual ICU paragraph layout.
Definition: gfx_layout.cpp:126
FallbackParagraphLayoutFactory::GetParagraphLayout
static ParagraphLayouter * GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
Get the actual ParagraphLayout for the given buffer.
Definition: gfx_layout.cpp:307
Layouter::GetCachedParagraphLayout
static LineCacheItem & GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
Get reference to cache item.
Definition: gfx_layout.cpp:891
FallbackParagraphLayout::FallbackLine
A single line worth of VisualRuns.
Definition: gfx_layout.cpp:271
ICUParagraphLayoutFactory
Helper class to construct a new ICUParagraphLayout.
Definition: gfx_layout.cpp:182
GetCharacterWidth
byte GetCharacterWidth(FontSize size, WChar key)
Return width of character glyph.
Definition: gfx.cpp:1450
SmallMap::Find
std::vector< Pair >::const_iterator Find(const T &key) const
Finds given key in this map.
Definition: smallmap_type.hpp:41
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
FontState::SetFontSize
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:76
FallbackParagraphLayoutFactory::CharType
WChar CharType
Helper for GetLayouter, to get the right type.
Definition: gfx_layout.cpp:296
FontState
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:35
ICUParagraphLayout
Wrapper for doing layouts with ICU.
Definition: gfx_layout.cpp:125
Layouter::ResetLineCache
static void ResetLineCache()
Clear line cache.
Definition: gfx_layout.cpp:913
IsWhitespace
static bool IsWhitespace(WChar c)
Check whether UNICODE character is whitespace or not, i.e.
Definition: string_func.h:260
MacOSResetScriptCache
void MacOSResetScriptCache(FontSize size)
Delete CoreText font reference for a specific font size.
Definition: string_osx.cpp:291
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
ICUParagraphLayout::ICULine::l
icu::ParagraphLayout::Line * l
The actual ICU line.
Definition: gfx_layout.cpp:145
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
ICUParagraphLayout::ICULine
A single line worth of VisualRuns.
Definition: gfx_layout.cpp:144
FallbackParagraphLayout::buffer
const WChar * buffer
The current location in the buffer.
Definition: gfx_layout.cpp:282
FontState::PushColour
void PushColour()
Push the current colour on to the stack.
Definition: gfx_layout.h:67
FallbackParagraphLayout::FallbackVisualRun::glyph_to_char
int * glyph_to_char
The char index of the glyphs.
Definition: gfx_layout.cpp:255
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
Layouter::LineCacheItem::buffer
void * buffer
Accessed by both ICU's and our ParagraphLayout::nextLine.
Definition: gfx_layout.h:185
FallbackParagraphLayout::FallbackVisualRun::font
Font * font
The font used to layout these.
Definition: gfx_layout.cpp:252
IsTextDirectionChar
static bool IsTextDirectionChar(WChar c)
Is the given character a text direction character.
Definition: string_func.h:228
debug.h
FallbackParagraphLayout::NextLine
std::unique_ptr< const Line > NextLine(int max_width) override
Construct a new line with a maximum width.
Definition: gfx_layout.cpp:506
string_uniscribe.h
FallbackParagraphLayout::FallbackParagraphLayout
FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs)
Create a new paragraph layouter.
Definition: gfx_layout.cpp:488
FallbackParagraphLayout::FallbackVisualRun::GetFont
const Font * GetFont() const override
Get the font associated with this run.
Definition: gfx_layout.cpp:383
SmallMap::Contains
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
Definition: smallmap_type.hpp:79