~alcinnz/fontconfig-pure

ref: 18b29da8fa2cc23650f2fd33c0917bcc2289a7b6 fontconfig-pure/cbits/fontconfig-wrap.c -rw-r--r-- 20.6 KiB
18b29da8 — Adrian Cochrane Expose font weight adjustments & correct current-config refcount. 8 months ago
                                                                                
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
#include "transcode.h"
#include <fontconfig/fontconfig.h>
#include <stdlib.h>
#include <fontconfig/fcfreetype.h>

int fcPatternEqualSubset(uint8_t *data, size_t length) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, data, length)) return -1;
    uint32_t size = 0;
    if (!cmp_read_array(&in, &size) || size != 3) {
        cmp_bytes_take(&in, NULL);
        return -1;
    }
    FcPattern *pa = decodePattern(&in);
    if (pa == NULL) {cmp_bytes_take(&in, NULL); return -1;}
    FcPattern *pb = decodePattern(&in);
    if (pb == NULL) {
        cmp_bytes_take(&in, NULL);
        FcPatternDestroy(pa);
        return -1;
    }
    FcObjectSet *os = decodeObjectSet(&in);
    cmp_bytes_take(&in, NULL);

    int ret = -1;
    if (os != NULL) ret = FcPatternEqualSubset(pa, pb, os) ? 1 : 0;

    FcPatternDestroy(pa);
    FcPatternDestroy(pb);
    FcObjectSetDestroy(os);
    return ret;
}

uint8_t *fcDefaultSubstitute(uint8_t *data, size_t in_length, size_t *length) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, data, in_length)) return NULL;
    FcPattern *pat = decodePattern(&in);
    cmp_bytes_take(&in, NULL);
    if (pat == NULL) return NULL;

    FcDefaultSubstitute(pat);

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {FcPatternDestroy(pat); return NULL;}
    if (!encodePattern(&out, pat)) {
        cmp_bytes_free(&out);
        FcPatternDestroy(pat);
        return NULL;
    }
    FcPatternDestroy(pat);
    return cmp_bytes_take(&out, length);
}

uint8_t *fcNameParse(char *name, size_t *length) {
    FcPattern *pat = FcNameParse(name);
    if (pat == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {FcPatternDestroy(pat); return NULL;}
    if (!encodePattern(&out, pat)) {
        FcPatternDestroy(pat); cmp_bytes_free(&out); return NULL;
    }
    FcPatternDestroy(pat);
    return cmp_bytes_take(&out, length);
}

char *fcNameUnparse(uint8_t *data, size_t length) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, data, length)) return NULL;
    FcPattern *pat = decodePattern(&in);
    cmp_bytes_take(&in, NULL); // Caller frees `data`!
    if (pat == NULL) return NULL;

    char *ret = FcNameUnparse(pat);
    FcPatternDestroy(pat);
    return ret;
}

char *fcNameFormat(uint8_t *data, size_t length, char *format) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, data, length)) return NULL;
    FcPattern *pat = decodePattern(&in);
    cmp_bytes_take(&in, NULL); // Caller frees `data`!
    if (pat == NULL) return NULL;

    char *ret = FcPatternFormat(pat, format);
    FcPatternDestroy(pat);
    return ret;
}

uint8_t *fcFontSetList(FcConfig *config, uint8_t *sets, size_t sets_length,
        uint8_t *pat, size_t pat_length, uint8_t *objects, size_t objs_length, size_t *length) {
    cmp_ctx_t in;
    uint8_t *ret = NULL;

    if (!cmp_bytes_init(&in, sets, sets_length)) return NULL;
    size_t nsets;
    FcFontSet **fontsets = decodeFontSets(&in, &nsets);
    cmp_bytes_take(&in, NULL);
    if (fontsets == NULL) return NULL;

    if (!cmp_bytes_init(&in, pat, pat_length)) goto fail_pat;
    FcPattern *pattern = decodePattern(&in);
    cmp_bytes_take(&in, NULL);
    if (pattern == NULL) goto fail_pat;

    if (!cmp_bytes_init(&in, objects, objs_length)) goto fail_objs;
    FcObjectSet *objs = decodeObjectSet(&in);
    cmp_bytes_take(&in, NULL);
    if (objs == NULL) goto fail_objs;

    FcFontSet *res = FcFontSetList(config, fontsets, nsets, pattern, objs);

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) goto fail;
    bool ok = encodeFontSet(&out, res);
    ret = cmp_bytes_take(&out, length);
    if (!ok && ret != NULL) { free(ret); ret = NULL;}

