manpagez: man pages & more
html files: harfbuzz
Home | html | info | man

A simple shaping example

Below is the simplest HarfBuzz shaping example possible.

  1. Create a buffer and put your text in it.

      #include <hb.h>
      hb_buffer_t *buf;
      buf = hb_buffer_create();
      hb_buffer_add_utf8(buf, text, -1, 0, -1);
    
  1. Set the script, language and direction of the buffer.

      hb_buffer_set_direction(buf, HB_DIRECTION_LTR);
      hb_buffer_set_script(buf, HB_SCRIPT_LATIN);
      hb_buffer_set_language(buf, hb_language_from_string("en", -1));
    
  1. Create a face and a font, using FreeType for now.

      #include <hb-ft.h>
      FT_New_Face(ft_library, font_path, index, &face);
      FT_Set_Char_Size(face, 0, 1000, 0, 0);
      hb_font_t *font = hb_ft_font_create(face);
    
  1. Shape!

      hb_shape(font, buf, NULL, 0);
    
  1. Get the glyph and position information.

      hb_glyph_info_t *glyph_info    = hb_buffer_get_glyph_infos(buf, &glyph_count);
      hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(buf, &glyph_count);
    
  1. Iterate over each glyph.

      for (i = 0; i < glyph_count; ++i) {
          glyphid = glyph_info[i].codepoint;
          x_offset = glyph_pos[i].x_offset / 64.0;
          y_offset = glyph_pos[i].y_offset / 64.0;
          x_advance = glyph_pos[i].x_advance / 64.0;
          y_advance = glyph_pos[i].y_advance / 64.0;
          draw_glyph(glyphid, cursor_x + x_offset, cursor_y + y_offset);
          cursor_x += x_advance;
          cursor_y += y_advance;
      }
    
  1. Tidy up.

      hb_buffer_destroy(buf);
      hb_font_destroy(hb_ft_font);
    

This example shows enough to get us started using HarfBuzz. In the sections that follow, we will use the remainder of HarfBuzz's API to refine and extend the example and improve its text-shaping capabilities.

© manpagez.com 2000-2024
Individual documents may contain additional copyright information.