OpenTTD Source  1.11.2
Layouter Class Reference

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

#include <gfx_layout.h>

Inheritance diagram for Layouter:

Data Structures

struct  LineCacheItem
 Item in the linecache. More...
 
struct  LineCacheKey
 Key into the linecache. More...
 

Public Member Functions

 Layouter (const char *str, int maxw=INT32_MAX, TextColour colour=TC_FROMSTRING, FontSize fontsize=FS_NORMAL)
 Create a new layouter. More...
 
Dimension GetBounds ()
 Get the boundaries of this paragraph. More...
 
Point GetCharPosition (const char *ch) const
 Get the position of a character in the layout. More...
 
const char * GetCharAtPosition (int x) const
 Get the character that is at a position. More...
 

Static Public Member Functions

static Font * GetFont (FontSize size, TextColour colour)
 Get a static font instance.
 
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, LineCacheItemLineCache
 
typedef SmallMap< TextColour, Font * > FontColourMap
 

Static Private Member Functions

static LineCacheItemGetCachedParagraphLayout (const char *str, size_t len, const FontState &state)
 Get reference to cache item. More...
 

Private Attributes

const char * 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 151 of file gfx_layout.h.

Constructor & Destructor Documentation

◆ Layouter()

Layouter::Layouter ( const char *  str,
int  maxw = INT32_MAX,
TextColour  colour = TC_FROMSTRING,
FontSize  fontsize = FS_NORMAL 
)

Create a new layouter.

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

Definition at line 667 of file gfx_layout.cpp.

References DEBUG, GetCachedParagraphLayout(), Layouter::LineCacheItem::layout, Layouter::LineCacheItem::state_after, and Utf8Decode().

Member Function Documentation

◆ GetBounds()

Dimension Layouter::GetBounds ( )

Get the boundaries of this paragraph.

Returns
The boundaries.

Definition at line 746 of file gfx_layout.cpp.

Referenced by DrawStringMultiLine().

◆ GetCachedParagraphLayout()

Layouter::LineCacheItem & Layouter::GetCachedParagraphLayout ( const char *  str,
size_t  len,
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).
lenLength of str in bytes (no termination).
stateState of the font at the beginning of the line.
Returns
Reference to cache item.

Definition at line 884 of file gfx_layout.cpp.

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

Referenced by Layouter().

◆ GetCharAtPosition()

const char * Layouter::GetCharAtPosition ( int  x) const

Get the character that is at a position.

Parameters
xPosition in the string.
Returns
Pointer to the character at the position or nullptr if no character is at the position.

Definition at line 809 of file gfx_layout.cpp.

◆ GetCharPosition()

Point Layouter::GetCharPosition ( const char *  ch) const

Get the position of a character in the layout.

Parameters
chCharacter to get the position of.
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 762 of file gfx_layout.cpp.

References string, and Utf8Decode().

◆ ResetFontCache()

void Layouter::ResetFontCache ( FontSize  size)
static

Reset cached font information.

Parameters
sizeFont size to reset.

Definition at line 858 of file gfx_layout.cpp.

References fonts, and ResetLineCache().

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


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