41 if ((this->cur_colour &
TC_FORCED) == 0) this->cur_colour = c;
84using FontMap = std::vector<std::pair<int, Font *>>;
110 virtual const Font *GetFont()
const = 0;
111 virtual int GetGlyphCount()
const = 0;
112 virtual std::span<const GlyphID> GetGlyphs()
const = 0;
113 virtual std::span<const Position> GetPositions()
const = 0;
114 virtual int GetLeading()
const = 0;
115 virtual std::span<const int> GetGlyphToCharMap()
const = 0;
121 virtual ~Line() =
default;
122 virtual int GetLeading()
const = 0;
123 virtual int GetWidth()
const = 0;
124 virtual int CountRuns()
const = 0;
125 virtual const VisualRun &GetVisualRun(
int run)
const = 0;
126 virtual int GetInternalCharLength(
char32_t c)
const = 0;
129 virtual void Reflow() = 0;
130 virtual std::unique_ptr<const Line> NextLine(
int max_width) = 0;
138class Layouter :
public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
157 template<
typename Key1,
typename Key2>
160 if (lhs.state_before.fontsize != rhs.state_before.fontsize)
return lhs.state_before.fontsize < rhs.state_before.fontsize;
161 if (lhs.state_before.cur_colour != rhs.state_before.cur_colour)
return lhs.state_before.cur_colour < rhs.state_before.cur_colour;
162 if (lhs.state_before.colour_stack != rhs.state_before.colour_stack)
return lhs.state_before.colour_stack < rhs.state_before.colour_stack;
163 return lhs.str < rhs.str;
174 std::unique_ptr<ParagraphLayouter>
layout =
nullptr;
180 typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;
185 using FontColourMap = std::map<TextColour, std::unique_ptr<Font>>;
Font cache for basic fonts.
Container with information about a font.
FontCache * fc
The font we are using.
TextColour colour
The colour this font has to be.
The layouter performs all the layout work.
static void Initialize()
Perform initialization of layout engine.
static FontColourMap fonts[FS_END]
Cache of Font instances.
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.
ParagraphLayouter::Position GetCharPosition(std::string_view::const_iterator ch) const
Get the position of a character in the layout.
static Font * GetFont(FontSize size, TextColour colour)
Get a static font instance.
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.
static LineCache * linecache
Cache of ParagraphLayout lines.
static void ReduceLineCache()
Reduce the size of linecache if necessary to prevent infinite growth.
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.
Functions related to the gfx engine.
ParagraphLayouter::Position GetCharPosInString(std::string_view str, const char *ch, 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.
std::vector< std::pair< int, Font * > > FontMap
Mapping from index to font.
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.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
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.
std::stack< TextColour, std::vector< TextColour > > colour_stack
Stack of colours to assist with colour switching.
TextColour cur_colour
Current text colour.
bool operator()(const Key1 &lhs, const Key2 &rhs) const
Comparison operator for LineCacheKey and LineCacheQuery.
void is_transparent
Enable map queries with various key types.
FontMap runs
Accessed by our ParagraphLayout::nextLine.
void * buffer
Accessed by our ParagraphLayout::nextLine.
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).
Coordinates of a point in 2D.