OpenTTD Source: src/gfx_layout.cpp Source File

OpenTTD Source 1.11.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 "debug.h"
15
16 #include "table/control_codes.h"
17
18 #ifdef WITH_ICU_LX
19 #include <unicode/ustring.h>
20 #endif /* WITH_ICU_LX */
21
22 #ifdef WITH_UNISCRIBE
23 #include "os/windows/string_uniscribe.h"
24 #endif /* WITH_UNISCRIBE */
25
26 #ifdef WITH_COCOA
27 #include "os/macosx/string_osx.h"
28 #endif
29
30 #include "safeguards.h"
31
32
34 Layouter::LineCache *Layouter::linecache;
35
37 Layouter::FontColourMap Layouter::fonts[FS_END];
38
39
45 Font::Font(FontSize size, TextColour colour) :
46 fc(FontCache::Get(size)), colour(colour)
47 {
48 assert(size < FS_END);
49 }
50
51 #ifdef WITH_ICU_LX
52 /* Implementation details of LEFontInstance */
53
54 le_int32 Font::getUnitsPerEM() const
55 {
56 return this->fc->GetUnitsPerEM();
57 }
58
59 le_int32 Font::getAscent() const
60 {
61 return this->fc->GetAscender();
62 }
63
64 le_int32 Font::getDescent() const
65 {
66 return -this->fc->GetDescender();
67 }
68
69 le_int32 Font::getLeading() const
70 {
71 return this->fc->GetHeight();
72 }
73
74 float Font::getXPixelsPerEm() const
75 {
76 return (float)this->fc->GetHeight();
77 }
78
79 float Font::getYPixelsPerEm() const
80 {
81 return (float)this->fc->GetHeight();
82 }
83
84 float Font::getScaleFactorX() const
85 {
86 return 1.0f;
87 }
88
89 float Font::getScaleFactorY() const
90 {
91 return 1.0f;
92 }
93
94 const void *Font::getFontTable(LETag tableTag) const
95 {
96 size_t length;
97 return this->getFontTable(tableTag, length);
98 }
99
100 const void *Font::getFontTable(LETag tableTag, size_t &length) const
101 {
102 return this->fc->GetFontTable(tableTag, length);
103 }
104
105 LEGlyphID Font::mapCharToGlyph(LEUnicode32 ch) const
106 {
107 if (IsTextDirectionChar(ch)) return 0;
108 return this->fc->MapCharToGlyph(ch);
109 }
110
111 void Font::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
112 {
113 advance.fX = glyph == 0xFFFF ? 0 : this->fc->GetGlyphWidth(glyph);
114 advance.fY = 0;
115 }
116
117 le_bool Font::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
118 {
119 return false;
120 }
121
125 class ICUParagraphLayout : public ParagraphLayouter {
126 icu::ParagraphLayout *p;
127 public:
129 class ICUVisualRun : public ParagraphLayouter::VisualRun {
130 const icu::ParagraphLayout::VisualRun *vr;
131
132 public:
133 ICUVisualRun(const icu::ParagraphLayout::VisualRun *vr) : vr(vr) { }
134
135 const Font *GetFont() const override { return (const Font*)vr->getFont(); }
136 int GetGlyphCount() const override { return vr->getGlyphCount(); }
137 const GlyphID *GetGlyphs() const override { return vr->getGlyphs(); }
138 const float *GetPositions() const override { return vr->getPositions(); }
139 int GetLeading() const override { return vr->getLeading(); }
140 const int *GetGlyphToCharMap() const override { return vr->getGlyphToCharMap(); }
141 };
142
144 class ICULine : public std::vector<ICUVisualRun>, public ParagraphLayouter::Line {
145 icu::ParagraphLayout::Line *l;
146
147 public:
148 ICULine(icu::ParagraphLayout::Line *l) : l(l)
149 {
150 for (int i = 0; i < l->countRuns(); i++) {
151 this->emplace_back(l->getVisualRun(i));
152 }
153 }
154 ~ICULine() override { delete l; }
155
156 int GetLeading() const override { return l->getLeading(); }
157 int GetWidth() const override { return l->getWidth(); }
158 int CountRuns() const override { return l->countRuns(); }
159 const ParagraphLayouter::VisualRun &GetVisualRun(int run) const override { return this->at(run); }
160
161 int GetInternalCharLength(WChar c) const override
162 {
163 /* ICU uses UTF-16 internally which means we need to account for surrogate pairs. */
164 return Utf8CharLen(c) < 4 ? 1 : 2;
165 }
166 };
167
168 ICUParagraphLayout(icu::ParagraphLayout *p) : p(p) { }
169 ~ICUParagraphLayout() override { delete p; }
170 void Reflow() override { p->reflow(); }
171
172 std::unique_ptr<const Line> NextLine(int max_width) override
173 {
174 icu::ParagraphLayout::Line *l = p->nextLine(max_width);
175 return std::unique_ptr<const Line>(l == nullptr ? nullptr : new ICULine(l));
176 }
177 };
178
182 class ICUParagraphLayoutFactory {
183 public:
185 typedef UChar CharType;
187 static const bool SUPPORTS_RTL = true;
188
189 static ParagraphLayouter *GetParagraphLayout(UChar *buff, UChar *buff_end, FontMap &fontMapping)
190 {
191 int32 length = buff_end - buff;
192
193 if (length == 0) {
194 /* ICU's ParagraphLayout cannot handle empty strings, so fake one. */
195 buff[0] = ' ';
196 length = 1;
197 fontMapping.back().first++;
198 }
199
200 /* Fill ICU's FontRuns with the right data. */
201 icu::FontRuns runs(fontMapping.size());
202 for (auto &pair : fontMapping) {
203 runs.add(pair.second, pair.first);
204 }
205
206 LEErrorCode status = LE_NO_ERROR;
207 /* ParagraphLayout does not copy "buff", so it must stay valid.
208 * "runs" is copied according to the ICU source, but the documentation does not specify anything, so this might break somewhen. */
209 icu::ParagraphLayout *p = new icu::ParagraphLayout(buff, length, &runs, nullptr, nullptr, nullptr, _current_text_dir == TD_RTL ? 1 : 0, false, status);
210 if (status != LE_NO_ERROR) {
211 delete p;
212 return nullptr;
213 }
214
215 return new ICUParagraphLayout(p);
216 }
217
218 static size_t AppendToBuffer(UChar *buff, const UChar *buffer_last, WChar c)
219 {
220 /* Transform from UTF-32 to internal ICU format of UTF-16. */
221 int32 length = 0;
222 UErrorCode err = U_ZERO_ERROR;
223 u_strFromUTF32(buff, buffer_last - buff, &length, (UChar32*)&c, 1, &err);
224 return length;
225 }
226 };
227 #endif /* WITH_ICU_LX */
228
229 /*** Paragraph layout ***/
248 class FallbackParagraphLayout : public ParagraphLayouter {
249 public:
251 class FallbackVisualRun : public ParagraphLayouter::VisualRun {
252 Font *font;
253 GlyphID *glyphs;
254 float *positions;
255 int *glyph_to_char;
256 int glyph_count;
257
258 public:
259 FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x);
260 FallbackVisualRun(FallbackVisualRun &&other) noexcept;
261 ~FallbackVisualRun() override;
262 const Font *GetFont() const override;
263 int GetGlyphCount() const override;
264 const GlyphID *GetGlyphs() const override;
265 const float *GetPositions() const override;
266 int GetLeading() const override;
267 const int *GetGlyphToCharMap() const override;
268 };
269
271 class FallbackLine : public std::vector<FallbackVisualRun>, public ParagraphLayouter::Line {
272 public:
273 int GetLeading() const override;
274 int GetWidth() const override;
275 int CountRuns() const override;
276 const ParagraphLayouter::VisualRun &GetVisualRun(int run) const override;
277
278 int GetInternalCharLength(WChar c) const override { return 1; }
279 };
280
281 const WChar *buffer_begin;
282 const WChar *buffer;
283 FontMap &runs;
284
285 FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs);
286 void Reflow() override;
287 std::unique_ptr<const Line> NextLine(int max_width) override;
288 };
289
293 class FallbackParagraphLayoutFactory {
294 public:
296 typedef WChar CharType;
298 static const bool SUPPORTS_RTL = false;
299
307 static ParagraphLayouter *GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
308 {
309 return new FallbackParagraphLayout(buff, buff_end - buff, fontMapping);
310 }
311
319 static size_t AppendToBuffer(WChar *buff, const WChar *buffer_last, WChar c)
320 {
321 *buff = c;
322 return 1;
323 }
324 };
325
333 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(Font *font, const WChar *chars, int char_count, int x) :
334 font(font), glyph_count(char_count)
335 {
336 this->glyphs = MallocT<GlyphID>(this->glyph_count);
337 this->glyph_to_char = MallocT<int>(this->glyph_count);
338
339 /* Positions contains the location of the begin of each of the glyphs, and the end of the last one. */
340 this->positions = MallocT<float>(this->glyph_count * 2 + 2);
341 this->positions[0] = x;
342 this->positions[1] = 0;
343
344 for (int i = 0; i < this->glyph_count; i++) {
345 this->glyphs[i] = font->fc->MapCharToGlyph(chars[i]);
346 this->positions[2 * i + 2] = this->positions[2 * i] + font->fc->GetGlyphWidth(this->glyphs[i]);
347 this->positions[2 * i + 3] = 0;
348 this->glyph_to_char[i] = i;
349 }
350 }
351
353 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(FallbackVisualRun &&other) noexcept : font(other.font), glyph_count(other.glyph_count)
354 {
355 this->positions = other.positions;
356 this->glyph_to_char = other.glyph_to_char;
357 this->glyphs = other.glyphs;
358
359 other.positions = nullptr;
360 other.glyph_to_char = nullptr;
361 other.glyphs = nullptr;
362 }
363
365 FallbackParagraphLayout::FallbackVisualRun::~FallbackVisualRun()
366 {
367 free(this->positions);
368 free(this->glyph_to_char);
369 free(this->glyphs);
370 }
371
376 const Font *FallbackParagraphLayout::FallbackVisualRun::GetFont() const
377 {
378 return this->font;
379 }
380
385 int FallbackParagraphLayout::FallbackVisualRun::GetGlyphCount() const
386 {
387 return this->glyph_count;
388 }
389
394 const GlyphID *FallbackParagraphLayout::FallbackVisualRun::GetGlyphs() const
395 {
396 return this->glyphs;
397 }
398
403 const float *FallbackParagraphLayout::FallbackVisualRun::GetPositions() const
404 {
405 return this->positions;
406 }
407
412 const int *FallbackParagraphLayout::FallbackVisualRun::GetGlyphToCharMap() const
413 {
414 return this->glyph_to_char;
415 }
416
421 int FallbackParagraphLayout::FallbackVisualRun::GetLeading() const
422 {
423 return this->GetFont()->fc->GetHeight();
424 }
425
430 int FallbackParagraphLayout::FallbackLine::GetLeading() const
431 {
432 int leading = 0;
433 for (const auto &run : *this) {
434 leading = std::max(leading, run.GetLeading());
435 }
436
437 return leading;
438 }
439
444 int FallbackParagraphLayout::FallbackLine::GetWidth() const
445 {
446 if (this->size() == 0) return 0;
447
448 /*
449 * The last X position of a run contains is the end of that run.
450 * Since there is no left-to-right support, taking this value of
451 * the last run gives us the end of the line and thus the width.
452 */
453 const auto &run = this->GetVisualRun(this->CountRuns() - 1);
454 return (int)run.GetPositions()[run.GetGlyphCount() * 2];
455 }
456
461 int FallbackParagraphLayout::FallbackLine::CountRuns() const
462 {
463 return (uint)this->size();
464 }
465
470 const ParagraphLayouter::VisualRun &FallbackParagraphLayout::FallbackLine::GetVisualRun(int run) const
471 {
472 return this->at(run);
473 }
474
481 FallbackParagraphLayout::FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs) : buffer_begin(buffer), buffer(buffer), runs(runs)
482 {
483 assert(runs.End()[-1].first == length);
484 }
485
489 void FallbackParagraphLayout::Reflow()
490 {
491 this->buffer = this->buffer_begin;
492 }
493
499 std::unique_ptr<const ParagraphLayouter::Line> FallbackParagraphLayout::NextLine(int max_width)
500 {
501 /* Simple idea:
502 * - split a line at a newline character, or at a space where we can break a line.
503 * - split for a visual run whenever a new line happens, or the font changes.
504 */
505 if (this->buffer == nullptr) return nullptr;
506
507 std::unique_ptr<FallbackLine> l(new FallbackLine());
508
509 if (*this->buffer == '\0') {
510 /* Only a newline. */
511 this->buffer = nullptr;
512 l->emplace_back(this->runs.front().second, this->buffer, 0, 0);
513 return l;
514 }
515
516 int offset = this->buffer - this->buffer_begin;
517 FontMap::iterator iter = this->runs.data();
518 while (iter->first <= offset) {
519 iter++;
520 assert(iter != this->runs.End());
521 }
522
523 const FontCache *fc = iter->second->fc;
524 const WChar *next_run = this->buffer_begin + iter->first;
525
526 const WChar *begin = this->buffer;
527 const WChar *last_space = nullptr;
528 const WChar *last_char;
529 int width = 0;
530 for (;;) {
531 WChar c = *this->buffer;
532 last_char = this->buffer;
533
534 if (c == '\0') {
535 this->buffer = nullptr;
536 break;
537 }
538
539 if (this->buffer == next_run) {
540 int w = l->GetWidth();
541 l->emplace_back(iter->second, begin, this->buffer - begin, w);
542 iter++;
543 assert(iter != this->runs.End());
544
545 next_run = this->buffer_begin + iter->first;
546 begin = this->buffer;
547
548 last_space = nullptr;
549 }
550
551 if (IsWhitespace(c)) last_space = this->buffer;
552
553 if (IsPrintable(c) && !IsTextDirectionChar(c)) {
554 int char_width = GetCharacterWidth(fc->GetSize(), c);
555 width += char_width;
556 if (width > max_width) {
557 /* The string is longer than maximum width so we need to decide
558 * what to do with it. */
559 if (width == char_width) {
560 /* The character is wider than allowed width; don't know
561 * what to do with this case... bail out! */
562 this->buffer = nullptr;
563 return l;
564 }
565
566 if (last_space == nullptr) {
567 /* No space has been found. Just terminate at our current
568 * location. This usually happens for languages that do not
569 * require spaces in strings, like Chinese, Japanese and
570 * Korean. For other languages terminating mid-word might
571 * not be the best, but terminating the whole string instead
572 * of continuing the word at the next line is worse. */
573 last_char = this->buffer;
574 } else {
575 /* A space is found; perfect place to terminate */
576 this->buffer = last_space + 1;
577 last_char = last_space;
578 }
579 break;
580 }
581 }
582
583 this->buffer++;
584 }
585
586 if (l->size() == 0 || last_char - begin != 0) {
587 int w = l->GetWidth();
588 l->emplace_back(iter->second, begin, last_char - begin, w);
589 }
590 return l;
591 }
592
602 template <typename T>
603 static inline void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
604 {
605 if (line.buffer != nullptr) free(line.buffer);
606
607 typename T::CharType *buff_begin = MallocT<typename T::CharType>(DRAW_STRING_BUFFER);
608 const typename T::CharType *buffer_last = buff_begin + DRAW_STRING_BUFFER;
609 typename T::CharType *buff = buff_begin;
610 FontMap &fontMapping = line.runs;
611 Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
612
613 line.buffer = buff_begin;
614 fontMapping.clear();
615
616 /*
617 * Go through the whole string while adding Font instances to the font map
618 * whenever the font changes, and convert the wide characters into a format
619 * usable by ParagraphLayout.
620 */
621 for (; buff < buffer_last;) {
622 WChar c = Utf8Consume(const_cast<const char **>(&str));
623 if (c == '\0' || c == '\n') {
624 break;
625 } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
626 state.SetColour((TextColour)(c - SCC_BLUE));
627 } else if (c == SCC_PUSH_COLOUR) {
628 state.PushColour();
629 } else if (c == SCC_POP_COLOUR) {
630 state.PopColour();
631 } else if (c >= SCC_FIRST_FONT && c <= SCC_LAST_FONT) {
632 state.SetFontSize((FontSize)(c - SCC_FIRST_FONT));
633 } else {
634 /* Filter out non printable characters */
635 if (!IsPrintable(c)) continue;
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
746 Dimension Layouter::GetBounds()
747 {
748 Dimension d = { 0, 0 };
749 for (const auto &l : *this) {
750 d.width = std::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
844 Font *Layouter::GetFont(FontSize size, TextColour colour)
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
858 void Layouter::ResetFontCache(FontSize size)
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
900 void Layouter::ResetLineCache()
901 {
902 if (linecache != nullptr) linecache->clear();
903 }
904
908 void Layouter::ReduceLineCache()
909 {
910 if (linecache != nullptr) {
911 /* TODO LRU cache would be fancy, but not exactly necessary */
912 if (linecache->size() > 4096) ResetLineCache();
913 }
914 }
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
Layouter::LineCacheKey
Key into the linecache.
Definition: gfx_layout.h:155
Layouter::LineCacheItem
Item in the linecache.
Definition: gfx_layout.h:170
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
FontCache::GetSize
FontSize GetSize() const
Get the FontSize of the font.
Definition: fontcache.h:42
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
ICUParagraphLayout::ICUVisualRun::vr
const icu::ParagraphLayout::VisualRun * vr
The actual ICU vr.
Definition: gfx_layout.cpp:130
FontState::PopColour
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:55
ICUParagraphLayoutFactory::SUPPORTS_RTL
static const bool SUPPORTS_RTL
Helper for GetLayouter, to get whether the layouter supports RTL.
Definition: gfx_layout.cpp:187
Utf8CharLen
static int8 Utf8CharLen(WChar c)
Return the length of a UTF-8 encoded character.
Definition: string_func.h:109
Layouter::string
const char * string
Pointer to the original string.
Definition: gfx_layout.h:152
Layouter::linecache
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:183
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
Layouter::GetCharPosition
Point GetCharPosition(const char *ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:762
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:858
FallbackParagraphLayoutFactory
Helper class to construct a new FallbackParagraphLayout.
Definition: gfx_layout.cpp:293
FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun
FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x)
Create the visual run.
Definition: gfx_layout.cpp:333
SmallMap::Insert
bool Insert(const T &key, const U &data)
Adds new item to this map.
Definition: smallmap_type.hpp:127
FallbackParagraphLayout::runs
FontMap & runs
The fonts we have to use for this paragraph.
Definition: gfx_layout.cpp:283
ICUParagraphLayout::ICUVisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.cpp:129
ParagraphLayouter
Interface to glue fallback and normal layouter into one.
Definition: gfx_layout.h:115
SmallMap
Implementation of simple mapping class.
Definition: smallmap_type.hpp:26
Layouter::LineCacheItem::layout
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:176
control_codes.h
IsInsideMM
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:204
FallbackParagraphLayout::buffer_begin
const WChar * buffer_begin
Begin of the buffer.
Definition: gfx_layout.cpp:281
string_osx.h
ParagraphLayouter::Line
A single line worth of VisualRuns.
Definition: gfx_layout.h:132
Layouter::LineCacheKey::str
std::string str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:157
ICUParagraphLayoutFactory::CharType
UChar CharType
Helper for GetLayouter, to get the right type.
Definition: gfx_layout.cpp:185
FallbackParagraphLayout::FallbackLine::GetWidth
int GetWidth() const override
Get the width of this line.
Definition: gfx_layout.cpp:444
DRAW_STRING_BUFFER
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:85
Layouter::fonts
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition: gfx_layout.h:188
Layouter::LineCacheKey::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:156
FallbackParagraphLayout::FallbackVisualRun::GetPositions
const float * GetPositions() const override
Get the positions of this run.
Definition: gfx_layout.cpp:403
FallbackParagraphLayout::Reflow
void Reflow() override
Reset the position to the start of the paragraph.
Definition: gfx_layout.cpp:489
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:603
Layouter::LineCacheItem::runs
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:173
FallbackParagraphLayout::FallbackVisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.cpp:251
FontState::fontsize
FontSize fontsize
Current font size.
Definition: gfx_layout.h:34
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
Layouter::GetBounds
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:746
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:667
FallbackParagraphLayout::FallbackVisualRun::glyphs
GlyphID * glyphs
The glyphs we're drawing.
Definition: gfx_layout.cpp:253
safeguards.h
FallbackParagraphLayout::FallbackVisualRun::positions
float * positions
The positions of the glyphs.
Definition: gfx_layout.cpp:254
Layouter::GetFont
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition: gfx_layout.cpp:844
FallbackParagraphLayout::FallbackVisualRun::GetGlyphCount
int GetGlyphCount() const override
Get the number of glyphs in this run.
Definition: gfx_layout.cpp:385
FallbackParagraphLayout::FallbackLine::CountRuns
int CountRuns() const override
Get the number of runs in this line.
Definition: gfx_layout.cpp:461
FallbackParagraphLayoutFactory::SUPPORTS_RTL
static const bool SUPPORTS_RTL
Helper for GetLayouter, to get whether the layouter supports RTL.
Definition: gfx_layout.cpp:298
FallbackParagraphLayout::FallbackVisualRun::~FallbackVisualRun
~FallbackVisualRun() override
Free all data.
Definition: gfx_layout.cpp:365
gfx_layout.h
FallbackParagraphLayout::FallbackVisualRun::GetLeading
int GetLeading() const override
Get the height of this font.
Definition: gfx_layout.cpp:421
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:470
FallbackParagraphLayoutFactory::AppendToBuffer
static size_t AppendToBuffer(WChar *buff, const WChar *buffer_last, WChar c)
Append a wide character to the internal buffer.
Definition: gfx_layout.cpp:319
FallbackParagraphLayout
Class handling the splitting of a paragraph of text into lines and visual runs.
Definition: gfx_layout.cpp:248
Utf8Decode
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:481
Layouter::ReduceLineCache
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
Definition: gfx_layout.cpp:908
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:120
FallbackParagraphLayout::FallbackVisualRun::GetGlyphToCharMap
const int * GetGlyphToCharMap() const override
Get the glyph-to-character map for this visual run.
Definition: gfx_layout.cpp:412
FontState::cur_colour
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:35
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:394
FallbackParagraphLayout::FallbackLine::GetLeading
int GetLeading() const override
Get the height of the line.
Definition: gfx_layout.cpp:430
Layouter::GetCharAtPosition
const char * GetCharAtPosition(int x) const
Get the character that is at a position.
Definition: gfx_layout.cpp:809
FallbackParagraphLayout::FallbackVisualRun::glyph_count
int glyph_count
The number of glyphs.
Definition: gfx_layout.cpp:256
FontState::SetColour
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:46
Layouter::LineCacheItem::state_after
FontState state_after
Font state after the line.
Definition: gfx_layout.h:175
ICUParagraphLayout::p
icu::ParagraphLayout * p
The actual ICU paragraph layout.
Definition: gfx_layout.cpp:126
FallbackParagraphLayoutFactory::GetParagraphLayout
static ParagraphLayouter * GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
Get the actual ParagraphLayout for the given buffer.
Definition: gfx_layout.cpp:307
Layouter::GetCachedParagraphLayout
static LineCacheItem & GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
Get reference to cache item.
Definition: gfx_layout.cpp:884
FallbackParagraphLayout::FallbackLine
A single line worth of VisualRuns.
Definition: gfx_layout.cpp:271
ICUParagraphLayoutFactory
Helper class to construct a new ICUParagraphLayout.
Definition: gfx_layout.cpp:182
GetCharacterWidth
byte GetCharacterWidth(FontSize size, WChar key)
Return width of character glyph.
Definition: gfx.cpp:1295
SmallMap::Find
std::vector< Pair >::const_iterator Find(const T &key) const
Finds given key in this map.
Definition: smallmap_type.hpp:41
FontState::SetFontSize
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:74
FallbackParagraphLayoutFactory::CharType
WChar CharType
Helper for GetLayouter, to get the right type.
Definition: gfx_layout.cpp:296
FontState
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:33
ICUParagraphLayout
Wrapper for doing layouts with ICU.
Definition: gfx_layout.cpp:125
Layouter::ResetLineCache
static void ResetLineCache()
Clear line cache.
Definition: gfx_layout.cpp:900
IsWhitespace
static bool IsWhitespace(WChar c)
Check whether UNICODE character is whitespace or not, i.e.
Definition: string_func.h:252
MacOSResetScriptCache
void MacOSResetScriptCache(FontSize size)
Delete CoreText font reference for a specific font size.
Definition: string_osx.cpp:291
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
ICUParagraphLayout::ICULine::l
icu::ParagraphLayout::Line * l
The actual ICU line.
Definition: gfx_layout.cpp:145
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
ICUParagraphLayout::ICULine
A single line worth of VisualRuns.
Definition: gfx_layout.cpp:144
FallbackParagraphLayout::buffer
const WChar * buffer
The current location in the buffer.
Definition: gfx_layout.cpp:282
FontState::PushColour
void PushColour()
Push the current colour on to the stack.
Definition: gfx_layout.h:65
FallbackParagraphLayout::FallbackVisualRun::glyph_to_char
int * glyph_to_char
The char index of the glyphs.
Definition: gfx_layout.cpp:255
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition: strings_type.h:24
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:48
Layouter::LineCacheItem::buffer
void * buffer
Accessed by both ICU's and our ParagraphLayout::nextLine.
Definition: gfx_layout.h:172
FallbackParagraphLayout::FallbackVisualRun::font
Font * font
The font used to layout these.
Definition: gfx_layout.cpp:252
IsTextDirectionChar
static bool IsTextDirectionChar(WChar c)
Is the given character a text direction character.
Definition: string_func.h:220
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:499
string_uniscribe.h
FallbackParagraphLayout::FallbackParagraphLayout
FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs)
Create a new paragraph layouter.
Definition: gfx_layout.cpp:481
FallbackParagraphLayout::FallbackVisualRun::GetFont
const Font * GetFont() const override
Get the font associated with this run.
Definition: gfx_layout.cpp:376
SmallMap::Contains
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
Definition: smallmap_type.hpp:79