Raqm

Raqm — A library for complex text layout

Functions

Types and Values

Object Hierarchy


Includes

#include <raqm.h>

Description

Raqm is a light weight text layout library with strong emphasis on supporting languages and writing systems that require complex text layout.

The main object in Raqm API is raqm_t, it stores all the states of the input text, its properties, and the output of the layout process.

To start, you create a raqm_t object, add text and font(s) to it, run the layout process, and finally query about the output. For example:

#include "raqm.h"

int
main (int argc, char *argv[])
{
    const char *fontfile;
    const char *text;
    const char *direction;
    const char *language;
    int ret = 1;

    FT_Library library = NULL;
    FT_Face face = NULL;

    if (argc < 5)
    {
        printf ("Usage: %s FONT_FILE TEXT DIRECTION LANG\n", argv[0]);
        return 1;
    }

    fontfile =  argv[1];
    text = argv[2];
    direction = argv[3];
    language = argv[4];

    if (FT_Init_FreeType (&library) == 0)
    {
      if (FT_New_Face (library, fontfile, 0, &face) == 0)
      {
        if (FT_Set_Char_Size (face, face->units_per_EM, 0, 0, 0) == 0)
        {
          raqm_t *rq = raqm_create ();
          if (rq != NULL)
          {
            raqm_direction_t dir = RAQM_DIRECTION_DEFAULT;

            if (strcmp (direction, "r") == 0)
              dir = RAQM_DIRECTION_RTL;
            else if (strcmp (direction, "l") == 0)
              dir = RAQM_DIRECTION_LTR;

            if (raqm_set_text_utf8 (rq, text, strlen (text)) &&
                raqm_set_freetype_face (rq, face) &&
                raqm_set_par_direction (rq, dir) &&
                raqm_set_language (rq, language, 0, strlen (text)) &&
                raqm_layout (rq))
            {
              size_t count, i;
              raqm_glyph_t *glyphs = raqm_get_glyphs (rq, &count);

              ret = !(glyphs != NULL || count == 0);

              printf("glyph count: %zu\n", count);
              for (i = 0; i < count; i++)
              {
                  printf ("gid#%d off: (%d, %d) adv: (%d, %d) idx: %d\n",
                          glyphs[i].index,
                          glyphs[i].x_offset,
                          glyphs[i].y_offset,
                          glyphs[i].x_advance,
                          glyphs[i].y_advance,
                          glyphs[i].cluster);
              }
            }

            raqm_destroy (rq);
          }
        }

        FT_Done_Face (face);
      }

      FT_Done_FreeType (library);
    }

    return ret;
}

To compile this example:

cc -o test test.c `pkg-config --libs --cflags raqm`

Functions

raqm_create ()

raqm_t *
raqm_create (void);

Creates a new raqm_t with all its internal states initialized to their defaults.

Returns

A newly allocated raqm_t with a reference count of 1. The initial reference count should be released with raqm_destroy() when you are done using the raqm_t. Returns NULL in case of error.

Since 0.1


raqm_reference ()

raqm_t *
raqm_reference (raqm_t *rq);

Increases the reference count on rq by one. This prevents rq from being destroyed until a matching call to raqm_destroy() is made.

Parameters

rq

a raqm_t.

 

Returns

The referenced raqm_t.

Since 0.1


raqm_destroy ()

void
raqm_destroy (raqm_t *rq);

Decreases the reference count on rq by one. If the result is zero, then rq and all associated resources are freed. See cairo_reference().

Parameters

rq

a raqm_t.

 

Since 0.1


raqm_set_text ()

bool
raqm_set_text (raqm_t *rq,
               const uint32_t *text,
               size_t len);

Adds text to rq to be used for layout. It must be a valid UTF-32 text, any invalid character will be replaced with U+FFFD. The text should typically represent a full paragraph, since doing the layout of chunks of text separately can give improper output.

Parameters

