2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file gfx_layout.h Functions related to laying out the texts. */
13 #include "fontcache.h"
15 #include "core/smallmap_type.hpp"
23 #include "layout/ParagraphLayout.h"
24 #define ICU_FONTINSTANCE : public icu::LEFontInstance
25 #else /* WITH_ICU_LX */
26 #define ICU_FONTINSTANCE
27 #endif /* WITH_ICU_LX */
30 * Text drawing parameters, which can change while drawing a line, but are kept between multiple parts
31 * of the same text, e.g. on line breaks.
34 FontSize fontsize
; ///< Current font size.
35 TextColour cur_colour
; ///< Current text colour.
37 std::stack
<TextColour
, std::vector
<TextColour
>> colour_stack
; ///< Stack of colours to assist with colour switching.
39 FontState() : fontsize(FS_END
), cur_colour(TC_INVALID
) {}
40 FontState(TextColour colour
, FontSize fontsize
) : fontsize(fontsize
), cur_colour(colour
) {}
43 * Switch to new colour \a c.
44 * @param c New colour to use.
46 inline void SetColour(TextColour c
)
48 assert(c
>= TC_BLUE
&& c
<= TC_BLACK
);
49 if ((this->cur_colour
& TC_FORCED
) == 0) this->cur_colour
= c
;
53 * Switch to and pop the last saved colour on the stack.
55 inline void PopColour()
57 if (colour_stack
.empty()) return;
58 SetColour(colour_stack
.top());
63 * Push the current colour on to the stack.
65 inline void PushColour()
67 colour_stack
.push(this->cur_colour
);
71 * Switch to using a new font \a f.
72 * @param f New font to use.
74 inline void SetFontSize(FontSize f
)
81 * Container with information about a font.
83 class Font ICU_FONTINSTANCE
{
85 FontCache
*fc
; ///< The font we are using.
86 TextColour colour
; ///< The colour this font has to be.
88 Font(FontSize size
, TextColour colour
);
91 /* Implementation details of LEFontInstance */
93 le_int32
getUnitsPerEM() const;
94 le_int32
getAscent() const;
95 le_int32
getDescent() const;
96 le_int32
getLeading() const;
97 float getXPixelsPerEm() const;
98 float getYPixelsPerEm() const;
99 float getScaleFactorX() const;
100 float getScaleFactorY() const;
101 const void *getFontTable(LETag tableTag
) const;
102 const void *getFontTable(LETag tableTag
, size_t &length
) const;
103 LEGlyphID
mapCharToGlyph(LEUnicode32 ch
) const;
104 void getGlyphAdvance(LEGlyphID glyph
, LEPoint
&advance
) const;
105 le_bool
getGlyphPoint(LEGlyphID glyph
, le_int32 pointNumber
, LEPoint
&point
) const;
106 #endif /* WITH_ICU_LX */
109 /** Mapping from index to font. */
110 typedef SmallMap
<int, Font
*> FontMap
;
113 * Interface to glue fallback and normal layouter into one.
115 class ParagraphLayouter
{
117 virtual ~ParagraphLayouter() {}
119 /** Visual run contains data about the bit of text with the same font. */
122 virtual ~VisualRun() {}
123 virtual const Font
*GetFont() const = 0;
124 virtual int GetGlyphCount() const = 0;
125 virtual const GlyphID
*GetGlyphs() const = 0;
126 virtual const float *GetPositions() const = 0;
127 virtual int GetLeading() const = 0;
128 virtual const int *GetGlyphToCharMap() const = 0;
131 /** A single line worth of VisualRuns. */
135 virtual int GetLeading() const = 0;
136 virtual int GetWidth() const = 0;
137 virtual int CountRuns() const = 0;
138 virtual const VisualRun
&GetVisualRun(int run
) const = 0;
139 virtual int GetInternalCharLength(WChar c
) const = 0;
142 virtual void Reflow() = 0;
143 virtual std::unique_ptr
<const Line
> NextLine(int max_width
) = 0;
147 * The layouter performs all the layout work.
149 * It also accounts for the memory allocations and frees.
151 class Layouter
: public std::vector
<std::unique_ptr
<const ParagraphLayouter::Line
>> {
152 const char *string
; ///< Pointer to the original string.
154 /** Key into the linecache */
155 struct LineCacheKey
{
156 FontState state_before
; ///< Font state at the beginning of the line.
157 std::string str
; ///< Source string of the line (including colour and font size codes).
159 /** Comparison operator for std::map */
160 bool operator<(const LineCacheKey
&other
) const
162 if (this->state_before
.fontsize
!= other
.state_before
.fontsize
) return this->state_before
.fontsize
< other
.state_before
.fontsize
;
163 if (this->state_before
.cur_colour
!= other
.state_before
.cur_colour
) return this->state_before
.cur_colour
< other
.state_before
.cur_colour
;
164 if (this->state_before
.colour_stack
!= other
.state_before
.colour_stack
) return this->state_before
.colour_stack
< other
.state_before
.colour_stack
;
165 return this->str
< other
.str
;
169 /** Item in the linecache */
170 struct LineCacheItem
{
171 /* Stuff that cannot be freed until the ParagraphLayout is freed */
172 void *buffer
; ///< Accessed by both ICU's and our ParagraphLayout::nextLine.
173 FontMap runs
; ///< Accessed by our ParagraphLayout::nextLine.
175 FontState state_after
; ///< Font state after the line.
176 ParagraphLayouter
*layout
; ///< Layout of the line.
178 LineCacheItem() : buffer(nullptr), layout(nullptr) {}
179 ~LineCacheItem() { delete layout
; free(buffer
); }
182 typedef std::map
<LineCacheKey
, LineCacheItem
> LineCache
;
183 static LineCache
*linecache
;
185 static LineCacheItem
&GetCachedParagraphLayout(const char *str
, size_t len
, const FontState
&state
);
187 typedef SmallMap
<TextColour
, Font
*> FontColourMap
;
188 static FontColourMap fonts
[FS_END
];
190 static Font
*GetFont(FontSize size
, TextColour colour
);
192 Layouter(const char *str
, int maxw
= INT32_MAX
, TextColour colour
= TC_FROMSTRING
, FontSize fontsize
= FS_NORMAL
);
193 Dimension
GetBounds();
194 Point
GetCharPosition(const char *ch
) const;
195 const char *GetCharAtPosition(int x
) const;
197 static void ResetFontCache(FontSize size
);
198 static void ResetLineCache();
199 static void ReduceLineCache();
202 #endif /* GFX_LAYOUT_H */