fail:
    FcFontSetDestroy(res);
    FcObjectSetDestroy(objs);
fail_objs:
    FcPatternDestroy(pattern);
fail_pat:
    for (size_t i = 0; i < nsets; i++) FcFontSetDestroy(fontsets[i]);
    free(fontsets);
    return ret;
}


// FcResultMatch, FcResultNoMatch, FcResultTypeMismatch, FcResultNoId, FcResultOutOfMemory

uint8_t *fcFontSetMatch(FcConfig *config, uint8_t *sets, size_t sets_length,
        uint8_t *pat, size_t pat_length, size_t *length) {
    cmp_ctx_t in;
    uint8_t *ret = NULL;

    if (!cmp_bytes_init(&in, sets, sets_length)) return NULL;
    size_t nsets;
    FcFontSet **fontsets = decodeFontSets(&in, &nsets);
    cmp_bytes_take(&in, NULL);
    if (fontsets == NULL) return NULL;

    if (!cmp_bytes_init(&in, pat, pat_length)) goto fail;
    FcPattern *pattern = decodePattern(&in);
    cmp_bytes_take(&in, NULL);
    if (pattern == NULL) goto fail;

    // Necessary preprocessing!
    FcPattern *res = NULL;
    if (!FcConfigSubstitute(config, pattern, FcMatchPattern)) goto fail2;
    FcDefaultSubstitute(pattern);

    FcResult err;
    res = FcFontSetMatch(config, fontsets, nsets, pattern, &err);

    cmp_ctx_t out;
    bool ok;
    if (err == FcResultMatch) {
        if (!cmp_bytes_alloc(&out, 1024)) goto fail2;
        ok = encodePattern(&out, res);
        ret = cmp_bytes_take(&out, length);
        if (!ok && ret != NULL) { free(ret); ret = NULL; }
    } else {
        if (!cmp_bytes_alloc(&out, 32)) goto fail2;
        ok = encodeResult(&out, err);
        ret = cmp_bytes_take(&out, length);
        if (!ok && ret != NULL) { free(ret); ret = NULL; }
    }

fail2:
    if (res != NULL) FcPatternDestroy(res);
    FcPatternDestroy(pattern);
fail:
    for (size_t i = 0; i < nsets; i++) FcFontSetDestroy(fontsets[i]);
    free(fontsets);
    return ret;
}

