| 1 |
/* limglib.c |
| 2 |
|
| 3 |
Copyright 2006-2013 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 7 |
LuaTeX is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 2 of the License, or (at your |
| 10 |
option) any later version. |
| 11 |
|
| 12 |
LuaTeX is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License along |
| 18 |
with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#include "ptexlib.h" |
| 21 |
#include "lua/luatex-api.h" |
| 22 |
#include <stdio.h> |
| 23 |
#include <string.h> |
| 24 |
#include "lua.h" |
| 25 |
#include "lauxlib.h" |
| 26 |
|
| 27 |
#define img_types_max 8 |
| 28 |
|
| 29 |
const char *img_types[] = { |
| 30 |
"none", |
| 31 |
"pdf", |
| 32 |
"png", |
| 33 |
"jpg", |
| 34 |
"jp2", |
| 35 |
"jbig2", |
| 36 |
"stream", |
| 37 |
"memstream", |
| 38 |
"rawstream", |
| 39 |
NULL |
| 40 |
}; |
| 41 |
|
| 42 |
static void copy_image(lua_State * L, lua_Number scale) |
| 43 |
{ |
| 44 |
image *a, **aa, *b, **bb; |
| 45 |
image_dict *d; |
| 46 |
if (lua_gettop(L) == 0) |
| 47 |
luaL_error(L, "img.copy needs an image as argument"); |
| 48 |
aa = (image **) luaL_checkudata(L, 1, TYPE_IMG); /* a */ |
| 49 |
lua_pop(L, 1); /* - */ |
| 50 |
a = *aa; |
| 51 |
bb = (image **) lua_newuserdata(L, sizeof(image *)); /* b */ |
| 52 |
luaL_getmetatable(L, TYPE_IMG); /* m b */ |
| 53 |
lua_setmetatable(L, -2); /* b */ |
| 54 |
b = *bb = new_image(); |
| 55 |
if (!is_wd_running(a)) |
| 56 |
img_width(b) = do_zround(img_width(a) * scale); |
| 57 |
if (!is_ht_running(a)) |
| 58 |
img_height(b) = do_zround(img_height(a) * scale); |
| 59 |
if (!is_dp_running(a)) |
| 60 |
img_depth(b) = do_zround(img_depth(a) * scale); |
| 61 |
img_transform(b) = img_transform(a); |
| 62 |
img_dict(b) = img_dict(a); |
| 63 |
if (img_dictref(a) != LUA_NOREF) { |
| 64 |
lua_rawgeti(L, LUA_REGISTRYINDEX, img_dictref(a)); /* ad b */ |
| 65 |
img_dictref(b) = luaL_ref(L, LUA_REGISTRYINDEX); /* b */ |
| 66 |
d = img_dict(*aa); |
| 67 |
img_luaref(d) += 1; |
| 68 |
} else if (img_state(img_dict(a)) < DICT_REFERED) { |
| 69 |
luaL_error(L, "img.copy needs an proper image as argument"); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
static void lua_to_image(lua_State * L, image * a, image_dict * d); |
| 74 |
|
| 75 |
int l_new_image(lua_State * L) |
| 76 |
{ |
| 77 |
image *a, **aa; |
| 78 |
image_dict **add; |
| 79 |
if (lua_gettop(L) > 0 && ! lua_istable(L, -1)) { |
| 80 |
luaL_error(L, "img.new needs table as optional argument"); /* (t) */ |
| 81 |
} |
| 82 |
aa = (image **) lua_newuserdata(L, sizeof(image *)); /* i (t) */ |
| 83 |
luaL_getmetatable(L, TYPE_IMG); /* m i (t) */ |
| 84 |
lua_setmetatable(L, -2); /* i (t) */ |
| 85 |
a = *aa = new_image(); |
| 86 |
add = (image_dict **) lua_newuserdata(L, sizeof(image_dict *)); /* ad i (t) */ |
| 87 |
luaL_getmetatable(L, TYPE_IMG_DICT); /* m ad i (t) */ |
| 88 |
lua_setmetatable(L, -2); /* ad i (t) */ |
| 89 |
img_dict(a) = *add = new_image_dict(); |
| 90 |
img_dictref(a) = luaL_ref(L, LUA_REGISTRYINDEX); /* i (t) */ |
| 91 |
img_luaref(*add) += 1; |
| 92 |
if (lua_gettop(L) == 2) { /* i t, else just i */ |
| 93 |
lua_insert(L, -2); /* t i */ |
| 94 |
lua_pushnil(L); /* n t i (1st key for iterator) */ |
| 95 |
while (lua_next(L, -2) != 0) { /* v k t i */ |
| 96 |
lua_to_image(L, a, *add); /* v k t i */ |
| 97 |
lua_pop(L, 1); /* k t i */ |
| 98 |
} /* t i */ |
| 99 |
lua_pop(L, 1); /* i */ |
| 100 |
} /* i */ |
| 101 |
return 1; /* i */ |
| 102 |
} |
| 103 |
|
| 104 |
static int l_copy_image(lua_State * L) |
| 105 |
{ |
| 106 |
if (lua_gettop(L) != 1) { |
| 107 |
luaL_error(L, "img.copy needs an image as argument"); |
| 108 |
} else if (lua_istable(L, 1)) { |
| 109 |
(void) l_new_image(L); |
| 110 |
} else { |
| 111 |
(void) copy_image(L, 1.0); |
| 112 |
} |
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
static void read_scale_img(image * a) |
| 117 |
{ |
| 118 |
image_dict *ad; |
| 119 |
if (a == NULL) { |
| 120 |
luaL_error(Luas, "the image scaler needs a valid image"); |
| 121 |
} else { |
| 122 |
ad = img_dict(a); |
| 123 |
if (ad == NULL) { |
| 124 |
luaL_error(Luas, "the image scaler needs a valid dictionary"); |
| 125 |
} else { |
| 126 |
if (img_state(ad) == DICT_NEW) { |
| 127 |
if (img_type(ad) == IMG_TYPE_PDFSTREAM) |
| 128 |
check_pdfstream_dict(ad); |
| 129 |
else { |
| 130 |
read_img(ad); |
| 131 |
} |
| 132 |
} |
| 133 |
if ((img_type(ad) == IMG_TYPE_NONE) || (img_state(ad) == DICT_NEW)) { |
| 134 |
normal_warning("image","don't rely on the image data to be okay"); |
| 135 |
img_width(a) = 0; |
| 136 |
img_height(a) = 0; |
| 137 |
img_depth(a) = 0; |
| 138 |
} else if (is_wd_running(a) || is_ht_running(a) || is_dp_running(a)) { |
| 139 |
img_dimen(a) = scale_img(ad, img_dimen(a), img_transform(a)); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
static int l_scan_image(lua_State * L) |
| 146 |
{ |
| 147 |
image *a, **aa; |
| 148 |
if (lua_gettop(L) != 1) |
| 149 |
luaL_error(L, "img.scan needs exactly 1 argument"); |
| 150 |
if (lua_istable(L, 1)) |
| 151 |
(void) l_new_image(L); |
| 152 |
aa = (image **) luaL_checkudata(L, 1, TYPE_IMG); |
| 153 |
a = *aa; |
| 154 |
check_o_mode(static_pdf, "img.scan", 1 << OMODE_PDF, false); |
| 155 |
read_scale_img(a); |
| 156 |
return 1; |
| 157 |
} |
| 158 |
|
| 159 |
static halfword img_to_node(lua_State * L, image * a) |
| 160 |
{ |
| 161 |
image_dict *ad; |
| 162 |
halfword n = null; |
| 163 |
if (a == NULL) { |
| 164 |
luaL_error(L, "img.tonode needs a valid image"); |
| 165 |
} else { |
| 166 |
ad = img_dict(a); |
| 167 |
if (a == NULL) { |
| 168 |
luaL_error(L, "img.tonode image has no dictionary"); |
| 169 |
} else if (img_objnum(ad) == 0) { |
| 170 |
luaL_error(L, "img.tonode got image without object number"); |
| 171 |
} else { |
| 172 |
n = new_rule(image_rule); |
| 173 |
rule_index(n) = img_index(ad); |
| 174 |
width(n) = img_width(a); |
| 175 |
height(n) = img_height(a); |
| 176 |
depth(n) = img_depth(a); |
| 177 |
rule_transform(n) = img_transform(a); |
| 178 |
} |
| 179 |
} |
| 180 |
return n; |
| 181 |
} |
| 182 |
|
| 183 |
typedef enum { |
| 184 |
WR_WRITE, |
| 185 |
WR_IMMEDIATEWRITE, |
| 186 |
WR_NODE, |
| 187 |
WR_VF_IMG |
| 188 |
} wrtype_e; |
| 189 |
|
| 190 |
const char *wrtype_s[] = { |
| 191 |
"img.write()", |
| 192 |
"img.immediatewrite()", |
| 193 |
"img.node()", |
| 194 |
"write vf image" |
| 195 |
}; |
| 196 |
|
| 197 |
static void setup_image(PDF pdf, image * a, wrtype_e writetype) |
| 198 |
{ |
| 199 |
image_dict *ad; |
| 200 |
if (a == NULL) |
| 201 |
luaL_error(Luas, "no valid image passed"); /* todo, also check in caller */ |
| 202 |
ad = img_dict(a); |
| 203 |
check_o_mode(pdf, wrtype_s[writetype], 1 << OMODE_PDF, false); |
| 204 |
read_scale_img(a); |
| 205 |
if (img_objnum(ad) == 0) { /* latest needed just before out_img() */ |
| 206 |
pdf->ximage_count++; |
| 207 |
img_objnum(ad) = pdf_create_obj(pdf, obj_type_ximage, pdf->ximage_count); |
| 208 |
img_index(ad) = pdf->ximage_count; |
| 209 |
idict_to_array(ad); /* from now on ad is read-only */ |
| 210 |
obj_data_ptr(pdf, pdf->obj_ptr) = img_index(ad); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
static void write_image_or_node(lua_State * L, wrtype_e writetype) |
| 215 |
{ |
| 216 |
image *a, **aa; |
| 217 |
image_dict *ad; |
| 218 |
halfword n; |
| 219 |
if (lua_gettop(L) != 1) |
| 220 |
luaL_error(L, "%s expects an argument", wrtype_s[writetype]); |
| 221 |
if (lua_istable(L, 1)) |
| 222 |
(void) l_new_image(L); |
| 223 |
aa = (image **) luaL_checkudata(L, 1, TYPE_IMG); |
| 224 |
a = *aa; |
| 225 |
ad = img_dict(a); |
| 226 |
setup_image(static_pdf, a, writetype); |
| 227 |
switch (writetype) { |
| 228 |
case WR_WRITE: |
| 229 |
n = img_to_node(L, a); |
| 230 |
tail_append(n); |
| 231 |
break; |
| 232 |
case WR_IMMEDIATEWRITE: |
| 233 |
write_img(static_pdf, ad); |
| 234 |
break; |
| 235 |
case WR_NODE: |
| 236 |
lua_pop(L, 1); /* - */ |
| 237 |
n = img_to_node(L, a); |
| 238 |
lua_nodelib_push_fast(L, n); |
| 239 |
break; |
| 240 |
default: |
| 241 |
luaL_error(L, "%s expects an valid image", wrtype_s[writetype]); |
| 242 |
} |
| 243 |
if (img_state(ad) < DICT_REFERED) |
| 244 |
img_state(ad) = DICT_REFERED; |
| 245 |
} |
| 246 |
|
| 247 |
static int write_image_object(lua_State * L, wrtype_e writetype) |
| 248 |
{ |
| 249 |
image *a, **aa; |
| 250 |
image_dict *ad; |
| 251 |
int num; |
| 252 |
if (lua_gettop(L) != 2) |
| 253 |
luaL_error(L, "%s expects two argument", wrtype_s[writetype]); |
| 254 |
aa = (image **) luaL_checkudata(L, 1, TYPE_IMG); |
| 255 |
a = *aa; |
| 256 |
ad = img_dict(a); |
| 257 |
|
| 258 |
read_img(ad); |
| 259 |
|
| 260 |
/* setup_image(static_pdf, a, writetype); */ |
| 261 |
num = (int) lua_tointeger(L, 2); |
| 262 |
num = write_img_object(static_pdf, ad, num); |
| 263 |
lua_pushinteger(L,num); |
| 264 |
return 1; |
| 265 |
} |
| 266 |
|
| 267 |
static int l_write_image(lua_State * L) |
| 268 |
{ |
| 269 |
write_image_or_node(L, WR_WRITE); |
| 270 |
return 1; |
| 271 |
} |
| 272 |
|
| 273 |
static int l_immediatewrite_image(lua_State * L) |
| 274 |
{ |
| 275 |
check_o_mode(static_pdf, "img.immediatewrite", 1 << OMODE_PDF, true); |
| 276 |
if (global_shipping_mode != NOT_SHIPPING) { |
| 277 |
luaL_error(L, "img.immediatewrite can not be used with \\latelua"); |
| 278 |
} else { |
| 279 |
write_image_or_node(L, WR_IMMEDIATEWRITE); |
| 280 |
} |
| 281 |
return 1; |
| 282 |
} |
| 283 |
|
| 284 |
static int l_immediatewrite_image_object(lua_State * L) |
| 285 |
{ |
| 286 |
check_o_mode(static_pdf, "img.immediatewriteobject", 1 << OMODE_PDF, true); |
| 287 |
if (global_shipping_mode != NOT_SHIPPING) { |
| 288 |
luaL_error(L, "img.immediatewriteobject can not be used with \\latelua"); |
| 289 |
} else { |
| 290 |
write_image_object(L, WR_IMMEDIATEWRITE); |
| 291 |
} |
| 292 |
return 1; |
| 293 |
} |
| 294 |
|
| 295 |
static int l_image_node(lua_State * L) |
| 296 |
{ |
| 297 |
write_image_or_node(L, WR_NODE); |
| 298 |
return 1; |
| 299 |
} |
| 300 |
|
| 301 |
static int l_image_keys(lua_State * L) |
| 302 |
{ |
| 303 |
return lua_show_valid_keys(L, img_parms, img_parms_max); |
| 304 |
} |
| 305 |
|
| 306 |
static int l_image_types(lua_State * L) |
| 307 |
{ |
| 308 |
return lua_show_valid_list(L, img_types, 0, img_types_max); |
| 309 |
} |
| 310 |
|
| 311 |
static int l_image_boxes(lua_State * L) |
| 312 |
{ |
| 313 |
return lua_show_valid_keys(L, img_pageboxes, img_pageboxes_max); |
| 314 |
} |
| 315 |
|
| 316 |
static const struct luaL_Reg imglib_f[] = { |
| 317 |
{ "new", l_new_image }, |
| 318 |
{ "copy", l_copy_image }, |
| 319 |
{ "scan", l_scan_image }, |
| 320 |
{ "write", l_write_image }, |
| 321 |
{ "immediatewrite", l_immediatewrite_image }, |
| 322 |
{ "immediatewriteobject", l_immediatewrite_image_object }, |
| 323 |
{ "node", l_image_node }, |
| 324 |
{ "fields", l_image_keys }, |
| 325 |
{ "types", l_image_types }, |
| 326 |
{ "boxes", l_image_boxes }, |
| 327 |
/* for a while: */ |
| 328 |
{ "keys", l_image_keys }, |
| 329 |
{ NULL, NULL } |
| 330 |
}; |
| 331 |
|
| 332 |
void vf_out_image(PDF pdf, unsigned i) |
| 333 |
{ |
| 334 |
image *a, **aa; |
| 335 |
image_dict *ad; |
| 336 |
lua_rawgeti(Luas, LUA_REGISTRYINDEX, (int) i); |
| 337 |
aa = (image **) luaL_checkudata(Luas, -1, TYPE_IMG); |
| 338 |
a = *aa; |
| 339 |
ad = img_dict(a); |
| 340 |
if (ad == NULL) { |
| 341 |
luaL_error(Luas, "invalid image dictionary"); |
| 342 |
} |
| 343 |
setup_image(pdf, a, WR_VF_IMG); |
| 344 |
place_img(pdf, ad, img_dimen(a), img_transform(a)); |
| 345 |
lua_pop(Luas, 1); |
| 346 |
} |
| 347 |
|
| 348 |
/* metamethods for image */ |
| 349 |
|
| 350 |
static int m_img_get(lua_State * L) |
| 351 |
{ |
| 352 |
int j; |
| 353 |
const char *s; |
| 354 |
image **a = (image **) luaL_checkudata(L, 1, TYPE_IMG); /* k u */ |
| 355 |
image_dict *d = img_dict(*a); |
| 356 |
if (d == NULL) { |
| 357 |
luaL_error(L, "invalid image dictionary"); |
| 358 |
} |
| 359 |
s = lua_tostring(L, 2); |
| 360 |
if (lua_key_eq(s,width)) { |
| 361 |
if (is_wd_running(*a)) { |
| 362 |
lua_pushnil(L); |
| 363 |
} else { |
| 364 |
lua_pushinteger(L, img_width(*a)); |
| 365 |
} |
| 366 |
} else if (lua_key_eq(s,height)) { |
| 367 |
if (is_ht_running(*a)) { |
| 368 |
lua_pushnil(L); |
| 369 |
} else { |
| 370 |
lua_pushinteger(L, img_height(*a)); |
| 371 |
} |
| 372 |
} else if (lua_key_eq(s,depth)) { |
| 373 |
if (is_dp_running(*a)) { |
| 374 |
lua_pushnil(L); |
| 375 |
} else { |
| 376 |
lua_pushinteger(L, img_depth(*a)); |
| 377 |
} |
| 378 |
} else if (lua_key_eq(s,transform)) { |
| 379 |
lua_pushinteger(L, img_transform(*a)); |
| 380 |
} else if (lua_key_eq(s,filename)) { |
| 381 |
if (img_filename(d) == NULL || strlen(img_filename(d)) == 0) { |
| 382 |
lua_pushnil(L); |
| 383 |
} else { |
| 384 |
lua_pushstring(L, img_filename(d)); |
| 385 |
} |
| 386 |
} else if (lua_key_eq(s,keepopen)) { |
| 387 |
lua_pushboolean(L, img_keepopen(d)); |
| 388 |
} else if (lua_key_eq(s,nolength)) { |
| 389 |
lua_pushboolean(L, img_nolength(d)); |
| 390 |
} else if (lua_key_eq(s,notype)) { |
| 391 |
lua_pushboolean(L, img_notype(d)); |
| 392 |
} else if (lua_key_eq(s,nobbox)) { |
| 393 |
lua_pushboolean(L, img_nobbox(d)); |
| 394 |
} else if (lua_key_eq(s,filepath)) { |
| 395 |
if (img_filepath(d) == NULL || strlen(img_filepath(d)) == 0) { |
| 396 |
lua_pushnil(L); |
| 397 |
} else { |
| 398 |
lua_pushstring(L, img_filepath(d)); |
| 399 |
} |
| 400 |
} else if (lua_key_eq(s,attr)) { |
| 401 |
if (img_attr(d) == NULL || strlen(img_attr(d)) == 0) { |
| 402 |
lua_pushnil(L); |
| 403 |
} else { |
| 404 |
lua_pushstring(L, img_attr(d)); |
| 405 |
} |
| 406 |
} else if (lua_key_eq(s,page)) { |
| 407 |
if (img_pagename(d) != NULL && strlen(img_pagename(d)) != 0) { |
| 408 |
lua_pushstring(L, img_pagename(d)); |
| 409 |
} else { |
| 410 |
lua_pushinteger(L, img_pagenum(d)); |
| 411 |
} |
| 412 |
} else if (lua_key_eq(s,pages)) { |
| 413 |
lua_pushinteger(L, img_totalpages(d)); |
| 414 |
} else if (lua_key_eq(s,xsize)) { |
| 415 |
if ((img_rotation(d) & 1) == 0) { |
| 416 |
lua_pushinteger(L, img_xsize(d)); |
| 417 |
} else { |
| 418 |
lua_pushinteger(L, img_ysize(d)); |
| 419 |
} |
| 420 |
} else if (lua_key_eq(s,ysize)) { |
| 421 |
if ((img_rotation(d) & 1) == 0) { |
| 422 |
lua_pushinteger(L, img_ysize(d)); |
| 423 |
} else { |
| 424 |
lua_pushinteger(L, img_xsize(d)); |
| 425 |
} |
| 426 |
} else if (lua_key_eq(s,xres)) { |
| 427 |
lua_pushinteger(L, img_xres(d)); |
| 428 |
} else if (lua_key_eq(s,yres)) { |
| 429 |
lua_pushinteger(L, img_yres(d)); |
| 430 |
} else if (lua_key_eq(s,rotation)) { |
| 431 |
lua_pushinteger(L, img_rotation(d)); |
| 432 |
} else if (lua_key_eq(s,orientation)) { |
| 433 |
lua_pushinteger(L, img_orientation(d)); |
| 434 |
} else if (lua_key_eq(s,colorspace)) { |
| 435 |
if (img_colorspace(d) == 0) { |
| 436 |
lua_pushnil(L); |
| 437 |
} else { |
| 438 |
lua_pushinteger(L, img_colorspace(d)); |
| 439 |
} |
| 440 |
} else if (lua_key_eq(s,colordepth)) { |
| 441 |
if (img_colordepth(d) == 0) { |
| 442 |
lua_pushnil(L); |
| 443 |
} else { |
| 444 |
lua_pushinteger(L, img_colordepth(d)); |
| 445 |
} |
| 446 |
} else if (lua_key_eq(s,imagetype)) { |
| 447 |
j = img_type(d); |
| 448 |
if (j >= 0 && j <= img_types_max) { |
| 449 |
if (j == IMG_TYPE_NONE) { |
| 450 |
lua_pushnil(L); |
| 451 |
} else { |
| 452 |
lua_pushstring(L, img_types[j]); |
| 453 |
} |
| 454 |
} else { |
| 455 |
lua_pushnil(L); |
| 456 |
} |
| 457 |
} else if (lua_key_eq(s,pagebox)) { |
| 458 |
j = img_pagebox(d); |
| 459 |
if (j < 0 || j >= img_pageboxes_max) { |
| 460 |
j = 0; |
| 461 |
} |
| 462 |
lua_push_img_pagebox(L, j); |
| 463 |
} else if (lua_key_eq(s,bbox)) { |
| 464 |
if (!img_is_bbox(d)) { |
| 465 |
img_bbox(d)[0] = img_xorig(d); |
| 466 |
img_bbox(d)[1] = img_yorig(d); |
| 467 |
img_bbox(d)[2] = img_xorig(d) + img_xsize(d); |
| 468 |
img_bbox(d)[3] = img_yorig(d) + img_ysize(d); |
| 469 |
} |
| 470 |
lua_newtable(L); |
| 471 |
lua_pushinteger(L, 1); |
| 472 |
lua_pushinteger(L, img_bbox(d)[0]); |
| 473 |
lua_settable(L, -3); |
| 474 |
lua_pushinteger(L, 2); |
| 475 |
lua_pushinteger(L, img_bbox(d)[1]); |
| 476 |
lua_settable(L, -3); |
| 477 |
lua_pushinteger(L, 3); |
| 478 |
lua_pushinteger(L, img_bbox(d)[2]); |
| 479 |
lua_settable(L, -3); |
| 480 |
lua_pushinteger(L, 4); |
| 481 |
lua_pushinteger(L, img_bbox(d)[3]); |
| 482 |
lua_settable(L, -3); |
| 483 |
} else if (lua_key_eq(s,objnum)) { |
| 484 |
if (img_objnum(d) == 0) { |
| 485 |
lua_pushnil(L); |
| 486 |
} else { |
| 487 |
lua_pushinteger(L, img_objnum(d)); |
| 488 |
} |
| 489 |
} else if (lua_key_eq(s,index)) { |
| 490 |
if (img_index(d) == 0) { |
| 491 |
lua_pushnil(L); |
| 492 |
} else { |
| 493 |
lua_pushinteger(L, img_index(d)); |
| 494 |
} |
| 495 |
} else if (lua_key_eq(s,stream)) { |
| 496 |
if (img_type(d) != IMG_TYPE_PDFSTREAM |
| 497 |
|| img_pdfstream_ptr(d) == NULL |
| 498 |
|| img_pdfstream_stream(d) == NULL |
| 499 |
|| img_pdfstream_size(d) == 0) { |
| 500 |
lua_pushnil(L); |
| 501 |
} else { |
| 502 |
lua_pushlstring(L, img_pdfstream_stream(d), img_pdfstream_size(d)); |
| 503 |
} |
| 504 |
} else if (lua_key_eq(s,visiblefilename)) { |
| 505 |
if (img_visiblefilename(d) == NULL || strlen(img_visiblefilename(d)) == 0) { |
| 506 |
lua_pushnil(L); |
| 507 |
} else { |
| 508 |
lua_pushstring(L, img_visiblefilename(d)); |
| 509 |
} |
| 510 |
} else if (lua_key_eq(s,userpassword)) { |
| 511 |
if (img_userpassword(d) == NULL || strlen(img_userpassword(d)) == 0) { |
| 512 |
lua_pushnil(L); |
| 513 |
} else { |
| 514 |
lua_pushstring(L, img_userpassword(d)); |
| 515 |
} |
| 516 |
} else if (lua_key_eq(s,ownerpassword)) { |
| 517 |
if (img_ownerpassword(d) == NULL || strlen(img_ownerpassword(d)) == 0) { |
| 518 |
lua_pushnil(L); |
| 519 |
} else { |
| 520 |
lua_pushstring(L, img_ownerpassword(d)); |
| 521 |
} |
| 522 |
} else if (lua_key_eq(s,ref_count)) { |
| 523 |
lua_pushinteger(L, img_luaref(d)); |
| 524 |
} else { |
| 525 |
lua_pushnil(L); |
| 526 |
} |
| 527 |
return 1; |
| 528 |
} |
| 529 |
|
| 530 |
static void lua_to_image(lua_State * L, image * a, image_dict * d) |
| 531 |
{ |
| 532 |
int i, t; |
| 533 |
const char *s; |
| 534 |
s = lua_tostring(L,-2); |
| 535 |
t = lua_type(L, -1); |
| 536 |
if (lua_key_eq(s,width)) { |
| 537 |
if (t == LUA_TNIL) { |
| 538 |
set_wd_running(a); |
| 539 |
} else if (t == LUA_TNUMBER) { |
| 540 |
img_width(a) = (int) lua_roundnumber(L, -1); |
| 541 |
} else if (t == LUA_TSTRING) { |
| 542 |
img_width(a) = dimen_to_number(L, lua_tostring(L, -1)); |
| 543 |
} else { |
| 544 |
luaL_error(L, "img.width needs integer or nil value or dimension string"); |
| 545 |
} |
| 546 |
} else if (lua_key_eq(s,height)) { |
| 547 |
if (t == LUA_TNIL) { |
| 548 |
set_ht_running(a); |
| 549 |
} else if (t == LUA_TNUMBER) { |
| 550 |
img_height(a) = (int) lua_roundnumber(L, -1); |
| 551 |
} else if (t == LUA_TSTRING) { |
| 552 |
img_height(a) = dimen_to_number(L, lua_tostring(L, -1)); |
| 553 |
} else { |
| 554 |
luaL_error(L, "img.height needs integer or nil value or dimension string"); |
| 555 |
} |
| 556 |
} else if (lua_key_eq(s,depth)) { |
| 557 |
if (t == LUA_TNIL) { |
| 558 |
set_dp_running(a); |
| 559 |
} else if (t == LUA_TNUMBER) { |
| 560 |
img_depth(a) = (int) lua_roundnumber(L, -1); |
| 561 |
} else if (t == LUA_TSTRING) { |
| 562 |
img_depth(a) = dimen_to_number(L, lua_tostring(L, -1)); |
| 563 |
} else { |
| 564 |
luaL_error(L, "img.depth needs integer or nil value or dimension string"); |
| 565 |
} |
| 566 |
} else if (lua_key_eq(s,transform)) { |
| 567 |
if (t == LUA_TNUMBER) { |
| 568 |
img_transform(a) = (int) lua_tointeger(L, -1); |
| 569 |
} else { |
| 570 |
luaL_error(L, "img.transform needs integer value"); |
| 571 |
} |
| 572 |
} else if (lua_key_eq(s,filename)) { |
| 573 |
if (img_state(d) >= DICT_FILESCANNED) { |
| 574 |
luaL_error(L, "img.filename is now read-only"); |
| 575 |
} else if (img_type(d) == IMG_TYPE_PDFSTREAM) { |
| 576 |
/* just ignore */ |
| 577 |
} else if (t == LUA_TSTRING) { |
| 578 |
xfree(img_filename(d)); |
| 579 |
img_filename(d) = xstrdup(lua_tostring(L, -1)); |
| 580 |
} else { |
| 581 |
luaL_error(L, "img.filename needs string value"); |
| 582 |
} |
| 583 |
} else if (lua_key_eq(s,visiblefilename)) { |
| 584 |
if (img_state(d) >= DICT_FILESCANNED) { |
| 585 |
luaL_error(L, "img.visiblefilename is now read-only"); |
| 586 |
} else if (img_type(d) == IMG_TYPE_PDFSTREAM) { |
| 587 |
img_visiblefilename(d) = NULL; |
| 588 |
} else if (t == LUA_TSTRING) { |
| 589 |
xfree(img_visiblefilename(d)); |
| 590 |
img_visiblefilename(d) = xstrdup(lua_tostring(L, -1)); |
| 591 |
} else { |
| 592 |
luaL_error(L, "img.visiblefilename needs string value"); |
| 593 |
} |
| 594 |
} else if (lua_key_eq(s,userpassword)) { |
| 595 |
if (img_state(d) >= DICT_FILESCANNED) { |
| 596 |
luaL_error(L, "img.userpassword is now read-only"); |
| 597 |
} else if (img_type(d) == IMG_TYPE_PDFSTREAM) { |
| 598 |
img_userpassword(d) = NULL; |
| 599 |
} else if (t == LUA_TSTRING) { |
| 600 |
xfree(img_userpassword(d)); |
| 601 |
img_userpassword(d) = xstrdup(lua_tostring(L, -1)); |
| 602 |
} else { |
| 603 |
luaL_error(L, "img.userpassword needs string value"); |
| 604 |
} |
| 605 |
} else if (lua_key_eq(s,ownerpassword)) { |
| 606 |
if (img_state(d) >= DICT_FILESCANNED) { |
| 607 |
luaL_error(L, "img.ownerpassword is now read-only"); |
| 608 |
} else if (img_type(d) == IMG_TYPE_PDFSTREAM) { |
| 609 |
img_ownerpassword(d) = NULL; |
| 610 |
} else if (t == LUA_TSTRING) { |
| 611 |
xfree(img_ownerpassword(d)); |
| 612 |
img_ownerpassword(d) = xstrdup(lua_tostring(L, -1)); |
| 613 |
} else { |
| 614 |
luaL_error(L, "img.ownerpassword needs string value"); |
| 615 |
} |
| 616 |
} else if (lua_key_eq(s,attr)) { |
| 617 |
if (img_state(d) >= DICT_FILESCANNED) { |
| 618 |
luaL_error(L, "img.attr is now read-only"); |
| 619 |
} else if (t == LUA_TSTRING) { |
| 620 |
xfree(img_attr(d)); |
| 621 |
img_attr(d) = xstrdup(lua_tostring(L, -1)); |
| 622 |
} else if (t == LUA_TNIL) { |
| 623 |
xfree(img_attr(d)); |
| 624 |
} else { |
| 625 |
luaL_error(L, "img.attr needs string or nil value"); |
| 626 |
} |
| 627 |
} else if (lua_key_eq(s,page)) { |
| 628 |
if (img_state(d) >= DICT_FILESCANNED) { |
| 629 |
luaL_error(L, "img.page is now read-only"); |
| 630 |
} else if (t == LUA_TSTRING) { |
| 631 |
xfree(img_pagename(d)); |
| 632 |
img_pagename(d) = xstrdup(lua_tostring(L, -1)); |
| 633 |
img_pagenum(d) = 0; |
| 634 |
} else if (t == LUA_TNUMBER) { |
| 635 |
img_pagenum(d) = (int) lua_tointeger(L, -1); |
| 636 |
xfree(img_pagename(d)); |
| 637 |
} else { |
| 638 |
luaL_error(L, "img.page needs integer or string value"); |
| 639 |
} |
| 640 |
} else if (lua_key_eq(s,colorspace)) { |
| 641 |
if (img_state(d) >= DICT_FILESCANNED) { |
| 642 |
luaL_error(L, "img.colorspace is now read-only"); |
| 643 |
} else if (t == LUA_TNIL) { |
| 644 |
img_colorspace(d) = 0; |
| 645 |
} else if (t == LUA_TNUMBER) { |
| 646 |
img_colorspace(d) = (int) lua_tointeger(L, -1); |
| 647 |
} else { |
| 648 |
luaL_error(L, "img.colorspace needs integer or nil value"); |
| 649 |
} |
| 650 |
} else if (lua_key_eq(s,pagebox)) { |
| 651 |
if (img_state(d) >= DICT_FILESCANNED) { |
| 652 |
luaL_error(L, "img.pagebox is now read-only"); |
| 653 |
} else if (t == LUA_TNIL) { |
| 654 |
img_pagebox(d) = PDF_BOX_SPEC_MEDIA; |
| 655 |
} else if (t == LUA_TNUMBER) { |
| 656 |
i = lua_tointeger(L,-1); |
| 657 |
if (i < 0 || i >= img_pageboxes_max) { |
| 658 |
img_pagebox(d) = PDF_BOX_SPEC_MEDIA; |
| 659 |
} else { |
| 660 |
img_pagebox(d) = i; |
| 661 |
} |
| 662 |
} else if (t == LUA_TSTRING) { |
| 663 |
img_pagebox(d) = PDF_BOX_SPEC_MEDIA; |
| 664 |
for (i = 0; i < img_pageboxes_max; i++) { |
| 665 |
lua_rawgeti(L, LUA_REGISTRYINDEX, img_pageboxes[i]); |
| 666 |
if (lua_rawequal(L,-1,-2)) { |
| 667 |
img_pagebox(d) = i; |
| 668 |
lua_pop(L, 1); |
| 669 |
break; |
| 670 |
} else { |
| 671 |
lua_pop(L, 1); |
| 672 |
} |
| 673 |
} |
| 674 |
} else { |
| 675 |
luaL_error(L, "img.pagebox needs string, number or nil value"); |
| 676 |
} |
| 677 |
} else if (lua_key_eq(s,keepopen)) { |
| 678 |
if (img_state(d) >= DICT_FILESCANNED) { |
| 679 |
luaL_error(L, "img.keepopen is now read-only"); |
| 680 |
} else if (t != LUA_TBOOLEAN) { |
| 681 |
luaL_error(L, "img.bbox needs boolean value"); |
| 682 |
} else { |
| 683 |
img_keepopen(d) = lua_toboolean(L, -1); |
| 684 |
} |
| 685 |
} else if (lua_key_eq(s,nolength)) { |
| 686 |
img_nolength(d) = lua_toboolean(L, -1); |
| 687 |
} else if (lua_key_eq(s,notype)) { |
| 688 |
img_notype(d) = lua_toboolean(L, -1); |
| 689 |
} else if (lua_key_eq(s,nobbox)) { |
| 690 |
img_nobbox(d) = lua_toboolean(L, -1); |
| 691 |
} else if (lua_key_eq(s,bbox)) { |
| 692 |
if (img_state(d) >= DICT_FILESCANNED) { |
| 693 |
luaL_error(L, "img.bbox is now read-only"); |
| 694 |
} else if (t != LUA_TTABLE) { |
| 695 |
luaL_error(L, "img.bbox needs table value"); |
| 696 |
} else if (lua_rawlen(L, -1) != 4) { |
| 697 |
luaL_error(L, "img.bbox table must have exactly 4 elements"); |
| 698 |
} else { |
| 699 |
for (i = 1; i <= 4; i++) { /* v k t ... */ |
| 700 |
lua_pushinteger(L, i); /* idx v k t ... */ |
| 701 |
lua_gettable(L, -2); /* int v k t ... */ |
| 702 |
t = lua_type(L, -1); |
| 703 |
if (t == LUA_TNUMBER) { |
| 704 |
img_bbox(d)[i - 1] = (int) lua_roundnumber(L, -1); |
| 705 |
} else if (t == LUA_TSTRING) { |
| 706 |
img_bbox(d)[i - 1] = dimen_to_number(L, lua_tostring(L, -1)); |
| 707 |
} else { |
| 708 |
luaL_error(L, "img.bbox table needs integer value or dimension string elements"); |
| 709 |
} |
| 710 |
lua_pop(L, 1); /* v k t ... */ |
| 711 |
} |
| 712 |
img_set_bbox(d); |
| 713 |
} |
| 714 |
} else if (lua_key_eq(s,stream)) { |
| 715 |
if (img_filename(d) != NULL) { |
| 716 |
luaL_error(L, "img.stream can't be used with image.filename"); |
| 717 |
} else if (img_state(d) >= DICT_FILESCANNED) { |
| 718 |
luaL_error(L, "img.stream is now read-only"); |
| 719 |
} else { |
| 720 |
size_t size = 0; |
| 721 |
const char *stream = lua_tolstring(L, -1, &size); |
| 722 |
if (img_pdfstream_ptr(d) == NULL) { |
| 723 |
new_img_pdfstream_struct(d); |
| 724 |
} |
| 725 |
xfree(img_pdfstream_stream(d)); |
| 726 |
img_pdfstream_size(d) = size; |
| 727 |
img_pdfstream_stream(d) = xmalloc(size); |
| 728 |
memcpy(img_pdfstream_stream(d),stream,size); |
| 729 |
img_type(d) = IMG_TYPE_PDFSTREAM; |
| 730 |
} |
| 731 |
} else { |
| 732 |
/*tex Let's be a bit more tolerant to adding private keys. */ |
| 733 |
/* luaL_error(L, "img.%s can not be set", s); */ |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
static int m_img_set(lua_State * L) |
| 738 |
{ |
| 739 |
image **a = (image **) luaL_checkudata(L, 1, TYPE_IMG); /* v k u */ |
| 740 |
image_dict *d = img_dict(*a); |
| 741 |
if (d == NULL) { |
| 742 |
luaL_error(L, "invalid image dictionary"); |
| 743 |
} else { |
| 744 |
lua_to_image(L, *a, d); |
| 745 |
} |
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
static int m_img_mul(lua_State * L) |
| 750 |
{ |
| 751 |
lua_Number scale; |
| 752 |
if (lua_type(L, 1) == LUA_TNUMBER) { /* u? n */ |
| 753 |
(void) luaL_checkudata(L, 2, TYPE_IMG); /* u n */ |
| 754 |
lua_insert(L, -2); /* n a */ |
| 755 |
} else if (lua_type(L, 2) != LUA_TNUMBER) { /* n u? */ |
| 756 |
(void) luaL_checkudata(L, 1, TYPE_IMG); /* n a */ |
| 757 |
} /* n a */ |
| 758 |
scale = lua_tonumber(L, 2); /* float */ /* n a */ |
| 759 |
lua_pop(L, 1); /* a */ |
| 760 |
copy_image(L, scale); /* b */ |
| 761 |
return 1; |
| 762 |
} |
| 763 |
|
| 764 |
static int m_img_print(lua_State * L) |
| 765 |
{ |
| 766 |
image **aa; |
| 767 |
image_dict *d; |
| 768 |
aa = (image **) luaL_checkudata(L, 1, TYPE_IMG); |
| 769 |
d = img_dict(*aa); |
| 770 |
/* formatted a bit like a node */ |
| 771 |
if (img_filename(d) == NULL) { |
| 772 |
if (img_pagename(d) != NULL && strlen(img_pagename(d)) != 0) { |
| 773 |
lua_pushfstring(L, "<img unset : %d >", img_dictref(*aa)); |
| 774 |
} else { |
| 775 |
lua_pushfstring(L, "<img unset : %d >", img_dictref(*aa)); |
| 776 |
} |
| 777 |
} else { |
| 778 |
if (img_pagename(d) != NULL && strlen(img_pagename(d)) != 0) { |
| 779 |
lua_pushfstring(L, "<img %s : %s : %d >", img_filename(d), img_pagename(d), img_dictref(*aa)); |
| 780 |
} else { |
| 781 |
lua_pushfstring(L, "<img %s : %d : %d >", img_filename(d), img_pagenum(d), img_dictref(*aa)); |
| 782 |
} |
| 783 |
} |
| 784 |
return 1; |
| 785 |
} |
| 786 |
|
| 787 |
/* this finalizes instance */ |
| 788 |
|
| 789 |
static int m_img_gc(lua_State * L) |
| 790 |
{ |
| 791 |
image *a, **aa; |
| 792 |
image_dict *d; |
| 793 |
aa = (image **) luaL_checkudata(L, 1, TYPE_IMG); |
| 794 |
a = *aa; |
| 795 |
d = img_dict(*aa); |
| 796 |
luaL_unref(L, LUA_REGISTRYINDEX, img_dictref(a)); |
| 797 |
img_luaref(d) -= 1; |
| 798 |
/* we need to check this */ |
| 799 |
if (!img_is_refered(d)) { |
| 800 |
xfree(a); |
| 801 |
} |
| 802 |
/* till here */ |
| 803 |
return 0; |
| 804 |
} |
| 805 |
|
| 806 |
/* the instance */ |
| 807 |
|
| 808 |
static const struct luaL_Reg img_m[] = { |
| 809 |
{"__index", m_img_get}, |
| 810 |
{"__newindex", m_img_set}, |
| 811 |
{"__mul", m_img_mul}, |
| 812 |
{"__tostring", m_img_print}, |
| 813 |
{"__gc", m_img_gc}, |
| 814 |
{NULL, NULL} |
| 815 |
}; |
| 816 |
|
| 817 |
/* this finalizes the dict */ |
| 818 |
|
| 819 |
static int m_img_dict_gc(lua_State * L) |
| 820 |
{ |
| 821 |
image_dict *ad, **add; |
| 822 |
add = (image_dict **) luaL_checkudata(L, 1, TYPE_IMG_DICT); |
| 823 |
ad = *add; |
| 824 |
if (img_luaref(ad) > 0) { |
| 825 |
luaL_error(L, "disposing image dict that has references"); |
| 826 |
} else { |
| 827 |
/* we need to check this */ |
| 828 |
if (img_state(ad) < DICT_REFERED) { |
| 829 |
free_image_dict(ad); |
| 830 |
} |
| 831 |
/* till here */ |
| 832 |
} |
| 833 |
return 0; |
| 834 |
} |
| 835 |
|
| 836 |
/* the (shared) dict */ |
| 837 |
|
| 838 |
static const struct luaL_Reg img_dict_m[] = { |
| 839 |
{"__gc", m_img_dict_gc}, |
| 840 |
{NULL, NULL} |
| 841 |
}; |
| 842 |
|
| 843 |
int luaopen_img(lua_State * L) |
| 844 |
{ |
| 845 |
luaL_newmetatable(L, TYPE_IMG); |
| 846 |
#ifdef LuajitTeX |
| 847 |
luaL_openlib(L, NULL, img_m, 0); |
| 848 |
luaL_newmetatable(L, TYPE_IMG_DICT); |
| 849 |
luaL_openlib(L, NULL, img_dict_m, 0); |
| 850 |
#else |
| 851 |
luaL_setfuncs(L, img_m, 0); |
| 852 |
luaL_newmetatable(L, TYPE_IMG_DICT); |
| 853 |
luaL_setfuncs(L, img_dict_m, 0); |
| 854 |
#endif |
| 855 |
luaL_openlib(L, "img", imglib_f, 0); |
| 856 |
return 1; |
| 857 |
} |