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