OpenTTD Source: src/gfx_layout.h Source File

OpenTTD Source 15.0-beta1

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) || (c & TC_COLOUR_MASK) == TC_INVALID);
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 {
76public:
77 FontCache *fc;
78 TextColour colour;
79
80 Font(FontSize size, TextColour colour);
81};
82
84 using FontMap = std::vector<std::pair<int, Font *>>;
85
89 class ParagraphLayouter {
90public:
91 virtual ~ParagraphLayouter() = default;
92
94 class Position {
95 public:
96 int16_t left;
97 int16_t right;
98 int16_t top;
99
100 constexpr inline Position(int16_t left, int16_t right, int16_t top) : left(left), right(right), top(top) { }
101
103 constexpr inline Position(const Point &pt) : left(pt.x), right(pt.x), top(pt.y) { }
104 };
105
107 class VisualRun {
108 public:
109 virtual ~VisualRun() = default;
110 virtual const Font *GetFont() const = 0;
111 virtual int GetGlyphCount() const = 0;
112 virtual std::span<const GlyphID> GetGlyphs() const = 0;
113 virtual std::span<const Position> GetPositions() const = 0;
114 virtual int GetLeading() const = 0;
115 virtual std::span<const int> GetGlyphToCharMap() const = 0;
116 };
117
119 class Line {
120 public:
121 virtual ~Line() = default;
122 virtual int GetLeading() const = 0;
123 virtual int GetWidth() const = 0;
124 virtual int CountRuns() const = 0;
125 virtual const VisualRun &GetVisualRun(int run) const = 0;
126 virtual int GetInternalCharLength(char32_t c) const = 0;
127 };
128
129 virtual void Reflow() = 0;
130 virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
131};
132
138 class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
139 std::string_view string;
140
142 struct LineCacheKey {
143 FontState state_before;
144 std::string str;
145 };
146
147 struct LineCacheQuery {
148 const FontState &state_before;
149 std::string_view str;
150 };
151
153 struct LineCacheCompare {
154 using is_transparent = void;
155
157 template<typename Key1, typename Key2>
158 bool operator()(const Key1 &lhs, const Key2 &rhs) const
159 {
160 if (lhs.state_before.fontsize != rhs.state_before.fontsize) return lhs.state_before.fontsize < rhs.state_before.fontsize;
161 if (lhs.state_before.cur_colour != rhs.state_before.cur_colour) return lhs.state_before.cur_colour < rhs.state_before.cur_colour;
162 if (lhs.state_before.colour_stack != rhs.state_before.colour_stack) return lhs.state_before.colour_stack < rhs.state_before.colour_stack;
163 return lhs.str < rhs.str;
164 }
165 };
166public:
168 struct LineCacheItem {
169 /* Stuff that cannot be freed until the ParagraphLayout is freed */
170 void *buffer;
171 FontMap runs;
172
173 FontState state_after;
174 std::unique_ptr<ParagraphLayouter> layout = nullptr;
175
176 LineCacheItem() : buffer(nullptr) {}
177 ~LineCacheItem() { free(buffer); }
178 };
179private:
180 typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
181 static LineCache *linecache;
182
183 static LineCacheItem &GetCachedParagraphLayout(std::string_view str, const FontState &state);
184
185 using FontColourMap = std::map<TextColour, std::unique_ptr<Font>>;
186 static FontColourMap fonts[FS_END];
187public:
188 static Font *GetFont(FontSize size, TextColour colour);
189
190 Layouter(std::string_view str, int maxw = INT32_MAX, FontSize fontsize = FS_NORMAL);
191 Dimension GetBounds();
192 ParagraphLayouter::Position GetCharPosition(std::string_view::const_iterator ch) const;
193 ptrdiff_t GetCharAtPosition(int x, size_t line_index) const;
194
195 static void Initialize();
196 static void ResetFontCache(FontSize size);
197 static void ResetLineCache();
198 static void ReduceLineCache();
199};
200
201 ParagraphLayouter::Position GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize = FS_NORMAL);
202ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize = FS_NORMAL);
203
204#endif /* GFX_LAYOUT_H */
FontCache
Font cache for basic fonts.
Definition fontcache.h:21
Font
Container with information about a font.
Definition gfx_layout.h:75
Font::fc
FontCache * fc
The font we are using.
Definition gfx_layout.h:77
Font::colour
TextColour colour
The colour this font has to be.
Definition gfx_layout.h:78
Layouter
The layouter performs all the layout work.
Definition gfx_layout.h:138
Layouter::Initialize
static void Initialize()
Perform initialization of layout engine.
Definition gfx_layout.cpp:342
Layouter::fonts
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition gfx_layout.h:186
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:290
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition gfx_layout.cpp:353
Layouter::GetCharPosition
ParagraphLayouter::Position GetCharPosition(std::string_view::const_iterator ch) const
Get the position of a character in the layout.
Definition gfx_layout.cpp:228
Layouter::GetFont
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition gfx_layout.cpp:330
Layouter::string
std::string_view string
Pointer to the original string.
Definition gfx_layout.h:139
Layouter::ResetLineCache
static void ResetLineCache()
Clear line cache.
Definition gfx_layout.cpp:397
Layouter::GetBounds
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition gfx_layout.cpp:199
Layouter::GetCachedParagraphLayout
static LineCacheItem & GetCachedParagraphLayout(std::string_view str, const FontState &state)
Get reference to cache item.
Definition gfx_layout.cpp:375
Layouter::linecache
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition gfx_layout.h:181
Layouter::ReduceLineCache
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
Definition gfx_layout.cpp:405
ParagraphLayouter::Line
A single line worth of VisualRuns.
Definition gfx_layout.h:119
ParagraphLayouter::Position
Position of a glyph within a VisualRun.
Definition gfx_layout.h:94
ParagraphLayouter::Position::right
int16_t right
Right-most position of glyph.
Definition gfx_layout.h:97
ParagraphLayouter::Position::Position
constexpr Position(const Point &pt)
Conversion from a single point to a Position.
Definition gfx_layout.h:103
ParagraphLayouter::Position::left
int16_t left
Left-most position of glyph.
Definition gfx_layout.h:96
ParagraphLayouter::Position::top
int16_t top
Top-most position of glyph.
Definition gfx_layout.h:98
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition gfx_layout.h:107
ParagraphLayouter
Interface to glue fallback and normal layouter into one.
Definition gfx_layout.h:89
fontcache.h
Functions to read fonts from files and cache them.
gfx_func.h
Functions related to the gfx engine.
GetCharPosInString
ParagraphLayouter::Position GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize=FS_NORMAL)
Get the leading corner of a character in a single-line string relative to the start of the string.
Definition gfx_layout.cpp:421
GetCharAtPosition
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize=FS_NORMAL)
Get the character from a string that is drawn at a specific position.
Definition gfx_layout.cpp:438
FontMap
std::vector< std::pair< int, Font * > > FontMap
Mapping from index to font.
Definition gfx_layout.h:84
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:208
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:209
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:260
TC_FORCED
@ TC_FORCED
Ignore colour changes from strings.
Definition gfx_type.h:285
TC_FLAGS_MASK
@ TC_FLAGS_MASK
Mask to test if TextColour (with flags) is within limits.
Definition gfx_type.h:288
TC_COLOUR_MASK
@ TC_COLOUR_MASK
Mask to test if TextColour (without flags) is within limits.
Definition gfx_type.h:287
math_func.hpp
Integer math functions.
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition stdafx.h:334
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:30
FontState
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition gfx_layout.h:24
FontState::PushColour
void PushColour()
Push the current colour on to the stack.
Definition gfx_layout.h:57
FontState::SetColour
void SetColour(TextColour c)
Switch to new colour c.
Definition gfx_layout.h:37
FontState::PopColour
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition gfx_layout.h:47
FontState::SetFontSize
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition gfx_layout.h:66
FontState::fontsize
FontSize fontsize
Current font size.
Definition gfx_layout.h:25
FontState::colour_stack
std::stack< TextColour, std::vector< TextColour > > colour_stack
Stack of colours to assist with colour switching.
Definition gfx_layout.h:28
FontState::cur_colour
TextColour cur_colour
Current text colour.
Definition gfx_layout.h:26
Layouter::LineCacheCompare
Comparator for std::map.
Definition gfx_layout.h:153
Layouter::LineCacheCompare::operator()
bool operator()(const Key1 &lhs, const Key2 &rhs) const
Comparison operator for LineCacheKey and LineCacheQuery.
Definition gfx_layout.h:158
Layouter::LineCacheCompare::is_transparent
void is_transparent
Enable map queries with various key types.
Definition gfx_layout.h:154
Layouter::LineCacheItem
Item in the linecache.
Definition gfx_layout.h:168
Layouter::LineCacheItem::runs
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:171
Layouter::LineCacheItem::buffer
void * buffer
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:170
Layouter::LineCacheItem::state_after
FontState state_after
Font state after the line.
Definition gfx_layout.h:173
Layouter::LineCacheItem::layout
std::unique_ptr< ParagraphLayouter > layout
Layout of the line.
Definition gfx_layout.h:174
Layouter::LineCacheKey
Key into the linecache.
Definition gfx_layout.h:142
Layouter::LineCacheKey::state_before
FontState state_before
Font state at the beginning of the line.
Definition gfx_layout.h:143
Layouter::LineCacheKey::str
std::string str
Source string of the line (including colour and font size codes).
Definition gfx_layout.h:144
Layouter::LineCacheQuery
Definition gfx_layout.h:147
Layouter::LineCacheQuery::state_before
const FontState & state_before
Font state at the beginning of the line.
Definition gfx_layout.h:148
Layouter::LineCacheQuery::str
std::string_view str
Source string of the line (including colour and font size codes).
Definition gfx_layout.h:149
Point
Coordinates of a point in 2D.
Definition geometry_type.hpp:21