OpenTTD Source  20240919-master-gdf0233f4c2
Layouter Class Reference

The layouter performs all the layout work. More...

#include <gfx_layout.h>

Inheritance diagram for Layouter:

Data Structures

struct  LineCacheCompare
 Comparator for std::map. More...
 
struct  LineCacheItem
 Item in the linecache. More...
 
struct  LineCacheKey
 Key into the linecache. More...
 
struct  LineCacheQuery
 

Public Member Functions

 Layouter (std::string_view str, int maxw=INT32_MAX, FontSize fontsize=FS_NORMAL)
 Create a new layouter. More...
 
Dimension GetBounds ()
 Get the boundaries of this paragraph. More...
 
ParagraphLayouter::Position GetCharPosition (std::string_view::const_iterator ch) const
 Get the position of a character in the layout. More...
 
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. More...
 

Static Public Member Functions

static FontGetFont (FontSize size, TextColour colour)
 Get a static font instance.
 
static void Initialize ()
 Perform initialization of layout engine.
 
static void ResetFontCache (FontSize size)
 Reset cached font information. More...
 
static void ResetLineCache ()
 Clear line cache.
 
static void ReduceLineCache ()
 Reduce the size of linecache if necessary to prevent infinite growth.
 

Private Types

typedef std::map< LineCacheKey, LineCacheItem, LineCacheCompareLineCache
 
using FontColourMap = std::map< TextColour, std::unique_ptr< Font > >
 

Static Private Member Functions

static LineCacheItemGetCachedParagraphLayout (std::string_view str, const FontState &state)
 Get reference to cache item. More...
 

Private Attributes

std::string_view string
 Pointer to the original string.
 

Static Private Attributes

static LineCache * linecache
 Cache of ParagraphLayout lines.
 
static FontColourMap fonts [FS_END]
 Cache of Font instances.
 

Detailed Description

The layouter performs all the layout work.

It also accounts for the memory allocations and frees.

Definition at line 138 of file gfx_layout.h.

Constructor & Destructor Documentation

◆ Layouter()

Layouter::Layouter ( std::string_view  str,
int  maxw = INT32_MAX,
FontSize  fontsize = FS_NORMAL 
)

Create a new layouter.

Parameters
strThe string to create the layout for.
maxwThe maximum width.
fontsizeThe size of font to use.

Definition at line 130 of file gfx_layout.cpp.

Member Function Documentation

◆ GetBounds()

Dimension Layouter::GetBounds ( )

Get the boundaries of this paragraph.

Returns
The boundaries.

Definition at line 199 of file gfx_layout.cpp.

Referenced by DrawStringMultiLine(), GetStringBoundingBox(), and GetStringHeight().

◆ GetCachedParagraphLayout()

Layouter::LineCacheItem & Layouter::GetCachedParagraphLayout ( std::string_view  str,
const FontState state 
)
staticprivate

Get reference to cache item.

If the item does not exist yet, it is default constructed.

Parameters
strSource string of the line (including colour and font size codes).
stateState of the font at the beginning of the line.
Returns
Reference to cache item.

Definition at line 375 of file gfx_layout.cpp.

References linecache, Layouter::LineCacheKey::state_before, and Layouter::LineCacheKey::str.

◆ GetCharAtPosition()

ptrdiff_t Layouter::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.

Parameters
xPosition in the string.
line_indexWhich line of the layout to search
Returns
String offset of the position (bytes) or -1 if no character is at the position.

Definition at line 290 of file gfx_layout.cpp.

Referenced by TextfileWindow::CheckHyperlinkClick().

◆ GetCharPosition()

ParagraphLayouter::Position Layouter::GetCharPosition ( std::string_view::const_iterator  ch) const

Get the position of a character in the layout.

Parameters
chCharacter to get the position of. Must be an iterator of the string passed to the constructor.
Returns
Upper left corner of the character relative to the start of the string.
Note
Will only work right for single-line strings.

Definition at line 228 of file gfx_layout.cpp.

References _current_text_dir, and TD_LTR.

Referenced by GetCharPosInString().

◆ ResetFontCache()

void Layouter::ResetFontCache ( FontSize  size)
static

Reset cached font information.

Parameters
sizeFont size to reset.

Definition at line 353 of file gfx_layout.cpp.

References fonts, and ResetLineCache().

Referenced by SpriteFontCache::ClearFontCache(), TrueTypeFontCache::ClearFontCache(), and FontCache::~FontCache().


The documentation for this class was generated from the following files: