| 1 |
#ifndef CARYLL_SUPPORT_JSON_FUNCS_H |
| 2 |
#define CARYLL_SUPPORT_JSON_FUNCS_H |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <math.h> |
| 6 |
|
| 7 |
#include "dep/json-builder.h" |
| 8 |
#include "dep/json.h" |
| 9 |
#include "dep/sds.h" |
| 10 |
|
| 11 |
#include "caryll/ownership.h" |
| 12 |
#include "otfcc/primitives.h" |
| 13 |
#include "otfcc/vf/vq.h" |
| 14 |
#include "otfcc/table/fvar.h" |
| 15 |
|
| 16 |
#ifndef INLINE |
| 17 |
#ifdef _MSC_VER |
| 18 |
#define INLINE __forceinline /* use __forceinline (VC++ specific) */ |
| 19 |
#else |
| 20 |
#define INLINE inline /* use standard inline */ |
| 21 |
#endif |
| 22 |
#endif |
| 23 |
|
| 24 |
static INLINE json_value *preserialize(MOVE json_value *x); |
| 25 |
|
| 26 |
static INLINE json_value *json_obj_get(const json_value *obj, const char *key) { |
| 27 |
if (!obj || obj->type != json_object) return NULL; |
| 28 |
for (uint32_t _k = 0; _k < obj->u.object.length; _k++) { |
| 29 |
char *ck = obj->u.object.values[_k].name; |
| 30 |
if (strcmp(ck, key) == 0) return obj->u.object.values[_k].value; |
| 31 |
} |
| 32 |
return NULL; |
| 33 |
} |
| 34 |
static INLINE json_value *json_obj_get_type(const json_value *obj, const char *key, |
| 35 |
const json_type type) { |
| 36 |
json_value *v = json_obj_get(obj, key); |
| 37 |
if (v && v->type == type) return v; |
| 38 |
return NULL; |
| 39 |
} |
| 40 |
static INLINE sds json_obj_getsds(const json_value *obj, const char *key) { |
| 41 |
json_value *v = json_obj_get_type(obj, key, json_string); |
| 42 |
if (!v) |
| 43 |
return NULL; |
| 44 |
else |
| 45 |
return sdsnewlen(v->u.string.ptr, v->u.string.length); |
| 46 |
} |
| 47 |
static INLINE char *json_obj_getstr_share(const json_value *obj, const char *key) { |
| 48 |
json_value *v = json_obj_get_type(obj, key, json_string); |
| 49 |
if (!v) |
| 50 |
return NULL; |
| 51 |
else |
| 52 |
return v->u.string.ptr; |
| 53 |
} |
| 54 |
|
| 55 |
static INLINE json_value *json_object_push_tag(json_value *a, uint32_t tag, json_value *b) { |
| 56 |
char tags[4] = {(tag & 0xff000000) >> 24, (tag & 0xff0000) >> 16, (tag & 0xff00) >> 8, |
| 57 |
(tag & 0xff)}; |
| 58 |
return json_object_push_length(a, 4, tags, b); |
| 59 |
} |
| 60 |
|
| 61 |
// Coordinates, VV and VQ |
| 62 |
static INLINE double json_numof(const json_value *cv) { |
| 63 |
if (cv && cv->type == json_integer) return cv->u.integer; |
| 64 |
if (cv && cv->type == json_double) return cv->u.dbl; |
| 65 |
return 0; |
| 66 |
} |
| 67 |
static INLINE json_value *json_new_position(pos_t z) { |
| 68 |
if (round(z) == z) { |
| 69 |
return json_integer_new(z); |
| 70 |
} else { |
| 71 |
return json_double_new(z); |
| 72 |
} |
| 73 |
} |
| 74 |
json_value *json_new_VQRegion_Explicit(const vq_Region *rs, const table_fvar *fvar); |
| 75 |
json_value *json_new_VQRegion(const vq_Region *rs, const table_fvar *fvar); |
| 76 |
json_value *json_new_VQ(const VQ z, const table_fvar *fvar); |
| 77 |
json_value *json_new_VV(const VV x, const table_fvar *fvar); |
| 78 |
json_value *json_new_VVp(const VV *x, const table_fvar *fvar); |
| 79 |
VQ json_vqOf(const json_value *cv, const table_fvar *fvar); |
| 80 |
|
| 81 |
static INLINE double json_obj_getnum(const json_value *obj, const char *key) { |
| 82 |
if (!obj || obj->type != json_object) return 0.0; |
| 83 |
for (uint32_t _k = 0; _k < obj->u.object.length; _k++) { |
| 84 |
char *ck = obj->u.object.values[_k].name; |
| 85 |
json_value *cv = obj->u.object.values[_k].value; |
| 86 |
if (strcmp(ck, key) == 0) { |
| 87 |
if (cv && cv->type == json_integer) return cv->u.integer; |
| 88 |
if (cv && cv->type == json_double) return cv->u.dbl; |
| 89 |
} |
| 90 |
} |
| 91 |
return 0.0; |
| 92 |
} |
| 93 |
static INLINE int32_t json_obj_getint(const json_value *obj, const char *key) { |
| 94 |
if (!obj || obj->type != json_object) return 0; |
| 95 |
for (uint32_t _k = 0; _k < obj->u.object.length; _k++) { |
| 96 |
char *ck = obj->u.object.values[_k].name; |
| 97 |
json_value *cv = obj->u.object.values[_k].value; |
| 98 |
if (strcmp(ck, key) == 0) { |
| 99 |
if (cv && cv->type == json_integer) return (int32_t)cv->u.integer; |
| 100 |
if (cv && cv->type == json_double) return cv->u.dbl; |
| 101 |
} |
| 102 |
} |
| 103 |
return 0; |
| 104 |
} |
| 105 |
static INLINE double json_obj_getnum_fallback(const json_value *obj, const char *key, |
| 106 |
double fallback) { |
| 107 |
if (!obj || obj->type != json_object) return fallback; |
| 108 |
for (uint32_t _k = 0; _k < obj->u.object.length; _k++) { |
| 109 |
char *ck = obj->u.object.values[_k].name; |
| 110 |
json_value *cv = obj->u.object.values[_k].value; |
| 111 |
if (strcmp(ck, key) == 0) { |
| 112 |
if (cv && cv->type == json_integer) return cv->u.integer; |
| 113 |
if (cv && cv->type == json_double) return cv->u.dbl; |
| 114 |
} |
| 115 |
} |
| 116 |
return fallback; |
| 117 |
} |
| 118 |
static INLINE int32_t json_obj_getint_fallback(const json_value *obj, const char *key, |
| 119 |
int32_t fallback) { |
| 120 |
if (!obj || obj->type != json_object) return fallback; |
| 121 |
for (uint32_t _k = 0; _k < obj->u.object.length; _k++) { |
| 122 |
char *ck = obj->u.object.values[_k].name; |
| 123 |
json_value *cv = obj->u.object.values[_k].value; |
| 124 |
if (strcmp(ck, key) == 0) { |
| 125 |
if (cv && cv->type == json_integer) return (int32_t)cv->u.integer; |
| 126 |
if (cv && cv->type == json_double) return cv->u.dbl; |
| 127 |
} |
| 128 |
} |
| 129 |
return fallback; |
| 130 |
} |
| 131 |
static INLINE bool json_boolof(const json_value *cv) { |
| 132 |
if (cv && cv->type == json_boolean) return cv->u.boolean; |
| 133 |
return false; |
| 134 |
} |
| 135 |
static INLINE bool json_obj_getbool(const json_value *obj, const char *key) { |
| 136 |
if (!obj || obj->type != json_object) return false; |
| 137 |
for (uint32_t _k = 0; _k < obj->u.object.length; _k++) { |
| 138 |
char *ck = obj->u.object.values[_k].name; |
| 139 |
json_value *cv = obj->u.object.values[_k].value; |
| 140 |
if (strcmp(ck, key) == 0) { |
| 141 |
if (cv && cv->type == json_boolean) return cv->u.boolean; |
| 142 |
} |
| 143 |
} |
| 144 |
return false; |
| 145 |
} |
| 146 |
static INLINE bool json_obj_getbool_fallback(const json_value *obj, const char *key, |
| 147 |
bool fallback) { |
| 148 |
if (!obj || obj->type != json_object) return fallback; |
| 149 |
for (uint32_t _k = 0; _k < obj->u.object.length; _k++) { |
| 150 |
char *ck = obj->u.object.values[_k].name; |
| 151 |
json_value *cv = obj->u.object.values[_k].value; |
| 152 |
if (strcmp(ck, key) == 0) { |
| 153 |
if (cv && cv->type == json_boolean) return cv->u.boolean; |
| 154 |
} |
| 155 |
} |
| 156 |
return fallback; |
| 157 |
} |
| 158 |
|
| 159 |
static INLINE json_value *json_from_sds(const sds str) { |
| 160 |
return json_string_new_length((uint32_t)sdslen(str), str); |
| 161 |
} |
| 162 |
|
| 163 |
// flags reader and writer |
| 164 |
static INLINE json_value *otfcc_dump_flags(int flags, const char *labels[]) { |
| 165 |
json_value *v = json_object_new(0); |
| 166 |
for (uint16_t j = 0; labels[j]; j++) |
| 167 |
if (flags & (1 << j)) { json_object_push(v, labels[j], json_boolean_new(true)); } |
| 168 |
return v; |
| 169 |
} |
| 170 |
static INLINE uint32_t otfcc_parse_flags(const json_value *v, const char *labels[]) { |
| 171 |
if (!v) return 0; |
| 172 |
if (v->type == json_integer) { |
| 173 |
return (uint32_t)v->u.integer; |
| 174 |
} else if (v->type == json_double) { |
| 175 |
return (uint32_t)v->u.dbl; |
| 176 |
} else if (v->type == json_object) { |
| 177 |
uint32_t flags = 0; |
| 178 |
for (uint16_t j = 0; labels[j]; j++) { |
| 179 |
if (json_obj_getbool(v, labels[j])) { flags |= (1 << j); } |
| 180 |
} |
| 181 |
return flags; |
| 182 |
} else { |
| 183 |
return 0; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
static INLINE json_value *preserialize(MOVE json_value *x) { |
| 188 |
#ifdef CARYLL_USE_PRE_SERIALIZED |
| 189 |
json_serialize_opts opts = {.mode = json_serialize_mode_packed}; |
| 190 |
size_t preserialize_len = json_measure_ex(x, opts); |
| 191 |
char *buf = (char *)malloc(preserialize_len); |
| 192 |
json_serialize_ex(buf, x, opts); |
| 193 |
json_builder_free(x); |
| 194 |
|
| 195 |
json_value *xx = json_string_new_nocopy((uint32_t)(preserialize_len - 1), buf); |
| 196 |
xx->type = json_pre_serialized; |
| 197 |
return xx; |
| 198 |
#else |
| 199 |
return x; |
| 200 |
#endif |
| 201 |
} |
| 202 |
|
| 203 |
#endif |