(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / gfx_layout.cpp
blobf5463d4015a26bfd7ab7165a8e447024c68335b6
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file gfx_layout.cpp Handling of laying out text. */
12 #include "stdafx.h"
13 #include "gfx_layout.h"
14 #include "string_func.h"
15 #include "strings_func.h"
16 #include "debug.h"
18 #include "table/control_codes.h"
20 #ifdef WITH_ICU_LAYOUT
21 #include <unicode/ustring.h>
22 #endif /* WITH_ICU_LAYOUT */
24 #include "safeguards.h"
27 /** Cache of ParagraphLayout lines. */
28 Layouter::LineCache *Layouter::linecache;
30 /** Cache of Font instances. */
31 Layouter::FontColourMap Layouter::fonts[FS_END];
34 /**
35 * Construct a new font.
36 * @param size The font size to use for this font.
37 * @param colour The colour to draw this font in.
39 Font::Font(FontSize size, TextColour colour) :
40 fc(FontCache::Get(size)), colour(colour)
42 assert(size < FS_END);
45 #ifdef WITH_ICU_LAYOUT
46 /* Implementation details of LEFontInstance */
48 le_int32 Font::getUnitsPerEM() const
50 return this->fc->GetUnitsPerEM();
53 le_int32 Font::getAscent() const
55 return this->fc->GetAscender();
58 le_int32 Font::getDescent() const
60 return -this->fc->GetDescender();
63 le_int32 Font::getLeading() const
65 return this->fc->GetHeight();
68 float Font::getXPixelsPerEm() const
70 return (float)this->fc->GetHeight();
73 float Font::getYPixelsPerEm() const
75 return (float)this->fc->GetHeight();
78 float Font::getScaleFactorX() const
80 return 1.0f;
83 float Font::getScaleFactorY() const
85 return 1.0f;
88 const void *Font::getFontTable(LETag tableTag) const
90 size_t length;
91 return this->getFontTable(tableTag, length);
94 const void *Font::getFontTable(LETag tableTag, size_t &length) const
96 return this->fc->GetFontTable(tableTag, length);
99 LEGlyphID Font::mapCharToGlyph(LEUnicode32 ch) const
101 if (IsTextDirectionChar(ch)) return 0;
102 return this->fc->MapCharToGlyph(ch);
105 void Font::getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const
107 advance.fX = glyph == 0xFFFF ? 0 : this->fc->GetGlyphWidth(glyph);
108 advance.fY = 0;
111 le_bool Font::getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const
113 return FALSE;
116 static size_t AppendToBuffer(UChar *buff, const UChar *buffer_last, WChar c)
118 /* Transform from UTF-32 to internal ICU format of UTF-16. */
119 int32 length = 0;
120 UErrorCode err = U_ZERO_ERROR;
121 u_strFromUTF32(buff, buffer_last - buff, &length, (UChar32*)&c, 1, &err);
122 return length;
126 * Wrapper for doing layouts with ICU.
128 class ICUParagraphLayout : public AutoDeleteSmallVector<ParagraphLayouter::Line *, 4>, public ParagraphLayouter {
129 ParagraphLayout *p; ///< The actual ICU paragraph layout.
130 public:
131 /** Helper for GetLayouter, to get the right type. */
132 typedef UChar CharType;
133 /** Helper for GetLayouter, to get whether the layouter supports RTL. */
134 static const bool SUPPORTS_RTL = true;
136 /** Visual run contains data about the bit of text with the same font. */
137 class ICUVisualRun : public ParagraphLayouter::VisualRun {
138 const ParagraphLayout::VisualRun *vr; ///< The actual ICU vr.
140 public:
141 ICUVisualRun(const ParagraphLayout::VisualRun *vr) : vr(vr) { }
143 const Font *GetFont() const { return (const Font*)vr->getFont(); }
144 int GetGlyphCount() const { return vr->getGlyphCount(); }
145 const GlyphID *GetGlyphs() const { return vr->getGlyphs(); }
146 const float *GetPositions() const { return vr->getPositions(); }
147 int GetLeading() const { return vr->getLeading(); }
148 const int *GetGlyphToCharMap() const { return vr->getGlyphToCharMap(); }
151 /** A single line worth of VisualRuns. */
152 class ICULine : public AutoDeleteSmallVector<ICUVisualRun *, 4>, public ParagraphLayouter::Line {
153 ParagraphLayout::Line *l; ///< The actual ICU line.
155 public:
156 ICULine(ParagraphLayout::Line *l) : l(l)
158 for (int i = 0; i < l->countRuns(); i++) {
159 *this->Append() = new ICUVisualRun(l->getVisualRun(i));
162 ~ICULine() { delete l; }
164 int GetLeading() const { return l->getLeading(); }
165 int GetWidth() const { return l->getWidth(); }
166 int CountRuns() const { return l->countRuns(); }
167 const ParagraphLayouter::VisualRun *GetVisualRun(int run) const { return *this->Get(run); }
169 int GetInternalCharLength(WChar c) const
171 /* ICU uses UTF-16 internally which means we need to account for surrogate pairs. */
172 return Utf8CharLen(c) < 4 ? 1 : 2;
176 ICUParagraphLayout(ParagraphLayout *p) : p(p) { }
177 ~ICUParagraphLayout() { delete p; }
178 void Reflow() { p->reflow(); }
180 ParagraphLayouter::Line *NextLine(int max_width)
182 ParagraphLayout::Line *l = p->nextLine(max_width);
183 return l == NULL ? NULL : new ICULine(l);
187 static ParagraphLayouter *GetParagraphLayout(UChar *buff, UChar *buff_end, FontMap &fontMapping)
189 int32 length = buff_end - buff;
191 if (length == 0) {
192 /* ICU's ParagraphLayout cannot handle empty strings, so fake one. */
193 buff[0] = ' ';
194 length = 1;
195 fontMapping.End()[-1].first++;
198 /* Fill ICU's FontRuns with the right data. */
199 FontRuns runs(fontMapping.Length());
200 for (FontMap::iterator iter = fontMapping.Begin(); iter != fontMapping.End(); iter++) {
201 runs.add(iter->second, iter->first);
204 LEErrorCode status = LE_NO_ERROR;
205 /* ParagraphLayout does not copy "buff", so it must stay valid.
206 * "runs" is copied according to the ICU source, but the documentation does not specify anything, so this might break somewhen. */
207 ParagraphLayout *p = new ParagraphLayout(buff, length, &runs, NULL, NULL, NULL, _current_text_dir == TD_RTL ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR, false, status);
208 if (status != LE_NO_ERROR) {
209 delete p;
210 return NULL;
213 return new ICUParagraphLayout(p);
216 #endif /* WITH_ICU_LAYOUT */
218 /*** Paragraph layout ***/
220 * Class handling the splitting of a paragraph of text into lines and
221 * visual runs.
223 * One constructs this class with the text that needs to be split into
224 * lines. Then nextLine is called with the maximum width until NULL is
225 * returned. Each nextLine call creates VisualRuns which contain the
226 * length of text that are to be drawn with the same font. In other
227 * words, the result of this class is a list of sub strings with their
228 * font. The sub strings are then already fully laid out, and only
229 * need actual drawing.
231 * The positions in a visual run are sequential pairs of X,Y of the
232 * begin of each of the glyphs plus an extra pair to mark the end.
234 * @note This variant does not handle left-to-right properly. This
235 * is supported in the one ParagraphLayout coming from ICU.
237 class FallbackParagraphLayout : public ParagraphLayouter {
238 public:
239 /** Helper for GetLayouter, to get the right type. */
240 typedef WChar CharType;
241 /** Helper for GetLayouter, to get whether the layouter supports RTL. */
242 static const bool SUPPORTS_RTL = false;
244 /** Visual run contains data about the bit of text with the same font. */
245 class FallbackVisualRun : public ParagraphLayouter::VisualRun {
246 Font *font; ///< The font used to layout these.
247 GlyphID *glyphs; ///< The glyphs we're drawing.
248 float *positions; ///< The positions of the glyphs.
249 int *glyph_to_char; ///< The char index of the glyphs.
250 int glyph_count; ///< The number of glyphs.
252 public:
253 FallbackVisualRun(Font *font, const WChar *chars, int glyph_count, int x);
254 ~FallbackVisualRun();
255 const Font *GetFont() const;
256 int GetGlyphCount() const;
257 const GlyphID *GetGlyphs() const;
258 const float *GetPositions() const;
259 int GetLeading() const;
260 const int *GetGlyphToCharMap() const;
263 /** A single line worth of VisualRuns. */
264 class FallbackLine : public AutoDeleteSmallVector<FallbackVisualRun *, 4>, public ParagraphLayouter::Line {
265 public:
266 int GetLeading() const;
267 int GetWidth() const;
268 int CountRuns() const;
269 const ParagraphLayouter::VisualRun *GetVisualRun(int run) const;
271 int GetInternalCharLength(WChar c) const { return 1; }
274 const WChar *buffer_begin; ///< Begin of the buffer.
275 const WChar *buffer; ///< The current location in the buffer.
276 FontMap &runs; ///< The fonts we have to use for this paragraph.
278 FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs);
279 void Reflow();
280 const ParagraphLayouter::Line *NextLine(int max_width);
284 * Create the visual run.
285 * @param font The font to use for this run.
286 * @param chars The characters to use for this run.
287 * @param char_count The number of characters in this run.
288 * @param x The initial x position for this run.
290 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(Font *font, const WChar *chars, int char_count, int x) :
291 font(font), glyph_count(char_count)
293 this->glyphs = MallocT<GlyphID>(this->glyph_count);
294 this->glyph_to_char = MallocT<int>(this->glyph_count);
296 /* Positions contains the location of the begin of each of the glyphs, and the end of the last one. */
297 this->positions = MallocT<float>(this->glyph_count * 2 + 2);
298 this->positions[0] = x;
299 this->positions[1] = 0;
301 for (int i = 0; i < this->glyph_count; i++) {
302 this->glyphs[i] = font->fc->MapCharToGlyph(chars[i]);
303 this->positions[2 * i + 2] = this->positions[2 * i] + font->fc->GetGlyphWidth(this->glyphs[i]);
304 this->positions[2 * i + 3] = 0;
305 this->glyph_to_char[i] = i;
309 /** Free all data. */
310 FallbackParagraphLayout::FallbackVisualRun::~FallbackVisualRun()
312 free(this->positions);
313 free(this->glyph_to_char);
314 free(this->glyphs);
318 * Get the font associated with this run.
319 * @return The font.
321 const Font *FallbackParagraphLayout::FallbackVisualRun::GetFont() const
323 return this->font;
327 * Get the number of glyphs in this run.
328 * @return The number of glyphs.
330 int FallbackParagraphLayout::FallbackVisualRun::GetGlyphCount() const
332 return this->glyph_count;
336 * Get the glyphs of this run.
337 * @return The glyphs.
339 const GlyphID *FallbackParagraphLayout::FallbackVisualRun::GetGlyphs() const
341 return this->glyphs;
345 * Get the positions of this run.
346 * @return The positions.
348 const float *FallbackParagraphLayout::FallbackVisualRun::GetPositions() const
350 return this->positions;
354 * Get the glyph-to-character map for this visual run.
355 * @return The glyph-to-character map.
357 const int *FallbackParagraphLayout::FallbackVisualRun::GetGlyphToCharMap() const
359 return this->glyph_to_char;
363 * Get the height of this font.
364 * @return The height of the font.
366 int FallbackParagraphLayout::FallbackVisualRun::GetLeading() const
368 return this->GetFont()->fc->GetHeight();
372 * Get the height of the line.
373 * @return The maximum height of the line.
375 int FallbackParagraphLayout::FallbackLine::GetLeading() const
377 int leading = 0;
378 for (const FallbackVisualRun * const *run = this->Begin(); run != this->End(); run++) {
379 leading = max(leading, (*run)->GetLeading());
382 return leading;
386 * Get the width of this line.
387 * @return The width of the line.
389 int FallbackParagraphLayout::FallbackLine::GetWidth() const
391 if (this->Length() == 0) return 0;
394 * The last X position of a run contains is the end of that run.
395 * Since there is no left-to-right support, taking this value of
396 * the last run gives us the end of the line and thus the width.
398 const ParagraphLayouter::VisualRun *run = this->GetVisualRun(this->CountRuns() - 1);
399 return (int)run->GetPositions()[run->GetGlyphCount() * 2];
403 * Get the number of runs in this line.
404 * @return The number of runs.
406 int FallbackParagraphLayout::FallbackLine::CountRuns() const
408 return this->Length();
412 * Get a specific visual run.
413 * @return The visual run.
415 const ParagraphLayouter::VisualRun *FallbackParagraphLayout::FallbackLine::GetVisualRun(int run) const
417 return *this->Get(run);
421 * Create a new paragraph layouter.
422 * @param buffer The characters of the paragraph.
423 * @param length The length of the paragraph.
424 * @param runs The font mapping of this paragraph.
426 FallbackParagraphLayout::FallbackParagraphLayout(WChar *buffer, int length, FontMap &runs) : buffer_begin(buffer), buffer(buffer), runs(runs)
428 assert(runs.End()[-1].first == length);
432 * Reset the position to the start of the paragraph.
434 void FallbackParagraphLayout::Reflow()
436 this->buffer = this->buffer_begin;
440 * Construct a new line with a maximum width.
441 * @param max_width The maximum width of the string.
442 * @return A Line, or NULL when at the end of the paragraph.
444 const ParagraphLayouter::Line *FallbackParagraphLayout::NextLine(int max_width)
446 /* Simple idea:
447 * - split a line at a newline character, or at a space where we can break a line.
448 * - split for a visual run whenever a new line happens, or the font changes.
450 if (this->buffer == NULL) return NULL;
452 FallbackLine *l = new FallbackLine();
454 if (*this->buffer == '\0') {
455 /* Only a newline. */
456 this->buffer = NULL;
457 *l->Append() = new FallbackVisualRun(this->runs.Begin()->second, this->buffer, 0, 0);
458 return l;
461 const WChar *begin = this->buffer;
462 const WChar *last_space = NULL;
463 const WChar *last_char = begin;
464 int width = 0;
466 int offset = this->buffer - this->buffer_begin;
467 FontMap::iterator iter = this->runs.Begin();
468 while (iter->first <= offset) {
469 iter++;
470 assert(iter != this->runs.End());
473 const FontCache *fc = iter->second->fc;
474 const WChar *next_run = this->buffer_begin + iter->first;
476 for (;;) {
477 WChar c = *this->buffer;
478 last_char = this->buffer;
480 if (c == '\0') {
481 this->buffer = NULL;
482 break;
485 if (this->buffer == next_run) {
486 int w = l->GetWidth();
487 *l->Append() = new FallbackVisualRun(iter->second, begin, this->buffer - begin, w);
488 iter++;
489 assert(iter != this->runs.End());
491 next_run = this->buffer_begin + iter->first;
492 begin = this->buffer;
494 last_space = NULL;
497 if (IsWhitespace(c)) last_space = this->buffer;
499 if (IsPrintable(c) && !IsTextDirectionChar(c)) {
500 int char_width = GetCharacterWidth(fc->GetSize(), c);
501 width += char_width;
502 if (width > max_width) {
503 /* The string is longer than maximum width so we need to decide
504 * what to do with it. */
505 if (width == char_width) {
506 /* The character is wider than allowed width; don't know
507 * what to do with this case... bail out! */
508 this->buffer = NULL;
509 return l;
512 if (last_space == NULL) {
513 /* No space has been found. Just terminate at our current
514 * location. This usually happens for languages that do not
515 * require spaces in strings, like Chinese, Japanese and
516 * Korean. For other languages terminating mid-word might
517 * not be the best, but terminating the whole string instead
518 * of continuing the word at the next line is worse. */
519 last_char = this->buffer;
520 } else {
521 /* A space is found; perfect place to terminate */
522 this->buffer = last_space + 1;
523 last_char = last_space;
525 break;
529 this->buffer++;
532 if (l->Length() == 0 || last_char - begin != 0) {
533 int w = l->GetWidth();
534 *l->Append() = new FallbackVisualRun(iter->second, begin, last_char - begin, w);
536 return l;
540 * Appand a wide character to the internal buffer.
541 * @param buff The buffer to append to.
542 * @param buffer_last The end of the buffer.
543 * @param c The character to add.
544 * @return The number of buffer spaces that were used.
546 static size_t AppendToBuffer(WChar *buff, const WChar *buffer_last, WChar c)
548 *buff = c;
549 return 1;
553 * Get the actual ParagraphLayout for the given buffer.
554 * @param buff The begin of the buffer.
555 * @param buff_end The location after the last element in the buffer.
556 * @param fontMapping THe mapping of the fonts.
557 * @return The ParagraphLayout instance.
559 static FallbackParagraphLayout *GetParagraphLayout(WChar *buff, WChar *buff_end, FontMap &fontMapping)
561 return new FallbackParagraphLayout(buff, buff_end - buff, fontMapping);
565 * Helper for getting a ParagraphLayouter of the given type.
567 * @note In case no ParagraphLayouter could be constructed, line.layout will be NULL.
568 * @param line The cache item to store our layouter in.
569 * @param str The string to create a layouter for.
570 * @param state The state of the font and color.
571 * @tparam T The type of layouter we want.
573 template <typename T>
574 static inline void GetLayouter(Layouter::LineCacheItem &line, const char *&str, FontState &state)
576 if (line.buffer != NULL) free(line.buffer);
578 typename T::CharType *buff_begin = MallocT<typename T::CharType>(DRAW_STRING_BUFFER);
579 const typename T::CharType *buffer_last = buff_begin + DRAW_STRING_BUFFER;
580 typename T::CharType *buff = buff_begin;
581 FontMap &fontMapping = line.runs;
582 Font *f = Layouter::GetFont(state.fontsize, state.cur_colour);
584 line.buffer = buff_begin;
587 * Go through the whole string while adding Font instances to the font map
588 * whenever the font changes, and convert the wide characters into a format
589 * usable by ParagraphLayout.
591 for (; buff < buffer_last;) {
592 WChar c = Utf8Consume(const_cast<const char **>(&str));
593 if (c == '\0' || c == '\n') {
594 break;
595 } else if (c >= SCC_BLUE && c <= SCC_BLACK) {
596 state.SetColour((TextColour)(c - SCC_BLUE));
597 } else if (c == SCC_PREVIOUS_COLOUR) { // Revert to the previous colour.
598 state.SetPreviousColour();
599 } else if (c == SCC_TINYFONT) {
600 state.SetFontSize(FS_SMALL);
601 } else if (c == SCC_BIGFONT) {
602 state.SetFontSize(FS_LARGE);
603 } else {
604 /* Filter out text direction characters that shouldn't be drawn, and
605 * will not be handled in the fallback non ICU case because they are
606 * mostly needed for RTL languages which need more ICU support. */
607 if (!T::SUPPORTS_RTL && IsTextDirectionChar(c)) continue;
608 buff += AppendToBuffer(buff, buffer_last, c);
609 continue;
612 if (!fontMapping.Contains(buff - buff_begin)) {
613 fontMapping.Insert(buff - buff_begin, f);
615 f = Layouter::GetFont(state.fontsize, state.cur_colour);
618 /* Better safe than sorry. */
619 *buff = '\0';
621 if (!fontMapping.Contains(buff - buff_begin)) {
622 fontMapping.Insert(buff - buff_begin, f);
624 line.layout = GetParagraphLayout(buff_begin, buff, fontMapping);
625 line.state_after = state;
629 * Create a new layouter.
630 * @param str The string to create the layout for.
631 * @param maxw The maximum width.
632 * @param colour The colour of the font.
633 * @param fontsize The size of font to use.
635 Layouter::Layouter(const char *str, int maxw, TextColour colour, FontSize fontsize) : string(str)
637 FontState state(colour, fontsize);
638 WChar c = 0;
640 do {
641 /* Scan string for end of line */
642 const char *lineend = str;
643 for (;;) {
644 size_t len = Utf8Decode(&c, lineend);
645 if (c == '\0' || c == '\n') break;
646 lineend += len;
649 LineCacheItem& line = GetCachedParagraphLayout(str, lineend - str, state);
650 if (line.layout != NULL) {
651 /* Line is in cache */
652 str = lineend + 1;
653 state = line.state_after;
654 line.layout->Reflow();
655 } else {
656 /* Line is new, layout it */
657 #ifdef WITH_ICU_LAYOUT
658 FontState old_state = state;
659 const char *old_str = str;
661 GetLayouter<ICUParagraphLayout>(line, str, state);
662 if (line.layout == NULL) {
663 static bool warned = false;
664 if (!warned) {
665 DEBUG(misc, 0, "ICU layouter bailed on the font. Falling back to the fallback layouter");
666 warned = true;
669 state = old_state;
670 str = old_str;
671 GetLayouter<FallbackParagraphLayout>(line, str, state);
673 #else
674 GetLayouter<FallbackParagraphLayout>(line, str, state);
675 #endif
678 /* Copy all lines into a local cache so we can reuse them later on more easily. */
679 const ParagraphLayouter::Line *l;
680 while ((l = line.layout->NextLine(maxw)) != NULL) {
681 *this->Append() = l;
684 } while (c != '\0');
688 * Get the boundaries of this paragraph.
689 * @return The boundaries.
691 Dimension Layouter::GetBounds()
693 Dimension d = { 0, 0 };
694 for (const ParagraphLayouter::Line **l = this->Begin(); l != this->End(); l++) {
695 d.width = max<uint>(d.width, (*l)->GetWidth());
696 d.height += (*l)->GetLeading();
698 return d;
702 * Get the position of a character in the layout.
703 * @param ch Character to get the position of.
704 * @return Upper left corner of the character relative to the start of the string.
705 * @note Will only work right for single-line strings.
707 Point Layouter::GetCharPosition(const char *ch) const
709 /* Find the code point index which corresponds to the char
710 * pointer into our UTF-8 source string. */
711 size_t index = 0;
712 const char *str = this->string;
713 while (str < ch) {
714 WChar c;
715 size_t len = Utf8Decode(&c, str);
716 if (c == '\0' || c == '\n') break;
717 str += len;
718 index += (*this->Begin())->GetInternalCharLength(c);
721 if (str == ch) {
722 /* Valid character. */
723 const ParagraphLayouter::Line *line = *this->Begin();
725 /* Pointer to the end-of-string/line marker? Return total line width. */
726 if (*ch == '\0' || *ch == '\n') {
727 Point p = { line->GetWidth(), 0 };
728 return p;
731 /* Scan all runs until we've found our code point index. */
732 for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
733 const ParagraphLayouter::VisualRun *run = line->GetVisualRun(run_index);
735 for (int i = 0; i < run->GetGlyphCount(); i++) {
736 /* Matching glyph? Return position. */
737 if ((size_t)run->GetGlyphToCharMap()[i] == index) {
738 Point p = { (int)run->GetPositions()[i * 2], (int)run->GetPositions()[i * 2 + 1] };
739 return p;
745 Point p = { 0, 0 };
746 return p;
750 * Get the character that is at a position.
751 * @param x Position in the string.
752 * @return Pointer to the character at the position or NULL if no character is at the position.
754 const char *Layouter::GetCharAtPosition(int x) const
756 const ParagraphLayouter::Line *line = *this->Begin();
758 for (int run_index = 0; run_index < line->CountRuns(); run_index++) {
759 const ParagraphLayouter::VisualRun *run = line->GetVisualRun(run_index);
761 for (int i = 0; i < run->GetGlyphCount(); i++) {
762 /* Not a valid glyph (empty). */
763 if (run->GetGlyphs()[i] == 0xFFFF) continue;
765 int begin_x = (int)run->GetPositions()[i * 2];
766 int end_x = (int)run->GetPositions()[i * 2 + 2];
768 if (IsInsideMM(x, begin_x, end_x)) {
769 /* Found our glyph, now convert to UTF-8 string index. */
770 size_t index = run->GetGlyphToCharMap()[i];
772 size_t cur_idx = 0;
773 for (const char *str = this->string; *str != '\0'; ) {
774 if (cur_idx == index) return str;
776 WChar c = Utf8Consume(&str);
777 cur_idx += line->GetInternalCharLength(c);
783 return NULL;
787 * Get a static font instance.
789 Font *Layouter::GetFont(FontSize size, TextColour colour)
791 FontColourMap::iterator it = fonts[size].Find(colour);
792 if (it != fonts[size].End()) return it->second;
794 Font *f = new Font(size, colour);
795 *fonts[size].Append() = FontColourMap::Pair(colour, f);
796 return f;
800 * Reset cached font information.
801 * @param size Font size to reset.
803 void Layouter::ResetFontCache(FontSize size)
805 for (FontColourMap::iterator it = fonts[size].Begin(); it != fonts[size].End(); ++it) {
806 delete it->second;
808 fonts[size].Clear();
810 /* We must reset the linecache since it references the just freed fonts */
811 ResetLineCache();
815 * Get reference to cache item.
816 * If the item does not exist yet, it is default constructed.
817 * @param str Source string of the line (including colour and font size codes).
818 * @param len Length of \a str in bytes (no termination).
819 * @param state State of the font at the beginning of the line.
820 * @return Reference to cache item.
822 Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
824 if (linecache == NULL) {
825 /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
826 linecache = new LineCache();
829 LineCacheKey key;
830 key.state_before = state;
831 key.str.assign(str, len);
832 return (*linecache)[key];
836 * Clear line cache.
838 void Layouter::ResetLineCache()
840 if (linecache != NULL) linecache->clear();
844 * Reduce the size of linecache if necessary to prevent infinite growth.
846 void Layouter::ReduceLineCache()
848 if (linecache != NULL) {
849 /* TODO LRU cache would be fancy, but not exactly necessary */
850 if (linecache->size() > 4096) ResetLineCache();