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 */
25 #include "os/windows/string_uniscribe.h"
26 #endif /* WITH_UNISCRIBE */
29 #include "os/macosx/string_osx.h"
32 #include "safeguards.h"
35 /** Cache of ParagraphLayout lines. */
36 Layouter::LineCache
*Layouter::linecache
;
38 /** Cache of Font instances. */
39 Layouter::FontColourMap
Layouter::fonts
[FS_END
];
43 * Construct a new font.
44 * @param size The font size to use for this font.
45 * @param colour The colour to draw this font in.
47 Font::Font(FontSize size
, TextColour colour
) :
48 fc(FontCache::Get(size
)), colour(colour
)
50 assert(size
< FS_END
);
53 #ifdef WITH_ICU_LAYOUT
54 /* Implementation details of LEFontInstance */
56 le_int32
Font::getUnitsPerEM() const
58 return this->fc
->GetUnitsPerEM();
61 le_int32
Font::getAscent() const
63 return this->fc
->GetAscender();
66 le_int32
Font::getDescent() const
68 return -this->fc
->GetDescender();
71 le_int32
Font::getLeading() const
73 return this->fc
->GetHeight();
76 float Font::getXPixelsPerEm() const
78 return (float)this->fc
->GetHeight();
81 float Font::getYPixelsPerEm() const
83 return (float)this->fc
->GetHeight();
86 float Font::getScaleFactorX() const
91 float Font::getScaleFactorY() const
96 const void *Font::getFontTable(LETag tableTag
) const
99 return this->getFontTable(tableTag
, length
);
102 const void *Font::getFontTable(LETag tableTag
, size_t &length
) const
104 return this->fc
->GetFontTable(tableTag
, length
);
107 LEGlyphID
Font::mapCharToGlyph(LEUnicode32 ch
) const
109 if (IsTextDirectionChar(ch
)) return 0;
110 return this->fc
->MapCharToGlyph(ch
);
113 void Font::getGlyphAdvance(LEGlyphID glyph
, LEPoint
&advance
) const
115 advance
.fX
= glyph
== 0xFFFF ? 0 : this->fc
->GetGlyphWidth(glyph
);
119 le_bool
Font::getGlyphPoint(LEGlyphID glyph
, le_int32 pointNumber
, LEPoint
&point
) const
125 * Wrapper for doing layouts with ICU.
127 class ICUParagraphLayout
: public AutoDeleteSmallVector
<ParagraphLayouter::Line
*, 4>, public ParagraphLayouter
{
128 icu::ParagraphLayout
*p
; ///< The actual ICU paragraph layout.
130 /** Visual run contains data about the bit of text with the same font. */
131 class ICUVisualRun
: public ParagraphLayouter::VisualRun
{
132 const icu::ParagraphLayout::VisualRun
*vr
; ///< The actual ICU vr.
135 ICUVisualRun(const icu::ParagraphLayout::VisualRun
*vr
) : vr(vr
) { }
137 const Font
*GetFont() const { return (const Font
*)vr
->getFont(); }
138 int GetGlyphCount() const { return vr
->getGlyphCount(); }
139 const GlyphID
*GetGlyphs() const { return vr
->getGlyphs(); }
140 const float *GetPositions() const { return vr
->getPositions(); }
141 int GetLeading() const { return vr
->getLeading(); }
142 const int *GetGlyphToCharMap() const { return vr
->getGlyphToCharMap(); }
145 /** A single line worth of VisualRuns. */
146 class ICULine
: public AutoDeleteSmallVector
<ICUVisualRun
*, 4>, public ParagraphLayouter::Line
{
147 icu::ParagraphLayout::Line
*l
; ///< The actual ICU line.
150 ICULine(icu::ParagraphLayout::Line
*l
) : l(l
)
152 for (int i
= 0; i
< l
->countRuns(); i
++) {
153 *this->Append() = new ICUVisualRun(l
->getVisualRun(i
));
156 ~ICULine() { delete l
; }
158 int GetLeading() const { return l
->getLeading(); }
159 int GetWidth() const { return l
->getWidth(); }
160 int CountRuns() const { return l
->countRuns(); }
161 const ParagraphLayouter::VisualRun
*GetVisualRun(int run
) const { return *this->Get(run
); }
163 int GetInternalCharLength(WChar c
) const
165 /* ICU uses UTF-16 internally which means we need to account for surrogate pairs. */
166 return Utf8CharLen(c
) < 4 ? 1 : 2;
170 ICUParagraphLayout(icu::ParagraphLayout
*p
) : p(p
) { }
171 ~ICUParagraphLayout() { delete p
; }
172 void Reflow() { p
->reflow(); }
174 ParagraphLayouter::Line
*NextLine(int max_width
)
176 icu::ParagraphLayout::Line
*l
= p
->nextLine(max_width
);
177 return l
== NULL
? NULL
: new ICULine(l
);
182 * Helper class to construct a new #ICUParagraphLayout.
184 class ICUParagraphLayoutFactory
{
186 /** Helper for GetLayouter, to get the right type. */
187 typedef UChar CharType
;
188 /** Helper for GetLayouter, to get whether the layouter supports RTL. */
189 static const bool SUPPORTS_RTL
= true;
191 static ParagraphLayouter
*GetParagraphLayout(UChar
*buff
, UChar
*buff_end
, FontMap
&fontMapping
)
193 int32 length
= buff_end
- buff
;
196 /* ICU's ParagraphLayout cannot handle empty strings, so fake one. */
199 fontMapping
.End()[-1].first
++;
202 /* Fill ICU's FontRuns with the right data. */
203 icu::FontRuns
runs(fontMapping
.Length());
204 for (FontMap::iterator iter
= fontMapping
.Begin(); iter
!= fontMapping
.End(); iter
++) {
205 runs
.add(iter
->second
, iter
->first
);
208 LEErrorCode status
= LE_NO_ERROR
;
209 /* ParagraphLayout does not copy "buff", so it must stay valid.
210 * "runs" is copied according to the ICU source, but the documentation does not specify anything, so this might break somewhen. */
211 icu::ParagraphLayout
*p
= new icu::ParagraphLayout(buff
, length
, &runs
, NULL
, NULL
, NULL
, _current_text_dir
== TD_RTL
? UBIDI_DEFAULT_RTL
: UBIDI_DEFAULT_LTR
, false, status
);
212 if (status
!= LE_NO_ERROR
) {
217 return new ICUParagraphLayout(p
);
220 static size_t AppendToBuffer(UChar
*buff
, const UChar
*buffer_last
, WChar c
)
222 /* Transform from UTF-32 to internal ICU format of UTF-16. */
224 UErrorCode err
= U_ZERO_ERROR
;
225 u_strFromUTF32(buff
, buffer_last
- buff
, &length
, (UChar32
*)&c
, 1, &err
);
229 #endif /* WITH_ICU_LAYOUT */
231 /*** Paragraph layout ***/
233 * Class handling the splitting of a paragraph of text into lines and
236 * One constructs this class with the text that needs to be split into
237 * lines. Then nextLine is called with the maximum width until NULL is
238 * returned. Each nextLine call creates VisualRuns which contain the
239 * length of text that are to be drawn with the same font. In other
240 * words, the result of this class is a list of sub strings with their
241 * font. The sub strings are then already fully laid out, and only
242 * need actual drawing.
244 * The positions in a visual run are sequential pairs of X,Y of the
245 * begin of each of the glyphs plus an extra pair to mark the end.
247 * @note This variant does not handle left-to-right properly. This
248 * is supported in the one ParagraphLayout coming from ICU.
250 class FallbackParagraphLayout
: public ParagraphLayouter
{
252 /** Visual run contains data about the bit of text with the same font. */
253 class FallbackVisualRun
: public ParagraphLayouter::VisualRun
{
254 Font
*font
; ///< The font used to layout these.
255 GlyphID
*glyphs
; ///< The glyphs we're drawing.
256 float *positions
; ///< The positions of the glyphs.
257 int *glyph_to_char
; ///< The char index of the glyphs.
258 int glyph_count
; ///< The number of glyphs.
261 FallbackVisualRun(Font
*font
, const WChar
*chars
, int glyph_count
, int x
);
262 ~FallbackVisualRun();
263 const Font
*GetFont() const;
264 int GetGlyphCount() const;
265 const GlyphID
*GetGlyphs() const;
266 const float *GetPositions() const;
267 int GetLeading() const;
268 const int *GetGlyphToCharMap() const;
271 /** A single line worth of VisualRuns. */
272 class FallbackLine
: public AutoDeleteSmallVector
<FallbackVisualRun
*, 4>, public ParagraphLayouter::Line
{
274 int GetLeading() const;
275 int GetWidth() const;
276 int CountRuns() const;
277 const ParagraphLayouter::VisualRun
*GetVisualRun(int run
) const;
279 int GetInternalCharLength(WChar c
) const { return 1; }
282 const WChar
*buffer_begin
; ///< Begin of the buffer.
283 const WChar
*buffer
; ///< The current location in the buffer.
284 FontMap
&runs
; ///< The fonts we have to use for this paragraph.
286 FallbackParagraphLayout(WChar
*buffer
, int length
, FontMap
&runs
);
288 const ParagraphLayouter::Line
*NextLine(int max_width
);
292 * Helper class to construct a new #FallbackParagraphLayout.
294 class FallbackParagraphLayoutFactory
{
296 /** Helper for GetLayouter, to get the right type. */
297 typedef WChar CharType
;
298 /** Helper for GetLayouter, to get whether the layouter supports RTL. */
299 static const bool SUPPORTS_RTL
= false;
302 * Get the actual ParagraphLayout for the given buffer.
303 * @param buff The begin of the buffer.
304 * @param buff_end The location after the last element in the buffer.
305 * @param fontMapping THe mapping of the fonts.
306 * @return The ParagraphLayout instance.
308 static ParagraphLayouter
*GetParagraphLayout(WChar
*buff
, WChar
*buff_end
, FontMap
&fontMapping
)
310 return new FallbackParagraphLayout(buff
, buff_end
- buff
, fontMapping
);
314 * Append a wide character to the internal buffer.
315 * @param buff The buffer to append to.
316 * @param buffer_last The end of the buffer.
317 * @param c The character to add.
318 * @return The number of buffer spaces that were used.
320 static size_t AppendToBuffer(WChar
*buff
, const WChar
*buffer_last
, WChar c
)
328 * Create the visual run.
329 * @param font The font to use for this run.
330 * @param chars The characters to use for this run.
331 * @param char_count The number of characters in this run.
332 * @param x The initial x position for this run.
334 FallbackParagraphLayout::FallbackVisualRun::FallbackVisualRun(Font
*font
, const WChar
*chars
, int char_count
, int x
) :
335 font(font
), glyph_count(char_count
)
337 this->glyphs
= MallocT
<GlyphID
>(this->glyph_count
);
338 this->glyph_to_char
= MallocT
<int>(this->glyph_count
);
340 /* Positions contains the location of the begin of each of the glyphs, and the end of the last one. */
341 this->positions
= MallocT
<float>(this->glyph_count
* 2 + 2);
342 this->positions
[0] = x
;
343 this->positions
[1] = 0;
345 for (int i
= 0; i
< this->glyph_count
; i
++) {
346 this->glyphs
[i
] = font
->fc
->MapCharToGlyph(chars
[i
]);
347 this->positions
[2 * i
+ 2] = this->positions
[2 * i
] + font
->fc
->GetGlyphWidth(this->glyphs
[i
]);
348 this->positions
[2 * i
+ 3] = 0;
349 this->glyph_to_char
[i
] = i
;
353 /** Free all data. */
354 FallbackParagraphLayout::FallbackVisualRun::~FallbackVisualRun()
356 free(this->positions
);
357 free(this->glyph_to_char
);
362 * Get the font associated with this run.
365 const Font
*FallbackParagraphLayout::FallbackVisualRun::GetFont() const
371 * Get the number of glyphs in this run.
372 * @return The number of glyphs.
374 int FallbackParagraphLayout::FallbackVisualRun::GetGlyphCount() const
376 return this->glyph_count
;
380 * Get the glyphs of this run.
381 * @return The glyphs.
383 const GlyphID
*FallbackParagraphLayout::FallbackVisualRun::GetGlyphs() const
389 * Get the positions of this run.
390 * @return The positions.
392 const float *FallbackParagraphLayout::FallbackVisualRun::GetPositions() const
394 return this->positions
;
398 * Get the glyph-to-character map for this visual run.
399 * @return The glyph-to-character map.
401 const int *FallbackParagraphLayout::FallbackVisualRun::GetGlyphToCharMap() const
403 return this->glyph_to_char
;
407 * Get the height of this font.
408 * @return The height of the font.
410 int FallbackParagraphLayout::FallbackVisualRun::GetLeading() const
412 return this->GetFont()->fc
->GetHeight();
416 * Get the height of the line.
417 * @return The maximum height of the line.
419 int FallbackParagraphLayout::FallbackLine::GetLeading() const
422 for (const FallbackVisualRun
* const *run
= this->Begin(); run
!= this->End(); run
++) {
423 leading
= max(leading
, (*run
)->GetLeading());
430 * Get the width of this line.
431 * @return The width of the line.
433 int FallbackParagraphLayout::FallbackLine::GetWidth() const
435 if (this->Length() == 0) return 0;
438 * The last X position of a run contains is the end of that run.
439 * Since there is no left-to-right support, taking this value of
440 * the last run gives us the end of the line and thus the width.
442 const ParagraphLayouter::VisualRun
*run
= this->GetVisualRun(this->CountRuns() - 1);
443 return (int)run
->GetPositions()[run
->GetGlyphCount() * 2];
447 * Get the number of runs in this line.
448 * @return The number of runs.
450 int FallbackParagraphLayout::FallbackLine::CountRuns() const
452 return this->Length();
456 * Get a specific visual run.
457 * @return The visual run.
459 const ParagraphLayouter::VisualRun
*FallbackParagraphLayout::FallbackLine::GetVisualRun(int run
) const
461 return *this->Get(run
);
465 * Create a new paragraph layouter.
466 * @param buffer The characters of the paragraph.
467 * @param length The length of the paragraph.
468 * @param runs The font mapping of this paragraph.
470 FallbackParagraphLayout::FallbackParagraphLayout(WChar
*buffer
, int length
, FontMap
&runs
) : buffer_begin(buffer
), buffer(buffer
), runs(runs
)
472 assert(runs
.End()[-1].first
== length
);
476 * Reset the position to the start of the paragraph.
478 void FallbackParagraphLayout::Reflow()
480 this->buffer
= this->buffer_begin
;
484 * Construct a new line with a maximum width.
485 * @param max_width The maximum width of the string.
486 * @return A Line, or NULL when at the end of the paragraph.
488 const ParagraphLayouter::Line
*FallbackParagraphLayout::NextLine(int max_width
)
491 * - split a line at a newline character, or at a space where we can break a line.
492 * - split for a visual run whenever a new line happens, or the font changes.
494 if (this->buffer
== NULL
) return NULL
;
496 FallbackLine
*l
= new FallbackLine();
498 if (*this->buffer
== '\0') {
499 /* Only a newline. */
501 *l
->Append() = new FallbackVisualRun(this->runs
.Begin()->second
, this->buffer
, 0, 0);
505 int offset
= this->buffer
- this->buffer_begin
;
506 FontMap::iterator iter
= this->runs
.Begin();
507 while (iter
->first
<= offset
) {
509 assert(iter
!= this->runs
.End());
512 const FontCache
*fc
= iter
->second
->fc
;
513 const WChar
*next_run
= this->buffer_begin
+ iter
->first
;
515 const WChar
*begin
= this->buffer
;
516 const WChar
*last_space
= NULL
;
517 const WChar
*last_char
;
520 WChar c
= *this->buffer
;
521 last_char
= this->buffer
;
528 if (this->buffer
== next_run
) {
529 int w
= l
->GetWidth();
530 *l
->Append() = new FallbackVisualRun(iter
->second
, begin
, this->buffer
- begin
, w
);
532 assert(iter
!= this->runs
.End());
534 next_run
= this->buffer_begin
+ iter
->first
;
535 begin
= this->buffer
;
540 if (IsWhitespace(c
)) last_space
= this->buffer
;
542 if (IsPrintable(c
) && !IsTextDirectionChar(c
)) {
543 int char_width
= GetCharacterWidth(fc
->GetSize(), c
);
545 if (width
> max_width
) {
546 /* The string is longer than maximum width so we need to decide
547 * what to do with it. */
548 if (width
== char_width
) {
549 /* The character is wider than allowed width; don't know
550 * what to do with this case... bail out! */
555 if (last_space
== NULL
) {
556 /* No space has been found. Just terminate at our current
557 * location. This usually happens for languages that do not
558 * require spaces in strings, like Chinese, Japanese and
559 * Korean. For other languages terminating mid-word might
560 * not be the best, but terminating the whole string instead
561 * of continuing the word at the next line is worse. */
562 last_char
= this->buffer
;
564 /* A space is found; perfect place to terminate */
565 this->buffer
= last_space
+ 1;
566 last_char
= last_space
;
575 if (l
->Length() == 0 || last_char
- begin
!= 0) {
576 int w
= l
->GetWidth();
577 *l
->Append() = new FallbackVisualRun(iter
->second
, begin
, last_char
- begin
, w
);
583 * Helper for getting a ParagraphLayouter of the given type.
585 * @note In case no ParagraphLayouter could be constructed, line.layout will be NULL.
586 * @param line The cache item to store our layouter in.
587 * @param str The string to create a layouter for.
588 * @param state The state of the font and color.
589 * @tparam T The type of layouter we want.
591 template <typename T
>
592 static inline void GetLayouter(Layouter::LineCacheItem
&line
, const char *&str
, FontState
&state
)
594 if (line
.buffer
!= NULL
) free(line
.buffer
);
596 typename
T::CharType
*buff_begin
= MallocT
<typename
T::CharType
>(DRAW_STRING_BUFFER
);
597 const typename
T::CharType
*buffer_last
= buff_begin
+ DRAW_STRING_BUFFER
;
598 typename
T::CharType
*buff
= buff_begin
;
599 FontMap
&fontMapping
= line
.runs
;
600 Font
*f
= Layouter::GetFont(state
.fontsize
, state
.cur_colour
);
602 line
.buffer
= buff_begin
;
606 * Go through the whole string while adding Font instances to the font map
607 * whenever the font changes, and convert the wide characters into a format
608 * usable by ParagraphLayout.
610 for (; buff
< buffer_last
;) {
611 WChar c
= Utf8Consume(const_cast<const char **>(&str
));
612 if (c
== '\0' || c
== '\n') {
614 } else if (c
>= SCC_BLUE
&& c
<= SCC_BLACK
) {
615 state
.SetColour((TextColour
)(c
- SCC_BLUE
));
616 } else if (c
== SCC_PUSH_COLOUR
) {
618 } else if (c
== SCC_POP_COLOUR
) {
620 } else if (c
>= SCC_FIRST_FONT
&& c
<= SCC_LAST_FONT
) {
621 state
.SetFontSize((FontSize
)(c
- SCC_FIRST_FONT
));
623 /* Filter out text direction characters that shouldn't be drawn, and
624 * will not be handled in the fallback non ICU case because they are
625 * mostly needed for RTL languages which need more ICU support. */
626 if (!T::SUPPORTS_RTL
&& IsTextDirectionChar(c
)) continue;
627 buff
+= T::AppendToBuffer(buff
, buffer_last
, c
);
631 if (!fontMapping
.Contains(buff
- buff_begin
)) {
632 fontMapping
.Insert(buff
- buff_begin
, f
);
634 f
= Layouter::GetFont(state
.fontsize
, state
.cur_colour
);
637 /* Better safe than sorry. */
640 if (!fontMapping
.Contains(buff
- buff_begin
)) {
641 fontMapping
.Insert(buff
- buff_begin
, f
);
643 line
.layout
= T::GetParagraphLayout(buff_begin
, buff
, fontMapping
);
644 line
.state_after
= state
;
648 * Create a new layouter.
649 * @param str The string to create the layout for.
650 * @param maxw The maximum width.
651 * @param colour The colour of the font.
652 * @param fontsize The size of font to use.
654 Layouter::Layouter(const char *str
, int maxw
, TextColour colour
, FontSize fontsize
) : string(str
)
656 FontState
state(colour
, fontsize
);
660 /* Scan string for end of line */
661 const char *lineend
= str
;
663 size_t len
= Utf8Decode(&c
, lineend
);
664 if (c
== '\0' || c
== '\n') break;
668 LineCacheItem
& line
= GetCachedParagraphLayout(str
, lineend
- str
, state
);
669 if (line
.layout
!= NULL
) {
670 /* Line is in cache */
672 state
= line
.state_after
;
673 line
.layout
->Reflow();
675 /* Line is new, layout it */
676 FontState old_state
= state
;
677 #if defined(WITH_ICU_LAYOUT) || defined(WITH_UNISCRIBE) || defined(WITH_COCOA)
678 const char *old_str
= str
;
681 #ifdef WITH_ICU_LAYOUT
682 GetLayouter
<ICUParagraphLayoutFactory
>(line
, str
, state
);
683 if (line
.layout
== NULL
) {
684 static bool warned
= false;
686 DEBUG(misc
, 0, "ICU layouter bailed on the font. Falling back to the fallback layouter");
695 #ifdef WITH_UNISCRIBE
696 if (line
.layout
== NULL
) {
697 GetLayouter
<UniscribeParagraphLayoutFactory
>(line
, str
, state
);
698 if (line
.layout
== NULL
) {
706 if (line
.layout
== NULL
) {
707 GetLayouter
<CoreTextParagraphLayoutFactory
>(line
, str
, state
);
708 if (line
.layout
== NULL
) {
715 if (line
.layout
== NULL
) {
716 GetLayouter
<FallbackParagraphLayoutFactory
>(line
, str
, state
);
720 /* Copy all lines into a local cache so we can reuse them later on more easily. */
721 const ParagraphLayouter::Line
*l
;
722 while ((l
= line
.layout
->NextLine(maxw
)) != NULL
) {
730 * Get the boundaries of this paragraph.
731 * @return The boundaries.
733 Dimension
Layouter::GetBounds()
735 Dimension d
= { 0, 0 };
736 for (const ParagraphLayouter::Line
**l
= this->Begin(); l
!= this->End(); l
++) {
737 d
.width
= max
<uint
>(d
.width
, (*l
)->GetWidth());
738 d
.height
+= (*l
)->GetLeading();
744 * Get the position of a character in the layout.
745 * @param ch Character to get the position of.
746 * @return Upper left corner of the character relative to the start of the string.
747 * @note Will only work right for single-line strings.
749 Point
Layouter::GetCharPosition(const char *ch
) const
751 /* Find the code point index which corresponds to the char
752 * pointer into our UTF-8 source string. */
754 const char *str
= this->string
;
757 size_t len
= Utf8Decode(&c
, str
);
758 if (c
== '\0' || c
== '\n') break;
760 index
+= (*this->Begin())->GetInternalCharLength(c
);
764 /* Valid character. */
765 const ParagraphLayouter::Line
*line
= *this->Begin();
767 /* Pointer to the end-of-string/line marker? Return total line width. */
768 if (*ch
== '\0' || *ch
== '\n') {
769 Point p
= { line
->GetWidth(), 0 };
773 /* Scan all runs until we've found our code point index. */
774 for (int run_index
= 0; run_index
< line
->CountRuns(); run_index
++) {
775 const ParagraphLayouter::VisualRun
*run
= line
->GetVisualRun(run_index
);
777 for (int i
= 0; i
< run
->GetGlyphCount(); i
++) {
778 /* Matching glyph? Return position. */
779 if ((size_t)run
->GetGlyphToCharMap()[i
] == index
) {
780 Point p
= { (int)run
->GetPositions()[i
* 2], (int)run
->GetPositions()[i
* 2 + 1] };
792 * Get the character that is at a position.
793 * @param x Position in the string.
794 * @return Pointer to the character at the position or NULL if no character is at the position.
796 const char *Layouter::GetCharAtPosition(int x
) const
798 const ParagraphLayouter::Line
*line
= *this->Begin();
800 for (int run_index
= 0; run_index
< line
->CountRuns(); run_index
++) {
801 const ParagraphLayouter::VisualRun
*run
= line
->GetVisualRun(run_index
);
803 for (int i
= 0; i
< run
->GetGlyphCount(); i
++) {
804 /* Not a valid glyph (empty). */
805 if (run
->GetGlyphs()[i
] == 0xFFFF) continue;
807 int begin_x
= (int)run
->GetPositions()[i
* 2];
808 int end_x
= (int)run
->GetPositions()[i
* 2 + 2];
810 if (IsInsideMM(x
, begin_x
, end_x
)) {
811 /* Found our glyph, now convert to UTF-8 string index. */
812 size_t index
= run
->GetGlyphToCharMap()[i
];
815 for (const char *str
= this->string
; *str
!= '\0'; ) {
816 if (cur_idx
== index
) return str
;
818 WChar c
= Utf8Consume(&str
);
819 cur_idx
+= line
->GetInternalCharLength(c
);
829 * Get a static font instance.
831 Font
*Layouter::GetFont(FontSize size
, TextColour colour
)
833 FontColourMap::iterator it
= fonts
[size
].Find(colour
);
834 if (it
!= fonts
[size
].End()) return it
->second
;
836 Font
*f
= new Font(size
, colour
);
837 *fonts
[size
].Append() = FontColourMap::Pair(colour
, f
);
842 * Reset cached font information.
843 * @param size Font size to reset.
845 void Layouter::ResetFontCache(FontSize size
)
847 for (FontColourMap::iterator it
= fonts
[size
].Begin(); it
!= fonts
[size
].End(); ++it
) {
852 /* We must reset the linecache since it references the just freed fonts */
855 #if defined(WITH_UNISCRIBE)
856 UniscribeResetScriptCache(size
);
858 #if defined(WITH_COCOA)
859 MacOSResetScriptCache(size
);
864 * Get reference to cache item.
865 * If the item does not exist yet, it is default constructed.
866 * @param str Source string of the line (including colour and font size codes).
867 * @param len Length of \a str in bytes (no termination).
868 * @param state State of the font at the beginning of the line.
869 * @return Reference to cache item.
871 Layouter::LineCacheItem
&Layouter::GetCachedParagraphLayout(const char *str
, size_t len
, const FontState
&state
)
873 if (linecache
== NULL
) {
874 /* Create linecache on first access to avoid trouble with initialisation order of static variables. */
875 linecache
= new LineCache();
879 key
.state_before
= state
;
880 key
.str
.assign(str
, len
);
881 return (*linecache
)[key
];
887 void Layouter::ResetLineCache()
889 if (linecache
!= NULL
) linecache
->clear();
893 * Reduce the size of linecache if necessary to prevent infinite growth.
895 void Layouter::ReduceLineCache()
897 if (linecache
!= NULL
) {
898 /* TODO LRU cache would be fancy, but not exactly necessary */
899 if (linecache
->size() > 4096) ResetLineCache();