uint8_t *fcFontSetSort(FcConfig *config, uint8_t *sets, size_t sets_length,
        uint8_t *pat, size_t pat_length, bool trim, size_t *length) {
    cmp_ctx_t in;
    uint8_t *ret = NULL;

    if (!cmp_bytes_init(&in, sets, sets_length)) return NULL;
    size_t nsets;
    FcFontSet **fontsets = decodeFontSets(&in, &nsets);
    cmp_bytes_take(&in, NULL);
    if (fontsets == NULL) return NULL;

    if (!cmp_bytes_init(&in, pat, pat_length)) goto fail;
    FcPattern *pattern = decodePattern(&in);
    cmp_bytes_take(&in, NULL);
    if (pattern == NULL) goto fail;

    // Necessary preprocessing!
    if (!FcConfigSubstitute(config, pattern, FcMatchPattern)) goto fail2;
    FcDefaultSubstitute(pattern);

    FcResult err;
    FcCharSet *charset = NULL;
    FcFontSet *res = FcFontSetSort(config, fontsets, nsets, pattern, trim, &charset, &err);

    cmp_ctx_t out;
    bool ok = true;
    switch (err) {
    case FcResultMatch:
        if (!cmp_bytes_alloc(&out, 1024*res->nfont)) goto fail3;
        ok = ok || cmp_write_array(&out, 2);
        if (res == NULL) cmp_write_nil(&out);
        else {
            // FIXME: Postprocess each font! Rather than call encodeFontSet!
            ok = ok || encodeFontSet(&out, res);
        }
        if (charset == NULL) cmp_write_nil(&out);
        else ok = ok || encodeCharSet(&out, charset);
        break;
    case FcResultNoMatch:
        if (!cmp_bytes_alloc(&out, 1024)) goto fail3;
        ok = ok || cmp_write_array(&out, 2);
        ok = ok || cmp_write_array(&out, 0);
        if (charset == NULL) cmp_write_nil(&out);
        else ok = ok || encodeCharSet(&out, charset);
        break;
    default:
        if (!cmp_bytes_alloc(&out, 32)) goto fail3;
        ok = ok || encodeResult(&out, err);
    }
    ret = cmp_bytes_take(&out, length);
    if (!ok && ret != NULL) { free(ret); ret = NULL; }

fail3:
    FcFontSetDestroy(res);
    if (charset != NULL) FcCharSetDestroy(charset);
fail2:
    FcPatternDestroy(pattern);
fail:
    for (size_t i = 0; i < nsets; i++) FcFontSetDestroy(fontsets[i]);
    free(fontsets);
    return ret;
}

unsigned int fcFreeTypeCharIndex(FT_Face *face, uint32_t ucs4) {
    return FcFreeTypeCharIndex(*face, ucs4);
}

uint8_t *fcFreeTypeCharSet(FT_Face *face, size_t *length) {
    FcCharSet *res = FcFreeTypeCharSet(*face, NULL);

    if (res == NULL) return NULL;
    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcCharSetDestroy(res);
        return NULL;
    }
    if (!encodeCharSet(&out, res)) {
        FcCharSetDestroy(res);
        cmp_bytes_free(&out);
        return NULL;
    }
    FcCharSetDestroy(res);
    return cmp_bytes_take(&out, length);;
}

uint8_t *fcFreeTypeCharSetAndSpacing(FT_Face *face, size_t *length) {
    int spacing;
    FcCharSet *res = FcFreeTypeCharSetAndSpacing(*face, NULL, &spacing);
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcCharSetDestroy(res);
        return NULL;
    }
    if (!cmp_write_array(&out, 2)) goto fail;
    switch (spacing) { // This branches ensures a consistant ABI for Haskell to bind against.
    case FC_MONO:
        if (!cmp_write_integer(&out, 0)) goto fail;
        break;
    case FC_DUAL:
        if (!cmp_write_integer(&out, 1)) goto fail;
        break;
    case FC_PROPORTIONAL:
        if (!cmp_write_integer(&out, 2)) goto fail;
        break;
    default:
        if (!cmp_write_integer(&out, 3)) goto fail;
        break;
    }
    if (!encodeCharSet(&out, res)) goto fail;
    FcCharSetDestroy(res);
    return cmp_bytes_take(&out, length);;

fail:
    FcCharSetDestroy(res);
    cmp_bytes_free(&out);
    return NULL;
}

uint8_t *fcFreeTypeQuery(char *file, int id, size_t *length) {
    int count;
    FcPattern *res = FcFreeTypeQuery(file, id, NULL, &count);
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcPatternDestroy(res);
        return NULL;
    }
    if (!cmp_write_array(&out, 2)) goto fail;
    if (!cmp_write_integer(&out, count)) goto fail;
    if (!encodePattern(&out, res)) goto fail;
    FcPatternDestroy(res);
    return cmp_bytes_take(&out, length);;

fail:
    FcPatternDestroy(res);
    cmp_bytes_free(&out);
    return NULL;
}

