-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplutoprint.c
More file actions
1110 lines (927 loc) · 31 KB
/
Copy pathplutoprint.c
File metadata and controls
1110 lines (927 loc) · 31 KB
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <node_api.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <plutobook.h>
static bool get_callback_info(napi_env env, napi_callback_info info, size_t* argc, napi_value* argv, napi_value* thisArg, size_t required, size_t optional)
{
size_t provided = required + optional;
napi_get_cb_info(env, info, &provided, argv, thisArg, NULL);
size_t expected = required + optional;
if(expected == 0 && provided > 0) {
napi_throw_type_error(env, NULL, "No arguments expected");
return false;
}
if(provided < required) {
char msg[128];
snprintf(msg, sizeof(msg), "Expected at least %zu argument%s, got %zu", required, required == 1 ? "" : "s", provided);
napi_throw_type_error(env, NULL, msg);
return false;
}
if(provided > expected) {
char msg[128];
snprintf(msg, sizeof(msg), "Expected at most %zu argument%s, got %zu", expected, expected == 1 ? "" : "s", provided);
napi_throw_type_error(env, NULL, msg);
return false;
}
if(argc)
*argc = provided;
return true;
}
static const char* type_name(napi_valuetype type)
{
switch(type) {
case napi_undefined:
return "undefined";
case napi_null:
return "null";
case napi_boolean:
return "boolean";
case napi_number:
return "number";
case napi_string:
return "string";
case napi_symbol:
return "symbol";
case napi_object:
return "object";
case napi_function:
return "function";
case napi_external:
return "external";
case napi_bigint:
return "bigint";
default:
return "unknown";
}
}
static void throw_argument_type_error(napi_env env, napi_value* argv, size_t argi, napi_valuetype expected)
{
napi_valuetype provided;
napi_typeof(env, argv[argi], &provided);
char msg[128];
snprintf(msg, sizeof(msg), "Argument %zu must be %s, not %s", argi + 1, type_name(expected), type_name(provided));
napi_throw_type_error(env, NULL, msg);
}
static bool get_string_value(napi_env env, napi_value value, char** result)
{
size_t size;
napi_status status = napi_get_value_string_utf8(env, value, NULL, 0, &size);
if(status != napi_ok) {
return false;
}
char* buffer = malloc(size + 1);
napi_get_value_string_utf8(env, value, buffer, size + 1, &size);
*result = buffer;
return true;
}
static char* get_string_argument(napi_env env, napi_value* argv, size_t argi)
{
char* result;
if(!get_string_value(env, argv[argi], &result)) {
throw_argument_type_error(env, argv, argi, napi_string);
return NULL;
}
return result;
}
static bool striequals(const char* a, const char* b)
{
while(*a && *b) {
if(tolower(*a) != tolower(*b))
return false;
++a;
++b;
}
return (*a == '\0' && *b == '\0');
}
static bool string_option_func(napi_env env, napi_value property, const char* name, void* result)
{
if(get_string_value(env, property, result)) {
return true;
}
napi_valuetype type;
napi_typeof(env, property, &type);
char msg[128];
snprintf(msg, sizeof(msg), "Property `%s` must be string, not %s", name, type_name(type));
napi_throw_type_error(env, NULL, msg);
return false;
}
static bool integer_option_func(napi_env env, napi_value property, const char* name, void* result)
{
if(napi_get_value_int64(env, property, result) == napi_ok) {
return true;
}
napi_valuetype type;
napi_typeof(env, property, &type);
char msg[128];
snprintf(msg, sizeof(msg), "Property `%s` must be number, not %s", name, type_name(type));
napi_throw_type_error(env, NULL, msg);
return false;
}
static bool media_option_func(napi_env env, napi_value property, const char* name, void* result)
{
char* value;
if(!string_option_func(env, property, name, &value))
return false;
const struct {
const char* name;
plutobook_media_type_t value;
} table[] = {
{"print", PLUTOBOOK_MEDIA_TYPE_PRINT},
{"screen", PLUTOBOOK_MEDIA_TYPE_SCREEN},
{NULL}
};
for(int i = 0; table[i].name; ++i) {
if(striequals(table[i].name, value)) {
*(plutobook_media_type_t*)(result) = table[i].value;
free(value);
return true;
}
}
char msg[128];
snprintf(msg, sizeof(msg), "Property `%s` has invalid value \"%s\"", name, value);
napi_throw_type_error(env, NULL, msg);
free(value);
return false;
}
static bool size_option_func(napi_env env, napi_value property, const char* name, void* result)
{
char* value;
if(!string_option_func(env, property, name, &value))
return false;
const struct {
const char* name;
plutobook_page_size_t value;
} table[] = {
{"a3", PLUTOBOOK_PAGE_SIZE_A3},
{"a4", PLUTOBOOK_PAGE_SIZE_A4},
{"a5", PLUTOBOOK_PAGE_SIZE_A5},
{"b4", PLUTOBOOK_PAGE_SIZE_B4},
{"b5", PLUTOBOOK_PAGE_SIZE_B5},
{"letter", PLUTOBOOK_PAGE_SIZE_LETTER},
{"legal", PLUTOBOOK_PAGE_SIZE_LEGAL},
{"ledger", PLUTOBOOK_PAGE_SIZE_LEDGER},
{NULL}
};
for(int i = 0; table[i].name; ++i) {
if(striequals(table[i].name, value)) {
*(plutobook_page_size_t*)(result) = table[i].value;
free(value);
return true;
}
}
char msg[128];
snprintf(msg, sizeof(msg), "Property `%s` has invalid value \"%s\"", name, value);
napi_throw_type_error(env, NULL, msg);
free(value);
return false;
}
static bool length_option_func(napi_env env, napi_value property, const char* name, void* result)
{
if(napi_get_value_double(env, property, result) == napi_ok) {
return true;
}
char* value;
if(!get_string_value(env, property, &value)) {
napi_valuetype type;
napi_typeof(env, property, &type);
char msg[128];
snprintf(msg, sizeof(msg), "Property `%s` must be string or number, not %s", name, type_name(type));
napi_throw_type_error(env, NULL, msg);
return false;
}
char* end;
double length = strtod(value, &end);
const struct {
const char* name;
const double factor;
} table[] = {
{"pt", PLUTOBOOK_UNITS_PT},
{"pc", PLUTOBOOK_UNITS_PC},
{"in", PLUTOBOOK_UNITS_IN},
{"cm", PLUTOBOOK_UNITS_CM},
{"mm", PLUTOBOOK_UNITS_MM},
{"px", PLUTOBOOK_UNITS_PX},
{NULL}
};
for(int i = 0; table[i].name; ++i) {
if(striequals(table[i].name, end)) {
*(double*)(result) = length * table[i].factor;
free(value);
return true;
}
}
char msg[128];
snprintf(msg, sizeof(msg), "Property `%s` must be a valid length, not \"%s\"", name, value);
napi_throw_type_error(env, NULL, msg);
free(value);
return false;
}
static bool date_option_func(napi_env env, napi_value property, const char* name, void* result)
{
if(napi_get_date_value(env, property, result) == napi_ok) {
return true;
}
napi_valuetype type;
napi_typeof(env, property, &type);
char msg[128];
snprintf(msg, sizeof(msg), "Property `%s` must be date, not %s", name, type_name(type));
napi_throw_type_error(env, NULL, msg);
return false;
}
typedef bool (*option_func_t)(napi_env env, napi_value property, const char* name, void* result);
typedef struct {
const char* name;
option_func_t func;
void* result;
} option_t;
static bool parse_options(napi_env env, napi_value* argv, size_t argc, size_t argi, option_t* options)
{
napi_valuetype type;
napi_typeof(env, argv[argi], &type);
if(type != napi_object) {
throw_argument_type_error(env, argv, argi, napi_object);
return false;
}
for(int i = 0; options[i].name; ++i) {
bool has_property;
napi_has_named_property(env, argv[argi], options[i].name, &has_property);
if(has_property) {
napi_value property;
napi_get_named_property(env, argv[argi], options[i].name, &property);
if(!options[i].func(env, property, options[i].name, options[i].result)) {
return false;
}
}
}
return true;
}
static napi_value CreateBook(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1];
if(!get_callback_info(env, info, &argc, argv, NULL, 0, 1)) {
return NULL;
}
napi_ref BookClass_Ref;
napi_get_instance_data(env, (void**)&BookClass_Ref);
napi_value BookClass;
napi_get_reference_value(env, BookClass_Ref, &BookClass);
napi_value instance;
napi_new_instance(env, BookClass, argc, argv, &instance);
return instance;
}
static void BookClass_Finalize(napi_env env, void* data, void* hint)
{
plutobook_destroy(data);
}
static void set_date_metadata(plutobook_t* book, plutobook_pdf_metadata_t metadata, double date)
{
char value[sizeof("2025-12-11T05:36:01Z")];
time_t time = (time_t)(date / 1000);
strftime(value, sizeof(value), "%FT%TZ", gmtime(&time));
plutobook_set_metadata(book, metadata, value);
}
static napi_value BookClass_Constructor(napi_env env, napi_callback_info info)
{
napi_value new_target;
napi_get_new_target(env, info, &new_target);
if(new_target == NULL) {
return CreateBook(env, info);
}
size_t argc = 1;
napi_value argv[1];
napi_value thisArg;
if(!get_callback_info(env, info, &argc, argv, &thisArg, 0, 1)) {
return NULL;
}
plutobook_page_size_t size = PLUTOBOOK_PAGE_SIZE_A4;
plutobook_media_type_t media = PLUTOBOOK_MEDIA_TYPE_PRINT;
double width = -1;
double height = -1;
double margin = 72;
double marginTop = -1;
double marginRight = -1;
double marginBottom = -1;
double marginLeft = -1;
char* title = NULL;
char* subject = NULL;
char* author = NULL;
char* keywords = NULL;
char* creator = NULL;
double creationDate = -1;
double modificationDate = -1;
if(argc == 1) {
option_t options[] = {
{"size", size_option_func, &size},
{"media", media_option_func, &media},
{"width", length_option_func, &width},
{"height", length_option_func, &height},
{"margin", length_option_func, &margin},
{"marginTop", length_option_func, &marginTop},
{"marginRight", length_option_func, &marginRight},
{"marginBottom", length_option_func, &marginBottom},
{"marginLeft", length_option_func, &marginLeft},
{"title", string_option_func, &title},
{"subject", string_option_func, &subject},
{"author", string_option_func, &author},
{"keywords", string_option_func, &keywords},
{"creator", string_option_func, &creator},
{"creationDate", date_option_func, &creationDate},
{"modificationDate", date_option_func, &modificationDate},
{NULL}
};
if(!parse_options(env, argv, argc, 0, options)) {
thisArg = NULL;
goto cleanup;
}
}
if(width != -1)
size.width = width;
if(height != -1) {
size.height = height;
}
plutobook_page_margins_t margins = PLUTOBOOK_MAKE_PAGE_MARGINS(margin, margin, margin, margin);
if(marginTop != -1)
margins.top = marginTop;
if(marginRight != -1)
margins.right = marginRight;
if(marginBottom != -1)
margins.bottom = marginBottom;
if(marginLeft != -1) {
margins.left = marginLeft;
}
plutobook_t* book = plutobook_create(size, margins, media);
if(title)
plutobook_set_metadata(book, PLUTOBOOK_PDF_METADATA_TITLE, title);
if(subject)
plutobook_set_metadata(book, PLUTOBOOK_PDF_METADATA_SUBJECT, subject);
if(author)
plutobook_set_metadata(book, PLUTOBOOK_PDF_METADATA_AUTHOR, author);
if(keywords)
plutobook_set_metadata(book, PLUTOBOOK_PDF_METADATA_KEYWORDS, keywords);
if(creator) {
plutobook_set_metadata(book, PLUTOBOOK_PDF_METADATA_CREATOR, creator);
}
if(creationDate != -1)
set_date_metadata(book, PLUTOBOOK_PDF_METADATA_CREATION_DATE, creationDate);
if(modificationDate != -1) {
set_date_metadata(book, PLUTOBOOK_PDF_METADATA_MODIFICATION_DATE, modificationDate);
}
napi_wrap(env, thisArg, book, BookClass_Finalize, NULL, NULL);
cleanup:
free(title);
free(subject);
free(author);
free(keywords);
free(creator);
return thisArg;
}
static napi_value Book_PageCount(napi_env env, napi_callback_info info)
{
napi_value thisArg;
if(!get_callback_info(env, info, NULL, NULL, &thisArg, 0, 0)) {
return NULL;
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
napi_value result;
napi_create_uint32(env, plutobook_get_page_count(book), &result);
return result;
}
static napi_value Book_DocumentWidth(napi_env env, napi_callback_info info)
{
napi_value thisArg;
if(!get_callback_info(env, info, NULL, NULL, &thisArg, 0, 0)) {
return NULL;
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
napi_value result;
napi_create_double(env, plutobook_get_document_width(book), &result);
return result;
}
static napi_value Book_DocumentHeight(napi_env env, napi_callback_info info)
{
napi_value thisArg;
if(!get_callback_info(env, info, NULL, NULL, &thisArg, 0, 0)) {
return NULL;
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
napi_value result;
napi_create_double(env, plutobook_get_document_height(book), &result);
return result;
}
static napi_value Book_ViewportWidth(napi_env env, napi_callback_info info)
{
napi_value thisArg;
if(!get_callback_info(env, info, NULL, NULL, &thisArg, 0, 0)) {
return NULL;
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
napi_value result;
napi_create_double(env, plutobook_get_viewport_width(book), &result);
return result;
}
static napi_value Book_ViewportHeight(napi_env env, napi_callback_info info)
{
napi_value thisArg;
if(!get_callback_info(env, info, NULL, NULL, &thisArg, 0, 0)) {
return NULL;
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
napi_value result;
napi_create_double(env, plutobook_get_viewport_height(book), &result);
return result;
}
static napi_value Book_LoadUrl(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value argv[2];
napi_value thisArg;
if(!get_callback_info(env, info, &argc, argv, &thisArg, 1, 1)) {
return NULL;
}
char* url = get_string_argument(env, argv, 0);
if(url == NULL) {
return NULL;
}
char* userStyle = NULL;
char* userScript = NULL;
if(argc == 2) {
option_t options[] = {
{"userStyle", string_option_func, &userStyle},
{"userScript", string_option_func, &userScript},
{NULL}
};
if(!parse_options(env, argv, argc, 1, options)) {
thisArg = NULL;
goto cleanup;
}
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
const char* user_style = userStyle ? userStyle : "";
const char* user_script = userScript ? userScript : "";
if(!plutobook_load_url(book, url, user_style, user_script)) {
napi_throw_error(env, NULL, plutobook_get_error_message());
thisArg = NULL;
}
cleanup:
free(url);
free(userStyle);
free(userScript);
return thisArg;
}
static napi_value Book_LoadHtml(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value argv[2];
napi_value thisArg;
if(!get_callback_info(env, info, &argc, argv, &thisArg, 1, 1)) {
return NULL;
}
char* content = get_string_argument(env, argv, 0);
if(content == NULL) {
return NULL;
}
char* userStyle = NULL;
char* userScript = NULL;
char* baseUrl = NULL;
if(argc == 2) {
option_t options[] = {
{"userStyle", string_option_func, &userStyle},
{"userScript", string_option_func, &userScript},
{"baseUrl", string_option_func, &baseUrl},
{NULL}
};
if(!parse_options(env, argv, argc, 1, options)) {
thisArg = NULL;
goto cleanup;
}
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
const char* user_style = userStyle ? userStyle : "";
const char* user_script = userScript ? userScript : "";
const char* base_url = baseUrl ? baseUrl : "";
if(!plutobook_load_html(book, content, -1, user_style, user_script, base_url)) {
napi_throw_error(env, NULL, plutobook_get_error_message());
thisArg = NULL;
}
cleanup:
free(content);
free(userStyle);
free(userScript);
free(baseUrl);
return thisArg;
}
static napi_value Book_LoadXml(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value argv[2];
napi_value thisArg;
if(!get_callback_info(env, info, &argc, argv, &thisArg, 1, 1)) {
return NULL;
}
char* content = get_string_argument(env, argv, 0);
if(content == NULL) {
return NULL;
}
char* userStyle = NULL;
char* userScript = NULL;
char* baseUrl = NULL;
if(argc == 2) {
option_t options[] = {
{"userStyle", string_option_func, &userStyle},
{"userScript", string_option_func, &userScript},
{"baseUrl", string_option_func, &baseUrl},
{NULL}
};
if(!parse_options(env, argv, argc, 1, options)) {
thisArg = NULL;
goto cleanup;
}
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
const char* user_style = userStyle ? userStyle : "";
const char* user_script = userScript ? userScript : "";
const char* base_url = baseUrl ? baseUrl : "";
if(!plutobook_load_xml(book, content, -1, user_style, user_script, base_url)) {
napi_throw_error(env, NULL, plutobook_get_error_message());
thisArg = NULL;
}
cleanup:
free(content);
free(userStyle);
free(userScript);
free(baseUrl);
return thisArg;
}
static bool get_buffer_argument(napi_env env, napi_value* argv, size_t argi, void** buffer, size_t* length)
{
if(napi_get_buffer_info(env, argv[argi], buffer, length) == napi_ok) {
return true;
}
napi_valuetype type;
napi_typeof(env, argv[argi], &type);
char msg[128];
snprintf(msg, sizeof(msg), "Argument %zu must be buffer, not %s", argi + 1, type_name(type));
napi_throw_type_error(env, NULL, msg);
return false;
}
static napi_value Book_LoadData(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value argv[2];
napi_value thisArg;
if(!get_callback_info(env, info, &argc, argv, &thisArg, 1, 1)) {
return NULL;
}
void* buffer;
size_t length;
if(!get_buffer_argument(env, argv, 0, &buffer, &length)) {
return NULL;
}
char* mimeType = NULL;
char* textEncoding = NULL;
char* userStyle = NULL;
char* userScript = NULL;
char* baseUrl = NULL;
if(argc == 2) {
option_t options[] = {
{"mimeType", string_option_func, &mimeType},
{"textEncoding", string_option_func, &textEncoding},
{"userStyle", string_option_func, &userStyle},
{"userScript", string_option_func, &userScript},
{"baseUrl", string_option_func, &baseUrl},
{NULL}
};
if(!parse_options(env, argv, argc, 1, options)) {
thisArg = NULL;
goto cleanup;
}
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
const char* mime_type = mimeType ? mimeType : "";
const char* text_encoding = textEncoding ? textEncoding : "";
const char* user_style = userStyle ? userStyle : "";
const char* user_script = userScript ? userScript : "";
const char* base_url = baseUrl ? baseUrl : "";
if(!plutobook_load_data(book, buffer, length, mime_type, text_encoding, user_style, user_script, base_url)) {
napi_throw_error(env, NULL, plutobook_get_error_message());
thisArg = NULL;
}
cleanup:
free(mimeType);
free(textEncoding);
free(userStyle);
free(userScript);
free(baseUrl);
return thisArg;
}
static napi_value Book_LoadImage(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value argv[2];
napi_value thisArg;
if(!get_callback_info(env, info, &argc, argv, &thisArg, 1, 1)) {
return NULL;
}
void* buffer;
size_t length;
if(!get_buffer_argument(env, argv, 0, &buffer, &length)) {
return NULL;
}
char* mimeType = NULL;
char* textEncoding = NULL;
char* userStyle = NULL;
char* userScript = NULL;
char* baseUrl = NULL;
if(argc == 2) {
option_t options[] = {
{"mimeType", string_option_func, &mimeType},
{"textEncoding", string_option_func, &textEncoding},
{"userStyle", string_option_func, &userStyle},
{"userScript", string_option_func, &userScript},
{"baseUrl", string_option_func, &baseUrl},
{NULL}
};
if(!parse_options(env, argv, argc, 1, options)) {
thisArg = NULL;
goto cleanup;
}
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
const char* mime_type = mimeType ? mimeType : "";
const char* text_encoding = textEncoding ? textEncoding : "";
const char* user_style = userStyle ? userStyle : "";
const char* user_script = userScript ? userScript : "";
const char* base_url = baseUrl ? baseUrl : "";
if(!plutobook_load_image(book, buffer, length, mime_type, text_encoding, user_style, user_script, base_url)) {
napi_throw_error(env, NULL, plutobook_get_error_message());
thisArg = NULL;
}
cleanup:
free(mimeType);
free(textEncoding);
free(userStyle);
free(userScript);
free(baseUrl);
return thisArg;
}
static napi_value Book_WriteToPdf(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value argv[2];
napi_value thisArg;
if(!get_callback_info(env, info, &argc, argv, &thisArg, 1, 1)) {
return NULL;
}
char* path = get_string_argument(env, argv, 0);
if(path == NULL) {
return NULL;
}
napi_value result = NULL;
int64_t pageStart = PLUTOBOOK_MIN_PAGE_COUNT;
int64_t pageEnd = PLUTOBOOK_MAX_PAGE_COUNT;
int64_t pageStep = 1;
if(argc == 2) {
option_t options[] = {
{"pageStart", integer_option_func, &pageStart},
{"pageEnd", integer_option_func, &pageEnd},
{"pageStep", integer_option_func, &pageStep},
{NULL}
};
if(!parse_options(env, argv, argc, 1, options)) {
goto cleanup;
}
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
if(!plutobook_write_to_pdf_range(book, path, pageStart, pageEnd, pageStep)) {
napi_throw_error(env, NULL, plutobook_get_error_message());
goto cleanup;
}
napi_get_undefined(env, &result);
cleanup:
free(path);
return result;
}
typedef struct {
char* data;
size_t size;
size_t capacity;
} memory_stream_t;
static void memory_stream_init(memory_stream_t* stream)
{
stream->data = NULL;
stream->size = 0;
stream->capacity = 0;
}
static void memory_stream_destroy(memory_stream_t* stream)
{
free(stream->data);
}
static plutobook_stream_status_t stream_write_func(void* closure, const char* data, unsigned int length)
{
memory_stream_t* stream = closure;
size_t required_capacity = stream->size + length;
if(required_capacity > stream->capacity) {
size_t new_capacity = stream->capacity == 0 ? 128 : stream->capacity;
while(new_capacity < required_capacity) {
new_capacity *= 2;
}
char* new_data = realloc(stream->data, new_capacity);
if(new_data == NULL)
return PLUTOBOOK_STREAM_STATUS_WRITE_ERROR;
stream->data = new_data;
stream->capacity = new_capacity;
}
memcpy(stream->data + stream->size, data, length);
stream->size += length;
return PLUTOBOOK_STREAM_STATUS_SUCCESS;
}
static napi_value Book_WriteToPdfBuffer(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1];
napi_value thisArg;
if(!get_callback_info(env, info, &argc, argv, &thisArg, 0, 1)) {
return NULL;
}
napi_value result = NULL;
memory_stream_t stream;
memory_stream_init(&stream);
int64_t pageStart = PLUTOBOOK_MIN_PAGE_COUNT;
int64_t pageEnd = PLUTOBOOK_MAX_PAGE_COUNT;
int64_t pageStep = 1;
if(argc == 1) {
option_t options[] = {
{"pageStart", integer_option_func, &pageStart},
{"pageEnd", integer_option_func, &pageEnd},
{"pageStep", integer_option_func, &pageStep},
{NULL}
};
if(!parse_options(env, argv, argc, 0, options)) {
goto cleanup;
}
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
if(!plutobook_write_to_pdf_stream_range(book, stream_write_func, &stream, pageStart, pageEnd, pageStep)) {
napi_throw_error(env, NULL, plutobook_get_error_message());
goto cleanup;
}
napi_create_buffer_copy(env, stream.size, stream.data, NULL, &result);
cleanup:
memory_stream_destroy(&stream);
return result;
}
static napi_value Book_WriteToPng(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value argv[2];
napi_value thisArg;
if(!get_callback_info(env, info, &argc, argv, &thisArg, 1, 1)) {
return NULL;
}
char* path = get_string_argument(env, argv, 0);
if(path == NULL) {
return NULL;
}
napi_value result = NULL;
int64_t width = -1;
int64_t height = -1;
if(argc == 2) {
option_t options[] = {
{"width", integer_option_func, &width},
{"height", integer_option_func, &height},
{NULL}
};
if(!parse_options(env, argv, argc, 1, options)) {
goto cleanup;
}
}
plutobook_t* book;
napi_unwrap(env, thisArg, (void**)&book);
if(!plutobook_write_to_png(book, path, width, height)) {
napi_throw_error(env, NULL, plutobook_get_error_message());
goto cleanup;
}
napi_get_undefined(env, &result);
cleanup:
free(path);
return result;
}
static napi_value Book_WriteToPngBuffer(napi_env env, napi_callback_info info)
{
size_t argc = 1;
napi_value argv[1];
napi_value thisArg;
if(!get_callback_info(env, info, &argc, argv, &thisArg, 0, 1)) {
return NULL;
}
napi_value result = NULL;
memory_stream_t stream;
memory_stream_init(&stream);
int64_t width = -1;
int64_t height = -1;