Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/otf-writer/otf-writer.c

raw
1 #include "support/util.h"
2 #include "otfcc/font.h"
3 #include "table/all.h"
4 #include "otfcc/sfnt-builder.h"
5 #include "stat.h"
6
7 #define OTFCC_CHR(a,b,c,d) ( ((a)<<24) | ((b)<<16) | ((c)<<8) | (d) )
8
9 static void *serializeToOTF(otfcc_Font *font, const otfcc_Options *options) {
10 // do stat before serialize
11 otfcc_statFont(font, options);
12
13 otfcc_SFNTBuilder *builder =
14 otfcc_newSFNTBuilder(font->subtype == FONTTYPE_CFF ? OTFCC_CHR('O','T','T','O') : 0x00010000, options);
15 // Outline data
16 if (font->subtype == FONTTYPE_TTF) {
17 table_GlyfAndLocaBuffers pair = otfcc_buildGlyf(font->glyf, font->head, options);
18 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('g','l','y','f'), pair.glyf);
19 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('l','o','c','a'), pair.loca);
20 } else {
21 table_CFFAndGlyf r = {font->CFF_, font->glyf};
22 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('C','F','F',' '), otfcc_buildCFF(r, options));
23 }
24
25 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('h','e','a','d'), otfcc_buildHead(font->head, options));
26 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('h','h','e','a'), otfcc_buildHhea(font->hhea, options));
27 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('O','S','/','2'), otfcc_buildOS_2(font->OS_2, options));
28 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('m','a','x','p'), otfcc_buildMaxp(font->maxp, options));
29 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('n','a','m','e'), otfcc_buildName(font->name, options));
30 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('m','e','t','a'), otfcc_buildMeta(font->meta, options));
31 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('p','o','s','t'),
32 otfcc_buildPost(font->post, font->glyph_order, options));
33 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('c','m','a','p'), otfcc_buildCmap(font->cmap, options));
34 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('g','a','s','p'), otfcc_buildGasp(font->gasp, options));
35
36 if (font->subtype == FONTTYPE_TTF) {
37 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('f','p','g','m'), otfcc_buildFpgmPrep(font->fpgm, options));
38 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('p','r','e','p'), otfcc_buildFpgmPrep(font->prep, options));
39 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('c','v','t',' '), otfcc_buildCvt(font->cvt_, options));
40 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('L','T','S','H'), otfcc_buildLTSH(font->LTSH, options));
41 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('V','D','M','X'), otfcc_buildVDMX(font->VDMX, options));
42 }
43
44 if (font->hhea && font->maxp && font->hmtx) {
45 uint16_t hmtx_counta = font->hhea->numberOfMetrics;
46 uint16_t hmtx_countk = font->maxp->numGlyphs - font->hhea->numberOfMetrics;
47 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('h','m','t','x'),
48 otfcc_buildHmtx(font->hmtx, hmtx_counta, hmtx_countk, options));
49 }
50
51 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('v','h','e','a'), otfcc_buildVhea(font->vhea, options));
52 if (font->vhea && font->maxp && font->vmtx) {
53 uint16_t vmtx_counta = font->vhea->numOfLongVerMetrics;
54 uint16_t vmtx_countk = font->maxp->numGlyphs - font->vhea->numOfLongVerMetrics;
55 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('v','m','t','x'),
56 otfcc_buildVmtx(font->vmtx, vmtx_counta, vmtx_countk, options));
57 }
58 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('V','O','R','G'), otfcc_buildVORG(font->VORG, options));
59
60 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('G','S','U','B'), otfcc_buildOtl(font->GSUB, options, "GSUB"));
61 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('G','P','O','S'), otfcc_buildOtl(font->GPOS, options, "GPOS"));
62 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('G','D','E','F'), otfcc_buildGDEF(font->GDEF, options));
63 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('B','A','S','E'), otfcc_buildBASE(font->BASE, options));
64
65 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('C','P','A','L'), otfcc_buildCPAL(font->CPAL, options));
66 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('C','O','L','R'), otfcc_buildCOLR(font->COLR, options));
67 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('S','V','G',' '), otfcc_buildSVG(font->SVG_, options));
68
69 {
70 tsi_BuildTarget target = otfcc_buildTSI(font->TSI_01, options);
71 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('T','S','I','0'), target.indexPart);
72 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('T','S','I','1'), target.textPart);
73 }
74 {
75 tsi_BuildTarget target = otfcc_buildTSI(font->TSI_23, options);
76 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('T','S','I','2'), target.indexPart);
77 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('T','S','I','3'), target.textPart);
78 }
79 if (font->glyf) {
80 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('T','S','I','5'),
81 otfcc_buildTSI5(font->TSI5, options, font->glyf->length));
82 }
83
84 if (options->dummy_DSIG) {
85 caryll_Buffer *dsig = bufnew();
86 bufwrite32b(dsig, 0x00000001);
87 bufwrite16b(dsig, 0);
88 bufwrite16b(dsig, 0);
89 otfcc_SFNTBuilder_pushTable(builder, OTFCC_CHR('D','S','I','G'), dsig);
90 }
91
92 caryll_Buffer *otf = otfcc_SFNTBuilder_serialize(builder);
93 otfcc_deleteSFNTBuilder(builder);
94 otfcc_unstatFont(font, options);
95 return otf;
96 }
97 static void freeFontWriter(otfcc_IFontSerializer *self) {
98 free(self);
99 }
100 otfcc_IFontSerializer *otfcc_newOTFWriter() {
101 otfcc_IFontSerializer *writer;
102 NEW(writer);
103 writer->serialize = serializeToOTF;
104 writer->free = freeFontWriter;
105 return writer;
106 }