Update: Translations from eints
[openttd-github.git] / src / gfx_layout.h
blobb400596c3e9813217b13e2d2d40d40f1c3599998
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 */
8 /** @file gfx_layout.h Functions related to laying out the texts. */
10 #ifndef GFX_LAYOUT_H
11 #define GFX_LAYOUT_H
13 #include "fontcache.h"
14 #include "gfx_func.h"
15 #include "core/math_func.hpp"
17 #include <stack>
18 #include <string_view>
20 /**
21 * Text drawing parameters, which can change while drawing a line, but are kept between multiple parts
22 * of the same text, e.g. on line breaks.
24 struct FontState {
25 FontSize fontsize; ///< Current font size.
26 TextColour cur_colour; ///< Current text colour.
28 std::stack<TextColour, std::vector<TextColour>> colour_stack; ///< Stack of colours to assist with colour switching.
30 FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
31 FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour) {}
33 /**
34 * Switch to new colour \a c.
35 * @param c New colour to use.
37 inline void SetColour(TextColour c)
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;
44 /**
45 * Switch to and pop the last saved colour on the stack.
47 inline void PopColour()
49 if (colour_stack.empty()) return;
50 SetColour(colour_stack.top());
51 colour_stack.pop();
54 /**
55 * Push the current colour on to the stack.
57 inline void PushColour()
59 colour_stack.push(this->cur_colour);
62 /**
63 * Switch to using a new font \a f.
64 * @param f New font to use.
66 inline void SetFontSize(FontSize f)
68 this->fontsize = f;
72 /**
73 * Container with information about a font.
75 class Font {
76 public:
77 FontCache *fc; ///< The font we are using.
78 TextColour colour; ///< The colour this font has to be.
80 Font(FontSize size, TextColour colour);
83 /** Mapping from index to font. The pointer is owned by FontColourMap. */
84 using FontMap = std::map<int, Font *>;
86 /**
87 * Interface to glue fallback and normal layouter into one.
89 class ParagraphLayouter {
90 public:
91 virtual ~ParagraphLayouter() = default;
93 /** Position of a glyph within a VisualRun. */
94 class Position {
95 public:
96 int16_t left; ///< Left-most position of glyph.
97 int16_t right; ///< Right-most position of glyph.
98 int16_t top; ///< Top-most position of glyph.
100 constexpr inline Position(int16_t left, int16_t right, int16_t top) : left(left), right(right), top(top) { }
102 /** Conversion from a single point to a Position. */
103 constexpr inline Position(const Point &pt) : left(pt.x), right(pt.x), top(pt.y) { }
106 /** Visual run contains data about the bit of text with the same font. */
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;
118 /** A single line worth of VisualRuns. */
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;
129 virtual void Reflow() = 0;
130 virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
134 * The layouter performs all the layout work.
136 * It also accounts for the memory allocations and frees.
138 class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
139 std::string_view string; ///< Pointer to the original string.
141 /** Key into the linecache */
142 struct LineCacheKey {
143 FontState state_before; ///< Font state at the beginning of the line.
144 std::string str; ///< Source string of the line (including colour and font size codes).
147 struct LineCacheQuery {
148 const FontState &state_before; ///< Font state at the beginning of the line.
149 std::string_view str; ///< Source string of the line (including colour and font size codes).
152 /** Comparator for std::map */
153 struct LineCacheCompare {
154 using is_transparent = void; ///< Enable map queries with various key types
156 /** Comparison operator for LineCacheKey and LineCacheQuery */
157 template<typename Key1, typename Key2>
158 bool operator()(const Key1 &lhs, const Key2 &rhs) const
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;
166 public:
167 /** Item in the linecache */
168 struct LineCacheItem {
169 /* Stuff that cannot be freed until the ParagraphLayout is freed */
170 void *buffer; ///< Accessed by our ParagraphLayout::nextLine.
171 FontMap runs; ///< Accessed by our ParagraphLayout::nextLine.
173 FontState state_after; ///< Font state after the line.
174 ParagraphLayouter *layout; ///< Layout of the line.
176 LineCacheItem() : buffer(nullptr), layout(nullptr) {}
177 ~LineCacheItem() { delete layout; free(buffer); }
179 private:
180 typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
181 static LineCache *linecache;
183 static LineCacheItem &GetCachedParagraphLayout(std::string_view str, const FontState &state);
185 using FontColourMap = std::map<TextColour, std::unique_ptr<Font>>;
186 static FontColourMap fonts[FS_END];
187 public:
188 static Font *GetFont(FontSize size, TextColour colour);
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;
195 static void Initialize();
196 static void ResetFontCache(FontSize size);
197 static void ResetLineCache();
198 static void ReduceLineCache();
201 ParagraphLayouter::Position GetCharPosInString(std::string_view str, const char *ch, FontSize start_fontsize = FS_NORMAL);
202 ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize = FS_NORMAL);
204 #endif /* GFX_LAYOUT_H */