| 1 |
#include "../VDMX.h" |
| 2 |
|
| 3 |
#include "support/util.h" |
| 4 |
#include "bk/bkgraph.h" |
| 5 |
|
| 6 |
table_VDMX *otfcc_readVDMX(const otfcc_Packet packet, const otfcc_Options *options) { |
| 7 |
table_VDMX *vdmx = NULL; |
| 8 |
FOR_TABLE(OTFCC_CHR('V','D','M','X'), table) { |
| 9 |
if (table.length < 6) goto FAIL; |
| 10 |
uint16_t version = read_16u(table.data + 0); |
| 11 |
uint16_t numRatios = read_16u(table.data + 4); |
| 12 |
if (table.length < 6 + 6 * numRatios) goto FAIL; |
| 13 |
|
| 14 |
vdmx = table_iVDMX.create(); |
| 15 |
vdmx->version = version; |
| 16 |
for (shapeid_t g = 0; g < numRatios; g++) { |
| 17 |
const size_t ratioRangeOffset = 6 + 4 * g; |
| 18 |
const size_t offsetOffset = 6 + 4 * numRatios + 2 * g; |
| 19 |
vdmx_RatioRange r; |
| 20 |
vdmx_iRatioRange.init(&r); |
| 21 |
r.bCharset = read_8u(table.data + ratioRangeOffset + 0); |
| 22 |
r.xRatio = read_8u(table.data + ratioRangeOffset + 1); |
| 23 |
r.yStartRatio = read_8u(table.data + ratioRangeOffset + 2); |
| 24 |
r.yEndRatio = read_8u(table.data + ratioRangeOffset + 3); |
| 25 |
|
| 26 |
uint16_t groupOffset = read_16u(table.data + offsetOffset); |
| 27 |
uint16_t recs = read_16u(table.data + groupOffset + 0); |
| 28 |
for (uint16_t j = 0; j < recs; j++) { |
| 29 |
uint16_t yPelHeight = read_16u(table.data + groupOffset + 4 + j * 6 + 0); |
| 30 |
int16_t yMax = read_16s(table.data + groupOffset + 4 + j * 6 + 2); |
| 31 |
int16_t yMin = read_16s(table.data + groupOffset + 4 + j * 6 + 4); |
| 32 |
vdmx_iGroup.push(&r.records, (vdmx_Record){ |
| 33 |
.yPelHeight = yPelHeight, |
| 34 |
.yMax = yMax, |
| 35 |
.yMin = yMin, |
| 36 |
}); |
| 37 |
} |
| 38 |
vdmx_iRatioRangeList.push(&vdmx->ratios, r); |
| 39 |
} |
| 40 |
return vdmx; |
| 41 |
FAIL: |
| 42 |
logWarning("Table 'VDMX' corrupted.\n"); |
| 43 |
table_iVDMX.free(vdmx); |
| 44 |
vdmx = NULL; |
| 45 |
} |
| 46 |
return vdmx; |
| 47 |
} |
| 48 |
|
| 49 |
void otfcc_dumpVDMX(const table_VDMX *vdmx, json_value *root, const otfcc_Options *options) { |
| 50 |
if (!vdmx) return; |
| 51 |
loggedStep("VDMX") { |
| 52 |
json_value *_vdmx = json_object_new(2); |
| 53 |
json_object_push(_vdmx, "version", json_integer_new(vdmx->version)); |
| 54 |
|
| 55 |
json_value *_ratios = json_array_new(vdmx->ratios.length); |
| 56 |
json_object_push(_vdmx, "ratios", _ratios); |
| 57 |
foreach (vdmx_RatioRange *rr, vdmx->ratios) { |
| 58 |
json_value *_rr = json_object_new(5); |
| 59 |
json_array_push(_ratios, _rr); |
| 60 |
json_object_push(_rr, "bCharset", json_integer_new(rr->bCharset)); |
| 61 |
json_object_push(_rr, "xRatio", json_integer_new(rr->xRatio)); |
| 62 |
json_object_push(_rr, "yStartRatio", json_integer_new(rr->yStartRatio)); |
| 63 |
json_object_push(_rr, "yEndRatio", json_integer_new(rr->yEndRatio)); |
| 64 |
|
| 65 |
json_value *_records = json_array_new(rr->records.length); |
| 66 |
json_object_push(_rr, "records", _records); |
| 67 |
foreach (vdmx_Record *r, rr->records) { |
| 68 |
json_value *_r = json_object_new(3); |
| 69 |
json_array_push(_records, _r); |
| 70 |
json_object_push(_r, "yPelHeight", json_integer_new(r->yPelHeight)); |
| 71 |
json_object_push(_r, "yMax", json_integer_new(r->yMax)); |
| 72 |
json_object_push(_r, "yMin", json_integer_new(r->yMin)); |
| 73 |
} |
| 74 |
} |
| 75 |
json_object_push(root, "VDMX", _vdmx); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
table_VDMX *otfcc_parseVDMX(const json_value *root, const otfcc_Options *options) { |
| 80 |
json_value *_vdmx = NULL; |
| 81 |
if (!(_vdmx = json_obj_get_type(root, "VDMX", json_object))) return NULL; |
| 82 |
table_VDMX *vdmx = table_iVDMX.create(); |
| 83 |
loggedStep("VDMX") { |
| 84 |
vdmx->version = json_obj_getnum(_vdmx, "version"); |
| 85 |
json_value *_ratios = json_obj_get_type(_vdmx, "ratios", json_array); |
| 86 |
for (size_t j = 0; j < _ratios->u.array.length; j++) { |
| 87 |
json_value *_rr = _ratios->u.array.values[j]; |
| 88 |
if (!_rr || _rr->type != json_object) continue; |
| 89 |
vdmx_RatioRange r; |
| 90 |
vdmx_iRatioRange.init(&r); |
| 91 |
r.bCharset = json_obj_getnum(_rr, "bCharset"); |
| 92 |
r.xRatio = json_obj_getnum(_rr, "xRatio"); |
| 93 |
r.yStartRatio = json_obj_getnum(_rr, "yStartRatio"); |
| 94 |
r.yEndRatio = json_obj_getnum(_rr, "yEndRatio"); |
| 95 |
json_value *_records = json_obj_get_type(_rr, "records", json_array); |
| 96 |
if (!_records) { |
| 97 |
vdmx_iRatioRange.dispose(&r); |
| 98 |
continue; |
| 99 |
} |
| 100 |
for (size_t j = 0; j < _records->u.array.length; j++) { |
| 101 |
json_value *_r = _records->u.array.values[j]; |
| 102 |
if (!_r || _r->type != json_object) continue; |
| 103 |
vdmx_iGroup.push(&r.records, (vdmx_Record){ |
| 104 |
.yPelHeight = json_obj_getnum(_r, "yPelHeight"), |
| 105 |
.yMax = json_obj_getnum(_r, "yMax"), |
| 106 |
.yMin = json_obj_getnum(_r, "yMin"), |
| 107 |
}); |
| 108 |
} |
| 109 |
vdmx_iRatioRangeList.push(&vdmx->ratios, r); |
| 110 |
} |
| 111 |
} |
| 112 |
return vdmx; |
| 113 |
} |
| 114 |
|
| 115 |
caryll_Buffer *otfcc_buildVDMX(const table_VDMX *vdmx, const otfcc_Options *options) { |
| 116 |
if (!vdmx || !vdmx->ratios.length) return NULL; |
| 117 |
bk_Block *root = bk_new_Block(b16, vdmx->version, // Version |
| 118 |
b16, vdmx->ratios.length, // numRecs |
| 119 |
b16, vdmx->ratios.length, // numRatios |
| 120 |
bkover); |
| 121 |
foreach (vdmx_RatioRange *rr, vdmx->ratios) { |
| 122 |
bk_push(root, // RatioRange |
| 123 |
b8, rr->bCharset, // bCharset |
| 124 |
b8, rr->xRatio, // bCharset |
| 125 |
b8, rr->yStartRatio, // bCharset |
| 126 |
b8, rr->yEndRatio, // bCharset |
| 127 |
bkover); |
| 128 |
} |
| 129 |
foreach (vdmx_RatioRange *rr, vdmx->ratios) { |
| 130 |
uint16_t startsz = 0xFFFF; |
| 131 |
uint16_t endsz = 0; |
| 132 |
foreach (vdmx_Record *r, rr->records) { |
| 133 |
if (startsz > r->yPelHeight) startsz = r->yPelHeight; |
| 134 |
if (endsz < r->yPelHeight) endsz = r->yPelHeight; |
| 135 |
} |
| 136 |
bk_Block *group = bk_new_Block(b16, rr->records.length, // recs |
| 137 |
b8, startsz, // |
| 138 |
b8, endsz, // |
| 139 |
bkover); |
| 140 |
foreach (vdmx_Record *r, rr->records) { |
| 141 |
bk_push(group, // |
| 142 |
b16, r->yPelHeight, // |
| 143 |
b16, r->yMax, // |
| 144 |
b16, r->yMin, // |
| 145 |
bkover); |
| 146 |
} |
| 147 |
bk_push(root, p16, group, bkover); |
| 148 |
} |
| 149 |
return bk_build_Block_noMinimize(root); |
| 150 |
} |