Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/table/hmtx.c

raw
1 #include "hmtx.h"
2
3 #include "support/util.h"
4
5 static INLINE void disposeHmtx(MOVE table_hmtx *table) {
6 if (table->metrics != NULL) FREE(table->metrics);
7 if (table->leftSideBearing != NULL) FREE(table->leftSideBearing);
8 }
9
10 caryll_standardRefType(table_hmtx, table_iHmtx, disposeHmtx);
11
12 table_hmtx *otfcc_readHmtx(const otfcc_Packet packet, const otfcc_Options *options,
13 table_hhea *hhea, table_maxp *maxp) {
14 if (!hhea || !maxp || !hhea->numberOfMetrics || maxp->numGlyphs < hhea->numberOfMetrics) {
15 return NULL;
16 }
17 FOR_TABLE(OTFCC_CHR('h','m','t','x'), table) {
18 font_file_pointer data = table.data;
19 uint32_t length = table.length;
20
21 table_hmtx *hmtx = NULL;
22
23 glyphid_t count_a = hhea->numberOfMetrics;
24 glyphid_t count_k = maxp->numGlyphs - hhea->numberOfMetrics;
25 if (length < count_a * 4 + count_k * 2) goto HMTX_CORRUPTED;
26
27 NEW(hmtx);
28 NEW(hmtx->metrics, count_a);
29 NEW(hmtx->leftSideBearing, count_k);
30
31 for (glyphid_t ia = 0; ia < count_a; ia++) {
32 hmtx->metrics[ia].advanceWidth = read_16u(data + ia * 4);
33 hmtx->metrics[ia].lsb = read_16s(data + ia * 4 + 2);
34 }
35
36 for (glyphid_t ik = 0; ik < count_k; ik++) {
37 hmtx->leftSideBearing[ik] = read_16s(data + count_a * 4 + ik * 2);
38 }
39
40 return hmtx;
41 HMTX_CORRUPTED:
42 logWarning("Table 'hmtx' corrupted.\n");
43 if (hmtx) { table_iHmtx.free(hmtx), hmtx = NULL; }
44 }
45 return NULL;
46 }
47
48 caryll_Buffer *otfcc_buildHmtx(const table_hmtx *hmtx, glyphid_t count_a, glyphid_t count_k,
49 const otfcc_Options *options) {
50 caryll_Buffer *buf = bufnew();
51 if (!hmtx) return buf;
52 if (hmtx->metrics) {
53 for (glyphid_t j = 0; j < count_a; j++) {
54 bufwrite16b(buf, hmtx->metrics[j].advanceWidth);
55 bufwrite16b(buf, hmtx->metrics[j].lsb);
56 }
57 }
58 if (hmtx->leftSideBearing) {
59 for (glyphid_t j = 0; j < count_k; j++) {
60 bufwrite16b(buf, hmtx->leftSideBearing[j]);
61 }
62 }
63 return buf;
64 }