Fix: Stopped ships shouldn't block depots (#8578)
[openttd-github.git] / src / gfx_layout.cpp
blob9f1f2742ade535fa3676faeb98cd900922436f84
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 */
8 /** @file gfx_layout.cpp Handling of laying out text. */
10 #include "stdafx.h"
11 #include "gfx_layout.h"
12 #include "string_func.h"
13 #include "strings_func.h"
14 #include "debug.h"
16 #include "table/control_codes.h"
18 #ifdef WITH_ICU_LX
19 #include <unicode/ustring.h>
20 #endif /* WITH_ICU_LX */
22 #ifdef WITH_UNISCRIBE
23 #include "os/windows/string_uniscribe.h"
24 #endif /* WITH_UNISCRIBE */
26 #ifdef WITH_COCOA
27 #include "os/macosx/string_osx.h"
28 #endif
30 #include "safeguards.h"
33 /** Cache of ParagraphLayout lines. */
34 Layouter::LineCache *Layouter::linecache;
36 /** Cache of Font instances. */
37 Layouter::FontColourMap Layouter::fonts[FS_END];
40 /**
41 * Construct a new font.
42 * @param size The font size to use for this font.
43 * @param colour The colour to draw this font in.
45 Font::Font(FontSize size, TextColour colour) :
46 fc(FontCache::Get(size)), colour(colour)
48 assert(size < FS_END);
51 #ifdef WITH_ICU_LX
52 /* Implementation details of LEFontInstance */
54 le_int32 Font::getUnitsPerEM() const
56 return this->fc->GetUnitsPerEM();
59 le_int32 Font::getAscent() const
61 return this->fc->GetAscender();
64 le_int32 Font::getDescent() const
66 return -this->fc->GetDescender();
69 le_int32 Font::getLeading() const
71 return this->fc->GetHeight();
74 float Font::getXPixelsPerEm() const
76 return (float)this->fc->GetHeight();
79 float Font::getYPixelsPerEm() const
81 return (float)this->fc->GetHeight();
84 float Font::getScaleFactorX() const
86 return 1.0f;
89 float Font::getScaleFactorY() const
91 return 1.0f;
94 const void *Font::getFontTable(LETag tableTag) const
96 size_t length;
97 return this->getFontTable(tableTag, length);
100 const void *Font::getFontTable(LETag tableTag, size_t &length) const
102 return this->fc->GetFontTable(tableTag, length);
105 LEGlyphID Font::mapCharToGlyph(LEUnicode32 ch) const
107 if (IsTextDirectionChar(ch)) return 0;
108 return this->fc->MapCharToGlyph(ch);
111 void Font::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
113 advance.fX = glyph == 0xFFFF ? 0 : this->fc->GetGlyphWidth(glyph);
114 advance.fY = 0;
117 le_bool Font::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
119 return false;
123 * Wrapper for doing layouts with ICU.
125 class ICUParagraphLayout : public ParagraphLayouter {
126 icu::ParagraphLayout *p; ///< The actual ICU paragraph layout.
127 public:
128 /** Visual run contains data about the bit of text with the same font. */
129 class ICUVisualRun : public ParagraphLayouter::VisualRun {
130 const icu::ParagraphLayout::VisualRun *vr; ///< The actual ICU vr.
132 public:
133 ICUVisualRun(const icu::ParagraphLayout::VisualRun *vr) : vr(vr) { }
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(); }
143 /** A single line worth of VisualRuns. */
144 class ICULine : public std::vector<ICUVisualRun>, public ParagraphLayouter::Line {
145 icu::ParagraphLayout::Line *l; ///< The actual ICU line.
147 public:
148 ICULine(icu::ParagraphLayout::Line *l) : l(l)
150 for (int i = 0; i < l->countRuns(); i++) {
151 this->emplace_back(l->getVisualRun(i));
154 ~ICULine() override { delete l; }
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); }
161 int GetInternalCharLength(WChar c) const override
163 /* ICU uses UTF-16 internally which means we need to account for surrogate pairs. */
164 return Utf8CharLen(c) < 4 ? 1 : 2;
168 ICUParagraphLayout(icu::ParagraphLayout *p) : p(p) { }
169 ~ICUParagraphLayout() override { delete p; }
170 void Reflow() override { p->reflow(); }
172 std::unique_ptr<const Line> NextLine(int max_width) override
174 icu::ParagraphLayout::Line *l = p->nextLine(max_width);
175 return std::unique_ptr<const Line>(l == nullptr ? nullptr : new ICULine(l));
180 * Helper class to construct a new #ICUParagraphLayout.
182 class ICUParagraphLayoutFactory {
183 public:
184 /** Helper for GetLayouter, to get the right type. */
185 typedef UChar CharType;
186 /** Helper for GetLayouter, to get whether the layouter supports RTL. */
187 static const bool SUPPORTS_RTL = true;
189 static ParagraphLayouter *GetParagraphLayout(UChar *buff, UChar *buff_end, FontMap &fontMapping)
191 int32 length = buff_end - buff;
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++;
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);
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;
215 return new ICUParagraphLayout(p);
218 static size_t AppendToBuffer(UChar *buff, const UChar *buffer_last, WChar c)
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;
227 #endif /* WITH_ICU_LX */
229 /*** Paragraph layout ***/
231 * Class handling the splitting of a paragraph of text into lines and
232 * visual runs.
234 * One constructs this class with the text that needs to be split into
235 * lines. Then nextLine is called with the maximum width until nullptr is
236 * returned. Each nextLine call creates VisualRuns which contain the
237 * length of text that are to be drawn with the same font. In other
238 * words, the result of this class is a list of sub strings with their
239 * font. The sub strings are then already fully laid out, and only
240 * need actual drawing.
242 * The positions in a visual run are sequential pairs of X,Y of the
243 * begin of each of the glyphs plus an extra pair to mark the end.
245 * @note This variant does not handle left-to-right properly. This
246 * is supported in the one ParagraphLayout coming from ICU.
248 class FallbackParagraphLayout : public ParagraphLayouter {
249 public:
250 /** Visual run contains data about the bit of text with the same font. */
251 class FallbackVisualRun : public ParagraphLayouter::VisualRun {
252 Font *font; ///< The font used to layout these.
253 GlyphID *glyphs; ///< The glyphs we're drawing.
254 float *positions; ///< The positions of the glyphs.
255 int *glyph_to_char; ///< The char index of the glyphs.
256 int glyph_count; ///< The number of glyphs.
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;
270 /** A single line worth of VisualRuns. */
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;
278 int GetInternalCharLength(WChar c) const override { return 1; }
281 const WChar *buffer_begin; ///< Begin of the buffer.
282 const WChar *buffer; ///< The current location in the buffer.
283 FontMap &runs; ///< The fonts we have to use for this paragraph.
285 FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs);
286 void Reflow() override;
287 std::unique_ptr<const Line> NextLine(int max_width) override;
291 * Helper class to construct a new #FallbackParagraphLayout.
293 class FallbackParagraphLayoutFactory {
294 public:
295 /** Helper for GetLayouter, to get the right type. */
296 typedef WChar CharType;
297 /** Helper for GetLayouter, to get whether the layouter supports RTL. */
298 static const bool SUPPORTS_RTL = false;
301 * Get the actual ParagraphLayout for the given buffer.
302 * @param buff The begin of the buffer.
303 * @param buff_end The location after the last element in the buffer.
304 * @param fontMapping THe mapping of the fonts.
305 * @return The ParagraphLayout instance.
307 static ParagraphLayouter *GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
309 return new FallbackParagraphLayout(buff, buff_end - buff, fontMapping);
313 * Append a wide character to the internal buffer.
314 * @param buff The buffer to append to.
315 * @param buffer_last The end of the buffer.
316 * @param c The character to add.
317 * @return The number of buffer spaces that were used.
319 static size_t AppendToBuffer(WChar *buff, const WChar *buffer_last, WChar c)
321 *buff = c;
322 return 1;
327 * Create the visual run.
328 * @param font The font to use for this run.
329 * @param chars The characters to use for this run.
330 * @param char_count The number of characters in this run.
331 * @param x The initial x position for this run.
333 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(Font *font, const WChar *chars, int char_count, int x) :
334 font(font), glyph_count(char_count)
336 this->glyphs = MallocT<GlyphID>(this->glyph_count);
337 this->glyph_to_char = MallocT<int>(this->glyph_count);
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;
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;
352 /** Move constructor for visual runs.*/
353 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(FallbackVisualRun &&other) noexcept : font(other.font), glyph_count(other.glyph_count)
355 this->positions = other.positions;
356 this->glyph_to_char = other.glyph_to_char;
357 this->glyphs = other.glyphs;
359 other.positions = nullptr;
360 other.glyph_to_char = nullptr;
361 other.glyphs = nullptr;
364 /** Free all data. */
365 FallbackParagraphLayout::FallbackVisualRun::~FallbackVisualRun()
367 free(this->positions);
368 free(this->glyph_to_char);
369 free(this->glyphs);
373 * Get the font associated with this run.
374 * @return The font.
376 const Font *FallbackParagraphLayout::FallbackVisualRun::GetFont() const
378 return this->font;
382 * Get the number of glyphs in this run.
383 * @return The number of glyphs.
385 int FallbackParagraphLayout::FallbackVisualRun::GetGlyphCount() const
387 return this->glyph_count;
391 * Get the glyphs of this run.
392 * @return The glyphs.
394 const GlyphID *FallbackParagraphLayout::FallbackVisualRun::GetGlyphs() const
396 return this->glyphs;
400 * Get the positions of this run.
401 * @return The positions.
403 const float *FallbackParagraphLayout::FallbackVisualRun::GetPositions() const
405 return this->positions;
409 * Get the glyph-to-character map for this visual run.
410 * @return The glyph-to-character map.
412 const int *FallbackParagraphLayout::FallbackVisualRun::GetGlyphToCharMap() const
414 return this->glyph_to_char;
418 * Get the height of this font.
419 * @return The height of the font.
421 int FallbackParagraphLayout::FallbackVisualRun::GetLeading() const
423 return this->GetFont()->fc->GetHeight();
427 * Get the height of the line.
428 * @return The maximum height of the line.
430 int FallbackParagraphLayout::FallbackLine::GetLeading() const
432 int leading = 0;
433 for (const auto &run : *this) {
434 leading = std::max(leading, run.GetLeading());
437 return leading;
441 * Get the width of this line.
442 * @return The width of the line.
444 int FallbackParagraphLayout::FallbackLine::GetWidth() const
446 if (this->size() == 0) return 0;
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.
453 const auto &run = this->GetVisualRun(this->CountRuns() - 1);
454 return (int)run.GetPositions()[run.GetGlyphCount() * 2];
458 * Get the number of runs in this line.
459 * @return The number of runs.
461 int FallbackParagraphLayout::FallbackLine::CountRuns() const
463 return (uint)this->size();
467 * Get a specific visual run.
468 * @return The visual run.
470 const ParagraphLayouter::VisualRun &FallbackParagraphLayout::FallbackLine::GetVisualRun(int run) const
472 return this->at(run);
476 * Create a new paragraph layouter.
477 * @param buffer The characters of the paragraph.
478 * @param length The length of the paragraph.
479 * @param runs The font mapping of this paragraph.
481 FallbackParagraphLayout::FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs) : buffer_begin(buffer), buffer(buffer), runs(runs)
483 assert(runs.End()[-1].first == length);
487 * Reset the position to the start of the paragraph.
489 void FallbackParagraphLayout::Reflow()
491 this->buffer = this->buffer_begin;
495 * Construct a new line with a maximum width.
496 * @param max_width The maximum width of the string.
497 * @return A Line, or nullptr when at the end of the paragraph.
499 std::unique_ptr<const ParagraphLayouter::Line> FallbackParagraphLayout::NextLine(int max_width)
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.
505 if (this->buffer == nullptr) return nullptr;
507 std::unique_ptr<FallbackLine> l(new FallbackLine());
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;
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());
523 const FontCache *fc = iter->second->fc;
524 const WChar *next_run = this->buffer_begin + iter->first;
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;
534 if (c == '\0') {
535 this->buffer = nullptr;
536 break;
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());
545 next_run = this->buffer_begin + iter->first;
546 begin = this->buffer;
548 last_space = nullptr;
551 if (IsWhitespace(c)) last_space = this->buffer;
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;
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;
579 break;
583 this->buffer++;
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);
590 return l;
594 * Helper for getting a ParagraphLayouter of the given type.
596 * @note In case no ParagraphLayouter could be constructed, line.layout will be nullptr.
597 * @param line The cache item to store our layouter in.
598 * @param str The string to create a layouter for.
599 * @param state The state of the font and color.
600 * @tparam T The type of layouter we want.
602 template <typename T>
603 static inline void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
605 if (line.buffer != nullptr) free(line.buffer);
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);
613 line.buffer = buff_begin;
614 fontMapping.clear();
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.
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;
644 if (!fontMapping.Contains(buff - buff_begin)) {
645 fontMapping.Insert(buff - buff_begin, f);
647 f = Layouter::GetFont(state.fontsize, state.cur_colour);
650 /* Better safe than sorry. */
651 *buff = '\0';
653 if (!fontMapping.Contains(buff - buff_begin)) {
654 fontMapping.Insert(buff - buff_begin, f);
656 line.layout = T::GetParagraphLayout(buff_begin, buff, fontMapping);
657 line.state_after = state;
661 * Create a new layouter.
662 * @param str The string to create the layout for.
663 * @param maxw The maximum width.
664 * @param colour The colour of the font.
665 * @param fontsize The size of font to use.
667 Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsize) : string(str)
669 FontState state(colour, fontsize);
670 WChar c = 0;
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;
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
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;
703 state = old_state;
704 str = old_str;
706 #endif
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;
716 #endif
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;
726 #endif
728 if (line.layout == nullptr) {
729 GetLayouter<FallbackParagraphLayoutFactory>(line, str, state);
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));
739 } while (c != '\0');
743 * Get the boundaries of this paragraph.
744 * @return The boundaries.
746 Dimension Layouter::GetBounds()
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();
753 return d;
757 * Get the position of a character in the layout.
758 * @param ch Character to get the position of.
759 * @return Upper left corner of the character relative to the start of the string.
760 * @note Will only work right for single-line strings.
762 Point Layouter::GetCharPosition(const char *ch) const
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);
776 if (str == ch) {
777 /* Valid character. */
778 const auto &line = this->front();
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;
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);
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;
800 Point p = { 0, 0 };
801 return p;
805 * Get the character that is at a position.
806 * @param x Position in the string.
807 * @return Pointer to the character at the position or nullptr if no character is at the position.
809 const char *Layouter::GetCharAtPosition(int x) const
811 const auto &line = this->front();
813 for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
814 const ParagraphLayouter::VisualRun &run = line->GetVisualRun(run_index);
816 for (int i = 0; i < run.GetGlyphCount(); i++) {
817 /* Not a valid glyph (empty). */
818 if (run.GetGlyphs()[i] == 0xFFFF) continue;
820 int begin_x = (int)run.GetPositions()[i * 2];
821 int end_x = (int)run.GetPositions()[i * 2 + 2];
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];
827 size_t cur_idx = 0;
828 for (const char *str = this->string; *str != '\0'; ) {
829 if (cur_idx == index) return str;
831 WChar c = Utf8Consume(&str);
832 cur_idx += line->GetInternalCharLength(c);
838 return nullptr;
842 * Get a static font instance.
844 Font *Layouter::GetFont(FontSize size, TextColour colour)
846 FontColourMap::iterator it = fonts[size].Find(colour);
847 if (it != fonts[size].End()) return it->second;
849 Font *f = new Font(size, colour);
850 fonts[size].emplace_back(colour, f);
851 return f;
855 * Reset cached font information.
856 * @param size Font size to reset.
858 void Layouter::ResetFontCache(FontSize size)
860 for (auto &pair : fonts[size]) {
861 delete pair.second;
863 fonts[size].clear();
865 /* We must reset the linecache since it references the just freed fonts */
866 ResetLineCache();
868 #if defined(WITH_UNISCRIBE)
869 UniscribeResetScriptCache(size);
870 #endif
871 #if defined(WITH_COCOA)
872 MacOSResetScriptCache(size);
873 #endif
877 * Get reference to cache item.
878 * If the item does not exist yet, it is default constructed.
879 * @param str Source string of the line (including colour and font size codes).
880 * @param len Length of \a str in bytes (no termination).
881 * @param state State of the font at the beginning of the line.
882 * @return Reference to cache item.
884 Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
886 if (linecache == nullptr) {
887 /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
888 linecache = new LineCache();
891 LineCacheKey key;
892 key.state_before = state;
893 key.str.assign(str, len);
894 return (*linecache)[key];
898 * Clear line cache.
900 void Layouter::ResetLineCache()
902 if (linecache != nullptr) linecache->clear();
906 * Reduce the size of linecache if necessary to prevent infinite growth.
908 void Layouter::ReduceLineCache()
910 if (linecache != nullptr) {
911 /* TODO LRU cache would be fancy, but not exactly necessary */
912 if (linecache->size() > 4096) ResetLineCache();