forked from flh/ocaml-fontconfig
/
pattern.c
294 lines (258 loc) · 6.51 KB
/
pattern.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/custom.h>
#include <caml/callback.h>
#include <caml/fail.h>
#include <fontconfig/fontconfig.h>
#include "fccaml.h"
/* Matrix conversions from/to Caml, defined in matrix.c */
FcMatrix *fcmatrix_from_caml(value m);
value caml_from_fcmatrix(const FcMatrix *m);
/* Constant arrays defined in types.c */
extern const int fcSlant[];
extern const int fcWidth[];
extern const int fcWeight[];
/* Custom type for OCaml, boxing the FcPattern struct */
/* Accessing the FcPattern part of an OCaml custom block */
#define FcPattern_val(v) (*((FcPattern **) Data_custom_val(v)))
void fcpattern_finalize(value v)
{
FcPatternDestroy(FcPattern_val(v));
}
long fcpattern_hash(value v)
{
long hash = 0;
hash += FcPatternHash(FcPattern_val(v));
return hash;
}
static struct custom_operations fcpattern_ops =
{
"fr.freedesktop.org.Software.fontconfig.FcPattern",
&fcpattern_finalize,
custom_compare_default,
&fcpattern_hash,
custom_serialize_default,
custom_deserialize_default
};
FcValue fcvalue_from_caml(value c)
{
FcValue res;
if(Is_block(c)) {
switch(Tag_val(c)) {
case 0: /* Integer */
res.type = FcTypeInteger;
res.u.i = Int_val(Field(c, 0));
break;
case 1: /* Double */
res.type = FcTypeDouble;
res.u.d = Double_val(Field(c, 0));
break;
case 2: /* String */
res.type = FcTypeString;
res.u.s = FcStrCopy((FcChar8 *)String_val(Field(c, 0)));
break;
case 3: /* Bool */
res.type = FcTypeBool;
res.u.b = Bool_val(Field(c, 0)) ? FcTrue : FcFalse;
break;
case 4: /* Matrix */
res.type = FcTypeMatrix;
res.u.m = fcmatrix_from_caml(Field(c, 0));
break;
}
}
else {
/* C'est void */
res.type = FcTypeVoid;
}
return res;
}
value caml_from_fcvalue(FcValue v)
{
CAMLparam0();
CAMLlocal2(res, arr);
switch(v.type) {
case FcTypeVoid:
res = Val_int(0);
break;
case FcTypeInteger:
res = caml_alloc(1, 0);
Store_field(res, 0, Val_int(v.u.i));
break;
case FcTypeDouble:
res = caml_alloc(1, 1);
Store_field(res, 0, caml_copy_double(v.u.d));
break;
case FcTypeString:
res = caml_alloc(1, 2);
Store_field(res, 0, caml_copy_string((char *)v.u.s));
break;
case FcTypeBool:
res = caml_alloc(1, 3);
Store_field(res, 0, v.u.b ? Val_true : Val_false);
break;
case FcTypeMatrix:
res = caml_from_fcmatrix(v.u.m);
default:
/* caml_invalid_argument ? */
break;
}
CAMLreturn(res);
}
/* Module Pattern */
value caml_box_fcpattern(FcPattern *pat)
{
CAMLparam0();
CAMLlocal1(res);
res = caml_alloc_custom(&fcpattern_ops, sizeof(FcPattern *), 0, 1);
FcPattern_val(res) = pat;
CAMLreturn(res);
}
value caml_copy_pattern(FcPattern *pat)
{
CAMLparam0();
CAMLlocal1(res);
CAMLreturn(caml_box_fcpattern(FcPatternDuplicate(pat)));
}
CAMLprim value pattern_make(value props)
{
CAMLparam0();
CAMLreturn(caml_box_fcpattern(FcPatternCreate()));
}
CAMLprim value pattern_get(value pat, value prop, value id)
{
CAMLparam0();
CAMLlocal1(res);
FcResult result;
FcValue val;
result = FcPatternGet(FcPattern_val(pat), String_val(prop), Int_val(id), &val);
switch(result) {
case FcResultMatch:
res = caml_from_fcvalue(val);
break;
case FcResultNoId:
caml_invalid_argument("pattern object id");
break;
default:
caml_invalid_argument("pattern object unsupported type");
break;
}
CAMLreturn(res);
}
CAMLprim value pattern_add(value pat, value prop, value append)
{
CAMLparam0();
FcPatternAdd(FcPattern_val(pat),
String_val(Field(prop, 0)),
fcvalue_from_caml(Field(prop, 1)),
Bool_val(append) ? FcTrue : FcFalse);
CAMLreturn(Val_unit);
}
CAMLprim value pattern_addweak(value pat, value prop, value append)
{
CAMLparam0();
FcPatternAddWeak(FcPattern_val(pat),
String_val(Field(prop, 0)),
fcvalue_from_caml(Field(prop, 1)),
Bool_val(append) ? FcTrue : FcFalse);
CAMLreturn(Val_unit);
}
CAMLprim value pattern_del(value pat, value prop)
{
CAMLparam0();
FcBool res = FcPatternDel(FcPattern_val(pat),
String_val(prop));
CAMLreturn(CamlFcBool(res));
}
CAMLprim value pattern_copy(value pat)
{
CAMLparam0();
CAMLreturn(caml_copy_pattern(FcPattern_val(pat)));
}
CAMLprim value pattern_remove(value pat, value prop, value pos)
{
CAMLparam0();
FcBool res = FcPatternRemove(FcPattern_val(pat),
String_val(prop),
Int_val(pos));
CAMLreturn(CamlFcBool(res));
}
CAMLprim value default_substitute(value pat)
{
CAMLparam0();
FcDefaultSubstitute(FcPattern_val(pat));
CAMLreturn(Val_unit);
}
CAMLprim value pattern_format(value pat, value format)
{
CAMLparam0();
CAMLlocal1(res);
char *str_res = (char *)FcPatternFormat(FcPattern_val(pat),
(FcChar8 *) String_val(format));
if(str_res == NULL) {
caml_invalid_argument("pattern format");
}
else {
res = caml_copy_string(str_res);
free(str_res);
}
CAMLreturn(res);
}
CAMLprim value slant_to_int(value v)
{
CAMLparam0();
CAMLreturn(Val_int(fcSlant[Int_val(v)]));
}
CAMLprim value width_to_int(value v)
{
CAMLparam0();
CAMLreturn(Val_int(fcWidth[Int_val(v)]));
}
CAMLprim value weight_to_int(value v)
{
CAMLparam0();
CAMLreturn(Val_int(fcWeight[Int_val(v)]));
}
CAMLprim value pattern_find_font(value plist)
{
CAMLparam0();
CAMLlocal1(res);
FcPattern *pat, *match;
FcResult result = FcResultMatch;
pat = FcPattern_val(plist);
FcConfigSubstitute(NULL, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
match = FcFontMatch(NULL, pat, &result);
if(result == FcResultMatch) {
res = caml_box_fcpattern(match);
}
else {
caml_raise_constant(*caml_named_value("not_found exception"));
}
CAMLreturn(res);
}
CAMLprim value pattern_font_sort(value plist, value trim)
{
CAMLparam0();
CAMLlocal2(res, nres);
FcPattern *pat;
FcFontSet *match;
FcResult result;
int i;
pat = FcPattern_val(plist);
FcConfigSubstitute(NULL, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
match = FcFontSort(NULL, pat, Bool_val(trim) ? FcTrue : FcFalse, NULL, &result);
/* Reconstruire la belle liste */
res = Val_int(0); /* empty list */
for(i = match->nfont; i >= 0; i--) {
nres = caml_alloc(2, 0);
Store_field(nres, 0, caml_copy_pattern(match->fonts[i]));
Store_field(nres, 1, res);
res = nres;
}
FcFontSetDestroy(match);
FcPatternDestroy(pat);
CAMLreturn(res);
}