OpenTTD Source: src/gfx_layout.h Source File

OpenTTD Source 14.0-beta3

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/math_func.hpp"
16
17 #include <stack>
18 #include <string_view>
19
24 struct FontState {
25 FontSize fontsize;
26 TextColour cur_colour;
27
28 std::stack<TextColour, std::vector<TextColour>> colour_stack;
29
30 FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
31 FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour) {}
32
37 inline void SetColour(TextColour c)
38 {
39 assert((c & TC_COLOUR_MASK) >= TC_BLUE && (c & TC_COLOUR_MASK) <= TC_BLACK);
40 assert((c & (TC_COLOUR_MASK | TC_FLAGS_MASK)) == c);
41 if ((this->cur_colour & TC_FORCED) == 0) this->cur_colour = c;
42 }
43
47 inline void PopColour()
48 {
49 if (colour_stack.empty()) return;
50 SetColour(colour_stack.top());
51 colour_stack.pop();
52 }
53
57 inline void PushColour()
58 {
59 colour_stack.push(this->cur_colour);
60 }
61
66 inline void SetFontSize(FontSize f)
67 {
68 this->fontsize = f;
69 }
70 };
71
75 class Font {
76 public:
77 FontCache *fc;
78 TextColour colour;
79
80 Font(FontSize size, TextColour colour);
81 };
82
84 using FontMap = std::map<int, Font *>;
85
89 class ParagraphLayouter {
90 public:
91 virtual ~ParagraphLayouter() = default;
92
94 class VisualRun {
95 public:
96 virtual ~VisualRun() = default;
97 virtual const Font *GetFont() const = 0;
98 virtual int GetGlyphCount() const = 0;
99 virtual const std::vector<GlyphID> &GetGlyphs() const = 0;
100 virtual const std::vector<Point> &GetPositions() const = 0;
101 virtual int GetLeading() const = 0;
102 virtual const std::vector<int> &GetGlyphToCharMap() const = 0;
103 };
104
106 class Line {
107 public:
108 virtual ~Line() = default;
109 virtual int GetLeading() const = 0;
110 virtual int GetWidth() const = 0;
111 virtual int CountRuns() const = 0;
112 virtual const VisualRun &GetVisualRun(int run) const = 0;
113 virtual int GetInternalCharLength(char32_t c) const = 0;
114 };
115
116 virtual void Reflow() = 0;
117 virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
118 };
119
125 class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
126 std::string_view string;
127
129 struct LineCacheKey {
130 FontState state_before;
131 std::string str;
132 };
133
134 struct LineCacheQuery {
135 FontState state_before;
136 std::string_view str;
137 };
138
140 struct LineCacheCompare {
141 using is_transparent = void;
142
144 template<typename Key1, typename Key2>
145 bool operator()(const Key1 &lhs, const Key2 &rhs) const
146 {
147 if (lhs.state_before.fontsize != rhs.state_before.fontsize) return lhs.state_before.fontsize < rhs.state_before.fontsize;
148 if (lhs.state_before.cur_colour != rhs.state_before.cur_colour) return lhs.state_before.cur_colour < rhs.state_before.cur_colour;
149 if (lhs.state_before.colour_stack != rhs.state_before.colour_stack) return lhs.state_before.colour_stack < rhs.state_before.colour_stack;
150 return lhs.str < rhs.str;
151 }
152 };
153 public:
155 struct LineCacheItem {
156 /* Stuff that cannot be freed until the ParagraphLayout is freed */
157 void *buffer;
158 FontMap runs;
159
160 FontState state_after;
161 ParagraphLayouter *layout;
162
163 LineCacheItem() : buffer(nullptr), layout(nullptr) {}
164 ~LineCacheItem() { delete layout; free(buffer); }
165 };
166 private:
167 typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
168 static LineCache *linecache;
169
170 static LineCacheItem &GetCachedParagraphLayout(std::string_view str, const FontState &state);
171
172 using FontColourMap = std::map<TextColour, std::unique_ptr<Font>>;
173 static FontColourMap fonts[FS_END];
174 public:
175 static Font *GetFont(FontSize size, TextColour colour);
176
177 Layouter(std::string_view str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
178 Dimension GetBounds();
179 Point GetCharPosition(std::string_view::const_iterator ch) const;
180 ptrdiff_t GetCharAtPosition(int x, size_t line_index) const;
181
182 static void Initialize();
183 static void ResetFontCache(FontSize size);
184 static void ResetLineCache();
185 static void ReduceLineCache();
186 };
187
188 #endif /* GFX_LAYOUT_H */
TC_FORCED
@ TC_FORCED
Ignore colour changes from strings.
Definition: gfx_type.h:278
Layouter::LineCacheKey
Key into the linecache.
Definition: gfx_layout.h:129
Layouter::LineCacheItem
Item in the linecache.
Definition: gfx_layout.h:155
Layouter::string
std::string_view string
Pointer to the original string.
Definition: gfx_layout.h:126
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
Font::fc
FontCache * fc
The font we are using.
Definition: gfx_layout.h:77
Layouter::LineCacheQuery::str
std::string_view str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:136
Layouter::LineCacheCompare
Comparator for std::map.
Definition: gfx_layout.h:140
FontState::PopColour
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:47
math_func.hpp
Layouter::linecache
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:168
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:253
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:351
Layouter::GetCachedParagraphLayout
static LineCacheItem & GetCachedParagraphLayout(std::string_view str, const FontState &state)
Get reference to cache item.
Definition: gfx_layout.cpp:373
ParagraphLayouter
Interface to glue fallback and normal layouter into one.
Definition: gfx_layout.h:89
Layouter::LineCacheItem::layout
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:161
Layouter::LineCacheCompare::is_transparent
void is_transparent
Enable map queries with various key types.
Definition: gfx_layout.h:141
gfx_func.h
ParagraphLayouter::Line
A single line worth of VisualRuns.
Definition: gfx_layout.h:106
Layouter::LineCacheKey::str
std::string str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:131
Layouter::fonts
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition: gfx_layout.h:173
Layouter::LineCacheKey::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:130
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:203
Layouter::LineCacheItem::runs
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:158
FontState::fontsize
FontSize fontsize
Current font size.
Definition: gfx_layout.h:25
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:379
Layouter::LineCacheCompare::operator()
bool operator()(const Key1 &lhs, const Key2 &rhs) const
Comparison operator for LineCacheKey and LineCacheQuery.
Definition: gfx_layout.h:145
Layouter::GetBounds
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:199
Layouter
The layouter performs all the layout work.
Definition: gfx_layout.h:125
Font::Font
Font(FontSize size, TextColour colour)
Construct a new font.
Definition: gfx_layout.cpp:47
Layouter::LineCacheQuery::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:135
Layouter::LineCacheQuery
Definition: gfx_layout.h:134
Layouter::GetFont
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition: gfx_layout.cpp:328
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
Font
Container with information about a font.
Definition: gfx_layout.h:75
Layouter::ReduceLineCache
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
Definition: gfx_layout.cpp:403
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:94
FontState::colour_stack
std::stack< TextColour, std::vector< TextColour > > colour_stack
Stack of colours to assist with colour switching.
Definition: gfx_layout.h:28
Font::colour
TextColour colour
The colour this font has to be.
Definition: gfx_layout.h:78
FontState::cur_colour
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:26
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
FontState::SetColour
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:37
TC_FLAGS_MASK
@ TC_FLAGS_MASK
Mask to test if TextColour (with flags) is within limits.
Definition: gfx_type.h:281
Layouter::LineCacheItem::state_after
FontState state_after
Font state after the line.
Definition: gfx_layout.h:160
Layouter::Layouter
Layouter(std::string_view str, int maxw=INT32_MAX, TextColour colour=TC_FROMSTRING, FontSize fontsize=FS_NORMAL)
Create a new layouter.
Definition: gfx_layout.cpp:130
FontState::SetFontSize
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:66
FontState
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:24
Layouter::ResetLineCache
static void ResetLineCache()
Clear line cache.
Definition: gfx_layout.cpp:395
fontcache.h
Layouter::GetCharPosition
Point GetCharPosition(std::string_view::const_iterator ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:228
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:202
TC_COLOUR_MASK
@ TC_COLOUR_MASK
Mask to test if TextColour (without flags) is within limits.
Definition: gfx_type.h:280
FontMap
std::map< int, Font * > FontMap
Mapping from index to font.
Definition: gfx_layout.h:84
Layouter::Initialize
static void Initialize()
Perform initialization of layout engine.
Definition: gfx_layout.cpp:340
FontState::PushColour
void PushColour()
Push the current colour on to the stack.
Definition: gfx_layout.h:57
Layouter::LineCacheItem::buffer
void * buffer
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:157
Layouter::GetCharAtPosition
ptrdiff_t GetCharAtPosition(int x, size_t line_index) const
Get the character that is at a pixel position in the first line of the layouted text.
Definition: gfx_layout.cpp:288