OpenTTD: Layouter Class Reference

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- Data Structures
- Class Hierarchy
- Data Fields

Data Structures | Public Member Functions | Static Public Member Functions | Private Types | Static Private Member Functions | Private Attributes | Static Private Attributes
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.
Dimension | GetBounds ()
| Get the boundaries of this paragraph.
Point | GetCharPosition (const char *ch) const
| Get the position of a character in the layout.
const char * | GetCharAtPosition (int x) const
| Get the character that is at a position.
Public Member Functions inherited from AutoDeleteSmallVector< const ParagraphLayouter::Line *, 4 >
void | Clear ()
| Remove all items from the list.
Public Member Functions inherited from SmallVector< const ParagraphLayouter::Line *, S >
| SmallVector (const SmallVector &other)
| Copy constructor.
| SmallVector (const SmallVector< const ParagraphLayouter::Line *, X > &other)
| Generic copy constructor.
SmallVector & | operator= (const SmallVector &other)
| Assignment.
SmallVector & | operator= (const SmallVector< const ParagraphLayouter::Line *, X > &other)
| Generic assignment.
void | Assign (const SmallVector< const ParagraphLayouter::Line *, X > &other)
| Assign items from other vector.
void | Reset ()
| Remove all items from the list and free allocated memory.
void | Compact ()
| Compact the list down to the smallest block size boundary.
const ParagraphLayouter::Line ** | Append (uint to_add=1)
| Append an item and return it.
void | Resize (uint num_items)
| Set the size of the vector, effectively truncating items from the end or appending uninitialised ones.
const const ParagraphLayouter::Line ** | Find (const const ParagraphLayouter::Line *&item) const
| Search for the first occurrence of an item.
const ParagraphLayouter::Line ** | Find (const const ParagraphLayouter::Line *&item)
| Search for the first occurrence of an item.
int | FindIndex (const const ParagraphLayouter::Line *&item) const
| Search for the first occurrence of an item.
bool | Contains (const const ParagraphLayouter::Line *&item) const
| Tests whether a item is present in the vector.
void | Erase (const ParagraphLayouter::Line **item)
| Removes given item from this vector.
void | ErasePreservingOrder (uint pos, uint count=1)
| Remove items from the vector while preserving the order of other items.
bool | Include (const const ParagraphLayouter::Line *&item)
| Tests whether a item is present in the vector, and appends it to the end if not.
uint | Length () const
| Get the number of items in the list.
const const ParagraphLayouter::Line ** | Begin () const
| Get the pointer to the first item (const)
const ParagraphLayouter::Line ** | Begin ()
| Get the pointer to the first item.
const const ParagraphLayouter::Line ** | End () const
| Get the pointer behind the last valid item (const)
const ParagraphLayouter::Line ** | End ()
| Get the pointer behind the last valid item.
const const ParagraphLayouter::Line ** | Get (uint index) const
| Get the pointer to item "number" (const)
const ParagraphLayouter::Line ** | Get (uint index)
| Get the pointer to item "number".
const const ParagraphLayouter::Line *& | operator[] (uint index) const
| Get item "number" (const)
const ParagraphLayouter::Line *& | operator[] (uint index)
| Get item "number".

## 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.
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 > | LineCache
typedef SmallMap< TextColour, Font * > | FontColourMap

## Static Private Member Functions
static LineCacheItem & | GetCachedParagraphLayout (const char *str, size_t len, const FontState &state)
| Get reference to cache item.

## 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.

## Additional Inherited Members
Protected Attributes inherited from SmallVector< const ParagraphLayouter::Line *, S >
const ParagraphLayouter::Line ** | data
| The pointer to the first item.
uint | items
| The number of items stored.
uint | capacity
| The available space for storing items.

## Detailed Description

The layouter performs all the layout work.

It also accounts for the memory allocations and frees.

Definition at line 139 of file gfx_layout.h.

## Constructor & Destructor Documentation

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

Create a new layouter.

Parameters

  str | The string to create the layout for.
  maxw | The maximum width.
  colour | The colour of the font.
  fontsize | The size of font to use.

Definition at line 635 of file gfx_layout.cpp.

References SmallVector< const ParagraphLayouter::Line *, S >::Append(), DEBUG, GetCachedParagraphLayout(), Layouter::LineCacheItem::layout, Layouter::LineCacheItem::state_after, and Utf8Decode().

## Member Function Documentation

Dimension Layouter::GetBounds | ( |  | ) |

Get the boundaries of this paragraph.

Returns
  The boundaries.

Definition at line 691 of file gfx_layout.cpp.

References SmallVector< const ParagraphLayouter::Line *, S >::Begin(), and SmallVector< const ParagraphLayouter::Line *, S >::End().

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

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

  str | Source string of the line (including colour and font size codes).
  len | Length of str in bytes (no termination).
  state | State of the font at the beginning of the line.

Returns
  Reference to cache item.

Definition at line 822 of file gfx_layout.cpp.

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

Referenced by Layouter().

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

Get the character that is at a position.

Parameters

  x | Position in the string.

Returns
  Pointer to the character at the position or NULL if no character is at the position.

Definition at line 754 of file gfx_layout.cpp.

References SmallVector< const ParagraphLayouter::Line *, S >::Begin(), and IsInsideMM().

Referenced by GetCharAtPosition().

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

Get the position of a character in the layout.

Parameters

  ch | Character 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 707 of file gfx_layout.cpp.

References SmallVector< const ParagraphLayouter::Line *, S >::Begin(), string, and Utf8Decode().

Referenced by GetCharPosInString().

void Layouter::ResetFontCache | ( | FontSize | size | ) |
| static

Reset cached font information.

Parameters

  size | Font size to reset.

Definition at line 803 of file gfx_layout.cpp.

References SmallVector< const ParagraphLayouter::Line *, S >::Begin(), SmallVector< T, S >::Clear(), SmallVector< T, S >::End(), fonts, and ResetLineCache().

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

## Field Documentation

Layouter::FontColourMap Layouter::fonts
| staticprivate

Cache of Font instances.

Definition at line 176 of file gfx_layout.h.

Referenced by GetFont(), and ResetFontCache().

Layouter::LineCache * Layouter::linecache
| staticprivate

Cache of ParagraphLayout lines.

Definition at line 171 of file gfx_layout.h.

Referenced by GetCachedParagraphLayout(), ReduceLineCache(), and ResetLineCache().

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

- src/gfx_layout.h
- src/gfx_layout.cpp