| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]> |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software |
| 20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "system.h" |
| 28 |
#include "mem.h" |
| 29 |
#include "error.h" |
| 30 |
#include "dpxutil.h" |
| 31 |
|
| 32 |
#include "pdfdraw.h" |
| 33 |
#include "pdfdev.h" |
| 34 |
#include "pdfximage.h" |
| 35 |
|
| 36 |
#include "pdfdoc.h" |
| 37 |
|
| 38 |
#include "specials.h" |
| 39 |
#include "spc_util.h" |
| 40 |
|
| 41 |
#include "spc_html.h" |
| 42 |
|
| 43 |
|
| 44 |
#define ENABLE_HTML_IMG_SUPPORT 1 |
| 45 |
#define ENABLE_HTML_SVG_TRANSFORM 1 |
| 46 |
#define ENABLE_HTML_SVG_OPACITY 1 |
| 47 |
|
| 48 |
/* _FIXME_ |
| 49 |
* Please rewrite this or remove html special support |
| 50 |
*/ |
| 51 |
|
| 52 |
#define ANCHOR_TYPE_HREF 0 |
| 53 |
#define ANCHOR_TYPE_NAME 1 |
| 54 |
|
| 55 |
struct spc_html_ |
| 56 |
{ |
| 57 |
struct { |
| 58 |
int extensions; |
| 59 |
} opts; |
| 60 |
|
| 61 |
pdf_obj *link_dict; |
| 62 |
char *baseurl; |
| 63 |
int pending_type; |
| 64 |
}; |
| 65 |
|
| 66 |
static struct spc_html_ _html_state = { |
| 67 |
{ 0 }, |
| 68 |
NULL, NULL, -1 |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
#ifdef ENABLE_HTML_SVG_TRANSFORM |
| 73 |
static int cvt_a_to_tmatrix (pdf_tmatrix *M, const char *ptr, const char **nextptr); |
| 74 |
#endif /* ENABLE_HTML_SVG_TRANSFORM */ |
| 75 |
|
| 76 |
#define \ |
| 77 |
downcasify(s) \ |
| 78 |
if ((s)) { \ |
| 79 |
char *_p = (char *) (s); \ |
| 80 |
while (*(_p) != 0) { \ |
| 81 |
if (*(_p) >= 'A' && *(_p) <= 'Z') { \ |
| 82 |
*(_p) = (*(_p) - 'A') + 'a'; \ |
| 83 |
} \ |
| 84 |
_p++; \ |
| 85 |
} \ |
| 86 |
} |
| 87 |
|
| 88 |
static int |
| 89 |
parse_key_val (const char **pp, const char *endptr, char **kp, char **vp) |
| 90 |
{ |
| 91 |
const char *q, *p; |
| 92 |
char *k, *v; |
| 93 |
int n, error = 0; |
| 94 |
|
| 95 |
for (p = *pp ; p < endptr && isspace((unsigned char)*p); p++) |
| 96 |
; |
| 97 |
#if 0 |
| 98 |
while (!error && p < endptr && |
| 99 |
((*p >= 'a' && *p <= 'z') || |
| 100 |
(*p >= 'A' && *p <= 'Z')) |
| 101 |
) { |
| 102 |
#endif |
| 103 |
k = v = NULL; |
| 104 |
for (q = p, n = 0; |
| 105 |
p < endptr && |
| 106 |
((*p >= 'a' && *p <= 'z') || |
| 107 |
(*p >= 'A' && *p <= 'Z') || |
| 108 |
(*p >= '0' && *p <= '9') || |
| 109 |
*p == '-' || *p == ':' |
| 110 |
); n++, p++); |
| 111 |
if (n == 0) { |
| 112 |
#if 0 |
| 113 |
break; |
| 114 |
#else |
| 115 |
*kp = *vp = NULL; |
| 116 |
return -1; |
| 117 |
#endif |
| 118 |
} |
| 119 |
k = NEW(n + 1, char); |
| 120 |
memcpy(k, q, n); k[n] = '\0'; |
| 121 |
if (p + 2 >= endptr || p[0] != '=' || (p[1] != '\"' && p[1] != '\'')) { |
| 122 |
RELEASE(k); k = NULL; |
| 123 |
*pp = p; |
| 124 |
error = -1; |
| 125 |
} else { |
| 126 |
char qchr = p[1]; |
| 127 |
p += 2; /* skip '="' */ |
| 128 |
for (q = p, n = 0; p < endptr && *p != qchr; p++, n++); |
| 129 |
if (p == endptr || *p != qchr) |
| 130 |
error = -1; |
| 131 |
else { |
| 132 |
v = NEW(n + 1, char); |
| 133 |
memcpy(v, q, n); v[n] = '\0'; |
| 134 |
#if 0 |
| 135 |
pdf_add_dict(t->attr, |
| 136 |
pdf_new_name(k), |
| 137 |
pdf_new_string(v, n)); |
| 138 |
RELEASE(v); |
| 139 |
#endif |
| 140 |
p++; |
| 141 |
} |
| 142 |
} |
| 143 |
#if 0 |
| 144 |
RELEASE(k); |
| 145 |
if (!error) |
| 146 |
for ( ; p < endptr && isspace(*p); p++); |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
*kp = k; *vp = v; *pp = p; |
| 151 |
return error; |
| 152 |
} |
| 153 |
|
| 154 |
#define HTML_TAG_NAME_MAX 127 |
| 155 |
#define HTML_TAG_TYPE_EMPTY 1 |
| 156 |
#define HTML_TAG_TYPE_OPEN 1 |
| 157 |
#define HTML_TAG_TYPE_CLOSE 2 |
| 158 |
|
| 159 |
static int |
| 160 |
read_html_tag (char *name, pdf_obj *attr, int *type, const char **pp, const char *endptr) |
| 161 |
{ |
| 162 |
const char *p = *pp; |
| 163 |
int n = 0, error = 0; |
| 164 |
|
| 165 |
for ( ; p < endptr && isspace((unsigned char)*p); p++); |
| 166 |
if (p >= endptr || *p != '<') |
| 167 |
return -1; |
| 168 |
|
| 169 |
*type = HTML_TAG_TYPE_OPEN; |
| 170 |
for (++p; p < endptr && isspace((unsigned char)*p); p++); |
| 171 |
if (p < endptr && *p == '/') { |
| 172 |
*type = HTML_TAG_TYPE_CLOSE; |
| 173 |
for (++p; p < endptr && isspace((unsigned char)*p); p++); |
| 174 |
} |
| 175 |
|
| 176 |
#define ISDELIM(c) ((c) == '>' || (c) == '/' || isspace((unsigned char)c)) |
| 177 |
for (n = 0; p < endptr && n < HTML_TAG_NAME_MAX && !ISDELIM(*p); n++, p++) { |
| 178 |
name[n] = *p; |
| 179 |
} |
| 180 |
name[n] = '\0'; |
| 181 |
if (n == 0 || p == endptr || !ISDELIM(*p)) { |
| 182 |
*pp = p; |
| 183 |
return -1; |
| 184 |
} |
| 185 |
|
| 186 |
for ( ; p < endptr && isspace((unsigned char)*p); p++); |
| 187 |
while (p < endptr && !error && *p != '/' && *p != '>') { |
| 188 |
char *kp = NULL, *vp = NULL; |
| 189 |
error = parse_key_val(&p, endptr, &kp, &vp); |
| 190 |
if (!error) { |
| 191 |
downcasify(kp); |
| 192 |
pdf_add_dict(attr, |
| 193 |
pdf_new_name(kp), |
| 194 |
pdf_new_string(vp, strlen(vp) + 1)); /* include trailing NULL here!!! */ |
| 195 |
RELEASE(kp); |
| 196 |
RELEASE(vp); |
| 197 |
} |
| 198 |
for ( ; p < endptr && isspace((unsigned char)*p); p++); |
| 199 |
} |
| 200 |
if (error) { |
| 201 |
*pp = p; |
| 202 |
return error; |
| 203 |
} |
| 204 |
|
| 205 |
if (p < endptr && *p == '/') { |
| 206 |
*type = HTML_TAG_TYPE_EMPTY; |
| 207 |
for (++p; p < endptr && isspace((unsigned char)*p); p++); |
| 208 |
} |
| 209 |
if (p == endptr || *p != '>') { |
| 210 |
*pp = p; |
| 211 |
return -1; |
| 212 |
} |
| 213 |
p++; |
| 214 |
|
| 215 |
downcasify(name); |
| 216 |
*pp = p; |
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
static int |
| 222 |
spc_handler_html__init (void *dp) |
| 223 |
{ |
| 224 |
struct spc_html_ *sd = dp; |
| 225 |
|
| 226 |
sd->link_dict = NULL; |
| 227 |
sd->baseurl = NULL; |
| 228 |
sd->pending_type = -1; |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
static int |
| 234 |
spc_handler_html__clean (struct spc_env *spe, void *dp) |
| 235 |
{ |
| 236 |
struct spc_html_ *sd = dp; |
| 237 |
|
| 238 |
if (sd->baseurl) |
| 239 |
RELEASE(sd->baseurl); |
| 240 |
|
| 241 |
if (sd->pending_type >= 0 || sd->link_dict) |
| 242 |
spc_warn(spe, "Unclosed html anchor found."); |
| 243 |
|
| 244 |
if (sd->link_dict) |
| 245 |
pdf_release_obj(sd->link_dict); |
| 246 |
|
| 247 |
sd->pending_type = -1; |
| 248 |
sd->baseurl = NULL; |
| 249 |
sd->link_dict = NULL; |
| 250 |
|
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
static int |
| 256 |
spc_handler_html__bophook (struct spc_env *spe, void *dp) |
| 257 |
{ |
| 258 |
struct spc_html_ *sd = dp; |
| 259 |
|
| 260 |
if (sd->pending_type >= 0) { |
| 261 |
spc_warn(spe, "...html anchor continues from previous page processed..."); |
| 262 |
} |
| 263 |
|
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
static int |
| 268 |
spc_handler_html__eophook (struct spc_env *spe, void *dp) |
| 269 |
{ |
| 270 |
struct spc_html_ *sd = dp; |
| 271 |
|
| 272 |
if (sd->pending_type >= 0) { |
| 273 |
spc_warn(spe, "Unclosed html anchor at end-of-page!"); |
| 274 |
} |
| 275 |
|
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
static char * |
| 281 |
fqurl (const char *baseurl, const char *name) |
| 282 |
{ |
| 283 |
char *q; |
| 284 |
int len = 0; |
| 285 |
|
| 286 |
len = strlen(name); |
| 287 |
if (baseurl) |
| 288 |
len += strlen(baseurl) + 1; /* we may want to add '/' */ |
| 289 |
|
| 290 |
q = NEW(len + 1, char); |
| 291 |
*q = '\0'; |
| 292 |
if (baseurl && baseurl[0]) { |
| 293 |
char *p; |
| 294 |
strcpy(q, baseurl); |
| 295 |
p = q + strlen(q) - 1; |
| 296 |
if (*p == '/') |
| 297 |
*p = '\0'; |
| 298 |
if (name[0] && name[0] != '/') |
| 299 |
strcat(q, "/"); |
| 300 |
} |
| 301 |
strcat(q, name); |
| 302 |
|
| 303 |
return q; |
| 304 |
} |
| 305 |
|
| 306 |
static int |
| 307 |
html_open_link (struct spc_env *spe, const char *name, struct spc_html_ *sd) |
| 308 |
{ |
| 309 |
pdf_obj *color; |
| 310 |
char *url; |
| 311 |
|
| 312 |
ASSERT( name ); |
| 313 |
ASSERT( sd->link_dict == NULL ); /* Should be checked somewhere else */ |
| 314 |
|
| 315 |
sd->link_dict = pdf_new_dict(); |
| 316 |
pdf_add_dict(sd->link_dict, pdf_new_name("Type"), pdf_new_name ("Annot")); |
| 317 |
pdf_add_dict(sd->link_dict, pdf_new_name("Subtype"), pdf_new_name ("Link")); |
| 318 |
|
| 319 |
color = pdf_new_array (); |
| 320 |
pdf_add_array(color, pdf_new_number(0.0)); |
| 321 |
pdf_add_array(color, pdf_new_number(0.0)); |
| 322 |
pdf_add_array(color, pdf_new_number(1.0)); |
| 323 |
pdf_add_dict(sd->link_dict, pdf_new_name("C"), color); |
| 324 |
|
| 325 |
url = fqurl(sd->baseurl, name); |
| 326 |
if (url[0] == '#') { |
| 327 |
/* url++; causes memory leak in RELEASE(url) */ |
| 328 |
pdf_add_dict(sd->link_dict, pdf_new_name("Dest"), pdf_new_string(url+1, strlen(url+1))); |
| 329 |
} else { /* Assume this is URL */ |
| 330 |
pdf_obj *action = pdf_new_dict(); |
| 331 |
pdf_add_dict(action, pdf_new_name("Type"), pdf_new_name("Action")); |
| 332 |
pdf_add_dict(action, pdf_new_name("S"), pdf_new_name("URI")); |
| 333 |
pdf_add_dict(action, pdf_new_name("URI"), pdf_new_string(url, strlen(url))); |
| 334 |
pdf_add_dict(sd->link_dict, pdf_new_name("A"), pdf_link_obj(action)); |
| 335 |
pdf_release_obj(action); |
| 336 |
} |
| 337 |
RELEASE(url); |
| 338 |
|
| 339 |
spc_begin_annot(spe, pdf_link_obj(sd->link_dict)); |
| 340 |
|
| 341 |
sd->pending_type = ANCHOR_TYPE_HREF; |
| 342 |
|
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
static int |
| 347 |
html_open_dest (struct spc_env *spe, const char *name, struct spc_html_ *sd) |
| 348 |
{ |
| 349 |
int error; |
| 350 |
pdf_obj *array, *page_ref; |
| 351 |
pdf_coord cp; |
| 352 |
|
| 353 |
cp.x = spe->x_user; cp.y = spe->y_user; |
| 354 |
pdf_dev_transform(&cp, NULL); |
| 355 |
|
| 356 |
page_ref = pdf_doc_this_page_ref(); |
| 357 |
ASSERT( page_ref ); /* Otherwise must be bug */ |
| 358 |
|
| 359 |
array = pdf_new_array(); |
| 360 |
pdf_add_array(array, page_ref); |
| 361 |
pdf_add_array(array, pdf_new_name("XYZ")); |
| 362 |
pdf_add_array(array, pdf_new_null()); |
| 363 |
pdf_add_array(array, pdf_new_number(cp.y + 24.0)); |
| 364 |
pdf_add_array(array, pdf_new_null()); |
| 365 |
|
| 366 |
error = pdf_doc_add_names("Dests", name, strlen(name), array); |
| 367 |
|
| 368 |
if (error) |
| 369 |
spc_warn(spe, "Failed to add named destination: %s", name); |
| 370 |
|
| 371 |
sd->pending_type = ANCHOR_TYPE_NAME; |
| 372 |
|
| 373 |
return error; |
| 374 |
} |
| 375 |
|
| 376 |
#define ANCHOR_STARTED(s) ((s)->pending_type >= 0 || (s)->link_dict) |
| 377 |
|
| 378 |
static int |
| 379 |
spc_html__anchor_open (struct spc_env *spe, pdf_obj *attr, struct spc_html_ *sd) |
| 380 |
{ |
| 381 |
pdf_obj *href, *name; |
| 382 |
int error = 0; |
| 383 |
|
| 384 |
if (ANCHOR_STARTED(sd)) { |
| 385 |
spc_warn(spe, "Nested html anchors found!"); |
| 386 |
return -1; |
| 387 |
} |
| 388 |
|
| 389 |
href = pdf_lookup_dict(attr, "href"); |
| 390 |
name = pdf_lookup_dict(attr, "name"); |
| 391 |
if (href && name) { |
| 392 |
spc_warn(spe, "Sorry, you can't have both \"href\" and \"name\" in anchor tag..."); |
| 393 |
error = -1; |
| 394 |
} else if (href) { |
| 395 |
error = html_open_link(spe, pdf_string_value(href), sd); |
| 396 |
} else if (name) { /* name */ |
| 397 |
error = html_open_dest(spe, pdf_string_value(name), sd); |
| 398 |
} else { |
| 399 |
spc_warn(spe, "You should have \"href\" or \"name\" in anchor tag!"); |
| 400 |
error = -1; |
| 401 |
} |
| 402 |
|
| 403 |
return error; |
| 404 |
} |
| 405 |
|
| 406 |
static int |
| 407 |
spc_html__anchor_close (struct spc_env *spe, struct spc_html_ *sd) |
| 408 |
{ |
| 409 |
int error = 0; |
| 410 |
|
| 411 |
switch (sd->pending_type) { |
| 412 |
case ANCHOR_TYPE_HREF: |
| 413 |
if (sd->link_dict) { |
| 414 |
spc_end_annot(spe); |
| 415 |
pdf_release_obj(sd->link_dict); |
| 416 |
sd->link_dict = NULL; |
| 417 |
sd->pending_type = -1; |
| 418 |
} else { |
| 419 |
spc_warn(spe, "Closing html anchor (link) without starting!"); |
| 420 |
error = -1; |
| 421 |
} |
| 422 |
break; |
| 423 |
case ANCHOR_TYPE_NAME: |
| 424 |
sd->pending_type = -1; |
| 425 |
break; |
| 426 |
default: |
| 427 |
spc_warn(spe, "No corresponding opening tag for html anchor."); |
| 428 |
error = -1; |
| 429 |
break; |
| 430 |
} |
| 431 |
|
| 432 |
return error; |
| 433 |
} |
| 434 |
|
| 435 |
static int |
| 436 |
spc_html__base_empty (struct spc_env *spe, pdf_obj *attr, struct spc_html_ *sd) |
| 437 |
{ |
| 438 |
pdf_obj *href; |
| 439 |
char *vp; |
| 440 |
|
| 441 |
href = pdf_lookup_dict(attr, "href"); |
| 442 |
if (!href) { |
| 443 |
spc_warn(spe, "\"href\" not found for \"base\" tag!"); |
| 444 |
return -1; |
| 445 |
} |
| 446 |
|
| 447 |
vp = (char *) pdf_string_value(href); |
| 448 |
if (sd->baseurl) { |
| 449 |
spc_warn(spe, "\"baseurl\" changed: \"%s\" --> \"%s\"", sd->baseurl, vp); |
| 450 |
RELEASE(sd->baseurl); |
| 451 |
} |
| 452 |
sd->baseurl = NEW(strlen(vp) + 1, char); |
| 453 |
strcpy(sd->baseurl, vp); |
| 454 |
|
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
#ifdef ENABLE_HTML_IMG_SUPPORT |
| 460 |
/* This isn't completed. |
| 461 |
* Please think about placement of images. |
| 462 |
*/ |
| 463 |
static double |
| 464 |
atopt (const char *a) |
| 465 |
{ |
| 466 |
char *q; |
| 467 |
const char *p = a; |
| 468 |
double v, u = 1.0; |
| 469 |
const char *_ukeys[] = { |
| 470 |
#define K_UNIT__PT 0 |
| 471 |
#define K_UNIT__IN 1 |
| 472 |
#define K_UNIT__CM 2 |
| 473 |
#define K_UNIT__MM 3 |
| 474 |
#define K_UNIT__BP 4 |
| 475 |
#define K_UNIT__PC 5 |
| 476 |
#define K_UNIT__DD 6 |
| 477 |
#define K_UNIT__CC 7 |
| 478 |
#define K_UNIT__SP 8 |
| 479 |
"pt", "in", "cm", "mm", "bp", "pc", "dd", "cc", "sp", |
| 480 |
#define K_UNIT__PX 9 |
| 481 |
"px", |
| 482 |
NULL |
| 483 |
}; |
| 484 |
int k; |
| 485 |
|
| 486 |
q = parse_float_decimal(&p, p + strlen(p)); |
| 487 |
if (!q) { |
| 488 |
WARN("Invalid length value: %s (%c)", a, *p); |
| 489 |
return 0.0; |
| 490 |
} |
| 491 |
|
| 492 |
v = atof(q); |
| 493 |
RELEASE(q); |
| 494 |
|
| 495 |
q = parse_c_ident(&p, p + strlen(p)); |
| 496 |
if (q) { |
| 497 |
for (k = 0; _ukeys[k] && strcmp(_ukeys[k], q); k++); |
| 498 |
switch (k) { |
| 499 |
case K_UNIT__PT: u *= 72.0 / 72.27; break; |
| 500 |
case K_UNIT__IN: u *= 72.0; break; |
| 501 |
case K_UNIT__CM: u *= 72.0 / 2.54 ; break; |
| 502 |
case K_UNIT__MM: u *= 72.0 / 25.4 ; break; |
| 503 |
case K_UNIT__BP: u *= 1.0 ; break; |
| 504 |
case K_UNIT__PC: u *= 12.0 * 72.0 / 72.27 ; break; |
| 505 |
case K_UNIT__DD: u *= 1238.0 / 1157.0 * 72.0 / 72.27 ; break; |
| 506 |
case K_UNIT__CC: u *= 12.0 * 1238.0 / 1157.0 * 72.0 / 72.27 ; break; |
| 507 |
case K_UNIT__SP: u *= 72.0 / (72.27 * 65536) ; break; |
| 508 |
case K_UNIT__PX: u *= 1.0 ; break; /* 72dpi */ |
| 509 |
default: |
| 510 |
WARN("Unknown unit of measure: %s", q); |
| 511 |
break; |
| 512 |
} |
| 513 |
RELEASE(q); |
| 514 |
} |
| 515 |
|
| 516 |
return v * u; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
#ifdef ENABLE_HTML_SVG_OPACITY |
| 521 |
/* Replicated from spc_tpic */ |
| 522 |
static pdf_obj * |
| 523 |
create_xgstate (double a) |
| 524 |
{ |
| 525 |
pdf_obj *dict; |
| 526 |
|
| 527 |
dict = pdf_new_dict(); |
| 528 |
pdf_add_dict(dict, pdf_new_name("Type"), pdf_new_name("ExtGState")); |
| 529 |
pdf_add_dict(dict, pdf_new_name("ca"), pdf_new_number(a)); |
| 530 |
|
| 531 |
return dict; |
| 532 |
} |
| 533 |
#endif /* ENABLE_HTML_SVG_OPACITY */ |
| 534 |
|
| 535 |
static int |
| 536 |
spc_html__img_empty (struct spc_env *spe, pdf_obj *attr) |
| 537 |
{ |
| 538 |
pdf_obj *src, *obj; |
| 539 |
transform_info ti; |
| 540 |
load_options options = {1, 0, NULL}; |
| 541 |
int id, error = 0; |
| 542 |
#ifdef ENABLE_HTML_SVG_OPACITY |
| 543 |
double alpha = 1.0; /* meaning fully opaque */ |
| 544 |
#endif /* ENABLE_HTML_SVG_OPACITY */ |
| 545 |
|
| 546 |
spc_warn(spe, "html \"img\" tag found (not completed, plese don't use!)."); |
| 547 |
|
| 548 |
src = pdf_lookup_dict(attr, "src"); |
| 549 |
if (!src) { |
| 550 |
spc_warn(spe, "\"src\" attribute not found for \"img\" tag!"); |
| 551 |
return -1; |
| 552 |
} |
| 553 |
|
| 554 |
transform_info_clear(&ti); |
| 555 |
obj = pdf_lookup_dict(attr, "width"); |
| 556 |
if (obj) { |
| 557 |
ti.width = atopt(pdf_string_value(obj)); |
| 558 |
ti.flags |= INFO_HAS_WIDTH; |
| 559 |
} |
| 560 |
obj = pdf_lookup_dict(attr, "height"); |
| 561 |
if (obj) { |
| 562 |
ti.height = atopt(pdf_string_value(obj)); |
| 563 |
ti.flags |= INFO_HAS_HEIGHT; |
| 564 |
} |
| 565 |
|
| 566 |
#ifdef ENABLE_HTML_SVG_OPACITY |
| 567 |
obj = pdf_lookup_dict(attr, "svg:opacity"); |
| 568 |
if (obj) { |
| 569 |
alpha = atof(pdf_string_value(obj)); |
| 570 |
if (alpha < 0.0 || alpha > 1.0) { |
| 571 |
spc_warn(spe, "Invalid opacity value: %s", pdf_string_value(obj)); |
| 572 |
alpha = 1.0; |
| 573 |
} |
| 574 |
} |
| 575 |
#endif /* ENABLE_HTML_SVG_OPCAITY */ |
| 576 |
|
| 577 |
#ifdef ENABLE_HTML_SVG_TRANSFORM |
| 578 |
obj = pdf_lookup_dict(attr, "svg:transform"); |
| 579 |
if (obj) { |
| 580 |
const char *p = pdf_string_value(obj); |
| 581 |
pdf_tmatrix N; |
| 582 |
for ( ; *p && isspace((unsigned char)*p); p++); |
| 583 |
while (*p && !error) { |
| 584 |
pdf_setmatrix(&N, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0); |
| 585 |
error = cvt_a_to_tmatrix(&N, p, &p); |
| 586 |
if (!error) { |
| 587 |
N.f = -N.f; |
| 588 |
pdf_concatmatrix(&ti.matrix, &N); |
| 589 |
for ( ; *p && isspace((unsigned char)*p); p++); |
| 590 |
if (*p == ',') |
| 591 |
for (++p; *p && isspace((unsigned char)*p); p++); |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
#endif /* ENABLE_HTML_SVG_TRANSFORM */ |
| 596 |
|
| 597 |
if (error) { |
| 598 |
spc_warn(spe, "Error in html \"img\" tag attribute."); |
| 599 |
return error; |
| 600 |
} |
| 601 |
|
| 602 |
id = pdf_ximage_load_image(NULL, pdf_string_value(src), options); |
| 603 |
if (id < 0) { |
| 604 |
spc_warn(spe, "Could not find/load image: %s", pdf_string_value(src)); |
| 605 |
error = -1; |
| 606 |
} else { |
| 607 |
#ifdef ENABLE_HTML_SVG_OPACITY |
| 608 |
if (alpha != 0.0) { |
| 609 |
pdf_obj *dict; |
| 610 |
dict = create_xgstate(alpha); |
| 611 |
pdf_dev_xgstate_push(dict); |
| 612 |
} |
| 613 |
#endif |
| 614 |
spc_put_image(spe, id, &ti, spe->x_user, spe->y_user); |
| 615 |
#ifdef ENABLE_HTML_SVG_OPACITY |
| 616 |
if (alpha != 0.0) { |
| 617 |
pdf_dev_xgstate_pop(); |
| 618 |
} |
| 619 |
#endif |
| 620 |
} |
| 621 |
|
| 622 |
return error; |
| 623 |
} |
| 624 |
#else |
| 625 |
static int |
| 626 |
spc_html__img_empty (struct spc_env *spe, pdf_obj *attr) |
| 627 |
{ |
| 628 |
spc_warn(spe, "IMG tag not yet supported yet..."); |
| 629 |
return -1; |
| 630 |
} |
| 631 |
#endif /* ENABLE_HTML_IMG_SUPPORT */ |
| 632 |
|
| 633 |
|
| 634 |
static int |
| 635 |
spc_handler_html_default (struct spc_env *spe, struct spc_arg *ap) |
| 636 |
{ |
| 637 |
struct spc_html_ *sd = &_html_state; |
| 638 |
char name[HTML_TAG_NAME_MAX + 1]; |
| 639 |
pdf_obj *attr; |
| 640 |
int error = 0, type = HTML_TAG_TYPE_OPEN; |
| 641 |
|
| 642 |
if (ap->curptr >= ap->endptr) |
| 643 |
return 0; |
| 644 |
|
| 645 |
attr = pdf_new_dict(); |
| 646 |
error = read_html_tag(name, attr, &type, &ap->curptr, ap->endptr); |
| 647 |
if (error) { |
| 648 |
pdf_release_obj(attr); |
| 649 |
return error; |
| 650 |
} |
| 651 |
if (!strcmp(name, "a")) { |
| 652 |
switch (type) { |
| 653 |
case HTML_TAG_TYPE_OPEN: |
| 654 |
error = spc_html__anchor_open (spe, attr, sd); |
| 655 |
break; |
| 656 |
case HTML_TAG_TYPE_CLOSE: |
| 657 |
error = spc_html__anchor_close(spe, sd); |
| 658 |
break; |
| 659 |
default: |
| 660 |
spc_warn(spe, "Empty html anchor tag???"); |
| 661 |
error = -1; |
| 662 |
break; |
| 663 |
} |
| 664 |
} else if (!strcmp(name, "base")) { |
| 665 |
if (type == HTML_TAG_TYPE_CLOSE) { |
| 666 |
spc_warn(spe, "Close tag for \"base\"???"); |
| 667 |
error = -1; |
| 668 |
} else { /* treat "open" same as "empty" */ |
| 669 |
error = spc_html__base_empty(spe, attr, sd); |
| 670 |
} |
| 671 |
} else if (!strcmp(name, "img")) { |
| 672 |
if (type == HTML_TAG_TYPE_CLOSE) { |
| 673 |
spc_warn(spe, "Close tag for \"img\"???"); |
| 674 |
error = -1; |
| 675 |
} else { /* treat "open" same as "empty" */ |
| 676 |
error = spc_html__img_empty(spe, attr); |
| 677 |
} |
| 678 |
} |
| 679 |
pdf_release_obj(attr); |
| 680 |
|
| 681 |
for ( ; ap->curptr < ap->endptr && isspace((unsigned char)ap->curptr[0]); ap->curptr++); |
| 682 |
|
| 683 |
return error; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
#ifdef ENABLE_HTML_SVG_TRANSFORM |
| 688 |
/* translate wsp* '(' wsp* number (comma-wsp number)? wsp* ')' */ |
| 689 |
static int |
| 690 |
cvt_a_to_tmatrix (pdf_tmatrix *M, const char *ptr, const char **nextptr) |
| 691 |
{ |
| 692 |
char *q; |
| 693 |
const char *p = ptr; |
| 694 |
int n; |
| 695 |
double v[6]; |
| 696 |
static const char *_tkeys[] = { |
| 697 |
#define K_TRNS__MATRIX 0 |
| 698 |
"matrix", /* a b c d e f */ |
| 699 |
#define K_TRNS__TRANSLATE 1 |
| 700 |
"translate", /* tx [ty] : dflt. tf = 0 */ |
| 701 |
#define K_TRNS__SCALE 2 |
| 702 |
"scale", /* sx [sy] : dflt. sy = sx */ |
| 703 |
#define K_TRNS__ROTATE 3 |
| 704 |
"rotate", /* ang [cx cy] : dflt. cx, cy = 0 */ |
| 705 |
#define K_TRNS__SKEWX 4 |
| 706 |
#define K_TRNS__SKEWY 5 |
| 707 |
"skewX", /* ang */ |
| 708 |
"skewY", /* ang */ |
| 709 |
NULL |
| 710 |
}; |
| 711 |
int k; |
| 712 |
|
| 713 |
for ( ; *p && isspace((unsigned char)*p); p++); |
| 714 |
|
| 715 |
q = parse_c_ident(&p, p + strlen(p)); |
| 716 |
if (!q) |
| 717 |
return -1; |
| 718 |
/* parsed transformation key */ |
| 719 |
for (k = 0; _tkeys[k] && strcmp(q, _tkeys[k]); k++); |
| 720 |
RELEASE(q); |
| 721 |
|
| 722 |
/* handle args */ |
| 723 |
for ( ; *p && isspace((unsigned char)*p); p++); |
| 724 |
if (*p != '(' || *(p + 1) == 0) |
| 725 |
return -1; |
| 726 |
for (++p; *p && isspace((unsigned char)*p); p++); |
| 727 |
for (n = 0; n < 6 && *p && *p != ')'; n++) { |
| 728 |
q = parse_float_decimal(&p, p + strlen(p)); |
| 729 |
if (!q) |
| 730 |
break; |
| 731 |
else { |
| 732 |
v[n] = atof(q); |
| 733 |
if (*p == ',') |
| 734 |
p++; |
| 735 |
for ( ; *p && isspace((unsigned char)*p); p++); |
| 736 |
if (*p == ',') |
| 737 |
for (++p; *p && isspace((unsigned char)*p); p++); |
| 738 |
RELEASE(q); |
| 739 |
} |
| 740 |
} |
| 741 |
if (*p != ')') |
| 742 |
return -1; |
| 743 |
p++; |
| 744 |
|
| 745 |
switch (k) { |
| 746 |
case K_TRNS__MATRIX: |
| 747 |
if (n != 6) |
| 748 |
return -1; |
| 749 |
M->a = v[0]; M->c = v[1]; |
| 750 |
M->b = v[2]; M->d = v[3]; |
| 751 |
M->e = v[4]; M->f = v[5]; |
| 752 |
break; |
| 753 |
case K_TRNS__TRANSLATE: |
| 754 |
if (n != 1 && n != 2) |
| 755 |
return -1; |
| 756 |
M->a = M->d = 1.0; |
| 757 |
M->c = M->b = 0.0; |
| 758 |
M->e = v[0]; M->f = (n == 2) ? v[1] : 0.0; |
| 759 |
break; |
| 760 |
case K_TRNS__SCALE: |
| 761 |
if (n != 1 && n != 2) |
| 762 |
return -1; |
| 763 |
M->a = v[0]; M->d = (n == 2) ? v[1] : v[0]; |
| 764 |
M->c = M->b = 0.0; |
| 765 |
M->e = M->f = 0.0; |
| 766 |
break; |
| 767 |
case K_TRNS__ROTATE: |
| 768 |
if (n != 1 && n != 3) |
| 769 |
return -1; |
| 770 |
M->a = cos(v[0] * M_PI / 180.0); |
| 771 |
M->c = sin(v[0] * M_PI / 180.0); |
| 772 |
M->b = -M->c; M->d = M->a; |
| 773 |
M->e = (n == 3) ? v[1] : 0.0; |
| 774 |
M->f = (n == 3) ? v[2] : 0.0; |
| 775 |
break; |
| 776 |
case K_TRNS__SKEWX: |
| 777 |
if (n != 1) |
| 778 |
return -1; |
| 779 |
M->a = M->d = 1.0; |
| 780 |
M->c = 0.0; |
| 781 |
M->b = tan(v[0] * M_PI / 180.0); |
| 782 |
break; |
| 783 |
case K_TRNS__SKEWY: |
| 784 |
if (n != 1) |
| 785 |
return -1; |
| 786 |
M->a = M->d = 1.0; |
| 787 |
M->c = tan(v[0] * M_PI / 180.0); |
| 788 |
M->b = 0.0; |
| 789 |
break; |
| 790 |
} |
| 791 |
|
| 792 |
if (nextptr) |
| 793 |
*nextptr = p; |
| 794 |
return 0; |
| 795 |
} |
| 796 |
#endif /* ENABLE_HTML_SVG_TRANSFORM */ |
| 797 |
|
| 798 |
int |
| 799 |
spc_html_at_begin_document (void) |
| 800 |
{ |
| 801 |
struct spc_html_ *sd = &_html_state; |
| 802 |
return spc_handler_html__init(sd); |
| 803 |
} |
| 804 |
|
| 805 |
int |
| 806 |
spc_html_at_begin_page (void) |
| 807 |
{ |
| 808 |
struct spc_html_ *sd = &_html_state; |
| 809 |
return spc_handler_html__bophook(NULL, sd); |
| 810 |
} |
| 811 |
|
| 812 |
int |
| 813 |
spc_html_at_end_page (void) |
| 814 |
{ |
| 815 |
struct spc_html_ *sd = &_html_state; |
| 816 |
return spc_handler_html__eophook(NULL, sd); |
| 817 |
} |
| 818 |
|
| 819 |
int |
| 820 |
spc_html_at_end_document (void) |
| 821 |
{ |
| 822 |
struct spc_html_ *sd = &_html_state; |
| 823 |
return spc_handler_html__clean(NULL, sd); |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
int |
| 828 |
spc_html_check_special (const char *buffer, int size) |
| 829 |
{ |
| 830 |
const char *p, *endptr; |
| 831 |
|
| 832 |
p = buffer; |
| 833 |
endptr = p + size; |
| 834 |
|
| 835 |
for ( ; p < endptr && isspace((unsigned char)*p); p++); |
| 836 |
size = (int) (endptr - p); |
| 837 |
if (size >= strlen("html:") && |
| 838 |
!memcmp(p, "html:", strlen("html:"))) { |
| 839 |
return 1; |
| 840 |
} |
| 841 |
|
| 842 |
return 0; |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
int |
| 847 |
spc_html_setup_handler (struct spc_handler *sph, |
| 848 |
struct spc_env *spe, struct spc_arg *ap) |
| 849 |
{ |
| 850 |
ASSERT(sph && spe && ap); |
| 851 |
|
| 852 |
for ( ; ap->curptr < ap->endptr && isspace((unsigned char)ap->curptr[0]); ap->curptr++); |
| 853 |
if (ap->curptr + strlen("html:") > ap->endptr || |
| 854 |
memcmp(ap->curptr, "html:", strlen("html:"))) { |
| 855 |
return -1; |
| 856 |
} |
| 857 |
|
| 858 |
ap->command = ""; |
| 859 |
|
| 860 |
sph->key = "html:"; |
| 861 |
sph->exec = &spc_handler_html_default; |
| 862 |
|
| 863 |
ap->curptr += strlen("html:"); |
| 864 |
for ( ; ap->curptr < ap->endptr && isspace((unsigned char)ap->curptr[0]); ap->curptr++); |
| 865 |
|
| 866 |
return 0; |
| 867 |
} |