Debian Codegraph @

texk/web2c/luatexdir/lua/lpdfelib.c

raw
1 /* loslibext.c
2
3 This file is part of LuaTeX.
4
5 LuaTeX is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 2 of the License, or (at your option) any later
8 version.
9
10 LuaTeX is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 details.
14
15 You should have received a copy of the GNU General Public License along with
16 LuaTeX; if not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20 /*tex
21
22 This file hosts the encapsulated \PDF\ support code used for inclusion and
23 access from \LUA.
24
25 */
26
27 # include "ptexlib.h"
28
29 /*tex
30
31 We need to avoid collision with some defines in |cpascal.h|.
32
33 */
34
35 # undef lpdfelib_orig_input
36 # undef lpdfelib_orig_output
37
38 # ifdef input
39 # define lpdfelib_orig_input input
40 # undef input
41 # endif
42
43 # ifdef output
44 # define lpdfelib_orig_output output
45 # undef output
46 # endif
47
48 # include "pplib.h"
49
50 # include "image/epdf.h"
51
52 # ifdef lpdfelib_orig_input
53 # define input lpdfelib_orig_input
54 # undef lpdfelib_orig_input
55 # endif
56
57 # ifdef lpdfelib_orig_output
58 # define output lpdfelib_orig_output
59 # undef lpdfelib_orig_output
60 # endif
61
62 # include "lua/luatex-api.h"
63
64 /*tex
65
66 We start with some housekeeping. Dictionaries, arrays, streams and references
67 get userdata, while strings, names, integers, floats and booleans become regular
68 \LUA\ objects. We need to define a few metatable identifiers too.
69
70 */
71
72 # define PDFE_METATABLE "luatex.pdfe"
73 # define PDFE_METATABLE_DICTIONARY "luatex.pdfe.dictionary"
74 # define PDFE_METATABLE_ARRAY "luatex.pdfe.array"
75 # define PDFE_METATABLE_STREAM "luatex.pdfe.stream"
76 # define PDFE_METATABLE_REFERENCE "luatex.pdfe.reference"
77
78 typedef struct {
79 ppdoc *document;
80 boolean open;
81 boolean isfile;
82 char *memstream;
83 int pages;
84 int index;
85 } pdfe_document ;
86
87 typedef struct {
88 ppdict *dictionary;
89 } pdfe_dictionary;
90
91 typedef struct {
92 pparray *array;
93 } pdfe_array;
94
95 typedef struct {
96 ppstream *stream;
97 int decode;
98 int open;
99 } pdfe_stream;
100
101 typedef struct {
102 /* ppref *reference; */
103 ppxref *xref;
104 int onum;
105 } pdfe_reference;
106
107 /*tex
108
109 We need to check if we have the right userdata. A similar warning is issued
110 when encounter a problem. We don't exit.
111
112 */
113
114 static void pdfe_invalid_object_warning(const char * detail)
115 {
116 formatted_warning("pdfe lib","lua <pdfe %s> expected",detail);
117 }
118
119 static pdfe_document *check_isdocument(lua_State * L, int n)
120 {
121 pdfe_document *p = (pdfe_document *)lua_touserdata(L, n);
122 if (p != NULL && lua_getmetatable(L, n)) {
123 lua_get_metatablelua(luatex_pdfe);
124 if (!lua_rawequal(L, -1, -2)) {
125 p = NULL;
126 }
127 lua_pop(L, 2);
128 if (p != NULL) {
129 return p;
130 }
131 }
132 pdfe_invalid_object_warning("document");
133 return NULL;
134 }
135
136 static pdfe_dictionary *check_isdictionary(lua_State * L, int n)
137 {
138 pdfe_dictionary *p = (pdfe_dictionary *)lua_touserdata(L, n);
139 if (p != NULL && lua_getmetatable(L, n)) {
140 lua_get_metatablelua(luatex_pdfe_dictionary);
141 if (!lua_rawequal(L, -1, -2)) {
142 p = NULL;
143 }
144 lua_pop(L, 2);
145 if (p != NULL) {
146 return p;
147 }
148 }
149 pdfe_invalid_object_warning("dictionary");
150 return NULL;
151 }
152
153 static pdfe_array *check_isarray(lua_State * L, int n)
154 {
155 pdfe_array *p = (pdfe_array *)lua_touserdata(L, n);
156 if (p != NULL && lua_getmetatable(L, n)) {
157 lua_get_metatablelua(luatex_pdfe_array);
158 if (!lua_rawequal(L, -1, -2)) {
159 p = NULL;
160 }
161 lua_pop(L, 2);
162 if (p != NULL) {
163 return p;
164 }
165 }
166 pdfe_invalid_object_warning("array");
167 return NULL;
168 }
169
170 static pdfe_stream *check_isstream(lua_State * L, int n)
171 {
172 pdfe_stream *p = (pdfe_stream *)lua_touserdata(L, n);
173 if (p != NULL && lua_getmetatable(L, n)) {
174 lua_get_metatablelua(luatex_pdfe_stream);
175 if (!lua_rawequal(L, -1, -2)) {
176 p = NULL;
177 }
178 lua_pop(L, 2);
179 if (p != NULL) {
180 return p;
181 }
182 }
183 pdfe_invalid_object_warning("stream");
184 return NULL;
185 }
186
187 static pdfe_reference *check_isreference(lua_State * L, int n)
188 {
189 pdfe_reference *p = (pdfe_reference *)lua_touserdata(L, n);
190 if (p != NULL && lua_getmetatable(L, n)) {
191 lua_get_metatablelua(luatex_pdfe_reference);
192 if (!lua_rawequal(L, -1, -2)) {
193 p = NULL;
194 }
195 lua_pop(L, 2);
196 if (p != NULL) {
197 return p;
198 }
199 }
200 pdfe_invalid_object_warning("reference");
201 return NULL;
202 }
203
204 /*tex
205
206 Reporting the type of a userdata is just a sequence of tests till we find the
207 right one. We return nothing is it is no pdfe type.
208
209 \starttyping
210 t = pdfe.type(<pdfe document|dictionary|array|reference|stream>)
211 \stoptyping
212
213 */
214
215 # define check_type(field,meta,name) do { \
216 lua_get_metatablelua(luatex_##meta); \
217 if (lua_rawequal(L, -1, -2)) { \
218 lua_pushstring(L,name); \
219 return 1; \
220 } \
221 lua_pop(L, 1); \
222 } while (0)
223
224 static int pdfelib_type(lua_State * L)
225 {
226 void *p = lua_touserdata(L, 1);
227 if (p != NULL && lua_getmetatable(L, 1)) {
228 check_type(document, pdfe, "pdfe");
229 check_type(dictionary,pdfe_dictionary,"pdfe.dictionary");
230 check_type(array, pdfe_array, "pdfe.array");
231 check_type(reference, pdfe_reference, "pdfe.reference");
232 check_type(stream, pdfe_stream, "pdfe.stream");
233 }
234 return 0;
235 }
236
237 /*tex
238
239 The \type {tostring} metamethods are similar and report a pdfe type plus a
240 pointer value, as is rather usual in \LUA.
241
242 */
243
244 # define define_to_string(field,what) \
245 static int pdfelib_tostring_##field(lua_State * L) { \
246 pdfe_##field *p = check_is##field(L, 1); \
247 if (p != NULL) { \
248 lua_pushfstring(L, "<" what " %p>", (ppdoc *) p->field); \
249 return 1; \
250 } \
251 return 0; \
252 }
253
254 define_to_string(document, "pdfe")
255 define_to_string(dictionary,"pdfe.dictionary")
256 define_to_string(array, "pdfe.array")
257 define_to_string(stream, "pdfe.stream")
258
259 static int pdfelib_tostring_reference(lua_State * L) { \
260 pdfe_reference *p = check_isreference(L, 1); \
261 if (p != NULL) { \
262 lua_pushfstring(L, "<pdfe.reference " "%d>", p->onum); \
263 return 1; \
264 } \
265 return 0; \
266 }
267
268 /*tex
269
270 The pushers look rather similar. We have two variants, one that just pushes
271 the object, and another that also pushes some extra information.
272
273 */
274
275 # define pdfe_push_dictionary do { \
276 pdfe_dictionary *d = (pdfe_dictionary *)lua_newuserdata(L, sizeof(pdfe_dictionary)); \
277 luaL_getmetatable(L, PDFE_METATABLE_DICTIONARY); \
278 lua_setmetatable(L, -2); \
279 d->dictionary = dictionary; \
280 } while(0)
281
282 static int pushdictionary(lua_State * L, ppdict *dictionary)
283 {
284 if (dictionary != NULL) {
285 pdfe_push_dictionary;
286 lua_pushinteger(L,dictionary->size);
287 return 2;
288 }
289 return 0;
290 }
291
292 static int pushdictionaryonly(lua_State * L, ppdict *dictionary)
293 {
294 if (dictionary != NULL) {
295 pdfe_push_dictionary;
296 return 1;
297 }
298 return 0;
299 }
300
301 # define pdfe_push_array do { \
302 pdfe_array *a = (pdfe_array *)lua_newuserdata(L, sizeof(pdfe_array)); \
303 luaL_getmetatable(L, PDFE_METATABLE_ARRAY); \
304 lua_setmetatable(L, -2); \
305 a->array = array; \
306 } while (0)
307
308 static int pusharray(lua_State * L, pparray * array)
309 {
310 if (array != NULL) {
311 pdfe_push_array;
312 lua_pushinteger(L,array->size);
313 return 2;
314 }
315 return 0;
316 }
317
318 static int pusharrayonly(lua_State * L, pparray * array)
319 {
320 if (array != NULL) {
321 pdfe_push_array;
322 return 1;
323 }
324 return 0;
325 }
326
327 # define pdfe_push_stream do { \
328 pdfe_stream *s = (pdfe_stream *)lua_newuserdata(L, sizeof(pdfe_stream)); \
329 luaL_getmetatable(L, PDFE_METATABLE_STREAM); \
330 lua_setmetatable(L, -2); \
331 s->stream = stream; \
332 s->open = 0; \
333 s->decode = 0; \
334 } while(0)
335
336 static int pushstream(lua_State * L, ppstream * stream)
337 {
338 if (stream != NULL) {
339 pdfe_push_stream;
340 if (pushdictionary(L, stream->dict) > 0)
341 return 3;
342 else
343 return 1;
344 }
345 return 0;
346 }
347
348 static int pushstreamonly(lua_State * L, ppstream * stream)
349 {
350 if (stream != NULL) {
351 pdfe_push_stream;
352 if (pushdictionaryonly(L, stream->dict) > 0)
353 return 2;
354 else
355 return 1;
356 }
357 return 0;
358 }
359
360 # define pdfe_push_reference do { \
361 pdfe_reference *r = (pdfe_reference *)lua_newuserdata(L, sizeof(pdfe_reference)); \
362 luaL_getmetatable(L, PDFE_METATABLE_REFERENCE); \
363 lua_setmetatable(L, -2); \
364 r->xref = reference->xref; \
365 r->onum = reference->number; \
366 } while (0)
367
368 static int pushreference(lua_State * L, ppref * reference)
369 {
370 if (reference != NULL && reference->number != 0) {
371 pdfe_push_reference;
372 lua_pushinteger(L,reference->number);
373 return 2;
374 }
375 return 0;
376 }
377
378 /*tex
379
380 The next function checks for the type and then pushes the matching data on
381 the stack.
382
383 \starttabulate[|c|l|l|l|]
384 \BC type \BC meaning \BC value \BC detail \NC \NR
385 \NC \type {0} \NC none \NC nil \NC \NC \NR
386 \NC \type {1} \NC null \NC nil \NC \NC \NR
387 \NC \type {2} \NC boolean \NC boolean \NC \NC \NR
388 \NC \type {3} \NC boolean \NC integer \NC \NC \NR
389 \NC \type {4} \NC number \NC float \NC \NC \NR
390 \NC \type {5} \NC name \NC string \NC \NC \NR
391 \NC \type {6} \NC string \NC string \NC type \NC \NR
392 \NC \type {7} \NC array \NC arrayobject \NC size \NC \NR
393 \NC \type {8} \NC dictionary \NC dictionaryobject \NC size \NC \NR
394 \NC \type {9} \NC stream \NC streamobject \NC dictionary size \NC \NR
395 \NC \type {10} \NC reference \NC integer \NC \NC \NR
396 \LL
397 \stoptabulate
398
399 A name and string can be distinguished by the extra type value that a string
400 has.
401
402 */
403
404 static int pushvalue(lua_State * L, ppobj *object)
405 {
406 switch (object->type) {
407 case PPNONE:
408 case PPNULL:
409 lua_pushnil(L);
410 return 1;
411 break;
412 case PPBOOL:
413 lua_pushboolean(L,object->integer);
414 return 1;
415 break;
416 case PPINT:
417 lua_pushinteger(L, object-> integer);
418 return 1;
419 break;
420 case PPNUM:
421 lua_pushnumber(L, object->number);
422 return 1;
423 break;
424 case PPNAME:
425 {
426 ppname * n = ppname_decoded(object->name) ;
427 lua_pushlstring(L, ppname_data(n), ppname_size(n));
428 return 1;
429 }
430 break;
431 case PPSTRING:
432 lua_pushlstring(L, ppstring_data(object->string), ppstring_size(object->string));
433 lua_pushboolean(L, ppstring_hex(object->string));
434 return 2;
435 break;
436 case PPARRAY:
437 return pusharray(L, object->array);
438 break;
439 case PPDICT:
440 return pushdictionary(L, object->dict);
441 break;
442 case PPSTREAM:
443 return pushstream(L, object->stream);
444 break;
445 case PPREF:
446 return pushreference(L, object->ref);
447 break;
448 }
449 return 0;
450 }
451
452 /*tex
453
454 We need to start someplace when we traverse a document's tree. There are
455 three places:
456
457 \starttyping
458 catalogdictionary = getcatalog(documentobject)
459 trailerdictionary = gettrailer(documentobject)
460 infodictionary = getinfo (documentobject)
461 \stoptyping
462
463 */
464
465 static int pdfelib_getcatalog(lua_State * L)
466 {
467 pdfe_document *p = check_isdocument(L, 1);
468 if (p == NULL)
469 return 0;
470 return pushdictionaryonly(L,ppdoc_catalog(p->document));
471 }
472
473 static int pdfelib_gettrailer(lua_State * L)
474 {
475 pdfe_document *p = check_isdocument(L, 1);
476 if (p == NULL)
477 return 0;
478 return pushdictionaryonly(L,ppdoc_trailer(p->document));
479 }
480
481 static int pdfelib_getinfo(lua_State * L)
482 {
483 pdfe_document *p = check_isdocument(L, 1);
484 if (p == NULL)
485 return 0;
486 return pushdictionaryonly(L,ppdoc_info(p->document));
487 }
488
489 /*tex
490
491 We have three more helpers.
492
493 \starttyping
494 [key,] type, value, detail = getfromdictionary(dictionaryobject,name|index)
495 type, value, detail = getfromarray (arrayobject,index)
496 [key,] type, value, detail = getfromstream (streamobject,name|index)
497 \stoptyping
498
499 */
500
501 static int pdfelib_getfromarray(lua_State * L)
502 {
503 pdfe_array *a = check_isarray(L, 1);
504 if (a != NULL) {
505 int index = luaL_checkint(L, 2) - 1;
506 if (index < a->array->size) {
507 ppobj *object = pparray_at(a->array,index);
508 if (object != NULL) {
509 lua_pushinteger(L,(int) object->type);
510 return 1 + pushvalue(L,object);
511 }
512 }
513 }
514 return 0;
515 }
516
517 static int pdfelib_getfromdictionary(lua_State * L)
518 {
519 pdfe_dictionary *d = check_isdictionary(L, 1);
520 if (d != NULL) {
521 if (lua_type(L,2) == LUA_TSTRING) {
522 const char *name = luaL_checkstring(L, 2);
523 ppobj *object = ppdict_get_obj(d->dictionary,name);
524 if (object != NULL) {
525 lua_pushinteger(L,(int) object->type);
526 return 1 + pushvalue(L,object);
527 }
528 } else {
529 int index = luaL_checkint(L, 2) - 1;
530 if (index < d->dictionary->size) {
531 ppobj *object = ppdict_at(d->dictionary,index);
532 if (object != NULL) {
533 ppname * key = ppname_decoded(ppdict_key(d->dictionary,index));
534 lua_pushlstring(L, ppname_data(key), ppname_size(key));
535 lua_pushinteger(L,(int) object->type);
536 return 2 + pushvalue(L,object);
537 }
538 }
539 }
540 }
541 return 0;
542 }
543
544 static int pdfelib_getfromstream(lua_State * L)
545 {
546 pdfe_stream *s = (pdfe_stream *)lua_touserdata(L, 1);
547 if (s != NULL) {
548 ppdict *d = s->stream->dict;
549 if (lua_type(L,2) == LUA_TSTRING) {
550 const char *name = luaL_checkstring(L, 2);
551 ppobj *object = ppdict_get_obj(d,name);
552 if (object != NULL) {
553 lua_pushinteger(L,(int) object->type);
554 return 1 + pushvalue(L,object);
555 }
556 } else {
557 int index = luaL_checkint(L, 2) - 1;
558 if (index < d->size) {
559 ppobj *object = ppdict_at(d,index);
560 if (object != NULL) {
561 ppname * key = ppname_decoded(ppdict_key(d,index));
562 lua_pushlstring(L, ppname_data(key), ppname_size(key));
563 lua_pushinteger(L,(int) object->type);
564 return 2 + pushvalue(L,object);
565 }
566 }
567 }
568 }
569 return 0;
570 }
571
572 /*tex
573
574 An indexed table with all entries in an array can be fetched with::
575
576 \starttyping
577 t = arraytotable(arrayobject)
578 \stoptyping
579
580 An hashed table with all entries in an dictionary can be fetched with::
581
582 \starttyping
583 t = dictionarytotable(arrayobject)
584 \stoptyping
585
586 */
587
588 static void pdfelib_totable(lua_State * L, ppobj * object, int flat)
589 {
590 int n = pushvalue(L,object);
591 if (flat && n < 2) {
592 return;
593 }
594 /* [value] [extra] [more] */
595 lua_createtable(L,n+1,0);
596 if (n == 1) {
597 /* value { nil, nil } */
598 lua_insert(L,-2);
599 /* { nil, nil } value */
600 lua_rawseti(L,-2,2);
601 /* { nil , value } */
602 } else if (n == 2) {
603 /* value extra { nil, nil, nil } */
604 lua_insert(L,-3);
605 /* { nil, nil, nil } value extra */
606 lua_rawseti(L,-3,3);
607 /* { nil, nil, extra } value */
608 lua_rawseti(L,-2,2);
609 /* { nil, value, extra } */
610 } else if (n == 3) {
611 /* value extra more { nil, nil, nil, nil } */
612 lua_insert(L,-4);
613 /* { nil, nil, nil, nil, nil } value extra more */
614 lua_rawseti(L,-4,4);
615 /* { nil, nil, nil, more } value extra */
616 lua_rawseti(L,-3,3);
617 /* { nil, nil, extra, more } value */
618 lua_rawseti(L,-2,2);
619 /* { nil, value, extra, more } */
620 }
621 lua_pushinteger(L,(int) object->type);
622 /* { nil, [value], [extra], [more] } type */
623 lua_rawseti(L,-2,1);
624 /* { type, [value], [extra], [more] } */
625 return;
626 }
627
628 static int pdfelib_arraytotable(lua_State * L)
629 {
630 pdfe_array *a = check_isarray(L, 1);
631 if (a != NULL) {
632 int flat = lua_isboolean(L,2);
633 int i = 0;
634 int j = 0;
635 lua_createtable(L,i,0);
636 /* table */
637 for (i=0;i<a->array->size;i++) {
638 ppobj *object = pparray_at(a->array,i);
639 if (object != NULL) {
640 pdfelib_totable(L,object,flat);
641 /* table { type, [value], [extra], [more] } */
642 lua_rawseti(L,-2,++j);
643 /* table[i] = { type, [value], [extra], [more] } */
644 }
645 }
646 return 1;
647 }
648 return 0;
649 }
650
651 static int pdfelib_dictionarytotable(lua_State * L)
652 {
653 pdfe_dictionary *d = check_isdictionary(L, 1);
654 if (d != NULL) {
655 int flat = lua_isboolean(L,2);
656 int i = 0;
657 lua_createtable(L,0,i);
658 /* table */
659 for (i=0;i<d->dictionary->size;i++) {
660 ppobj *object = ppdict_at(d->dictionary,i);
661 if (object != NULL) {
662 ppname * key = ppname_decoded(ppdict_key(d->dictionary,i));
663 lua_pushlstring(L, ppname_data(key), ppname_size(key));
664 /* table key */
665 pdfelib_totable(L,object,flat);
666 /* table key { type, [value], [extra], [more] } */
667 lua_rawset(L,-3);
668 /* table[key] = { type, [value], [extra] } */
669 }
670 }
671 return 1;
672 }
673 return 0;
674 }
675
676 /*tex
677
678 All pages are collected with:
679
680 \starttyping
681 { { dict, size, objnum }, ... } = pagestotable(document)
682 \stoptyping
683
684 */
685
686 static int pdfelib_pagestotable(lua_State * L)
687 {
688 pdfe_document *p = check_isdocument(L, 1);
689 if (p != NULL) {
690 ppdoc *d = p->document;
691 ppref *r = NULL;
692 int i = 0;
693 int j = 0;
694 lua_createtable(L,ppdoc_page_count(d),0);
695 /* pages[1..n] */
696 for (r = ppdoc_first_page(d), i = 1; r != NULL; r = ppdoc_next_page(d), ++i) {
697 lua_createtable(L,3,0);
698 if (ppref_obj(r) != NULL) {
699 pushdictionary(L,ppref_obj(r)->dict);
700 /* table dictionary n */
701 lua_rawseti(L,-3,2);
702 /* table dictionary */
703 lua_rawseti(L,-2,1);
704 /* table */
705 lua_pushinteger(L,r->number);
706 /* table reference */
707 lua_rawseti(L,-2,3);
708 /* table */
709 lua_rawseti(L,-2,++j);
710 /* pages[i] = { dictionary, size, objnum } */
711 }
712 }
713 return 1;
714 }
715 return 0;
716 }
717
718 /*tex
719
720 Streams can be fetched on one go:
721
722 \starttyping
723 string, n = readwholestream(streamobject,decode)
724 \stoptyping
725
726 */
727
728 static int pdfelib_readwholestream(lua_State * L)
729 {
730 pdfe_stream *s = check_isstream(L, 1);
731 if (s != NULL) {
732 uint8_t *b = NULL;
733 int decode = 0;
734 size_t n = 0;
735 if (s->open > 0) {
736 ppstream_done(s->stream);
737 s->open = 0;
738 s->decode = 0;
739 }
740 if (lua_gettop(L) > 1 && lua_isboolean(L, 2)) {
741 decode = lua_toboolean(L, 2);
742 }
743 b = ppstream_all(s->stream,&n,decode);
744 lua_pushlstring(L, (const char *) b, n);
745 lua_pushinteger(L, (int) n);
746 ppstream_done(s->stream);
747 return 2;
748 }
749 return 0;
750 }
751
752 /*tex
753
754 Alternatively streams can be fetched stepwise:
755
756 \starttyping
757 okay = openstream(streamobject,[decode])
758 string, n = readfromstream(streamobject)
759 closestream(streamobject)
760 \stoptyping
761
762 */
763
764 static int pdfelib_openstream(lua_State * L)
765 {
766 pdfe_stream *s = check_isstream(L, 1);
767 if (s != NULL) {
768 if (s->open == 0) {
769 if (lua_gettop(L) > 1) {
770 s->decode = lua_isboolean(L, 2);
771 }
772 s->open = 1;
773 }
774 lua_pushboolean(L,1);
775 return 1;
776 }
777 return 0;
778 }
779
780 static int pdfelib_closestream(lua_State * L)
781 {
782 pdfe_stream *s = check_isstream(L, 1);
783 if (s != NULL) {
784 if (s->open >0) {
785 ppstream_done(s->stream);
786 s->open = 0;
787 s->decode = 0;
788 }
789 }
790 return 0;
791 }
792
793 static int pdfelib_readfromstream(lua_State * L)
794 {
795 pdfe_stream *s = check_isstream(L, 1);
796 if (s != NULL) {
797 size_t n = 0;
798 uint8_t *d = NULL;
799 if (s->open == 1) {
800 d = ppstream_first(s->stream,&n,s->decode);
801 s->open = 2;
802 } else if (s->open == 2) {
803 d = ppstream_next(s->stream,&n);
804 } else {
805 return 0;
806 }
807 lua_pushlstring(L, (const char *) d, n);
808 lua_pushinteger(L, (int) n);
809 return 2;
810 }
811 return 0;
812 }
813
814 /*tex
815
816 There are two methods for opening a document: files and strings.
817
818 \starttyping
819 documentobject = open(filename)
820 documentobject = new(string,length)
821 \stoptyping
822
823 Closing happens with:
824
825 \starttyping
826 close(documentobject)
827 \stoptyping
828
829 When the \type {new} function gets a peudo filename as third argument,
830 no user data will be created but the stream is accessible as image.
831
832 */
833
834 static int pdfelib_open(lua_State * L)
835 {
836 const char *filename = luaL_checkstring(L, 1);
837 ppdoc *d = ppdoc_load(filename);
838 if (d == NULL) {
839 formatted_warning("pdfe lib","no valid pdf file '%s'",filename);
840 } else {
841 pdfe_document *p = (pdfe_document *) lua_newuserdata(L, sizeof(pdfe_document));
842 luaL_getmetatable(L, PDFE_METATABLE);
843 lua_setmetatable(L, -2);
844 p->document = d;
845 p->open = true;
846 p->isfile = true;
847 p->memstream = NULL;
848 return 1;
849 }
850 return 0;
851 }
852
853 static int pdfelib_new(lua_State * L)
854 {
855 const char *docstream = NULL;
856 char *memstream = NULL ;
857 unsigned long long streamsize;
858 switch (lua_type(L, 1)) {
859 case LUA_TSTRING:
860 /* stream as Lua string */
861 docstream = luaL_checkstring(L, 1);
862 break;
863 case LUA_TLIGHTUSERDATA:
864 /* stream as sequence of bytes */
865 docstream = (const char *) lua_touserdata(L, 1);
866 break;
867 default:
868 luaL_error(L, "bad <pdfe> argument: string or lightuserdata expected");
869 break;
870 }
871 if (docstream == NULL) {
872 luaL_error(L, "bad <pdfe> document");
873 }
874 /* size of the stream */
875 streamsize = (unsigned long long) luaL_checkint(L, 2);
876 memstream = xmalloc((unsigned) (streamsize + 1));
877 if (! memstream) {
878 luaL_error(L, "no room for <pdfe> stream");
879 }
880 memcpy(memstream, docstream, streamsize);
881 memstream[streamsize]='\0';
882 if (lua_gettop(L) == 2) {
883 /* we stay at the lua end */
884 ppdoc *d = ppdoc_mem(memstream, streamsize);
885 if (d == NULL) {
886 normal_warning("pdfe lib","no valid pdf mem stream");
887 } else {
888 pdfe_document *p = (pdfe_document *) lua_newuserdata(L, sizeof(pdfe_document));
889 luaL_getmetatable(L, PDFE_METATABLE);
890 lua_setmetatable(L, -2);
891 p->document = d;
892 p->open = true;
893 p->isfile = false;
894 p->memstream = memstream;
895 return 1;
896 }
897 } else {
898 /* pseudo file name */
899 PdfDocument *pdf_doc;
900 const char *file_id = luaL_checkstring(L, 3);
901 if (file_id == NULL) {
902 luaL_error(L, "<pdfe> stream has an invalid id");
903 }
904 if (strlen(file_id) > STREAM_FILE_ID_LEN ) {
905 /* a limit to the length of the string */
906 luaL_error(L, "<pdfe> stream has a too long id");
907 }
908 pdf_doc = refMemStreamPdfDocument(memstream, streamsize, file_id);
909 if (pdf_doc != NULL) {
910 lua_pushstring(L,pdf_doc->file_path);
911 return 1;
912 } else {
913 /* pplib does this: xfree(memstream); */
914 }
915 }
916 return 0;
917 }
918
919 /*
920
921 There is no garbage collection needed as the library itself manages the
922 objects. Normally objects don't take much space. Streams use buffers so (I
923 assume) that they are not persistent. The only collector is in the parent
924 object (the document).
925
926 */
927
928 static int pdfelib_free(lua_State * L)
929 {
930 pdfe_document *p = check_isdocument(L, 1);
931 if (p != NULL && p->open) {
932 if (p->document != NULL) {
933 ppdoc_free(p->document);
934 p->document = NULL;
935 }
936 if (p->memstream != NULL) {
937 /* pplib does this: xfree(p->memstream); */
938 p->memstream = NULL;
939 }
940 p->open = false;
941 }
942 return 0;
943 }
944
945 static int pdfelib_close(lua_State * L)
946 {
947 return pdfelib_free(L);
948 }
949
950 /*tex
951
952 A document is can be uncrypted with:
953
954 \starttyping
955 status = unencrypt(documentobject,user,owner)
956 \stoptyping
957
958 Instead of a password \type {nil} can be passed, so there are three possible
959 useful combinations.
960
961 */
962
963 static int pdfelib_unencrypt(lua_State * L)
964 {
965 pdfe_document *p = check_isdocument(L, 1);
966 if (p != NULL) {
967 size_t u = 0;
968 size_t o = 0;
969 const char* user = NULL;
970 const char* owner = NULL;
971 int top = lua_gettop(L);
972 if (top > 1) {
973 if (lua_type(L,2) == LUA_TSTRING) {
974 user = lua_tolstring(L, 2, &u);
975 } else {
976 /*tex we're not too picky but normally it will be nil or false */
977 }
978 if (top > 2) {
979 if (lua_type(L,3) == LUA_TSTRING) {
980 owner = lua_tolstring(L, 3, &o);
981 } else {
982 /*tex we're not too picky but normally it will be nil or false */
983 }
984 }
985 lua_pushinteger(L, (int) ppdoc_crypt_pass(p->document,user,u,owner,o));
986 return 1;
987 }
988 }
989 lua_pushinteger(L, (int) PPCRYPT_FAIL);
990 return 1;
991 }
992
993 /*tex
994
995 There are a couple of ways to get information about the document:
996
997 \starttyping
998 n = getsize (documentobject)
999 major, minor = getversion (documentobject)
1000 status = getstatus (documentobject)
1001 n = getnofobjects (documentobject)
1002 n = getnofpages (documentobject)
1003 bytes, waste = getmemoryusage(documentobject)
1004 \stoptyping
1005
1006 */
1007
1008 static int pdfelib_getsize(lua_State * L)
1009 {
1010 pdfe_document *p = check_isdocument(L, 1);
1011 if (p == NULL)
1012 return 0;
1013 lua_pushinteger(L,(int) ppdoc_file_size(p->document));
1014 return 1;
1015 }
1016
1017 static int pdfelib_getversion(lua_State * L)
1018 {
1019 pdfe_document *p = check_isdocument(L, 1);
1020 if (p == NULL) {
1021 return 0;
1022 } else {
1023 int minor;
1024 int major = ppdoc_version_number(p->document, &minor);
1025 lua_pushinteger(L,(int) major);
1026 lua_pushinteger(L,(int) minor);
1027 return 2;
1028 }
1029 }
1030
1031 static int pdfelib_getstatus(lua_State * L)
1032 {
1033 pdfe_document *p = check_isdocument(L, 1);
1034 if (p == NULL)
1035 return 0;
1036 lua_pushinteger(L,(int) ppdoc_crypt_status(p->document));
1037 return 1;
1038 }
1039
1040 static int pdfelib_getnofobjects(lua_State * L)
1041 {
1042 pdfe_document *p = check_isdocument(L, 1);
1043 if (p == NULL)
1044 return 0;
1045 lua_pushinteger(L,(int) ppdoc_objects(p->document));
1046 return 1;
1047 }
1048
1049 static int pdfelib_getnofpages(lua_State * L)
1050 {
1051 pdfe_document *p = check_isdocument(L, 1);
1052 if (p == NULL)
1053 return 0;
1054 lua_pushinteger(L,(int) ppdoc_page_count(p->document));
1055 return 1;
1056 }
1057
1058 static int pdfelib_getmemoryusage(lua_State * L)
1059 {
1060 pdfe_document *p = check_isdocument(L, 1);
1061 if (p != NULL) {
1062 size_t w = 0;
1063 size_t m = ppdoc_memory(p->document,&w);
1064 lua_pushinteger(L,(int) m);
1065 lua_pushinteger(L,(int) w);
1066 return 2;
1067 }
1068 return 0;
1069 }
1070
1071 /*
1072 A specific page dictionary can be filtered with the next command. So, there
1073 is no need to parse the document page tree (with these \type {kids} arrays).
1074
1075 \starttyping
1076 dictionaryobject = getpage(documentobject,pagenumber)
1077 \stoptyping
1078
1079 */
1080
1081 static int pushpage(lua_State * L, ppdoc * d, int page)
1082 {
1083 if (page <= 0 || page > ppdoc_page_count(d)) {
1084 return 0;
1085 } else {
1086 ppref *pp = ppdoc_page(d,page);
1087 return pushdictionaryonly(L, ppref_obj(pp)->dict);
1088 }
1089 }
1090
1091 static int pdfelib_getpage(lua_State * L)
1092 {
1093 pdfe_document *p = check_isdocument(L, 1);
1094 if (p == NULL) {
1095 return 0;
1096 } else {
1097 return pushpage(L, p->document, luaL_checkint(L, 2));
1098 }
1099 }
1100
1101 static int pushpages(lua_State * L, ppdoc * d)
1102 {
1103 int i = 0;
1104 ppref *r;
1105 lua_createtable(L,ppdoc_page_count(d),0);
1106 /* pages[1..n] */
1107 for (r = ppdoc_first_page(d), i = 1; r != NULL; r = ppdoc_next_page(d), ++i) {
1108 pushdictionaryonly(L,ppref_obj(r)->dict);
1109 lua_rawseti(L,-2,i);
1110 }
1111 return 1 ;
1112 }
1113
1114 static int pdfelib_getpages(lua_State * L)
1115 {
1116 pdfe_document *p = check_isdocument(L, 1);
1117 if (p == NULL) {
1118 return 0;
1119 } else {
1120 return pushpages(L, p->document);
1121 }
1122 }
1123
1124 /*tex
1125
1126 The boundingbox (\type {MediaBox) and similar boxes can be available in a
1127 (page) doctionary but also in a parent object. Therefore a helper is
1128 available that does the (backtracked) lookup.
1129
1130 \starttyping
1131 { lx, ly, rx, ry } = getbox(dictionaryobject)
1132 \stoptyping
1133
1134 */
1135
1136 static int pdfelib_getbox(lua_State * L)
1137 {
1138 if (lua_gettop(L) > 1 && lua_type(L,2) == LUA_TSTRING) {
1139 pdfe_dictionary *p = check_isdictionary(L, 1);
1140 if (p != NULL) {
1141 const char *key = lua_tostring(L,2);
1142 pprect box;
1143 pprect *r;
1144 box.lx = box.rx = box.ly = box.ry = 0;
1145 r = ppdict_get_box(p->dictionary,key,&box);
1146 if (r != NULL) {
1147 lua_createtable(L,4,0);
1148 lua_pushnumber(L,r->lx);
1149 lua_rawseti(L,-2,1);
1150 lua_pushnumber(L,r->ly);
1151 lua_rawseti(L,-2,2);
1152 lua_pushnumber(L,r->rx);
1153 lua_rawseti(L,-2,3);
1154 lua_pushnumber(L,r->ry);
1155 lua_rawseti(L,-2,4);
1156 return 1;
1157 }
1158 }
1159 }
1160 return 0;
1161 }
1162
1163 /*tex
1164
1165 This one is needed when you use the detailed getters and run into an
1166 object reference. The regular getters resolve this automatically.
1167
1168 \starttyping
1169 [dictionary|array|stream]object = getfromreference(referenceobject)
1170 \stoptyping
1171
1172 */
1173
1174 static int pdfelib_getfromreference(lua_State * L)
1175 {
1176 pdfe_reference *r = check_isreference(L, 1);
1177 if (r != NULL && r->xref != NULL) {
1178 ppref *rr = ppxref_find(r->xref, (ppuint) r->onum);
1179 if (rr != NULL) {
1180 ppobj *o = ppref_obj(rr);
1181 if (o != NULL) {
1182 lua_pushinteger(L,o->type);
1183 return 1 + pushvalue(L,o);
1184 }
1185 }
1186 }
1187 return 0;
1188 }
1189
1190 /*tex
1191
1192 Here are some convenient getters:
1193
1194 \starttyping
1195 <string> = getstring (array|dict|ref,index|key)
1196 <integer> = getinteger (array|dict|ref,index|key)
1197 <number> = getnumber (array|dict|ref,index|key)
1198 <boolan> = getboolean (array|dict|ref,index|key)
1199 <string> = getname (array|dict|ref,index|key)
1200 <dictionary> = getdictionary(array|dict|ref,index|key)
1201 <array> = getarray (array|dict|ref,index|key)
1202 <stream>, <dict> = getstream (array|dict|ref,index|key)
1203 \stoptyping
1204
1205 We report issues when reasonable but are silent when it makes sense. We don't
1206 error on this because we expect the user code to act reasonable on a return
1207 value.
1208
1209 */
1210
1211 # define pdfelib_get_value_check_1 do { \
1212 if (p == NULL) { \
1213 if (t == LUA_TSTRING) { \
1214 normal_warning("pdfe lib","lua <pdfe dictionary> expected"); \
1215 } else if (t == LUA_TNUMBER) { \
1216 normal_warning("pdfe lib","lua <pdfe array> expected"); \
1217 } else { \
1218 normal_warning("pdfe lib","invalid arguments"); \
1219 } \
1220 return 0; \
1221 } else if (! lua_getmetatable(L, 1)) { \
1222 normal_warning("pdfe lib","first argument should be a <pde array> or <pde dictionary>"); \
1223 } \
1224 } while (0)
1225
1226 # define pdfelib_get_value_check_2 \
1227 normal_warning("pdfe lib","second argument should be integer or string");
1228
1229 /*tex
1230
1231 The direct fetcher returns the result or |NULL| when there is nothing
1232 found.
1233
1234 */
1235
1236 # define pdfelib_get_indirect_o(p) \
1237 ppref *r = (((pdfe_reference *) p)->xref != NULL) ? ppxref_find(((pdfe_reference *) p)->xref, (ppuint) (((pdfe_reference *) p)->onum)) : NULL; \
1238 ppobj *o = (r != NULL) ? ppref_obj(r) : NULL; \
1239
1240 # define pdfelib_get_value_direct(get_d,get_a) do { \
1241 int t = lua_type(L,2); \
1242 void *p = lua_touserdata(L, 1); \
1243 lua_settop(L,2); \
1244 pdfelib_get_value_check_1; \
1245 if (t == LUA_TSTRING) { \
1246 const char *key = lua_tostring(L,-2); \
1247 lua_get_metatablelua(luatex_pdfe_dictionary); \
1248 if (lua_rawequal(L, -1, -2)) { \
1249 value = get_d(((pdfe_dictionary *) p)->dictionary, key); \
1250 } else { \
1251 lua_pop(L,1); \
1252 lua_get_metatablelua(luatex_pdfe_reference); \
1253 if (lua_rawequal(L, -1, -2)) { \
1254 pdfelib_get_indirect_o(p); \
1255 if (o != NULL && o->type == PPDICT) { \
1256 value = get_d((ppdict *)o->dict, key); \
1257 } \
1258 } \
1259 } \
1260 } else if (t == LUA_TNUMBER) { \
1261 size_t index = lua_tointeger(L,-2); \
1262 lua_get_metatablelua(luatex_pdfe_array); \
1263 if (lua_rawequal(L, -1, -2)) { \
1264 value = get_a(((pdfe_array *) p)->array, index); \
1265 } else { \
1266 lua_pop(L,1); \
1267 lua_get_metatablelua(luatex_pdfe_reference); \
1268 if (lua_rawequal(L, -1, -2)) { \
1269 pdfelib_get_indirect_o(p); \
1270 if (o != NULL && o->type == PPARRAY) { \
1271 value = get_a((pparray *) o->array, index); \
1272 } \
1273 } \
1274 } \
1275 } else { \
1276 pdfelib_get_value_check_2; \
1277 } \
1278 } while (0)
1279
1280 /*tex
1281
1282 The indirect fetcher passes a pointer to the target variable and returns
1283 success state.
1284
1285 */
1286
1287 # define pdfelib_get_value_indirect(get_d,get_a) do { \
1288 int t = lua_type(L,2); \
1289 void *p = lua_touserdata(L, 1); \
1290 lua_settop(L,2); \
1291 pdfelib_get_value_check_1; \
1292 if (t == LUA_TSTRING) { \
1293 const char *key = lua_tostring(L,-2); \
1294 lua_get_metatablelua(luatex_pdfe_dictionary); \
1295 if (lua_rawequal(L, -1, -2)) { \
1296 okay = get_d(((pdfe_dictionary *) p)->dictionary, key, &value); \
1297 } else { \
1298 lua_pop(L,1); \
1299 lua_get_metatablelua(luatex_pdfe_reference); \
1300 if (lua_rawequal(L, -1, -2)) { \
1301 pdfelib_get_indirect_o(p); \
1302 if (o != NULL && o->type == PPDICT) \
1303 okay = get_d(o->dict, key, &value); \
1304 } \
1305 } \
1306 } else if (t == LUA_TNUMBER) { \
1307 size_t index = lua_tointeger(L,-2); \
1308 lua_get_metatablelua(luatex_pdfe_array); \
1309 if (lua_rawequal(L, -1, -2)) { \
1310 okay = get_a(((pdfe_array *) p)->array, index, &value); \
1311 } else { \
1312 lua_pop(L,1); \
1313 lua_get_metatablelua(luatex_pdfe_reference); \
1314 if (lua_rawequal(L, -1, -2)) { \
1315 pdfelib_get_indirect_o(p); \
1316 if (o != NULL && o->type == PPARRAY) \
1317 okay = get_a(o->array, index, &value); \
1318 } \
1319 } \
1320 } else { \
1321 pdfelib_get_value_check_2; \
1322 } \
1323 } while (0)
1324
1325 /* pre 1.13 version:
1326
1327 static int pdfelib_getstring(lua_State * L)
1328 {
1329 if (lua_gettop(L) > 1) {
1330 ppstring * value = NULL;
1331 pdfelib_get_value_direct(ppdict_rget_string,pparray_rget_string);
1332 if (value != NULL) {
1333 lua_pushlstring(L, ppstring_data(value), ppstring_size(value));
1334 return 2;
1335 }
1336 }
1337 return 0;
1338 }
1339
1340 */
1341
1342 static int pdfelib_getstring(lua_State *L)
1343 {
1344 if (lua_gettop(L) > 1) {
1345 ppstring *value = NULL;
1346 int how = 0;
1347 if (lua_type(L, 3) == LUA_TBOOLEAN) {
1348 if (lua_toboolean(L, 3)) {
1349 how = 1;
1350 } else {
1351 how = 2;
1352 }
1353 }
1354 pdfelib_get_value_direct(ppdict_rget_string,pparray_rget_string);
1355 if (value != NULL) {
1356 if (how == 1) {
1357 value = ppstring_decoded(value);
1358 }
1359 lua_pushlstring(L,ppstring_data(value),ppstring_size(value));
1360 if (how == 2) {
1361 lua_pushboolean(L,ppstring_hex(value));
1362 return 2;
1363 } else {
1364 return 1;
1365 }
1366 }
1367 }
1368 return 0;
1369 }
1370
1371 static int pdfelib_getinteger(lua_State * L)
1372 {
1373 if (lua_gettop(L) > 1) {
1374 ppint value = 0;
1375 int okay = 0;
1376 pdfelib_get_value_indirect(ppdict_rget_int,pparray_rget_int);
1377 if (okay) {
1378 lua_pushinteger(L,(int) value);
1379 return 1;
1380 }
1381 }
1382 return 0;
1383 }
1384
1385 static int pdfelib_getnumber(lua_State * L)
1386 {
1387 if (lua_gettop(L) > 1) {
1388 ppnum value = 0;
1389 int okay = 0;
1390 pdfelib_get_value_indirect(ppdict_rget_num,pparray_rget_num);
1391 if (okay) {
1392 lua_pushnumber(L,value);
1393 return 1;
1394 }
1395 }
1396 return 0;
1397 }
1398
1399 static int pdfelib_getboolean(lua_State * L)
1400 {
1401 if (lua_gettop(L) > 1) {
1402 int value = 0;
1403 int okay = 0;
1404 pdfelib_get_value_indirect(ppdict_rget_bool,pparray_rget_bool);
1405 if (okay) {
1406 lua_pushboolean(L,value);
1407 return 1;
1408 }
1409 }
1410 return 0;
1411 }
1412
1413 static int pdfelib_getname(lua_State * L)
1414 {
1415 if (lua_gettop(L) > 1) {
1416 ppname * value = NULL;
1417 pdfelib_get_value_direct(ppdict_rget_name,pparray_rget_name);
1418 if (value != NULL) {
1419 value = ppname_decoded(value) ;
1420 lua_pushlstring(L, ppname_data(value), ppname_size(value));
1421 return 1;
1422 }
1423 }
1424 return 0;
1425 }
1426
1427 static int pdfelib_getdictionary(lua_State * L)
1428 {
1429 if (lua_gettop(L) > 1) {
1430 ppdict * value = NULL;
1431 pdfelib_get_value_direct(ppdict_rget_dict,pparray_rget_dict);
1432 if (value != NULL) {
1433 return pushdictionaryonly(L,value);
1434 }
1435 }
1436 return 0;
1437 }
1438
1439 static int pdfelib_getarray(lua_State * L)
1440 {
1441 if (lua_gettop(L) > 1) {
1442 pparray * value = NULL;
1443 pdfelib_get_value_direct(ppdict_rget_array,pparray_rget_array);
1444 if (value != NULL) {
1445 return pusharrayonly(L,value);
1446 }
1447 }
1448 return 0;
1449 }
1450
1451 static int pdfelib_getstream(lua_State * L)
1452 {
1453 if (lua_gettop(L) > 1) {
1454 ppobj * value = NULL;
1455 pdfelib_get_value_direct(ppdict_rget_obj,pparray_rget_obj);
1456 if (value != NULL && value->type == PPSTREAM) {
1457 return pushstreamonly(L,(ppstream *) value->stream);
1458 }
1459 }
1460 return 0;
1461 }
1462
1463 /*tex
1464
1465 The generic pushed that does a similar job as the previous getters acts upon
1466 the type.
1467
1468 */
1469
1470 static int pdfelib_pushvalue(lua_State * L, ppobj *object)
1471 {
1472 switch (object->type) {
1473 case PPNONE:
1474 case PPNULL:
1475 lua_pushnil(L);
1476 break;
1477 case PPBOOL:
1478 lua_pushboolean(L, object->integer);
1479 break;
1480 case PPINT:
1481 lua_pushinteger(L, object->integer);
1482 break;
1483 case PPNUM:
1484 lua_pushnumber(L, object->number);
1485 break;
1486 case PPNAME:
1487 {
1488 ppname * n = ppname_decoded(object->name) ;
1489 lua_pushlstring(L, ppname_data(n), ppname_size(n));
1490 }
1491 break;
1492 case PPSTRING:
1493 lua_pushlstring(L, ppstring_data(object->string), ppstring_size(object->string));
1494 break;
1495 case PPARRAY:
1496 return pusharrayonly(L, object->array);
1497 break;
1498 case PPDICT:
1499 return pushdictionary(L, object->dict);
1500 break;
1501 case PPSTREAM:
1502 return pushstream(L, object->stream);
1503 break;
1504 case PPREF:
1505 pushreference(L, object->ref);
1506 break;
1507 default:
1508 lua_pushnil(L);
1509 break;
1510 }
1511 return 1;
1512 }
1513
1514 /*tex
1515
1516 Finally we arrived at the acessors for the userdata objects. The use
1517 previously defined helpers.
1518
1519 */
1520
1521 static int pdfelib_access(lua_State * L)
1522 {
1523 if (lua_type(L,2) == LUA_TSTRING) {
1524 pdfe_document *p = (pdfe_document *)lua_touserdata(L, 1);
1525 const char *s = lua_tostring(L,2);
1526 if (lua_key_eq(s,catalog) || lua_key_eq(s,Catalog)) {
1527 return pushdictionaryonly(L,ppdoc_catalog(p->document));
1528 } else if (lua_key_eq(s,info) || lua_key_eq(s,Info)) {
1529 return pushdictionaryonly(L,ppdoc_info(p->document));
1530 } else if (lua_key_eq(s,trailer) || lua_key_eq(s,Trailer)) {
1531 return pushdictionaryonly(L,ppdoc_trailer(p->document));
1532 } else if (lua_key_eq(s,pages) || lua_key_eq(s,Pages)) {
1533 return pushpages(L,p->document);
1534 }
1535 }
1536 return 0;
1537 }
1538
1539 static int pdfelib_array_access(lua_State * L)
1540 {
1541 if (lua_type(L,2) == LUA_TNUMBER) {
1542 pdfe_array *p = (pdfe_array *)lua_touserdata(L, 1);
1543 ppint index = lua_tointeger(L,2) - 1;
1544 ppobj *o = pparray_rget_obj(p->array,index);
1545 if (o != NULL) {
1546 return pdfelib_pushvalue(L,o);
1547 }
1548 }
1549 return 0;
1550 }
1551
1552 static int pdfelib_dictionary_access(lua_State * L)
1553 {
1554 pdfe_dictionary *p = (pdfe_dictionary *)lua_touserdata(L, 1);
1555 if (lua_type(L,2) == LUA_TSTRING) {
1556 const char *key = lua_tostring(L,2);
1557 ppobj *o = ppdict_rget_obj(p->dictionary,key);
1558 if (o != NULL) {
1559 return pdfelib_pushvalue(L,o);
1560 }
1561 } else if (lua_type(L,2) == LUA_TNUMBER) {
1562 ppint index = lua_tointeger(L,2) - 1;
1563 ppobj *o = ppdict_at(p->dictionary,index);
1564 if (o != NULL) {
1565 return pdfelib_pushvalue(L,o);
1566 }
1567 }
1568 return 0;
1569 }
1570
1571 static int pdfelib_stream_access(lua_State * L)
1572 {
1573 pdfe_stream *p = (pdfe_stream *)lua_touserdata(L, 1);
1574 if (lua_type(L,2) == LUA_TSTRING) {
1575 const char *key = lua_tostring(L,2);
1576 ppobj *o = ppdict_rget_obj(p->stream->dict,key);
1577 if (o != NULL) {
1578 return pdfelib_pushvalue(L,o);
1579 }
1580 } else if (lua_type(L,2) == LUA_TNUMBER) {
1581 ppint index = lua_tointeger(L,2) - 1;
1582 ppobj *o = ppdict_at(p->stream->dict,index);
1583 if (o != NULL) {
1584 return pdfelib_pushvalue(L,o);
1585 }
1586 }
1587 return 0;
1588 }
1589
1590 /*tex
1591
1592 The length metamethods are defined last.
1593
1594 */
1595
1596 static int pdfelib_array_size(lua_State * L)
1597 {
1598 pdfe_array *p = (pdfe_array *)lua_touserdata(L, 1);
1599 lua_pushinteger(L,p->array->size);
1600 return 1;
1601 }
1602
1603 static int pdfelib_dictionary_size(lua_State * L)
1604 {
1605 pdfe_dictionary *p = (pdfe_dictionary *)lua_touserdata(L, 1);
1606 lua_pushinteger(L,p->dictionary->size);
1607 return 1;
1608 }
1609
1610 static int pdfelib_stream_size(lua_State * L)
1611 {
1612 pdfe_stream *p = (pdfe_stream *)lua_touserdata(L, 1);
1613 lua_pushinteger(L,p->stream->dict->size);
1614 return 1;
1615 }
1616
1617 /*tex
1618
1619 We now initialize the main interface. We might add few more informational
1620 helpers but this is it.
1621
1622 */
1623
1624 static const struct luaL_Reg pdfelib[] = {
1625 /* management */
1626 { "type", pdfelib_type },
1627 { "open", pdfelib_open },
1628 { "new", pdfelib_new },
1629 { "close", pdfelib_close },
1630 { "unencrypt", pdfelib_unencrypt },
1631 /* statistics */
1632 { "getversion", pdfelib_getversion },
1633 { "getstatus", pdfelib_getstatus },
1634 { "getsize", pdfelib_getsize },
1635 { "getnofobjects", pdfelib_getnofobjects },
1636 { "getnofpages", pdfelib_getnofpages },
1637 { "getmemoryusage", pdfelib_getmemoryusage },
1638 /* getters */
1639 { "getcatalog", pdfelib_getcatalog },
1640 { "gettrailer", pdfelib_gettrailer },
1641 { "getinfo", pdfelib_getinfo },
1642 { "getpage", pdfelib_getpage },
1643 { "getpages", pdfelib_getpages },
1644 { "getbox", pdfelib_getbox },
1645 { "getfromreference", pdfelib_getfromreference },
1646 { "getfromdictionary", pdfelib_getfromdictionary },
1647 { "getfromarray", pdfelib_getfromarray },
1648 { "getfromstream", pdfelib_getfromstream },
1649 /* collectors */
1650 { "dictionarytotable", pdfelib_dictionarytotable },
1651 { "arraytotable", pdfelib_arraytotable },
1652 { "pagestotable", pdfelib_pagestotable },
1653 /* more getters */
1654 { "getstring", pdfelib_getstring },
1655 { "getinteger", pdfelib_getinteger },
1656 { "getnumber", pdfelib_getnumber },
1657 { "getboolean", pdfelib_getboolean },
1658 { "getname", pdfelib_getname },
1659 { "getdictionary", pdfelib_getdictionary },
1660 { "getarray", pdfelib_getarray },
1661 { "getstream", pdfelib_getstream },
1662 /* streams */
1663 { "readwholestream", pdfelib_readwholestream },
1664 /* not really needed */
1665 { "openstream", pdfelib_openstream },
1666 { "readfromstream", pdfelib_readfromstream },
1667 { "closestream", pdfelib_closestream },
1668 /* done */
1669 { NULL, NULL}
1670 };
1671
1672 /*tex
1673
1674 The user data metatables are defined as follows. Watch how only the
1675 document needs a garbage collector.
1676
1677 */
1678
1679 static const struct luaL_Reg pdfelib_m[] = {
1680 { "__tostring", pdfelib_tostring_document },
1681 { "__gc", pdfelib_free },
1682 { "__index", pdfelib_access },
1683 { NULL, NULL}
1684 };
1685
1686 static const struct luaL_Reg pdfelib_m_dictionary[] = {
1687 { "__tostring", pdfelib_tostring_dictionary },
1688 { "__index", pdfelib_dictionary_access },
1689 { "__len", pdfelib_dictionary_size },
1690 { NULL, NULL}
1691 };
1692
1693 static const struct luaL_Reg pdfelib_m_array[] = {
1694 { "__tostring", pdfelib_tostring_array },
1695 { "__index", pdfelib_array_access },
1696 { "__len", pdfelib_array_size },
1697 { NULL, NULL}
1698 };
1699
1700 static const struct luaL_Reg pdfelib_m_stream[] = {
1701 { "__tostring", pdfelib_tostring_stream },
1702 { "__index", pdfelib_stream_access },
1703 { "__len", pdfelib_stream_size },
1704 { "__call", pdfelib_readwholestream },
1705 { NULL, NULL}
1706 };
1707
1708 static const struct luaL_Reg pdfelib_m_reference[] = {
1709 { "__tostring", pdfelib_tostring_reference },
1710 { NULL, NULL}
1711 };
1712
1713 /*tex
1714
1715 Finally we hav earrived at the main initialiser that will be called as part
1716 of \LUATEX's initializer.
1717
1718 */
1719
1720 /*tex
1721
1722 Here we hook in the error handler.
1723
1724 */
1725
1726 static void pdfelib_message(const char *message, void *alien)
1727 {
1728 normal_warning("pdfe",message);
1729 }
1730
1731 int luaopen_pdfe(lua_State * L)
1732 {
1733 /*tex First the four userdata object get their metatables defined. */
1734
1735 luaL_newmetatable(L, PDFE_METATABLE_DICTIONARY);
1736 luaL_openlib(L, NULL, pdfelib_m_dictionary, 0);
1737
1738 luaL_newmetatable(L, PDFE_METATABLE_ARRAY);
1739 luaL_openlib(L, NULL, pdfelib_m_array, 0);
1740
1741 luaL_newmetatable(L, PDFE_METATABLE_STREAM);
1742 luaL_openlib(L, NULL, pdfelib_m_stream, 0);
1743
1744 luaL_newmetatable(L, PDFE_METATABLE_REFERENCE);
1745 luaL_openlib(L, NULL, pdfelib_m_reference, 0);
1746
1747 /*tex Then comes the main (document) metatable: */
1748
1749 luaL_newmetatable(L, PDFE_METATABLE);
1750 luaL_openlib(L, NULL, pdfelib_m, 0);
1751
1752 /*tex Last the library opens up itself to the world. */
1753
1754 luaL_openlib(L, "pdfe", pdfelib, 0);
1755
1756 pplog_callback(pdfelib_message, stderr);
1757
1758 return 1;
1759 }