uint8_t *fcFreeTypeQueryAll(char *file, size_t *length) {
    int count;
    FcFontSet *fontset = FcFontSetCreate();
    if (fontset == NULL) return NULL;
    unsigned int npatterns = FcFreeTypeQueryAll(file, -1, NULL, &count, fontset);

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcFontSetDestroy(fontset);
        return NULL;
    }
    if (!cmp_write_array(&out, 3)) goto fail;
    if (!cmp_write_integer(&out, npatterns)) goto fail;
    if (!cmp_write_integer(&out, count)) goto fail;
    if (!encodeFontSet(&out, fontset)) goto fail;
    uint8_t *ret = cmp_bytes_take(&out, length);
    FcFontSetDestroy(fontset);
    return ret;

fail:
    FcFontSetDestroy(fontset);
    cmp_bytes_free(&out);
    return NULL;
}

uint8_t *fcFreeTypeQueryFace(FT_Face *face, char *file, int id, size_t *length) {
    FcPattern *res = FcFreeTypeQueryFace(*face, file, id, NULL);
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcPatternDestroy(res);
        return NULL;
    }
    if (!encodePattern(&out, res)) {
        FcPatternDestroy(res);
        cmp_bytes_free(&out);
    }
    uint8_t *ret = cmp_bytes_take(&out, length);
    FcPatternDestroy(res);
    return ret;
}

int fcLangSetCompare(uint8_t *langset, size_t length) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, langset, length)) return -1;
    uint32_t size = 0;
    if (!cmp_read_array(&in, &size) || size != 2) {cmp_bytes_take(&in, NULL); return -1;}
    FcLangSet *a = decodeLangSet(&in);
    if (a == NULL) {cmp_bytes_take(&in, NULL); return -1;}
    FcLangSet *b = decodeLangSet(&in);
    cmp_bytes_take(&in, NULL);
    if (b == NULL) return -1;

    FcLangResult ret = FcLangSetCompare(a, b);
    FcLangSetDestroy(a);
    FcLangSetDestroy(b);

    switch (ret) {
    case FcLangDifferentLang: return 0;
    case FcLangEqual: return 1;
    case FcLangDifferentTerritory: return 2;
    default: return -2;
    }
}

int fcLangSetHasLang(uint8_t *langset, size_t length, const char *lang) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, langset, length)) return -1;
    FcLangSet *ls = decodeLangSet(&in);
    cmp_bytes_take(&in, NULL); // Caller frees `langset`
    if (ls == NULL) return -1;

    FcLangResult ret = FcLangSetHasLang(ls, lang);
    FcLangSetDestroy(ls);
    switch (ret) {
    case FcLangDifferentLang:
        return 0;
    case FcLangEqual:
        return 1;
    case FcLangDifferentTerritory:
        return 2;
    default:
        return -2;
    }
}

uint8_t *fcGetDefaultLangs(size_t *length) {
    FcStrSet *res = FcGetDefaultLangs();
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcStrSetDestroy(res);
        return NULL;
    }
    if (!encodeStrSet(&out, res)) {
        cmp_bytes_free(&out);
        FcStrSetDestroy(res);
        return NULL;
    }
    FcStrSetDestroy(res);
    return cmp_bytes_take(&out, length);
}

uint8_t *fcGetLangs(size_t *length) {
    FcStrSet *res = FcGetLangs();
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcStrSetDestroy(res);
        return NULL;
    }
    if (!encodeStrSet(&out, res)) {
        cmp_bytes_free(&out);
        FcStrSetDestroy(res);
        return NULL;
    }
    FcStrSetDestroy(res);
    return cmp_bytes_take(&out, length);
}

char *fcLangNormalize(char *lang) {return FcLangNormalize(lang);}

uint8_t *fcLangGetCharSet(const char *lang, size_t *length) {
    const FcCharSet *res = FcLangGetCharSet(lang);
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) return NULL;
    if (!encodeCharSet(&out, res)) {
        cmp_bytes_free(&out);
        return NULL;
    }
    return cmp_bytes_take(&out, length);
}

uint8_t *fcConfigGetConfigDirs(FcConfig *conf, size_t *length) {
    FcStrList *res = FcConfigGetConfigDirs(conf);
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcStrListDone(res);
        return NULL;
    }
    if (!encodeStrList(&out, res)) {
        cmp_bytes_free(&out);
        FcStrListDone(res);
        return NULL;
    }
    FcStrListDone(res);
    return cmp_bytes_take(&out, length);
}

