38 auto operator<=>(
const FontState &)
const =
default;
79template <
typename T>
struct std::hash<std::vector<T>> {
80 size_t operator()(
const std::vector<T> &vec)
const
83 return std::transform_reduce(std::begin(vec), std::end(vec),
84 std::hash<size_t>{}(std::size(vec)),
85 [](
const size_t &a,
const size_t &b) ->
size_t {
return a ^ b; },
86 [](
const T &x) ->
size_t {
return std::hash<T>{}(x); });
99 size_t h1 = std::hash<TextColour>{}(tc.colour);
100 size_t h2 = std::hash<uint8_t>{}(tc.flags.base());
101 return h1 ^ (h2 << 1);
106 std::size_t operator()(
const FontState &state)
const noexcept
108 size_t h1 = std::hash<FontSize>{}(state.
fontsize);
109 size_t h2 = std::hash<ExtendedTextColour>{}(state.
cur_colour);
110 size_t h3 = std::hash<std::vector<ExtendedTextColour>>{}(state.
colour_stack);
111 return h1 ^ (h2 << 1) ^ (h3 << 2);
127using FontMap = std::vector<std::pair<int, Font *>>;
246 virtual std::unique_ptr<const Line>
NextLine(
int max_width) = 0;
254class Layouter :
public std::vector<const ParagraphLayouter::Line *> {
272 using is_transparent = void;
274 template <
typename Tlhs,
typename Trhs>
275 bool operator()(
const Tlhs &lhs,
const Trhs &rhs)
const
277 return lhs.state_before == rhs.state_before && lhs.str == rhs.str;
285 using Buffer = std::unique_ptr<void, void(*)(
void *)>;
291 std::unique_ptr<ParagraphLayouter>
layout =
nullptr;
293 std::vector<std::unique_ptr<const ParagraphLayouter::Line>>
cached_layout{};
302 using FontColourMap = std::map<ExtendedTextColour, std::unique_ptr<Font>>;
320template <>
struct std::hash<
Layouter::LineCacheQuery> {
323 size_t h1 = std::hash<std::string_view>{}(state.
str);
325 return h1 ^ (h2 << 1);
330 using is_transparent = void;
333 std::size_t operator()(
const Layouter::LineCacheQuery &query)
const {
return std::hash<Layouter::LineCacheQuery>{}(query); }
@ Invalid
Invalid town production effect.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Font cache for basic fonts.
Container with information about a font.
ExtendedTextColour colour
The colour this font has to be.
FontCache * fc
The font we are using.
Font(FontSize size, ExtendedTextColour colour)
Construct a new font.
Size limited cache with a least recently used eviction strategy.
The layouter performs all the layout work.
Layouter(std::string_view str, int maxw=INT32_MAX, FontSize fontsize=FontSize::Normal)
Create a new layouter.
static EnumIndexArray< FontColourMap, FontSize, FontSize::End > fonts
The colour mapping of each of the fonts.
static void Initialize()
Perform initialization of layout engine.
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.
static void ResetFontCache(FontSize size)
Reset cached font information.
static Font * GetFont(FontSize size, ExtendedTextColour colour)
Get a static font instance.
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.
virtual int GetLeading() const =0
Get the font leading, or distance between the baselines of consecutive lines.
virtual int GetInternalCharLength(char32_t c) const =0
Get the number of elements the given character occupies in the underlying text buffer of the Layouter...
virtual const VisualRun & GetVisualRun(size_t run) const =0
Get a reference to the given run.
virtual ~Line()=default
Ensure the destructor of the sub classes are called as well.
virtual int GetWidth() const =0
Get the width of this line.
virtual size_t CountRuns() const =0
Get the number of runs in this line.
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.
virtual std::span< const GlyphID > GetGlyphs() const =0
Get the glyphs to draw.
virtual int GetLeading() const =0
Get the font leading, or distance between the baselines of consecutive lines.
virtual std::span< const int > GetGlyphToCharMap() const =0
The offset for each of the glyphs to the character run that was passed to the Layouter.
virtual std::span< const Position > GetPositions() const =0
Get the positions for each of the glyphs.
virtual const Font * GetFont() const =0
Get the font.
virtual size_t GetGlyphCount() const =0
Get the number of glyphs.
virtual ~VisualRun()=default
Ensure the destructor of the sub classes are called as well.
Interface to glue fallback and normal layouter into one.
virtual std::unique_ptr< const Line > NextLine(int max_width)=0
Construct a new line with a maximum width.
virtual void Reflow()=0
Reset the position to the start of the paragraph.
virtual ~ParagraphLayouter()=default
Ensure the destructor of the sub classes are called as well.
#define T
Climate temperate.
EnumClassIndexContainer< std::array< T, to_underlying(N)>, Index > EnumIndexArray
A typedef for EnumClassIndexContainer using std::array as the backing container type.
Functions to read fonts from files and cache them.
Functions related to the gfx engine.
ptrdiff_t GetCharAtPosition(std::string_view str, int x, FontSize start_fontsize=FontSize::Normal)
Get the character from a string that is drawn at a specific position.
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=FontSize::Normal)
Get the leading corner of a character in a single-line string relative to the start of the string.
FontSize
Available font sizes.
@ Normal
Index of the normal font in the font tables.
@ IsPaletteColour
Colour value is already a real palette colour index, not an index of a StringColour.
@ Forced
Ignore colour changes from strings.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
@ Begin
Marker for the begin of the range.
@ End
Marker for the end of the range.
Size limited cache map with a least recently used eviction strategy.
#define Point
Macro that prevents name conflicts between included headers.
Dimensions (a width and height) of a rectangle in 2D.
Container for the text colour and some text colour related flags for drawing.
ExtendedTextColourFlags flags
The flags.
TextColour colour
The colour.
Text drawing parameters, which can change while drawing a line, but are kept between multiple parts o...
std::vector< ExtendedTextColour > colour_stack
Stack of colours to assist with colour switching.
void PushColour()
Push the current colour on to the stack.
FontState()
Create the font state with an invalid state.
void PopColour()
Switch to and pop the last saved colour on the stack.
void SetColour(ExtendedTextColour c)
Switch to new colour c.
ExtendedTextColour cur_colour
Current text colour.
void SetFontSize(FontSize f)
Switch to using a new font f.
FontState(ExtendedTextColour colour, FontSize fontsize)
Create the font state.
FontSize fontsize
Current font size.
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< void, void(*)(void *)> Buffer
Due to the type of data in the buffer differing depending on the Layouter, we need to pass our own de...
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).
std::size_t operator()(const ExtendedTextColour &tc) const noexcept
Hash the text colour.