OpenTTD Source 20261112-master-gbcbec5e63f
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
16#include <string_view>
17
21class Font {
22public:
25
30 constexpr auto operator<=>(const Font &) const = default;
31
36 inline FontCache &GetFontCache() const { return *FontCache::Get(this->fontsize); }
37};
38
43class FontState : public Font {
44public:
45 std::vector<ExtendedTextColour> colour_stack;
46
48 FontState() = default;
49
56
61 auto operator<=>(const FontState &) const = default;
62
68 {
71 if (!this->colour.flags.Test(ExtendedTextColourFlag::Forced)) this->colour = c;
72 }
73
77 inline void PopColour()
78 {
79 if (colour_stack.empty()) return;
80 SetColour(colour_stack.back());
81 colour_stack.pop_back();
82 }
83
87 inline void PushColour()
88 {
89 colour_stack.push_back(this->colour);
90 }
91
96 inline void SetFontSize(FontSize f)
97 {
98 this->fontsize = f;
99 }
100};
101
102template <typename T> struct std::hash<std::vector<T>> {
103 size_t operator()(const std::vector<T> &vec) const
104 {
105 /* This is not an optimal hash algorithm but in most cases this is empty and therefore the same anyway. */
106 return std::transform_reduce(std::begin(vec), std::end(vec),
107 std::hash<size_t>{}(std::size(vec)),
108 [](const size_t &a, const size_t &b) -> size_t { return a ^ b; },
109 [](const T &x) -> size_t { return std::hash<T>{}(x); });
110 }
111};
112
114template <> struct std::hash<ExtendedTextColour> {
120 std::size_t operator()(const ExtendedTextColour &tc) const noexcept
121 {
122 size_t h1 = std::hash<TextColour>{}(tc.colour);
123 size_t h2 = std::hash<uint8_t>{}(tc.flags.base());
124 return h1 ^ (h2 << 1);
125 }
126};
127
128template <> struct std::hash<FontState> {
129 std::size_t operator()(const FontState &state) const noexcept
130 {
131 size_t h1 = std::hash<FontSize>{}(state.fontsize);
132 size_t h2 = std::hash<ExtendedTextColour>{}(state.colour);
133 size_t h3 = std::hash<std::vector<ExtendedTextColour>>{}(state.colour_stack);
134 return h1 ^ (h2 << 1) ^ (h3 << 2);
135 }
136};
137
139using FontMap = std::vector<std::pair<int, Font>>;
140
145public:
147 virtual ~ParagraphLayouter() = default;
148
150 class Position {
151 public:
152 int16_t left;
153 int16_t right;
154 int16_t top;
155
156 constexpr Position(int16_t left, int16_t right, int16_t top) : left(left), right(right), top(top) { }
157
162 constexpr Position(const Point &pt) : left(pt.x), right(pt.x), top(pt.y) { }
163 };
164
166 class VisualRun {
167 public:
169 virtual ~VisualRun() = default;
170
175 virtual const Font &GetFont() const = 0;
176
181 virtual size_t GetGlyphCount() const = 0;
182
187 virtual std::span<const GlyphID> GetGlyphs() const = 0;
188
193 virtual std::span<const Position> GetPositions() const = 0;
194
199 virtual int GetLeading() const = 0;
200
205 virtual std::span<const int> GetGlyphToCharMap() const = 0;
206 };
207
209 class Line {
210 public:
212 virtual ~Line() = default;
213
218 virtual int GetLeading() const = 0;
219
224 virtual int GetWidth() const = 0;
225
230 virtual size_t CountRuns() const = 0;
231
237 virtual const VisualRun &GetVisualRun(size_t run) const = 0;
238
245 virtual int GetInternalCharLength(char32_t c) const = 0;
246 };
247
251 virtual void Reflow() = 0;
252
258 virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
259};
260
266class Layouter : public std::vector<const ParagraphLayouter::Line *> {
267 std::string_view string;
268
272 std::string str;
273 };
274
277 std::string_view str;
278 };
279
280 friend struct std::hash<Layouter::LineCacheQuery>;
281 struct LineCacheHash;
282
284 using is_transparent = void;
285
286 template <typename Tlhs, typename Trhs>
287 bool operator()(const Tlhs &lhs, const Trhs &rhs) const
288 {
289 return lhs.state_before == rhs.state_before && lhs.str == rhs.str;
290 }
291 };
292
293public:
297 using Buffer = std::unique_ptr<void, void(*)(void *)>;
298 /* Stuff that cannot be freed until the ParagraphLayout is freed */
299 Buffer buffer{nullptr, [](void *){}};
301
303 std::unique_ptr<ParagraphLayouter> layout = nullptr;
304
305 std::vector<std::unique_ptr<const ParagraphLayouter::Line>> cached_layout{};
306 int cached_width = 0;
307 };
308private:
310 static std::unique_ptr<LineCache> linecache;
311
312 static LineCacheItem &GetCachedParagraphLayout(std::string_view str, const FontState &state);
313
314public:
315 Layouter(std::string_view str, int maxw = INT32_MAX, FontSize fontsize = FontSize::Normal);
317 ParagraphLayouter::Position GetCharPosition(std::string_view::const_iterator ch) const;
318 ptrdiff_t GetCharAtPosition(int x, size_t line_index) const;
319
320 static void Initialize();
321 static void ResetFontCache(FontSize size);
322 static void ResetLineCache();
323};
324
325ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize = FontSize::Normal);
326ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize = FontSize::Normal);
327
328template <> struct std::hash<Layouter::LineCacheQuery> {
329 std::size_t operator()(const Layouter::LineCacheQuery &state) const noexcept
330 {
331 size_t h1 = std::hash<std::string_view>{}(state.str);
332 size_t h2 = std::hash<FontState>{}(state.state_before);
333 return h1 ^ (h2 << 1);
334 }
335};
336
338 using is_transparent = void;
339
340 std::size_t operator()(const Layouter::LineCacheKey &query) const { return std::hash<Layouter::LineCacheQuery>{}(LineCacheQuery{query.state_before, query.str}); }
341 std::size_t operator()(const Layouter::LineCacheQuery &query) const { return std::hash<Layouter::LineCacheQuery>{}(query); }
342};
343
344#endif /* GFX_LAYOUT_H */
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Font cache for basic fonts.
Definition fontcache.h:23
static FontCache * Get(FontSize fs)
Get the font cache of a given font size.
Definition fontcache.h:136
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition gfx_layout.h:43
FontState(FontSize fontsize, ExtendedTextColour colour)
Create the font state.
Definition gfx_layout.h:55
std::vector< ExtendedTextColour > colour_stack
Stack of colours to assist with colour switching.
Definition gfx_layout.h:45
FontState()=default
Create the font state with an invalid state.
void PushColour()
Push the current colour on to the stack.
Definition gfx_layout.h:87
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition gfx_layout.h:77
void SetColour(ExtendedTextColour c)
Switch to new colour c.
Definition gfx_layout.h:67
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition gfx_layout.h:96
auto operator<=>(const FontState &) const =default
Compare with another instance of this class.
Container with information about a font.
Definition gfx_layout.h:21
ExtendedTextColour colour
The text colour.
Definition gfx_layout.h:24
constexpr auto operator<=>(const Font &) const =default
Compare with another instance of this class.
FontCache & GetFontCache() const
Get the font cache of this font.
Definition gfx_layout.h:36
FontSize fontsize
The font size.
Definition gfx_layout.h:23
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:266
Layouter(std::string_view str, int maxw=INT32_MAX, FontSize fontsize=FontSize::Normal)
Create a new layouter.
static void Initialize()
Perform initialization of layout engine.
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:310
std::string_view string
Pointer to the original string.
Definition gfx_layout.h:267
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:209
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 ~Line()=default
Ensure the destructor of the sub classes are called as well.
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:150
int16_t right
Right-most position of glyph.
Definition gfx_layout.h:153
constexpr Position(const Point &pt)
Conversion from a single point to a Position.
Definition gfx_layout.h:162
int16_t left
Left-most position of glyph.
Definition gfx_layout.h:152
int16_t top
Top-most position of glyph.
Definition gfx_layout.h:154
Visual run contains data about the bit of text with the same font.
Definition gfx_layout.h:166
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 size_t GetGlyphCount() const =0
Get the number of glyphs.
virtual ~VisualRun()=default
Ensure the destructor of the sub classes are called as well.
virtual const Font & GetFont() const =0
Get the font.
Interface to glue fallback and normal layouter into one.
Definition gfx_layout.h:144
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.
virtual ~ParagraphLayouter()=default
Ensure the destructor of the sub classes are called as well.
#define T
Climate temperate.
Definition engines.h:91
Functions to read fonts from files and cache them.
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize=FontSize::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:139
ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize=FontSize::Normal)
Get the leading corner of a character in a single-line string relative to the start of the string.
FontSize
Available font sizes.
Definition gfx_type.h:248
@ End
Marker for the end of the enumerations.
Definition gfx_type.h:254
@ Normal
Index of the normal font in the font tables.
Definition gfx_type.h:249
@ IsPaletteColour
Colour value is already a real palette colour index, not an index of a StringColour.
Definition gfx_type.h:341
@ Forced
Ignore colour changes from strings.
Definition gfx_type.h:343
@ Begin
Marker for the begin of the range.
Definition gfx_type.h:316
@ Invalid
Invalid colour.
Definition gfx_type.h:336
@ End
Marker for the end of the range.
Definition gfx_type.h:335
Size limited cache map with a least recently used eviction strategy.
#define Point
Macro that prevents name conflicts between included headers.
Dimensions (a width and height) of a rectangle in 2D.
Container for the text colour and some text colour related flags for drawing.
Definition gfx_type.h:349
ExtendedTextColourFlags flags
The flags.
Definition gfx_type.h:364
TextColour colour
The colour.
Definition gfx_type.h:363
Item in the linecache.
Definition gfx_layout.h:295
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:300
Buffer buffer
Accessed by our ParagraphLayout::nextLine.
Definition gfx_layout.h:299
int cached_width
Width used for the cached layout.
Definition gfx_layout.h:306
std::vector< std::unique_ptr< const ParagraphLayouter::Line > > cached_layout
Cached results of line layouting.
Definition gfx_layout.h:305
FontState state_after
Font state after the line.
Definition gfx_layout.h:302
std::unique_ptr< void, void(*)(void *)> Buffer
Due to the type of data in the buffer differing depending on the Layouter, we need to pass our own de...
Definition gfx_layout.h:297
std::unique_ptr< ParagraphLayouter > layout
Layout of the line.
Definition gfx_layout.h:303
Key into the linecache.
Definition gfx_layout.h:270
FontState state_before
Font state at the beginning of the line.
Definition gfx_layout.h:271
std::string str
Source string of the line (including colour and font size codes).
Definition gfx_layout.h:272
const FontState & state_before
Font state at the beginning of the line.
Definition gfx_layout.h:276
std::string_view str
Source string of the line (including colour and font size codes).
Definition gfx_layout.h:277
std::size_t operator()(const ExtendedTextColour &tc) const noexcept
Hash the text colour.
Definition gfx_layout.h:120