---
name: "asfont"
section: "3x"
platform: "linux"
package: "libafterimage-dev"
version: "2.2.12-18"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/asfont"
---

# asfont(3x)

text drawing functionality and handling of TTF and X11 fonts libAfterImage/asfont.h

## NAME

**asfont** - text drawing functionality and handling of TTF and X11 fonts libAfterImage/asfont.h

## NAMEasfont

## DESCRIPTION

Text drawing functionality. Text is drawn as an ASImage with only alpha channel. Since alpha channel is 8 bit widths that allows for 256 shades to be used in rendered glyphs. That in turn allows for smoothing and antialiasing of the drawn text. Such an approcah allows for easy manipulation of the drawn text, such as changing color, making it transparent, texturizing, rotation, etc.

libAfterImage supports two types of fonts : Fonts that could be rendered using standard Xlib functionality, and fonts rendered by FreeType 2 library. That may include TrueType fonts. When fonts are obtained via Xlib special processing is performed in order to smooth its shape and leverage 256 shades palette available.

Any font being used is has to be opened first. At that time its properties are analysed and glyphs are cached in clients memory. Special RLE compression method is used for font glyphs, significantly reducing memory utilization without any effect on performance.

Font management and drawing functionality has been designed with internatiolization in mind, althou support for locales is not complete yet.

## SEE ALSO

Structures :\
          ASFontManager\
          ASFont\
          ASGlyph\
          ASGlyphRange

Functions :\
          create\_font\_manager(), destroy\_font\_manager(),\
          open\_freetype\_font(), open\_X11\_font(), get\_asfont(),\
          destroy\_font(), print\_asfont(), print\_asglyph(),\
          draw\_text(),\
          get\_asfont\_glyph\_spacing(), set\_asfont\_glyph\_spacing()

Other libAfterImage modules :\
          ascmap.h asfont.h asimage.h asvisual.h blender.h export.h\
          import.h transform.h ximage.h

## AUTHOR

Sasha Vasko \<sasha at aftercode dot net\> libAfterImage/MAX\_GLYPHS\_PER\_FONT

## NAMEMAX\_GLYPHS\_PER\_FONT

\- Max value of glyphs per font allowed. We need that so we can detect and avoid broken fonts somehow.

## SOURCE

\#define MAX\_GLYPHS\_PER\_FONT  2048

libAfterImage/ASFontType

## NAMEASFontType

\- Supported types of fonts - Xlib or FreeType 2 ASF\_GuessWho will enable autodetection of the font type. It is attempted to be opened as FreeType font first, and if that fails - it will be opened as Xlib font.

## SOURCE

typedef enum {\
     ASF\_X11         = 0,\
     ASF\_Freetype    = (0x01\<\<0),\
     ASF\_GuessWho    = (0x01\<\<1), #define ASF\_TypeMask  (ASF\_GuessWho\|ASF\_Freetype)

    /\* flags below should be combined with one of the above values \*/\
     ASF\_Monospaced  = (0x01\<\<2),\
     ASF\_RightToLeft = (0x01\<\<3),  /\* direction of the text flow \*/\
     ASF\_HasKerning  = (0x01\<\<4) }ASFontType;

libAfterImage/ASGlyph

## NAMEASGlyph

## DESCRIPTION

Stores glyph's image, as well as width, height and other characteristics of the glyph.

## SOURCE

