OpenTTD
gfx_layout.h
Go to the documentation of this file.
1 /* $Id$ */
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 #include <stack>
22 #include <vector>
23 
24 #ifdef WITH_ICU_LAYOUT
25 #include "layout/ParagraphLayout.h"
26 #define ICU_FONTINSTANCE : public icu::LEFontInstance
27 #else /* WITH_ICU_LAYOUT */
28 #define ICU_FONTINSTANCE
29 #endif /* WITH_ICU_LAYOUT */
30 
35 struct FontState {
38 
39  std::stack<TextColour, std::vector<TextColour>> colour_stack;
40 
41  FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
42  FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour) {}
43 
48  inline void SetColour(TextColour c)
49  {
50  assert(c >= TC_BLUE && c <= TC_BLACK);
51  this->cur_colour = c;
52  }
53 
57  inline void PopColour()
58  {
59  if (colour_stack.empty()) return;
60  SetColour(colour_stack.top());
61  colour_stack.pop();
62  }
63 
67  inline void PushColour()
68  {
69  colour_stack.push(this->cur_colour);
70  }
71 
76  inline void SetFontSize(FontSize f)
77  {
78  this->fontsize = f;
79  }
80 };
81 
85 class Font ICU_FONTINSTANCE {
86 public:
89 
90  Font(FontSize size, TextColour colour);
91 
92 #ifdef WITH_ICU_LAYOUT
93  /* Implementation details of LEFontInstance */
94 
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_LAYOUT */
109 };
110 
113 
118 public:
119  virtual ~ParagraphLayouter() {}
120 
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;
131  };
132 
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;
142  };
143 
144  virtual void Reflow() = 0;
145  virtual const Line *NextLine(int max_width) = 0;
146 };
147 
153 class Layouter : public AutoDeleteSmallVector<const ParagraphLayouter::Line *, 4> {
154  const char *string;
155 
157  struct LineCacheKey {
159  std::string str;
160 
162  bool operator<(const LineCacheKey &other) const
163  {
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;
168  }
169  };
170 public:
172  struct LineCacheItem {
173  /* Stuff that cannot be freed until the ParagraphLayout is freed */
174  void *buffer;
176 
179 
180  LineCacheItem() : buffer(NULL), layout(NULL) {}
181  ~LineCacheItem() { delete layout; free(buffer); }
182  };
183 private:
184  typedef std::map<LineCacheKey, LineCacheItem> LineCache;
185  static LineCache *linecache;
186 
187  static LineCacheItem &GetCachedParagraphLayout(const char *str, size_t len, const FontState &state);
188 
190  static FontColourMap fonts[FS_END];
191 public:
192  static Font *GetFont(FontSize size, TextColour colour);
193 
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;
198 
199  static void ResetFontCache(FontSize size);
200  static void ResetLineCache();
201  static void ReduceLineCache();
202 };
203 
204 #endif /* GFX_LAYOUT_H */
TextColour colour
The colour this font has to be.
Definition: gfx_layout.h:88
void * buffer
Accessed by both ICU&#39;s and our ParagraphLayout::nextLine.
Definition: gfx_layout.h:174
std::stack< TextColour, std::vector< TextColour > > colour_stack
Stack of colours to assist with colour switching.
Definition: gfx_layout.h:39
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:185
std::string str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:159
Key into the linecache.
Definition: gfx_layout.h:157
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:122
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:175
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:76
Simple mapping class targeted for small sets of data.
Functions to read fonts from files and cache them.
FontCache * fc
The font we are using.
Definition: gfx_layout.h:87
A single line worth of VisualRuns.
Definition: gfx_layout.h:134
Interface to glue fallback and normal layouter into one.
Definition: gfx_layout.h:117
Functions related to the gfx engine.
Simple vector template class, with automatic delete.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:247
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:48
Font cache for basic fonts.
Definition: fontcache.h:23
SmallMap< int, Font * > FontMap
Mapping from index to font.
Definition: gfx_layout.h:112
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:35
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:57
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:37
FontSize fontsize
Current font size.
Definition: gfx_layout.h:36
FontSize
Available font sizes.
Definition: gfx_type.h:203
FontState state_after
Font state after the line.
Definition: gfx_layout.h:177
Index of the normal font in the font tables.
Definition: gfx_type.h:204
void PushColour()
Push the current colour on to the stack.
Definition: gfx_layout.h:67
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:158
Coordinates of a point in 2D.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
Container with information about a font.
Definition: gfx_layout.h:85
bool operator<(const LineCacheKey &other) const
Comparison operator for std::map.
Definition: gfx_layout.h:162
The layouter performs all the layout work.
Definition: gfx_layout.h:153
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:741
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:19
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:178
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
Dimensions (a width and height) of a rectangle in 2D.
const char * string
Pointer to the original string.
Definition: gfx_layout.h:154
Item in the linecache.
Definition: gfx_layout.h:172