OpenTTD

gfx_layout.h
Go to the documentation of this file.
1 /* $Id: gfx_layout.h 27367 2015-08-09 12:33:27Z rubidium $ */
2
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 */
9
12 #ifndef GFX_LAYOUT_H
13 #define GFX_LAYOUT_H
14
15 #include "fontcache.h"
16 #include "gfx_func.h"
17 #include "core/smallmap_type.hpp"
18
19 #include <map>
20 #include <string>
21
22 #ifdef WITH_ICU_LAYOUT
23 #include "layout/ParagraphLayout.h"
24 #define ICU_FONTINSTANCE : public LEFontInstance
25 #else /* WITH_ICU_LAYOUT */
26 #define ICU_FONTINSTANCE
27 #endif /* WITH_ICU_LAYOUT */
28
33 struct FontState {
34 FontSize fontsize;
35 TextColour cur_colour;
36 TextColour prev_colour;
37
38 FontState() : fontsize(FS_END), cur_colour(TC_INVALID), prev_colour(TC_INVALID) {}
39 FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour), prev_colour(colour) {}
40
45 inline void SetColour(TextColour c)
46 {
47 assert(c >= TC_BLUE && c <= TC_BLACK);
48 this->prev_colour = this->cur_colour;
49 this->cur_colour = c;
50 }
51
53 inline void SetPreviousColour()
54 {
55 Swap(this->cur_colour, this->prev_colour);
56 }
57
62 inline void SetFontSize(FontSize f)
63 {
64 this->fontsize = f;
65 }
66 };
67
71 class Font ICU_FONTINSTANCE {
72 public:
73 FontCache *fc;
74 TextColour colour;
75
76 Font(FontSize size, TextColour colour);
77
78 #ifdef WITH_ICU_LAYOUT
79 /* Implementation details of LEFontInstance */
80
81 le_int32 getUnitsPerEM() const;
82 le_int32 getAscent() const;
83 le_int32 getDescent() const;
84 le_int32 getLeading() const;
85 float getXPixelsPerEm() const;
86 float getYPixelsPerEm() const;
87 float getScaleFactorX() const;
88 float getScaleFactorY() const;
89 const void *getFontTable(LETag tableTag) const;
90 const void *getFontTable(LETag tableTag, size_t &length) const;
91 LEGlyphID mapCharToGlyph(LEUnicode32 ch) const;
92 void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const;
93 le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const;
94 #endif /* WITH_ICU_LAYOUT */
95 };
96
98 typedef SmallMap<int, Font *> FontMap;
99
103 class ParagraphLayouter {
104 public:
105 virtual ~ParagraphLayouter() {}
106
108 class VisualRun {
109 public:
110 virtual ~VisualRun() {}
111 virtual const Font *GetFont() const = 0;
112 virtual int GetGlyphCount() const = 0;
113 virtual const GlyphID *GetGlyphs() const = 0;
114 virtual const float *GetPositions() const = 0;
115 virtual int GetLeading() const = 0;
116 virtual const int *GetGlyphToCharMap() const = 0;
117 };
118
120 class Line {
121 public:
122 virtual ~Line() {}
123 virtual int GetLeading() const = 0;
124 virtual int GetWidth() const = 0;
125 virtual int CountRuns() const = 0;
126 virtual const VisualRun *GetVisualRun(int run) const = 0;
127 virtual int GetInternalCharLength(WChar c) const = 0;
128 };
129
130 virtual void Reflow() = 0;
131 virtual const Line *NextLine(int max_width) = 0;
132 };
133
139 class Layouter : public AutoDeleteSmallVector<const ParagraphLayouter::Line *, 4> {
140 const char *string;
141
143 struct LineCacheKey {
144 FontState state_before;
145 std::string str;
146
148 bool operator<(const LineCacheKey &other) const
149 {
150 if (this->state_before.fontsize != other.state_before.fontsize) return this->state_before.fontsize < other.state_before.fontsize;
151 if (this->state_before.cur_colour != other.state_before.cur_colour) return this->state_before.cur_colour < other.state_before.cur_colour;
152 if (this->state_before.prev_colour != other.state_before.prev_colour) return this->state_before.prev_colour < other.state_before.prev_colour;
153 return this->str < other.str;
154 }
155 };
156 public:
158 struct LineCacheItem {
159 /* Stuff that cannot be freed until the ParagraphLayout is freed */
160 void *buffer;
161 FontMap runs;
162
163 FontState state_after;
164 ParagraphLayouter *layout;
165
166 LineCacheItem() : buffer(NULL), layout(NULL) {}
167 ~LineCacheItem() { delete layout; free(buffer); }
168 };
169 private:
170 typedef std::map<LineCacheKey, LineCacheItem> LineCache;
171 static LineCache *linecache;
172
173 static LineCacheItem &GetCachedParagraphLayout(const char *str, size_t len, const FontState &state);
174
175 typedef SmallMap<TextColour, Font *> FontColourMap;
176 static FontColourMap fonts[FS_END];
177 public:
178 static Font *GetFont(FontSize size, TextColour colour);
179
180 Layouter(const char *str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
181 Dimension GetBounds();
182 Point GetCharPosition(const char *ch) const;
183 const char *GetCharAtPosition(int x) const;
184
185 static void ResetFontCache(FontSize size);
186 static void ResetLineCache();
187 static void ReduceLineCache();
188 };
189
190 #endif /* GFX_LAYOUT_H */
ICU_FONTINSTANCE::colour
TextColour colour
The colour this font has to be.
Definition: gfx_layout.h:74
Swap
static void Swap(T &a, T &b)
Type safe swap operation.
Definition: math_func.hpp:277
Layouter::LineCacheItem::buffer
void * buffer
Accessed by both ICU&#39;s and our ParagraphLayout::nextLine.
Definition: gfx_layout.h:160
Layouter::linecache
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:171
FontState::prev_colour
TextColour prev_colour
Text colour from before the last colour switch.
Definition: gfx_layout.h:36
SmallMap< int, Font * >
Layouter::LineCacheKey::str
std::string str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:145
Layouter::LineCacheKey
Key into the linecache.
Definition: gfx_layout.h:143
FontState::SetPreviousColour
void SetPreviousColour()
Switch to previous colour.
Definition: gfx_layout.h:53
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:108
Layouter::LineCacheItem::runs
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:161
FontState::SetFontSize
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:62
smallmap_type.hpp
Simple mapping class targeted for small sets of data.
fontcache.h
Functions to read fonts from files and cache them.
ICU_FONTINSTANCE::fc
FontCache * fc
The font we are using.
Definition: gfx_layout.h:73
ParagraphLayouter::Line
A single line worth of VisualRuns.
Definition: gfx_layout.h:120
ParagraphLayouter
Interface to glue fallback and normal layouter into one.
Definition: gfx_layout.h:103
gfx_func.h
Functions related to the gfx engine.
AutoDeleteSmallVector
Simple vector template class, with automatic delete.
Definition: smallvec_type.hpp:401
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:247
FontState::SetColour
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:45
FontCache
Font cache for basic fonts.
Definition: fontcache.h:23
FontMap
SmallMap< int, Font * > FontMap
Mapping from index to font.
Definition: gfx_layout.h:98
FontState
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:33
FontState::cur_colour
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:35
FontState::fontsize
FontSize fontsize
Current font size.
Definition: gfx_layout.h:34
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:203
Layouter::LineCacheItem::state_after
FontState state_after
Font state after the line.
Definition: gfx_layout.h:163
FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:204
Layouter::LineCacheKey::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:144
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:28
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
ICU_FONTINSTANCE
Container with information about a font.
Definition: gfx_layout.h:71
Layouter::LineCacheKey::operator<
bool operator<(const LineCacheKey &other) const
Comparison operator for std::map.
Definition: gfx_layout.h:148
Layouter
The layouter performs all the layout work.
Definition: gfx_layout.h:139
GetCharAtPosition
const char * GetCharAtPosition(const char *str, int x, FontSize start_fontsize)
Get the character from a string that is drawn at a specific position.
Definition: gfx.cpp:740
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:19
Layouter::LineCacheItem::layout
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:164
WChar
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:34
Layouter::string
const char * string
Pointer to the original string.
Definition: gfx_layout.h:140
Layouter::LineCacheItem
Item in the linecache.
Definition: gfx_layout.h:158