31 auto operator<=>(
const FontState &)
const =
default;
41 if ((this->cur_colour &
TC_FORCED) == 0) this->cur_colour = c;
69 this->font_index = FontCache::GetDefaultFontIndex(this->fontsize);
73template <
typename T>
struct std::hash<std::vector<T>> {
74 size_t operator()(
const std::vector<T> &vec)
const
77 return std::transform_reduce(std::begin(vec), std::end(vec),
78 std::hash<size_t>{}(std::size(vec)),
79 [](
const size_t &a,
const size_t &b) ->
size_t {
return a ^ b; },
80 [](
const T &x) ->
size_t {
return std::hash<T>{}(x); });
85 std::size_t operator()(
const FontState &state)
const noexcept
87 size_t h1 = std::hash<FontSize>{}(state.
fontsize);
88 size_t h2 = std::hash<FontIndex>{}(state.
font_index);
89 size_t h3 = std::hash<TextColour>{}(state.
cur_colour);
90 size_t h4 = std::hash<std::vector<TextColour>>{}(state.
colour_stack);
91 return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
107using FontMap = std::vector<std::pair<int, Font>>;
133 virtual const Font &GetFont()
const = 0;
134 virtual int GetGlyphCount()
const = 0;
135 virtual std::span<const GlyphID> GetGlyphs()
const = 0;
136 virtual std::span<const Position> GetPositions()
const = 0;
137 virtual int GetLeading()
const = 0;
138 virtual std::span<const int> GetGlyphToCharMap()
const = 0;
144 virtual ~Line() =
default;
145 virtual int GetLeading()
const = 0;
146 virtual int GetWidth()
const = 0;
147 virtual int CountRuns()
const = 0;
148 virtual const VisualRun &GetVisualRun(
int run)
const = 0;
149 virtual int GetInternalCharLength(
char32_t c)
const = 0;
152 virtual void Reflow() = 0;
153 virtual std::unique_ptr<const Line> NextLine(
int max_width) = 0;
161class Layouter :
public std::vector<const ParagraphLayouter::Line *> {
179 using is_transparent = void;
181 template <
typename Tlhs,
typename Trhs>
182 bool operator()(
const Tlhs &lhs,
const Trhs &rhs)
const
184 return lhs.state_before == rhs.state_before && lhs.str == rhs.str;
192 using Buffer = std::unique_ptr<void, void(*)(
void *)>;
198 std::unique_ptr<ParagraphLayouter>
layout =
nullptr;
200 std::vector<std::unique_ptr<const ParagraphLayouter::Line>>
cached_layout{};
223template <>
struct std::hash<
Layouter::LineCacheQuery> {
226 size_t h1 = std::hash<std::string_view>{}(state.
str);
228 return h1 ^ (h2 << 1);
233 using is_transparent = void;
236 std::size_t operator()(
const Layouter::LineCacheQuery &query)
const {
return std::hash<Layouter::LineCacheQuery>{}(query); }
Font cache for basic fonts.
static std::span< const std::unique_ptr< FontCache > > Get()
Get span of all FontCaches.
Container with information about a font.
TextColour colour
The colour this font has to be.
FontIndex font_index
The font we are using.
Size limited cache with a least recently used eviction strategy.
The layouter performs all the layout work.
static void Initialize()
Perform initialization of layout engine.
static void ResetFontCache(FontSize fs)
Reset cached font information.
ptrdiff_t GetCharAtPosition(int x, size_t line_index) const
Get the character that is at a pixel position in the first line of the layouted text.
ParagraphLayouter::Position GetCharPosition(std::string_view::const_iterator ch) const
Get the position of a character in the layout.
static std::unique_ptr< LineCache > linecache
Cache of ParagraphLayout lines.
std::string_view string
Pointer to the original string.
static void ResetLineCache()
Clear line cache.
Dimension GetBounds()
Get the boundaries of this paragraph.
static LineCacheItem & GetCachedParagraphLayout(std::string_view str, const FontState &state)
Get reference to cache item.
A single line worth of VisualRuns.
Position of a glyph within a VisualRun.
int16_t right
Right-most position of glyph.
constexpr Position(const Point &pt)
Conversion from a single point to a Position.
int16_t left
Left-most position of glyph.
int16_t top
Top-most position of glyph.
Visual run contains data about the bit of text with the same font.
Interface to glue fallback and normal layouter into one.
Functions to read fonts from files and cache them.
std::vector< std::pair< int, Font > > FontMap
Mapping from index to font.
ParagraphLayouter::Position GetCharPosInString(std::string_view str, size_t pos, FontSize start_fontsize=FS_NORMAL)
Get the leading corner of a character in a single-line string relative to the start of the string.
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize=FS_NORMAL)
Get the character from a string that is drawn at a specific position.
FontSize
Available font sizes.
@ FS_NORMAL
Index of the normal font in the font tables.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
@ TC_FORCED
Ignore colour changes from strings.
@ TC_FLAGS_MASK
Mask to test if TextColour (with flags) is within limits.
@ TC_COLOUR_MASK
Mask to test if TextColour (without flags) is within limits.
Size limited cache map with a least recently used eviction strategy.
Dimensions (a width and height) of a rectangle in 2D.
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
void PushColour()
Push the current colour on to the stack.
void SetColour(TextColour c)
Switch to new colour c.
void PopColour()
Switch to and pop the last saved colour on the stack.
void SetFontSize(FontSize f)
Switch to using a new font f.
FontSize fontsize
Current font size.
FontIndex font_index
Current font index.
std::vector< TextColour > colour_stack
Stack of colours to assist with colour switching.
TextColour cur_colour
Current text colour.
FontMap runs
Accessed by our ParagraphLayout::nextLine.
Buffer buffer
Accessed by our ParagraphLayout::nextLine.
int cached_width
Width used for the cached layout.
std::vector< std::unique_ptr< const ParagraphLayouter::Line > > cached_layout
Cached results of line layouting.
FontState state_after
Font state after the line.
std::unique_ptr< ParagraphLayouter > layout
Layout of the line.
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).
const 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).