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.h Functions related to laying out the texts. */
15 #include "fontcache.h"
17 #include "core/smallmap_type.hpp"
25 #include "layout/ParagraphLayout.h"
26 #define ICU_FONTINSTANCE : public icu::LEFontInstance
27 #else /* WITH_ICU_LX */
28 #define ICU_FONTINSTANCE
29 #endif /* WITH_ICU_LX */
32 * Text drawing parameters, which can change while drawing a line, but are kept between multiple parts
33 * of the same text, e.g. on line breaks.
36 FontSize fontsize
; ///< Current font size.
37 TextColour cur_colour
; ///< Current text colour.
39 std::stack
<TextColour
, std::vector
<TextColour
>> colour_stack
; ///< Stack of colours to assist with colour switching.
41 FontState() : fontsize(FS_END
), cur_colour(TC_INVALID
) {}
42 FontState(TextColour colour
, FontSize fontsize
) : fontsize(fontsize
), cur_colour(colour
) {}
45 * Switch to new colour \a c.
46 * @param c New colour to use.
48 inline void SetColour(TextColour c
)
50 assert(c
>= TC_BLUE
&& c
<= TC_BLACK
);
55 * Switch to and pop the last saved colour on the stack.
57 inline void PopColour()
59 if (colour_stack
.empty()) return;
60 SetColour(colour_stack
.top());
65 * Push the current colour on to the stack.
67 inline void PushColour()
69 colour_stack
.push(this->cur_colour
);
73 * Switch to using a new font \a f.
74 * @param f New font to use.
76 inline void SetFontSize(FontSize f
)
83 * Container with information about a font.
85 class Font ICU_FONTINSTANCE
{
87 FontCache
*fc
; ///< The font we are using.
88 TextColour colour
; ///< The colour this font has to be.
90 Font(FontSize size
, TextColour colour
);
93 /* Implementation details of LEFontInstance */
95 le_int32
getUnitsPerEM() const;
96 le_int32
getAscent() const;
97 le_int32
getDescent() const;
98 le_int32
getLeading() const;
99 float getXPixelsPerEm() const;
100 float getYPixelsPerEm() const;
101 float getScaleFactorX() const;
102 float getScaleFactorY() const;
103 const void *getFontTable(LETag tableTag
) const;
104 const void *getFontTable(LETag tableTag
, size_t &length
) const;
105 LEGlyphID
mapCharToGlyph(LEUnicode32 ch
) const;
106 void getGlyphAdvance(LEGlyphID glyph
, LEPoint
&advance
) const;
107 le_bool
getGlyphPoint(LEGlyphID glyph
, le_int32 pointNumber
, LEPoint
&point
) const;
108 #endif /* WITH_ICU_LX */
111 /** Mapping from index to font. */
112 typedef SmallMap
<int, Font
*> FontMap
;
115 * Interface to glue fallback and normal layouter into one.
117 class ParagraphLayouter
{
119 virtual ~ParagraphLayouter() {}
121 /** Visual run contains data about the bit of text with the same font. */
124 virtual ~VisualRun() {}
125 virtual const Font
*GetFont() const = 0;
126 virtual int GetGlyphCount() const = 0;
127 virtual const GlyphID
*GetGlyphs() const = 0;
128 virtual const float *GetPositions() const = 0;
129 virtual int GetLeading() const = 0;
130 virtual const int *GetGlyphToCharMap() const = 0;
133 /** A single line worth of VisualRuns. */
137 virtual int GetLeading() const = 0;
138 virtual int GetWidth() const = 0;
139 virtual int CountRuns() const = 0;
140 virtual const VisualRun
&GetVisualRun(int run
) const = 0;
141 virtual int GetInternalCharLength(WChar c
) const = 0;
144 virtual void Reflow() = 0;
145 virtual std::unique_ptr
<const Line
> NextLine(int max_width
) = 0;
149 * The layouter performs all the layout work.
151 * It also accounts for the memory allocations and frees.
153 class Layouter
: public std::vector
<std::unique_ptr
<const ParagraphLayouter::Line
>> {
154 const char *string
; ///< Pointer to the original string.
156 /** Key into the linecache */
157 struct LineCacheKey
{
158 FontState state_before
; ///< Font state at the beginning of the line.
159 std::string str
; ///< Source string of the line (including colour and font size codes).
161 /** Comparison operator for std::map */
162 bool operator<(const LineCacheKey
&other
) const
164 if (this->state_before
.fontsize
!= other
.state_before
.fontsize
) return this->state_before
.fontsize
< other
.state_before
.fontsize
;
165 if (this->state_before
.cur_colour
!= other
.state_before
.cur_colour
) return this->state_before
.cur_colour
< other
.state_before
.cur_colour
;
166 if (this->state_before
.colour_stack
!= other
.state_before
.colour_stack
) return this->state_before
.colour_stack
< other
.state_before
.colour_stack
;
167 return this->str
< other
.str
;
171 /** Item in the linecache */
172 struct LineCacheItem
{
173 /* Stuff that cannot be freed until the ParagraphLayout is freed */
174 void *buffer
; ///< Accessed by both ICU's and our ParagraphLayout::nextLine.
175 FontMap runs
; ///< Accessed by our ParagraphLayout::nextLine.
177 FontState state_after
; ///< Font state after the line.
178 ParagraphLayouter
*layout
; ///< Layout of the line.
180 LineCacheItem() : buffer(nullptr), layout(nullptr) {}
181 ~LineCacheItem() { delete layout
; free(buffer
); }
184 typedef std::map
<LineCacheKey
, LineCacheItem
> LineCache
;
185 static LineCache
*linecache
;
187 static LineCacheItem
&GetCachedParagraphLayout(const char *str
, size_t len
, const FontState
&state
);
189 typedef SmallMap
<TextColour
, Font
*> FontColourMap
;
190 static FontColourMap fonts
[FS_END
];
192 static Font
*GetFont(FontSize size
, TextColour colour
);
194 Layouter(const char *str
, int maxw
= INT32_MAX
, TextColour colour
= TC_FROMSTRING
, FontSize fontsize
= FS_NORMAL
);
195 Dimension
GetBounds();
196 Point
GetCharPosition(const char *ch
) const;
197 const char *GetCharAtPosition(int x
) const;
199 static void ResetFontCache(FontSize size
);
200 static void ResetLineCache();
201 static void ReduceLineCache();
204 #endif /* GFX_LAYOUT_H */