| 1 |
#include "gasp.h" |
| 2 |
|
| 3 |
#include "support/util.h" |
| 4 |
|
| 5 |
|
| 6 |
#define GASP_DOGRAY 0x0002 |
| 7 |
#define GASP_GRIDFIT 0x0001 |
| 8 |
#define GASP_SYMMETRIC_GRIDFIT 0x0004 |
| 9 |
#define GASP_SYMMETRIC_SMOOTHING 0x0008 |
| 10 |
|
| 11 |
caryll_standardType(gasp_Record, gasp_iRecord); |
| 12 |
caryll_standardVectorImpl(gasp_RecordList, gasp_Record, gasp_iRecord, gasp_iRecordList); |
| 13 |
|
| 14 |
static INLINE void initGasp(table_gasp *gasp) { |
| 15 |
gasp->version = 1; |
| 16 |
gasp_iRecordList.init(&gasp->records); |
| 17 |
} |
| 18 |
static INLINE void disposeGasp(MOVE table_gasp *gasp) { |
| 19 |
gasp_iRecordList.dispose(&gasp->records); |
| 20 |
} |
| 21 |
|
| 22 |
caryll_standardRefType(table_gasp, table_iGasp, initGasp, disposeGasp); |
| 23 |
|
| 24 |
table_gasp *otfcc_readGasp(const otfcc_Packet packet, const otfcc_Options *options) { |
| 25 |
table_gasp *gasp = NULL; |
| 26 |
FOR_TABLE(OTFCC_CHR('g','a','s','p'), table) { |
| 27 |
font_file_pointer data = table.data; |
| 28 |
uint32_t length = table.length; |
| 29 |
if (length < 4) { goto FAIL; } |
| 30 |
gasp = table_iGasp.create(); |
| 31 |
gasp->version = read_16u(data); |
| 32 |
tableid_t numRanges = read_16u(data + 2); |
| 33 |
if (length < 4 + numRanges * 4) { goto FAIL; } |
| 34 |
|
| 35 |
for (uint32_t j = 0; j < numRanges; j++) { |
| 36 |
gasp_Record record; |
| 37 |
record.rangeMaxPPEM = read_16u(data + 4 + j * 4); |
| 38 |
uint16_t rangeGaspBehavior = read_16u(data + 4 + j * 4 + 2); |
| 39 |
record.dogray = !!(rangeGaspBehavior & GASP_DOGRAY); |
| 40 |
record.gridfit = !!(rangeGaspBehavior & GASP_GRIDFIT); |
| 41 |
record.symmetric_smoothing = !!(rangeGaspBehavior & GASP_SYMMETRIC_SMOOTHING); |
| 42 |
record.symmetric_gridfit = !!(rangeGaspBehavior & GASP_SYMMETRIC_GRIDFIT); |
| 43 |
gasp_iRecordList.push(&gasp->records, record); |
| 44 |
} |
| 45 |
return gasp; |
| 46 |
|
| 47 |
FAIL: |
| 48 |
logWarning("table 'gasp' corrupted.\n"); |
| 49 |
table_iGasp.free(gasp); |
| 50 |
gasp = NULL; |
| 51 |
} |
| 52 |
return NULL; |
| 53 |
} |
| 54 |
void otfcc_dumpGasp(const table_gasp *table, json_value *root, const otfcc_Options *options) { |
| 55 |
if (!table) return; |
| 56 |
loggedStep("gasp") { |
| 57 |
json_value *t = json_array_new(table->records.length); |
| 58 |
for (uint16_t j = 0; j < table->records.length; j++) { |
| 59 |
json_value *rec = json_object_new(5); |
| 60 |
json_object_push(rec, "rangeMaxPPEM", |
| 61 |
json_integer_new(table->records.items[j].rangeMaxPPEM)); |
| 62 |
json_object_push(rec, "dogray", json_boolean_new(table->records.items[j].dogray)); |
| 63 |
json_object_push(rec, "gridfit", json_boolean_new(table->records.items[j].gridfit)); |
| 64 |
json_object_push(rec, "symmetric_smoothing", |
| 65 |
json_boolean_new(table->records.items[j].symmetric_smoothing)); |
| 66 |
json_object_push(rec, "symmetric_gridfit", |
| 67 |
json_boolean_new(table->records.items[j].symmetric_gridfit)); |
| 68 |
json_array_push(t, rec); |
| 69 |
} |
| 70 |
json_object_push(root, "gasp", t); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
table_gasp *otfcc_parseGasp(const json_value *root, const otfcc_Options *options) { |
| 75 |
table_gasp *gasp = NULL; |
| 76 |
json_value *table = NULL; |
| 77 |
if ((table = json_obj_get_type(root, "gasp", json_array))) { |
| 78 |
loggedStep("gasp") { |
| 79 |
gasp = table_iGasp.create(); |
| 80 |
for (uint16_t j = 0; j < table->u.array.length; j++) { |
| 81 |
json_value *r = table->u.array.values[j]; |
| 82 |
if (!r || r->type != json_object) continue; |
| 83 |
gasp_Record record; |
| 84 |
record.rangeMaxPPEM = json_obj_getint_fallback(r, "rangeMaxPPEM", 0xFFFF); |
| 85 |
record.dogray = json_obj_getbool(r, "dogray"); |
| 86 |
record.gridfit = json_obj_getbool(r, "gridfit"); |
| 87 |
record.symmetric_smoothing = json_obj_getbool(r, "symmetric_smoothing"); |
| 88 |
record.symmetric_gridfit = json_obj_getbool(r, "symmetric_gridfit"); |
| 89 |
gasp_iRecordList.push(&gasp->records, record); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return gasp; |
| 94 |
} |
| 95 |
|
| 96 |
caryll_Buffer *otfcc_buildGasp(const table_gasp *gasp, const otfcc_Options *options) { |
| 97 |
if (!gasp) return NULL; |
| 98 |
caryll_Buffer *buf = bufnew(); |
| 99 |
bufwrite16b(buf, 1); |
| 100 |
bufwrite16b(buf, gasp->records.length); |
| 101 |
for (uint16_t j = 0; j < gasp->records.length; j++) { |
| 102 |
gasp_Record *r = &(gasp->records.items[j]); |
| 103 |
bufwrite16b(buf, r->rangeMaxPPEM); |
| 104 |
bufwrite16b(buf, (r->dogray ? GASP_DOGRAY : 0) | (r->gridfit ? GASP_GRIDFIT : 0) | |
| 105 |
(r->symmetric_gridfit ? GASP_SYMMETRIC_GRIDFIT : 0) | |
| 106 |
(r->symmetric_smoothing ? GASP_SYMMETRIC_SMOOTHING : 0)); |
| 107 |
} |
| 108 |
return buf; |
| 109 |
} |