uint8_t *fcConfigGetFontDirs(FcConfig *conf, size_t *length) {
    FcStrList *res = FcConfigGetFontDirs(conf);
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcStrListDone(res);
        return NULL;
    }
    if (!encodeStrList(&out, res)) {
        cmp_bytes_free(&out);
        FcStrListDone(res);
        return NULL;
    }
    FcStrListDone(res);
    return cmp_bytes_take(&out, length);
}

uint8_t *fcConfigGetConfigFiles(FcConfig *conf, size_t *length) {
    FcStrList *res = FcConfigGetConfigFiles(conf);
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcStrListDone(res);
        return NULL;
    }
    if (!encodeStrList(&out, res)) {
        cmp_bytes_free(&out);
        FcStrListDone(res);
        return NULL;
    }
    FcStrListDone(res);
    return cmp_bytes_take(&out, length);
}

uint8_t *fcConfigGetCacheDirs(FcConfig *conf, size_t *length) {
    FcStrList *res = FcConfigGetCacheDirs(conf);
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) {
        FcStrListDone(res);
        return NULL;
    }
    if (!encodeStrList(&out, res)) {
        cmp_bytes_free(&out);
        FcStrListDone(res);
        return NULL;
    }
    FcStrListDone(res);
    return cmp_bytes_take(&out, length);
}

uint8_t *fcConfigGetFonts(FcConfig *conf, bool system, size_t *length) {
    // NOTE: We shouldn't free results!
    FcFontSet *res = FcConfigGetFonts(conf, system ? FcSetSystem : FcSetApplication);
    if (res == NULL) return NULL;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) return NULL;
    if (!encodeFontSet(&out, res)) {
        cmp_bytes_free(&out);
        return NULL;
    }
    return cmp_bytes_take(&out, length);
}

uint8_t *fcConfigSubstituteWithPat(FcConfig *conf, uint8_t *data, size_t in_length, bool isFont, size_t *length) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, data, in_length)) return NULL;

    uint32_t size = 0;
    if (!cmp_read_array(&in, &size) || size < 1 || size > 2) return NULL;
    FcPattern *p = decodePattern(&in);
    if (p == NULL) {cmp_bytes_take(&in, NULL); return NULL;}
    FcPattern *p_pat = NULL;
    if (size == 2) {
        p_pat = decodePattern(&in);
        if (p_pat == NULL) {cmp_bytes_take(&in, NULL); goto fail;}
    }
    cmp_bytes_take(&in, NULL);

    if (!FcConfigSubstituteWithPat(conf, p, p_pat, isFont ? FcMatchFont : FcMatchPattern)) goto fail;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) goto fail;
    if (!encodePattern(&out, p)) goto fail;
    FcPatternDestroy(p);
    if (p_pat != NULL) FcPatternDestroy(p_pat);
    return cmp_bytes_take(&out, length);

fail:
    FcPatternDestroy(p);
    if (p_pat != NULL) FcPatternDestroy(p_pat);
    return NULL;
}

uint8_t *fcFontMatch(FcConfig *conf, uint8_t *data, size_t in_length, size_t *length) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, data, in_length)) return NULL;
    FcPattern *p = decodePattern(&in);
    cmp_bytes_take(&in, NULL);
    if (p == NULL) return NULL;

    if (!FcConfigSubstitute(conf, p, FcMatchPattern)) goto fail;
    FcDefaultSubstitute(p);

    FcResult err;
    FcPattern *res = FcFontMatch(conf, p, &err);
    if (res == NULL) goto fail;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) goto fail;
    if (err != FcResultMatch) {
        if (!encodeResult(&out, err)) goto fail2;
    } else {
        if (!encodePattern(&out, res)) goto fail2;
    }
    FcPatternDestroy(p);
    return cmp_bytes_take(&out, length);;

fail2:
    cmp_bytes_free(&out);
fail:
    FcPatternDestroy(p);
    return NULL;
}

