-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathharfbuzz-tutorial.c
104 lines (83 loc) · 2.8 KB
/
harfbuzz-tutorial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <hb.h>
#include <hb-ft.h>
#define FONT_SIZE 36
#define MARGIN (FONT_SIZE * .5)
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];
/* Initialize FreeType and create FreeType font face. */
FT_Library ft_library;
FT_Face ft_face;
FT_Error ft_error;
if ((ft_error = FT_Init_FreeType (&ft_library)))
abort();
if ((ft_error = FT_New_Face (ft_library, fontfile, 0, &ft_face)))
abort();
if ((ft_error = FT_Set_Char_Size (ft_face, FONT_SIZE*64, FONT_SIZE*64, 0, 0)))
abort();
/* Create hb-ft font. */
hb_font_t *hb_font;
hb_font = hb_ft_font_create (ft_face, NULL);
/* 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);
FT_Done_Face (ft_face);
FT_Done_FreeType (ft_library);
return 0;
}