w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

json-funcs.h
Go to the documentation of this file.
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 
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) {
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) {
49  if (!v)
50  return NULL;
51  else
52  return v->u.string.ptr;
53 }
54 
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 }
68  if (round(z) == z) {
69  return json_integer_new(z);
70  } else {
71  return json_double_new(z);
72  }
73 }
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 
161 }
162 
163 // flags reader and writer
164 static INLINE json_value *otfcc_dump_flags(int flags, const char *labels[]) {
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 
188 #ifdef CARYLL_USE_PRE_SERIALIZED
190  size_t preserialize_len = json_measure_ex(x, opts);
191  char *buf = (char *)malloc(preserialize_len);
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
@ json_pre_serialized
Definition: CPAL.d:8692
#define type(a)
Definition: aptex-macros.h:171
static struct sed_label * labels
Definition: compile.c:117
#define b
Definition: jpegint.h:372
Options opts[]
Definition: dt2dv.c:309
int z
Definition: dviconv.c:26
int v
Definition: dviconv.c:10
static int xx[600]
Definition: psspecial.c:35
int strcmp()
Definition: coll.cpp:143
#define a(n)
Definition: gpos-common.c:148
static char * tags[]
Definition: htex.c:62
json_value * json_boolean_new(int)
json_value * json_string_new_length(unsigned int length, const char *)
void json_builder_free(json_value *)
void json_serialize_ex(char *buf, json_value *, json_serialize_opts)
json_value * json_string_new_nocopy(unsigned int length, char *)
#define json_serialize_mode_packed
Definition: json-builder.h:104
json_value * json_object_push(json_value *object, const char *name, json_value *)
json_value * json_integer_new(int64_t)
size_t json_measure_ex(json_value *, json_serialize_opts)
json_value * json_object_new(size_t length)
json_value * json_double_new(double)
json_value * json_object_push_length(json_value *object, unsigned int name_length, const char *name, json_value *)
static double json_numof(const json_value *cv)
Definition: json-funcs.h:62
static int32_t json_obj_getint_fallback(const json_value *obj, const char *key, int32_t fallback)
Definition: json-funcs.h:118
static bool json_boolof(const json_value *cv)
Definition: json-funcs.h:131
#define INLINE
Definition: json-funcs.h:20
static char * json_obj_getstr_share(const json_value *obj, const char *key)
Definition: json-funcs.h:47
static bool json_obj_getbool_fallback(const json_value *obj, const char *key, bool fallback)
Definition: json-funcs.h:146
static json_value * preserialize(MOVE json_value *x)
Definition: json-funcs.h:187
static json_value * json_obj_get(const json_value *obj, const char *key)
Definition: json-funcs.h:26
static double json_obj_getnum(const json_value *obj, const char *key)
Definition: json-funcs.h:81
static json_value * json_obj_get_type(const json_value *obj, const char *key, const json_type type)
Definition: json-funcs.h:34
static int32_t json_obj_getint(const json_value *obj, const char *key)
Definition: json-funcs.h:93
static json_value * json_new_position(pos_t z)
Definition: json-funcs.h:67
static json_value * otfcc_dump_flags(int flags, const char *labels[])
Definition: json-funcs.h:164
static sds json_obj_getsds(const json_value *obj, const char *key)
Definition: json-funcs.h:40
json_value * json_new_VQRegion_Explicit(const vq_Region *rs, const table_fvar *fvar)
Definition: fvar.c:303
static bool json_obj_getbool(const json_value *obj, const char *key)
Definition: json-funcs.h:135
static json_value * json_from_sds(const sds str)
Definition: json-funcs.h:159
VQ json_vqOf(const json_value *cv, const table_fvar *fvar)
Definition: fvar.c:288
json_value * json_new_VVp(const VV *x, const table_fvar *fvar)
Definition: fvar.c:266
static uint32_t otfcc_parse_flags(const json_value *v, const char *labels[])
Definition: json-funcs.h:170
json_value * json_new_VQ(const VQ z, const table_fvar *fvar)
Definition: fvar.c:233
json_value * json_new_VV(const VV x, const table_fvar *fvar)
Definition: fvar.c:247
static double json_obj_getnum_fallback(const json_value *obj, const char *key, double fallback)
Definition: json-funcs.h:105
json_value * json_new_VQRegion(const vq_Region *rs, const table_fvar *fvar)
Definition: fvar.c:319
static json_value * json_object_push_tag(json_value *a, uint32_t tag, json_value *b)
Definition: json-funcs.h:55
#define NULL
Definition: ftobjs.h:61
unsigned short uint16_t
Definition: stdint.h:79
unsigned int uint32_t
Definition: stdint.h:80
signed int int32_t
Definition: stdint.h:77
#define buf
static const unsigned char cv[]
Definition: nkf.c:555
#define malloc
Definition: alloca.c:91
float x
Definition: cordic.py:15
#define round(a)
Definition: pbmtopk.c:22
@ MOVE
Definition: pl-parser.h:87
double pos_t
Definition: primitives.h:24
#define flags
json_type
Definition: json.h:83
@ json_double
Definition: json.h:88
@ json_string
Definition: json.h:89
@ json_boolean
Definition: json.h:90
@ json_integer
Definition: json.h:87
@ json_object
Definition: json.h:85
sds sdsnewlen(const void *init, size_t initlen)
static size_t sdslen(const sds s)
Definition: sds.h:91
char * sds
Definition: sds.h:41
#define str(s)
Definition: sh6.c:399
Definition: vq.h:37
Definition: CPAL.d:17782
char * name
Definition: json.h:102
struct _json_value * value
Definition: json.h:105
json_type type
Definition: json.h:113
unsigned int length
Definition: json.h:123
struct _json_value::@1795::@1798 object
json_object_entry * values
Definition: json.h:132
union _json_value::@1795 u
Definition: xmlparse.c:179
int j
Definition: t4ht.c:1589
#define key
Definition: tex2xindy.c:753
static UBool fallback(char *loc)
Definition: ucurr.cpp:604