OpenTTD Source  13.0-RC1
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 "zoom_func.h"
15 #include "debug.h"
16 
17 #include "table/control_codes.h"
18 
19 #ifdef WITH_ICU_LX
20 #include <unicode/ustring.h>
21 #endif /* WITH_ICU_LX */
22 
23 #ifdef WITH_UNISCRIBE
25 #endif /* WITH_UNISCRIBE */
26 
27 #ifdef WITH_COCOA
28 #include "os/macosx/string_osx.h"
29 #endif
30 
31 #include "safeguards.h"
32 
33 
35 Layouter::LineCache *Layouter::linecache;
36 
39 
40 
46 Font::Font(FontSize size, TextColour colour) :
47  fc(FontCache::Get(size)), colour(colour)
48 {
49  assert(size < FS_END);
50 }
51 
52 #ifdef WITH_ICU_LX
53 /* Implementation details of LEFontInstance */
54 
55 le_int32 Font::getUnitsPerEM() const
56 {
57  return this->fc->GetUnitsPerEM();
58 }
59 
60 le_int32 Font::getAscent() const
61 {
62  return this->fc->GetAscender();
63 }
64 
65 le_int32 Font::getDescent() const
66 {
67  return -this->fc->GetDescender();
68 }
69 
70 le_int32 Font::getLeading() const
71 {
72  return this->fc->GetHeight();
73 }
74 
75 float Font::getXPixelsPerEm() const
76 {
77  return (float)this->fc->GetHeight();
78 }
79 
80 float Font::getYPixelsPerEm() const
81 {
82  return (float)this->fc->GetHeight();
83 }
84 
85 float Font::getScaleFactorX() const
86 {
87  return 1.0f;
88 }
89 
90 float Font::getScaleFactorY() const
91 {
92  return 1.0f;
93 }
94 
95 const void *Font::getFontTable(LETag tableTag) const
96 {
97  size_t length;
98  return this->getFontTable(tableTag, length);
99 }
100 
101 const void *Font::getFontTable(LETag tableTag, size_t &length) const
102 {
103  return this->fc->GetFontTable(tableTag, length);
104 }
105 
106 LEGlyphID Font::mapCharToGlyph(LEUnicode32 ch) const
107 {
108  if (IsTextDirectionChar(ch)) return 0;
109  return this->fc->MapCharToGlyph(ch);
110 }
111 
112 void Font::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
113 {
114  advance.fX = glyph == 0xFFFF ? 0 : this->fc->GetGlyphWidth(glyph);
115  advance.fY = 0;
116 }
117 
118 le_bool Font::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
119 {
120  return false;
121 }
122 
127  icu::ParagraphLayout *p;
128 public:
131  const icu::ParagraphLayout::VisualRun *vr;
132 
133  public:
134  ICUVisualRun(const icu::ParagraphLayout::VisualRun *vr) : vr(vr) { }
135 
136  const Font *GetFont() const override { return (const Font*)vr->getFont(); }
137  int GetGlyphCount() const override { return vr->getGlyphCount(); }
138  const GlyphID *GetGlyphs() const override { return vr->getGlyphs(); }
139  const float *GetPositions() const override { return vr->getPositions(); }
140  int GetLeading() const override { return vr->getLeading(); }
141  const int *GetGlyphToCharMap() const override { return vr->getGlyphToCharMap(); }
142  };
143 
145  class ICULine : public std::vector<ICUVisualRun>, public ParagraphLayouter::Line {
146  icu::ParagraphLayout::Line *l;
147 
148  public:
149  ICULine(icu::ParagraphLayout::Line *l) : l(l)
150  {
151  for (int i = 0; i < l->countRuns(); i++) {
152  this->emplace_back(l->getVisualRun(i));
153  }
154  }
155  ~ICULine() override { delete l; }
156 
157  int GetLeading() const override { return l->getLeading(); }
158  int GetWidth() const override { return l->getWidth(); }
159  int CountRuns() const override { return l->countRuns(); }
160  const ParagraphLayouter::VisualRun &GetVisualRun(int run) const override { return this->at(run); }
161 
162  int GetInternalCharLength(WChar c) const override
163  {
164  /* ICU uses UTF-16 internally which means we need to account for surrogate pairs. */
165  return Utf8CharLen(c) < 4 ? 1 : 2;
166  }
167  };
168 
169  ICUParagraphLayout(icu::ParagraphLayout *p) : p(p) { }
170  ~ICUParagraphLayout() override { delete p; }
171  void Reflow() override { p->reflow(); }
172 
173  std::unique_ptr<const Line> NextLine(int max_width) override
174  {
175  icu::ParagraphLayout::Line *l = p->nextLine(max_width);
176  return std::unique_ptr<const Line>(l == nullptr ? nullptr : new ICULine(l));
177  }
178 };
179 
184 public:
186  typedef UChar CharType;
188  static const bool SUPPORTS_RTL = true;
189 
190  static ParagraphLayouter *GetParagraphLayout(UChar *buff, UChar *buff_end, FontMap &fontMapping)
191  {
192  int32 length = buff_end - buff;
193 
194  if (length == 0) {
195  /* ICU's ParagraphLayout cannot handle empty strings, so fake one. */
196  buff[0] = ' ';
197  length = 1;
198  fontMapping.back().first++;
199  }
200 
201  /* Fill ICU's FontRuns with the right data. */
202  icu::FontRuns runs(fontMapping.size());
203  for (auto &pair : fontMapping) {
204  runs.add(pair.second, pair.first);
205  }
206 
207  LEErrorCode status = LE_NO_ERROR;
208  /* ParagraphLayout does not copy "buff", so it must stay valid.
209  * "runs" is copied according to the ICU source, but the documentation does not specify anything, so this might break somewhen. */
210  icu::ParagraphLayout *p = new icu::ParagraphLayout(buff, length, &runs, nullptr, nullptr, nullptr, _current_text_dir == TD_RTL ? 1 : 0, false, status);
211  if (status != LE_NO_ERROR) {
212  delete p;
213  return nullptr;
214  }
215 
216  return new ICUParagraphLayout(p);
217  }
218 
219  static size_t AppendToBuffer(UChar *buff, const UChar *buffer_last, WChar c)
220  {
221  /* Transform from UTF-32 to internal ICU format of UTF-16. */
222  int32 length = 0;
223  UErrorCode err = U_ZERO_ERROR;
224  u_strFromUTF32(buff, buffer_last - buff, &length, (UChar32*)&c, 1, &err);
225  return length;
226  }
227 };
228 #endif /* WITH_ICU_LX */
229 
230 /*** Paragraph layout ***/
250 public:
253  Font *font;
255  float *positions;
258 
259  public:
260  FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x);
261  FallbackVisualRun(FallbackVisualRun &&other) noexcept;
262  ~FallbackVisualRun() override;
263  const Font *GetFont() const override;
264  int GetGlyphCount() const override;
265  const GlyphID *GetGlyphs() const override;
266  const float *GetPositions() const override;
267  int GetLeading() const override;
268  const int *GetGlyphToCharMap() const override;
269  };
270 
272  class FallbackLine : public std::vector<FallbackVisualRun>, public ParagraphLayouter::Line {
273  public:
274  int GetLeading() const override;
275  int GetWidth() const override;
276  int CountRuns() const override;
277  const ParagraphLayouter::VisualRun &GetVisualRun(int run) const override;
278 
279  int GetInternalCharLength(WChar c) const override { return 1; }
280  };
281 
283  const WChar *buffer;
285 
287  void Reflow() override;
288  std::unique_ptr<const Line> NextLine(int max_width) override;
289 };
290 
295 public:
297  typedef WChar CharType;
299  static const bool SUPPORTS_RTL = false;
300 
308  static ParagraphLayouter *GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
309  {
310  return new FallbackParagraphLayout(buff, buff_end - buff, fontMapping);
311  }
312 
320  static size_t AppendToBuffer(WChar *buff, const WChar *buffer_last, WChar c)
321  {
322  *buff = c;
323  return 1;
324  }
325 };
326 
334 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(Font *font, const WChar *chars, int char_count, int x) :
335  font(font), glyph_count(char_count)
336 {
337  const bool isbuiltin = font->fc->IsBuiltInFont();
338 
339  this->glyphs = MallocT<GlyphID>(this->glyph_count);
340  this->glyph_to_char = MallocT<int>(this->glyph_count);
341 
342  /* Positions contains the location of the begin of each of the glyphs, and the end of the last one. */
343  this->positions = MallocT<float>(this->glyph_count * 2 + 2);
344  this->positions[0] = x;
345 
346  for (int i = 0; i < this->glyph_count; i++) {
347  this->glyphs[i] = font->fc->MapCharToGlyph(chars[i]);
348  if (isbuiltin) {
349  this->positions[2 * i + 1] = font->fc->GetAscender(); // Apply sprite font's ascender.
350  } else if (chars[i] >= SCC_SPRITE_START && chars[i] <= SCC_SPRITE_END) {
351  this->positions[2 * i + 1] = (font->fc->GetHeight() - ScaleSpriteTrad(FontCache::GetDefaultFontHeight(font->fc->GetSize()))) / 2; // Align sprite font to centre
352  } else {
353  this->positions[2 * i + 1] = 0; // No ascender adjustment.
354  }
355  this->positions[2 * i + 2] = this->positions[2 * i] + font->fc->GetGlyphWidth(this->glyphs[i]);
356  this->glyph_to_char[i] = i;
357  }
358 }
359 
361 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(FallbackVisualRun &&other) noexcept : font(other.font), glyph_count(other.glyph_count)
362 {
363  this->positions = other.positions;
364  this->glyph_to_char = other.glyph_to_char;
365  this->glyphs = other.glyphs;
366 
367  other.positions = nullptr;
368  other.glyph_to_char = nullptr;
369  other.glyphs = nullptr;
370 }
371 
374 {
375  free(this->positions);
376  free(this->glyph_to_char);
377  free(this->glyphs);
378 }
379 
385 {
386  return this->font;
387 }
388 
394 {
395  return this->glyph_count;
396 }
397 
403 {
404  return this->glyphs;
405 }
406 
412 {
413  return this->positions;
414 }
415 
421 {
422  return this->glyph_to_char;
423 }
424 
430 {
431  return this->GetFont()->fc->GetHeight();
432 }
433 
439 {
440  int leading = 0;
441  for (const auto &run : *this) {
442  leading = std::max(leading, run.GetLeading());
443  }
444 
445  return leading;
446 }
447 
453 {
454  if (this->size() == 0) return 0;
455 
456  /*
457  * The last X position of a run contains is the end of that run.
458  * Since there is no left-to-right support, taking this value of
459  * the last run gives us the end of the line and thus the width.
460  */
461  const auto &run = this->GetVisualRun(this->CountRuns() - 1);
462  return (int)run.GetPositions()[run.GetGlyphCount() * 2];
463 }
464 
470 {
471  return (uint)this->size();
472 }
473 
479 {
480  return this->at(run);
481 }
482 
490 {
491  assert(runs.End()[-1].first == length);
492 }
493 
498 {
499  this->buffer = this->buffer_begin;
500 }
501 
507 std::unique_ptr<const ParagraphLayouter::Line> FallbackParagraphLayout::NextLine(int max_width)
508 {
509  /* Simple idea:
510  * - split a line at a newline character, or at a space where we can break a line.
511  * - split for a visual run whenever a new line happens, or the font changes.
512  */
513  if (this->buffer == nullptr) return nullptr;
514 
515  std::unique_ptr<FallbackLine> l(new FallbackLine());
516 
517  if (*this->buffer == '\0') {
518  /* Only a newline. */
519  this->buffer = nullptr;
520  l->emplace_back(this->runs.front().second, this->buffer, 0, 0);
521  return l;
522  }
523 
524  int offset = this->buffer - this->buffer_begin;
525  FontMap::iterator iter = this->runs.data();
526  while (iter->first <= offset) {
527  iter++;
528  assert(iter != this->runs.End());
529  }
530 
531  const FontCache *fc = iter->second->fc;
532  const WChar *next_run = this->buffer_begin + iter->first;
533 
534  const WChar *begin = this->buffer;
535  const WChar *last_space = nullptr;
536  const WChar *last_char;
537  int width = 0;
538  for (;;) {
539  WChar c = *this->buffer;
540  last_char = this->buffer;
541 
542  if (c == '\0') {
543  this->buffer = nullptr;
544  break;
545  }
546 
547  if (this->buffer == next_run) {
548  int w = l->GetWidth();
549  l->emplace_back(iter->second, begin, this->buffer - begin, w);
550  iter++;
551  assert(iter != this->runs.End());
552 
553  next_run = this->buffer_begin + iter->first;
554  begin = this->buffer;
555 
556  last_space = nullptr;
557  }
558 
559  if (IsWhitespace(c)) last_space = this->buffer;
560 
561  if (IsPrintable(c) && !IsTextDirectionChar(c)) {
562  int char_width = GetCharacterWidth(fc->GetSize(), c);
563  width += char_width;
564  if (width > max_width) {
565  /* The string is longer than maximum width so we need to decide
566  * what to do with it. */
567  if (width == char_width) {
568  /* The character is wider than allowed width; don't know
569  * what to do with this case... bail out! */
570  this->buffer = nullptr;
571  return l;
572  }
573 
574  if (last_space == nullptr) {
575  /* No space has been found. Just terminate at our current
576  * location. This usually happens for languages that do not
577  * require spaces in strings, like Chinese, Japanese and
578  * Korean. For other languages terminating mid-word might
579  * not be the best, but terminating the whole string instead
580  * of continuing the word at the next line is worse. */
581  last_char = this->buffer;
582  } else {
583  /* A space is found; perfect place to terminate */
584  this->buffer = last_space + 1;
585  last_char = last_space;
586  }
587  break;
588  }
589  }
590 
591  this->buffer++;
592  }
593 
594  if (l->size() == 0 || last_char - begin != 0) {
595  int w = l->GetWidth();
596  l->emplace_back(iter->second, begin, last_char - begin, w);
597  }
598  return l;
599 }
600 
610 template <typename T>
611 static inline void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
612 {
613  if (line.buffer != nullptr) free(line.buffer);
614 
615  typename T::CharType *buff_begin = MallocT<typename T::CharType>(DRAW_STRING_BUFFER);
616  const typename T::CharType *buffer_last = buff_begin + DRAW_STRING_BUFFER;
617  typename T::CharType *buff = buff_begin;
618  FontMap &fontMapping = line.runs;
619  Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
620 
621  line.buffer = buff_begin;
622  fontMapping.clear();
623 
624  /*
625  * Go through the whole string while adding Font instances to the font map
626  * whenever the font changes, and convert the wide characters into a format
627  * usable by ParagraphLayout.
628  */
629  for (; buff < buffer_last;) {
630  WChar c = Utf8Consume(const_cast<const char **>(&str));
631  if (c == '\0' || c == '\n') {
632  break;
633  } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
634  state.SetColour((TextColour)(c - SCC_BLUE));
635  } else if (c == SCC_PUSH_COLOUR) {
636  state.PushColour();
637  } else if (c == SCC_POP_COLOUR) {
638  state.PopColour();
639  } else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
640  state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
641  } else {
642  /* Filter out non printable characters */
643  if (!IsPrintable(c)) continue;
644  /* Filter out text direction characters that shouldn't be drawn, and
645  * will not be handled in the fallback non ICU case because they are
646  * mostly needed for RTL languages which need more ICU support. */
647  if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
648  buff += T::AppendToBuffer(buff, buffer_last, c);
649  continue;
650  }
651 
652  if (!fontMapping.Contains(buff - buff_begin)) {
653  fontMapping.Insert(buff - buff_begin, f);
654  }
655  f = Layouter::GetFont(state.fontsize, state.cur_colour);
656  }
657 
658  /* Better safe than sorry. */
659  *buff = '\0';
660 
661  if (!fontMapping.Contains(buff - buff_begin)) {
662  fontMapping.Insert(buff - buff_begin, f);
663  }
664  line.layout = T::GetParagraphLayout(buff_begin, buff, fontMapping);
665  line.state_after = state;
666 }
667 
675 Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsize) : string(str)
676 {
677  FontState state(colour, fontsize);
678  WChar c = 0;
679 
680  do {
681  /* Scan string for end of line */
682  const char *lineend = str;
683  for (;;) {
684  size_t len = Utf8Decode(&c, lineend);
685  if (c == '\0' || c == '\n') break;
686  lineend += len;
687  }
688 
689  LineCacheItem& line = GetCachedParagraphLayout(str, lineend - str, state);
690  if (line.layout != nullptr) {
691  /* Line is in cache */
692  str = lineend + 1;
693  state = line.state_after;
694  line.layout->Reflow();
695  } else {
696  /* Line is new, layout it */
697  FontState old_state = state;
698 #if defined(WITH_ICU_LX) || defined(WITH_UNISCRIBE) || defined(WITH_COCOA)
699  const char *old_str = str;
700 #endif
701 
702 #ifdef WITH_ICU_LX
703  GetLayouter<ICUParagraphLayoutFactory>(line, str, state);
704  if (line.layout == nullptr) {
705  static bool warned = false;
706  if (!warned) {
707  Debug(misc, 0, "ICU layouter bailed on the font. Falling back to the fallback layouter");
708  warned = true;
709  }
710 
711  state = old_state;
712  str = old_str;
713  }
714 #endif
715 
716 #ifdef WITH_UNISCRIBE
717  if (line.layout == nullptr) {
718  GetLayouter<UniscribeParagraphLayoutFactory>(line, str, state);
719  if (line.layout == nullptr) {
720  state = old_state;
721  str = old_str;
722  }
723  }
724 #endif
725 
726 #ifdef WITH_COCOA
727  if (line.layout == nullptr) {
728  GetLayouter<CoreTextParagraphLayoutFactory>(line, str, state);
729  if (line.layout == nullptr) {
730  state = old_state;
731  str = old_str;
732  }
733  }
734 #endif
735 
736  if (line.layout == nullptr) {
737  GetLayouter<FallbackParagraphLayoutFactory>(line, str, state);
738  }
739  }
740 
741  /* Move all lines into a local cache so we can reuse them later on more easily. */
742  for (;;) {
743  auto l = line.layout->NextLine(maxw);
744  if (l == nullptr) break;
745  this->push_back(std::move(l));
746  }
747  } while (c != '\0');
748 }
749 
755 {
756  Dimension d = { 0, 0 };
757  for (const auto &l : *this) {
758  d.width = std::max<uint>(d.width, l->GetWidth());
759  d.height += l->GetLeading();
760  }
761  return d;
762 }
763 
770 Point Layouter::GetCharPosition(const char *ch) const
771 {
772  /* Find the code point index which corresponds to the char
773  * pointer into our UTF-8 source string. */
774  size_t index = 0;
775  const char *str = this->string;
776  while (str < ch) {
777  WChar c;
778  size_t len = Utf8Decode(&c, str);
779  if (c == '\0' || c == '\n') break;
780  str += len;
781  index += this->front()->GetInternalCharLength(c);
782  }
783 
784  if (str == ch) {
785  /* Valid character. */
786  const auto &line = this->front();
787 
788  /* Pointer to the end-of-string/line marker? Return total line width. */
789  if (*ch == '\0' || *ch == '\n') {
790  Point p = { line->GetWidth(), 0 };
791  return p;
792  }
793 
794  /* Scan all runs until we've found our code point index. */
795  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
796  const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
797 
798  for (int i = 0; i < run.GetGlyphCount(); i++) {
799  /* Matching glyph? Return position. */
800  if ((size_t)run.GetGlyphToCharMap()[i] == index) {
801  Point p = { (int)run.GetPositions()[i * 2], (int)run.GetPositions()[i * 2 + 1] };
802  return p;
803  }
804  }
805  }
806  }
807 
808  Point p = { 0, 0 };
809  return p;
810 }
811 
817 const char *Layouter::GetCharAtPosition(int x) const
818 {
819  const auto &line = this->front();
820 
821  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
822  const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
823 
824  for (int i = 0; i < run.GetGlyphCount(); i++) {
825  /* Not a valid glyph (empty). */
826  if (run.GetGlyphs()[i] == 0xFFFF) continue;
827 
828  int begin_x = (int)run.GetPositions()[i * 2];
829  int end_x = (int)run.GetPositions()[i * 2 + 2];
830 
831  if (IsInsideMM(x, begin_x, end_x)) {
832  /* Found our glyph, now convert to UTF-8 string index. */
833  size_t index = run.GetGlyphToCharMap()[i];
834 
835  size_t cur_idx = 0;
836  for (const char *str = this->string; *str != '\0'; ) {
837  if (cur_idx == index) return str;
838 
839  WChar c = Utf8Consume(&str);
840  cur_idx += line->GetInternalCharLength(c);
841  }
842  }
843  }
844  }
845 
846  return nullptr;
847 }
848 
853 {
854  FontColourMap::iterator it = fonts[size].Find(colour);
855  if (it != fonts[size].End()) return it->second;
856 
857  Font *f = new Font(size, colour);
858  fonts[size].emplace_back(colour, f);
859  return f;
860 }
861 
867 {
868  for (auto &pair : fonts[size]) {
869  delete pair.second;
870  }
871  fonts[size].clear();
872 
873  /* We must reset the linecache since it references the just freed fonts */
874  ResetLineCache();
875 
876 #if defined(WITH_UNISCRIBE)
877  UniscribeResetScriptCache(size);
878 #endif
879 #if defined(WITH_COCOA)
880  MacOSResetScriptCache(size);
881 #endif
882 }
883 
892 Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
893 {
894  if (linecache == nullptr) {
895  /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
896  linecache = new LineCache();
897  }
898 
899  if (auto match = linecache->find(LineCacheQuery{state, std::string_view{str, len}});
900  match != linecache->end()) {
901  return match->second;
902  }
903 
904  /* Create missing entry */
905  LineCacheKey key;
906  key.state_before = state;
907  key.str.assign(str, len);
908  return (*linecache)[key];
909 }
910 
915 {
916  if (linecache != nullptr) linecache->clear();
917 }
918 
923 {
924  if (linecache != nullptr) {
925  /* TODO LRU cache would be fancy, but not exactly necessary */
926  if (linecache->size() > 4096) ResetLineCache();
927  }
928 }
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:131
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:188
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:257
zoom_func.h
Layouter::GetCharPosition
Point GetCharPosition(const char *ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:770
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:866
FallbackParagraphLayoutFactory
Helper class to construct a new FallbackParagraphLayout.
Definition: gfx_layout.cpp:294
FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun
FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x)
Create the visual run.
Definition: gfx_layout.cpp:334
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:284
ICUParagraphLayout::ICUVisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.cpp:130
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:282
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:186
FallbackParagraphLayout::FallbackLine::GetWidth
int GetWidth() const override
Get the width of this line.
Definition: gfx_layout.cpp:452
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:411
FallbackParagraphLayout::Reflow
void Reflow() override
Reset the position to the start of the paragraph.
Definition: gfx_layout.cpp:497
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:611
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:252
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:754
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:675
ScaleSpriteTrad
static int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition: zoom_func.h:107
FallbackParagraphLayout::FallbackVisualRun::glyphs
GlyphID * glyphs
The glyphs we're drawing.
Definition: gfx_layout.cpp:254
safeguards.h
FallbackParagraphLayout::FallbackVisualRun::positions
float * positions
The positions of the glyphs.
Definition: gfx_layout.cpp:255
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:852
FallbackParagraphLayout::FallbackVisualRun::GetGlyphCount
int GetGlyphCount() const override
Get the number of glyphs in this run.
Definition: gfx_layout.cpp:393
FallbackParagraphLayout::FallbackLine::CountRuns
int CountRuns() const override
Get the number of runs in this line.
Definition: gfx_layout.cpp:469
FallbackParagraphLayoutFactory::SUPPORTS_RTL
static const bool SUPPORTS_RTL
Helper for GetLayouter, to get whether the layouter supports RTL.
Definition: gfx_layout.cpp:299
FallbackParagraphLayout::FallbackVisualRun::~FallbackVisualRun
~FallbackVisualRun() override
Free all data.
Definition: gfx_layout.cpp:373
gfx_layout.h
FallbackParagraphLayout::FallbackVisualRun::GetLeading
int GetLeading() const override
Get the height of this font.
Definition: gfx_layout.cpp:429
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:478
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:320
FallbackParagraphLayout
Class handling the splitting of a paragraph of text into lines and visual runs.
Definition: gfx_layout.cpp:249
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:922
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:420
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:402
FallbackParagraphLayout::FallbackLine::GetLeading
int GetLeading() const override
Get the height of the line.
Definition: gfx_layout.cpp:438
Layouter::GetCharAtPosition
const char * GetCharAtPosition(int x) const
Get the character that is at a position.
Definition: gfx_layout.cpp:817
FallbackParagraphLayout::FallbackVisualRun::glyph_count
int glyph_count
The number of glyphs.
Definition: gfx_layout.cpp:257
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:127
FallbackParagraphLayoutFactory::GetParagraphLayout
static ParagraphLayouter * GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
Get the actual ParagraphLayout for the given buffer.
Definition: gfx_layout.cpp:308
Layouter::GetCachedParagraphLayout
static LineCacheItem & GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
Get reference to cache item.
Definition: gfx_layout.cpp:892
FallbackParagraphLayout::FallbackLine
A single line worth of VisualRuns.
Definition: gfx_layout.cpp:272
ICUParagraphLayoutFactory
Helper class to construct a new ICUParagraphLayout.
Definition: gfx_layout.cpp:183
GetCharacterWidth
byte GetCharacterWidth(FontSize size, WChar key)
Return width of character glyph.
Definition: gfx.cpp:1465
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:297
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:126
Layouter::ResetLineCache
static void ResetLineCache()
Clear line cache.
Definition: gfx_layout.cpp:914
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:292
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:146
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:145
FallbackParagraphLayout::buffer
const WChar * buffer
The current location in the buffer.
Definition: gfx_layout.cpp:283
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:256
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:253
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:507
string_uniscribe.h
FallbackParagraphLayout::FallbackParagraphLayout
FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs)
Create a new paragraph layouter.
Definition: gfx_layout.cpp:489
FallbackParagraphLayout::FallbackVisualRun::GetFont
const Font * GetFont() const override
Get the font associated with this run.
Definition: gfx_layout.cpp:384
SmallMap::Contains
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
Definition: smallmap_type.hpp:79