-
Notifications
You must be signed in to change notification settings - Fork 4
/
js_size.cpp
632 lines (523 loc) · 17 KB
/
js_size.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#include "js_size.hpp"
#include "js_alloc.hpp"
#include "js_array.hpp"
#include "js_rect.hpp"
#include "jsbindings.hpp"
#include <quickjs.h>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <new>
#include <ostream>
#include <string>
enum js_size_fit_t { JS_SIZE_FIT_WIDTH = 1, JS_SIZE_FIT_HEIGHT = 2, JS_SIZE_FIT_INSIDE, JS_SIZE_FIT_OUTSIDE };
enum { SIZE_PROP_WIDTH, SIZE_PROP_HEIGHT, SIZE_PROP_ASPECT, SIZE_PROP_EMPTY, SIZE_PROP_AREA };
extern "C" {
thread_local JSValue size_proto = JS_UNDEFINED, size_class = JS_UNDEFINED;
thread_local JSClassID js_size_class_id = 0;
}
template<class T>
static inline JSSizeData<T>
js_size_fit(const JSSizeData<T>& size, T to, js_size_fit_t mode) {
JSSizeData<T> ret;
switch(mode) {
case JS_SIZE_FIT_WIDTH: {
ret.width = to;
ret.height = size.height * (to / size.width);
break;
}
case JS_SIZE_FIT_HEIGHT: {
ret.width = size.width * (to / size.height);
ret.height = to;
break;
}
}
return ret;
}
template<class T>
static JSSizeData<T>
js_size_fit(const JSSizeData<T>& size, const JSSizeData<T>& to, js_size_fit_t mode) {
JSSizeData<T> ret;
switch(mode) {
case JS_SIZE_FIT_INSIDE:
case JS_SIZE_FIT_OUTSIDE: {
ret = js_size_fit(size, to, JS_SIZE_FIT_WIDTH);
if(mode == JS_SIZE_FIT_INSIDE && ret.height > to.height)
ret = js_size_fit(size, to.height, JS_SIZE_FIT_HEIGHT);
break;
}
default: ret = js_size_fit(size, mode == JS_SIZE_FIT_WIDTH ? to.width : to.height, mode); break;
}
return ret;
}
static JSValue
js_size_constructor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst argv[]) {
JSSizeData<double> size, *s;
JSValue obj = JS_UNDEFINED;
JSValue proto;
s = js_allocate<JSSizeData<double>>(ctx);
if(!s)
return JS_EXCEPTION;
new(s) JSSizeData<double>();
if(argc > 0) {
if(js_size_read(ctx, argv[0], &size)) {
*s = size;
} else {
if(JS_ToFloat64(ctx, &size.width, argv[0]))
goto fail;
if(argc < 2 || JS_ToFloat64(ctx, &size.height, argv[1]))
goto fail;
*s = size;
}
}
/* using new_target to get the prototype is necessary when the
class is extended. */
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
if(JS_IsException(proto))
goto fail;
obj = JS_NewObjectProtoClass(ctx, proto, js_size_class_id);
JS_FreeValue(ctx, proto);
if(JS_IsException(obj))
goto fail;
JS_SetOpaque(obj, s);
return obj;
fail:
js_deallocate(ctx, s);
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
JSSizeData<double>*
js_size_data(JSValueConst val) {
return static_cast<JSSizeData<double>*>(JS_GetOpaque(val, js_size_class_id));
}
JSSizeData<double>*
js_size_data2(JSContext* ctx, JSValueConst val) {
return static_cast<JSSizeData<double>*>(JS_GetOpaque2(ctx, val, js_size_class_id));
}
static JSValue
js_size_get(JSContext* ctx, JSValueConst this_val, int magic) {
JSSizeData<double>* s;
JSValue ret = JS_UNDEFINED;
if(!(s = js_size_data2(ctx, this_val)))
return JS_EXCEPTION;
switch(magic) {
case SIZE_PROP_WIDTH: {
ret = JS_NewFloat64(ctx, s->width);
break;
}
case SIZE_PROP_HEIGHT: {
ret = JS_NewFloat64(ctx, s->height);
break;
}
case SIZE_PROP_ASPECT: {
ret = JS_NewFloat64(ctx, s->aspectRatio());
break;
}
case SIZE_PROP_EMPTY: {
ret = JS_NewBool(ctx, s->empty());
break;
}
case SIZE_PROP_AREA: {
ret = JS_NewFloat64(ctx, s->area());
break;
}
}
return ret;
}
JSValue
js_size_new(JSContext* ctx, double w, double h) {
JSValue ret;
JSSizeData<double>* s;
if(JS_IsUndefined(size_proto))
js_size_init(ctx, NULL);
ret = JS_NewObjectProtoClass(ctx, size_proto, js_size_class_id);
s = js_allocate<JSSizeData<double>>(ctx);
s->width = /*w <= DBL_EPSILON ? 0 :*/ w;
s->height = /*h <= DBL_EPSILON ? 0 :*/ h;
JS_SetOpaque(ret, s);
return ret;
}
JSValue
js_size_wrap(JSContext* ctx, const JSSizeData<double>& sz) {
return js_size_new(ctx, sz.width, sz.height);
}
static JSValue
js_size_set(JSContext* ctx, JSValueConst this_val, JSValueConst val, int magic) {
JSSizeData<double>* s;
double v;
if(!(s = js_size_data2(ctx, this_val)))
return JS_EXCEPTION;
if(JS_ToFloat64(ctx, &v, val))
return JS_EXCEPTION;
if(magic == 0)
s->width = v;
else
s->height = v;
return JS_UNDEFINED;
}
static JSValue
js_size_to_string(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
JSSizeData<double> size, *s;
std::ostringstream os;
const char* delim = "×";
if((s = js_size_data(this_val)) == nullptr) {
js_size_read(ctx, this_val, &size);
} else {
size = *s;
}
if(argc > 0)
delim = JS_ToCString(ctx, argv[0]);
os << size.width << delim << size.height;
return JS_NewString(ctx, os.str().c_str());
}
static JSValue
js_size_to_source(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
JSSizeData<double> size, *s;
std::ostringstream os;
if((s = js_size_data(this_val)) == nullptr) {
js_size_read(ctx, this_val, &size);
} else {
size = *s;
}
os << "{width:" << size.width << ",height:" << size.height << "}";
return JS_NewString(ctx, os.str().c_str());
}
static JSValue
js_size_inspect(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
JSSizeData<double>* s = js_size_data2(ctx, this_val);
JSValue obj = JS_NewObjectClass(ctx, js_size_class_id);
JS_DefinePropertyValueStr(ctx, obj, "width", JS_NewFloat64(ctx, s->width), JS_PROP_ENUMERABLE);
JS_DefinePropertyValueStr(ctx, obj, "height", JS_NewFloat64(ctx, s->height), JS_PROP_ENUMERABLE);
return obj;
}
enum {
SIZE_METHOD_EQUALS,
SIZE_METHOD_ROUND,
SIZE_METHOD_FLOOR,
SIZE_METHOD_CEIL,
SIZE_METHOD_TOOBJECT,
SIZE_METHOD_TOARRAY,
SIZE_METHOD_FITWIDTH,
SIZE_METHOD_FITHEIGHT,
SIZE_METHOD_FITINSIDE,
SIZE_METHOD_FITOUTSIDE,
SIZE_METHOD_ALIGN,
};
static JSValue
js_size_funcs(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[], int magic) {
JSSizeData<double> size, *s;
JSValue ret = JS_UNDEFINED;
if((s = js_size_data2(ctx, this_val)) == nullptr)
return ret;
size = *s;
switch(magic) {
case SIZE_METHOD_EQUALS: {
JSSizeData<double> other, *arg;
bool equals = false;
if(argc > 0) {
if((arg = js_size_data(argv[0]))) {
equals = s->width == arg->width && s->height == arg->height;
} else if(js_size_read(ctx, argv[0], &other)) {
equals = s->width == other.width && s->height == other.height;
}
ret = JS_NewBool(ctx, equals);
}
break;
}
case SIZE_METHOD_ROUND: {
double width, height, f;
int32_t precision = 0;
if(argc > 0)
JS_ToInt32(ctx, &precision, argv[0]);
f = std::pow(10, precision);
width = std::round(size.width * f) / f;
height = std::round(size.height * f) / f;
ret = js_size_new(ctx, width, height);
break;
}
case SIZE_METHOD_FLOOR: {
double width, height, f;
int32_t precision = 0;
if(argc > 0)
JS_ToInt32(ctx, &precision, argv[0]);
f = std::pow(10, precision);
width = std::floor(size.width * f) / f;
height = std::floor(size.height * f) / f;
ret = js_size_new(ctx, width, height);
break;
}
case SIZE_METHOD_CEIL: {
double width, height, f;
int32_t precision = 0;
if(argc > 0)
JS_ToInt32(ctx, &precision, argv[0]);
f = std::pow(10, precision);
width = std::ceil(size.width * f) / f;
height = std::ceil(size.height * f) / f;
ret = js_size_new(ctx, width, height);
break;
}
case SIZE_METHOD_TOOBJECT: {
ret = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, ret, "width", JS_NewFloat64(ctx, size.width));
JS_SetPropertyStr(ctx, ret, "height", JS_NewFloat64(ctx, size.height));
break;
}
case SIZE_METHOD_TOARRAY: {
ret = JS_NewArray(ctx);
JS_SetPropertyUint32(ctx, ret, 0, JS_NewFloat64(ctx, size.width));
JS_SetPropertyUint32(ctx, ret, 1, JS_NewFloat64(ctx, size.height));
break;
}
case SIZE_METHOD_FITWIDTH:
case SIZE_METHOD_FITHEIGHT:
case SIZE_METHOD_FITINSIDE:
case SIZE_METHOD_FITOUTSIDE: {
JSSizeData<double> other, result;
double arg;
js_size_fit_t fit_type = js_size_fit_t(magic - 3);
if(magic >= 6) {}
if(js_size_read(ctx, argv[0], &other)) {
result = js_size_fit(size, other, fit_type);
} else if(fit_type == JS_SIZE_FIT_WIDTH || fit_type == JS_SIZE_FIT_HEIGHT) {
JS_ToFloat64(ctx, &arg, argv[0]);
result = js_size_fit(size, arg, fit_type);
}
if(!result.empty())
ret = js_size_new(ctx, result.width, result.height);
break;
}
case SIZE_METHOD_ALIGN: {
JSSizeData<double> sz;
JSPointData<double> pt[2];
JSRectData<double> other, rect;
int32_t align = 0;
if(!js_size_read(ctx, argv[0], &sz))
return JS_ThrowTypeError(ctx, "argument 1 must be cv.Size");
JS_ToInt32(ctx, &align, argv[1]);
js_point_read(ctx, argv[0], &pt[0]);
other.x = pt[0].x;
other.y = pt[0].y;
other.width = sz.width;
other.height = sz.height;
pt[1].x = pt[0].x + sz.width;
pt[1].y = pt[0].y + sz.height;
rect.width = size.width;
rect.height = size.height;
switch(align & ALIGN_HORIZONTAL) {
case ALIGN_LEFT: {
rect.x = other.x;
break;
}
case ALIGN_CENTER: {
rect.x = other.x + (other.width - size.width) / 2;
break;
}
case ALIGN_RIGHT: {
rect.x = other.x + other.width - size.width;
break;
}
}
switch(align & ALIGN_VERTICAL) {
case ALIGN_TOP: {
rect.y = other.y;
break;
}
case ALIGN_MIDDLE: {
rect.y = other.y + (other.height - size.height) / 2;
break;
}
case ALIGN_BOTTOM: {
rect.y = other.y + other.height - size.height;
break;
}
}
ret = js_rect_new(ctx, rect);
break;
}
}
return ret;
}
static JSValue
js_size_add(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
JSSizeData<double>*s, size;
if((s = js_size_data2(ctx, this_val)) == nullptr)
return JS_EXCEPTION;
if(js_size_read(ctx, argv[0], &size)) {
s->width += size.width;
s->height += size.height;
} else if(argc >= 2 && JS_IsNumber(argv[0]) && JS_IsNumber(argv[1])) {
double width, height;
JS_ToFloat64(ctx, &width, argv[0]);
JS_ToFloat64(ctx, &height, argv[1]);
s->width += width;
s->height += height;
}
return JS_DupValue(ctx, this_val);
}
static JSValue
js_size_sub(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
JSSizeData<double>*s, size;
if((s = js_size_data2(ctx, this_val)) == nullptr)
return JS_EXCEPTION;
if(js_size_read(ctx, argv[0], &size)) {
s->width -= size.width;
s->height -= size.height;
} else if(argc >= 2 && JS_IsNumber(argv[0]) && JS_IsNumber(argv[1])) {
double width, height;
JS_ToFloat64(ctx, &width, argv[0]);
JS_ToFloat64(ctx, &height, argv[1]);
s->width -= width;
s->height -= height;
}
return JS_DupValue(ctx, this_val);
}
static JSValue
js_size_mul(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
JSSizeData<double> size, *s = js_size_data2(ctx, this_val);
double factor;
JS_ToFloat64(ctx, &factor, argv[0]);
size = *s;
size.width *= factor;
size.height *= factor;
return js_size_wrap(ctx, size);
}
static JSValue
js_size_div(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
JSSizeData<double> size, other, *s, *a = nullptr;
JSValue ret = JS_EXCEPTION;
if((s = js_size_data2(ctx, this_val)) != nullptr) {
size = *s;
if(JS_IsNumber(argv[0])) {
double divider;
JS_ToFloat64(ctx, ÷r, argv[0]);
size.width /= divider;
size.height /= divider;
ret = js_size_wrap(ctx, size);
} else {
if((a = js_size_data(argv[0])) != nullptr)
other = *a;
else {
js_size_read(ctx, argv[0], &other);
a = &other;
}
std::array<double, 2> result{size.width / other.width, size.height / other.height};
ret = JS_NewArray(ctx);
JS_SetPropertyUint32(ctx, ret, 0, JS_NewFloat64(ctx, result[0]));
JS_SetPropertyUint32(ctx, ret, 1, JS_NewFloat64(ctx, result[1]));
}
}
return ret;
}
static JSAtom iterator_symbol;
static JSValue
js_size_symbol_iterator(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst argv[]) {
JSValue arr, iter;
arr = js_size_funcs(ctx, this_val, argc, argv, SIZE_METHOD_TOARRAY);
if(iterator_symbol == 0)
iterator_symbol = js_symbol_atom(ctx, "iterator");
if(!JS_IsFunction(ctx, (iter = JS_GetProperty(ctx, arr, iterator_symbol))))
return JS_EXCEPTION;
return JS_Call(ctx, iter, arr, 0, argv);
}
static JSValue
js_size_from(JSContext* ctx, JSValueConst size, int argc, JSValueConst argv[]) {
std::array<double, 2> array;
JSValue ret = JS_EXCEPTION;
if(JS_IsString(argv[0])) {
const char* str = JS_ToCString(ctx, argv[0]);
char* endptr = nullptr;
for(size_t i = 0; i < 2; i++) {
while(!isdigit(*str) && *str != '-' && *str != '+' && !(*str == '.' && isdigit(str[1])))
str++;
if(*str == '\0')
break;
array[i] = strtod(str, &endptr);
str = endptr;
}
} else if(js_is_array(ctx, argv[0])) {
js_array_to<double, 2>(ctx, argv[0], array);
}
if(array[0] > 0 && array[1] > 0)
ret = js_size_new(ctx, array[0], array[1]);
return ret;
}
void
js_size_finalizer(JSRuntime* rt, JSValue val) {
JSSizeData<double>* s = static_cast<JSSizeData<double>*>(JS_GetOpaque(val, js_size_class_id));
/* Note: 's' can be NULL in case JS_SetOpaque() was not called */
if(s)
js_deallocate(rt, s);
}
JSClassDef js_size_class = {
.class_name = "Size",
.finalizer = js_size_finalizer,
};
const JSCFunctionListEntry js_size_proto_funcs[] = {
JS_CGETSET_ENUMERABLE_DEF("width", js_size_get, js_size_set, SIZE_PROP_WIDTH),
JS_CGETSET_ENUMERABLE_DEF("height", js_size_get, js_size_set, SIZE_PROP_HEIGHT),
JS_CGETSET_MAGIC_DEF("aspect", js_size_get, 0, SIZE_PROP_ASPECT),
JS_CGETSET_MAGIC_DEF("empty", js_size_get, 0, SIZE_PROP_EMPTY),
JS_CGETSET_MAGIC_DEF("area", js_size_get, 0, SIZE_PROP_AREA),
JS_CFUNC_MAGIC_DEF("equals", 1, js_size_funcs, SIZE_METHOD_EQUALS),
JS_CFUNC_MAGIC_DEF("round", 0, js_size_funcs, SIZE_METHOD_ROUND),
JS_CFUNC_MAGIC_DEF("floor", 0, js_size_funcs, SIZE_METHOD_FLOOR),
JS_CFUNC_MAGIC_DEF("ceil", 0, js_size_funcs, SIZE_METHOD_CEIL),
JS_CFUNC_MAGIC_DEF("toObject", 0, js_size_funcs, SIZE_METHOD_TOOBJECT),
JS_CFUNC_MAGIC_DEF("toArray", 0, js_size_funcs, SIZE_METHOD_TOARRAY),
JS_CFUNC_MAGIC_DEF("fitWidth", 0, js_size_funcs, SIZE_METHOD_FITWIDTH),
JS_CFUNC_MAGIC_DEF("fitHeight", 0, js_size_funcs, SIZE_METHOD_FITHEIGHT),
JS_CFUNC_MAGIC_DEF("fitInside", 0, js_size_funcs, SIZE_METHOD_FITINSIDE),
JS_CFUNC_MAGIC_DEF("fitOutside", 0, js_size_funcs, SIZE_METHOD_FITOUTSIDE),
JS_CFUNC_MAGIC_DEF("align", 1, js_size_funcs, SIZE_METHOD_ALIGN),
JS_CFUNC_DEF("toString", 0, js_size_to_string),
JS_CFUNC_DEF("toSource", 0, js_size_to_source),
JS_CFUNC_DEF("add", 1, js_size_add),
JS_CFUNC_DEF("sub", 1, js_size_sub),
JS_CFUNC_DEF("mul", 1, js_size_mul),
JS_CFUNC_DEF("div", 1, js_size_div),
JS_ALIAS_DEF("values", "toArray"),
JS_CFUNC_DEF("[Symbol.iterator]", 0, js_size_symbol_iterator),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Size", JS_PROP_CONFIGURABLE),
};
const JSCFunctionListEntry js_size_static_funcs[] = {JS_CFUNC_DEF("from", 1, js_size_from)};
int
js_size_init(JSContext* ctx, JSModuleDef* m) {
if(js_size_class_id == 0) {
/* create the Size class */
JS_NewClassID(&js_size_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_size_class_id, &js_size_class);
size_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, size_proto, js_size_proto_funcs, countof(js_size_proto_funcs));
JS_SetClassProto(ctx, js_size_class_id, size_proto);
size_class = JS_NewCFunction2(ctx, js_size_constructor, "Size", 0, JS_CFUNC_constructor, 0);
/* set proto.constructor and ctor.prototype */
JS_SetConstructor(ctx, size_class, size_proto);
JS_SetPropertyFunctionList(ctx, size_class, js_size_static_funcs, countof(js_size_static_funcs));
// js_set_inspect_method(ctx, size_proto, js_size_inspect);
}
if(m)
JS_SetModuleExport(ctx, m, "Size", size_class);
/*else
JS_SetPropertyStr(ctx, *static_cast<JSValue*>(m), name, size_class);*/
return 0;
}
extern "C" void
js_size_export(JSContext* ctx, JSModuleDef* m) {
JS_AddModuleExport(ctx, m, "Size");
}
#ifdef JS_SIZE_MODULE
#define JS_INIT_MODULE VISIBLE js_init_module
#else
#define JS_INIT_MODULE js_init_module_size
#endif
extern "C" JSModuleDef*
JS_INIT_MODULE(JSContext* ctx, const char* module_name) {
JSModuleDef* m;
if(!(m = JS_NewCModule(ctx, module_name, &js_size_init)))
return NULL;
js_size_export(ctx, m);
return m;
}