OpenTTD Source 20241224-master-gf74b0cf984
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
24struct FontState {
27
28 std::stack<TextColour, std::vector<TextColour>> colour_stack;
29
30 FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
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;
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
75class Font {
76public:
79
81};
82
84using FontMap = std::vector<std::pair<int, Font *>>;
85
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
138class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
139 std::string_view string;
140
144 std::string str;
145 };
146
149 std::string_view str;
150 };
151
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:
169 /* Stuff that cannot be freed until the ParagraphLayout is freed */
170 void *buffer;
172
174 std::unique_ptr<ParagraphLayouter> layout = nullptr;
175
176 LineCacheItem() : buffer(nullptr) {}
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);
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
201ParagraphLayouter::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 */
Font cache for basic fonts.
Definition fontcache.h:21
Container with information about a font.
Definition gfx_layout.h:75
FontCache * fc
The font we are using.
Definition gfx_layout.h:77
TextColour colour
The colour this font has to be.
Definition gfx_layout.h:78
The layouter performs all the layout work.
Definition gfx_layout.h:138
static void Initialize()
Perform initialization of layout engine.
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition gfx_layout.h:186
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.
static void ResetFontCache(FontSize size)
Reset cached font information.
ParagraphLayouter::Position GetCharPosition(std::string_view::const_iterator ch) const
Get the position of a character in the layout.
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
std::string_view string
Pointer to the original string.
Definition gfx_layout.h:139
static void ResetLineCache()
Clear line cache.
Dimension GetBounds()
Get the boundaries of this paragraph.
static LineCacheItem & GetCachedParagraphLayout(std::string_view str, const FontState &state)
Get reference to cache item.
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition gfx_layout.h:181
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
A single line worth of VisualRuns.
Definition gfx_layout.h:119
Position of a glyph within a VisualRun.
Definition gfx_layout.h:94
int16_t right
Right-most position of glyph.
Definition gfx_layout.h:97
constexpr Position(const Point &pt)
Conversion from a single point to a Position.
Definition gfx_layout.h:103
int16_t left
Left-most position of glyph.
Definition gfx_layout.h:96
int16_t top
Top-most position of glyph.
Definition gfx_layout.h:98
Visual run contains data about the bit of text with the same font.
Definition gfx_layout.h:107
Interface to glue fallback and normal layouter into one.
Definition gfx_layout.h:89
Functions to read fonts from files and cache them.
Functions related to the gfx engine.
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.
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.
std::vector< std::pair< int, Font * > > FontMap
Mapping from index to font.
Definition gfx_layout.h:84
FontSize
Available font sizes.
Definition gfx_type.h:208
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:209
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
Ignore colour changes from strings.
Definition gfx_type.h:285
@ TC_FLAGS_MASK
Mask to test if TextColour (with flags) is within limits.
Definition gfx_type.h:288
@ TC_COLOUR_MASK
Mask to test if TextColour (without flags) is within limits.
Definition gfx_type.h:287
Integer math functions.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition stdafx.h:334
Dimensions (a width and height) of a rectangle in 2D.
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition gfx_layout.h:24
void PushColour()
Push the current colour on to the stack.
Definition gfx_layout.h:57
void SetColour(TextColour c)
Switch to new colour c.
Definition gfx_layout.h:37
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition gfx_layout.h:47
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition gfx_layout.h:66
FontSize fontsize
Current font size.
Definition gfx_layout.h:25
std::stack< TextColour, std::vector< TextColour > > colour_stack
Stack of colours to assist with colour switching.
Definition gfx_layout.h:28
TextColour cur_colour
Current text colour.
Definition gfx_layout.h:26
Comparator for std::map.
Definition gfx_layout.h:153
bool operator()(const Key1 &lhs, const Key2 &rhs) const
Comparison operator for LineCacheKey and LineCacheQuery.
Definition gfx_layout.h:158
void is_transparent
Enable map queries with various key types.
Definition gfx_layout.h:154
Item in the linecache.
Definition gfx_layout.h:168
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:171
void * buffer
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:170
FontState state_after
Font state after the line.
Definition gfx_layout.h:173
std::unique_ptr< ParagraphLayouter > layout
Layout of the line.
Definition gfx_layout.h:174
Key into the linecache.
Definition gfx_layout.h:142
FontState state_before
Font state at the beginning of the line.
Definition gfx_layout.h:143
std::string str
Source string of the line (including colour and font size codes).
Definition gfx_layout.h:144
const FontState & state_before
Font state at the beginning of the line.
Definition gfx_layout.h:148
std::string_view str
Source string of the line (including colour and font size codes).
Definition gfx_layout.h:149
Coordinates of a point in 2D.