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 
23 #ifdef WITH_ICU_LAYOUT
24 #include "layout/ParagraphLayout.h"
25 #define ICU_FONTINSTANCE : public icu::LEFontInstance
26 #else /* WITH_ICU_LAYOUT */
27 #define ICU_FONTINSTANCE
28 #endif /* WITH_ICU_LAYOUT */
29 
34 struct FontState {
37 
38  std::stack<TextColour> colour_stack;
39 
40  FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
41  FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour) {}
42 
47  inline void SetColour(TextColour c)
48  {
49  assert(c >= TC_BLUE && c <= TC_BLACK);
50  this->cur_colour = c;
51  }
52 
56  inline void PopColour()
57  {
58  if (colour_stack.empty()) return;
59  SetColour(colour_stack.top());
60  colour_stack.pop();
61  }
62 
66  inline void PushColour()
67  {
68  colour_stack.push(this->cur_colour);
69  }
70 
75  inline void SetFontSize(FontSize f)
76  {
77  this->fontsize = f;
78  }
79 };
80 
84 class Font ICU_FONTINSTANCE {
85 public:
88 
89  Font(FontSize size, TextColour colour);
90 
91 #ifdef WITH_ICU_LAYOUT
92  /* Implementation details of LEFontInstance */
93 
94  le_int32 getUnitsPerEM() const;
95  le_int32 getAscent() const;
96  le_int32 getDescent() const;
97  le_int32 getLeading() const;
98  float getXPixelsPerEm() const;
99  float getYPixelsPerEm() const;
100  float getScaleFactorX() const;
101  float getScaleFactorY() const;
102  const void *getFontTable(LETag tableTag) const;
103  const void *getFontTable(LETag tableTag, size_t &length) const;
104  LEGlyphID mapCharToGlyph(LEUnicode32 ch) const;
105  void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const;
106  le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const;
107 #endif /* WITH_ICU_LAYOUT */
108 };
109 
112 
117 public:
118  virtual ~ParagraphLayouter() {}
119 
121  class VisualRun {
122  public:
123  virtual ~VisualRun() {}
124  virtual const Font *GetFont() const = 0;
125  virtual int GetGlyphCount() const = 0;
126  virtual const GlyphID *GetGlyphs() const = 0;
127  virtual const float *GetPositions() const = 0;
128  virtual int GetLeading() const = 0;
129  virtual const int *GetGlyphToCharMap() const = 0;
130  };
131 
133  class Line {
134  public:
135  virtual ~Line() {}
136  virtual int GetLeading() const = 0;
137  virtual int GetWidth() const = 0;
138  virtual int CountRuns() const = 0;
139  virtual const VisualRun *GetVisualRun(int run) const = 0;
140  virtual int GetInternalCharLength(WChar c) const = 0;
141  };
142 
143  virtual void Reflow() = 0;
144  virtual const Line *NextLine(int max_width) = 0;
145 };
146 
152 class Layouter : public AutoDeleteSmallVector<const ParagraphLayouter::Line *, 4> {
153  const char *string;
154 
156  struct LineCacheKey {
158  std::string str;
159 
161  bool operator<(const LineCacheKey &other) const
162  {
163  if (this->state_before.fontsize != other.state_before.fontsize) return this->state_before.fontsize < other.state_before.fontsize;
164  if (this->state_before.cur_colour != other.state_before.cur_colour) return this->state_before.cur_colour < other.state_before.cur_colour;
165  if (this->state_before.colour_stack != other.state_before.colour_stack) return this->state_before.colour_stack < other.state_before.colour_stack;
166  return this->str < other.str;
167  }
168  };
169 public:
171  struct LineCacheItem {
172  /* Stuff that cannot be freed until the ParagraphLayout is freed */
173  void *buffer;
175 
178 
179  LineCacheItem() : buffer(NULL), layout(NULL) {}
180  ~LineCacheItem() { delete layout; free(buffer); }
181  };
182 private:
183  typedef std::map<LineCacheKey, LineCacheItem> LineCache;
184  static LineCache *linecache;
185 
186  static LineCacheItem &GetCachedParagraphLayout(const char *str, size_t len, const FontState &state);
187 
189  static FontColourMap fonts[FS_END];
190 public:
191  static Font *GetFont(FontSize size, TextColour colour);
192 
193  Layouter(const char *str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
194  Dimension GetBounds();
195  Point GetCharPosition(const char *ch) const;
196  const char *GetCharAtPosition(int x) const;
197 
198  static void ResetFontCache(FontSize size);
199  static void ResetLineCache();
200  static void ReduceLineCache();
201 };
202 
203 #endif /* GFX_LAYOUT_H */
TextColour colour
The colour this font has to be.
Definition: gfx_layout.h:87
std::stack< TextColour > colour_stack
Stack of colours to assist with colour switching.
Definition: gfx_layout.h:38
void * buffer
Accessed by both ICU&#39;s and our ParagraphLayout::nextLine.
Definition: gfx_layout.h:173
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:184
std::string str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:158
Key into the linecache.
Definition: gfx_layout.h:156
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:121
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:174
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:75
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:86
A single line worth of VisualRuns.
Definition: gfx_layout.h:133
Interface to glue fallback and normal layouter into one.
Definition: gfx_layout.h:116
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:47
Font cache for basic fonts.
Definition: fontcache.h:23
SmallMap< int, Font * > FontMap
Mapping from index to font.
Definition: gfx_layout.h:111
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:34
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:56
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:36
FontSize fontsize
Current font size.
Definition: gfx_layout.h:35
FontSize
Available font sizes.
Definition: gfx_type.h:203
FontState state_after
Font state after the line.
Definition: gfx_layout.h:176
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:66
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:157
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:84
bool operator<(const LineCacheKey &other) const
Comparison operator for std::map.
Definition: gfx_layout.h:161
The layouter performs all the layout work.
Definition: gfx_layout.h:152
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:177
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:153
Item in the linecache.
Definition: gfx_layout.h:171