OpenTTD Source 20260218-master-g2123fca5ea
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef GFX_LAYOUT_H
11#define GFX_LAYOUT_H
12
13#include "misc/lrucache.hpp"
14#include "fontcache.h"
15#include "gfx_func.h"
16#include "core/math_func.hpp"
17
18#include <string_view>
19
24struct FontState {
27 std::vector<TextColour> colour_stack;
28
29 FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
31
32 auto operator<=>(const FontState &) const = default;
33
38 inline void SetColour(TextColour c)
39 {
40 assert(((c & TC_COLOUR_MASK) >= TC_BLUE && (c & TC_COLOUR_MASK) <= TC_BLACK) || (c & TC_COLOUR_MASK) == TC_INVALID);
41 assert((c & (TC_COLOUR_MASK | TC_FLAGS_MASK)) == c);
42 if ((this->cur_colour & TC_FORCED) == 0) this->cur_colour = c;
43 }
44
48 inline void PopColour()
49 {
50 if (colour_stack.empty()) return;
51 SetColour(colour_stack.back());
52 colour_stack.pop_back();
53 }
54
58 inline void PushColour()
59 {
60 colour_stack.push_back(this->cur_colour);
61 }
62
67 inline void SetFontSize(FontSize f)
68 {
69 this->fontsize = f;
70 }
71};
72
73template <typename T> struct std::hash<std::vector<T>> {
74 size_t operator()(const std::vector<T> &vec) const
75 {
76 /* This is not an optimal hash algorithm but in most cases this is empty and therefore the same anyway. */
77 return std::transform_reduce(std::begin(vec), std::end(vec),
78 std::hash<size_t>{}(std::size(vec)),
79 [](const size_t &a, const size_t &b) -> size_t { return a ^ b; },
80 [](const T &x) -> size_t { return std::hash<T>{}(x); });
81 }
82};
83
84template <> struct std::hash<FontState> {
85 std::size_t operator()(const FontState &state) const noexcept
86 {
87 size_t h1 = std::hash<FontSize>{}(state.fontsize);
88 size_t h2 = std::hash<TextColour>{}(state.cur_colour);
89 size_t h3 = std::hash<std::vector<TextColour>>{}(state.colour_stack);
90 return h1 ^ (h2 << 1) ^ (h3 << 2);
91 }
92};
93
97class Font {
98public:
101
103};
104
106using FontMap = std::vector<std::pair<int, Font *>>;
107
112public:
113 virtual ~ParagraphLayouter() = default;
114
116 class Position {
117 public:
118 int16_t left;
119 int16_t right;
120 int16_t top;
121
122 constexpr inline Position(int16_t left, int16_t right, int16_t top) : left(left), right(right), top(top) { }
123
128 constexpr inline Position(const Point &pt) : left(pt.x), right(pt.x), top(pt.y) { }
129 };
130
132 class VisualRun {
133 public:
134 virtual ~VisualRun() = default;
135
140 virtual const Font *GetFont() const = 0;
141
146 virtual size_t GetGlyphCount() const = 0;
147
152 virtual std::span<const GlyphID> GetGlyphs() const = 0;
153
158 virtual std::span<const Position> GetPositions() const = 0;
159
164 virtual int GetLeading() const = 0;
165
170 virtual std::span<const int> GetGlyphToCharMap() const = 0;
171 };
172
174 class Line {
175 public:
176 virtual ~Line() = default;
177
182 virtual int GetLeading() const = 0;
183
188 virtual int GetWidth() const = 0;
189
194 virtual size_t CountRuns() const = 0;
195
201 virtual const VisualRun &GetVisualRun(size_t run) const = 0;
202
209 virtual int GetInternalCharLength(char32_t c) const = 0;
210 };
211
215 virtual void Reflow() = 0;
216
222 virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
223};
224
230class Layouter : public std::vector<const ParagraphLayouter::Line *> {
231 std::string_view string;
232
236 std::string str;
237 };
238
241 std::string_view str;
242 };
243
244 friend struct std::hash<Layouter::LineCacheQuery>;
245 struct LineCacheHash;
246
248 using is_transparent = void;
249
250 template <typename Tlhs, typename Trhs>
251 bool operator()(const Tlhs &lhs, const Trhs &rhs) const
252 {
253 return lhs.state_before == rhs.state_before && lhs.str == rhs.str;
254 }
255 };
256
257public:
260 /* Due to the type of data in the buffer differing depending on the Layouter, we need to pass our own deleter routine. */
261 using Buffer = std::unique_ptr<void, void(*)(void *)>;
262 /* Stuff that cannot be freed until the ParagraphLayout is freed */
263 Buffer buffer{nullptr, [](void *){}};
265
267 std::unique_ptr<ParagraphLayouter> layout = nullptr;
268
269 std::vector<std::unique_ptr<const ParagraphLayouter::Line>> cached_layout{};
270 int cached_width = 0;
271 };
272private:
274 static std::unique_ptr<LineCache> linecache;
275
276 static LineCacheItem &GetCachedParagraphLayout(std::string_view str, const FontState &state);
277
278 using FontColourMap = std::map<TextColour, std::unique_ptr<Font>>;
279 static FontColourMap fonts[FS_END];
280public:
281 static Font *GetFont(FontSize size, TextColour colour);
282
283 Layouter(std::string_view str, int maxw = INT32_MAX, FontSize fontsize = FS_NORMAL);
285 ParagraphLayouter::Position GetCharPosition(std::string_view::const_iterator ch) const;
286 ptrdiff_t GetCharAtPosition(int x, size_t line_index) const;
287
288 static void Initialize();
289 static void ResetFontCache(FontSize size);
290 static void ResetLineCache();
291};
292
293ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize = FS_NORMAL);
294ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize = FS_NORMAL);
295
296template <> struct std::hash<Layouter::LineCacheQuery> {
297 std::size_t operator()(const Layouter::LineCacheQuery &state) const noexcept
298 {
299 size_t h1 = std::hash<std::string_view>{}(state.str);
300 size_t h2 = std::hash<FontState>{}(state.state_before);
301 return h1 ^ (h2 << 1);
302 }
303};
304
306 using is_transparent = void;
307
308 std::size_t operator()(const Layouter::LineCacheKey &query) const { return std::hash<Layouter::LineCacheQuery>{}(LineCacheQuery{query.state_before, query.str}); }
309 std::size_t operator()(const Layouter::LineCacheQuery &query) const { return std::hash<Layouter::LineCacheQuery>{}(query); }
310};
311
312#endif /* GFX_LAYOUT_H */
Font cache for basic fonts.
Definition fontcache.h:22
Container with information about a font.
Definition gfx_layout.h:97
Font(FontSize size, TextColour colour)
Construct a new font.
FontCache * fc
The font we are using.
Definition gfx_layout.h:99
TextColour colour
The colour this font has to be.
Definition gfx_layout.h:100
Size limited cache with a least recently used eviction strategy.
Definition lrucache.hpp:23
The layouter performs all the layout work.
Definition gfx_layout.h:230
static void Initialize()
Perform initialization of layout engine.
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition gfx_layout.h:279
Layouter(std::string_view str, int maxw=INT32_MAX, FontSize fontsize=FS_NORMAL)
Create a new layouter.
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 std::unique_ptr< LineCache > linecache
Cache of ParagraphLayout lines.
Definition gfx_layout.h:274
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:231
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.
A single line worth of VisualRuns.
Definition gfx_layout.h:174
virtual int GetLeading() const =0
Get the font leading, or distance between the baselines of consecutive lines.
virtual int GetInternalCharLength(char32_t c) const =0
Get the number of elements the given character occupies in the underlying text buffer of the Layouter...
virtual const VisualRun & GetVisualRun(size_t run) const =0
Get a reference to the given run.
virtual int GetWidth() const =0
Get the width of this line.
virtual size_t CountRuns() const =0
Get the number of runs in this line.
Position of a glyph within a VisualRun.
Definition gfx_layout.h:116
int16_t right
Right-most position of glyph.
Definition gfx_layout.h:119
constexpr Position(const Point &pt)
Conversion from a single point to a Position.
Definition gfx_layout.h:128
int16_t left
Left-most position of glyph.
Definition gfx_layout.h:118
int16_t top
Top-most position of glyph.
Definition gfx_layout.h:120
Visual run contains data about the bit of text with the same font.
Definition gfx_layout.h:132
virtual std::span< const GlyphID > GetGlyphs() const =0
Get the glyphs to draw.
virtual int GetLeading() const =0
Get the font leading, or distance between the baselines of consecutive lines.
virtual std::span< const int > GetGlyphToCharMap() const =0
The offset for each of the glyphs to the character run that was passed to the Layouter.
virtual std::span< const Position > GetPositions() const =0
Get the positions for each of the glyphs.
virtual const Font * GetFont() const =0
Get the font.
virtual size_t GetGlyphCount() const =0
Get the number of glyphs.
Interface to glue fallback and normal layouter into one.
Definition gfx_layout.h:111
virtual std::unique_ptr< const Line > NextLine(int max_width)=0
Construct a new line with a maximum width.
virtual void Reflow()=0
Reset the position to the start of the paragraph.
Functions to read fonts from files and cache them.
Functions related to the gfx engine.
ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize=FS_NORMAL)
Get the leading corner of a character in a single-line string relative to the start of the string.
std::vector< std::pair< int, Font * > > FontMap
Mapping from index to font.
Definition gfx_layout.h:106
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.
FontSize
Available font sizes.
Definition gfx_type.h:248
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
@ TC_FORCED
Ignore colour changes from strings.
Definition gfx_type.h:332
@ TC_FLAGS_MASK
Mask to test if TextColour (with flags) is within limits.
Definition gfx_type.h:335
@ TC_COLOUR_MASK
Mask to test if TextColour (without flags) is within limits.
Definition gfx_type.h:334
Size limited cache map with a least recently used eviction strategy.
#define Point
Macro that prevents name conflicts between included headers.
Integer math functions.
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:58
void SetColour(TextColour c)
Switch to new colour c.
Definition gfx_layout.h:38
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition gfx_layout.h:48
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition gfx_layout.h:67
FontSize fontsize
Current font size.
Definition gfx_layout.h:25
std::vector< TextColour > colour_stack
Stack of colours to assist with colour switching.
Definition gfx_layout.h:27
TextColour cur_colour
Current text colour.
Definition gfx_layout.h:26
Item in the linecache.
Definition gfx_layout.h:259
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:264
Buffer buffer
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:263
int cached_width
Width used for the cached layout.
Definition gfx_layout.h:270
std::vector< std::unique_ptr< const ParagraphLayouter::Line > > cached_layout
Cached results of line layouting.
Definition gfx_layout.h:269
FontState state_after
Font state after the line.
Definition gfx_layout.h:266
std::unique_ptr< ParagraphLayouter > layout
Layout of the line.
Definition gfx_layout.h:267
Key into the linecache.
Definition gfx_layout.h:234
FontState state_before
Font state at the beginning of the line.
Definition gfx_layout.h:235
std::string str
Source string of the line (including colour and font size codes).
Definition gfx_layout.h:236
const FontState & state_before
Font state at the beginning of the line.
Definition gfx_layout.h:240
std::string_view str
Source string of the line (including colour and font size codes).
Definition gfx_layout.h:241