OpenTTD Source: src/gfx_layout.cpp Source File

OpenTTD Source 13.1

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