beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / lua / lepdflib.cc
blob00e27863a2cda90383e90f5915eaf76447e92b2c
1 /* lepdflib.cc
3 Copyright 2009-2015 Taco Hoekwater <taco@luatex.org>
4 Copyright 2009-2013 Hartmut Henkel <hartmut@luatex.org>
6 This file is part of LuaTeX.
8 LuaTeX is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
13 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License along
19 with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
22 #include "image/epdf.h"
26 // define DEBUG
28 //**********************************************************************
29 // TODO: add more poppler functions (many are still missing)
31 //**********************************************************************
32 // objects allocated by poppler may not be deleted in the lepdflib
34 typedef enum { ALLOC_POPPLER, ALLOC_LEPDF } alloctype;
36 typedef struct {
37 void *d;
38 alloctype atype; // was it allocated by poppler or the lepdflib.cc?
39 PdfDocument *pd; // reference to PdfDocument, or NULL
40 unsigned long pc; // counter to detect PDFDoc change
41 } udstruct;
43 static const char *ErrorCodeNames[] = { "None", "OpenFile", "BadCatalog",
44 "Damaged", "Encrypted", "HighlightFile", "BadPrinter", "Printing",
45 "Permission", "BadPageNum", "FileIO", NULL
48 //**********************************************************************
50 #define M_Annot "epdf.Annot" /* ls-hh: epdf.* gives better protection in registry */
51 #define M_Annots "epdf.Annots"
52 #define M_Array "epdf.Array"
53 #define M_Catalog "epdf.Catalog"
54 #define M_Dict "epdf.Dict"
55 #define M_EmbFile "epdf.EmbFile"
56 #define M_FileSpec "epdf.FileSpec"
57 #define M_GooString "epdf.GooString"
58 #define M_LinkDest "epdf.LinkDest"
59 #define M_Link "epdf.Link"
60 #define M_Links "epdf.Links"
61 #define M_Object "epdf.Object"
62 #define M_Page "epdf.Page"
63 #define M_PDFDoc "epdf.PDFDoc"
64 #define M_PDFRectangle "epdf.PDFRectangle"
65 #define M_Ref "epdf.Ref"
66 #define M_Stream "epdf.Stream"
67 #define M_StructElement "epdf.StructElement"
68 #define M_Attribute "epdf.Attribute"
69 #define M_TextSpan "epdf.TextSpan"
70 #define M_StructTreeRoot "epdf.StructTreeRoot"
71 #define M_XRefEntry "epdf.XRefEntry"
72 #define M_XRef "epdf.XRef"
74 //**********************************************************************
76 #define new_poppler_userdata(type) \
77 static udstruct *new_##type##_userdata(lua_State * L) \
78 { \
79 udstruct *a; \
80 a = (udstruct *) lua_newuserdata(L, sizeof(udstruct)); /* udstruct ... */ \
81 a->atype = ALLOC_POPPLER; \
82 luaL_getmetatable(L, M_##type); /* m udstruct ... */ \
83 lua_setmetatable(L, -2); /* udstruct ... */ \
84 return a; \
87 new_poppler_userdata(PDFDoc);
89 new_poppler_userdata(Annot);
90 new_poppler_userdata(Array);
91 new_poppler_userdata(Catalog);
92 new_poppler_userdata(Dict);
93 new_poppler_userdata(EmbFile);
94 new_poppler_userdata(FileSpec);
95 new_poppler_userdata(LinkDest);
96 new_poppler_userdata(Links);
97 new_poppler_userdata(Object);
98 new_poppler_userdata(Page);
99 new_poppler_userdata(PDFRectangle);
100 new_poppler_userdata(Ref);
101 new_poppler_userdata(Stream);
102 new_poppler_userdata(StructElement);
103 new_poppler_userdata(Attribute);
104 new_poppler_userdata(TextSpan);
105 new_poppler_userdata(StructTreeRoot);
106 new_poppler_userdata(XRef);
108 //**********************************************************************
110 static void pdfdoc_changed_error(lua_State * L)
112 luaL_error(L, "PDFDoc changed or gone");
115 static void pdfdoc_differs_error(lua_State * L)
117 luaL_error(L, "PDFDoc differs between arguments");
120 //**********************************************************************
122 static int l_open_PDFDoc(lua_State * L)
124 const char *file_path;
125 udstruct *uout;
126 PdfDocument *d;
127 file_path = luaL_checkstring(L, 1); // path
128 d = refPdfDocument(file_path, FE_RETURN_NULL);
129 if (d == NULL)
130 lua_pushnil(L);
131 else {
132 if (!(globalParams)) // globalParams could be already created
133 globalParams = new GlobalParams();
134 uout = new_PDFDoc_userdata(L);
135 uout->d = d;
136 uout->atype = ALLOC_LEPDF;
137 uout->pc = d->pc;
138 uout->pd = d;
140 return 1; // doc path
143 static int l_open_MemStreamPDFDoc(lua_State * L)
145 const char *docstream = NULL;
146 char *docstream_usr = NULL ;
147 const char *file_id;
148 unsigned long long stream_size;
149 udstruct *uout;
150 PdfDocument *d;
151 switch (lua_type(L, 1)) {
152 case LUA_TSTRING:
153 docstream = luaL_checkstring(L, 1); // stream as Lua string
154 break;
155 case LUA_TLIGHTUSERDATA:
156 docstream = (const char *) lua_touserdata(L, 1); // stream as sequence of bytes
157 break;
158 default:
159 luaL_error(L, "bad argument: string or lightuserdata expected");
161 if (docstream==NULL)
162 luaL_error(L, "bad document");
163 stream_size = (unsigned long long) luaL_checkint(L, 2);// size of the stream
164 file_id = luaL_checkstring(L, 3); // a symbolic name for this stream, mandatory
165 if (file_id == NULL)
166 luaL_error(L, "PDFDoc has an invalid id");
167 if (strlen(file_id) >STREAM_FILE_ID_LEN ) // a limit to the length of the string
168 luaL_error(L, "PDFDoc has a too long id");
169 docstream_usr = (char *)gmalloc((unsigned) (stream_size + 1));
170 if (!docstream_usr)
171 luaL_error(L, "no room for PDFDoc");
172 memcpy(docstream_usr, docstream, (stream_size + 1));
173 docstream_usr[stream_size]='\0';
174 d = refMemStreamPdfDocument(docstream_usr, stream_size, file_id);
175 if (d == NULL) {
176 lua_pushnil(L);
177 lua_pushnil(L);
178 lua_pushnil(L);
180 else if (d->file_path == NULL ) {
181 lua_pushnil(L);
182 lua_pushnil(L);
183 lua_pushnil(L);
185 else {
186 if (!(globalParams)) // globalParams could be already created
187 globalParams = new GlobalParams();
188 uout = new_PDFDoc_userdata(L);
189 uout->d = d;
190 uout->atype = ALLOC_LEPDF;
191 uout->pc = d->pc;
192 uout->pd = d;
193 lua_pushstring(L,d->file_path);
194 lua_pushstring(L,STREAM_URI);
196 return 3; // stream, stream_id, stream_uri
202 static int l_new_Array(lua_State * L)
204 udstruct *uxref, *uout;
205 uxref = (udstruct *) luaL_checkudata(L, 1, M_XRef);
206 if (uxref->pd != NULL && uxref->pd->pc != uxref->pc)
207 pdfdoc_changed_error(L);
208 uout = new_Array_userdata(L);
209 uout->d = new Array((XRef *) uxref->d); // automatic init to length 0
210 uout->atype = ALLOC_LEPDF;
211 uout->pc = uxref->pc;
212 uout->pd = uxref->pd;
213 return 1;
216 static int l_new_Attribute(lua_State * L)
218 Attribute::Type t;
219 const char *n;
220 int nlen;
221 udstruct *uobj, *uout;
223 if (lua_type(L,1)==LUA_TNUMBER) {
224 uobj = (udstruct *) luaL_checkudata(L, 2, M_Object);
225 if (uobj->pd != NULL && uobj->pd->pc != uobj->pc)
226 pdfdoc_changed_error(L);
227 t = (Attribute::Type) luaL_checkint(L, 1);
228 uout = new_Attribute_userdata(L);
229 uout->d = new Attribute(t, (Object *)uobj->d);
230 uout->atype = ALLOC_LEPDF;
231 uout->pc = uobj->pc;
232 uout->pd = uobj->pd;
234 } else if (lua_type(L,1)==LUA_TSTRING) {
235 n = luaL_checkstring(L,1);
236 nlen = luaL_checkint(L,2);
237 uobj = (udstruct *) luaL_checkudata(L, 3, M_Object);
238 if (uobj->pd != NULL && uobj->pd->pc != uobj->pc)
239 pdfdoc_changed_error(L);
240 uout = new_Attribute_userdata(L);
241 uout->d = new Attribute(n, nlen, (Object *)uobj->d);
242 uout->atype = ALLOC_LEPDF;
243 uout->pc = uobj->pc;
244 uout->pd = uobj->pd;
245 } else
246 lua_pushnil(L);
247 return 1;
250 #define ATTRIBUTE_TYPE_ENTRY(name) \
251 lua_pushstring(L, #name); \
252 lua_pushinteger(L, Attribute::name); \
253 lua_settable(L,-3)
256 #define STRUCTELEMENT_TYPE_ENTRY(name) \
257 lua_pushstring(L, #name); \
258 lua_pushinteger(L, StructElement::name); \
259 lua_settable(L,-3)
262 static int l_Attribute_Type(lua_State * L) {
263 lua_createtable (L, 0, 42);
264 ATTRIBUTE_TYPE_ENTRY(BBox);
265 ATTRIBUTE_TYPE_ENTRY(BackgroundColor);
266 ATTRIBUTE_TYPE_ENTRY(BorderColor);
267 ATTRIBUTE_TYPE_ENTRY(BorderThickness);
268 ATTRIBUTE_TYPE_ENTRY(Color);
269 ATTRIBUTE_TYPE_ENTRY(ColumnGap);
270 ATTRIBUTE_TYPE_ENTRY(ColumnWidths);
271 ATTRIBUTE_TYPE_ENTRY(Desc);
272 ATTRIBUTE_TYPE_ENTRY(Role);
273 ATTRIBUTE_TYPE_ENTRY(TextDecorationColor);
274 ATTRIBUTE_TYPE_ENTRY(TextDecorationThickness);
275 ATTRIBUTE_TYPE_ENTRY(BaselineShift);
276 ATTRIBUTE_TYPE_ENTRY(BlockAlign);
277 ATTRIBUTE_TYPE_ENTRY(BorderStyle);
278 ATTRIBUTE_TYPE_ENTRY(ColSpan);
279 ATTRIBUTE_TYPE_ENTRY(ColumnCount);
280 ATTRIBUTE_TYPE_ENTRY(EndIndent);
281 ATTRIBUTE_TYPE_ENTRY(GlyphOrientationVertical);
282 ATTRIBUTE_TYPE_ENTRY(Headers);
283 ATTRIBUTE_TYPE_ENTRY(Height);
284 ATTRIBUTE_TYPE_ENTRY(InlineAlign);
285 ATTRIBUTE_TYPE_ENTRY(LineHeight);
286 ATTRIBUTE_TYPE_ENTRY(ListNumbering);
287 ATTRIBUTE_TYPE_ENTRY(Padding);
288 ATTRIBUTE_TYPE_ENTRY(Placement);
289 ATTRIBUTE_TYPE_ENTRY(RowSpan);
290 ATTRIBUTE_TYPE_ENTRY(RubyAlign);
291 ATTRIBUTE_TYPE_ENTRY(RubyPosition);
292 ATTRIBUTE_TYPE_ENTRY(Scope);
293 ATTRIBUTE_TYPE_ENTRY(SpaceAfter);
294 ATTRIBUTE_TYPE_ENTRY(SpaceBefore);
295 ATTRIBUTE_TYPE_ENTRY(StartIndent);
296 ATTRIBUTE_TYPE_ENTRY(Summary);
297 ATTRIBUTE_TYPE_ENTRY(TBorderStyle);
298 ATTRIBUTE_TYPE_ENTRY(TPadding);
299 ATTRIBUTE_TYPE_ENTRY(TextAlign);
300 ATTRIBUTE_TYPE_ENTRY(TextDecorationType);
301 ATTRIBUTE_TYPE_ENTRY(TextIndent);
302 ATTRIBUTE_TYPE_ENTRY(Width);
303 ATTRIBUTE_TYPE_ENTRY(WritingMode);
304 ATTRIBUTE_TYPE_ENTRY(Unknown);
305 ATTRIBUTE_TYPE_ENTRY(checked);
306 return 1;
309 static int l_StructElement_Type(lua_State * L) {
310 lua_createtable (L, 0, 50);
311 STRUCTELEMENT_TYPE_ENTRY(Document);
312 STRUCTELEMENT_TYPE_ENTRY(Part);
313 STRUCTELEMENT_TYPE_ENTRY(Art);
314 STRUCTELEMENT_TYPE_ENTRY(Sect);
315 STRUCTELEMENT_TYPE_ENTRY(Div);
316 STRUCTELEMENT_TYPE_ENTRY(BlockQuote);
317 STRUCTELEMENT_TYPE_ENTRY(Caption);
318 STRUCTELEMENT_TYPE_ENTRY(NonStruct);
319 STRUCTELEMENT_TYPE_ENTRY(Index);
320 STRUCTELEMENT_TYPE_ENTRY(Private);
321 STRUCTELEMENT_TYPE_ENTRY(Span);
322 STRUCTELEMENT_TYPE_ENTRY(Quote);
323 STRUCTELEMENT_TYPE_ENTRY(Note);
324 STRUCTELEMENT_TYPE_ENTRY(Reference);
325 STRUCTELEMENT_TYPE_ENTRY(BibEntry);
326 STRUCTELEMENT_TYPE_ENTRY(Code);
327 STRUCTELEMENT_TYPE_ENTRY(Link);
328 STRUCTELEMENT_TYPE_ENTRY(Annot);
329 STRUCTELEMENT_TYPE_ENTRY(Ruby);
330 STRUCTELEMENT_TYPE_ENTRY(RB);
331 STRUCTELEMENT_TYPE_ENTRY(RT);
332 STRUCTELEMENT_TYPE_ENTRY(RP);
333 STRUCTELEMENT_TYPE_ENTRY(Warichu);
334 STRUCTELEMENT_TYPE_ENTRY(WT);
335 STRUCTELEMENT_TYPE_ENTRY(WP);
336 STRUCTELEMENT_TYPE_ENTRY(P);
337 STRUCTELEMENT_TYPE_ENTRY(H);
338 STRUCTELEMENT_TYPE_ENTRY(H1);
339 STRUCTELEMENT_TYPE_ENTRY(H2);
340 STRUCTELEMENT_TYPE_ENTRY(H3);
341 STRUCTELEMENT_TYPE_ENTRY(H4);
342 STRUCTELEMENT_TYPE_ENTRY(H5);
343 STRUCTELEMENT_TYPE_ENTRY(H6);
344 STRUCTELEMENT_TYPE_ENTRY(L);
345 STRUCTELEMENT_TYPE_ENTRY(LI);
346 STRUCTELEMENT_TYPE_ENTRY(Lbl);
347 STRUCTELEMENT_TYPE_ENTRY(LBody);
348 STRUCTELEMENT_TYPE_ENTRY(Table);
349 STRUCTELEMENT_TYPE_ENTRY(TR);
350 STRUCTELEMENT_TYPE_ENTRY(TH);
351 STRUCTELEMENT_TYPE_ENTRY(TD);
352 STRUCTELEMENT_TYPE_ENTRY(THead);
353 STRUCTELEMENT_TYPE_ENTRY(TFoot);
354 STRUCTELEMENT_TYPE_ENTRY(TBody);
355 STRUCTELEMENT_TYPE_ENTRY(Figure);
356 STRUCTELEMENT_TYPE_ENTRY(Formula);
357 STRUCTELEMENT_TYPE_ENTRY(Form);
358 STRUCTELEMENT_TYPE_ENTRY(TOC);
359 STRUCTELEMENT_TYPE_ENTRY(TOCI);
360 lua_pushstring(L, "Unknown");
361 lua_pushinteger(L, 0);
362 lua_settable(L,-3);
363 return 1;
366 static int l_AttributeOwner_Type(lua_State * L) {
367 lua_createtable (L, 0, 12);
368 lua_pushstring(L, "XML-1.00"); lua_pushinteger(L, Attribute::XML_1_00); lua_settable(L,-3);
369 lua_pushstring(L, "HTML-3.20"); lua_pushinteger(L, Attribute::HTML_3_20); lua_settable(L,-3);
370 lua_pushstring(L, "HTML-4.01"); lua_pushinteger(L, Attribute::HTML_4_01); lua_settable(L,-3);
371 lua_pushstring(L, "OEB-1.00"); lua_pushinteger(L, Attribute::OEB_1_00); lua_settable(L,-3);
372 lua_pushstring(L, "RTF-1.05"); lua_pushinteger(L, Attribute::RTF_1_05); lua_settable(L,-3);
373 lua_pushstring(L, "CSS-1.00"); lua_pushinteger(L, Attribute::CSS_1_00); lua_settable(L,-3);
374 lua_pushstring(L, "CSS-2.00"); lua_pushinteger(L, Attribute::CSS_2_00); lua_settable(L,-3);
375 lua_pushstring(L, "Layout"); lua_pushinteger(L, Attribute::Layout); lua_settable(L,-3);
376 lua_pushstring(L, "PrintField"); lua_pushinteger(L, Attribute::PrintField); lua_settable(L,-3);
377 lua_pushstring(L, "Table"); lua_pushinteger(L, Attribute::Table); lua_settable(L,-3);
378 lua_pushstring(L, "List"); lua_pushinteger(L, Attribute::List); lua_settable(L,-3);
379 lua_pushstring(L, "UserProperties"); lua_pushinteger(L, Attribute::UserProperties);lua_settable(L,-3);
380 return 1;
384 static int l_new_Dict(lua_State * L)
386 udstruct *uxref, *uout;
387 uxref = (udstruct *) luaL_checkudata(L, 1, M_XRef);
388 if (uxref->pd != NULL && uxref->pd->pc != uxref->pc)
389 pdfdoc_changed_error(L);
390 uout = new_Dict_userdata(L);
391 uout->d = new Dict((XRef *) uxref->d); // automatic init to length 0
392 uout->atype = ALLOC_LEPDF;
393 uout->pc = uxref->pc;
394 uout->pd = uxref->pd;
395 return 1;
398 static int l_new_Object(lua_State * L)
400 udstruct *uout;
401 uout = new_Object_userdata(L);
402 uout->d = new Object(); // automatic init to type "none"
403 uout->atype = ALLOC_LEPDF;
404 uout->pc = 0;
405 uout->pd = NULL; // not connected to any PDFDoc
406 return 1;
409 // PDFRectangle see Page.h
411 static int l_new_PDFRectangle(lua_State * L)
413 udstruct *uout;
414 uout = new_PDFRectangle_userdata(L);
415 uout->d = new PDFRectangle(); // automatic init to [0, 0, 0, 0]
416 uout->atype = ALLOC_LEPDF;
417 uout->pc = 0;
418 uout->pd = NULL;
419 return 1;
422 static const struct luaL_Reg epdflib_f[] = {
423 {"open", l_open_PDFDoc},
424 {"openMemStream", l_open_MemStreamPDFDoc},
425 {"Array", l_new_Array},
426 {"Attribute", l_new_Attribute},
427 {"StructElement_Type", l_StructElement_Type},
428 {"Attribute_Type", l_Attribute_Type},
429 {"AttributeOwner_Type",l_AttributeOwner_Type},
430 {"Dict", l_new_Dict},
431 {"Object", l_new_Object},
432 {"PDFRectangle", l_new_PDFRectangle},
433 {NULL, NULL} // sentinel
436 //**********************************************************************
438 #define m_poppler_get_poppler(in, out, function) \
439 static int m_##in##_##function(lua_State * L) \
441 out *o; \
442 udstruct *uin, *uout; \
443 uin = (udstruct *) luaL_checkudata(L, 1, M_##in); \
444 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
445 pdfdoc_changed_error(L); \
446 o = ((in *) uin->d)->function(); \
447 if (o != NULL) { \
448 uout = new_##out##_userdata(L); \
449 uout->d = o; \
450 uout->pc = uin->pc; \
451 uout->pd = uin->pd; \
452 } else \
453 lua_pushnil(L); \
454 return 1; \
457 #define m_poppler_get_BOOL(in, function) \
458 static int m_##in##_##function(lua_State * L) \
460 udstruct *uin; \
461 uin = (udstruct *) luaL_checkudata(L, 1, M_##in); \
462 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
463 pdfdoc_changed_error(L); \
464 if (((in *) uin->d)->function()) \
465 lua_pushboolean(L, 1); \
466 else \
467 lua_pushboolean(L, 0); \
468 return 1; \
471 #define m_poppler_get_INT(in, function) \
472 static int m_##in##_##function(lua_State * L) \
474 int i; \
475 udstruct *uin; \
476 uin = (udstruct *) luaL_checkudata(L, 1, M_##in); \
477 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
478 pdfdoc_changed_error(L); \
479 i = (int) ((in *) uin->d)->function(); \
480 lua_pushinteger(L, i); \
481 return 1; \
485 #define m_poppler_get_GUINT(in, function) \
486 static int m_##in##_##function(lua_State * L) \
488 unsigned int i; \
489 udstruct *uin; \
490 uin = (udstruct *) luaL_checkudata(L, 1, M_##in); \
491 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
492 pdfdoc_changed_error(L); \
493 i = (unsigned int) ((in *) uin->d)->function(); \
494 lua_pushinteger(L, i); \
495 return 1; \
498 #define m_poppler_get_UINT(in, function) \
499 m_poppler_get_GUINT(in, function)
503 #define m_poppler_get_DOUBLE(in, function) \
504 static int m_##in##_##function(lua_State * L) \
506 double d; \
507 udstruct *uin; \
508 uin = (udstruct *) luaL_checkudata(L, 1, M_##in); \
509 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
510 pdfdoc_changed_error(L); \
511 d = (double) ((in *) uin->d)->function(); \
512 lua_pushnumber(L, d); /* float */ \
513 return 1; \
516 #define m_poppler_get_GOOSTRING(in, function) \
517 static int m_##in##_##function(lua_State * L) \
519 GooString *gs; \
520 udstruct *uin; \
521 uin = (udstruct *) luaL_checkudata(L, 1, M_##in); \
522 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
523 pdfdoc_changed_error(L); \
524 gs = ((in *) uin->d)->function(); \
525 if (gs != NULL) \
526 lua_pushlstring(L, gs->getCString(), gs->getLength()); \
527 else \
528 lua_pushnil(L); \
529 return 1; \
532 #define m_poppler_get_OBJECT(in, function) \
533 static int m_##in##_##function(lua_State * L) \
535 udstruct *uin, *uout; \
536 uin = (udstruct *) luaL_checkudata(L, 1, M_##in); \
537 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
538 pdfdoc_changed_error(L); \
539 uout = new_Object_userdata(L); \
540 uout->d = new Object(); \
541 ((in *) uin->d)->function((Object *) uout->d); \
542 uout->atype = ALLOC_LEPDF; \
543 uout->pc = uin->pc; \
544 uout->pd = uin->pd; \
545 return 1; \
548 #define m_poppler_do(in, function) \
549 static int m_##in##_##function(lua_State * L) \
551 udstruct *uin; \
552 uin = (udstruct *) luaL_checkudata(L, 1, M_##in); \
553 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
554 pdfdoc_changed_error(L); \
555 ((in *) uin->d)->function(); \
556 return 0; \
559 #define m_poppler__tostring(type) \
560 static int m_##type##__tostring(lua_State * L) \
562 udstruct *uin; \
563 uin = (udstruct *) luaL_checkudata(L, 1, M_##type); \
564 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
565 pdfdoc_changed_error(L); \
566 lua_pushfstring(L, "%s: %p", #type, (type *) uin->d); \
567 return 1; \
570 #define m_poppler_check_string(in, function) \
571 static int m_##in##_##function(lua_State * L) \
573 const char *s; \
574 udstruct *uin; \
575 uin = (udstruct *) luaL_checkudata(L, 1, M_##in); \
576 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
577 pdfdoc_changed_error(L); \
578 s = luaL_checkstring(L, 2); \
579 if (((in *) uin->d)->function(s)) \
580 lua_pushboolean(L, 1); \
581 else \
582 lua_pushboolean(L, 0); \
583 return 1; \
586 //**********************************************************************
587 // Annot
589 m_poppler_get_BOOL(Annot, isOk);
591 static int m_Annot_match(lua_State * L)
593 udstruct *uin, *uref;
594 uin = (udstruct *) luaL_checkudata(L, 1, M_Annot);
595 uref = (udstruct *) luaL_checkudata(L, 2, M_Ref);
596 if (uin->pd != NULL && uref->pd != NULL && uin->pd != uref->pd)
597 pdfdoc_differs_error(L);
598 if ((uin->pd != NULL && uin->pd->pc != uin->pc)
599 || (uref->pd != NULL && uref->pd->pc != uref->pc))
600 pdfdoc_changed_error(L);
601 lua_pushboolean(L, ((Annot *) uin->d)->match((Ref *) uref->d));
602 return 1;
605 m_poppler__tostring(Annot);
607 static int m_Annot__gc(lua_State * L)
609 udstruct *uin;
610 uin = (udstruct *) luaL_checkudata(L, 1, M_Annot);
611 if (uin->pd != NULL && uin->pd->pc != uin->pc)
612 pdfdoc_changed_error(L);
613 #ifdef DEBUG
614 printf("\n===== Annot GC ===== uin=<%p>\n", uin);
615 #endif
616 if (uin->atype == ALLOC_LEPDF)
617 #if 1 /* def HAVE_ANNOTDECREFCNT */
618 ((Annot *) uin->d)->decRefCnt();
619 #else
620 delete(Annot *) uin->d;
621 #endif
622 return 0;
625 static const struct luaL_Reg Annot_m[] = {
626 {"isOk", m_Annot_isOk},
627 {"match", m_Annot_match},
628 {"__tostring", m_Annot__tostring},
629 {"__gc", m_Annot__gc},
630 {NULL, NULL} // sentinel
633 //**********************************************************************
634 // Annots
636 m_poppler_get_INT(Annots, getNumAnnots);
638 static int m_Annots_getAnnot(lua_State * L)
640 int i, annots;
641 udstruct *uin, *uout;
642 uin = (udstruct *) luaL_checkudata(L, 1, M_Annots);
643 if (uin->pd != NULL && uin->pd->pc != uin->pc)
644 pdfdoc_changed_error(L);
645 i = luaL_checkint(L, 2);
646 annots = ((Annots *) uin->d)->getNumAnnots();
647 if (i > 0 && i <= annots) {
648 uout = new_Annot_userdata(L);
649 uout->d = ((Annots *) uin->d)->getAnnot(i);
650 uout->pc = uin->pc;
651 uout->pd = uin->pd;
652 } else
653 lua_pushnil(L);
654 return 1;
657 m_poppler__tostring(Annots);
659 static const struct luaL_Reg Annots_m[] = {
660 {"getNumAnnots", m_Annots_getNumAnnots},
661 {"getAnnot", m_Annots_getAnnot},
662 {"__tostring", m_Annots__tostring},
663 {NULL, NULL} // sentinel
666 //**********************************************************************
667 // Array
669 static int m_Array_incRef(lua_State * L)
671 int i;
672 udstruct *uin;
673 uin = (udstruct *) luaL_checkudata(L, 1, M_Array);
674 if (uin->pd != NULL && uin->pd->pc != uin->pc)
675 pdfdoc_changed_error(L);
676 i = ((Array *) uin->d)->incRef();
677 lua_pushinteger(L, i);
678 return 1;
681 static int m_Array_decRef(lua_State * L)
683 int i;
684 udstruct *uin;
685 uin = (udstruct *) luaL_checkudata(L, 1, M_Array);
686 if (uin->pd != NULL && uin->pd->pc != uin->pc)
687 pdfdoc_changed_error(L);
688 i = ((Array *) uin->d)->decRef();
689 lua_pushinteger(L, i);
690 return 1;
693 m_poppler_get_INT(Array, getLength);
695 static int m_Array_add(lua_State * L)
697 udstruct *uin, *uobj;
698 uin = (udstruct *) luaL_checkudata(L, 1, M_Array);
699 uobj = (udstruct *) luaL_checkudata(L, 2, M_Object);
700 if (uin->pd != NULL && uobj->pd != NULL && uin->pd != uobj->pd)
701 pdfdoc_differs_error(L);
702 if ((uin->pd != NULL && uin->pd->pc != uin->pc)
703 || (uobj->pd != NULL && uobj->pd->pc != uobj->pc))
704 pdfdoc_changed_error(L);
705 ((Array *) uin->d)->add(((Object *) uobj->d));
706 return 0;
709 static int m_Array_get(lua_State * L)
711 int i, len;
712 udstruct *uin, *uout;
713 uin = (udstruct *) luaL_checkudata(L, 1, M_Array);
714 if (uin->pd != NULL && uin->pd->pc != uin->pc)
715 pdfdoc_changed_error(L);
716 i = luaL_checkint(L, 2);
717 len = ((Array *) uin->d)->getLength();
718 if (i > 0 && i <= len) {
719 uout = new_Object_userdata(L);
720 uout->d = new Object();
721 ((Array *) uin->d)->get(i - 1, (Object *) uout->d);
722 uout->atype = ALLOC_LEPDF;
723 uout->pc = uin->pc;
724 uout->pd = uin->pd;
725 } else
726 lua_pushnil(L);
727 return 1;
730 static int m_Array_getNF(lua_State * L)
732 int i, len;
733 udstruct *uin, *uout;
734 uin = (udstruct *) luaL_checkudata(L, 1, M_Array);
735 if (uin->pd != NULL && uin->pd->pc != uin->pc)
736 pdfdoc_changed_error(L);
737 i = luaL_checkint(L, 2);
738 len = ((Array *) uin->d)->getLength();
739 if (i > 0 && i <= len) {
740 uout = new_Object_userdata(L);
741 uout->d = new Object();
742 ((Array *) uin->d)->getNF(i - 1, (Object *) uout->d);
743 uout->atype = ALLOC_LEPDF;
744 uout->pc = uin->pc;
745 uout->pd = uin->pd;
746 } else
747 lua_pushnil(L);
748 return 1;
751 static int m_Array_getString(lua_State * L)
753 GooString *gs;
754 int i, len;
755 udstruct *uin;
756 uin = (udstruct *) luaL_checkudata(L, 1, M_Array);
757 if (uin->pd != NULL && uin->pd->pc != uin->pc)
758 pdfdoc_changed_error(L);
759 i = luaL_checkint(L, 2);
760 len = ((Array *) uin->d)->getLength();
761 if (i > 0 && i <= len) {
762 gs = new GooString();
763 if (((Array *) uin->d)->getString(i - 1, gs))
764 lua_pushlstring(L, gs->getCString(), gs->getLength());
765 else
766 lua_pushnil(L);
767 delete gs;
768 } else
769 lua_pushnil(L);
770 return 1;
773 m_poppler__tostring(Array);
775 static const struct luaL_Reg Array_m[] = {
776 {"incRef", m_Array_incRef},
777 {"decRef", m_Array_decRef},
778 {"getLength", m_Array_getLength},
779 {"add", m_Array_add},
780 {"get", m_Array_get},
781 {"getNF", m_Array_getNF},
782 {"getString", m_Array_getString},
783 {"__tostring", m_Array__tostring},
784 {NULL, NULL} // sentinel
787 //**********************************************************************
788 // Catalog
790 m_poppler_get_BOOL(Catalog, isOk);
791 m_poppler_get_INT(Catalog, getNumPages);
793 static int m_Catalog_getPage(lua_State * L)
795 int i, pages;
796 udstruct *uin, *uout;
797 uin = (udstruct *) luaL_checkudata(L, 1, M_Catalog);
798 if (uin->pd != NULL && uin->pd->pc != uin->pc)
799 pdfdoc_changed_error(L);
800 i = luaL_checkint(L, 2);
801 pages = ((Catalog *) uin->d)->getNumPages();
802 if (i > 0 && i <= pages) {
803 uout = new_Page_userdata(L);
804 uout->d = ((Catalog *) uin->d)->getPage(i);
805 uout->pc = uin->pc;
806 uout->pd = uin->pd;
807 } else
808 lua_pushnil(L);
809 return 1;
812 static int m_Catalog_getPageRef(lua_State * L)
814 int i, pages;
815 udstruct *uin, *uout;
816 uin = (udstruct *) luaL_checkudata(L, 1, M_Catalog);
817 if (uin->pd != NULL && uin->pd->pc != uin->pc)
818 pdfdoc_changed_error(L);
819 i = luaL_checkint(L, 2);
820 pages = ((Catalog *) uin->d)->getNumPages();
821 if (i > 0 && i <= pages) {
822 uout = new_Ref_userdata(L);
823 uout->d = (Ref *) gmalloc(sizeof(Ref));
824 ((Ref *) uout->d)->num = ((Catalog *) uin->d)->getPageRef(i)->num;
825 ((Ref *) uout->d)->gen = ((Catalog *) uin->d)->getPageRef(i)->gen;
826 uout->atype = ALLOC_LEPDF;
827 uout->pc = uin->pc;
828 uout->pd = uin->pd;
829 } else
830 lua_pushnil(L);
831 return 1;
834 m_poppler_get_GOOSTRING(Catalog, getBaseURI);
835 m_poppler_get_GOOSTRING(Catalog, readMetadata);
836 m_poppler_get_poppler(Catalog, StructTreeRoot, getStructTreeRoot);
838 static int m_Catalog_findPage(lua_State * L)
840 int num, gen, i;
841 udstruct *uin;
842 uin = (udstruct *) luaL_checkudata(L, 1, M_Catalog);
843 if (uin->pd != NULL && uin->pd->pc != uin->pc)
844 pdfdoc_changed_error(L);
845 num = luaL_checkint(L, 2);
846 gen = luaL_checkint(L, 3);
847 i = ((Catalog *) uin->d)->findPage(num, gen);
848 if (i > 0)
849 lua_pushinteger(L, i);
850 else
851 lua_pushnil(L);
852 return 1;
855 static int m_Catalog_findDest(lua_State * L)
857 GooString *name;
858 LinkDest *dest;
859 const char *s;
860 size_t len;
861 udstruct *uin, *uout;
862 uin = (udstruct *) luaL_checkudata(L, 1, M_Catalog);
863 if (uin->pd != NULL && uin->pd->pc != uin->pc)
864 pdfdoc_changed_error(L);
865 s = luaL_checklstring(L, 2, &len);
866 name = new GooString(s, len);
867 dest = ((Catalog *) uin->d)->findDest(name);
868 if (dest != NULL) {
869 uout = new_LinkDest_userdata(L);
870 uout->d = dest;
871 uout->pc = uin->pc;
872 uout->pd = uin->pd;
873 } else
874 lua_pushnil(L);
875 delete name;
876 return 1;
879 m_poppler_get_poppler(Catalog, Object, getDests);
880 m_poppler_get_INT(Catalog, numEmbeddedFiles);
882 static int m_Catalog_embeddedFile(lua_State * L)
884 int i, len;
885 udstruct *uin, *uout;
886 uin = (udstruct *) luaL_checkudata(L, 1, M_Catalog);
887 if (uin->pd != NULL && uin->pd->pc != uin->pc)
888 pdfdoc_changed_error(L);
889 i = luaL_checkint(L, 2);
890 len = ((Catalog *) uin->d)->numEmbeddedFiles();
891 if (i > 0 && i <= len) {
892 uout = new_FileSpec_userdata(L);
893 uout->d = ((Catalog *) uin->d)->embeddedFile(i - 1);
894 uout->pc = uin->pc;
895 uout->pd = uin->pd;
896 } else
897 lua_pushnil(L);
898 return 1;
901 m_poppler_get_INT(Catalog, numJS);
903 static int m_Catalog_getJS(lua_State * L)
905 GooString *gs;
906 int i, len;
907 udstruct *uin;
908 uin = (udstruct *) luaL_checkudata(L, 1, M_Catalog);
909 if (uin->pd != NULL && uin->pd->pc != uin->pc)
910 pdfdoc_changed_error(L);
911 i = luaL_checkint(L, 2);
912 len = ((Catalog *) uin->d)->numJS();
913 if (i > 0 && i <= len) {
914 gs = ((Catalog *) uin->d)->getJS(i - 1);
915 if (gs != NULL)
916 lua_pushlstring(L, gs->getCString(), gs->getLength());
917 else
918 lua_pushnil(L);
919 delete gs;
920 } else
921 lua_pushnil(L);
922 return 1;
925 m_poppler_get_poppler(Catalog, Object, getOutline);
926 m_poppler_get_poppler(Catalog, Object, getAcroForm);
928 m_poppler__tostring(Catalog);
930 static const struct luaL_Reg Catalog_m[] = {
931 {"isOk", m_Catalog_isOk},
932 {"getNumPages", m_Catalog_getNumPages},
933 {"getPage", m_Catalog_getPage},
934 {"getPageRef", m_Catalog_getPageRef},
935 {"getBaseURI", m_Catalog_getBaseURI},
936 {"readMetadata", m_Catalog_readMetadata},
937 {"getStructTreeRoot", m_Catalog_getStructTreeRoot},
938 {"findPage", m_Catalog_findPage},
939 {"findDest", m_Catalog_findDest},
940 {"getDests", m_Catalog_getDests},
941 {"numEmbeddedFiles", m_Catalog_numEmbeddedFiles},
942 {"embeddedFile", m_Catalog_embeddedFile},
943 {"numJS", m_Catalog_numJS},
944 {"getJS", m_Catalog_getJS},
945 {"getOutline", m_Catalog_getOutline},
946 {"getAcroForm", m_Catalog_getAcroForm},
947 {"__tostring", m_Catalog__tostring},
948 {NULL, NULL} // sentinel
951 //**********************************************************************
952 // Dict
954 static int m_Dict_incRef(lua_State * L)
956 int i;
957 udstruct *uin;
958 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
959 if (uin->pd != NULL && uin->pd->pc != uin->pc)
960 pdfdoc_changed_error(L);
961 i = ((Dict *) uin->d)->incRef();
962 lua_pushinteger(L, i);
963 return 1;
966 static int m_Dict_decRef(lua_State * L)
968 int i;
969 udstruct *uin;
970 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
971 if (uin->pd != NULL && uin->pd->pc != uin->pc)
972 pdfdoc_changed_error(L);
973 i = ((Dict *) uin->d)->decRef();
974 lua_pushinteger(L, i);
975 return 1;
978 m_poppler_get_INT(Dict, getLength);
980 static int m_Dict_add(lua_State * L)
982 char *s;
983 udstruct *uin, *uobj;
984 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
985 if (uin->pd != NULL && uin->pd->pc != uin->pc)
986 pdfdoc_changed_error(L);
987 s = copyString(luaL_checkstring(L, 2));
988 uobj = (udstruct *) luaL_checkudata(L, 3, M_Object);
989 ((Dict *) uin->d)->add(s, ((Object *) uobj->d));
990 return 0;
993 static int m_Dict_set(lua_State * L)
995 const char *s;
996 udstruct *uin, *uobj;
997 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
998 if (uin->pd != NULL && uin->pd->pc != uin->pc)
999 pdfdoc_changed_error(L);
1000 s = luaL_checkstring(L, 2);
1001 uobj = (udstruct *) luaL_checkudata(L, 3, M_Object);
1002 ((Dict *) uin->d)->set(s, ((Object *) uobj->d));
1003 return 0;
1006 static int m_Dict_remove(lua_State * L)
1008 const char *s;
1009 udstruct *uin;
1010 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
1011 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1012 pdfdoc_changed_error(L);
1013 s = luaL_checkstring(L, 2);
1014 ((Dict *) uin->d)->remove(s);
1015 return 0;
1018 m_poppler_check_string(Dict, is);
1020 static int m_Dict_lookup(lua_State * L)
1022 const char *s;
1023 udstruct *uin, *uout;
1024 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
1025 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1026 pdfdoc_changed_error(L);
1027 s = luaL_checkstring(L, 2);
1028 uout = new_Object_userdata(L);
1029 uout->d = new Object();
1030 ((Dict *) uin->d)->lookup(s, (Object *) uout->d);
1031 uout->atype = ALLOC_LEPDF;
1032 uout->pc = uin->pc;
1033 uout->pd = uin->pd;
1034 return 1;
1037 static int m_Dict_lookupNF(lua_State * L)
1039 const char *s;
1040 udstruct *uin, *uout;
1041 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
1042 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1043 pdfdoc_changed_error(L);
1044 s = luaL_checkstring(L, 2);
1045 uout = new_Object_userdata(L);
1046 uout->d = new Object();
1047 ((Dict *) uin->d)->lookupNF(s, (Object *) uout->d);
1048 uout->atype = ALLOC_LEPDF;
1049 uout->pc = uin->pc;
1050 uout->pd = uin->pd;
1051 return 1;
1054 static int m_Dict_lookupInt(lua_State * L)
1056 const char *s1, *s2;
1057 int i;
1058 udstruct *uin;
1059 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
1060 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1061 pdfdoc_changed_error(L);
1062 s1 = luaL_checkstring(L, 2);
1063 s2 = luaL_checkstring(L, 3);
1064 if (((Dict *) uin->d)->lookupInt(s1, s2, &i))
1065 lua_pushinteger(L, i);
1066 else
1067 lua_pushnil(L);
1068 return 1;
1071 static int m_Dict_getKey(lua_State * L)
1073 int i, len;
1074 udstruct *uin;
1075 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
1076 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1077 pdfdoc_changed_error(L);
1078 i = luaL_checkint(L, 2);
1079 len = ((Dict *) uin->d)->getLength();
1080 if (i > 0 && i <= len)
1081 lua_pushstring(L, ((Dict *) uin->d)->getKey(i - 1));
1082 else
1083 lua_pushnil(L);
1084 return 1;
1087 static int m_Dict_getVal(lua_State * L)
1089 int i, len;
1090 udstruct *uin, *uout;
1091 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
1092 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1093 pdfdoc_changed_error(L);
1094 i = luaL_checkint(L, 2);
1095 len = ((Dict *) uin->d)->getLength();
1096 if (i > 0 && i <= len) {
1097 uout = new_Object_userdata(L);
1098 uout->d = new Object();
1099 ((Dict *) uin->d)->getVal(i - 1, (Object *) uout->d);
1100 uout->atype = ALLOC_LEPDF;
1101 uout->pc = uin->pc;
1102 uout->pd = uin->pd;
1103 } else
1104 lua_pushnil(L);
1105 return 1;
1108 static int m_Dict_getValNF(lua_State * L)
1110 int i, len;
1111 udstruct *uin, *uout;
1112 uin = (udstruct *) luaL_checkudata(L, 1, M_Dict);
1113 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1114 pdfdoc_changed_error(L);
1115 i = luaL_checkint(L, 2);
1116 len = ((Dict *) uin->d)->getLength();
1117 if (i > 0 && i <= len) {
1118 uout = new_Object_userdata(L);
1119 uout->d = new Object();
1120 ((Dict *) uin->d)->getValNF(i - 1, (Object *) uout->d);
1121 uout->atype = ALLOC_LEPDF;
1122 uout->pc = uin->pc;
1123 uout->pd = uin->pd;
1124 } else
1125 lua_pushnil(L);
1126 return 1;
1129 m_poppler_check_string(Dict, hasKey);
1131 m_poppler__tostring(Dict);
1133 static const struct luaL_Reg Dict_m[] = {
1134 {"incRef", m_Dict_incRef},
1135 {"decRef", m_Dict_decRef},
1136 {"getLength", m_Dict_getLength},
1137 {"add", m_Dict_add},
1138 {"set", m_Dict_set},
1139 {"remove", m_Dict_remove},
1140 {"is", m_Dict_is},
1141 {"lookup", m_Dict_lookup},
1142 {"lookupNF", m_Dict_lookupNF},
1143 {"lookupInt", m_Dict_lookupInt},
1144 {"getKey", m_Dict_getKey},
1145 {"getVal", m_Dict_getVal},
1146 {"getValNF", m_Dict_getValNF},
1147 {"hasKey", m_Dict_hasKey},
1148 {"__tostring", m_Dict__tostring},
1149 {NULL, NULL} // sentinel
1152 //**********************************************************************
1153 // EmbFile
1155 m_poppler_get_INT(EmbFile, size);
1156 m_poppler_get_GOOSTRING(EmbFile, modDate);
1157 m_poppler_get_GOOSTRING(EmbFile, createDate);
1158 m_poppler_get_GOOSTRING(EmbFile, checksum);
1159 m_poppler_get_GOOSTRING(EmbFile, mimeType);
1161 m_poppler_get_BOOL(EmbFile, isOk);
1163 static int m_EmbFile_save(lua_State * L)
1165 const char *s;
1166 size_t len;
1167 udstruct *uin;
1168 uin = (udstruct *) luaL_checkudata(L, 1, M_EmbFile);
1169 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1170 pdfdoc_changed_error(L);
1171 s = luaL_checklstring(L, 2, &len);
1172 if (((EmbFile *) uin->d)->save(s))
1173 lua_pushboolean(L, 1);
1174 else
1175 lua_pushboolean(L, 0);
1176 return 1;
1179 m_poppler__tostring(EmbFile);
1181 static const struct luaL_Reg EmbFile_m[] = {
1182 {"size", m_EmbFile_size},
1183 {"modDate", m_EmbFile_modDate},
1184 {"createDate", m_EmbFile_createDate},
1185 {"checksum", m_EmbFile_checksum},
1186 {"mimeType", m_EmbFile_mimeType},
1187 {"isOk", m_EmbFile_isOk},
1188 {"save", m_EmbFile_save},
1189 {"__tostring", m_EmbFile__tostring},
1190 {NULL, NULL} // sentinel
1193 //**********************************************************************
1194 // FileSpec
1196 m_poppler_get_BOOL(FileSpec, isOk);
1197 m_poppler_get_GOOSTRING(FileSpec, getFileName);
1198 m_poppler_get_GOOSTRING(FileSpec, getFileNameForPlatform);
1199 m_poppler_get_GOOSTRING(FileSpec, getDescription);
1201 static int m_FileSpec_getEmbeddedFile(lua_State * L)
1203 udstruct *uin, *uout;
1204 uin = (udstruct *) luaL_checkudata(L, 1, M_FileSpec);
1205 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1206 pdfdoc_changed_error(L);
1207 uout = new_EmbFile_userdata(L);
1208 uout->d = ((FileSpec *) uin->d)->getEmbeddedFile();
1209 uout->pc = uin->pc;
1210 uout->pd = uin->pd;
1211 return 1;
1214 m_poppler__tostring(FileSpec);
1216 static const struct luaL_Reg FileSpec_m[] = {
1217 {"isOk", m_FileSpec_isOk},
1218 {"getFileName", m_FileSpec_getFileName},
1219 {"getFileNameForPlatform", m_FileSpec_getFileNameForPlatform},
1220 {"getDescription", m_FileSpec_getDescription},
1221 {"getEmbeddedFile", m_FileSpec_getEmbeddedFile},
1222 {"__tostring", m_FileSpec__tostring},
1223 {NULL, NULL} // sentinel
1226 //**********************************************************************
1227 // GooString
1229 static int m_GooString__tostring(lua_State * L)
1231 udstruct *uin;
1232 uin = (udstruct *) luaL_checkudata(L, 1, M_GooString);
1233 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1234 pdfdoc_changed_error(L);
1235 lua_pushlstring(L, ((GooString *) uin->d)->getCString(),
1236 ((GooString *) uin->d)->getLength());
1237 return 1;
1240 static const struct luaL_Reg GooString_m[] = {
1241 {"__tostring", m_GooString__tostring},
1242 {NULL, NULL} // sentinel
1245 //**********************************************************************
1246 // LinkDest
1248 static const char *LinkDestKindNames[] =
1249 { "XYZ", "Fit", "FitH", "FitV", "FitR", "FitB", "FitBH", "FitBV", NULL };
1251 m_poppler_get_BOOL(LinkDest, isOk);
1253 static int m_LinkDest_getKind(lua_State * L)
1255 int i;
1256 udstruct *uin;
1257 uin = (udstruct *) luaL_checkudata(L, 1, M_LinkDest);
1258 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1259 pdfdoc_changed_error(L);
1260 i = (int) ((LinkDest *) uin->d)->getKind();
1261 lua_pushinteger(L, i);
1262 return 1;
1265 static int m_LinkDest_getKindName(lua_State * L)
1267 int i;
1268 udstruct *uin;
1269 uin = (udstruct *) luaL_checkudata(L, 1, M_LinkDest);
1270 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1271 pdfdoc_changed_error(L);
1272 i = (int) ((LinkDest *) uin->d)->getKind();
1273 lua_pushstring(L, LinkDestKindNames[i]);
1274 return 1;
1277 m_poppler_get_BOOL(LinkDest, isPageRef);
1278 m_poppler_get_INT(LinkDest, getPageNum);
1280 static int m_LinkDest_getPageRef(lua_State * L)
1282 udstruct *uin, *uout;
1283 uin = (udstruct *) luaL_checkudata(L, 1, M_LinkDest);
1284 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1285 pdfdoc_changed_error(L);
1286 uout = new_Ref_userdata(L);
1287 uout->d = (Ref *) gmalloc(sizeof(Ref));
1288 ((Ref *) uout->d)->num = ((LinkDest *) uin->d)->getPageRef().num;
1289 ((Ref *) uout->d)->gen = ((LinkDest *) uin->d)->getPageRef().gen;
1290 uout->atype = ALLOC_LEPDF;
1291 uout->pc = uin->pc;
1292 uout->pd = uin->pd;
1293 return 1;
1296 m_poppler_get_DOUBLE(LinkDest, getLeft);
1297 m_poppler_get_DOUBLE(LinkDest, getBottom);
1298 m_poppler_get_DOUBLE(LinkDest, getRight);
1299 m_poppler_get_DOUBLE(LinkDest, getTop);
1300 m_poppler_get_DOUBLE(LinkDest, getZoom);
1301 m_poppler_get_BOOL(LinkDest, getChangeLeft);
1302 m_poppler_get_BOOL(LinkDest, getChangeTop);
1303 m_poppler_get_BOOL(LinkDest, getChangeZoom);
1305 m_poppler__tostring(LinkDest);
1307 static const struct luaL_Reg LinkDest_m[] = {
1308 {"isOk", m_LinkDest_isOk},
1309 {"getKind", m_LinkDest_getKind},
1310 {"getKindName", m_LinkDest_getKindName}, // not poppler
1311 {"isPageRef", m_LinkDest_isPageRef},
1312 {"getPageNum", m_LinkDest_getPageNum},
1313 {"getPageRef", m_LinkDest_getPageRef},
1314 {"getLeft", m_LinkDest_getLeft},
1315 {"getBottom", m_LinkDest_getBottom},
1316 {"getRight", m_LinkDest_getRight},
1317 {"getTop", m_LinkDest_getTop},
1318 {"getZoom", m_LinkDest_getZoom},
1319 {"getChangeLeft", m_LinkDest_getChangeLeft},
1320 {"getChangeTop", m_LinkDest_getChangeTop},
1321 {"getChangeZoom", m_LinkDest_getChangeZoom},
1322 {"__tostring", m_LinkDest__tostring},
1323 {NULL, NULL} // sentinel
1326 //**********************************************************************
1327 // Links
1329 m_poppler_get_INT(Links, getNumLinks);
1331 m_poppler__tostring(Links);
1333 static const struct luaL_Reg Links_m[] = {
1334 {"getNumLinks", m_Links_getNumLinks},
1335 //{"getLink", m_Links_getLink},
1336 {"__tostring", m_Links__tostring},
1337 {NULL, NULL} // sentinel
1340 //**********************************************************************
1341 // Object
1343 #ifdef HAVE_OBJECT_INITCMD_CONST_CHARP
1344 #define CHARP_CAST
1345 #else
1346 // must cast arg of Object::initCmd, Object::isStream, and Object::streamIs
1347 // from 'const char *' to 'char *', although they are not modified.
1348 #define CHARP_CAST (char *)
1349 #endif
1351 // Special type checking.
1352 #define m_Object_isType_(function, cast) \
1353 static int m_Object_##function(lua_State * L) \
1355 udstruct *uin; \
1356 uin = (udstruct *) luaL_checkudata(L, 1, M_Object); \
1357 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
1358 pdfdoc_changed_error(L); \
1359 if (lua_gettop(L) >= 2) { \
1360 if (lua_isstring(L, 2) \
1361 && ((Object *) uin->d)->function(cast lua_tostring(L, 2))) \
1362 lua_pushboolean(L, 1); \
1363 else \
1364 lua_pushboolean(L, 0); \
1365 } else { \
1366 if (((Object *) uin->d)->function()) \
1367 lua_pushboolean(L, 1); \
1368 else \
1369 lua_pushboolean(L, 0); \
1371 return 1; \
1373 #define m_Object_isType(function) m_Object_isType_(function, )
1374 #define m_Object_isType_nonconst(function) m_Object_isType_(function, CHARP_CAST)
1376 static int m_Object_initBool(lua_State * L)
1378 udstruct *uin;
1379 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1380 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1381 pdfdoc_changed_error(L);
1382 luaL_checktype(L, 2, LUA_TBOOLEAN);
1383 if (lua_toboolean(L, 2) != 0)
1384 ((Object *) uin->d)->initBool(gTrue);
1385 else
1386 ((Object *) uin->d)->initBool(gFalse);
1387 return 0;
1390 static int m_Object_initInt(lua_State * L)
1392 int i;
1393 udstruct *uin;
1394 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1395 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1396 pdfdoc_changed_error(L);
1397 i = luaL_checkint(L, 2);
1398 ((Object *) uin->d)->initInt(i);
1399 return 0;
1402 static int m_Object_initReal(lua_State * L)
1404 double d;
1405 udstruct *uin;
1406 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1407 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1408 pdfdoc_changed_error(L);
1409 d = luaL_checknumber(L, 2);
1410 ((Object *) uin->d)->initReal(d);
1411 return 0;
1414 static int m_Object_initString(lua_State * L)
1416 GooString *gs;
1417 const char *s;
1418 size_t len;
1419 udstruct *uin;
1420 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1421 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1422 pdfdoc_changed_error(L);
1423 s = luaL_checklstring(L, 2, &len);
1424 gs = new GooString(s, len);
1425 ((Object *) uin->d)->initString(gs);
1426 return 0;
1429 static int m_Object_initName(lua_State * L)
1431 const char *s;
1432 udstruct *uin;
1433 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1434 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1435 pdfdoc_changed_error(L);
1436 s = luaL_checkstring(L, 2);
1437 ((Object *) uin->d)->initName(s);
1438 return 0;
1441 static int m_Object_initNull(lua_State * L)
1443 udstruct *uin;
1444 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1445 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1446 pdfdoc_changed_error(L);
1447 ((Object *) uin->d)->initNull();
1448 return 0;
1451 static int m_Object_initArray(lua_State * L)
1453 udstruct *uin, *uxref;
1454 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1455 uxref = (udstruct *) luaL_checkudata(L, 2, M_XRef);
1456 if (uin->pd != NULL && uxref->pd != NULL && uin->pd != uxref->pd)
1457 pdfdoc_differs_error(L);
1458 if ((uin->pd != NULL && uin->pd->pc != uin->pc)
1459 || (uxref->pd != NULL && uxref->pd->pc != uxref->pc))
1460 pdfdoc_changed_error(L);
1461 ((Object *) uin->d)->initArray((XRef *) uxref->d);
1462 return 0;
1465 // TODO: decide betweeen
1466 // Object *initDict(XRef *xref);
1467 // Object *initDict(Dict *dictA);
1469 static int m_Object_initDict(lua_State * L)
1471 udstruct *uin, *uxref;
1472 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1473 uxref = (udstruct *) luaL_checkudata(L, 2, M_XRef);
1474 if (uin->pd != NULL && uxref->pd != NULL && uin->pd != uxref->pd)
1475 pdfdoc_differs_error(L);
1476 if ((uin->pd != NULL && uin->pd->pc != uin->pc)
1477 || (uxref->pd != NULL && uxref->pd->pc != uxref->pc))
1478 pdfdoc_changed_error(L);
1479 ((Object *) uin->d)->initDict((XRef *) uxref->d);
1480 return 0;
1483 static int m_Object_initStream(lua_State * L)
1485 udstruct *uin, *ustream;
1486 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1487 ustream = (udstruct *) luaL_checkudata(L, 2, M_Stream);
1488 if (uin->pd != NULL && ustream->pd != NULL && uin->pd != ustream->pd)
1489 pdfdoc_differs_error(L);
1490 if ((uin->pd != NULL && uin->pd->pc != uin->pc)
1491 || (ustream->pd != NULL && ustream->pd->pc != ustream->pc))
1492 pdfdoc_changed_error(L);
1493 ((Object *) uin->d)->initStream((Stream *) ustream->d);
1494 return 0;
1497 static int m_Object_initRef(lua_State * L)
1499 int num, gen;
1500 udstruct *uin;
1501 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1502 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1503 pdfdoc_changed_error(L);
1504 num = luaL_checkint(L, 2);
1505 gen = luaL_checkint(L, 3);
1506 ((Object *) uin->d)->initRef(num, gen);
1507 return 0;
1510 static int m_Object_initCmd(lua_State * L)
1512 const char *s;
1513 udstruct *uin;
1514 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1515 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1516 pdfdoc_changed_error(L);
1517 s = luaL_checkstring(L, 2);
1518 ((Object *) uin->d)->initCmd(CHARP_CAST s);
1519 return 0;
1522 static int m_Object_initError(lua_State * L)
1524 udstruct *uin;
1525 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1526 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1527 pdfdoc_changed_error(L);
1528 ((Object *) uin->d)->initError();
1529 return 0;
1532 static int m_Object_initEOF(lua_State * L)
1534 udstruct *uin;
1535 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1536 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1537 pdfdoc_changed_error(L);
1538 ((Object *) uin->d)->initEOF();
1539 return 0;
1542 static int m_Object_fetch(lua_State * L)
1544 udstruct *uin, *uxref, *uout;
1545 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1546 uxref = (udstruct *) luaL_checkudata(L, 2, M_XRef);
1547 if (uin->pd != NULL && uxref->pd != NULL && uin->pd != uxref->pd)
1548 pdfdoc_differs_error(L);
1549 if ((uin->pd != NULL && uin->pd->pc != uin->pc)
1550 || (uxref->pd != NULL && uxref->pd->pc != uxref->pc))
1551 pdfdoc_changed_error(L);
1552 uout = new_Object_userdata(L);
1553 uout->d = new Object();
1554 ((Object *) uin->d)->fetch((XRef *) uxref->d, (Object *) uout->d);
1555 uout->atype = ALLOC_LEPDF;
1556 uout->pc = uin->pc;
1557 uout->pd = uin->pd;
1558 return 1;
1561 static int m_Object_getType(lua_State * L)
1563 ObjType t;
1564 udstruct *uin;
1565 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1566 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1567 pdfdoc_changed_error(L);
1568 t = ((Object *) uin->d)->getType();
1569 lua_pushinteger(L, (int) t);
1570 return 1;
1573 static int m_Object_getTypeName(lua_State * L)
1575 udstruct *uin;
1576 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1577 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1578 pdfdoc_changed_error(L);
1579 lua_pushstring(L, ((Object *) uin->d)->getTypeName());
1580 return 1;
1583 m_poppler_get_BOOL(Object, isBool);
1584 m_poppler_get_BOOL(Object, isInt);
1585 m_poppler_get_BOOL(Object, isReal);
1586 m_poppler_get_BOOL(Object, isNum);
1587 m_poppler_get_BOOL(Object, isString);
1588 m_Object_isType(isName);
1589 m_poppler_get_BOOL(Object, isNull);
1590 m_poppler_get_BOOL(Object, isArray);
1591 m_Object_isType(isDict);
1592 m_Object_isType_nonconst(isStream);
1593 m_poppler_get_BOOL(Object, isRef);
1594 m_Object_isType(isCmd);
1595 m_poppler_get_BOOL(Object, isError);
1596 m_poppler_get_BOOL(Object, isEOF);
1597 m_poppler_get_BOOL(Object, isNone);
1599 static int m_Object_getBool(lua_State * L)
1601 udstruct *uin;
1602 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1603 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1604 pdfdoc_changed_error(L);
1605 if (((Object *) uin->d)->isBool()) {
1606 if (((Object *) uin->d)->getBool())
1607 lua_pushboolean(L, 1);
1608 else
1609 lua_pushboolean(L, 0);
1610 } else
1611 lua_pushnil(L);
1612 return 1;
1615 static int m_Object_getInt(lua_State * L)
1617 udstruct *uin;
1618 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1619 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1620 pdfdoc_changed_error(L);
1621 if (((Object *) uin->d)->isInt())
1622 lua_pushinteger(L, ((Object *) uin->d)->getInt());
1623 else
1624 lua_pushnil(L);
1625 return 1;
1628 static int m_Object_getReal(lua_State * L)
1630 udstruct *uin;
1631 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1632 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1633 pdfdoc_changed_error(L);
1634 if (((Object *) uin->d)->isReal())
1635 lua_pushnumber(L, ((Object *) uin->d)->getReal()); /* float */
1636 else
1637 lua_pushnil(L);
1638 return 1;
1641 static int m_Object_getNum(lua_State * L)
1643 udstruct *uin;
1644 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1645 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1646 pdfdoc_changed_error(L);
1647 if (((Object *) uin->d)->isNum())
1648 lua_pushnumber(L, ((Object *) uin->d)->getNum()); /* integer or float */
1649 else
1650 lua_pushnil(L);
1651 return 1;
1654 static int m_Object_getString(lua_State * L)
1656 GooString *gs;
1657 udstruct *uin;
1658 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1659 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1660 pdfdoc_changed_error(L);
1661 if (((Object *) uin->d)->isString()) {
1662 gs = ((Object *) uin->d)->getString();
1663 lua_pushlstring(L, gs->getCString(), gs->getLength());
1664 } else
1665 lua_pushnil(L);
1666 return 1;
1669 static int m_Object_getName(lua_State * L)
1671 udstruct *uin;
1672 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1673 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1674 pdfdoc_changed_error(L);
1675 if (((Object *) uin->d)->isName())
1676 lua_pushstring(L, ((Object *) uin->d)->getName());
1677 else
1678 lua_pushnil(L);
1679 return 1;
1682 static int m_Object_getArray(lua_State * L)
1684 udstruct *uin, *uout;
1685 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1686 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1687 pdfdoc_changed_error(L);
1688 if (((Object *) uin->d)->isArray()) {
1689 uout = new_Array_userdata(L);
1690 uout->d = ((Object *) uin->d)->getArray();
1691 uout->pc = uin->pc;
1692 uout->pd = uin->pd;
1693 } else
1694 lua_pushnil(L);
1695 return 1;
1698 static int m_Object_getDict(lua_State * L)
1700 udstruct *uin, *uout;
1701 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1702 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1703 pdfdoc_changed_error(L);
1704 if (((Object *) uin->d)->isDict()) {
1705 uout = new_Dict_userdata(L);
1706 uout->d = ((Object *) uin->d)->getDict();
1707 uout->pc = uin->pc;
1708 uout->pd = uin->pd;
1709 } else
1710 lua_pushnil(L);
1711 return 1;
1714 static int m_Object_getStream(lua_State * L)
1716 udstruct *uin, *uout;
1717 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1718 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1719 pdfdoc_changed_error(L);
1720 if (((Object *) uin->d)->isStream()) {
1721 uout = new_Stream_userdata(L);
1722 uout->d = ((Object *) uin->d)->getStream();
1723 uout->pc = uin->pc;
1724 uout->pd = uin->pd;
1725 } else
1726 lua_pushnil(L);
1727 return 1;
1730 static int m_Object_getRef(lua_State * L)
1732 udstruct *uin, *uout;
1733 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1734 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1735 pdfdoc_changed_error(L);
1736 if (((Object *) uin->d)->isRef()) {
1737 uout = new_Ref_userdata(L);
1738 uout->d = (Ref *) gmalloc(sizeof(Ref));
1739 ((Ref *) uout->d)->num = ((Object *) uin->d)->getRef().num;
1740 ((Ref *) uout->d)->gen = ((Object *) uin->d)->getRef().gen;
1741 uout->atype = ALLOC_LEPDF;
1742 uout->pc = uin->pc;
1743 uout->pd = uin->pd;
1744 } else
1745 lua_pushnil(L);
1746 return 1;
1749 static int m_Object_getRefNum(lua_State * L)
1751 int i;
1752 udstruct *uin;
1753 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1754 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1755 pdfdoc_changed_error(L);
1756 if (((Object *) uin->d)->isRef()) {
1757 i = ((Object *) uin->d)->getRef().num;
1758 lua_pushinteger(L, i);
1759 } else
1760 lua_pushnil(L);
1761 return 1;
1764 static int m_Object_getRefGen(lua_State * L)
1766 int i;
1767 udstruct *uin;
1768 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1769 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1770 pdfdoc_changed_error(L);
1771 if (((Object *) uin->d)->isRef()) {
1772 i = ((Object *) uin->d)->getRef().gen;
1773 lua_pushinteger(L, i);
1774 } else
1775 lua_pushnil(L);
1776 return 1;
1779 static int m_Object_getCmd(lua_State * L)
1781 udstruct *uin;
1782 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1783 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1784 pdfdoc_changed_error(L);
1785 if (((Object *) uin->d)->isCmd())
1786 lua_pushstring(L, ((Object *) uin->d)->getCmd());
1787 else
1788 lua_pushnil(L);
1789 return 1;
1792 static int m_Object_arrayGetLength(lua_State * L)
1794 int len;
1795 udstruct *uin;
1796 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1797 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1798 pdfdoc_changed_error(L);
1799 if (((Object *) uin->d)->isArray()) {
1800 len = ((Object *) uin->d)->arrayGetLength();
1801 lua_pushinteger(L, len);
1802 } else
1803 lua_pushnil(L);
1804 return 1;
1807 static int m_Object_arrayAdd(lua_State * L)
1809 udstruct *uin, *uobj;
1810 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1811 uobj = (udstruct *) luaL_checkudata(L, 2, M_Object);
1812 if (uin->pd != NULL && uobj->pd != NULL && uin->pd != uobj->pd)
1813 pdfdoc_differs_error(L);
1814 if ((uin->pd != NULL && uin->pd->pc != uin->pc)
1815 || (uobj->pd != NULL && uobj->pd->pc != uobj->pd->pc))
1816 pdfdoc_changed_error(L);
1817 if (!((Object *) uin->d)->isArray())
1818 luaL_error(L, "Object is not an Array");
1819 ((Object *) uin->d)->arrayAdd((Object *) uobj->d);
1820 return 0;
1823 static int m_Object_arrayGet(lua_State * L)
1825 int i, len;
1826 udstruct *uin, *uout;
1827 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1828 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1829 pdfdoc_changed_error(L);
1830 i = luaL_checkint(L, 2);
1831 if (((Object *) uin->d)->isArray()) {
1832 len = ((Object *) uin->d)->arrayGetLength();
1833 if (i > 0 && i <= len) {
1834 uout = new_Object_userdata(L);
1835 uout->d = new Object();
1836 ((Object *) uin->d)->arrayGet(i - 1, (Object *) uout->d);
1837 uout->atype = ALLOC_LEPDF;
1838 uout->pc = uin->pc;
1839 uout->pd = uin->pd;
1840 } else
1841 lua_pushnil(L);
1842 } else
1843 lua_pushnil(L);
1844 return 1;
1847 static int m_Object_arrayGetNF(lua_State * L)
1849 int i, len;
1850 udstruct *uin, *uout;
1851 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1852 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1853 pdfdoc_changed_error(L);
1854 i = luaL_checkint(L, 2);
1855 if (((Object *) uin->d)->isArray()) {
1856 len = ((Object *) uin->d)->arrayGetLength();
1857 if (i > 0 && i <= len) {
1858 uout = new_Object_userdata(L);
1859 uout->d = new Object();
1860 ((Object *) uin->d)->arrayGetNF(i - 1, (Object *) uout->d);
1861 uout->atype = ALLOC_LEPDF;
1862 uout->pc = uin->pc;
1863 uout->pd = uin->pd;
1864 } else
1865 lua_pushnil(L);
1866 } else
1867 lua_pushnil(L);
1868 return 1;
1871 static int m_Object_dictGetLength(lua_State * L)
1873 int len;
1874 udstruct *uin;
1875 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1876 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1877 pdfdoc_changed_error(L);
1878 if (((Object *) uin->d)->isDict()) {
1879 len = ((Object *) uin->d)->dictGetLength();
1880 lua_pushinteger(L, len);
1881 } else
1882 lua_pushnil(L);
1883 return 1;
1886 static int m_Object_dictAdd(lua_State * L)
1888 const char *s;
1889 udstruct *uin, *uobj;
1890 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1891 s = luaL_checkstring(L, 2);
1892 uobj = (udstruct *) luaL_checkudata(L, 3, M_Object);
1893 if (uin->pd != NULL && uobj->pd != NULL && uin->pd != uobj->pd)
1894 pdfdoc_differs_error(L);
1895 if ((uin->pd != NULL && uin->pd->pc != uin->pc)
1896 || (uobj->pd != NULL && uobj->pd->pc != uobj->pd->pc))
1897 pdfdoc_changed_error(L);
1898 if (!((Object *) uin->d)->isDict())
1899 luaL_error(L, "Object is not a Dict");
1900 ((Object *) uin->d)->dictAdd(copyString(s), (Object *) uobj->d);
1901 return 0;
1904 static int m_Object_dictSet(lua_State * L)
1906 const char *s;
1907 udstruct *uin, *uobj;
1908 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1909 s = luaL_checkstring(L, 2);
1910 uobj = (udstruct *) luaL_checkudata(L, 3, M_Object);
1911 if (uin->pd != NULL && uobj->pd != NULL && uin->pd != uobj->pd)
1912 pdfdoc_differs_error(L);
1913 if ((uin->pd != NULL && uin->pd->pc != uin->pc)
1914 || (uobj->pd != NULL && uobj->pd->pc != uobj->pd->pc))
1915 pdfdoc_changed_error(L);
1916 if (!((Object *) uin->d)->isDict())
1917 luaL_error(L, "Object is not a Dict");
1918 ((Object *) uin->d)->dictSet(s, (Object *) uobj->d);
1919 return 0;
1922 static int m_Object_dictLookup(lua_State * L)
1924 const char *s;
1925 udstruct *uin, *uout;
1926 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1927 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1928 pdfdoc_changed_error(L);
1929 s = luaL_checkstring(L, 2);
1930 if (((Object *) uin->d)->isDict()) {
1931 uout = new_Object_userdata(L);
1932 uout->d = new Object();
1933 ((Object *) uin->d)->dictLookup(s, (Object *) uout->d);
1934 uout->atype = ALLOC_LEPDF;
1935 uout->pc = uin->pc;
1936 uout->pd = uin->pd;
1937 } else
1938 lua_pushnil(L);
1939 return 1;
1942 static int m_Object_dictLookupNF(lua_State * L)
1944 const char *s;
1945 udstruct *uin, *uout;
1946 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1947 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1948 pdfdoc_changed_error(L);
1949 s = luaL_checkstring(L, 2);
1950 if (((Object *) uin->d)->isDict()) {
1951 uout = new_Object_userdata(L);
1952 uout->d = new Object();
1953 ((Object *) uin->d)->dictLookupNF(s, (Object *) uout->d);
1954 uout->atype = ALLOC_LEPDF;
1955 uout->pc = uin->pc;
1956 uout->pd = uin->pd;
1957 } else
1958 lua_pushnil(L);
1959 return 1;
1962 static int m_Object_dictGetKey(lua_State * L)
1964 int i, len;
1965 udstruct *uin;
1966 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1967 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1968 pdfdoc_changed_error(L);
1969 i = luaL_checkint(L, 2);
1970 if (((Object *) uin->d)->isDict()) {
1971 len = ((Object *) uin->d)->dictGetLength();
1972 if (i > 0 && i <= len)
1973 lua_pushstring(L, ((Object *) uin->d)->dictGetKey(i - 1));
1974 else
1975 lua_pushnil(L);
1976 } else
1977 lua_pushnil(L);
1978 return 1;
1981 static int m_Object_dictGetVal(lua_State * L)
1983 int i, len;
1984 udstruct *uin, *uout;
1985 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
1986 if (uin->pd != NULL && uin->pd->pc != uin->pc)
1987 pdfdoc_changed_error(L);
1988 i = luaL_checkint(L, 2);
1989 if (((Object *) uin->d)->isDict()) {
1990 len = ((Object *) uin->d)->dictGetLength();
1991 if (i > 0 && i <= len) {
1992 uout = new_Object_userdata(L);
1993 uout->d = new Object();
1994 ((Object *) uin->d)->dictGetVal(i - 1, (Object *) uout->d);
1995 uout->atype = ALLOC_LEPDF;
1996 uout->pc = uin->pc;
1997 uout->pd = uin->pd;
1998 } else
1999 lua_pushnil(L);
2000 } else
2001 lua_pushnil(L);
2002 return 1;
2005 static int m_Object_dictGetValNF(lua_State * L)
2007 int i, len;
2008 udstruct *uin, *uout;
2009 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
2010 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2011 pdfdoc_changed_error(L);
2012 i = luaL_checkint(L, 2);
2013 if (((Object *) uin->d)->isDict()) {
2014 len = ((Object *) uin->d)->dictGetLength();
2015 if (i > 0 && i <= len) {
2016 uout = new_Object_userdata(L);
2017 uout->d = new Object();
2018 ((Object *) uin->d)->dictGetValNF(i - 1, (Object *) uout->d);
2019 uout->atype = ALLOC_LEPDF;
2020 uout->pc = uin->pc;
2021 uout->pd = uin->pd;
2022 } else
2023 lua_pushnil(L);
2024 } else
2025 lua_pushnil(L);
2026 return 1;
2029 static int m_Object_streamIs(lua_State * L)
2031 const char *s;
2032 udstruct *uin;
2033 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
2034 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2035 pdfdoc_changed_error(L);
2036 s = luaL_checkstring(L, 2);
2037 if (((Object *) uin->d)->isStream()) {
2038 if (((Object *) uin->d)->streamIs(CHARP_CAST s))
2039 lua_pushboolean(L, 1);
2040 else
2041 lua_pushboolean(L, 0);
2042 } else
2043 lua_pushnil(L);
2044 return 1;
2047 static int m_Object_streamReset(lua_State * L)
2049 udstruct *uin;
2050 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
2051 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2052 pdfdoc_changed_error(L);
2053 if (((Object *) uin->d)->isStream())
2054 ((Object *) uin->d)->streamReset();
2055 return 0;
2058 static int m_Object_streamGetChar(lua_State * L)
2060 int i;
2061 udstruct *uin;
2062 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
2063 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2064 pdfdoc_changed_error(L);
2065 if (((Object *) uin->d)->isStream()) {
2066 i = ((Object *) uin->d)->streamGetChar();
2067 lua_pushinteger(L, i);
2068 } else
2069 lua_pushnil(L);
2070 return 1;
2073 static int m_Object_streamLookChar(lua_State * L)
2075 int i;
2076 udstruct *uin;
2077 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
2078 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2079 pdfdoc_changed_error(L);
2080 if (((Object *) uin->d)->isStream()) {
2081 i = ((Object *) uin->d)->streamLookChar();
2082 lua_pushinteger(L, i);
2083 } else
2084 lua_pushnil(L);
2085 return 1;
2088 static int m_Object_streamGetPos(lua_State * L)
2090 int i;
2091 udstruct *uin;
2092 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
2093 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2094 pdfdoc_changed_error(L);
2095 if (((Object *) uin->d)->isStream()) {
2096 i = (int) ((Object *) uin->d)->streamGetPos();
2097 lua_pushinteger(L, i);
2098 } else
2099 lua_pushnil(L);
2100 return 1;
2103 static int m_Object_streamSetPos(lua_State * L)
2105 int i;
2106 udstruct *uin;
2107 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
2108 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2109 pdfdoc_changed_error(L);
2110 i = luaL_checkint(L, 2);
2111 if (((Object *) uin->d)->isStream())
2112 ((Object *) uin->d)->streamSetPos(i);
2113 return 0;
2116 static int m_Object_streamGetDict(lua_State * L)
2118 udstruct *uin, *uout;
2119 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
2120 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2121 pdfdoc_changed_error(L);
2122 if (((Object *) uin->d)->isStream()) {
2123 uout = new_Dict_userdata(L);
2124 uout->d = ((Object *) uin->d)->streamGetDict();
2125 uout->pc = uin->pc;
2126 uout->pd = uin->pd;
2127 } else
2128 lua_pushnil(L);
2129 return 1;
2132 static int m_Object__gc(lua_State * L)
2134 udstruct *uin;
2135 uin = (udstruct *) luaL_checkudata(L, 1, M_Object);
2136 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2137 pdfdoc_changed_error(L);
2138 #ifdef DEBUG
2139 printf("\n===== Object GC ===== uin=<%p>\n", uin);
2140 #endif
2141 if (uin->atype == ALLOC_LEPDF) {
2142 // free() seems to collide with the lua gc
2143 //((Object *) uin->d)->free();
2144 delete(Object *) uin->d;
2146 return 0;
2149 m_poppler__tostring(Object);
2151 static const struct luaL_Reg Object_m[] = {
2152 {"initBool", m_Object_initBool},
2153 {"initInt", m_Object_initInt},
2154 {"initReal", m_Object_initReal},
2155 {"initString", m_Object_initString},
2156 {"initName", m_Object_initName},
2157 {"initNull", m_Object_initNull},
2158 {"initArray", m_Object_initArray},
2159 {"initDict", m_Object_initDict},
2160 {"initStream", m_Object_initStream},
2161 {"initRef", m_Object_initRef},
2162 {"initCmd", m_Object_initCmd},
2163 {"initError", m_Object_initError},
2164 {"initEOF", m_Object_initEOF},
2165 // {"copy", m_Object_copy},
2166 {"fetch", m_Object_fetch},
2167 {"getType", m_Object_getType},
2168 {"getTypeName", m_Object_getTypeName},
2169 {"isBool", m_Object_isBool},
2170 {"isInt", m_Object_isInt},
2171 {"isReal", m_Object_isReal},
2172 {"isNum", m_Object_isNum},
2173 {"isString", m_Object_isString},
2174 {"isName", m_Object_isName},
2175 {"isNull", m_Object_isNull},
2176 {"isArray", m_Object_isArray},
2177 {"isDict", m_Object_isDict},
2178 {"isStream", m_Object_isStream},
2179 {"isRef", m_Object_isRef},
2180 {"isCmd", m_Object_isCmd},
2181 {"isError", m_Object_isError},
2182 {"isEOF", m_Object_isEOF},
2183 {"isNone", m_Object_isNone},
2184 {"getBool", m_Object_getBool},
2185 {"getInt", m_Object_getInt},
2186 {"getReal", m_Object_getReal},
2187 {"getNum", m_Object_getNum},
2188 {"getString", m_Object_getString},
2189 {"getName", m_Object_getName},
2190 {"getArray", m_Object_getArray},
2191 {"getDict", m_Object_getDict},
2192 {"getStream", m_Object_getStream},
2193 {"getRef", m_Object_getRef},
2194 {"getRefNum", m_Object_getRefNum},
2195 {"getRefGen", m_Object_getRefGen},
2196 {"getCmd", m_Object_getCmd},
2197 {"arrayGetLength", m_Object_arrayGetLength},
2198 {"arrayAdd", m_Object_arrayAdd},
2199 {"arrayGet", m_Object_arrayGet},
2200 {"arrayGetNF", m_Object_arrayGetNF},
2201 {"dictGetLength", m_Object_dictGetLength},
2202 {"dictAdd", m_Object_dictAdd},
2203 {"dictSet", m_Object_dictSet},
2204 {"dictLookup", m_Object_dictLookup},
2205 {"dictLookupNF", m_Object_dictLookupNF},
2206 {"dictGetKey", m_Object_dictGetKey},
2207 {"dictGetVal", m_Object_dictGetVal},
2208 {"dictGetValNF", m_Object_dictGetValNF},
2209 {"streamIs", m_Object_streamIs},
2210 {"streamReset", m_Object_streamReset},
2211 // {"streamClose", m_Object_streamClose},
2212 {"streamGetChar", m_Object_streamGetChar},
2213 {"streamLookChar", m_Object_streamLookChar},
2214 // {"streamGetLine", m_Object_streamGetLine},
2215 {"streamGetPos", m_Object_streamGetPos},
2216 {"streamSetPos", m_Object_streamSetPos},
2217 {"streamGetDict", m_Object_streamGetDict},
2218 {"__tostring", m_Object__tostring},
2219 {"__gc", m_Object__gc}, // finalizer
2220 {NULL, NULL} // sentinel
2223 //**********************************************************************
2224 // Page
2226 m_poppler_get_BOOL(Page, isOk);
2227 m_poppler_get_INT(Page, getNum);
2228 m_poppler_get_poppler(Page, PDFRectangle, getMediaBox);
2229 m_poppler_get_poppler(Page, PDFRectangle, getCropBox);
2230 m_poppler_get_BOOL(Page, isCropped);
2231 m_poppler_get_DOUBLE(Page, getMediaWidth);
2232 m_poppler_get_DOUBLE(Page, getMediaHeight);
2233 m_poppler_get_DOUBLE(Page, getCropWidth);
2234 m_poppler_get_DOUBLE(Page, getCropHeight);
2235 m_poppler_get_poppler(Page, PDFRectangle, getBleedBox);
2236 m_poppler_get_poppler(Page, PDFRectangle, getTrimBox);
2237 m_poppler_get_poppler(Page, PDFRectangle, getArtBox);
2238 m_poppler_get_INT(Page, getRotate);
2239 m_poppler_get_GOOSTRING(Page, getLastModified);
2240 m_poppler_get_poppler(Page, Dict, getBoxColorInfo);
2241 m_poppler_get_poppler(Page, Dict, getGroup);
2242 m_poppler_get_poppler(Page, Stream, getMetadata);
2243 m_poppler_get_poppler(Page, Dict, getPieceInfo);
2244 m_poppler_get_poppler(Page, Dict, getSeparationInfo);
2245 m_poppler_get_poppler(Page, Dict, getResourceDict);
2246 m_poppler_get_OBJECT(Page, getAnnots);
2248 m_poppler_get_OBJECT(Page, getContents);
2250 m_poppler__tostring(Page);
2252 static const struct luaL_Reg Page_m[] = {
2253 {"isOk", m_Page_isOk},
2254 {"getNum", m_Page_getNum},
2255 {"getMediaBox", m_Page_getMediaBox},
2256 {"getCropBox", m_Page_getCropBox},
2257 {"isCropped", m_Page_isCropped},
2258 {"getMediaWidth", m_Page_getMediaWidth},
2259 {"getMediaHeight", m_Page_getMediaHeight},
2260 {"getCropWidth", m_Page_getCropWidth},
2261 {"getCropHeight", m_Page_getCropHeight},
2262 {"getBleedBox", m_Page_getBleedBox},
2263 {"getTrimBox", m_Page_getTrimBox},
2264 {"getArtBox", m_Page_getArtBox},
2265 {"getRotate", m_Page_getRotate},
2266 {"getLastModified", m_Page_getLastModified},
2267 {"getBoxColorInfo", m_Page_getBoxColorInfo},
2268 {"getGroup", m_Page_getGroup},
2269 {"getMetadata", m_Page_getMetadata},
2270 {"getPieceInfo", m_Page_getPieceInfo},
2271 {"getSeparationInfo", m_Page_getSeparationInfo},
2272 {"getResourceDict", m_Page_getResourceDict},
2273 {"getAnnots", m_Page_getAnnots},
2274 {"getContents", m_Page_getContents},
2275 {"__tostring", m_Page__tostring},
2276 {NULL, NULL} // sentinel
2279 //**********************************************************************
2280 // PDFDoc
2282 #define m_PDFDoc_BOOL(function) \
2283 static int m_PDFDoc_##function(lua_State * L) \
2285 udstruct *uin; \
2286 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc); \
2287 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
2288 pdfdoc_changed_error(L); \
2289 if (((PdfDocument *) uin->d)->doc->function()) \
2290 lua_pushboolean(L, 1); \
2291 else \
2292 lua_pushboolean(L, 0); \
2293 return 1; \
2296 #define m_PDFDoc_INT(function) \
2297 static int m_PDFDoc_##function(lua_State * L) \
2299 int i; \
2300 udstruct *uin; \
2301 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc); \
2302 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
2303 pdfdoc_changed_error(L); \
2304 i = ((PdfDocument *) uin->d)->doc->function(); \
2305 lua_pushinteger(L, i); \
2306 return 1; \
2309 m_PDFDoc_BOOL(isOk);
2310 m_PDFDoc_INT(getErrorCode);
2312 static int m_PDFDoc_getFileName(lua_State * L)
2314 GooString *gs;
2315 udstruct *uin;
2316 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2317 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2318 pdfdoc_changed_error(L);
2319 gs = ((PdfDocument *) uin->d)->doc->getFileName();
2320 if (gs != NULL)
2321 lua_pushlstring(L, gs->getCString(), gs->getLength());
2322 else
2323 lua_pushnil(L);
2324 return 1;
2327 static int m_PDFDoc_getErrorCodeName(lua_State * L)
2329 int i;
2330 udstruct *uin;
2331 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2332 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2333 pdfdoc_changed_error(L);
2334 i = ((PdfDocument *) uin->d)->doc->getErrorCode();
2335 lua_pushstring(L, ErrorCodeNames[i]);
2336 return 1;
2339 static int m_PDFDoc_getXRef(lua_State * L)
2341 XRef *xref;
2342 udstruct *uin, *uout;
2343 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2344 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2345 pdfdoc_changed_error(L);
2346 xref = ((PdfDocument *) uin->d)->doc->getXRef();
2347 if (xref->isOk()) {
2348 uout = new_XRef_userdata(L);
2349 uout->d = xref;
2350 uout->pc = uin->pc;
2351 uout->pd = uin->pd;
2352 } else
2353 lua_pushnil(L);
2354 return 1;
2357 static int m_PDFDoc_getCatalog(lua_State * L)
2359 Catalog *cat;
2360 udstruct *uin, *uout;
2361 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2362 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2363 pdfdoc_changed_error(L);
2364 cat = ((PdfDocument *) uin->d)->doc->getCatalog();
2365 if (cat->isOk()) {
2366 uout = new_Catalog_userdata(L);
2367 uout->d = cat;
2368 uout->pc = uin->pc;
2369 uout->pd = uin->pd;
2370 } else
2371 lua_pushnil(L);
2372 return 1;
2375 #define m_PDFDoc_PAGEDIMEN(function) \
2376 static int m_PDFDoc_##function(lua_State * L) \
2378 int i, pages; \
2379 double d; \
2380 udstruct *uin; \
2381 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc); \
2382 if (uin->pd != NULL && uin->pd->pc != uin->pc) \
2383 pdfdoc_changed_error(L); \
2384 i = luaL_checkint(L, 2); \
2385 pages = ((PdfDocument *) uin->d)->doc->getNumPages(); \
2386 if (i > 0 && i <= pages) { \
2387 d = (double) ((PdfDocument *) uin->d)->doc->function(i); \
2388 lua_pushnumber(L, d); /* float */ \
2389 } else \
2390 lua_pushnil(L); \
2391 return 1; \
2394 m_PDFDoc_PAGEDIMEN(getPageMediaWidth);
2395 m_PDFDoc_PAGEDIMEN(getPageMediaHeight);
2396 m_PDFDoc_PAGEDIMEN(getPageCropWidth);
2397 m_PDFDoc_PAGEDIMEN(getPageCropHeight);
2398 m_PDFDoc_INT(getNumPages);
2400 static int m_PDFDoc_readMetadata(lua_State * L)
2402 GooString *gs;
2403 udstruct *uin;
2404 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2405 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2406 pdfdoc_changed_error(L);
2407 if (((PdfDocument *) uin->d)->doc->getCatalog()->isOk()) {
2408 gs = ((PdfDocument *) uin->d)->doc->readMetadata();
2409 if (gs != NULL)
2410 lua_pushlstring(L, gs->getCString(), gs->getLength());
2411 else
2412 lua_pushnil(L);
2413 } else
2414 lua_pushnil(L);
2415 return 1;
2418 static int m_PDFDoc_getStructTreeRoot(lua_State * L)
2420 StructTreeRoot *obj;
2421 udstruct *uin, *uout;
2422 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2423 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2424 pdfdoc_changed_error(L);
2425 if (((PdfDocument *) uin->d)->doc->getCatalog()->isOk()) {
2426 obj = ((PdfDocument *) uin->d)->doc->getStructTreeRoot();
2427 uout = new_StructTreeRoot_userdata(L);
2428 uout->d = obj;
2429 uout->pc = uin->pc;
2430 uout->pd = uin->pd;
2431 } else
2432 lua_pushnil(L);
2433 return 1;
2436 static int m_PDFDoc_findPage(lua_State * L)
2438 int num, gen, i;
2439 udstruct *uin;
2440 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2441 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2442 pdfdoc_changed_error(L);
2443 num = luaL_checkint(L, 2);
2444 gen = luaL_checkint(L, 3);
2445 if (((PdfDocument *) uin->d)->doc->getCatalog()->isOk()) {
2446 i = ((PdfDocument *) uin->d)->doc->findPage(num, gen);
2447 if (i > 0)
2448 lua_pushinteger(L, i);
2449 else
2450 lua_pushnil(L);
2451 } else
2452 lua_pushnil(L);
2453 return 1;
2456 static int m_PDFDoc_getLinks(lua_State * L)
2458 int i, pages;
2459 Links *links;
2460 udstruct *uin, *uout;
2461 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2462 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2463 pdfdoc_changed_error(L);
2464 i = luaL_checkint(L, 2);
2465 pages = ((PdfDocument *) uin->d)->doc->getNumPages();
2466 if (i > 0 && i <= pages) {
2467 links = ((PdfDocument *) uin->d)->doc->getLinks(i);
2468 if (links != NULL) {
2469 uout = new_Links_userdata(L);
2470 uout->d = links;
2471 uout->pc = uin->pc;
2472 uout->pd = uin->pd;
2473 } else
2474 lua_pushnil(L);
2475 } else
2476 lua_pushnil(L);
2477 return 1;
2480 static int m_PDFDoc_findDest(lua_State * L)
2482 GooString *name;
2483 LinkDest *dest;
2484 const char *s;
2485 size_t len;
2486 udstruct *uin, *uout;
2487 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2488 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2489 pdfdoc_changed_error(L);
2490 s = luaL_checklstring(L, 2, &len);
2491 name = new GooString(s, len);
2492 if (((PdfDocument *) uin->d)->doc->getCatalog()->isOk()) {
2493 dest = ((PdfDocument *) uin->d)->doc->findDest(name);
2494 if (dest != NULL) {
2495 uout = new_LinkDest_userdata(L);
2496 uout->d = dest;
2497 uout->pc = uin->pc;
2498 uout->pd = uin->pd;
2499 } else
2500 lua_pushnil(L);
2501 } else
2502 lua_pushnil(L);
2503 delete name;
2504 return 1;
2507 m_PDFDoc_BOOL(isEncrypted);
2508 m_PDFDoc_BOOL(okToPrint);
2509 m_PDFDoc_BOOL(okToChange);
2510 m_PDFDoc_BOOL(okToCopy);
2511 m_PDFDoc_BOOL(okToAddNotes);
2512 m_PDFDoc_BOOL(isLinearized);
2514 static int m_PDFDoc_getDocInfo(lua_State * L)
2516 udstruct *uin, *uout;
2517 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2518 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2519 pdfdoc_changed_error(L);
2520 if (((PdfDocument *) uin->d)->doc->getXRef()->isOk()) {
2521 uout = new_Object_userdata(L);
2522 uout->d = new Object();
2523 ((PdfDocument *) uin->d)->doc->getDocInfo((Object *) uout->d);
2524 uout->atype = ALLOC_LEPDF;
2525 uout->pc = uin->pc;
2526 uout->pd = uin->pd;
2527 } else
2528 lua_pushnil(L);
2529 return 1;
2532 static int m_PDFDoc_getDocInfoNF(lua_State * L)
2534 udstruct *uin, *uout;
2535 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2536 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2537 pdfdoc_changed_error(L);
2538 if (((PdfDocument *) uin->d)->doc->getXRef()->isOk()) {
2539 uout = new_Object_userdata(L);
2540 uout->d = new Object();
2541 ((PdfDocument *) uin->d)->doc->getDocInfoNF((Object *) uout->d);
2542 uout->atype = ALLOC_LEPDF;
2543 uout->pc = uin->pc;
2544 uout->pd = uin->pd;
2545 } else
2546 lua_pushnil(L);
2547 return 1;
2550 m_PDFDoc_INT(getPDFMajorVersion);
2551 m_PDFDoc_INT(getPDFMinorVersion);
2553 m_poppler__tostring(PDFDoc);
2555 static int m_PDFDoc__gc(lua_State * L)
2557 udstruct *uin;
2558 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFDoc);
2559 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2560 pdfdoc_changed_error(L);
2561 #ifdef DEBUG
2562 printf("\n===== PDFDoc GC ===== file_path=<%s>\n",
2563 ((PdfDocument *) uin->d)->file_path);
2564 #endif
2565 assert(uin->atype == ALLOC_LEPDF);
2566 unrefPdfDocument(((PdfDocument *) uin->d)->file_path);
2567 return 0;
2570 static const struct luaL_Reg PDFDoc_m[] = {
2571 {"isOk", m_PDFDoc_isOk},
2572 {"getErrorCode", m_PDFDoc_getErrorCode},
2573 {"getErrorCodeName", m_PDFDoc_getErrorCodeName}, // not poppler
2574 {"getFileName", m_PDFDoc_getFileName},
2575 {"getXRef", m_PDFDoc_getXRef},
2576 {"getCatalog", m_PDFDoc_getCatalog},
2577 // {"getBaseStream", m_PDFDoc_getBaseStream},
2578 {"getPageMediaWidth", m_PDFDoc_getPageMediaWidth},
2579 {"getPageMediaHeight", m_PDFDoc_getPageMediaHeight},
2580 {"getPageCropWidth", m_PDFDoc_getPageCropWidth},
2581 {"getPageCropHeight", m_PDFDoc_getPageCropHeight},
2582 {"getNumPages", m_PDFDoc_getNumPages},
2583 {"readMetadata", m_PDFDoc_readMetadata},
2584 {"getStructTreeRoot", m_PDFDoc_getStructTreeRoot},
2585 {"findPage", m_PDFDoc_findPage},
2586 {"getLinks", m_PDFDoc_getLinks},
2587 {"findDest", m_PDFDoc_findDest},
2588 {"isEncrypted", m_PDFDoc_isEncrypted},
2589 {"okToPrint", m_PDFDoc_okToPrint},
2590 {"okToChange", m_PDFDoc_okToChange},
2591 {"okToCopy", m_PDFDoc_okToCopy},
2592 {"okToAddNotes", m_PDFDoc_okToAddNotes},
2593 {"isLinearized", m_PDFDoc_isLinearized},
2594 {"getDocInfo", m_PDFDoc_getDocInfo},
2595 {"getDocInfoNF", m_PDFDoc_getDocInfoNF},
2596 {"getPDFMajorVersion", m_PDFDoc_getPDFMajorVersion},
2597 {"getPDFMinorVersion", m_PDFDoc_getPDFMinorVersion},
2598 {"__tostring", m_PDFDoc__tostring},
2599 {"__gc", m_PDFDoc__gc}, // finalizer
2600 {NULL, NULL} // sentinel
2603 //**********************************************************************
2604 // PDFRectangle
2606 m_poppler_get_BOOL(PDFRectangle, isValid);
2608 m_poppler__tostring(PDFRectangle);
2610 static int m_PDFRectangle__index(lua_State * L)
2612 const char *s;
2613 udstruct *uin;
2614 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFRectangle);
2615 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2616 pdfdoc_changed_error(L);
2617 s = luaL_checkstring(L, 2);
2618 if (strlen(s) == 2) {
2619 if (s[0] == 'x') {
2620 if (s[1] == '1')
2621 lua_pushnumber(L, ((PDFRectangle *) uin->d)->x1); /* float */
2622 else if (s[1] == '2')
2623 lua_pushnumber(L, ((PDFRectangle *) uin->d)->x2); /* float */
2624 else
2625 lua_pushnil(L);
2626 } else if (s[0] == 'y') {
2627 if (s[1] == '1')
2628 lua_pushnumber(L, ((PDFRectangle *) uin->d)->y1); /* float */
2629 else if (s[1] == '2')
2630 lua_pushnumber(L, ((PDFRectangle *) uin->d)->y2); /* float */
2631 else
2632 lua_pushnil(L);
2633 } else
2634 lua_pushnil(L);
2635 } else
2636 lua_pushnil(L);
2637 return 1;
2640 static int m_PDFRectangle__newindex(lua_State * L)
2642 double d;
2643 const char *s;
2644 udstruct *uin;
2645 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFRectangle);
2646 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2647 pdfdoc_changed_error(L);
2648 s = luaL_checkstring(L, 2);
2649 d = luaL_checknumber(L, 3);
2650 if (strlen(s) == 2) {
2651 if (s[0] == 'x') {
2652 if (s[1] == '1')
2653 ((PDFRectangle *) uin->d)->x1 = d;
2654 else if (s[1] == '2')
2655 ((PDFRectangle *) uin->d)->x2 = d;
2656 else
2657 luaL_error(L, "wrong PDFRectangle coordinate (%s)", s);
2658 } else if (s[0] == 'y') {
2659 if (s[1] == '1')
2660 ((PDFRectangle *) uin->d)->y1 = d;
2661 else if (s[1] == '2')
2662 ((PDFRectangle *) uin->d)->y2 = d;
2663 } else
2664 luaL_error(L, "wrong PDFRectangle coordinate (%s)", s);
2665 } else
2666 luaL_error(L, "wrong PDFRectangle coordinate (%s)", s);
2667 return 0;
2670 static int m_PDFRectangle__gc(lua_State * L)
2672 udstruct *uin;
2673 uin = (udstruct *) luaL_checkudata(L, 1, M_PDFRectangle);
2674 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2675 pdfdoc_changed_error(L);
2676 #ifdef DEBUG
2677 printf("\n===== PDFRectangle GC ===== uin=<%p>\n", uin);
2678 #endif
2679 if (uin->atype == ALLOC_LEPDF)
2680 delete(PDFRectangle *) uin->d;
2681 return 0;
2684 static const struct luaL_Reg PDFRectangle_m[] = {
2685 {"isValid", m_PDFRectangle_isValid},
2686 {"__index", m_PDFRectangle__index},
2687 {"__newindex", m_PDFRectangle__newindex},
2688 {"__tostring", m_PDFRectangle__tostring},
2689 {"__gc", m_PDFRectangle__gc},
2690 {NULL, NULL} // sentinel
2693 //**********************************************************************
2694 // Ref
2696 static int m_Ref__index(lua_State * L)
2698 const char *s;
2699 udstruct *uin;
2700 uin = (udstruct *) luaL_checkudata(L, 1, M_Ref);
2701 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2702 pdfdoc_changed_error(L);
2703 s = luaL_checkstring(L, 2);
2704 if (strcmp(s, "num") == 0)
2705 lua_pushinteger(L, ((Ref *) uin->d)->num);
2706 else if (strcmp(s, "gen") == 0)
2707 lua_pushinteger(L, ((Ref *) uin->d)->gen);
2708 else
2709 lua_pushnil(L);
2710 return 1;
2713 m_poppler__tostring(Ref);
2715 static int m_Ref__gc(lua_State * L)
2717 udstruct *uin;
2718 uin = (udstruct *) luaL_checkudata(L, 1, M_Ref);
2719 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2720 pdfdoc_changed_error(L);
2721 #ifdef DEBUG
2722 printf("\n===== Ref GC ===== uin=<%p>\n", uin);
2723 #endif
2724 if (uin->atype == ALLOC_LEPDF && ((Ref *) uin->d) != NULL)
2725 gfree(((Ref *) uin->d));
2726 return 0;
2729 static const struct luaL_Reg Ref_m[] = {
2730 {"__index", m_Ref__index},
2731 {"__tostring", m_Ref__tostring},
2732 {"__gc", m_Ref__gc}, // finalizer
2733 {NULL, NULL} // sentinel
2736 //**********************************************************************
2737 // Stream
2739 static const char *StreamKindNames[] =
2740 { "File", "ASCIIHex", "ASCII85", "LZW", "RunLength", "CCITTFax", "DCT",
2741 "Flate", "JBIG2", "JPX", "Weird", NULL
2744 m_poppler_get_INT(Stream, getKind);
2746 static int m_Stream_getKindName(lua_State * L)
2748 StreamKind t;
2749 udstruct *uin;
2750 uin = (udstruct *) luaL_checkudata(L, 1, M_Stream);
2751 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2752 pdfdoc_changed_error(L);
2753 t = ((Stream *) uin->d)->getKind();
2754 lua_pushstring(L, StreamKindNames[t]);
2755 return 1;
2758 m_poppler_do(Stream, reset);
2759 m_poppler_do(Stream, close);
2760 m_poppler_get_INT(Stream, getChar);
2761 m_poppler_get_INT(Stream, lookChar);
2762 m_poppler_get_INT(Stream, getRawChar);
2763 m_poppler_get_INT(Stream, getUnfilteredChar);
2764 m_poppler_do(Stream, unfilteredReset);
2765 m_poppler_get_INT(Stream, getPos);
2766 m_poppler_get_BOOL(Stream, isBinary);
2767 m_poppler_get_poppler(Stream, Stream, getUndecodedStream);
2768 m_poppler_get_poppler(Stream, Dict, getDict);
2770 m_poppler__tostring(Stream);
2772 static const struct luaL_Reg Stream_m[] = {
2773 {"getKind", m_Stream_getKind},
2774 {"getKindName", m_Stream_getKindName}, // not poppler
2775 {"reset", m_Stream_reset},
2776 {"close", m_Stream_close},
2777 {"getUndecodedStream", m_Stream_getUndecodedStream},
2778 {"getChar", m_Stream_getChar},
2779 {"lookChar", m_Stream_lookChar},
2780 {"getRawChar", m_Stream_getRawChar},
2781 {"getUnfilteredChar", m_Stream_getUnfilteredChar},
2782 {"unfilteredReset", m_Stream_unfilteredReset},
2783 // {"getLine", m_Stream_getLine},
2784 {"getPos", m_Stream_getPos},
2785 {"isBinary", m_Stream_isBinary},
2786 {"getUndecodedStream", m_Stream_getUndecodedStream},
2787 {"getDict", m_Stream_getDict},
2788 {"__tostring", m_Stream__tostring},
2789 {NULL, NULL} // sentinel
2792 //**********************************************************************
2793 // TextSpan
2795 m_poppler_get_GOOSTRING(TextSpan, getText);
2796 m_poppler__tostring(TextSpan);
2798 static const struct luaL_Reg TextSpan_m[] = {
2799 {"getText", m_TextSpan_getText},
2800 {"__tostring", m_TextSpan__tostring},
2801 {NULL, NULL} // sentinel
2807 //**********************************************************************
2808 // Attribute
2809 m_poppler_get_BOOL(Attribute,isOk);
2810 m_poppler_get_INT(Attribute,getType);
2811 m_poppler_get_INT(Attribute,getOwner);
2812 m_poppler_get_GOOSTRING(Attribute,getName);
2814 static int m_Attribute_getTypeName(lua_State * L)
2816 udstruct *uin;
2817 uin = (udstruct *) luaL_checkudata(L, 1, M_Attribute);
2818 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2819 pdfdoc_changed_error(L);
2820 lua_pushstring(L, ((Attribute *) uin->d)->getTypeName());
2821 return 1;
2824 static int m_Attribute_getOwnerName(lua_State * L)
2826 udstruct *uin;
2827 uin = (udstruct *) luaL_checkudata(L, 1, M_Attribute);
2828 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2829 pdfdoc_changed_error(L);
2830 lua_pushstring(L, ((Attribute *) uin->d)->getOwnerName());
2831 return 1;
2834 static int m_Attribute_getValue(lua_State * L)
2836 udstruct *uin, *uout;
2837 Object *origin;
2838 uin = (udstruct *) luaL_checkudata(L, 1, M_Attribute);
2839 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2840 pdfdoc_changed_error(L);
2841 uout = new_Object_userdata(L);
2842 uout->d = new Object();
2843 origin = (Object *) (((Attribute *) uin->d)->getValue());
2844 origin->copy ( ((Object *)uout->d) );
2845 uout->atype = ALLOC_LEPDF;
2846 uout->pc = uin->pc;
2847 uout->pd = uin->pd;
2848 return 1;
2852 static int m_Attribute_getDefaultValue(lua_State * L)
2854 Attribute::Type t;
2855 udstruct *uin, *uout;
2856 uin = (udstruct *) luaL_checkudata(L, 1, M_Attribute);
2857 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2858 pdfdoc_changed_error(L);
2859 t = (Attribute::Type) luaL_checkint(L, 2);
2860 uout = new_Object_userdata(L);
2861 uout->d = ((Attribute *)uin->d)->getDefaultValue(t) ;
2862 //uout->atype = ALLOC_LEPDF;
2863 uout->pc = uin->pc;
2864 uout->pd = uin->pd;
2865 return 1;
2869 m_poppler_get_GUINT(Attribute,getRevision);
2871 static int m_Attribute_setRevision(lua_State * L)
2873 Guint i;
2874 udstruct *uin;
2875 uin = (udstruct *) luaL_checkudata(L, 1, M_Attribute);
2876 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2877 pdfdoc_changed_error(L);
2878 i = (Guint) luaL_checkint(L, 2);
2879 ((Attribute *) uin->d)->setRevision(i);
2880 return 0;
2883 m_poppler_get_BOOL(Attribute, isHidden);
2885 static int m_Attribute_setHidden(lua_State * L)
2887 GBool i;
2888 udstruct *uin;
2889 uin = (udstruct *) luaL_checkudata(L, 1, M_Attribute);
2890 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2891 pdfdoc_changed_error(L);
2892 i = (GBool) lua_toboolean(L, 2);
2893 ((Attribute *) uin->d)->setHidden(i);
2894 return 0;
2897 static int m_Attribute_getFormattedValue(lua_State * L)
2899 udstruct *uin;
2900 uin = (udstruct *) luaL_checkudata(L, 1, M_Attribute);
2901 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2902 pdfdoc_changed_error(L);
2903 lua_pushstring(L, ((Attribute *) uin->d)->getFormattedValue());
2904 return 1;
2908 static int m_Attribute_setFormattedValue(lua_State * L)
2910 const char *c;
2911 udstruct *uin;
2912 uin = (udstruct *) luaL_checkudata(L, 1, M_Attribute);
2913 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2914 pdfdoc_changed_error(L);
2915 c = luaL_checkstring(L, 2);
2916 ((Attribute *) uin->d)->setFormattedValue(c);
2917 return 0;
2920 static int m_Attribute__gc(lua_State * L)
2922 udstruct *uin;
2923 uin = (udstruct *) luaL_checkudata(L, 1, M_Attribute);
2924 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2925 pdfdoc_changed_error(L);
2926 #ifdef DEBUG
2927 printf("\n===== Attribute GC ===== uin=<%p>\n", uin);
2928 #endif
2929 if (uin->atype == ALLOC_LEPDF) {
2930 delete(Attribute *) uin->d;
2932 return 0;
2936 m_poppler__tostring(Attribute);
2939 static const struct luaL_Reg Attribute_m[] = {
2940 {"isOk",m_Attribute_isOk},
2941 {"getType",m_Attribute_getType},
2942 {"getOwner",m_Attribute_getOwner},
2943 {"getTypeName",m_Attribute_getTypeName},
2944 {"getOwnerName",m_Attribute_getOwnerName},
2945 {"getValue",m_Attribute_getValue},
2946 {"getDefaultValue",m_Attribute_getDefaultValue},
2947 {"getName",m_Attribute_getName},
2948 {"getRevision",m_Attribute_getRevision},
2949 {"setRevision",m_Attribute_setRevision},
2950 {"istHidden",m_Attribute_isHidden},
2951 {"setHidden",m_Attribute_setHidden},
2952 {"getFormattedValue",m_Attribute_getFormattedValue},
2953 {"setFormattedValue",m_Attribute_setFormattedValue},
2954 {"__gc", m_Attribute__gc},
2955 {"__tostring", m_Attribute__tostring},
2956 {NULL, NULL} // sentinel
2962 //**********************************************************************
2963 // StructElement
2966 m_poppler_get_INT(StructElement,getType);
2967 m_poppler_get_BOOL(StructElement,isOk);
2968 m_poppler_get_BOOL(StructElement,isBlock);
2969 m_poppler_get_BOOL(StructElement,isInline);
2970 m_poppler_get_BOOL(StructElement,isGrouping);
2971 m_poppler_get_BOOL(StructElement,isContent);
2972 m_poppler_get_BOOL(StructElement,isObjectRef);
2973 m_poppler_get_BOOL(StructElement,hasPageRef);
2974 m_poppler_get_INT(StructElement,getMCID);
2975 m_poppler_get_INT(StructElement, getNumChildren);
2977 m_poppler_get_GUINT(StructElement,getRevision);
2978 m_poppler_get_UINT(StructElement,getNumAttributes);
2980 m_poppler_get_GOOSTRING(StructElement, getID);
2981 m_poppler_get_GOOSTRING(StructElement, getLanguage);
2982 m_poppler_get_GOOSTRING(StructElement, getTitle);
2983 m_poppler_get_GOOSTRING(StructElement, getExpandedAbbr);
2984 m_poppler_get_GOOSTRING(StructElement, getAltText);
2985 m_poppler_get_GOOSTRING(StructElement, getActualText);
2987 m_poppler_get_poppler(StructElement, StructTreeRoot, getStructTreeRoot);
2988 m_poppler__tostring(StructElement);
2991 static int m_StructElement_getObjectRef(lua_State * L)
2993 udstruct *uin, *uout;
2994 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
2995 if (uin->pd != NULL && uin->pd->pc != uin->pc)
2996 pdfdoc_changed_error(L);
2997 uout = new_Ref_userdata(L);
2998 uout->d = (Ref *) gmalloc(sizeof(Ref));
2999 ((Ref *) uout->d)->num = ((StructElement *) uin->d)->getObjectRef().num;
3000 ((Ref *) uout->d)->gen = ((StructElement *) uin->d)->getObjectRef().gen;
3001 uout->atype = ALLOC_LEPDF;
3002 uout->pc = uin->pc;
3003 uout->pd = uin->pd;
3004 return 1;
3008 static int m_StructElement_getParentRef(lua_State * L)
3010 udstruct *uin, *uout;
3011 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3012 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3013 pdfdoc_changed_error(L);
3014 uout = new_Ref_userdata(L);
3015 uout->d = (Ref *) gmalloc(sizeof(Ref));
3016 ((Ref *) uout->d)->num = ((StructElement *) uin->d)->getParentRef().num;
3017 ((Ref *) uout->d)->gen = ((StructElement *) uin->d)->getParentRef().gen;
3018 uout->atype = ALLOC_LEPDF;
3019 uout->pc = uin->pc;
3020 uout->pd = uin->pd;
3021 return 1;
3024 // Not exactly as the header:
3025 // Ref = StructElement:getPageRef()
3026 // Ref is false if the C++ functione return false
3027 static int m_StructElement_getPageRef(lua_State * L)
3029 GBool b;
3030 Ref *r;
3031 udstruct *uin, *uout;
3032 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3033 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3034 pdfdoc_changed_error(L);
3035 r = (Ref *) gmalloc(sizeof(Ref));
3036 b = ((StructElement *) uin->d)->getPageRef( *r );
3037 if (b) {
3038 uout = new_Ref_userdata(L);
3039 uout->d = r ;
3040 //uout->atype = ALLOC_LEPDF;
3041 uout->pc = uin->pc;
3042 uout->pd = uin->pd;
3043 } else
3044 lua_pushboolean(L,0);
3045 return 1;
3050 static int m_StructElement_getTypeName(lua_State * L)
3052 udstruct *uin;
3053 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3054 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3055 pdfdoc_changed_error(L);
3056 lua_pushstring(L, ((StructElement *) uin->d)->getTypeName());
3057 return 1;
3061 static int m_StructElement_setRevision(lua_State * L)
3063 Guint i;
3064 udstruct *uin;
3065 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3066 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3067 pdfdoc_changed_error(L);
3068 i = (Guint) luaL_checkint(L, 2);
3069 ((StructElement *) uin->d)->setRevision(i);
3070 return 0;
3073 static int m_StructElement_getText(lua_State * L)
3075 GBool i;
3076 GooString *gs;
3077 udstruct *uin;
3078 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3079 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3080 pdfdoc_changed_error(L);
3081 i = (GBool) lua_toboolean(L, 2);
3082 gs = ((StructElement *) uin->d)->getText(i);
3083 if (gs != NULL)
3084 lua_pushlstring(L, gs->getCString(), gs->getLength());
3085 else
3086 lua_pushnil(L);
3087 return 1;
3091 static int m_StructElement_getChild(lua_State * L)
3093 StructElement *c;
3094 int i;
3095 udstruct *uin, *uout;
3096 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3097 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3098 pdfdoc_changed_error(L);
3099 i = (int) luaL_checkint(L, 2);
3100 c = ((StructElement *) uin->d)->getChild(i-1);
3101 if (c != NULL) {
3102 uout = new_StructElement_userdata(L);
3103 uout->d = c ;
3104 //uout->atype = ALLOC_LEPDF;
3105 uout->pc = uin->pc;
3106 uout->pd = uin->pd;
3108 else
3109 lua_pushnil(L);
3110 return 1;
3114 static int m_StructElement_appendChild(lua_State * L)
3116 udstruct *uin, *uin1;
3117 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3118 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3119 pdfdoc_changed_error(L);
3120 uin1 = (udstruct *) luaL_checkudata(L, 2, M_StructElement);
3121 if (uin1->pd != NULL && uin1->pd->pc != uin1->pc)
3122 pdfdoc_changed_error(L);
3123 ((StructElement *) uin->d)->appendChild( (StructElement *)uin1->d );
3124 return 0;
3128 static int m_StructElement_getAttribute(lua_State * L)
3130 Attribute *a;
3131 int i;
3132 udstruct *uin, *uout;
3133 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3134 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3135 pdfdoc_changed_error(L);
3136 i = (int) luaL_checkint(L, 2);
3137 a = ((StructElement *) uin->d)->getAttribute(i-1);
3138 if (a != NULL) {
3139 uout = new_Attribute_userdata(L);
3140 uout->d = a ;
3141 uout->pc = uin->pc;
3142 uout->pd = uin->pd;
3144 else
3145 lua_pushnil(L);
3146 return 1;
3151 static int m_StructElement_appendAttribute(lua_State * L)
3154 udstruct *uin, *uin1;
3155 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3156 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3157 pdfdoc_changed_error(L);
3158 uin1 = (udstruct *) luaL_checkudata(L, 2, M_Attribute);
3159 if (uin1->pd != NULL && uin1->pd->pc != uin1->pc)
3160 pdfdoc_changed_error(L);
3161 ((StructElement *) uin->d)->appendAttribute( (Attribute *)uin1->d );
3162 return 0;
3166 static int m_StructElement_findAttribute(lua_State * L)
3168 Attribute::Type t;
3169 Attribute::Owner o;
3170 GBool g;
3171 udstruct *uin, *uout;
3172 const Attribute *a;
3173 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3174 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3175 pdfdoc_changed_error(L);
3176 t = (Attribute::Type) luaL_checkint(L,1);
3177 o = (Attribute::Owner) luaL_checkint(L,2);
3178 g = (GBool) lua_toboolean(L, 3);
3179 a = ((StructElement *) uin->d)->findAttribute(t,g,o);
3181 if (a!=NULL){
3182 uout = new_Attribute_userdata(L);
3183 uout->d = new Attribute(a->getType(),a->getValue());
3184 uout->atype = ALLOC_LEPDF;
3185 uout->pc = uin->pc;
3186 uout->pd = uin->pd;
3187 } else
3188 lua_pushnil(L);
3189 return 1;
3192 // This returns a lua table
3193 static int m_StructElement_getTextSpans(lua_State * L)
3195 int i ;
3196 udstruct *uin, *uout;
3197 uin = (udstruct *) luaL_checkudata(L, 1, M_StructElement);
3198 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3199 pdfdoc_changed_error(L);
3201 if ((((StructElement *) uin->d)->getTextSpans()).size()>0) {
3202 lua_createtable (L,
3203 (int) (((StructElement *) uin->d)->getTextSpans()).size(),
3205 for(i=0;i<(int) (((StructElement *) uin->d)->getTextSpans()).size(); i++){
3206 uout = new_TextSpan_userdata(L);
3207 uout->d = new TextSpan( (((StructElement *) uin->d)->getTextSpans())[i] );
3208 uout->atype = ALLOC_LEPDF;
3209 uout->pc = uin->pc;
3210 uout->pd = uin->pd;
3211 lua_rawseti(L,-2,i+1);
3213 } else
3214 lua_pushnil(L);
3215 return 1;
3220 static const struct luaL_Reg StructElement_m[] = {
3221 {"getTypeName", m_StructElement_getTypeName},
3222 {"getType",m_StructElement_getType},
3223 {"isOk",m_StructElement_isOk},
3224 {"isBlock",m_StructElement_isBlock},
3225 {"isInline",m_StructElement_isInline},
3226 {"isGrouping",m_StructElement_isGrouping},
3227 {"isContent",m_StructElement_isContent},
3228 {"isObjectRef",m_StructElement_isObjectRef},
3229 {"getMCID",m_StructElement_getMCID},
3230 {"getObjectRef",m_StructElement_getObjectRef},
3231 {"getParentRef",m_StructElement_getParentRef},
3232 {"hasPageRef",m_StructElement_hasPageRef},
3233 {"getPageRef",m_StructElement_getPageRef},
3234 {"getStructTreeRoot",m_StructElement_getStructTreeRoot},
3235 {"getID",m_StructElement_getID},
3236 {"getLanguage",m_StructElement_getLanguage},
3237 {"getRevision",m_StructElement_getRevision},
3238 {"setRevision",m_StructElement_setRevision},
3239 {"getTitle",m_StructElement_getTitle},
3240 {"getExpandedAbbr",m_StructElement_getExpandedAbbr},
3241 {"getNumChildren",m_StructElement_getNumChildren},
3242 {"getChild",m_StructElement_getChild},
3243 {"appendChild",m_StructElement_appendChild},
3244 {"getNumAttributes",m_StructElement_getNumAttributes},
3245 {"getAttribute",m_StructElement_getAttribute},
3246 {"appendAttribute",m_StructElement_appendAttribute},
3247 {"findAttribute",m_StructElement_findAttribute},
3248 {"getAltText",m_StructElement_getAltText},
3249 {"getActualText",m_StructElement_getActualText},
3250 {"getText",m_StructElement_getText},
3251 {"getTextSpans",m_StructElement_getTextSpans},
3252 {"__tostring", m_StructElement__tostring},
3253 {NULL, NULL} // sentinel
3257 //**********************************************************************
3258 // StructTreeRoot
3260 m_poppler_get_INT(StructTreeRoot, getNumChildren);
3261 m_poppler_get_poppler(StructTreeRoot, PDFDoc, getDoc);
3262 m_poppler_get_poppler(StructTreeRoot, Dict, getRoleMap);
3263 m_poppler_get_poppler(StructTreeRoot, Dict, getClassMap);
3264 m_poppler__tostring(StructTreeRoot);
3266 static int m_StructTreeRoot_getChild(lua_State * L)
3268 unsigned int i;
3269 udstruct *uin, *uout;
3270 StructElement *child ;
3271 StructTreeRoot *root ;
3273 uin = (udstruct *) luaL_checkudata(L, 1, M_StructTreeRoot);
3274 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3275 pdfdoc_changed_error(L);
3276 i = (unsigned) luaL_checkint(L, 2);
3277 root = (StructTreeRoot *) uin->d;
3278 if (i-1 < root->getNumChildren() ){
3279 child = root->getChild(i-1);
3280 uout = new_StructElement_userdata(L);
3281 uout->d = child;
3282 //uout->atype = ALLOC_LEPDF;
3283 uout->pc = uin->pc;
3284 uout->pd = uin->pd;
3285 } else
3286 lua_pushnil(L);
3287 return 1;
3290 static int m_StructTreeRoot_appendChild(lua_State * L)
3292 udstruct *uin, *uin_child;
3293 StructElement *child ;
3294 StructTreeRoot *root ;
3295 uin = (udstruct *) luaL_checkudata(L, 1, M_StructTreeRoot);
3296 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3297 pdfdoc_changed_error(L);
3298 uin_child = (udstruct *) luaL_checkudata(L, 2, M_StructElement);
3299 if (uin_child->pd != NULL && uin_child->pd->pc != uin_child->pc)
3300 pdfdoc_changed_error(L);
3301 root = (StructTreeRoot *) uin->d;
3302 child = (StructElement *) uin_child->d;
3303 root->appendChild(child);
3304 return 0;
3308 static int m_StructTreeRoot_findParentElement(lua_State * L)
3310 unsigned int i;
3311 udstruct *uin, *uout;
3312 const StructElement *parent ;
3313 StructTreeRoot *root ;
3315 uin = (udstruct *) luaL_checkudata(L, 1, M_StructTreeRoot);
3316 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3317 pdfdoc_changed_error(L);
3318 i = (unsigned) luaL_checkint(L, 2);
3319 root = (StructTreeRoot *) uin->d;
3320 parent = root->findParentElement(i-1);
3321 if (parent != NULL) {
3322 uout = new_StructElement_userdata(L);
3323 uout->d = new StructElement( *parent );
3324 uout->atype = ALLOC_LEPDF;
3325 uout->pc = uin->pc;
3326 uout->pd = uin->pd;
3327 } else
3328 lua_pushnil(L);
3329 return 1;
3333 static const struct luaL_Reg StructTreeRoot_m[] = {
3334 {"findParentElement", m_StructTreeRoot_findParentElement},
3335 {"getDoc",m_StructTreeRoot_getDoc},
3336 {"getRoleMap",m_StructTreeRoot_getRoleMap},
3337 {"getClassMap",m_StructTreeRoot_getClassMap},
3338 {"getNumChildren",m_StructTreeRoot_getNumChildren},
3339 {"getChild",m_StructTreeRoot_getChild},
3340 {"appendChild",m_StructTreeRoot_appendChild},
3341 {"findParentElement",m_StructTreeRoot_findParentElement},
3342 {"__tostring", m_StructTreeRoot__tostring},
3343 {NULL, NULL} // sentinel
3346 //**********************************************************************
3347 // XRef
3349 m_poppler_get_BOOL(XRef, isOk);
3350 m_poppler_get_INT(XRef, getErrorCode);
3351 m_poppler_get_BOOL(XRef, isEncrypted);
3352 m_poppler_get_BOOL(XRef, okToPrint);
3353 m_poppler_get_BOOL(XRef, okToPrintHighRes);
3354 m_poppler_get_BOOL(XRef, okToChange);
3355 m_poppler_get_BOOL(XRef, okToCopy);
3356 m_poppler_get_BOOL(XRef, okToAddNotes);
3357 m_poppler_get_BOOL(XRef, okToFillForm);
3358 m_poppler_get_BOOL(XRef, okToAccessibility);
3359 m_poppler_get_BOOL(XRef, okToAssemble);
3360 m_poppler_get_OBJECT(XRef, getCatalog);
3362 static int m_XRef_fetch(lua_State * L)
3364 int num, gen;
3365 udstruct *uin, *uout;
3366 uin = (udstruct *) luaL_checkudata(L, 1, M_XRef);
3367 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3368 pdfdoc_changed_error(L);
3369 num = luaL_checkint(L, 2);
3370 gen = luaL_checkint(L, 3);
3371 uout = new_Object_userdata(L);
3372 uout->d = new Object();
3373 ((XRef *) uin->d)->fetch(num, gen, (Object *) uout->d);
3374 uout->atype = ALLOC_LEPDF;
3375 uout->pc = uin->pc;
3376 uout->pd = uin->pd;
3377 return 1;
3380 m_poppler_get_OBJECT(XRef, getDocInfo);
3381 m_poppler_get_OBJECT(XRef, getDocInfoNF);
3382 m_poppler_get_INT(XRef, getNumObjects);
3383 m_poppler_get_INT(XRef, getRootNum);
3384 m_poppler_get_INT(XRef, getRootGen);
3385 // getStreamEnd
3387 static int m_XRef_getNumEntry(lua_State * L)
3389 int i, offset;
3390 udstruct *uin;
3391 uin = (udstruct *) luaL_checkudata(L, 1, M_XRef);
3392 if (uin->pd != NULL && uin->pd->pc != uin->pc)
3393 pdfdoc_changed_error(L);
3394 offset = luaL_checkint(L, 2);
3395 i = ((XRef *) uin->d)->getNumEntry(offset);
3396 if (i >= 0)
3397 lua_pushinteger(L, i);
3398 else
3399 lua_pushnil(L);
3400 return 1;
3403 m_poppler_get_poppler(XRef, Object, getTrailerDict);
3405 m_poppler__tostring(XRef);
3407 static const struct luaL_Reg XRef_m[] = {
3408 {"isOk", m_XRef_isOk},
3409 {"getErrorCode", m_XRef_getErrorCode},
3410 {"isEncrypted", m_XRef_isEncrypted},
3411 {"okToPrint", m_XRef_okToPrint},
3412 {"okToPrintHighRes", m_XRef_okToPrintHighRes},
3413 {"okToChange", m_XRef_okToChange},
3414 {"okToCopy", m_XRef_okToCopy},
3415 {"okToAddNotes", m_XRef_okToAddNotes},
3416 {"okToFillForm", m_XRef_okToFillForm},
3417 {"okToAccessibility", m_XRef_okToAccessibility},
3418 {"okToAssemble", m_XRef_okToAssemble},
3419 {"getCatalog", m_XRef_getCatalog},
3420 {"fetch", m_XRef_fetch},
3421 {"getDocInfo", m_XRef_getDocInfo},
3422 {"getDocInfoNF", m_XRef_getDocInfoNF},
3423 {"getNumObjects", m_XRef_getNumObjects},
3424 {"getRootNum", m_XRef_getRootNum},
3425 {"getRootGen", m_XRef_getRootGen},
3426 // {"getStreamEnd", m_XRef_getStreamEnd},
3427 {"getNumEntry", m_XRef_getNumEntry},
3428 {"getTrailerDict", m_XRef_getTrailerDict},
3429 {"__tostring", m_XRef__tostring},
3430 {NULL, NULL} // sentinel
3433 //**********************************************************************
3434 // XRefEntry
3436 m_poppler__tostring(XRefEntry);
3438 static const struct luaL_Reg XRefEntry_m[] = {
3439 {"__tostring", m_XRefEntry__tostring},
3440 {NULL, NULL} // sentinel
3443 //**********************************************************************
3445 #ifdef LuajitTeX
3446 #define setfuncs_meta(type) \
3447 luaL_newmetatable(L, M_##type); \
3448 lua_pushvalue(L, -1); \
3449 lua_setfield(L, -2, "__index"); \
3450 lua_pushstring(L, "no user access"); \
3451 lua_setfield(L, -2, "__metatable"); \
3452 luaL_register(L, NULL, type##_m)
3453 #else
3454 #define setfuncs_meta(type) \
3455 luaL_newmetatable(L, M_##type); \
3456 lua_pushvalue(L, -1); \
3457 lua_setfield(L, -2, "__index"); \
3458 lua_pushstring(L, "no user access"); \
3459 lua_setfield(L, -2, "__metatable"); \
3460 luaL_setfuncs(L, type##_m, 0)
3461 #endif
3463 int luaopen_epdf(lua_State * L)
3465 setfuncs_meta(Annot);
3466 setfuncs_meta(Annots);
3467 setfuncs_meta(Array);
3468 setfuncs_meta(Catalog);
3469 setfuncs_meta(Dict);
3470 setfuncs_meta(EmbFile);
3471 setfuncs_meta(FileSpec);
3472 setfuncs_meta(GooString);
3473 setfuncs_meta(LinkDest);
3474 setfuncs_meta(Links);
3475 setfuncs_meta(Object);
3476 setfuncs_meta(Page);
3477 setfuncs_meta(PDFDoc);
3478 setfuncs_meta(PDFRectangle);
3479 setfuncs_meta(Ref);
3480 setfuncs_meta(Stream);
3481 setfuncs_meta(Attribute);
3482 setfuncs_meta(StructElement);
3483 setfuncs_meta(StructTreeRoot);
3484 setfuncs_meta(TextSpan);
3485 setfuncs_meta(XRef);
3486 setfuncs_meta(XRefEntry);
3488 #ifdef LuajitTeX
3489 luaL_register(L, "epdf", epdflib_f);
3490 #else
3491 luaL_newlib(L, epdflib_f);
3492 #endif
3493 return 1;