OpenTTD Source  13.2.1
gfx_layout.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #ifndef GFX_LAYOUT_H
11 #define GFX_LAYOUT_H
12 
13 #include "fontcache.h"
14 #include "gfx_func.h"
15 #include "core/smallmap_type.hpp"
16 
17 #include <map>
18 #include <string>
19 #include <stack>
20 #include <string_view>
21 #include <type_traits>
22 #include <vector>
23 
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 */
30 
35 struct FontState {
38 
39  std::stack<TextColour, std::vector<TextColour>> colour_stack;
40 
41  FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
43 
48  inline void SetColour(TextColour c)
49  {
50  assert((c & TC_COLOUR_MASK) >= TC_BLUE && (c & TC_COLOUR_MASK) <= TC_BLACK);
51  assert((c & (TC_COLOUR_MASK | TC_FLAGS_MASK)) == c);
52  if ((this->cur_colour & TC_FORCED) == 0) this->cur_colour = c;
53  }
54 
58  inline void PopColour()
59  {
60  if (colour_stack.empty()) return;
61  SetColour(colour_stack.top());
62  colour_stack.pop();
63  }
64 
68  inline void PushColour()
69  {
70  colour_stack.push(this->cur_colour);
71  }
72 
77  inline void SetFontSize(FontSize f)
78  {
79  this->fontsize = f;
80  }
81 };
82 
86 class Font ICU_FONTINSTANCE {
87 public:
90 
91  Font(FontSize size, TextColour colour);
92 
93 #ifdef WITH_ICU_LX
94  /* Implementation details of LEFontInstance */
95 
96  le_int32 getUnitsPerEM() const;
97  le_int32 getAscent() const;
98  le_int32 getDescent() const;
99  le_int32 getLeading() const;
100  float getXPixelsPerEm() const;
101  float getYPixelsPerEm() const;
102  float getScaleFactorX() const;
103  float getScaleFactorY() const;
104  const void *getFontTable(LETag tableTag) const;
105  const void *getFontTable(LETag tableTag, size_t &length) const;
106  LEGlyphID mapCharToGlyph(LEUnicode32 ch) const;
107  void getGlyphAdvance(LEGlyphID glyph, LEPoint &advance) const;
108  le_bool getGlyphPoint(LEGlyphID glyph, le_int32 pointNumber, LEPoint &point) const;
109 #endif /* WITH_ICU_LX */
110 };
111 
114 
119 public:
120  virtual ~ParagraphLayouter() {}
121 
123  class VisualRun {
124  public:
125  virtual ~VisualRun() {}
126  virtual const Font *GetFont() const = 0;
127  virtual int GetGlyphCount() const = 0;
128  virtual const GlyphID *GetGlyphs() const = 0;
129  virtual const float *GetPositions() const = 0;
130  virtual int GetLeading() const = 0;
131  virtual const int *GetGlyphToCharMap() const = 0;
132  };
133 
135  class Line {
136  public:
137  virtual ~Line() {}
138  virtual int GetLeading() const = 0;
139  virtual int GetWidth() const = 0;
140  virtual int CountRuns() const = 0;
141  virtual const VisualRun &GetVisualRun(int run) const = 0;
142  virtual int GetInternalCharLength(WChar c) const = 0;
143  };
144 
145  virtual void Reflow() = 0;
146  virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
147 };
148 
154 class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
155  const char *string;
156 
158  struct LineCacheKey {
160  std::string str;
161  };
162 
163  struct LineCacheQuery {
165  std::string_view str;
166  };
167 
170  using is_transparent = void;
171 
173  template<typename Key1, typename Key2>
174  bool operator()(const Key1 &lhs, const Key2 &rhs) const
175  {
176  if (lhs.state_before.fontsize != rhs.state_before.fontsize) return lhs.state_before.fontsize < rhs.state_before.fontsize;
177  if (lhs.state_before.cur_colour != rhs.state_before.cur_colour) return lhs.state_before.cur_colour < rhs.state_before.cur_colour;
178  if (lhs.state_before.colour_stack != rhs.state_before.colour_stack) return lhs.state_before.colour_stack < rhs.state_before.colour_stack;
179  return lhs.str < rhs.str;
180  }
181  };
182 public:
184  struct LineCacheItem {
185  /* Stuff that cannot be freed until the ParagraphLayout is freed */
186  void *buffer;
188 
191 
192  LineCacheItem() : buffer(nullptr), layout(nullptr) {}
193  ~LineCacheItem() { delete layout; free(buffer); }
194  };
195 private:
196  typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
197  static LineCache *linecache;
198 
199  static LineCacheItem &GetCachedParagraphLayout(const char *str, size_t len, const FontState &state);
200 
202  static FontColourMap fonts[FS_END];
203 public:
204  static Font *GetFont(FontSize size, TextColour colour);
205 
206  Layouter(const char *str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
208  Point GetCharPosition(const char *ch) const;
209  const char *GetCharAtPosition(int x) const;
210 
211  static void ResetFontCache(FontSize size);
212  static void ResetLineCache();
213  static void ReduceLineCache();
214 };
215 
216 #endif /* GFX_LAYOUT_H */
TC_FORCED
@ TC_FORCED
Ignore colour changes from strings.
Definition: gfx_type.h:278
GlyphID
uint32 GlyphID
Glyphs are characters from a font.
Definition: fontcache.h:17
Layouter::LineCacheKey
Key into the linecache.
Definition: gfx_layout.h:158
Layouter::LineCacheItem
Item in the linecache.
Definition: gfx_layout.h:184
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:36
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
Layouter::LineCacheQuery::str
std::string_view str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:165
Layouter::LineCacheCompare
Comparator for std::map.
Definition: gfx_layout.h:169
FontState::PopColour
void PopColour()
Switch to and pop the last saved colour on the stack.
Definition: gfx_layout.h:58
Layouter::string
const char * string
Pointer to the original string.
Definition: gfx_layout.h:155
Layouter::linecache
static LineCache * linecache
Cache of ParagraphLayout lines.
Definition: gfx_layout.h:197
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:253
Layouter::GetCharPosition
Point GetCharPosition(const char *ch) const
Get the position of a character in the layout.
Definition: gfx_layout.cpp:768
Layouter::ResetFontCache
static void ResetFontCache(FontSize size)
Reset cached font information.
Definition: gfx_layout.cpp:864
smallmap_type.hpp
ICU_FONTINSTANCE::fc
FontCache * fc
The font we are using.
Definition: gfx_layout.h:88
ParagraphLayouter
Interface to glue fallback and normal layouter into one.
Definition: gfx_layout.h:118
SmallMap< int, Font * >
Layouter::LineCacheItem::layout
ParagraphLayouter * layout
Layout of the line.
Definition: gfx_layout.h:190
Layouter::LineCacheCompare::is_transparent
void is_transparent
Enable map queries with various key types.
Definition: gfx_layout.h:170
FontMap
SmallMap< int, Font * > FontMap
Mapping from index to font.
Definition: gfx_layout.h:113
gfx_func.h
ParagraphLayouter::Line
A single line worth of VisualRuns.
Definition: gfx_layout.h:135
Layouter::LineCacheKey::str
std::string str
Source string of the line (including colour and font size codes).
Definition: gfx_layout.h:160
ICU_FONTINSTANCE
Container with information about a font.
Definition: gfx_layout.h:86
Layouter::fonts
static FontColourMap fonts[FS_END]
Cache of Font instances.
Definition: gfx_layout.h:202
Layouter::LineCacheKey::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:159
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:203
Layouter::LineCacheItem::runs
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Definition: gfx_layout.h:187
FontState::fontsize
FontSize fontsize
Current font size.
Definition: gfx_layout.h:36
Layouter::LineCacheCompare::operator()
bool operator()(const Key1 &lhs, const Key2 &rhs) const
Comparison operator for LineCacheKey and LineCacheQuery.
Definition: gfx_layout.h:174
Layouter::GetBounds
Dimension GetBounds()
Get the boundaries of this paragraph.
Definition: gfx_layout.cpp:752
Layouter::Layouter
Layouter(const char *str, int maxw=INT32_MAX, TextColour colour=TC_FROMSTRING, FontSize fontsize=FS_NORMAL)
Create a new layouter.
Definition: gfx_layout.cpp:673
Layouter
The layouter performs all the layout work.
Definition: gfx_layout.h:154
Layouter::LineCacheQuery::state_before
FontState state_before
Font state at the beginning of the line.
Definition: gfx_layout.h:164
Layouter::LineCacheQuery
Definition: gfx_layout.h:163
Layouter::GetFont
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
Definition: gfx_layout.cpp:850
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
Layouter::ReduceLineCache
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
Definition: gfx_layout.cpp:920
ParagraphLayouter::VisualRun
Visual run contains data about the bit of text with the same font.
Definition: gfx_layout.h:123
FontState::colour_stack
std::stack< TextColour, std::vector< TextColour > > colour_stack
Stack of colours to assist with colour switching.
Definition: gfx_layout.h:39
FontState::cur_colour
TextColour cur_colour
Current text colour.
Definition: gfx_layout.h:37
FontCache
Font cache for basic fonts.
Definition: fontcache.h:21
Layouter::GetCharAtPosition
const char * GetCharAtPosition(int x) const
Get the character that is at a position.
Definition: gfx_layout.cpp:815
FontState::SetColour
void SetColour(TextColour c)
Switch to new colour c.
Definition: gfx_layout.h:48
TC_FLAGS_MASK
@ TC_FLAGS_MASK
Mask to test if TextColour (with flags) is within limits.
Definition: gfx_type.h:281
Layouter::LineCacheItem::state_after
FontState state_after
Font state after the line.
Definition: gfx_layout.h:189
Layouter::GetCachedParagraphLayout
static LineCacheItem & GetCachedParagraphLayout(const char *str, size_t len, const FontState &state)
Get reference to cache item.
Definition: gfx_layout.cpp:890
FontState::SetFontSize
void SetFontSize(FontSize f)
Switch to using a new font f.
Definition: gfx_layout.h:77
FontState
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
Definition: gfx_layout.h:35
Layouter::ResetLineCache
static void ResetLineCache()
Clear line cache.
Definition: gfx_layout.cpp:912
fontcache.h
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:202
TC_COLOUR_MASK
@ TC_COLOUR_MASK
Mask to test if TextColour (without flags) is within limits.
Definition: gfx_type.h:280
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
FontState::PushColour
void PushColour()
Push the current colour on to the stack.
Definition: gfx_layout.h:68
ICU_FONTINSTANCE::colour
TextColour colour
The colour this font has to be.
Definition: gfx_layout.h:89
Layouter::LineCacheItem::buffer
void * buffer
Accessed by both ICU's and our ParagraphLayout::nextLine.
Definition: gfx_layout.h:186