typedef struct ASGlyph {\
     CARD8          \*pixmap ;        /\* glyph's RLE encoded pixmap \*/\
     short   width, height ; /\* meaningful width and height\
                                      \* of the glyphs pixmap \*/\
     short  lead, step ;             /\* distance pen position to glyph\
                                      \* beginning and to the next glyph \*/\
                                     /\* in XRender it should be used as so:\
                                      \* x = -lead, xOff = step\
                                      \*/\
     short  ascend, descend ;        /\* distance of the top of the\
                                      \* glyph from the baseline \*/\
                                     /\* in XRender it should be used as so:\
                                      \* y = -ascend, yOff = 0\
                                      \*/\
     unsigned int font\_gid ;         /\* index of the glyph inside the font( TTF only ) \*/\
     long         xrender\_gid ;      /\* Used only with XRender  - gid of the glyph in GlyphSet \*/         }ASGlyph;

libAfterImage/ASGlyphRange

## NAMEASGlyphRange

## DESCRIPTION

Organizes glyphs that belongs to the continuos range of char codes. ASGlyphRange structures could be tied together to cover entire codeset supported by the font.

## SOURCE

typedef struct ASGlyphRange {\
     unsigned long   min\_char, max\_char; /\* Code range.\
                                          \* for some locales that would\
                                          \* be sufficient to simply set\
                                          \* range of characteres\
                                          \* supported by font \*/\
     ASGlyph \*glyphs;                    /\* array of glyphs belonging to\
                                          \* that code range \*/\
     struct ASGlyphRange \*below, \*above; }ASGlyphRange;

libAfterImage/ASFont

## NAMEASFont

## DESCRIPTION

Structure to contain all the font characteristics, as well as set of glyph images. Such structure has to be created/opened prior to being able to draw characters with any font.

## SOURCE

typedef struct ASFont {\
     unsigned long   magic ;\
     int             ref\_count ;

    struct ASFontManager \*fontman;  /\* our owner \*/\
     char                 \*name;

    ASFontType      type ;\
     ASFlagType      flags ;

    ASGlyphRange   \*codemap; /\* linked list of glyphsets, each\
                               \* representing continuos range of\
                               \* available codes - used for ASCII \
                               \* codes \*/\
     ASHashTable    \*locale\_glyphs;  /\* hash of locale specific glyphs \*/

    ASGlyph         default\_glyph;  /\* valid glyph to be drawn when\
                                      \* code is not valid \*/

    int             max\_height;  /\* maximiu height of the character\
                                   \* glyph \*/\
     int             max\_ascend,  /\* maximum distance from the baseline\
                                   \* to the top of the character glyph \*/\
                     max\_descend; /\* need both descend and ascend to be \
                                     able to dynamically recalculate font \
                                     height while adding new characters \*/\
     int             space\_size;  /\* fixed width value to be used when\
                                   \* rendering spaces and tabs \*/\
     int             spacing\_x, spacing\_y;\
     /\* If these are set to anything above 0 then all the glyphs has to be \
      \* padded ( if its smaller then the cell ) or scaled down \
      \* ( if its bigger then the cell )\
      \*/                                \
     int    cell\_width, cell\_height ;

\#ifdef HAVE\_FREETYPE\
     FT\_Face         ft\_face;        /\* free type font handle \*/ #else\
     CARD32         \*pad; #endif

    unsigned long   xrender\_glyphset ;  /\* GlyphSet is the actuall datatype, \
                                          \* but for easier compilation - \
                                          \* we use generic which is the same \*/  }ASFont;

libAfterImage/ASFontManager

## NAMEASFontManager

## DESCRIPTION

Global data identifying connection to external libraries, as well as fonts location paths. This structure has to be created/initialized prior to any font being loaded. It also holds list of fonts that are currently open, allowing for easy access to fonts. When ASFontManagaer object is destroyd it automagically closes all the open fonts.

## SOURCE

typedef struct ASFontManager {\
     Display    \*dpy;\
     char       \*font\_path ;

    ASHashTable \*fonts\_hash ;

    size\_t       unicode\_used;\
     CARD32      \*local\_unicode; /\* list of unicodes from current locale\
                                  \* - we use it to limit number of glyphs\
                                  \* we load \*/\
     Bool        ft\_ok ; #ifdef HAVE\_FREETYPE\
     FT\_Library  ft\_library;     /\* free type library handle \*/ #else\
     void       \*pad ; #endif }ASFontManager;

libAfterImage/ASText3DType

## NAMEASText3DType

\- Available types of 3D text to be drawn.

## SOURCE

typedef enum ASText3DType{\
     AST\_Plain =0,                              /\* regular 2D text \*/\
     AST\_Embossed,\
     AST\_Sunken,\
     AST\_ShadeAbove,\
     AST\_ShadeBelow,\
     AST\_EmbossedThick,\
     AST\_SunkenThick,\
     AST\_OutlineAbove,\
     AST\_OutlineBelow,\
     AST\_OutlineFull,\
     AST\_3DTypes }ASText3DType;

libAfterImage/ASTextAttributes

## NAMEASTextAttributes

\- Attributes for text rendering

## SOURCE

typedef struct ASTextAttributes { #define ASTA\_UseTabStops    (0x01\<\<0)   \
     unsigned int version ;     \
                     /\* structure version, so we can implement future \
                      \* extensions without breaking binary apps \*/             \
     ASFlagType   rendition\_flags ;\
     ASText3DType type;\
     ASCharType   char\_type;\
     unsigned int tab\_size ;   /\* tab size in chars  - defaults to 8 \*/\
     unsigned int origin ;     /\* distance from the left margin \
                                \* (in pixels) \*/\
     unsigned int \*tab\_stops ; /\* tab stops in pixels where \
                                \* left margin is 0 \*/                   \
     unsigned int tab\_stops\_num ;

    ARGB32      fore\_color ;   /\* used with 3D type of Outlined \*/\
   unsigned int width; #define ASTA\_VERSION\_1  1

\#define ASTA\_VERSION\_INTERNAL   ASTA\_VERSION\_1 }ASTextAttributes;

libAfterImage/asfont/create\_font\_manager()

## NAMEcreate\_font\_manager()

## SYNOPSIS

ASFontManager \*create\_font\_manager( Display \*dpy,\
                                     const char \*font\_path,\
                                     ASFontManager \*reusable\_memory );

## INPUTS

- dpy

  \- pointer to valid and opened Display.

- font\_path

  \- string, representing colon separated list of directories to search for FreeType fonts.

- reusable\_memory

  \- optional preallocated memory for the ASFontMagaer object

## RETURN VALUE

Pointer to Initialized ASFontManager object on success. NULL otherwise.

## DESCRIPTION

create\_font\_manager() will create new ASFontManager structure if needed. It wioll then store copy of font\_path and supplied pointer to Display in it. At that time Hash table of loaded fonts is initialized, and if needed FreeType library is initialized as well. ASFontManager object returned by this functions has to be open at all times until text drawing is no longer needed. libAfterImage/asfont/destroy\_font\_manager()

## NAMEdestroy\_font\_manager()

## SYNOPSIS

void destroy\_font\_manager( ASFontManager \*fontman,\
                            Bool reusable );

## INPUTS

- fontman

  \- pointer to valid ASFontManager object to be deallocated.

- reusable

  \- If True, then memory holding object itself will not be freed - only resources will be deallocated. That is useful for structures created on stack.

## DESCRIPTION

destroy\_font\_manager() closes all the fonts open with this ASFontManager. It will also close connection to FreeType library, and deallocate all cached data. If reusable is False - then memory used for object itself will not be freed. libAfterImage/asfont/open\_freetype\_font()

## NAMEopen\_freetype\_font()

## SYNOPSIS

ASFont \*open\_freetype\_font( ASFontManager \*fontman,\
                             const char \*font\_string,\
                             int face\_no,\
                             int size, Bool verbose);

## INPUTS

- fontman

  \- pointer to previously created ASFontManager. Needed for connection to FreeType library, as well as path to search fonts in.

- font\_string

  \- filename of the file containing font's data.

- face\_no

  \- number of face within the font file

- size

  \- font size in points. Applicable only to scalable fonts, such as TrueType.

- verbose

  \- if True, extensive error messages will be printed if problems encountered.

## RETURN VALUE

pointer to Opened ASFont structure, containing all the glyphs of the font, as well as other relevant info. On failure returns NULL.

## DESCRIPTION

open\_freetype\_font() will attempt to find font file in any of the directories specified in ASFontManager's font\_path. If it fails to do so - then it will check if filename has alldigit extensions. It will then try to interpret that extension as a face number, and try and find the file with extension stripped off. If file was found function will atempt to read it using FreeType library. If requested face is not available in the font - face 0 will be used. On success all the font's glyphs will be rendered and cached, and needed font geometry info collected. When FreeType Library is not available that function does nothing. libAfterImage/asfont/open\_X11\_font()

## NAMEopen\_X11\_font()

## SYNOPSIS

ASFont \*open\_X11\_font( ASFontManager \*fontman,\
                        const char \*font\_string);

## INPUTS

- fontman

  \- pointer to previously created ASFontManager. Needed for connection X Server.

- font\_string

  \- name of the font as recognized by Xlib.

## RETURN VALUE

pointer to Opened ASFont structure, containing all the glyphs of the font, as well as other relevant info. On failure returns NULL.

## DESCRIPTION

open\_X11\_font() attempts to load and query font using Xlib calls. On success it goes thgroughthe codemap of the font and renders all the glyphs available. Glyphs then gets transferred to the client's memory and encoded using RLE compression. At this time smoothing filters are applied on glyph pixmaps, if its size exceeds threshold.

## TODO

implement proper XFontSet support, when used with I18N enabled. libAfterImage/asfont/get\_asfont()

## NAMEget\_asfont()

## SYNOPSIS

ASFont \*get\_asfont( ASFontManager \*fontman,\
                     const char \*font\_string,\
                     int face\_no, int size,\
                     ASFontType type );

## INPUTS

- fontman

  \- pointer to previously created ASFontManager. Needed for connection to FreeType library, path to search fonts in, and X Server connection.

- font\_string

  \- font name or filename of the file containing font's data.

- face\_no

  \- number of face within the font file

- size

  \- font size in points. Applicable only to scalable fonts, such as TrueType.

- type

  \- specifies the type of the font, or GuessWho for autodetection.

## RETURN VALUE

pointer to Opened ASFont structure, containing all the glyphs of the font, as well as other relevant info. On failure returns NULL.

## DESCRIPTION

This function provides unified interface to font loading. It performs search in ASFontManager's list to see if this specific font has been loaded already, and if so - returns pointer to relevant structure. Otherwise it tryes to load font as FreeType font first, and then Xlib font, unless exact font type is specifyed. libAfterImage/asfont/release\_font()

## NAMErelease\_font()

## SYNOPSIS

void release\_font( ASFont \*font );

## INPUTS

- font

  \- pointer to the valid ASFont structure containing loaded font.

## RETURN VALUE

returns current reference count. -1 means that object has been destroyed.

## DESCRIPTION

This function will decrement reference count on loaded font and if reference count will be less then 0 ( meaning that release\_font() has been called more times then get\_asfont() ) it will close the font, remove it from ASFontManager's list, destroy all the glyphs and generally free everything else used by ASFont. Otherwise font will remain in memory for faster reuse. libAfterImage/asfont/print\_asfont()

## NAMEprint\_asfont()

## SYNOPSIS

void    print\_asfont( FILE\* stream,\
                       ASFont\* font);

## INPUTS

- stream

  \- output file pointer

- font

  \- pointer to ASFont structure to print.

## DESCRIPTION

prints all the geometry information about font. libAfterImage/asfont/print\_asglyph()

## NAMEprint\_asglyph()

## SYNOPSIS

void    print\_asglyph( FILE\* stream,\
                         ASFont\* font, unsigned long c);

## INPUTS

- stream

  \- output file pointer

- font

  \- pointer to ASFont structure to print.

- c

  \- character code to print glyph for

## DESCRIPTION

prints out contents of the cached glyph for specifyed character code. libAfterImage/asfont/draw\_text()

## NAMEdraw\_text()

draw\_fancy\_text()

get\_text\_size()

get\_fancy\_text\_size();

## SYNOPSIS

ASImage \*draw\_text( const char \*text,\
                     ASFont \*font, ASText3DType type,\
                     int compression ); ASImage \*draw\_fancy\_text( const void \*text,\
                          struct ASFont \*font, ASTextAttributes \*attr,\
                              int compression, \
                              int length ); Bool get\_text\_size( const char \*text,\
                     ASFont \*font, ASText3DType type,\
                     unsigned int \*width, unsigned int \*height ); Bool get\_fancy\_text\_size( const void \*text,\
                              struct ASFont \*font, ASTextAttributes \*attr,\
                          unsigned int \*width, unsigned int \*height,\
                              int length,\
                              int \*x\_positions );

## INPUTS

- text

  \- actuall text to render

- font

  \- pointer to ASFont to render text with

- type

  \- one of the few available types of 3D text.

- compression

  \- compression level to attempt on resulting ASImage.

- width

  \- pointer to value to be set to text width.

- height

  \- pointer to value to be set to text height.

## RETURN VALUE

Pointer to new ASImage containing rendered text on success. NULL on failure.

## DESCRIPTION

draw\_text() creates new ASImage of the size big enough to contain entire text. It then renders the text using supplied font as an alpha channel of ASImage. get\_text\_size() can be used to determine the size of the text about to be drawn, so that appropriate drawable can be prepared. libAfterImage/asfont/get\_asfont\_glyph\_spacing()

## NAMEget\_asfont\_glyph\_spacing()

## SYNOPSIS

Bool get\_asfont\_glyph\_spacing( ASFont\* font, int \*x, int \*y );

## INPUTS

- font

  \- Loaded ASFont structure.

- x

  \- pointer to the variable to receive horizontal spacing value.

- y

  \- pointer to the variable to receive vertical spacing value.

## RETURN VALUE

True if meaningful information has been returned.

## DESCRIPTION

Returns inter-glyph spacing of specified font. libAfterImage/asfont/set\_asfont\_glyph\_spacing()

## NAMEset\_asfont\_glyph\_spacing()

## SYNOPSIS

Bool set\_asfont\_glyph\_spacing( ASFont\* font, int x, int y );

## INPUTS

- font

  \- Loaded ASFont structure.

- x

  \- new horizontal spacing value.

- y

  \- new vertical spacing value.

## RETURN VALUE

TRue on success.

## DESCRIPTION

Changes inter-glyph spacing of the specified font.
