# Introduction

Text Rendering involves both the handling of Unicode standards regarding character encoding as well as the generation and rendering of the glyphs. The sub-chapters here explain the basic principles.

####   
**Character Encodings**

character encoding system consists of a code that pairs each character from a given repertoire with something else such as a bit pattern, sequence of natural numbers, octets, or electrical pulses (Source: Wikipedia).

Unicode is the most important standard for character encodings and defines (among other things) the mapping of a character to a Unicode code point (bit pattern):

<div class="contents" id="bkmrk-fixed-length-encodin"><div class="contents"><div class="textblock">

- Fixed length encodings of code points (ASCII, UCS2, UCS4 etc).
- Variable length encoding of code points (UTF-8, UTF-16, UTF-32)
- Chinese GB18030 is the only relevant other encoding standard

</div></div></div>All texts which are rendered within the TextEngine have to be of type TChar\*. Text in TChar\* have to be encoded in UTF-8. UTF-8 uses a varying number of TChars to encode one glyph. E.g. a UTF-8 encoded string with special characters: "&amp;auml&amp;Auml&amp;ouml&amp;Ouml&amp;uuml&amp;Uuml&amp;szlig&amp;#8364" consists of 8 glyphs but of 17 bytes because several of these special characters are encoded in more than one byte.

The methods <span style="color: rgb(230, 126, 35);">[FeatStd::String::GetCharCount()](http://dev.doc.cgistudio.at/APILINK/class_feat_std_1_1_string.html#a664debb9d7058c47f4d4e1687e83ae0f)</span> and <span style="color: rgb(230, 126, 35);">[FeatStd::String::GetCodePointCount()](http://dev.doc.cgistudio.at/APILINK/class_feat_std_1_1_string.html#a3ddbc24da69094a85d8c6ff870284e5a)</span> can be used to get the numbers as explained in the example above. An instance of <span style="color: rgb(230, 126, 35);">[FeatStd::String](http://dev.doc.cgistudio.at/APILINK/class_feat_std_1_1_string.html)</span> class can be constructed with TChar\* by the constructor <span style="color: rgb(230, 126, 35);">[FeatStd::String::String(const TChar \*)](http://dev.doc.cgistudio.at/APILINK/class_feat_std_1_1_string.html#a2b5ab81b2f9de953b42677a508146831)</span>.

<div class="contents" id="bkmrk-char-data-type-must-"><div class="textblock"><dl class="note"><dt></dt><dd><p class="callout info">Char data type must not be used for I18N text and always maps to ASCII.</p>

</dd></dl></div></div>#### **Complex Script Languages**

A complex script is a writing system which requires complex transformation of glyphs like substitution and positioning. Examples of such writing systems are arabic, hebrew and thai. Complex transformations include

<div class="contents" id="bkmrk-bi-directional-order"><div class="contents"><div class="textblock">

- bi-directional ordering of characters,
- contextual shaping which means that displayed glyphs depend on the surrounding code points in the text - displayed glyphs can have different shapes depending on their position in a word or can be combined to ligatures,
- combining characters where several characters are combined into one shape

</div></div></div><div drawio-diagram="2191"><img src="https://doc316en.candera.eu/uploads/images/drawio/2023-02/drawing-4-1677033945.png" alt=""/></div>

Above the characters as they are written and below as they are displayed.

#### **Text Rendering - Functional Steps**

Text Rendering involves the following functional steps:

1. Glyph Rendering
2. Shaping
3. Layouting

##### **1. Glyph Rendering**

Takes a single code point and font metrics to render the according glyph. The Freetype font engine for example takes a code point and outputs a 8 bit alpha.

<div class="contents" id="bkmrk--0"><div class="contents"></div></div>##### **2. Shaping**

Shaping is the transformation from logical to display presentation of a text. The Shaping process is responsible for considering complex scripts. The Shaping process is applied on paragraph level.

##### <a class="anchor" id="bkmrk--4"></a>GPOS/GSUB Tables

OpenType fonts may include GSUB (glyph substitution) and GPOS (glyph positioning) tables.

The GSUB table contains (among other things)

<div class="contents" id="bkmrk-ligature-substitutio"><div class="contents"><div class="textblock">

- Ligature substitution (replace multiple code points with single code point)
- Contextual substitution (alternative presentation of the glyph depending on his context) If no GSUB table is available, the standard substitutions defined by Unicode apply (eg. mandatory set of Arabic ligatures).

</div></div></div>The GPOS table contains (among other things)

<div class="contents" id="bkmrk-glyph-placement-adju"><div class="contents"><div class="contents"><div class="textblock">

- glyph placement
- adjustment of marks
- kerning
- attachment points for combining characters

</div></div></div></div><a class="anchor" id="bkmrk--5" style="font-size: 1.4em; font-weight: 400; background-color: rgb(255, 255, 255);"></a><span style="color: rgb(34, 34, 34); font-size: 1.4em; font-weight: 400;">Shaping Libraries</span>

<div class="contents" id="bkmrk-feat-complexscript-l"><div class="contents"><div class="textblock">

- FEAT ComplexScript library does shaping for Arabic language
- FreeBidi - functional equivalent to FEAT ComplexScript
- Harfbuzz - Arabic + many other scripts

</div></div></div>A <span style="color: rgb(230, 126, 35);">[Candera](http://dev.doc.cgistudio.at/APILINK/namespace_candera.html "&#91;DataBinding&#95;RefTypeSample&#93;")</span> feature define is available in CMake to select between the 3rd party software components *ComplexScriptLib* and *HarfBuzz* for text shaping.

#####   


##### **3. Layouting**

Layout text that has been shaped. Typical operations:

<div class="contents" id="bkmrk-line-breaking-word-b"><div class="textblock">

- Line breaking
- Word breaking
- Text alignment (horizontal and vertical)

</div></div>