OpenTTD Source: src/gfx_layout.h Source File

OpenTTD Source 1.11.0-RC1

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 <vector>
21
22 #ifdef WITH_ICU_LX
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 */
28
33 struct FontState {
34 FontSize fontsize;
35 TextColour cur_colour;
36
37 std::stack<TextColour, std::vector<TextColour>> colour_stack;
38
39 FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
40 FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour) {}
41
46 inline void SetColour(TextColour c)
47 {
48 assert(c >= TC_BLUE && c <= TC_BLACK);
49 if ((this->cur_colour & TC_FORCED) == 0) this->cur_colour = c;
50 }
51
55 inline void PopColour()
56 {
57 if (colour_stack.empty()) return;
58 SetColour(colour_stack.top());
59 colour_stack.pop();
60 }
61
65 inline void PushColour()
66 {
67 colour_stack.push(this->cur_colour);
68 }
69
74 inline void SetFontSize(FontSize f)
75 {
76 this->fontsize = f;
77 }
78 };
79
83 class Font ICU_FONTINSTANCE {
84 public:
85 FontCache *fc;
86 TextColour colour;
87
88 Font(FontSize size, TextColour colour);
89
90 #ifdef WITH_ICU_LX
91 /* Implementation details of LEFontInstance */
92
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 */
107 };
108
110 typedef SmallMap<int, Font *> FontMap;
111
115 class ParagraphLayouter {
116 public:
117 virtual ~ParagraphLayouter() {}
118
120 class VisualRun {
121 public:
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;
129 };
130
132 class Line {
133 public:
134 virtual ~Line() {}
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;
140 };
141
142 virtual void Reflow() = 0;
143 virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
144 };
145
151 class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
152 const char *string;
153
155 struct LineCacheKey {
156 FontState state_before;
157 std::string str;
158
160 bool operator<(const LineCacheKey &other) const
161 {
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;
166 }
167 };
168 public:
170 struct LineCacheItem {
171 /* Stuff that cannot be freed until the ParagraphLayout is freed */
172 void *buffer;
173 FontMap runs;
174
175 FontState state_after;
176 ParagraphLayouter *layout;
177
178 LineCacheItem() : buffer(nullptr), layout(nullptr) {}
179 ~LineCacheItem() { delete layout; free(buffer); }
180 };
181 private:
182 typedef std::map<LineCacheKey, LineCacheItem> LineCache;
183 static LineCache *linecache;
184
185 static LineCacheItem &GetCachedParagraphLayout(const char *str, size_t len, const FontState &state);
186
187 typedef SmallMap<TextColour, Font *> FontColourMap;
188 static FontColourMap fonts[FS_END];
189 public:
190 static Font *GetFont(FontSize size, TextColour colour);
191
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;
196
197 static void ResetFontCache(FontSize size);
198 static void ResetLineCache();
199 static void ReduceLineCache();
200 };
201
202 #endif /* GFX_LAYOUT_H */
TC_FORCED
@ TC_FORCED
Ignore colour changes from strings.
Definition: gfx_type.h:275
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
Layouter::LineCacheKey
Key into the linecache.
Definition: gfx_layout.h:155
Layouter::LineCacheItem
Item in the linecache.
Definition: gfx_layout.h:170
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
FontState::PopColour
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:55
Layouter::string
const char * string
Pointer to the original string.
Definition: gfx_layout.h:152
Layouter::linecache
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:183
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
Layouter::GetCharPosition
Point GetCharPosition(const char *ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:762
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:858
smallmap_type.hpp
ICU_FONTINSTANCE::fc
FontCache * fc
The font we are using.
Definition: gfx_layout.h:85
ParagraphLayouter
Interface to glue fallback and normal layouter into one.
Definition: gfx_layout.h:115
SmallMap< int, Font * >
Layouter::LineCacheItem::layout
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:176
FontMap
SmallMap< int, Font * > FontMap
Mapping from index to font.
Definition: gfx_layout.h:110
gfx_func.h
ParagraphLayouter::Line
A single line worth of VisualRuns.
Definition: gfx_layout.h:132
Layouter::LineCacheKey::str
std::string str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:157
ICU_FONTINSTANCE
Container with information about a font.
Definition: gfx_layout.h:83
Layouter::fonts
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition: gfx_layout.h:188
Layouter::LineCacheKey::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:156
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:173
FontState::fontsize
FontSize fontsize
Current font size.
Definition: gfx_layout.h:34
Layouter::GetBounds
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:746
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:667
Layouter
The layouter performs all the layout work.
Definition: gfx_layout.h:151
Layouter::GetFont
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition: gfx_layout.cpp:844
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:908
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:120
FontState::colour_stack
std::stack< TextColour, std::vector< TextColour > > colour_stack
Stack of colours to assist with colour switching.
Definition: gfx_layout.h:37
FontState::cur_colour
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:35
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:809
FontState::SetColour
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:46
Layouter::LineCacheKey::operator<
bool operator<(const LineCacheKey &other) const
Comparison operator for std::map.
Definition: gfx_layout.h:160
Layouter::LineCacheItem::state_after
FontState state_after
Font state after the line.
Definition: gfx_layout.h:175
Layouter::GetCachedParagraphLayout
static LineCacheItem & GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
Get reference to cache item.
Definition: gfx_layout.cpp:884
FontState::SetFontSize
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:74
FontState
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:33
Layouter::ResetLineCache
static void ResetLineCache()
Clear line cache.
Definition: gfx_layout.cpp:900
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:454
FontState::PushColour
void PushColour()
Push the current colour on to the stack.
Definition: gfx_layout.h:65
ICU_FONTINSTANCE::colour
TextColour colour
The colour this font has to be.
Definition: gfx_layout.h:86
Layouter::LineCacheItem::buffer
void * buffer
Accessed by both ICU's and our ParagraphLayout::nextLine.
Definition: gfx_layout.h:172