Files
@ r28541:42e13f513738
Branch filter:
Location: cpp/openttd-patchpack/source/src/gfx_layout.h - annotation
r28541:42e13f513738
6.1 KiB
text/x-c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r27384:b919ab98bb53 r20450:002c8057d672 r22818:dc0401fd5990 r25898:e57f843dc921 r20544:29afe601d5e7 r20450:002c8057d672 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r22818:dc0401fd5990 r23375:ca293b1be7a0 r20541:b3cc03065e98 r22818:dc0401fd5990 r22818:dc0401fd5990 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r27185:77a5797bb16b r27185:77a5797bb16b r23984:68c55dd06ad8 r20541:b3cc03065e98 r20541:b3cc03065e98 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r20541:b3cc03065e98 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r22818:dc0401fd5990 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20541:b3cc03065e98 r20450:002c8057d672 r20450:002c8057d672 r27221:3735cc2cef6b r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r27385:dea7924e367a r27384:b919ab98bb53 r20450:002c8057d672 r20450:002c8057d672 r20952:c84424569e57 r20450:002c8057d672 r20952:c84424569e57 r20450:002c8057d672 r27372:06d384d76bd2 r20450:002c8057d672 r20952:c84424569e57 r20952:c84424569e57 r20450:002c8057d672 r27372:06d384d76bd2 r20953:64db27cf67a7 r20953:64db27cf67a7 r28510:44c3f063764d r28511:2d7264916867 r20953:64db27cf67a7 r28510:44c3f063764d r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20952:c84424569e57 r20450:002c8057d672 r27372:06d384d76bd2 r20953:64db27cf67a7 r20953:64db27cf67a7 r20953:64db27cf67a7 r23598:7da4fa077a86 r27737:728d55b97775 r20450:002c8057d672 r20450:002c8057d672 r20953:64db27cf67a7 r23599:b4bf61c1f787 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 r23599:b4bf61c1f787 r27288:52957325547f r20613:8e39d5a2a85b r20544:29afe601d5e7 r20544:29afe601d5e7 r20544:29afe601d5e7 r20544:29afe601d5e7 r25898:e57f843dc921 r20544:29afe601d5e7 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r20544:29afe601d5e7 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r25898:e57f843dc921 r20544:29afe601d5e7 r20544:29afe601d5e7 r20952:c84424569e57 r20544:29afe601d5e7 r20544:29afe601d5e7 r20544:29afe601d5e7 r27221:3735cc2cef6b r20952:c84424569e57 r20544:29afe601d5e7 r20951:cea059b83159 r20951:cea059b83159 r20544:29afe601d5e7 r23607:36c15679007d r20952:c84424569e57 r20544:29afe601d5e7 r20952:c84424569e57 r25898:e57f843dc921 r20546:b6881fee3922 r20544:29afe601d5e7 r27288:52957325547f r20543:89cc7ba70ae8 r27385:dea7924e367a r20543:89cc7ba70ae8 r20952:c84424569e57 r20543:89cc7ba70ae8 r20450:002c8057d672 r27288:52957325547f r20450:002c8057d672 r27288:52957325547f r27901:d0a1e7017e52 r20543:89cc7ba70ae8 r20543:89cc7ba70ae8 r20544:29afe601d5e7 r20544:29afe601d5e7 r20450:002c8057d672 r20450:002c8057d672 r20450:002c8057d672 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file gfx_layout.h Functions related to laying out the texts. */
#ifndef GFX_LAYOUT_H
#define GFX_LAYOUT_H
#include "fontcache.h"
#include "gfx_func.h"
#include "core/math_func.hpp"
#include <stack>
#include <string_view>
/**
* Text drawing parameters, which can change while drawing a line, but are kept between multiple parts
* of the same text, e.g. on line breaks.
*/
struct FontState {
FontSize fontsize; ///< Current font size.
TextColour cur_colour; ///< Current text colour.
std::stack<TextColour, std::vector<TextColour>> colour_stack; ///< Stack of colours to assist with colour switching.
FontState() : fontsize(FS_END), cur_colour(TC_INVALID) {}
FontState(TextColour colour, FontSize fontsize) : fontsize(fontsize), cur_colour(colour) {}
/**
* Switch to new colour \a c.
* @param c New colour to use.
*/
inline void SetColour(TextColour c)
{
assert((c & TC_COLOUR_MASK) >= TC_BLUE && (c & TC_COLOUR_MASK) <= TC_BLACK);
assert((c & (TC_COLOUR_MASK | TC_FLAGS_MASK)) == c);
if ((this->cur_colour & TC_FORCED) == 0) this->cur_colour = c;
}
/**
* Switch to and pop the last saved colour on the stack.
*/
inline void PopColour()
{
if (colour_stack.empty()) return;
SetColour(colour_stack.top());
colour_stack.pop();
}
/**
* Push the current colour on to the stack.
*/
inline void PushColour()
{
colour_stack.push(this->cur_colour);
}
/**
* Switch to using a new font \a f.
* @param f New font to use.
*/
inline void SetFontSize(FontSize f)
{
this->fontsize = f;
}
};
/**
* Container with information about a font.
*/
class Font {
public:
FontCache *fc; ///< The font we are using.
TextColour colour; ///< The colour this font has to be.
Font(FontSize size, TextColour colour);
};
/** Mapping from index to font. The pointer is owned by FontColourMap. */
using FontMap = std::map<int, Font *>;
/**
* Interface to glue fallback and normal layouter into one.
*/
class ParagraphLayouter {
public:
virtual ~ParagraphLayouter() = default;
/** Visual run contains data about the bit of text with the same font. */
class VisualRun {
public:
virtual ~VisualRun() = default;
virtual const Font *GetFont() const = 0;
virtual int GetGlyphCount() const = 0;
virtual const std::vector<GlyphID> &GetGlyphs() const = 0;
virtual const std::vector<Point> &GetPositions() const = 0;
virtual int GetLeading() const = 0;
virtual const std::vector<int> &GetGlyphToCharMap() const = 0;
};
/** A single line worth of VisualRuns. */
class Line {
public:
virtual ~Line() = default;
virtual int GetLeading() const = 0;
virtual int GetWidth() const = 0;
virtual int CountRuns() const = 0;
virtual const VisualRun &GetVisualRun(int run) const = 0;
virtual int GetInternalCharLength(char32_t c) const = 0;
};
virtual void Reflow() = 0;
virtual std::unique_ptr<const Line> NextLine(int max_width) = 0;
};
/**
* The layouter performs all the layout work.
*
* It also accounts for the memory allocations and frees.
*/
class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
std::string_view string; ///< Pointer to the original string.
/** Key into the linecache */
struct LineCacheKey {
FontState state_before; ///< Font state at the beginning of the line.
std::string str; ///< Source string of the line (including colour and font size codes).
};
struct LineCacheQuery {
FontState state_before; ///< Font state at the beginning of the line.
std::string_view str; ///< Source string of the line (including colour and font size codes).
};
/** Comparator for std::map */
struct LineCacheCompare {
using is_transparent = void; ///< Enable map queries with various key types
/** Comparison operator for LineCacheKey and LineCacheQuery */
template<typename Key1, typename Key2>
bool operator()(const Key1 &lhs, const Key2 &rhs) const
{
if (lhs.state_before.fontsize != rhs.state_before.fontsize) return lhs.state_before.fontsize < rhs.state_before.fontsize;
if (lhs.state_before.cur_colour != rhs.state_before.cur_colour) return lhs.state_before.cur_colour < rhs.state_before.cur_colour;
if (lhs.state_before.colour_stack != rhs.state_before.colour_stack) return lhs.state_before.colour_stack < rhs.state_before.colour_stack;
return lhs.str < rhs.str;
}
};
public:
/** Item in the linecache */
struct LineCacheItem {
/* Stuff that cannot be freed until the ParagraphLayout is freed */
void *buffer; ///< Accessed by our ParagraphLayout::nextLine.
FontMap runs; ///< Accessed by our ParagraphLayout::nextLine.
FontState state_after; ///< Font state after the line.
ParagraphLayouter *layout; ///< Layout of the line.
LineCacheItem() : buffer(nullptr), layout(nullptr) {}
~LineCacheItem() { delete layout; free(buffer); }
};
private:
typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
static LineCache *linecache;
static LineCacheItem &GetCachedParagraphLayout(std::string_view str, const FontState &state);
using FontColourMap = std::map<TextColour, std::unique_ptr<Font>>;
static FontColourMap fonts[FS_END];
public:
static Font *GetFont(FontSize size, TextColour colour);
Layouter(std::string_view str, int maxw = INT32_MAX, TextColour colour = TC_FROMSTRING, FontSize fontsize = FS_NORMAL);
Dimension GetBounds();
Point GetCharPosition(std::string_view::const_iterator ch) const;
ptrdiff_t GetCharAtPosition(int x, size_t line_index) const;
static void ResetFontCache(FontSize size);
static void ResetLineCache();
static void ReduceLineCache();
};
#endif /* GFX_LAYOUT_H */
|