OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

gfx_layout.h
Go to the documentation of this file.
1 /* $Id: gfx_layout.h 26029 2013-11-17 17:08:20Z michi_cc $ */
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
23 #include "layout/ParagraphLayout.h"
24 #define ICU_FONTINSTANCE : public LEFontInstance
25 #else /* WITH_ICU */
26 #define ICU_FONTINSTANCE
27 #endif /* WITH_ICU */
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
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 */
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 */