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/>.
10 /** @file gfx_layout.cpp Handling of laying out text. */
13 #include "gfx_layout.h"
14 #include "string_func.h"
15 #include "strings_func.h"
18 #include "table/control_codes.h"
21 #include <unicode/ustring.h>
25 /** Cache of ParagraphLayout lines. */
26 Layouter::LineCache
*Layouter::linecache
;
28 /** Cache of Font instances. */
29 Layouter::FontColourMap
Layouter::fonts
[FS_END
];
33 * Construct a new font.
34 * @param size The font size to use for this font.
35 * @param colour The colour to draw this font in.
37 Font::Font(FontSize size
, TextColour colour
) :
38 fc(FontCache::Get(size
)), colour(colour
)
40 assert(size
< FS_END
);
44 /* Implementation details of LEFontInstance */
46 le_int32
Font::getUnitsPerEM() const
48 return this->fc
->GetUnitsPerEM();
51 le_int32
Font::getAscent() const
53 return this->fc
->GetAscender();
56 le_int32
Font::getDescent() const
58 return -this->fc
->GetDescender();
61 le_int32
Font::getLeading() const
63 return this->fc
->GetHeight();
66 float Font::getXPixelsPerEm() const
68 return (float)this->fc
->GetHeight();
71 float Font::getYPixelsPerEm() const
73 return (float)this->fc
->GetHeight();
76 float Font::getScaleFactorX() const
81 float Font::getScaleFactorY() const
86 const void *Font::getFontTable(LETag tableTag
) const
89 return this->getFontTable(tableTag
, length
);
92 const void *Font::getFontTable(LETag tableTag
, size_t &length
) const
94 return this->fc
->GetFontTable(tableTag
, length
);
97 LEGlyphID
Font::mapCharToGlyph(LEUnicode32 ch
) const
99 if (IsTextDirectionChar(ch
)) return 0;
100 return this->fc
->MapCharToGlyph(ch
);
103 void Font::getGlyphAdvance(LEGlyphID glyph
, LEPoint
&advance
) const
105 advance
.fX
= glyph
== 0xFFFF ? 0 : this->fc
->GetGlyphWidth(glyph
);
109 le_bool
Font::getGlyphPoint(LEGlyphID glyph
, le_int32 pointNumber
, LEPoint
&point
) const
114 static size_t AppendToBuffer(UChar
*buff
, const UChar
*buffer_last
, WChar c
)
116 /* Transform from UTF-32 to internal ICU format of UTF-16. */
118 UErrorCode err
= U_ZERO_ERROR
;
119 u_strFromUTF32(buff
, buffer_last
- buff
, &length
, (UChar32
*)&c
, 1, &err
);
124 * Wrapper for doing layouts with ICU.
126 class ICUParagraphLayout
: public AutoDeleteSmallVector
<ParagraphLayouter::Line
*, 4>, public ParagraphLayouter
{
127 ParagraphLayout
*p
; ///< The actual ICU paragraph layout.
129 /** Helper for GetLayouter, to get the right type. */
130 typedef UChar CharType
;
131 /** Helper for GetLayouter, to get whether the layouter supports RTL. */
132 static const bool SUPPORTS_RTL
= true;
134 /** Visual run contains data about the bit of text with the same font. */
135 class ICUVisualRun
: public ParagraphLayouter::VisualRun
{
136 const ParagraphLayout::VisualRun
*vr
; ///< The actual ICU vr.
139 ICUVisualRun(const ParagraphLayout::VisualRun
*vr
) : vr(vr
) { }
141 const Font
*GetFont() const { return (const Font
*)vr
->getFont(); }
142 int GetGlyphCount() const { return vr
->getGlyphCount(); }
143 const GlyphID
*GetGlyphs() const { return vr
->getGlyphs(); }
144 const float *GetPositions() const { return vr
->getPositions(); }
145 int GetLeading() const { return vr
->getLeading(); }
146 const int *GetGlyphToCharMap() const { return vr
->getGlyphToCharMap(); }
149 /** A single line worth of VisualRuns. */
150 class ICULine
: public AutoDeleteSmallVector
<ICUVisualRun
*, 4>, public ParagraphLayouter::Line
{
151 ParagraphLayout::Line
*l
; ///< The actual ICU line.
154 ICULine(ParagraphLayout::Line
*l
) : l(l
)
156 for (int i
= 0; i
< l
->countRuns(); i
++) {
157 *this->Append() = new ICUVisualRun(l
->getVisualRun(i
));
160 ~ICULine() { delete l
; }
162 int GetLeading() const { return l
->getLeading(); }
163 int GetWidth() const { return l
->getWidth(); }
164 int CountRuns() const { return l
->countRuns(); }
165 const ParagraphLayouter::VisualRun
*GetVisualRun(int run
) const { return *this->Get(run
); }
167 int GetInternalCharLength(WChar c
) const
169 /* ICU uses UTF-16 internally which means we need to account for surrogate pairs. */
170 return Utf8CharLen(c
) < 4 ? 1 : 2;
174 ICUParagraphLayout(ParagraphLayout
*p
) : p(p
) { }
175 ~ICUParagraphLayout() { delete p
; }
176 void Reflow() { p
->reflow(); }
178 ParagraphLayouter::Line
*NextLine(int max_width
)
180 ParagraphLayout::Line
*l
= p
->nextLine(max_width
);
181 return l
== NULL
? NULL
: new ICULine(l
);
185 static ParagraphLayouter
*GetParagraphLayout(UChar
*buff
, UChar
*buff_end
, FontMap
&fontMapping
)
187 int32 length
= buff_end
- buff
;
190 /* ICU's ParagraphLayout cannot handle empty strings, so fake one. */
193 fontMapping
.End()[-1].first
++;
196 /* Fill ICU's FontRuns with the right data. */
197 FontRuns
runs(fontMapping
.Length());
198 for (FontMap::iterator iter
= fontMapping
.Begin(); iter
!= fontMapping
.End(); iter
++) {
199 runs
.add(iter
->second
, iter
->first
);
202 LEErrorCode status
= LE_NO_ERROR
;
203 /* ParagraphLayout does not copy "buff", so it must stay valid.
204 * "runs" is copied according to the ICU source, but the documentation does not specify anything, so this might break somewhen. */
205 ParagraphLayout
*p
= new ParagraphLayout(buff
, length
, &runs
, NULL
, NULL
, NULL
, _current_text_dir
== TD_RTL
? UBIDI_DEFAULT_RTL
: UBIDI_DEFAULT_LTR
, false, status
);
206 if (status
!= LE_NO_ERROR
) {
211 return new ICUParagraphLayout(p
);
214 #endif /* WITH_ICU */
216 /*** Paragraph layout ***/
218 * Class handling the splitting of a paragraph of text into lines and
221 * One constructs this class with the text that needs to be split into
222 * lines. Then nextLine is called with the maximum width until NULL is
223 * returned. Each nextLine call creates VisualRuns which contain the
224 * length of text that are to be drawn with the same font. In other
225 * words, the result of this class is a list of sub strings with their
226 * font. The sub strings are then already fully laid out, and only
227 * need actual drawing.
229 * The positions in a visual run are sequential pairs of X,Y of the
230 * begin of each of the glyphs plus an extra pair to mark the end.
232 * @note This variant does not handle left-to-right properly. This
233 * is supported in the one ParagraphLayout coming from ICU.
235 class FallbackParagraphLayout
: public ParagraphLayouter
{
237 /** Helper for GetLayouter, to get the right type. */
238 typedef WChar CharType
;
239 /** Helper for GetLayouter, to get whether the layouter supports RTL. */
240 static const bool SUPPORTS_RTL
= false;
242 /** Visual run contains data about the bit of text with the same font. */
243 class FallbackVisualRun
: public ParagraphLayouter::VisualRun
{
244 Font
*font
; ///< The font used to layout these.
245 GlyphID
*glyphs
; ///< The glyphs we're drawing.
246 float *positions
; ///< The positions of the glyphs.
247 int *glyph_to_char
; ///< The char index of the glyphs.
248 int glyph_count
; ///< The number of glyphs.
251 FallbackVisualRun(Font
*font
, const WChar
*chars
, int glyph_count
, int x
);
252 ~FallbackVisualRun();
253 const Font
*GetFont() const;
254 int GetGlyphCount() const;
255 const GlyphID
*GetGlyphs() const;
256 const float *GetPositions() const;
257 int GetLeading() const;
258 const int *GetGlyphToCharMap() const;
261 /** A single line worth of VisualRuns. */
262 class FallbackLine
: public AutoDeleteSmallVector
<FallbackVisualRun
*, 4>, public ParagraphLayouter::Line
{
264 int GetLeading() const;
265 int GetWidth() const;
266 int CountRuns() const;
267 const ParagraphLayouter::VisualRun
*GetVisualRun(int run
) const;
269 int GetInternalCharLength(WChar c
) const { return 1; }
272 const WChar
*buffer_begin
; ///< Begin of the buffer.
273 const WChar
*buffer
; ///< The current location in the buffer.
274 FontMap
&runs
; ///< The fonts we have to use for this paragraph.
276 FallbackParagraphLayout(WChar
*buffer
, int length
, FontMap
&runs
);
278 const ParagraphLayouter::Line
*NextLine(int max_width
);
282 * Create the visual run.
283 * @param font The font to use for this run.
284 * @param chars The characters to use for this run.
285 * @param char_count The number of characters in this run.
286 * @param x The initial x position for this run.
288 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(Font
*font
, const WChar
*chars
, int char_count
, int x
) :
289 font(font
), glyph_count(char_count
)
291 this->glyphs
= MallocT
<GlyphID
>(this->glyph_count
);
292 this->glyph_to_char
= MallocT
<int>(this->glyph_count
);
294 /* Positions contains the location of the begin of each of the glyphs, and the end of the last one. */
295 this->positions
= MallocT
<float>(this->glyph_count
* 2 + 2);
296 this->positions
[0] = x
;
297 this->positions
[1] = 0;
299 for (int i
= 0; i
< this->glyph_count
; i
++) {
300 this->glyphs
[i
] = font
->fc
->MapCharToGlyph(chars
[i
]);
301 this->positions
[2 * i
+ 2] = this->positions
[2 * i
] + font
->fc
->GetGlyphWidth(this->glyphs
[i
]);
302 this->positions
[2 * i
+ 3] = 0;
303 this->glyph_to_char
[i
] = i
;
307 /** Free all data. */
308 FallbackParagraphLayout::FallbackVisualRun::~FallbackVisualRun()
310 free(this->positions
);
311 free(this->glyph_to_char
);
316 * Get the font associated with this run.
319 const Font
*FallbackParagraphLayout::FallbackVisualRun::GetFont() const
325 * Get the number of glyphs in this run.
326 * @return The number of glyphs.
328 int FallbackParagraphLayout::FallbackVisualRun::GetGlyphCount() const
330 return this->glyph_count
;
334 * Get the glyphs of this run.
335 * @return The glyphs.
337 const GlyphID
*FallbackParagraphLayout::FallbackVisualRun::GetGlyphs() const
343 * Get the positions of this run.
344 * @return The positions.
346 const float *FallbackParagraphLayout::FallbackVisualRun::GetPositions() const
348 return this->positions
;
352 * Get the glyph-to-character map for this visual run.
353 * @return The glyph-to-character map.
355 const int *FallbackParagraphLayout::FallbackVisualRun::GetGlyphToCharMap() const
357 return this->glyph_to_char
;
361 * Get the height of this font.
362 * @return The height of the font.
364 int FallbackParagraphLayout::FallbackVisualRun::GetLeading() const
366 return this->GetFont()->fc
->GetHeight();
370 * Get the height of the line.
371 * @return The maximum height of the line.
373 int FallbackParagraphLayout::FallbackLine::GetLeading() const
376 for (const FallbackVisualRun
* const *run
= this->Begin(); run
!= this->End(); run
++) {
377 leading
= max(leading
, (*run
)->GetLeading());
384 * Get the width of this line.
385 * @return The width of the line.
387 int FallbackParagraphLayout::FallbackLine::GetWidth() const
389 if (this->Length() == 0) return 0;
392 * The last X position of a run contains is the end of that run.
393 * Since there is no left-to-right support, taking this value of
394 * the last run gives us the end of the line and thus the width.
396 const ParagraphLayouter::VisualRun
*run
= this->GetVisualRun(this->CountRuns() - 1);
397 return (int)run
->GetPositions()[run
->GetGlyphCount() * 2];
401 * Get the number of runs in this line.
402 * @return The number of runs.
404 int FallbackParagraphLayout::FallbackLine::CountRuns() const
406 return this->Length();
410 * Get a specific visual run.
411 * @return The visual run.
413 const ParagraphLayouter::VisualRun
*FallbackParagraphLayout::FallbackLine::GetVisualRun(int run
) const
415 return *this->Get(run
);
419 * Create a new paragraph layouter.
420 * @param buffer The characters of the paragraph.
421 * @param length The length of the paragraph.
422 * @param runs The font mapping of this paragraph.
424 FallbackParagraphLayout::FallbackParagraphLayout(WChar
*buffer
, int length
, FontMap
&runs
) : buffer_begin(buffer
), buffer(buffer
), runs(runs
)
426 assert(runs
.End()[-1].first
== length
);
430 * Reset the position to the start of the paragraph.
432 void FallbackParagraphLayout::Reflow()
434 this->buffer
= this->buffer_begin
;
438 * Construct a new line with a maximum width.
439 * @param max_width The maximum width of the string.
440 * @return A Line, or NULL when at the end of the paragraph.
442 const ParagraphLayouter::Line
*FallbackParagraphLayout::NextLine(int max_width
)
445 * - split a line at a newline character, or at a space where we can break a line.
446 * - split for a visual run whenever a new line happens, or the font changes.
448 if (this->buffer
== NULL
) return NULL
;
450 FallbackLine
*l
= new FallbackLine();
452 if (*this->buffer
== '\0') {
453 /* Only a newline. */
455 *l
->Append() = new FallbackVisualRun(this->runs
.Begin()->second
, this->buffer
, 0, 0);
459 const WChar
*begin
= this->buffer
;
460 const WChar
*last_space
= NULL
;
461 const WChar
*last_char
= begin
;
464 int offset
= this->buffer
- this->buffer_begin
;
465 FontMap::iterator iter
= this->runs
.Begin();
466 while (iter
->first
<= offset
) {
468 assert(iter
!= this->runs
.End());
471 const FontCache
*fc
= iter
->second
->fc
;
472 const WChar
*next_run
= this->buffer_begin
+ iter
->first
;
475 WChar c
= *this->buffer
;
476 last_char
= this->buffer
;
483 if (this->buffer
== next_run
) {
484 int w
= l
->GetWidth();
485 *l
->Append() = new FallbackVisualRun(iter
->second
, begin
, this->buffer
- begin
, w
);
487 assert(iter
!= this->runs
.End());
489 next_run
= this->buffer_begin
+ iter
->first
;
490 begin
= this->buffer
;
495 if (IsWhitespace(c
)) last_space
= this->buffer
;
497 if (IsPrintable(c
) && !IsTextDirectionChar(c
)) {
498 int char_width
= GetCharacterWidth(fc
->GetSize(), c
);
500 if (width
> max_width
) {
501 /* The string is longer than maximum width so we need to decide
502 * what to do with it. */
503 if (width
== char_width
) {
504 /* The character is wider than allowed width; don't know
505 * what to do with this case... bail out! */
510 if (last_space
== NULL
) {
511 /* No space has been found. Just terminate at our current
512 * location. This usually happens for languages that do not
513 * require spaces in strings, like Chinese, Japanese and
514 * Korean. For other languages terminating mid-word might
515 * not be the best, but terminating the whole string instead
516 * of continuing the word at the next line is worse. */
517 last_char
= this->buffer
;
519 /* A space is found; perfect place to terminate */
520 this->buffer
= last_space
+ 1;
521 last_char
= last_space
;
530 if (l
->Length() == 0 || last_char
- begin
!= 0) {
531 int w
= l
->GetWidth();
532 *l
->Append() = new FallbackVisualRun(iter
->second
, begin
, last_char
- begin
, w
);
538 * Appand a wide character to the internal buffer.
539 * @param buff The buffer to append to.
540 * @param buffer_last The end of the buffer.
541 * @param c The character to add.
542 * @return The number of buffer spaces that were used.
544 static size_t AppendToBuffer(WChar
*buff
, const WChar
*buffer_last
, WChar c
)
551 * Get the actual ParagraphLayout for the given buffer.
552 * @param buff The begin of the buffer.
553 * @param buff_end The location after the last element in the buffer.
554 * @param fontMapping THe mapping of the fonts.
555 * @return The ParagraphLayout instance.
557 static FallbackParagraphLayout
*GetParagraphLayout(WChar
*buff
, WChar
*buff_end
, FontMap
&fontMapping
)
559 return new FallbackParagraphLayout(buff
, buff_end
- buff
, fontMapping
);
563 * Helper for getting a ParagraphLayouter of the given type.
565 * @note In case no ParagraphLayouter could be constructed, line.layout will be NULL.
566 * @param line The cache item to store our layouter in.
567 * @param str The string to create a layouter for.
568 * @param state The state of the font and color.
569 * @tparam T The type of layouter we want.
571 template <typename T
>
572 static inline void GetLayouter(Layouter::LineCacheItem
&line
, const char *&str
, FontState
&state
)
574 if (line
.buffer
!= NULL
) free(line
.buffer
);
576 typename
T::CharType
*buff_begin
= MallocT
<typename
T::CharType
>(DRAW_STRING_BUFFER
);
577 const typename
T::CharType
*buffer_last
= buff_begin
+ DRAW_STRING_BUFFER
;
578 typename
T::CharType
*buff
= buff_begin
;
579 FontMap
&fontMapping
= line
.runs
;
580 Font
*f
= Layouter::GetFont(state
.fontsize
, state
.cur_colour
);
582 line
.buffer
= buff_begin
;
585 * Go through the whole string while adding Font instances to the font map
586 * whenever the font changes, and convert the wide characters into a format
587 * usable by ParagraphLayout.
589 for (; buff
< buffer_last
;) {
590 WChar c
= Utf8Consume(const_cast<const char **>(&str
));
591 if (c
== '\0' || c
== '\n') {
593 } else if (c
>= SCC_BLUE
&& c
<= SCC_BLACK
) {
594 state
.SetColour((TextColour
)(c
- SCC_BLUE
));
595 } else if (c
== SCC_PREVIOUS_COLOUR
) { // Revert to the previous colour.
596 state
.SetPreviousColour();
597 } else if (c
== SCC_TINYFONT
) {
598 state
.SetFontSize(FS_SMALL
);
599 } else if (c
== SCC_BIGFONT
) {
600 state
.SetFontSize(FS_LARGE
);
602 /* Filter out text direction characters that shouldn't be drawn, and
603 * will not be handled in the fallback non ICU case because they are
604 * mostly needed for RTL languages which need more ICU support. */
605 if (!T::SUPPORTS_RTL
&& IsTextDirectionChar(c
)) continue;
606 buff
+= AppendToBuffer(buff
, buffer_last
, c
);
610 if (!fontMapping
.Contains(buff
- buff_begin
)) {
611 fontMapping
.Insert(buff
- buff_begin
, f
);
613 f
= Layouter::GetFont(state
.fontsize
, state
.cur_colour
);
616 /* Better safe than sorry. */
619 if (!fontMapping
.Contains(buff
- buff_begin
)) {
620 fontMapping
.Insert(buff
- buff_begin
, f
);
622 line
.layout
= GetParagraphLayout(buff_begin
, buff
, fontMapping
);
623 line
.state_after
= state
;
627 * Create a new layouter.
628 * @param str The string to create the layout for.
629 * @param maxw The maximum width.
630 * @param colour The colour of the font.
631 * @param fontsize The size of font to use.
633 Layouter::Layouter(const char *str
, int maxw
, TextColour colour
, FontSize fontsize
) : string(str
)
635 FontState
state(colour
, fontsize
);
639 /* Scan string for end of line */
640 const char *lineend
= str
;
642 size_t len
= Utf8Decode(&c
, lineend
);
643 if (c
== '\0' || c
== '\n') break;
647 LineCacheItem
& line
= GetCachedParagraphLayout(str
, lineend
- str
, state
);
648 if (line
.layout
!= NULL
) {
649 /* Line is in cache */
651 state
= line
.state_after
;
652 line
.layout
->Reflow();
654 /* Line is new, layout it */
656 FontState old_state
= state
;
657 const char *old_str
= str
;
659 GetLayouter
<ICUParagraphLayout
>(line
, str
, state
);
660 if (line
.layout
== NULL
) {
661 static bool warned
= false;
663 DEBUG(misc
, 0, "ICU layouter bailed on the font. Falling back to the fallback layouter");
669 GetLayouter
<FallbackParagraphLayout
>(line
, str
, state
);
672 GetLayouter
<FallbackParagraphLayout
>(line
, str
, state
);
676 /* Copy all lines into a local cache so we can reuse them later on more easily. */
677 const ParagraphLayouter::Line
*l
;
678 while ((l
= line
.layout
->NextLine(maxw
)) != NULL
) {
686 * Get the boundaries of this paragraph.
687 * @return The boundaries.
689 Dimension
Layouter::GetBounds()
691 Dimension d
= { 0, 0 };
692 for (const ParagraphLayouter::Line
**l
= this->Begin(); l
!= this->End(); l
++) {
693 d
.width
= max
<uint
>(d
.width
, (*l
)->GetWidth());
694 d
.height
+= (*l
)->GetLeading();
700 * Get the position of a character in the layout.
701 * @param ch Character to get the position of.
702 * @return Upper left corner of the character relative to the start of the string.
703 * @note Will only work right for single-line strings.
705 Point
Layouter::GetCharPosition(const char *ch
) const
707 /* Find the code point index which corresponds to the char
708 * pointer into our UTF-8 source string. */
710 const char *str
= this->string
;
713 size_t len
= Utf8Decode(&c
, str
);
714 if (c
== '\0' || c
== '\n') break;
716 index
+= (*this->Begin())->GetInternalCharLength(c
);
720 /* Valid character. */
721 const ParagraphLayouter::Line
*line
= *this->Begin();
723 /* Pointer to the end-of-string/line marker? Return total line width. */
724 if (*ch
== '\0' || *ch
== '\n') {
725 Point p
= { line
->GetWidth(), 0 };
729 /* Scan all runs until we've found our code point index. */
730 for (int run_index
= 0; run_index
< line
->CountRuns(); run_index
++) {
731 const ParagraphLayouter::VisualRun
*run
= line
->GetVisualRun(run_index
);
733 for (int i
= 0; i
< run
->GetGlyphCount(); i
++) {
734 /* Matching glyph? Return position. */
735 if ((size_t)run
->GetGlyphToCharMap()[i
] == index
) {
736 Point p
= { (int)run
->GetPositions()[i
* 2], (int)run
->GetPositions()[i
* 2 + 1] };
748 * Get the character that is at a position.
749 * @param x Position in the string.
750 * @return Pointer to the character at the position or NULL if no character is at the position.
752 const char *Layouter::GetCharAtPosition(int x
) const
754 const ParagraphLayouter::Line
*line
= *this->Begin();
756 for (int run_index
= 0; run_index
< line
->CountRuns(); run_index
++) {
757 const ParagraphLayouter::VisualRun
*run
= line
->GetVisualRun(run_index
);
759 for (int i
= 0; i
< run
->GetGlyphCount(); i
++) {
760 /* Not a valid glyph (empty). */
761 if (run
->GetGlyphs()[i
] == 0xFFFF) continue;
763 int begin_x
= (int)run
->GetPositions()[i
* 2];
764 int end_x
= (int)run
->GetPositions()[i
* 2 + 2];
766 if (IsInsideMM(x
, begin_x
, end_x
)) {
767 /* Found our glyph, now convert to UTF-8 string index. */
768 size_t index
= run
->GetGlyphToCharMap()[i
];
771 for (const char *str
= this->string
; *str
!= '\0'; ) {
772 if (cur_idx
== index
) return str
;
774 WChar c
= Utf8Consume(&str
);
775 cur_idx
+= line
->GetInternalCharLength(c
);
785 * Get a static font instance.
787 Font
*Layouter::GetFont(FontSize size
, TextColour colour
)
789 FontColourMap::iterator it
= fonts
[size
].Find(colour
);
790 if (it
!= fonts
[size
].End()) return it
->second
;
792 Font
*f
= new Font(size
, colour
);
793 *fonts
[size
].Append() = FontColourMap::Pair(colour
, f
);
798 * Reset cached font information.
799 * @param size Font size to reset.
801 void Layouter::ResetFontCache(FontSize size
)
803 for (FontColourMap::iterator it
= fonts
[size
].Begin(); it
!= fonts
[size
].End(); ++it
) {
808 /* We must reset the linecache since it references the just freed fonts */
813 * Get reference to cache item.
814 * If the item does not exist yet, it is default constructed.
815 * @param str Source string of the line (including colour and font size codes).
816 * @param len Length of \a str in bytes (no termination).
817 * @param state State of the font at the beginning of the line.
818 * @return Reference to cache item.
820 Layouter::LineCacheItem
&Layouter::GetCachedParagraphLayout(const char *str
, size_t len
, const FontState
&state
)
822 if (linecache
== NULL
) {
823 /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
824 linecache
= new LineCache();
828 key
.state_before
= state
;
829 key
.str
.assign(str
, len
);
830 return (*linecache
)[key
];
836 void Layouter::ResetLineCache()
838 if (linecache
!= NULL
) linecache
->clear();
842 * Reduce the size of linecache if necessary to prevent infinite growth.
844 void Layouter::ReduceLineCache()
846 if (linecache
!= NULL
) {
847 /* TODO LRU cache would be fancy, but not exactly necessary */
848 if (linecache
->size() > 4096) ResetLineCache();