uint8_t *fcFontSort(FcConfig *conf, uint8_t *data, size_t in_length, bool trim, size_t *length) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, data, in_length)) return NULL;
    FcPattern *p = decodePattern(&in);
    cmp_bytes_take(&in, NULL);
    if (p == NULL) return NULL;

    if (!FcConfigSubstitute(conf, p, FcMatchPattern)) goto fail;
    FcDefaultSubstitute(p);

    FcResult err;
    FcCharSet *csp;
    FcFontSet *res = FcFontSort(conf, p, trim, &csp, &err);
    if (res == NULL) goto fail2;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) goto fail2;
    if (err != FcResultMatch) {
        if (!encodeResult(&out, err)) goto fail3;
    } else if (csp != NULL) {
        if (!cmp_write_array(&out, 2)) goto fail3;
        if (!encodeFontSet(&out, res)) goto fail3;
        if (!encodeCharSet(&out, csp)) goto fail3;
    } else {
        if (!cmp_write_array(&out, 2)) goto fail3;
        if (!encodeFontSet(&out, res)) goto fail3;
        if (!cmp_write_array(&out, 0)) goto fail3;
    }
    FcPatternDestroy(p);
    if (csp != NULL) FcCharSetDestroy(csp);
    return cmp_bytes_take(&out, length);

fail3:
    cmp_bytes_free(&out);
fail2:
    if (csp != NULL) FcCharSetDestroy(csp);
fail:
    FcPatternDestroy(p);
    return NULL;
}

uint8_t *fcFontRenderPrepare(FcConfig *conf, uint8_t *data, size_t in_length, size_t *length) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, data, in_length)) return NULL;
    uint32_t size = 0;
    if (!cmp_read_array(&in, &size) || size != 2) return NULL;
    FcPattern *pat = decodePattern(&in);
    if (pat == NULL) {cmp_bytes_take(&in, NULL); return NULL;}
    FcPattern *font = decodePattern(&in);
    cmp_bytes_take(&in, NULL);
    if (font == NULL) {FcPatternDestroy(pat); return NULL; }

    FcPattern *res = FcFontRenderPrepare(conf, pat, font);
    if (res == NULL) goto fail0;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) goto fail;
    if (!encodePattern(&out, res)) {cmp_bytes_free(&out); goto fail;}
    FcPatternDestroy(pat);
    FcPatternDestroy(font);
    FcPatternDestroy(res);
    return cmp_bytes_take(&out, length);

fail:
    FcPatternDestroy(res);
fail0:
    FcPatternDestroy(pat);
    FcPatternDestroy(font);
    return NULL;
}

uint8_t *fcFontList(FcConfig *conf, uint8_t *data, size_t in_length, size_t *length) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, data, in_length)) return NULL;
    uint32_t size = 0;
    if (!cmp_read_array(&in, &size) || size != 2) {
        cmp_bytes_take(&in, NULL);
        return NULL;
    }
    FcPattern *pat = decodePattern(&in);
    if (pat == NULL) {cmp_bytes_take(&in, NULL); return NULL;}
    FcObjectSet *os = decodeObjectSet(&in);
    cmp_bytes_take(&in, NULL);
    if (os == NULL) {FcPatternDestroy(pat); return NULL;}

    FcFontSet *res = FcFontList(conf, pat, os);
    if (res == NULL) goto fail0;

    cmp_ctx_t out;
    if (!cmp_bytes_alloc(&out, 1024)) goto fail;
    if (!encodeFontSet(&out, res)) { cmp_bytes_free(&out); goto fail;}
    FcFontSetDestroy(res);
    return cmp_bytes_take(&out, length);

fail:
    FcFontSetDestroy(res);
fail0:
    FcPatternDestroy(pat);
    FcObjectSetDestroy(os);
    return NULL;
}

/*int fcConfigAcceptFont(FcConfig *conf, uint8_t *data, size_t length) {
    cmp_ctx_t in;
    if (!cmp_bytes_init(&in, data, length)) return -1;
    FcPattern *pat = decodePattern(&in);
    if (pat == NULL) return -1;

    FcBool ret = FcConfigAcceptFont(conf, pat);
    FcPatternDestroy(pat);
    return ret ? 1 : 0;
}*/