1 /* $Id: gfx_layout.cpp 26037 2013-11-18 19:47:43Z rubidium $ */
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"
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
];
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
83 float Font::getScaleFactorY() const
88 const void *Font::getFontTable(LETag tableTag
) const
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
);
111 le_bool
Font::getGlyphPoint(LEGlyphID glyph
, le_int32 pointNumber
, LEPoint
&point
) const
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. */
120 UErrorCode err
= U_ZERO_ERROR
;
121 u_strFromUTF32(buff
, buffer_last
- buff
, &length
, (UChar32
*)&c
, 1, &err
);
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.
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.
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.
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
;
192 /* ICU's ParagraphLayout cannot handle empty strings, so fake one. */
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
) {
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
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
{
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.
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
{
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
);
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
);
318 * Get the font associated with this run.
321 const Font
*FallbackParagraphLayout::FallbackVisualRun::GetFont() const
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
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
378 for (const FallbackVisualRun
* const *run
= this->Begin(); run
!= this->End(); run
++) {
379 leading
= max(leading
, (*run
)->GetLeading());
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
)
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. */
457 *l
->Append() = new FallbackVisualRun(this->runs
.Begin()->second
, this->buffer
, 0, 0);
461 const WChar
*begin
= this->buffer
;
462 const WChar
*last_space
= NULL
;
463 const WChar
*last_char
= begin
;
466 int offset
= this->buffer
- this->buffer_begin
;
467 FontMap::iterator iter
= this->runs
.Begin();
468 while (iter
->first
<= offset
) {
470 assert(iter
!= this->runs
.End());
473 const FontCache
*fc
= iter
->second
->fc
;
474 const WChar
*next_run
= this->buffer_begin
+ iter
->first
;
477 WChar c
= *this->buffer
;
478 last_char
= this->buffer
;
485 if (this->buffer
== next_run
) {
486 int w
= l
->GetWidth();
487 *l
->Append() = new FallbackVisualRun(iter
->second
, begin
, this->buffer
- begin
, w
);
489 assert(iter
!= this->runs
.End());
491 next_run
= this->buffer_begin
+ iter
->first
;
492 begin
= this->buffer
;
497 if (IsWhitespace(c
)) last_space
= this->buffer
;
499 if (IsPrintable(c
) && !IsTextDirectionChar(c
)) {
500 int char_width
= GetCharacterWidth(fc
->GetSize(), c
);
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! */
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
;
521 /* A space is found; perfect place to terminate */
522 this->buffer
= last_space
+ 1;
523 last_char
= last_space
;
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
);
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
)
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') {
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
);
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
);
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. */
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
);
641 /* Scan string for end of line */
642 const char *lineend
= str
;
644 size_t len
= Utf8Decode(&c
, lineend
);
645 if (c
== '\0' || c
== '\n') break;
649 LineCacheItem
& line
= GetCachedParagraphLayout(str
, lineend
- str
, state
);
650 if (line
.layout
!= NULL
) {
651 /* Line is in cache */
653 state
= line
.state_after
;
654 line
.layout
->Reflow();
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;
665 DEBUG(misc
, 0, "ICU layouter bailed on the font. Falling back to the fallback layouter");
671 GetLayouter
<FallbackParagraphLayout
>(line
, str
, state
);
674 GetLayouter
<FallbackParagraphLayout
>(line
, str
, state
);
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
) {
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();
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. */
712 const char *str
= this->string
;
715 size_t len
= Utf8Decode(&c
, str
);
716 if (c
== '\0' || c
== '\n') break;
718 index
+= (*this->Begin())->GetInternalCharLength(c
);
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 };
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] };
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
];
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
);
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
);
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
) {
810 /* We must reset the linecache since it references the just freed fonts */
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();
830 key
.state_before
= state
;
831 key
.str
.assign(str
, len
);
832 return (*linecache
)[key
];
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();