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_LAYOUT
21 #include <unicode/ustring.h>
22 #endif /* WITH_ICU_LAYOUT */
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_LAYOUT
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 
127 class ICUParagraphLayout : public AutoDeleteSmallVector<ParagraphLayouter::Line *, 4>, public ParagraphLayouter {
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 { return (const Font*)vr->getFont(); }
138  int GetGlyphCount() const { return vr->getGlyphCount(); }
139  const GlyphID *GetGlyphs() const { return vr->getGlyphs(); }
140  const float *GetPositions() const { return vr->getPositions(); }
141  int GetLeading() const { return vr->getLeading(); }
142  const int *GetGlyphToCharMap() const { return vr->getGlyphToCharMap(); }
143  };
144 
146  class ICULine : public AutoDeleteSmallVector<ICUVisualRun *, 4>, 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->Append() = new ICUVisualRun(l->getVisualRun(i));
154  }
155  }
156  ~ICULine() { delete l; }
157 
158  int GetLeading() const { return l->getLeading(); }
159  int GetWidth() const { return l->getWidth(); }
160  int CountRuns() const { return l->countRuns(); }
161  const ParagraphLayouter::VisualRun *GetVisualRun(int run) const { return *this->Get(run); }
162 
163  int GetInternalCharLength(WChar c) const
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() { delete p; }
172  void Reflow() { p->reflow(); }
173 
174  ParagraphLayouter::Line *NextLine(int max_width)
175  {
176  icu::ParagraphLayout::Line *l = p->nextLine(max_width);
177  return l == NULL ? NULL : 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.End()[-1].first++;
200  }
201 
202  /* Fill ICU's FontRuns with the right data. */
203  icu::FontRuns runs(fontMapping.Length());
204  for (FontMap::iterator iter = fontMapping.Begin(); iter != fontMapping.End(); iter++) {
205  runs.add(iter->second, iter->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, NULL, NULL, NULL, _current_text_dir == TD_RTL ? 1 : 0, false, status);
212  if (status != LE_NO_ERROR) {
213  delete p;
214  return NULL;
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_LAYOUT */
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);
263  const Font *GetFont() const;
264  int GetGlyphCount() const;
265  const GlyphID *GetGlyphs() const;
266  const float *GetPositions() const;
267  int GetLeading() const;
268  const int *GetGlyphToCharMap() const;
269  };
270 
272  class FallbackLine : public AutoDeleteSmallVector<FallbackVisualRun *, 4>, public ParagraphLayouter::Line {
273  public:
274  int GetLeading() const;
275  int GetWidth() const;
276  int CountRuns() const;
277  const ParagraphLayouter::VisualRun *GetVisualRun(int run) const;
278 
279  int GetInternalCharLength(WChar c) const { return 1; }
280  };
281 
283  const WChar *buffer;
285 
286  FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs);
287  void Reflow();
288  const ParagraphLayouter::Line *NextLine(int max_width);
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  this->glyphs = MallocT<GlyphID>(this->glyph_count);
338  this->glyph_to_char = MallocT<int>(this->glyph_count);
339 
340  /* Positions contains the location of the begin of each of the glyphs, and the end of the last one. */
341  this->positions = MallocT<float>(this->glyph_count * 2 + 2);
342  this->positions[0] = x;
343  this->positions[1] = 0;
344 
345  for (int i = 0; i < this->glyph_count; i++) {
346  this->glyphs[i] = font->fc->MapCharToGlyph(chars[i]);
347  this->positions[2 * i + 2] = this->positions[2 * i] + font->fc->GetGlyphWidth(this->glyphs[i]);
348  this->positions[2 * i + 3] = 0;
349  this->glyph_to_char[i] = i;
350  }
351 }
352 
355 {
356  free(this->positions);
357  free(this->glyph_to_char);
358  free(this->glyphs);
359 }
360 
366 {
367  return this->font;
368 }
369 
375 {
376  return this->glyph_count;
377 }
378 
384 {
385  return this->glyphs;
386 }
387 
393 {
394  return this->positions;
395 }
396 
402 {
403  return this->glyph_to_char;
404 }
405 
411 {
412  return this->GetFont()->fc->GetHeight();
413 }
414 
420 {
421  int leading = 0;
422  for (const FallbackVisualRun * const *run = this->Begin(); run != this->End(); run++) {
423  leading = max(leading, (*run)->GetLeading());
424  }
425 
426  return leading;
427 }
428 
434 {
435  if (this->Length() == 0) return 0;
436 
437  /*
438  * The last X position of a run contains is the end of that run.
439  * Since there is no left-to-right support, taking this value of
440  * the last run gives us the end of the line and thus the width.
441  */
442  const ParagraphLayouter::VisualRun *run = this->GetVisualRun(this->CountRuns() - 1);
443  return (int)run->GetPositions()[run->GetGlyphCount() * 2];
444 }
445 
451 {
452  return this->Length();
453 }
454 
460 {
461  return *this->Get(run);
462 }
463 
470 FallbackParagraphLayout::FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs) : buffer_begin(buffer), buffer(buffer), runs(runs)
471 {
472  assert(runs.End()[-1].first == length);
473 }
474 
479 {
480  this->buffer = this->buffer_begin;
481 }
482 
489 {
490  /* Simple idea:
491  * - split a line at a newline character, or at a space where we can break a line.
492  * - split for a visual run whenever a new line happens, or the font changes.
493  */
494  if (this->buffer == NULL) return NULL;
495 
496  FallbackLine *l = new FallbackLine();
497 
498  if (*this->buffer == '\0') {
499  /* Only a newline. */
500  this->buffer = NULL;
501  *l->Append() = new FallbackVisualRun(this->runs.Begin()->second, this->buffer, 0, 0);
502  return l;
503  }
504 
505  int offset = this->buffer - this->buffer_begin;
506  FontMap::iterator iter = this->runs.Begin();
507  while (iter->first <= offset) {
508  iter++;
509  assert(iter != this->runs.End());
510  }
511 
512  const FontCache *fc = iter->second->fc;
513  const WChar *next_run = this->buffer_begin + iter->first;
514 
515  const WChar *begin = this->buffer;
516  const WChar *last_space = NULL;
517  const WChar *last_char;
518  int width = 0;
519  for (;;) {
520  WChar c = *this->buffer;
521  last_char = this->buffer;
522 
523  if (c == '\0') {
524  this->buffer = NULL;
525  break;
526  }
527 
528  if (this->buffer == next_run) {
529  int w = l->GetWidth();
530  *l->Append() = new FallbackVisualRun(iter->second, begin, this->buffer - begin, w);
531  iter++;
532  assert(iter != this->runs.End());
533 
534  next_run = this->buffer_begin + iter->first;
535  begin = this->buffer;
536 
537  last_space = NULL;
538  }
539 
540  if (IsWhitespace(c)) last_space = this->buffer;
541 
542  if (IsPrintable(c) && !IsTextDirectionChar(c)) {
543  int char_width = GetCharacterWidth(fc->GetSize(), c);
544  width += char_width;
545  if (width > max_width) {
546  /* The string is longer than maximum width so we need to decide
547  * what to do with it. */
548  if (width == char_width) {
549  /* The character is wider than allowed width; don't know
550  * what to do with this case... bail out! */
551  this->buffer = NULL;
552  return l;
553  }
554 
555  if (last_space == NULL) {
556  /* No space has been found. Just terminate at our current
557  * location. This usually happens for languages that do not
558  * require spaces in strings, like Chinese, Japanese and
559  * Korean. For other languages terminating mid-word might
560  * not be the best, but terminating the whole string instead
561  * of continuing the word at the next line is worse. */
562  last_char = this->buffer;
563  } else {
564  /* A space is found; perfect place to terminate */
565  this->buffer = last_space + 1;
566  last_char = last_space;
567  }
568  break;
569  }
570  }
571 
572  this->buffer++;
573  }
574 
575  if (l->Length() == 0 || last_char - begin != 0) {
576  int w = l->GetWidth();
577  *l->Append() = new FallbackVisualRun(iter->second, begin, last_char - begin, w);
578  }
579  return l;
580 }
581 
591 template <typename T>
592 static inline void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
593 {
594  if (line.buffer != NULL) free(line.buffer);
595 
596  typename T::CharType *buff_begin = MallocT<typename T::CharType>(DRAW_STRING_BUFFER);
597  const typename T::CharType *buffer_last = buff_begin + DRAW_STRING_BUFFER;
598  typename T::CharType *buff = buff_begin;
599  FontMap &fontMapping = line.runs;
600  Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
601 
602  line.buffer = buff_begin;
603  fontMapping.Clear();
604 
605  /*
606  * Go through the whole string while adding Font instances to the font map
607  * whenever the font changes, and convert the wide characters into a format
608  * usable by ParagraphLayout.
609  */
610  for (; buff < buffer_last;) {
611  WChar c = Utf8Consume(const_cast<const char **>(&str));
612  if (c == '\0' || c == '\n') {
613  break;
614  } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
615  state.SetColour((TextColour)(c - SCC_BLUE));
616  } else if (c == SCC_PUSH_COLOUR) {
617  state.PushColour();
618  } else if (c == SCC_POP_COLOUR) {
619  state.PopColour();
620  } else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
621  state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
622  } else {
623  /* Filter out text direction characters that shouldn't be drawn, and
624  * will not be handled in the fallback non ICU case because they are
625  * mostly needed for RTL languages which need more ICU support. */
626  if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
627  buff += T::AppendToBuffer(buff, buffer_last, c);
628  continue;
629  }
630 
631  if (!fontMapping.Contains(buff - buff_begin)) {
632  fontMapping.Insert(buff - buff_begin, f);
633  }
634  f = Layouter::GetFont(state.fontsize, state.cur_colour);
635  }
636 
637  /* Better safe than sorry. */
638  *buff = '\0';
639 
640  if (!fontMapping.Contains(buff - buff_begin)) {
641  fontMapping.Insert(buff - buff_begin, f);
642  }
643  line.layout = T::GetParagraphLayout(buff_begin, buff, fontMapping);
644  line.state_after = state;
645 }
646 
654 Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsize) : string(str)
655 {
656  FontState state(colour, fontsize);
657  WChar c = 0;
658 
659  do {
660  /* Scan string for end of line */
661  const char *lineend = str;
662  for (;;) {
663  size_t len = Utf8Decode(&c, lineend);
664  if (c == '\0' || c == '\n') break;
665  lineend += len;
666  }
667 
668  LineCacheItem& line = GetCachedParagraphLayout(str, lineend - str, state);
669  if (line.layout != NULL) {
670  /* Line is in cache */
671  str = lineend + 1;
672  state = line.state_after;
673  line.layout->Reflow();
674  } else {
675  /* Line is new, layout it */
676  FontState old_state = state;
677 #if defined(WITH_ICU_LAYOUT) || defined(WITH_UNISCRIBE) || defined(WITH_COCOA)
678  const char *old_str = str;
679 #endif
680 
681 #ifdef WITH_ICU_LAYOUT
682  GetLayouter<ICUParagraphLayoutFactory>(line, str, state);
683  if (line.layout == NULL) {
684  static bool warned = false;
685  if (!warned) {
686  DEBUG(misc, 0, "ICU layouter bailed on the font. Falling back to the fallback layouter");
687  warned = true;
688  }
689 
690  state = old_state;
691  str = old_str;
692  }
693 #endif
694 
695 #ifdef WITH_UNISCRIBE
696  if (line.layout == NULL) {
697  GetLayouter<UniscribeParagraphLayoutFactory>(line, str, state);
698  if (line.layout == NULL) {
699  state = old_state;
700  str = old_str;
701  }
702  }
703 #endif
704 
705 #ifdef WITH_COCOA
706  if (line.layout == NULL) {
707  GetLayouter<CoreTextParagraphLayoutFactory>(line, str, state);
708  if (line.layout == NULL) {
709  state = old_state;
710  str = old_str;
711  }
712  }
713 #endif
714 
715  if (line.layout == NULL) {
716  GetLayouter<FallbackParagraphLayoutFactory>(line, str, state);
717  }
718  }
719 
720  /* Copy all lines into a local cache so we can reuse them later on more easily. */
721  const ParagraphLayouter::Line *l;
722  while ((l = line.layout->NextLine(maxw)) != NULL) {
723  *this->Append() = l;
724  }
725 
726  } while (c != '\0');
727 }
728 
734 {
735  Dimension d = { 0, 0 };
736  for (const ParagraphLayouter::Line **l = this->Begin(); l != this->End(); l++) {
737  d.width = max<uint>(d.width, (*l)->GetWidth());
738  d.height += (*l)->GetLeading();
739  }
740  return d;
741 }
742 
749 Point Layouter::GetCharPosition(const char *ch) const
750 {
751  /* Find the code point index which corresponds to the char
752  * pointer into our UTF-8 source string. */
753  size_t index = 0;
754  const char *str = this->string;
755  while (str < ch) {
756  WChar c;
757  size_t len = Utf8Decode(&c, str);
758  if (c == '\0' || c == '\n') break;
759  str += len;
760  index += (*this->Begin())->GetInternalCharLength(c);
761  }
762 
763  if (str == ch) {
764  /* Valid character. */
765  const ParagraphLayouter::Line *line = *this->Begin();
766 
767  /* Pointer to the end-of-string/line marker? Return total line width. */
768  if (*ch == '\0' || *ch == '\n') {
769  Point p = { line->GetWidth(), 0 };
770  return p;
771  }
772 
773  /* Scan all runs until we've found our code point index. */
774  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
775  const ParagraphLayouter::VisualRun *run = line->GetVisualRun(run_index);
776 
777  for (int i = 0; i < run->GetGlyphCount(); i++) {
778  /* Matching glyph? Return position. */
779  if ((size_t)run->GetGlyphToCharMap()[i] == index) {
780  Point p = { (int)run->GetPositions()[i * 2], (int)run->GetPositions()[i * 2 + 1] };
781  return p;
782  }
783  }
784  }
785  }
786 
787  Point p = { 0, 0 };
788  return p;
789 }
790 
796 const char *Layouter::GetCharAtPosition(int x) const
797 {
798  const ParagraphLayouter::Line *line = *this->Begin();
799 
800  for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
801  const ParagraphLayouter::VisualRun *run = line->GetVisualRun(run_index);
802 
803  for (int i = 0; i < run->GetGlyphCount(); i++) {
804  /* Not a valid glyph (empty). */
805  if (run->GetGlyphs()[i] == 0xFFFF) continue;
806 
807  int begin_x = (int)run->GetPositions()[i * 2];
808  int end_x = (int)run->GetPositions()[i * 2 + 2];
809 
810  if (IsInsideMM(x, begin_x, end_x)) {
811  /* Found our glyph, now convert to UTF-8 string index. */
812  size_t index = run->GetGlyphToCharMap()[i];
813 
814  size_t cur_idx = 0;
815  for (const char *str = this->string; *str != '\0'; ) {
816  if (cur_idx == index) return str;
817 
818  WChar c = Utf8Consume(&str);
819  cur_idx += line->GetInternalCharLength(c);
820  }
821  }
822  }
823  }
824 
825  return NULL;
826 }
827 
832 {
833  FontColourMap::iterator it = fonts[size].Find(colour);
834  if (it != fonts[size].End()) return it->second;
835 
836  Font *f = new Font(size, colour);
837  *fonts[size].Append() = FontColourMap::Pair(colour, f);
838  return f;
839 }
840 
846 {
847  for (FontColourMap::iterator it = fonts[size].Begin(); it != fonts[size].End(); ++it) {
848  delete it->second;
849  }
850  fonts[size].Clear();
851 
852  /* We must reset the linecache since it references the just freed fonts */
853  ResetLineCache();
854 
855 #if defined(WITH_UNISCRIBE)
856  UniscribeResetScriptCache(size);
857 #endif
858 #if defined(WITH_COCOA)
859  MacOSResetScriptCache(size);
860 #endif
861 }
862 
871 Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
872 {
873  if (linecache == NULL) {
874  /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
875  linecache = new LineCache();
876  }
877 
878  LineCacheKey key;
879  key.state_before = state;
880  key.str.assign(str, len);
881  return (*linecache)[key];
882 }
883 
888 {
889  if (linecache != NULL) linecache->clear();
890 }
891 
896 {
897  if (linecache != NULL) {
898  /* TODO LRU cache would be fancy, but not exactly necessary */
899  if (linecache->size() > 4096) ResetLineCache();
900  }
901 }
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
const ParagraphLayouter::Line * NextLine(int max_width)
Construct a new line with a maximum width.
Definition: gfx_layout.cpp:488
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
Control codes that are embedded in the translation strings.
const Pair * Find(const T &key) const
Finds given key in this map.
FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x)
Create the visual run.
Definition: gfx_layout.cpp:334
static bool IsInsideMM(const T x, const uint min, const uint max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
Helper class to construct a new FallbackParagraphLayout.
Definition: gfx_layout.cpp:294
static void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
Helper for getting a ParagraphLayouter of the given type.
Definition: gfx_layout.cpp:592
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:470
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:185
const WChar * buffer_begin
Begin of the buffer.
Definition: gfx_layout.cpp:282
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
int GetWidth() const
Get the width of this line.
Definition: gfx_layout.cpp:433
Key into the linecache.
Definition: gfx_layout.h:157
void Clear()
Remove all items from the list.
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:1151
const T * Begin() const
Get the pointer to the first item (const)
int GetLeading() const
Get the height of this font.
Definition: gfx_layout.cpp:410
static bool IsTextDirectionChar(WChar c)
Is the given character a text direction character.
Definition: string_func.h:210
bool Insert(const T &key, const U &data)
Adds new item to this map.
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:87
const char * GetCharAtPosition(int x) const
Get the character that is at a position.
Definition: gfx_layout.cpp:796
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:448
const int * GetGlyphToCharMap() const
Get the glyph-to-character map for this visual run.
Definition: gfx_layout.cpp:401
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:271
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
Functions related to laying out the texts.
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:175
const T * End() const
Get the pointer behind the last valid item (const)
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition: gfx_layout.cpp:831
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:76
ParagraphLayouter::Line * * Append(uint to_add=1)
Append an item and return it.
icu::ParagraphLayout::Line * l
The actual ICU line.
Definition: gfx_layout.cpp:147
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
uint Length() const
Get the number of items in the list.
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:284
Simple vector template class, with automatic delete.
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
const ParagraphLayouter::VisualRun * GetVisualRun(int run) const
Get a specific visual run.
Definition: gfx_layout.cpp:459
WChar CharType
Helper for GetLayouter, to get the right type.
Definition: gfx_layout.cpp:297
void Reflow()
Reset the position to the start of the paragraph.
Definition: gfx_layout.cpp:478
Functions related to localized text support on OSX.
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:48
const float * GetPositions() const
Get the positions of this run.
Definition: gfx_layout.cpp:392
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:320
int GetLeading() const
Get the height of the line.
Definition: gfx_layout.cpp:419
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:845
Font cache for basic fonts.
Definition: fontcache.h:23
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:733
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:36
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
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:57
int CountRuns() const
Get the number of runs in this line.
Definition: gfx_layout.cpp:450
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:52
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:895
static void ResetLineCache()
Clear line cache.
Definition: gfx_layout.cpp:887
A single line worth of VisualRuns.
Definition: gfx_layout.cpp:272
static LineCacheItem & GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
Get reference to cache item.
Definition: gfx_layout.cpp:871
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:114
icu::ParagraphLayout * p
The actual ICU paragraph layout.
Definition: gfx_layout.cpp:128
const ParagraphLayouter::Line * * Get(uint index) const
Get the pointer to item "number" (const)
const Font * GetFont() const
Get the font associated with this run.
Definition: gfx_layout.cpp:365
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
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:178
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
int GetGlyphCount() const
Get the number of glyphs in this run.
Definition: gfx_layout.cpp:374
const WChar * buffer
The current location in the buffer.
Definition: gfx_layout.cpp:283
Layouter(const char *str, int maxw=INT32_MAX, TextColour colour=TC_FROMSTRING, FontSize fontsize=FS_NORMAL)
Create a new layouter.
Definition: gfx_layout.cpp:654
Dimensions (a width and height) of a rectangle in 2D.
Point GetCharPosition(const char *ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:749
const char * string
Pointer to the original string.
Definition: gfx_layout.h:154
const GlyphID * GetGlyphs() const
Get the glyphs of this run.
Definition: gfx_layout.cpp:383
static ParagraphLayouter * GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
Get the actual ParagraphLayout for the given buffer.
Definition: gfx_layout.cpp:308
Item in the linecache.
Definition: gfx_layout.h:172