rq

a raqm_t.

 

text

a UTF-32 encoded text string.

 

len

the length of text .

 

Returns

true if no errors happened, false otherwise.

Since 0.1


raqm_set_text_utf8 ()

bool
raqm_set_text_utf8 (raqm_t *rq,
                    const char *text,
                    size_t len);

Same as raqm_set_text(), but for text encoded in UTF-8 encoding.

Parameters

rq

a raqm_t.

 

text

a UTF-8 encoded text string.

 

len

the length of text .

 

Returns

true if no errors happened, false otherwise.

Since 0.1


raqm_set_par_direction ()

bool
raqm_set_par_direction (raqm_t *rq,
                        raqm_direction_t dir);

Sets the paragraph direction, also known as block direction in CSS. For horizontal text, this controls the overall direction in the Unicode Bidirectional Algorithm, so when the text is mainly right-to-left (with or without some left-to-right) text, then the base direction should be set to RAQM_DIRECTION_RTL and vice versa.

The default is RAQM_DIRECTION_DEFAULT, which determines the paragraph direction based on the first character with strong bidi type (see rule P2 in Unicode Bidirectional Algorithm), which can be good enough for many cases but has problems when a mainly right-to-left paragraph starts with a left-to-right character and vice versa as the detected paragraph direction will be the wrong one, or when text does not contain any characters with string bidi types (e.g. only punctuation or numbers) as this will default to left-to-right paragraph direction.

For vertical, top-to-bottom text, RAQM_DIRECTION_TTB should be used. Raqm, however, provides limited vertical text support and does not handle rotated horizontal text in vertical text, instead everything is treated as vertical text.

Parameters

rq

a raqm_t.

 

dir

the direction of the paragraph.

 

Returns

true if no errors happened, false otherwise.

Since 0.1


raqm_set_language ()

bool
raqm_set_language (raqm_t *rq,
                   const char *lang,
                   size_t start,
                   size_t len);

Sets a BCP47 language code to be used for len -number of characters staring at start . The start and len are input string array indices (i.e. counting bytes in UTF-8 and scaler values in UTF-32).

This method can be used repeatedly to set different languages for different parts of the text.

Parameters

rq

a raqm_t.

 

lang

a BCP47 language code.

 

start

index of first character that should use face .

 

len

number of characters using face .

 

Returns

true if no errors happened, false otherwise.

Since 0.2

Stability Level: Unstable


raqm_set_freetype_face ()

bool
raqm_set_freetype_face (raqm_t *rq,
                        FT_Face face);

Sets an FT_Face to be used for all characters in rq .

See also raqm_set_freetype_face_range().

Parameters

rq

a raqm_t.

 

face

an FT_Face.

 

Returns

true if no errors happened, false otherwise.

Since 0.1


raqm_set_freetype_face_range ()

bool
raqm_set_freetype_face_range (raqm_t *rq,
                              FT_Face face,
                              size_t start,
                              size_t len);

Sets an FT_Face to be used for len -number of characters staring at start . The start and len are input string array indices (i.e. counting bytes in UTF-8 and scaler values in UTF-32).

This method can be used repeatedly to set different faces for different parts of the text. It is the responsibility of the client to make sure that face ranges cover the whole text.

See also raqm_set_freetype_face().

Parameters

rq

a raqm_t.

 

face

an FT_Face.

 

start

index of first character that should use face .

 

len

number of characters using face .

 

Returns

true if no errors happened, false otherwise.

Since 0.1


raqm_set_freetype_load_flags ()

bool
raqm_set_freetype_load_flags (raqm_t *rq,
                              int flags);

Sets the load flags passed to FreeType when loading glyphs, should be the same flags used by the client when rendering FreeType glyphs.

This requires version of HarfBuzz that has hb_ft_font_set_load_flags(), for older version the flags will be ignored.

Parameters

rq

a raqm_t.

 

flags

