summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/mfluadir/otfcc/lib/libcff/cff-index.c
blob: 7a42b16ad9407127ed2801fb86df8eb121baa204 (plain)
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
#include "cff-index.h"
// INDEX util functions

static INLINE void disposeCffIndex(cff_Index *in) {
	if (in->offset) FREE(in->offset);
	if (in->data) FREE(in->data);
}

caryll_standardRefTypeFn(cff_Index, disposeCffIndex);

static uint32_t getIndexLength(const cff_Index *i) {
	if (i->count != 0)
		return 3 + (i->offset[i->count] - 1) + ((i->count + 1) * i->offSize);
	else
		return 3;
}
static void emptyIndex(cff_Index *i) {
	cff_iIndex.dispose(i);
	memset(i, 0, sizeof(*i));
}

static void extractIndex(uint8_t *data, uint32_t pos, cff_Index *in) {
	in->count = gu2(data, pos);
	in->offSize = gu1(data, pos + 2);

	if (in->count > 0) {
		NEW(in->offset, in->count + 1);

		for (arity_t i = 0; i <= in->count; i++) {
			switch (in->offSize) {
				case 1:
					in->offset[i] = gu1(data, pos + 3 + (i * in->offSize));
					break;
				case 2:
					in->offset[i] = gu2(data, pos + 3 + (i * in->offSize));
					break;
				case 3:
					in->offset[i] = gu3(data, pos + 3 + (i * in->offSize));
					break;
				case 4:
					in->offset[i] = gu4(data, pos + 3 + (i * in->offSize));
					break;
			}
		}

		NEW(in->data, in->offset[in->count] - 1);
		memcpy(in->data, data + pos + 3 + (in->count + 1) * in->offSize, in->offset[in->count] - 1);
	} else {
		in->offset = NULL;
		in->data = NULL;
	}
}

static cff_Index *newIndexByCallback(void *context, uint32_t length, caryll_Buffer *(*fn)(void *, uint32_t)) {
	cff_Index *idx = cff_iIndex.create();
	idx->count = length;
	NEW(idx->offset, idx->count + 1);
	idx->offset[0] = 1;
	idx->data = NULL;

	size_t used = 0;
	size_t blank = 0;
	for (arity_t i = 0; i < length; i++) {
		caryll_Buffer *blob = fn(context, i);
		if (blank < blob->size) {
			used += blob->size;
			blank = (used >> 1) & 0xFFFFFF;
			RESIZE(idx->data, used + blank);
		} else {
			used += blob->size;
			blank -= blob->size;
		}
		idx->offset[i + 1] = (uint32_t)(blob->size + idx->offset[i]);
		memcpy(idx->data + idx->offset[i] - 1, blob->data, blob->size);
		buffree(blob);
	}
	idx->offSize = 4;
	return idx;
}

static caryll_Buffer *buildIndex(const cff_Index *index) {
	caryll_Buffer *blob = bufnew();
	if (!index->count) {
		bufwrite8(blob, 0);
		bufwrite8(blob, 0);
		bufwrite8(blob, 0);
		return blob;
	}

	uint32_t lastOffset = index->offset[index->count];
	uint8_t offSize = 4;
	if (lastOffset < 0x100) {
		offSize = 1;
	} else if (lastOffset < 0x10000) {
		offSize = 2;
	} else if (lastOffset < 0x1000000) {
		offSize = 3;
	} else {
		offSize = 4;
	}

	if (index->count != 0)
		blob->size = 3 + (index->offset[index->count] - 1) + ((index->count + 1) * offSize);
	else
		blob->size = 3;

	NEW(blob->data, blob->size);
	blob->data[0] = index->count / 256;
	blob->data[1] = index->count % 256;
	blob->data[2] = offSize;

	if (index->count > 0) {
		for (arity_t i = 0; i <= index->count; i++) {
			switch (offSize) {
				case 1:
					blob->data[3 + i] = index->offset[i];
					break;
				case 2:
					blob->data[3 + i * 2] = index->offset[i] / 256;
					blob->data[4 + i * 2] = index->offset[i] % 256;
					break;
				case 3:
					blob->data[3 + i * 3] = index->offset[i] / 65536;
					blob->data[4 + i * 3] = (index->offset[i] % 65536) / 256;
					blob->data[5 + i * 3] = (index->offset[i] % 65536) % 256;
					break;
				case 4:
					blob->data[3 + i * 4] = (index->offset[i] / 65536) / 256;
					blob->data[4 + i * 4] = (index->offset[i] / 65536) % 256;
					blob->data[5 + i * 4] = (index->offset[i] % 65536) / 256;
					blob->data[6 + i * 4] = (index->offset[i] % 65536) % 256;
					break;
			}
		}

		if (index->data != NULL)
			memcpy(blob->data + 3 + ((index->count + 1) * offSize), index->data, index->offset[index->count] - 1);
	}
	blob->cursor = blob->size;
	return blob;
}

caryll_ElementInterfaceOf(cff_Index) cff_iIndex = {
    caryll_standardRefTypeMethods(cff_Index), .getLength = getIndexLength, .empty = emptyIndex, .parse = extractIndex,
    .fromCallback = newIndexByCallback,       .build = buildIndex,
};