OpenTTD Source 20251213-master-g1091fa6071
Layouter Class Reference

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

#include <gfx_layout.h>

Inheritance diagram for Layouter:

Data Structures

struct  LineCacheEqualTo
 
struct  LineCacheHash
 
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.
 
Dimension GetBounds ()
 Get the boundaries of this paragraph.
 
ParagraphLayouter::Position GetCharPosition (std::string_view::const_iterator ch) const
 Get the position of a character in the layout.
 
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 Public Member Functions

static void Initialize ()
 Perform initialization of layout engine.
 
static void ResetFontCache (FontSize fs)
 Reset cached font information.
 
static void ResetLineCache ()
 Clear line cache.
 

Private Types

using LineCache = LRUCache< LineCacheKey, LineCacheItem, LineCacheHash, LineCacheEqualTo >
 

Static Private Member Functions

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

Private Attributes

std::string_view string
 Pointer to the original string.
 

Static Private Attributes

static std::unique_ptr< LineCachelinecache
 Cache of ParagraphLayout lines.
 

Friends

struct std::hash< Layouter::LineCacheQuery >
 

Detailed Description

The layouter performs all the layout work.

It also accounts for the memory allocations and frees.

Definition at line 161 of file gfx_layout.h.

Member Typedef Documentation

◆ LineCache

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 196 of file gfx_layout.cpp.

References Layouter::LineCacheItem::cached_layout, Layouter::LineCacheItem::cached_width, GetCachedParagraphLayout(), Layouter::LineCacheItem::layout, and Layouter::LineCacheItem::state_after.

Member Function Documentation

◆ GetBounds()

Dimension Layouter::GetBounds ( )

Get the boundaries of this paragraph.

Returns
The boundaries.

Definition at line 275 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 441 of file gfx_layout.cpp.

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

Referenced by Layouter().

◆ 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 370 of file gfx_layout.cpp.

References IsConsumedFormattingCode(), and IsInsideMM().

Referenced by GetCharAtPosition().

◆ 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 304 of file gfx_layout.cpp.

References _current_text_dir, Utf8View::GetIterAtByte(), IsConsumedFormattingCode(), and TD_LTR.

Referenced by GetCharPosInString().

◆ Initialize()

void Layouter::Initialize ( )
static

Perform initialization of layout engine.

Definition at line 411 of file gfx_layout.cpp.

References ICUParagraphLayoutFactory::InitializeLayouter().

Referenced by ReadLanguagePack().

◆ ResetFontCache()

void Layouter::ResetFontCache ( FontSize  fs)
static

Reset cached font information.

Definition at line 421 of file gfx_layout.cpp.

References MacOSResetScriptCache(), and ResetLineCache().

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

◆ ResetLineCache()

void Layouter::ResetLineCache ( )
static

Clear line cache.

Definition at line 464 of file gfx_layout.cpp.

References linecache.

Referenced by ResetFontCache().

Friends And Related Symbol Documentation

◆ std::hash< Layouter::LineCacheQuery >

friend struct std::hash< Layouter::LineCacheQuery >
friend

Definition at line 162 of file gfx_layout.h.

Field Documentation

◆ linecache

std::unique_ptr< Layouter::LineCache > Layouter::linecache
staticprivate

Cache of ParagraphLayout lines.

Definition at line 205 of file gfx_layout.h.

Referenced by GetCachedParagraphLayout(), and ResetLineCache().

◆ string

std::string_view Layouter::string
private

Pointer to the original string.

Definition at line 162 of file gfx_layout.h.


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