Codechange: Use null pointer literal instead of the NULL macro
[openttd-github.git] / src / gfx_layout.h
blob0539376473f4244530bd7dd8b9c69190c2857ed0
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file gfx_layout.h Functions related to laying out the texts. */
12 #ifndef GFX_LAYOUT_H
13 #define GFX_LAYOUT_H
15 #include "fontcache.h"
16 #include "gfx_func.h"
17 #include "core/smallmap_type.hpp"
19 #include <map>
20 #include <string>
21 #include <stack>
22 #include <vector>
24 #ifdef WITH_ICU_LX
25 #include "layout/ParagraphLayout.h"
26 #define ICU_FONTINSTANCE : public icu::LEFontInstance
27 #else /* WITH_ICU_LX */
28 #define ICU_FONTINSTANCE
29 #endif /* WITH_ICU_LX */
31 /**
32 * Text drawing parameters, which can change while drawing a line, but are kept between multiple parts
33 * of the same text, e.g. on line breaks.
35 struct FontState {
36 FontSize fontsize; ///< Current font size.
37 TextColour cur_colour; ///< Current text colour.
39 std::stack<TextColour, std::vector<TextColour>> colour_stack; ///< Stack of colours to assist with colour switching.
41 FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
42 FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour) {}
44 /**
45 * Switch to new colour \a c.
46 * @param c New colour to use.
48 inline void SetColour(TextColour c)
50 assert(c >= TC_BLUE && c <= TC_BLACK);
51 this->cur_colour = c;
54 /**
55 * Switch to and pop the last saved colour on the stack.
57 inline void PopColour()
59 if (colour_stack.empty()) return;
60 SetColour(colour_stack.top());
61 colour_stack.pop();
64 /**
65 * Push the current colour on to the stack.
67 inline void PushColour()
69 colour_stack.push(this->cur_colour);
72 /**
73 * Switch to using a new font \a f.
74 * @param f New font to use.
76 inline void SetFontSize(FontSize f)
78 this->fontsize = f;
82 /**
83 * Container with information about a font.
85 class Font ICU_FONTINSTANCE {
86 public:
87 FontCache *fc; ///< The font we are using.
88 TextColour colour; ///< The colour this font has to be.
90 Font(FontSize size, TextColour colour);
92 #ifdef WITH_ICU_LX
93 /* Implementation details of LEFontInstance */
95 le_int32 getUnitsPerEM() const;
96 le_int32 getAscent() const;
97 le_int32 getDescent() const;
98 le_int32 getLeading() const;
99 float getXPixelsPerEm() const;
100 float getYPixelsPerEm() const;
101 float getScaleFactorX() const;
102 float getScaleFactorY() const;
103 const void *getFontTable(LETag tableTag) const;
104 const void *getFontTable(LETag tableTag, size_t &length) const;
105 LEGlyphID mapCharToGlyph(LEUnicode32 ch) const;
106 void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const;
107 le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const;
108 #endif /* WITH_ICU_LX */
111 /** Mapping from index to font. */
112 typedef SmallMap<int, Font *> FontMap;
115 * Interface to glue fallback and normal layouter into one.
117 class ParagraphLayouter {
118 public:
119 virtual ~ParagraphLayouter() {}
121 /** Visual run contains data about the bit of text with the same font. */
122 class VisualRun {
123 public:
124 virtual ~VisualRun() {}
125 virtual const Font *GetFont() const = 0;
126 virtual int GetGlyphCount() const = 0;
127 virtual const GlyphID *GetGlyphs() const = 0;
128 virtual const float *GetPositions() const = 0;
129 virtual int GetLeading() const = 0;
130 virtual const int *GetGlyphToCharMap() const = 0;
133 /** A single line worth of VisualRuns. */
134 class Line {
135 public:
136 virtual ~Line() {}
137 virtual int GetLeading() const = 0;
138 virtual int GetWidth() const = 0;
139 virtual int CountRuns() const = 0;
140 virtual const VisualRun &GetVisualRun(int run) const = 0;
141 virtual int GetInternalCharLength(WChar c) const = 0;
144 virtual void Reflow() = 0;
145 virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
149 * The layouter performs all the layout work.
151 * It also accounts for the memory allocations and frees.
153 class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
154 const char *string; ///< Pointer to the original string.
156 /** Key into the linecache */
157 struct LineCacheKey {
158 FontState state_before; ///< Font state at the beginning of the line.
159 std::string str; ///< Source string of the line (including colour and font size codes).
161 /** Comparison operator for std::map */
162 bool operator<(const LineCacheKey &other) const
164 if (this->state_before.fontsize != other.state_before.fontsize) return this->state_before.fontsize < other.state_before.fontsize;
165 if (this->state_before.cur_colour != other.state_before.cur_colour) return this->state_before.cur_colour < other.state_before.cur_colour;
166 if (this->state_before.colour_stack != other.state_before.colour_stack) return this->state_before.colour_stack < other.state_before.colour_stack;
167 return this->str < other.str;
170 public:
171 /** Item in the linecache */
172 struct LineCacheItem {
173 /* Stuff that cannot be freed until the ParagraphLayout is freed */
174 void *buffer; ///< Accessed by both ICU's and our ParagraphLayout::nextLine.
175 FontMap runs; ///< Accessed by our ParagraphLayout::nextLine.
177 FontState state_after; ///< Font state after the line.
178 ParagraphLayouter *layout; ///< Layout of the line.
180 LineCacheItem() : buffer(nullptr), layout(nullptr) {}
181 ~LineCacheItem() { delete layout; free(buffer); }
183 private:
184 typedef std::map<LineCacheKey, LineCacheItem> LineCache;
185 static LineCache *linecache;
187 static LineCacheItem &GetCachedParagraphLayout(const char *str, size_t len, const FontState &state);
189 typedef SmallMap<TextColour, Font *> FontColourMap;
190 static FontColourMap fonts[FS_END];
191 public:
192 static Font *GetFont(FontSize size, TextColour colour);
194 Layouter(const char *str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
195 Dimension GetBounds();
196 Point GetCharPosition(const char *ch) const;
197 const char *GetCharAtPosition(int x) const;
199 static void ResetFontCache(FontSize size);
200 static void ResetLineCache();
201 static void ReduceLineCache();
204 #endif /* GFX_LAYOUT_H */