text_compute_render - ryzom/ryzomcore GitHub Wiki


title: How to compute and render text description: published: true date: 2023-03-01T05:21:56.803Z tags: editor: markdown dateCreated: 2022-03-14T04:10:16.141Z

A character is displayed using a CTextureFont. To avoid creating a new CTextureFont for each character we use a CFontManager which provides the textures and manages CTextureFont instances in memory. A font manager is able to compute an entire string, to do that it fills a CComputedString. A CComputedString contains a list of primitive blocks (CPrimitiveBlock) [FIXME: CPrimitiveBlock no longer exists since the direct3d driver was implemented...] and materials (CMaterial).

It can then render the string through a driver (IDriver).

Documentation

CTextureFont

A CTextureFont object is a texture representing a unicode character. It inherits ITexture, and therefore CBitmap. The texture bitmap is built by calling generate() which fills in the same time useful fields, as:

  • GlyphIndex: number of the character in the this font

  • Top: Distance between origin and top of the texture

  • Left: Distance between origin and left of the texture

  • AdvX: Advance to the next caracter

    All these values are given by the font generator. {.is-info}

    About bitmap dimensions: if width or height are not powers of 2 there are set to the next power of 2. The true dimensions are still availables in the CTextureFont level. {.is-info}

     

CFontManager

The font manager manages CTextureFont pointers through a list of CSmartPtr. When the user requires the texture font representing a character, it generates and stores this pointer in the list. If this character has already been generated, and lies in the list, it increments its reference count and return the pointer.

A character is defined by:

  • A font name
  • A unicode char
  • A font size

The maximum memory used by the list of generated font textures in the font manager can be, and SHOULD be, set by the user after manager creation. Indeed the default value is 0. When the maximum memory is reached, the manager delete the useless characters until memory ammount is acceptable.

CFontGenerator

The font generator is the interface between Nel and FreeType. One generator is used for one font. Then, providing a unicode char and a size, the generator can return a generated bitmap containing the char bitmap.

CDisplayDescriptor

A CDisplayDescriptor is a structure used to store screen parameters:

  • Screen width
  • Screen height
  • Font ratio

CComputedString

Rendering parameters

The coordinates are screen-relatives:

  • 0,0 is the bottom-left corner
  • 1,1 is the top-right corner

The scale parameters have the default value 1.

The rotation angle is set to 0 by default.

A string as a "hot spot" which indicates its relative display origin.

By default this value is LeftBottom.

The others are:

  • LeftMiddle
  • LeftTop
  • MiddleBottom
  • MiddleMiddle
  • MiddleTop
  • RightBottom
  • RightMiddle
  • RightTop

font.svg

Example

Here is an example generating and rendering a string:

// Font manager
CFontManager fontManager;
fontManager.setMaxMemory(2000000);

// Font generator
CFontGenerator fontGenerator("c:/winnt/fonts/arialuni.ttf");

// Screen parameters
CDisplayDescriptor displayDesc;
displayDesc.ResX = 800;
displayDesc.ResY = 600;

// Compute string
CComputedString cptedString;
fontManager.computeString("Nevrax", &fontGenerator, CRGBA(255,255,255), 100, displayDesc, cptedString);

//...here the driver is initialized...

// render string
cptedString.render2D(driver, 0.5, 0.7);

Credits

Authors

  • Stephane Coutelas

Source

⚠️ **GitHub.com Fallback** ⚠️