FreeType load flags.

 

Returns

true if no errors happened, false otherwise.

Since 0.3


raqm_add_font_feature ()

bool
raqm_add_font_feature (raqm_t *rq,
                       const char *feature,
                       int len);

Adds a font feature to be used by the raqm_t during text layout. This is usually used to turn on optional font features that are not enabled by default, for example dlig or ss01, but can be also used to turn off default font features.

feature is string representing a single font feature, in the syntax understood by hb_feature_from_string().

This function can be called repeatedly, new features will be appended to the end of the features list and can potentially override previous features.

Parameters

rq

a raqm_t.

 

feature

a font feature string.

[transfer none]

len

length of feature , -1 for NULL-terminated.

 

Returns

true if parsing feature succeeded, false otherwise.

Since 0.1


raqm_layout ()

bool
raqm_layout (raqm_t *rq);

Run the text layout process on rq . This is the main Raqm function where the Unicode Bidirectional Text algorithm will be applied to the text in rq , text shaping, and any other part of the layout process.

Parameters

rq

a raqm_t.

 

Returns

true if the layout process was successful, false otherwise.

Since 0.1


raqm_get_glyphs ()

raqm_glyph_t *
raqm_get_glyphs (raqm_t *rq,
                 size_t *length);

Gets the final result of Raqm layout process, an array of raqm_glyph_t containing the glyph indices in the font, their positions and other possible information.

Parameters

rq

a raqm_t.

 

length

output array length.

[out]

Returns

An array of raqm_glyph_t, or NULL in case of error. This is owned by rq and must not be freed.

[transfer none]

Since 0.1


raqm_index_to_position ()

bool
raqm_index_to_position (raqm_t *rq,
                        size_t *index,
                        int *x,
                        int *y);

Calculates the cursor position after the character at index . If the character is right-to-left, then the cursor will be at the left of it, whereas if the character is left-to-right, then the cursor will be at the right of it.

Parameters

rq

a raqm_t.

 

index

character index.

[inout]

x

output x position.

[out]

y

output y position.

[out]

Returns

true if the process was successful, false otherwise.

Since 0.2


raqm_position_to_index ()

bool
raqm_position_to_index (raqm_t *rq,
                        int x,
                        int y,
                        size_t *index);

Returns the index of the character at x and y position within text. If the position is outside the text, the last character is chosen as index .

Parameters

rq

a raqm_t.

 

x

x position.

 

y

y position.

 

index

output character index.

[out]

Returns

true if the process was successful, false in case of error.

Since 0.2

Types and Values

raqm_t

typedef struct _raqm raqm_t;

This is the main object holding all state of the currently processed text as well as its output.

Since 0.1


enum raqm_direction_t

Base paragraph direction, see raqm_set_par_direction().

Members

RAQM_DIRECTION_DEFAULT

Detect paragraph direction automatically.

 

RAQM_DIRECTION_RTL

Paragraph is mainly right-to-left text.

 

RAQM_DIRECTION_LTR

Paragraph is mainly left-to-right text.

 

RAQM_DIRECTION_TTB

Paragraph is mainly vertical top-to-bottom text.

 

Since 0.1


raqm_glyph_t

typedef struct {
    unsigned int index;
    int x_advance;
    int y_advance;
    int x_offset;
    int y_offset;
    uint32_t cluster;
    FT_Face ftface;
} raqm_glyph_t;

The structure that holds information about output glyphs, returned from raqm_get_glyphs().

Members

unsigned int index;

the index of the glyph in the font file.

 

int x_advance;

the glyph advance width in horizontal text.

 

int y_advance;

the glyph advance width in vertical text.

 

int x_offset;

the horizontal movement of the glyph from the current point.

 

int y_offset;

the vertical movement of the glyph from the current point.

 

uint32_t cluster;

the index of original character in input text.

 

FT_Face ftface;

the FT_Face of the glyph.