| 1 |
#include "BASE.h" |
| 2 |
|
| 3 |
#include "otl/private.h" |
| 4 |
|
| 5 |
|
| 6 |
static void deleteBaseAxis(MOVE otl_BaseAxis *axis) { |
| 7 |
if (!axis) return; |
| 8 |
if (axis->entries) { |
| 9 |
for (tableid_t j = 0; j < axis->scriptCount; j++) { |
| 10 |
if (axis->entries[j].baseValues) FREE(axis->entries[j].baseValues); |
| 11 |
} |
| 12 |
FREE(axis->entries); |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
static INLINE void disposeBASE(MOVE table_BASE *base) { |
| 17 |
deleteBaseAxis(base->horizontal); |
| 18 |
deleteBaseAxis(base->vertical); |
| 19 |
} |
| 20 |
|
| 21 |
caryll_standardRefType(table_BASE, table_iBASE, disposeBASE); |
| 22 |
|
| 23 |
static int16_t readBaseValue(font_file_pointer data, uint32_t tableLength, uint16_t offset) { |
| 24 |
checkLength(offset + 4); |
| 25 |
return read_16s(data + offset + 2); |
| 26 |
FAIL: |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
static void readBaseScript(const font_file_pointer data, uint32_t tableLength, uint16_t offset, |
| 31 |
otl_BaseScriptEntry *entry, uint32_t *baseTagList, uint16_t nBaseTags) { |
| 32 |
entry->baseValuesCount = 0; |
| 33 |
entry->baseValues = NULL; |
| 34 |
entry->defaultBaselineTag = 0; |
| 35 |
checkLength(offset + 2); // care about base values only now |
| 36 |
uint16_t baseValuesOffset = read_16u(data + offset); |
| 37 |
if (baseValuesOffset) { |
| 38 |
baseValuesOffset += offset; |
| 39 |
checkLength(baseValuesOffset + 4); |
| 40 |
uint16_t defaultIndex = read_16u(data + baseValuesOffset) % nBaseTags; |
| 41 |
entry->defaultBaselineTag = baseTagList[defaultIndex]; |
| 42 |
entry->baseValuesCount = read_16u(data + baseValuesOffset + 2); |
| 43 |
if (entry->baseValuesCount != nBaseTags) goto FAIL; |
| 44 |
checkLength(baseValuesOffset + 4 + 2 * entry->baseValuesCount); |
| 45 |
NEW(entry->baseValues, entry->baseValuesCount); |
| 46 |
for (tableid_t j = 0; j < entry->baseValuesCount; j++) { |
| 47 |
entry->baseValues[j].tag = baseTagList[j]; |
| 48 |
uint16_t _valOffset = read_16u(data + baseValuesOffset + 4 + 2 * j); |
| 49 |
if (_valOffset) { |
| 50 |
entry->baseValues[j].coordinate = |
| 51 |
readBaseValue(data, tableLength, baseValuesOffset + _valOffset); |
| 52 |
} else { |
| 53 |
entry->baseValues[j].coordinate = 0; |
| 54 |
} |
| 55 |
} |
| 56 |
return; |
| 57 |
} |
| 58 |
FAIL: |
| 59 |
entry->baseValuesCount = 0; |
| 60 |
if (entry->baseValues) FREE(entry->baseValues); |
| 61 |
entry->baseValues = NULL; |
| 62 |
entry->defaultBaselineTag = 0; |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
static otl_BaseAxis *readAxis(font_file_pointer data, uint32_t tableLength, uint16_t offset) { |
| 67 |
otl_BaseAxis *axis = NULL; |
| 68 |
uint32_t *baseTagList = NULL; |
| 69 |
checkLength(offset + 4); |
| 70 |
|
| 71 |
// Read BaseTagList |
| 72 |
uint16_t baseTagListOffset = offset + read_16u(data + offset); |
| 73 |
if (baseTagListOffset <= offset) goto FAIL; |
| 74 |
checkLength(baseTagListOffset + 2); |
| 75 |
uint16_t nBaseTags = read_16u(data + baseTagListOffset); |
| 76 |
if (!nBaseTags) goto FAIL; |
| 77 |
checkLength(baseTagListOffset + 2 + 4 * nBaseTags); |
| 78 |
NEW(baseTagList, nBaseTags); |
| 79 |
for (uint16_t j = 0; j < nBaseTags; j++) { |
| 80 |
baseTagList[j] = read_32u(data + baseTagListOffset + 2 + j * 4); |
| 81 |
} |
| 82 |
|
| 83 |
uint16_t baseScriptListOffset = offset + read_16u(data + offset + 2); |
| 84 |
if (baseScriptListOffset <= offset) goto FAIL; |
| 85 |
checkLength(baseScriptListOffset + 2); |
| 86 |
tableid_t nBaseScripts = read_16u(data + baseScriptListOffset); |
| 87 |
checkLength(baseScriptListOffset + 2 + 6 * nBaseScripts); |
| 88 |
NEW(axis); |
| 89 |
axis->scriptCount = nBaseScripts; |
| 90 |
NEW(axis->entries, nBaseScripts); |
| 91 |
for (tableid_t j = 0; j < nBaseScripts; j++) { |
| 92 |
axis->entries[j].tag = read_32u(data + baseScriptListOffset + 2 + 6 * j); |
| 93 |
uint16_t baseScriptOffset = read_16u(data + baseScriptListOffset + 2 + 6 * j + 4); |
| 94 |
if (baseScriptOffset) { |
| 95 |
readBaseScript(data, tableLength, baseScriptListOffset + baseScriptOffset, |
| 96 |
&(axis->entries[j]), baseTagList, nBaseTags); |
| 97 |
} else { |
| 98 |
axis->entries[j].baseValuesCount = 0; |
| 99 |
axis->entries[j].baseValues = NULL; |
| 100 |
axis->entries[j].defaultBaselineTag = 0; |
| 101 |
} |
| 102 |
} |
| 103 |
return axis; |
| 104 |
|
| 105 |
FAIL: |
| 106 |
if (baseTagList) FREE(baseTagList); |
| 107 |
DELETE(deleteBaseAxis, axis); |
| 108 |
return axis; |
| 109 |
} |
| 110 |
|
| 111 |
table_BASE *otfcc_readBASE(const otfcc_Packet packet, const otfcc_Options *options) { |
| 112 |
table_BASE *base = NULL; |
| 113 |
FOR_TABLE(OTFCC_CHR('B','A','S','E'), table) { |
| 114 |
font_file_pointer data = table.data; |
| 115 |
uint32_t tableLength = table.length; |
| 116 |
checkLength(8); |
| 117 |
NEW(base); |
| 118 |
uint16_t offsetH = read_16u(data + 4); |
| 119 |
if (offsetH) base->horizontal = readAxis(data, tableLength, offsetH); |
| 120 |
uint16_t offsetV = read_16u(data + 6); |
| 121 |
if (offsetV) base->vertical = readAxis(data, tableLength, offsetV); |
| 122 |
return base; |
| 123 |
FAIL: |
| 124 |
logWarning("Table 'BASE' Corrupted"); |
| 125 |
DELETE(table_iBASE.free, base); |
| 126 |
} |
| 127 |
return base; |
| 128 |
} |
| 129 |
|
| 130 |
static json_value *axisToJson(const otl_BaseAxis *axis) { |
| 131 |
json_value *_axis = json_object_new(axis->scriptCount); |
| 132 |
for (tableid_t j = 0; j < axis->scriptCount; j++) { |
| 133 |
if (!axis->entries[j].tag) continue; |
| 134 |
json_value *_entry = json_object_new(3); |
| 135 |
if (axis->entries[j].defaultBaselineTag) { |
| 136 |
char tag[4]; |
| 137 |
tag2str(axis->entries[j].defaultBaselineTag, tag); |
| 138 |
json_object_push(_entry, "defaultBaseline", json_string_new_length(4, tag)); |
| 139 |
} |
| 140 |
json_value *_values = json_object_new(axis->entries[j].baseValuesCount); |
| 141 |
for (tableid_t k = 0; k < axis->entries[j].baseValuesCount; k++) { |
| 142 |
if (axis->entries[j].baseValues[k].tag) { |
| 143 |
|
| 144 |
json_object_push_tag(_values, axis->entries[j].baseValues[k].tag, |
| 145 |
json_new_position(axis->entries[j].baseValues[k].coordinate)); |
| 146 |
} |
| 147 |
} |
| 148 |
json_object_push(_entry, "baselines", _values); |
| 149 |
json_object_push_tag(_axis, axis->entries[j].tag, _entry); |
| 150 |
} |
| 151 |
return _axis; |
| 152 |
} |
| 153 |
|
| 154 |
void otfcc_dumpBASE(const table_BASE *base, json_value *root, const otfcc_Options *options) { |
| 155 |
if (!base) return; |
| 156 |
loggedStep("BASE") { |
| 157 |
json_value *_base = json_object_new(2); |
| 158 |
if (base->horizontal) json_object_push(_base, "horizontal", axisToJson(base->horizontal)); |
| 159 |
if (base->vertical) json_object_push(_base, "vertical", axisToJson(base->vertical)); |
| 160 |
json_object_push(root, "BASE", _base); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
static void baseScriptFromJson(const json_value *_sr, otl_BaseScriptEntry *entry) { |
| 165 |
entry->defaultBaselineTag = str2tag(json_obj_getstr_share(_sr, "defaultBaseline")); |
| 166 |
json_value *_basevalues = json_obj_get_type(_sr, "baselines", json_object); |
| 167 |
if (!_basevalues) { |
| 168 |
entry->baseValuesCount = 0; |
| 169 |
entry->baseValues = NULL; |
| 170 |
} else { |
| 171 |
entry->baseValuesCount = _basevalues->u.object.length; |
| 172 |
NEW(entry->baseValues, entry->baseValuesCount); |
| 173 |
for (tableid_t j = 0; j < entry->baseValuesCount; j++) { |
| 174 |
entry->baseValues[j].tag = str2tag(_basevalues->u.object.values[j].name); |
| 175 |
entry->baseValues[j].coordinate = json_numof(_basevalues->u.object.values[j].value); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
static int by_script_tag(const void *a, const void *b) { |
| 181 |
return ((const otl_BaseScriptEntry *)a)->tag - ((const otl_BaseScriptEntry *)b)->tag; |
| 182 |
} |
| 183 |
|
| 184 |
static otl_BaseAxis *axisFromJson(const json_value *_axis) { |
| 185 |
if (!_axis) return NULL; |
| 186 |
otl_BaseAxis *axis; |
| 187 |
NEW(axis); |
| 188 |
axis->scriptCount = _axis->u.object.length; |
| 189 |
NEW(axis->entries, axis->scriptCount); |
| 190 |
tableid_t jj = 0; |
| 191 |
for (tableid_t j = 0; j < axis->scriptCount; j++) { |
| 192 |
if (_axis->u.object.values[j].value && |
| 193 |
_axis->u.object.values[j].value->type == json_object) { |
| 194 |
axis->entries[jj].tag = str2tag(_axis->u.object.values[j].name); |
| 195 |
baseScriptFromJson(_axis->u.object.values[j].value, &(axis->entries[jj])); |
| 196 |
jj++; |
| 197 |
} |
| 198 |
} |
| 199 |
axis->scriptCount = jj; |
| 200 |
qsort(axis->entries, axis->scriptCount, sizeof(otl_BaseScriptEntry), by_script_tag); |
| 201 |
return axis; |
| 202 |
} |
| 203 |
|
| 204 |
table_BASE *otfcc_parseBASE(const json_value *root, const otfcc_Options *options) { |
| 205 |
table_BASE *base = NULL; |
| 206 |
json_value *table = NULL; |
| 207 |
if ((table = json_obj_get_type(root, "BASE", json_object))) { |
| 208 |
loggedStep("BASE") { |
| 209 |
NEW(base); |
| 210 |
base->horizontal = axisFromJson(json_obj_get_type(table, "horizontal", json_object)); |
| 211 |
base->vertical = axisFromJson(json_obj_get_type(table, "vertical", json_object)); |
| 212 |
} |
| 213 |
} |
| 214 |
return base; |
| 215 |
} |
| 216 |
|
| 217 |
static int by_tag(const void *a, const void *b) { |
| 218 |
return *((const uint32_t *)a) - *((const uint32_t *)b); |
| 219 |
} |
| 220 |
|
| 221 |
static bk_Block *axisToBk(const otl_BaseAxis *axis) { |
| 222 |
if (!axis) return NULL; |
| 223 |
struct { |
| 224 |
tableid_t size; |
| 225 |
uint32_t *items; |
| 226 |
} taglist; |
| 227 |
taglist.size = 0; |
| 228 |
taglist.items = NULL; |
| 229 |
|
| 230 |
for (tableid_t j = 0; j < axis->scriptCount; j++) { |
| 231 |
otl_BaseScriptEntry *entry = &(axis->entries[j]); |
| 232 |
if (entry->defaultBaselineTag) { |
| 233 |
bool found = false; |
| 234 |
for (tableid_t jk = 0; jk < taglist.size; jk++) { |
| 235 |
if (taglist.items[jk] == entry->defaultBaselineTag) { |
| 236 |
found = true; |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
if (!found) { |
| 241 |
taglist.size += 1; |
| 242 |
RESIZE(taglist.items, taglist.size); |
| 243 |
taglist.items[taglist.size - 1] = entry->defaultBaselineTag; |
| 244 |
} |
| 245 |
} |
| 246 |
for (tableid_t k = 0; k < entry->baseValuesCount; k++) { |
| 247 |
uint32_t tag = entry->baseValues[k].tag; |
| 248 |
bool found = false; |
| 249 |
for (tableid_t jk = 0; jk < taglist.size; jk++) { |
| 250 |
if (taglist.items[jk] == tag) { |
| 251 |
found = true; |
| 252 |
break; |
| 253 |
} |
| 254 |
} |
| 255 |
if (!found) { |
| 256 |
taglist.size += 1; |
| 257 |
RESIZE(taglist.items, taglist.size); |
| 258 |
taglist.items[taglist.size - 1] = tag; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
qsort(taglist.items, taglist.size, sizeof(uint32_t), by_tag); |
| 263 |
bk_Block *baseTagList = bk_new_Block(b16, taglist.size, bkover); |
| 264 |
for (tableid_t j = 0; j < taglist.size; j++) { |
| 265 |
bk_push(baseTagList, b32, taglist.items[j], bkover); |
| 266 |
} |
| 267 |
|
| 268 |
bk_Block *baseScriptList = bk_new_Block(b16, axis->scriptCount, bkover); |
| 269 |
for (tableid_t j = 0; j < axis->scriptCount; j++) { |
| 270 |
otl_BaseScriptEntry *entry = &(axis->entries[j]); |
| 271 |
bk_Block *baseValues = bk_new_Block(bkover); |
| 272 |
{ |
| 273 |
tableid_t defaultIndex = 0; |
| 274 |
for (tableid_t m = 0; m < taglist.size; m++) { |
| 275 |
if (taglist.items[m] == entry->defaultBaselineTag) { |
| 276 |
defaultIndex = m; |
| 277 |
break; |
| 278 |
} |
| 279 |
} |
| 280 |
bk_push(baseValues, b16, defaultIndex, bkover); |
| 281 |
} |
| 282 |
bk_push(baseValues, b16, taglist.size, bkover); |
| 283 |
for (size_t m = 0; m < taglist.size; m++) { |
| 284 |
bool found = false; |
| 285 |
tableid_t foundIndex = 0; |
| 286 |
for (tableid_t k = 0; k < entry->baseValuesCount; k++) { |
| 287 |
if (entry->baseValues[k].tag == taglist.items[m]) { |
| 288 |
found = true, foundIndex = k; |
| 289 |
break; |
| 290 |
} |
| 291 |
} |
| 292 |
if (found) { |
| 293 |
bk_push(baseValues, // base value |
| 294 |
p16, |
| 295 |
bk_new_Block( |
| 296 |
b16, 1, // format |
| 297 |
b16, (int16_t)entry->baseValues[foundIndex].coordinate, // coordinate |
| 298 |
bkover), |
| 299 |
bkover); |
| 300 |
} else { |
| 301 |
bk_push(baseValues, // assign a zero value |
| 302 |
p16, |
| 303 |
bk_new_Block(b16, 1, // format |
| 304 |
b16, 0, // coordinate |
| 305 |
bkover), |
| 306 |
bkover); |
| 307 |
} |
| 308 |
} |
| 309 |
bk_Block *scriptRecord = bk_new_Block(p16, baseValues, // BaseValues |
| 310 |
p16, NULL, // DefaultMinMax |
| 311 |
b16, 0, // BaseLangSysCount |
| 312 |
bkover); |
| 313 |
bk_push(baseScriptList, b32, entry->tag, // BaseScriptTag |
| 314 |
p16, scriptRecord, // BaseScript |
| 315 |
bkover); |
| 316 |
} |
| 317 |
FREE(taglist.items); |
| 318 |
return bk_new_Block(p16, baseTagList, // BaseTagList |
| 319 |
p16, baseScriptList, // BaseScriptList |
| 320 |
bkover); |
| 321 |
} |
| 322 |
|
| 323 |
caryll_Buffer *otfcc_buildBASE(const table_BASE *base, const otfcc_Options *options) { |
| 324 |
if (!base) return NULL; |
| 325 |
bk_Block *root = bk_new_Block(b32, 0x10000, // Version |
| 326 |
p16, axisToBk(base->horizontal), // HorizAxis |
| 327 |
p16, axisToBk(base->vertical), // VertAxis |
| 328 |
bkover); |
| 329 |
return bk_build_Block(root); |
| 330 |
} |