Skip to content

Commit

Permalink
Add OpenType example
Browse files Browse the repository at this point in the history
hello-harfbuzz-opentype uses HarfBuzz's native OpenType implementation
and has no other dependencies (e.g. on FreeType or Cairo). It only
outputs glyph and offset information and does no rendering.
  • Loading branch information
nasser committed Mar 12, 2018
1 parent 337e68f commit 164edab
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 6 deletions.
19 changes: 13 additions & 6 deletions Makefile
@@ -1,9 +1,16 @@
PKGS = harfbuzz cairo-ft freetype2
HB_PKGS = harfbuzz
FT_PKGS = harfbuzz cairo-ft freetype2

CFLAGS = `pkg-config --cflags $(PKGS)`
LDFLAGS = `pkg-config --libs $(PKGS)` -lm
HB_CFLAGS = `pkg-config --cflags $(HB_PKGS)`
HB_LDFLAGS = `pkg-config --libs $(HB_PKGS)` -lm

all: hello-harfbuzz
FT_CFLAGS = `pkg-config --cflags $(FT_PKGS)`
FT_LDFLAGS = `pkg-config --libs $(FT_PKGS)` -lm

%: %.c
$(CC) -std=c99 -o $@ $^ $(CFLAGS) $(LDFLAGS)
all: hello-harfbuzz-freetype hello-harfbuzz-opentype

hello-harfbuzz-opentype: hello-harfbuzz-opentype.c
$(CC) -std=c99 -o $@ $^ $(HB_CFLAGS) $(HB_LDFLAGS)

hello-harfbuzz-freetype: hello-harfbuzz-freetype.c
$(CC) -std=c99 -o $@ $^ $(FT_CFLAGS) $(FT_LDFLAGS)
File renamed without changes.
113 changes: 113 additions & 0 deletions hello-harfbuzz-opentype.c
@@ -0,0 +1,113 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <hb.h>
#include <hb-ot.h>

#define FONT_SIZE 36
#define MARGIN (FONT_SIZE * .5)

/* Use native open type implementation to load font
https://github.com/harfbuzz/harfbuzz/issues/255 */
hb_font_t*
get_font_ot(const char *filename, int size)
{
FILE* file = fopen(filename, "rb");
fseek(file, 0, SEEK_END);
unsigned int length = ftell(file);
fseek(file, 0, SEEK_SET);

char* data = malloc(length);
fread(data, length, 1, file);
fclose(file);

hb_blob_t* blob = hb_blob_create(data, length, HB_MEMORY_MODE_WRITABLE, (void*)data, NULL);
hb_face_t* face = hb_face_create(blob, 0);
hb_font_t* font = hb_font_create(face);

hb_ot_font_set_funcs(font);
hb_font_set_scale(font, size, size);

return font;
}

int
main(int argc, char **argv)
{
const char *fontfile;
const char *text;

if (argc < 3)
{
fprintf (stderr, "usage: hello-harfbuzz font-file.ttf text\n");
exit (1);
}

fontfile = argv[1];
text = argv[2];

/* Create hb-ft font. */
hb_font_t *hb_font;
hb_font = get_font_ot (fontfile, FONT_SIZE*64);

/* Create hb-buffer and populate. */
hb_buffer_t *hb_buffer;
hb_buffer = hb_buffer_create ();
hb_buffer_add_utf8 (hb_buffer, text, -1, 0, -1);
hb_buffer_guess_segment_properties (hb_buffer);

/* Shape it! */
hb_shape (hb_font, hb_buffer, NULL, 0);

/* Get glyph information and positions out of the buffer. */
unsigned int len = hb_buffer_get_length (hb_buffer);
hb_glyph_info_t *info = hb_buffer_get_glyph_infos (hb_buffer, NULL);
hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (hb_buffer, NULL);

/* Print them out as is. */
printf ("Raw buffer contents:\n");
for (unsigned int i = 0; i < len; i++)
{
hb_codepoint_t gid = info[i].codepoint;
unsigned int cluster = info[i].cluster;
double x_advance = pos[i].x_advance / 64.;
double y_advance = pos[i].y_advance / 64.;
double x_offset = pos[i].x_offset / 64.;
double y_offset = pos[i].y_offset / 64.;

char glyphname[32];
hb_font_get_glyph_name (hb_font, gid, glyphname, sizeof (glyphname));

printf ("glyph='%s' cluster=%d advance=(%g,%g) offset=(%g,%g)\n",
glyphname, cluster, x_advance, y_advance, x_offset, y_offset);
}

printf ("Converted to absolute positions:\n");
/* And converted to absolute positions. */
{
double current_x = 0;
double current_y = 0;
for (unsigned int i = 0; i < len; i++)
{
hb_codepoint_t gid = info[i].codepoint;
unsigned int cluster = info[i].cluster;
double x_position = current_x + pos[i].x_offset / 64.;
double y_position = current_y + pos[i].y_offset / 64.;


char glyphname[32];
hb_font_get_glyph_name (hb_font, gid, glyphname, sizeof (glyphname));

printf ("glyph='%s' cluster=%d position=(%g,%g)\n",
glyphname, cluster, x_position, y_position);

current_x += pos[i].x_advance / 64.;
current_y += pos[i].y_advance / 64.;
}
}

hb_buffer_destroy (hb_buffer);
hb_font_destroy (hb_font);

return 0;
}

0 comments on commit 164edab

Please sign in to comment.