1 /* $Id: gfx_layout.h 26029 2013-11-17 17:08:20Z michi_cc $ */
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/>.
10 /** @file gfx_layout.h Functions related to laying out the texts. */
15 #include "fontcache.h"
17 #include "core/smallmap_type.hpp"
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 */
30 * Text drawing parameters, which can change while drawing a line, but are kept between multiple parts
31 * of the same text, e.g. on line breaks.
34 FontSize fontsize
; ///< Current font size.
35 TextColour cur_colour
; ///< Current text colour.
36 TextColour prev_colour
; ///< Text colour from before the last colour switch.
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
) {}
42 * Switch to new colour \a c.
43 * @param c New colour to use.
45 inline void SetColour(TextColour c
)
47 assert(c
>= TC_BLUE
&& c
<= TC_BLACK
);
48 this->prev_colour
= this->cur_colour
;
52 /** Switch to previous colour. */
53 inline void SetPreviousColour()
55 Swap(this->cur_colour
, this->prev_colour
);
59 * Switch to using a new font \a f.
60 * @param f New font to use.
62 inline void SetFontSize(FontSize f
)
69 * Container with information about a font.
71 class Font ICU_FONTINSTANCE
{
73 FontCache
*fc
; ///< The font we are using.
74 TextColour colour
; ///< The colour this font has to be.
76 Font(FontSize size
, TextColour colour
);
78 #ifdef WITH_ICU_LAYOUT
79 /* Implementation details of LEFontInstance */
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 */
97 /** Mapping from index to font. */
98 typedef SmallMap
<int, Font
*> FontMap
;
101 * Interface to glue fallback and normal layouter into one.
103 class ParagraphLayouter
{
105 virtual ~ParagraphLayouter() {}
107 /** Visual run contains data about the bit of text with the same font. */
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;
119 /** A single line worth of VisualRuns. */
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;
130 virtual void Reflow() = 0;
131 virtual const Line
*NextLine(int max_width
) = 0;
135 * The layouter performs all the layout work.
137 * It also accounts for the memory allocations and frees.
139 class Layouter
: public AutoDeleteSmallVector
<const ParagraphLayouter::Line
*, 4> {
140 const char *string
; ///< Pointer to the original string.
142 /** Key into the linecache */
143 struct LineCacheKey
{
144 FontState state_before
; ///< Font state at the beginning of the line.
145 std::string str
; ///< Source string of the line (including colour and font size codes).
147 /** Comparison operator for std::map */
148 bool operator<(const LineCacheKey
&other
) const
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
;
157 /** Item in the linecache */
158 struct LineCacheItem
{
159 /* Stuff that cannot be freed until the ParagraphLayout is freed */
160 void *buffer
; ///< Accessed by both ICU's and our ParagraphLayout::nextLine.
161 FontMap runs
; ///< Accessed by our ParagraphLayout::nextLine.
163 FontState state_after
; ///< Font state after the line.
164 ParagraphLayouter
*layout
; ///< Layout of the line.
166 LineCacheItem() : buffer(NULL
), layout(NULL
) {}
167 ~LineCacheItem() { delete layout
; free(buffer
); }
170 typedef std::map
<LineCacheKey
, LineCacheItem
> LineCache
;
171 static LineCache
*linecache
;
173 static LineCacheItem
&GetCachedParagraphLayout(const char *str
, size_t len
, const FontState
&state
);
175 typedef SmallMap
<TextColour
, Font
*> FontColourMap
;
176 static FontColourMap fonts
[FS_END
];
178 static Font
*GetFont(FontSize size
, TextColour colour
);
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;
185 static void ResetFontCache(FontSize size
);
186 static void ResetLineCache();
187 static void ReduceLineCache();
190 #endif /* GFX_LAYOUT_H */