-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
justenoughfontconfig.c
171 lines (142 loc) · 4.52 KB
/
justenoughfontconfig.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#define _GNU_SOURCE
#include <string.h>
#include <fontconfig/fontconfig.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "silewin32.h"
// #define COMPAT53_PREFIX compat53
#include "compat-5.3.h"
int je_face_from_options(lua_State* L) {
FcChar8 * font_path, * fullname, * familyname;
FcPattern* p;
FcPattern* matched;
FcResult result;
int index = 0;
const char *family = "Gentium";
double pointSize = 12;
int slant = FC_SLANT_ROMAN;
int weight = 100;
const char *script = "latin";
const char *language = "eng";
const char *style = "";
if (!lua_istable(L, 1)) return 0;
lua_pushstring(L, "size");
lua_gettable(L, -2);
if (lua_isnumber(L, -1)) { pointSize = lua_tonumber(L, -1); }
lua_pop(L,1);
lua_pushstring(L, "filename");
lua_gettable(L, -2);
if (lua_isstring(L, -1)) {
font_path = (char*)lua_tostring(L, -1);
lua_pop(L,1);
lua_newtable(L);
lua_pushstring(L, "filename");
lua_pushstring(L, (char*)font_path);
lua_settable(L, -3);
goto done_match;
}
lua_pop(L,1);
lua_pushstring(L, "family");
lua_gettable(L, -2);
if (lua_isstring(L, -1)) { family = lua_tostring(L, -1); }
lua_pop(L,1);
lua_pushstring(L, "weight");
lua_gettable(L, -2);
if (lua_isnumber(L, -1)) {
int newWeight = lua_tointeger(L, -1);
#if FC_VERSION >= 21191
newWeight = FcWeightFromOpenType(newWeight);
#else
if (newWeight <= 100) newWeight = FC_WEIGHT_THIN;
else if (newWeight <= 200) newWeight = FC_WEIGHT_ULTRALIGHT;
else if (newWeight <= 300) newWeight = FC_WEIGHT_LIGHT;
else if (newWeight <= 400) newWeight = FC_WEIGHT_NORMAL;
else if (newWeight <= 500) newWeight = FC_WEIGHT_MEDIUM;
else if (newWeight <= 600) newWeight = FC_WEIGHT_DEMIBOLD;
else if (newWeight <= 700) newWeight = FC_WEIGHT_BOLD;
else if (newWeight <= 800) newWeight = FC_WEIGHT_ULTRABOLD;
else newWeight = FC_WEIGHT_HEAVY;
#endif
weight = newWeight;
}
lua_pop(L,1);
lua_pushstring(L, "language");
lua_gettable(L, -2);
if (lua_isstring(L, -1)) { language = lua_tostring(L, -1); }
lua_pop(L,1);
lua_pushstring(L, "style");
lua_gettable(L, -2);
if (lua_isstring(L, -1)) {
style = lua_tostring(L, -1);
if (strcasestr(style,"italic")) {
slant = FC_SLANT_ITALIC;
style = "\0";
} else if (strcasestr(style, "oblique")) {
slant = FC_SLANT_OBLIQUE;
style = "\0";
}
}
lua_pop(L,1);
p = FcPatternCreate();
FcPatternAddString (p, FC_FAMILY, (FcChar8*)(family));
FcPatternAddDouble (p, FC_SIZE, pointSize);
if (strlen(style) > 0) {
FcPatternAddString (p, FC_STYLE, style);
} else {
FcPatternAddInteger(p, FC_SLANT, slant);
}
FcPatternAddInteger(p, FC_WEIGHT, weight);
// /* Add fallback fonts here. Some of the standard 14 should be fine. */
FcPatternAddString (p, FC_FAMILY,(FcChar8*) "Times-Roman");
FcPatternAddString (p, FC_FAMILY,(FcChar8*) "Times");
FcPatternAddString (p, FC_FAMILY,(FcChar8*) "Helvetica");
FcConfigSubstitute (NULL, p, FcMatchFont);
FcDefaultSubstitute (p);
matched = FcFontMatch (0, p, &result);
if (FcPatternGetString (matched, FC_FILE, 0, &font_path) != FcResultMatch)
return 0;
FcPatternGetInteger(matched, FC_INDEX, 0, &index);
font_path = (FcChar8 *)strdup((char*)font_path);
if (!font_path) {
printf("Finding font path failed\n");
return 0;
}
/* Push back slant and weight, we need to pass them to libpdftex */
FcPatternGetInteger(matched, FC_SLANT, 0, &slant);
FcPatternGetInteger(matched, FC_WEIGHT, 0, &weight);
/* Find out which family we did actually pick up */
if (FcPatternGetString (matched, FC_FAMILY, 0, &familyname) != FcResultMatch)
return 0;
lua_newtable(L);
lua_pushstring(L, "filename");
lua_pushstring(L, (char*)font_path);
lua_settable(L, -3);
lua_pushstring(L, "family");
lua_pushstring(L, (char*)(familyname));
lua_settable(L, -3);
if (FcPatternGetString (matched, FC_FULLNAME, 0, &fullname) == FcResultMatch) {
lua_pushstring(L, "fullname");
lua_pushstring(L, (char*)(fullname));
lua_settable(L, -3);
}
FcPatternDestroy (matched);
FcPatternDestroy (p);
done_match:
lua_pushstring(L, "index");
lua_pushinteger(L, index);
lua_settable(L, -3);
lua_pushstring(L, "pointsize");
lua_pushnumber(L, pointSize);
lua_settable(L, -3);
return 1;
}
static const struct luaL_Reg lib_table [] = {
{"_face", je_face_from_options},
{NULL, NULL}
};
int luaopen_justenoughfontconfig (lua_State *L) {
lua_newtable(L);
luaL_setfuncs(L, lib_table, 0);
return 1;
}