OpenTTD Source: src/gfx_layout.h Source File

OpenTTD Source 13.1

gfx_layout.h
Go to the documentation of this file.
1 /*
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/>.
6 */
7
10 #ifndef GFX_LAYOUT_H
11 #define GFX_LAYOUT_H
12
13 #include "fontcache.h"
14 #include "gfx_func.h"
15 #include "core/smallmap_type.hpp"
16
17 #include <map>
18 #include <string>
19 #include <stack>
20 #include <string_view>
21 #include <type_traits>
22 #include <vector>
23
24 #ifdef WITH_ICU_LX
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 */
30
35 struct FontState {
36 FontSize fontsize;
37 TextColour cur_colour;
38
39 std::stack<TextColour, std::vector<TextColour>> colour_stack;
40
41 FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
42 FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour) {}
43
48 inline void SetColour(TextColour c)
49 {
50 assert(c >= TC_BLUE && c <= TC_BLACK);
51 if ((this->cur_colour & TC_FORCED) == 0) this->cur_colour = c;
52 }
53
57 inline void PopColour()
58 {
59 if (colour_stack.empty()) return;
60 SetColour(colour_stack.top());
61 colour_stack.pop();
62 }
63
67 inline void PushColour()
68 {
69 colour_stack.push(this->cur_colour);
70 }
71
76 inline void SetFontSize(FontSize f)
77 {
78 this->fontsize = f;
79 }
80 };
81
85 class Font ICU_FONTINSTANCE {
86 public:
87 FontCache *fc;
88 TextColour colour;
89
90 Font(FontSize size, TextColour colour);
91
92 #ifdef WITH_ICU_LX
93 /* Implementation details of LEFontInstance */
94
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 */
109 };
110
112 typedef SmallMap<int, Font *> FontMap;
113
117 class ParagraphLayouter {
118 public:
119 virtual ~ParagraphLayouter() {}
120
122 class VisualRun {
123 public:
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;
131 };
132
134 class Line {
135 public:
136 virtual ~Line() {}
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;
142 };
143
144 virtual void Reflow() = 0;
145 virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
146 };
147
153 class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
154 const char *string;
155
157 struct LineCacheKey {
158 FontState state_before;
159 std::string str;
160 };
161
162 struct LineCacheQuery {
163 FontState state_before;
164 std::string_view str;
165 };
166
168 struct LineCacheCompare {
169 using is_transparent = void;
170
172 template<typename Key1, typename Key2>
173 bool operator()(const Key1 &lhs, const Key2 &rhs) const
174 {
175 if (lhs.state_before.fontsize != rhs.state_before.fontsize) return lhs.state_before.fontsize < rhs.state_before.fontsize;
176 if (lhs.state_before.cur_colour != rhs.state_before.cur_colour) return lhs.state_before.cur_colour < rhs.state_before.cur_colour;
177 if (lhs.state_before.colour_stack != rhs.state_before.colour_stack) return lhs.state_before.colour_stack < rhs.state_before.colour_stack;
178 return lhs.str < rhs.str;
179 }
180 };
181 public:
183 struct LineCacheItem {
184 /* Stuff that cannot be freed until the ParagraphLayout is freed */
185 void *buffer;
186 FontMap runs;
187
188 FontState state_after;
189 ParagraphLayouter *layout;
190
191 LineCacheItem() : buffer(nullptr), layout(nullptr) {}
192 ~LineCacheItem() { delete layout; free(buffer); }
193 };
194 private:
195 typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
196 static LineCache *linecache;
197
198 static LineCacheItem &GetCachedParagraphLayout(const char *str, size_t len, const FontState &state);
199
200 typedef SmallMap<TextColour, Font *> FontColourMap;
201 static FontColourMap fonts[FS_END];
202 public:
203 static Font *GetFont(FontSize size, TextColour colour);
204
205 Layouter(const char *str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
206 Dimension GetBounds();
207 Point GetCharPosition(const char *ch) const;
208 const char *GetCharAtPosition(int x) const;
209
210 static void ResetFontCache(FontSize size);
211 static void ResetLineCache();
212 static void ReduceLineCache();
213 };
214
215 #endif /* GFX_LAYOUT_H */
TC_FORCED
@ TC_FORCED
Ignore colour changes from strings.
Definition: gfx_type.h:282
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
Layouter::LineCacheKey
Key into the linecache.
Definition: gfx_layout.h:157
Layouter::LineCacheItem
Item in the linecache.
Definition: gfx_layout.h:183
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:36
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
Layouter::LineCacheQuery::str
std::string_view str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:164
Layouter::LineCacheCompare
Comparator for std::map.
Definition: gfx_layout.h:168
FontState::PopColour
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:57
Layouter::string
const char * string
Pointer to the original string.
Definition: gfx_layout.h:154
Layouter::linecache
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:196
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:257
Layouter::GetCharPosition
Point GetCharPosition(const char *ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:768
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:864
smallmap_type.hpp
ICU_FONTINSTANCE::fc
FontCache * fc
The font we are using.
Definition: gfx_layout.h:87
ParagraphLayouter
Interface to glue fallback and normal layouter into one.
Definition: gfx_layout.h:117
SmallMap< int, Font * >
Layouter::LineCacheItem::layout
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:189
Layouter::LineCacheCompare::is_transparent
void is_transparent
Enable map queries with various key types.
Definition: gfx_layout.h:169
FontMap
SmallMap< int, Font * > FontMap
Mapping from index to font.
Definition: gfx_layout.h:112
gfx_func.h
ParagraphLayouter::Line
A single line worth of VisualRuns.
Definition: gfx_layout.h:134
Layouter::LineCacheKey::str
std::string str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:159
ICU_FONTINSTANCE
Container with information about a font.
Definition: gfx_layout.h:85
Layouter::fonts
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition: gfx_layout.h:201
Layouter::LineCacheKey::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:158
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:207
Layouter::LineCacheItem::runs
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:186
FontState::fontsize
FontSize fontsize
Current font size.
Definition: gfx_layout.h:36
Layouter::LineCacheCompare::operator()
bool operator()(const Key1 &lhs, const Key2 &rhs) const
Comparison operator for LineCacheKey and LineCacheQuery.
Definition: gfx_layout.h:173
Layouter::GetBounds
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:752
Layouter::Layouter
Layouter(const char *str, int maxw=INT32_MAX, TextColour colour=TC_FROMSTRING, FontSize fontsize=FS_NORMAL)
Create a new layouter.
Definition: gfx_layout.cpp:673
Layouter
The layouter performs all the layout work.
Definition: gfx_layout.h:153
Layouter::LineCacheQuery::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:163
Layouter::LineCacheQuery
Definition: gfx_layout.h:162
Layouter::GetFont
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition: gfx_layout.cpp:850
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
Layouter::ReduceLineCache
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
Definition: gfx_layout.cpp:920
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:122
FontState::colour_stack
std::stack< TextColour, std::vector< TextColour > > colour_stack
Stack of colours to assist with colour switching.
Definition: gfx_layout.h:39
FontState::cur_colour
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:37
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
Layouter::GetCharAtPosition
const char * GetCharAtPosition(int x) const
Get the character that is at a position.
Definition: gfx_layout.cpp:815
FontState::SetColour
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:48
Layouter::LineCacheItem::state_after
FontState state_after
Font state after the line.
Definition: gfx_layout.h:188
Layouter::GetCachedParagraphLayout
static LineCacheItem & GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
Get reference to cache item.
Definition: gfx_layout.cpp:890
FontState::SetFontSize
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:76
FontState
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:35
Layouter::ResetLineCache
static void ResetLineCache()
Clear line cache.
Definition: gfx_layout.cpp:912
fontcache.h
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
FontState::PushColour
void PushColour()
Push the current colour on to the stack.
Definition: gfx_layout.h:67
ICU_FONTINSTANCE::colour
TextColour colour
The colour this font has to be.
Definition: gfx_layout.h:88
Layouter::LineCacheItem::buffer
void * buffer
Accessed by both ICU's and our ParagraphLayout::nextLine.
Definition: gfx_layout.h:185