-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
hb-utils.c
156 lines (127 loc) · 4.39 KB
/
hb-utils.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "hb-utils.h"
#include <stdlib.h>
#include <hb-ot.h>
int has_table(hb_face_t* face, hb_tag_t tag) {
hb_blob_t *blob = hb_face_reference_table(face, tag);
int ret = hb_blob_get_length(blob) != 0;
hb_blob_destroy(blob);
return ret;
}
static hb_variation_t* scan_variation_string(const char* cp1, unsigned int* ret) {
hb_variation_t* variations = NULL;
hb_variation_t variation;
unsigned int nVariations = 0;
const char* cp2;
if (!cp1)
return NULL;
while (*cp1) {
if ((*cp1 == ':') || (*cp1 == ';') || (*cp1 == ','))
++cp1;
while ((*cp1 == ' ') || (*cp1 == '\t')) /* skip leading whitespace */
++cp1;
if (*cp1 == 0) /* break if end of string */
break;
cp2 = cp1;
while (*cp2 && (*cp2 != ':') && (*cp2 != ';') && (*cp2 != ','))
++cp2;
if (hb_variation_from_string(cp1, cp2 - cp1, &variation)) {
variations = realloc(variations, (nVariations + 1) * sizeof(hb_variation_t));
variations[nVariations++] = variation;
}
cp1 = cp2;
}
*ret = nVariations;
return variations;
}
hb_font_t* get_hb_font(lua_State *L, int index) {
const char * filename;
int face_index = 0;
hb_blob_t* blob;
hb_face_t* face;
hb_font_t* font;
unsigned int upem;
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, "hbFont");
if (lua_islightuserdata(L, -1)) { return lua_touserdata(L, -1); }
lua_getfield(L, index, "filename");
if (lua_isstring(L, -1)) { filename = lua_tostring(L, -1); }
else { luaL_error(L, "No font filename supplied to get_glyph_dimensions"); }
lua_getfield(L, index, "index");
if (lua_isnumber(L, -1)) { face_index = lua_tointeger(L, -1); }
blob = hb_blob_create_from_file(filename);
face = hb_face_create(blob, face_index);
font = hb_font_create(face);
upem = hb_face_get_upem(face);
hb_font_set_scale(font, upem, upem);
hb_ot_font_set_funcs(font);
#ifdef HAVE_HARFBUZZ_SUBSET
if (hb_ot_var_has_data(face)) {
hb_ot_var_axis_info_t* axes;
unsigned int nAxes;
unsigned int nCoords;
const float* coords;
float* newCoords;
/* Get font axes */
nAxes = hb_ot_var_get_axis_infos(face, 0, NULL, NULL);
axes = malloc(nAxes * sizeof(hb_ot_var_axis_info_t));
hb_ot_var_get_axis_infos(face, 0, &nAxes, axes);
/* Get existing variation coords, e.g. from named instance */
coords = hb_font_get_var_coords_design(font, &nCoords);
/* Set up new variation coords */
/* First copy existing coords (e.g. from named instance) or use axis
* default value. */
newCoords = malloc(nAxes * sizeof(float));
for (unsigned i = 0; i < nAxes; i++) {
if (i < nCoords)
newCoords[i] = coords[i];
else
newCoords[i] = axes[i].default_value;
}
/* Then we set variation axes that have corresponding font options. */
for (unsigned i = 0; i < nAxes; i++) {
switch (axes[i].tag) {
case HB_TAG('o', 'p', 's', 'z'):
lua_getfield(L, index, "pointsize");
if (lua_isnumber(L, -1)) { newCoords[i] = luaL_checknumber(L, -1); }
break;
case HB_TAG('w', 'g', 'h', 't'):
lua_getfield(L, index, "weight");
if (lua_isnumber(L, -1)) { newCoords[i] = luaL_checknumber(L, -1); }
break;
default: break;
}
}
/* Finally use any explicitly set variations */
lua_getfield(L, index, "variations");
if (lua_isstring(L, -1)) {
const char* variationstring = lua_tostring(L, -1);
unsigned int nVariations = 0;
hb_variation_t* variations = scan_variation_string(variationstring, &nVariations);
if (variations) {
for (unsigned nVariation = 0; nVariation < nVariations; nVariation++) {
for (unsigned nAxis = 0; nAxis < nAxes; nAxis++) {
if (variations[nVariation].tag == axes[nAxis].tag)
newCoords[nAxis] = variations[nVariation].value;
}
}
free(variations);
}
}
/* Don’t apply variations if we can’t instanciate the font.
* Except if the font does not have glyph variations (i.e. only feature
* variations).
* */
#ifndef HAVE_HARFBUZZ_SUBSET
if (!has_table(face, HB_TAG('g', 'v', 'a', 'r'))
#endif
hb_font_set_var_coords_design(font, newCoords, nAxes);
free(axes);
free(newCoords);
}
#endif
hb_face_destroy(face);
hb_blob_destroy(blob);
lua_pushlightuserdata(L, font);
lua_setfield(L, index, "hbFont");
return font;
}