| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2021 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
Copyright (C) 2012-2015 by Khaled Hosny <[email protected]> |
| 7 |
|
| 8 |
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]> |
| 9 |
|
| 10 |
This program is free software; you can redistribute it and/or modify |
| 11 |
it under the terms of the GNU General Public License as published by |
| 12 |
the Free Software Foundation; either version 2 of the License, or |
| 13 |
(at your option) any later version. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifdef HAVE_CONFIG_H |
| 26 |
#include <config.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <sys/types.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <ctype.h> |
| 33 |
#include <string.h> |
| 34 |
|
| 35 |
#ifdef WIN32 |
| 36 |
#include <fcntl.h> |
| 37 |
#include <io.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#include "system.h" |
| 41 |
#include "mem.h" |
| 42 |
#include "error.h" |
| 43 |
#include "mfileio.h" |
| 44 |
#include "numbers.h" |
| 45 |
#include "dpxconf.h" |
| 46 |
#include "dpxutil.h" |
| 47 |
|
| 48 |
#include "pdfdev.h" |
| 49 |
#include "pdfdoc.h" |
| 50 |
#include "pdfparse.h" |
| 51 |
#include "pdfresource.h" |
| 52 |
#include "pdfdraw.h" |
| 53 |
|
| 54 |
#include "fontmap.h" |
| 55 |
|
| 56 |
#include "dvicodes.h" |
| 57 |
#include "tfm.h" |
| 58 |
#include "vf.h" |
| 59 |
#include "subfont.h" |
| 60 |
|
| 61 |
#include "spc_util.h" |
| 62 |
#include "specials.h" |
| 63 |
|
| 64 |
#include "dvi.h" |
| 65 |
#include "dvipdfmx.h" |
| 66 |
|
| 67 |
#include "dpxfile.h" |
| 68 |
#include "pdfximage.h" |
| 69 |
#include "tt_aux.h" |
| 70 |
#include "tt_table.h" |
| 71 |
#include "t1_load.h" |
| 72 |
#include "t1_char.h" |
| 73 |
#include "cff_dict.h" |
| 74 |
|
| 75 |
#define DVI_STACK_DEPTH_MAX 256u |
| 76 |
#define TEX_FONTS_ALLOC_SIZE 16u |
| 77 |
#define VF_NESTING_MAX 16u |
| 78 |
|
| 79 |
/* UTF-32 over U+FFFF -> UTF-16 surrogate pair */ |
| 80 |
#define UTF32toUTF16HS(x) (0xd800 + (((x-0x10000) >> 10) & 0x3ff)) |
| 81 |
#define UTF32toUTF16LS(x) (0xdc00 + ( x & 0x3ff)) |
| 82 |
|
| 83 |
/* Interal Variables */ |
| 84 |
static FILE *dvi_file = NULL; |
| 85 |
static char linear = 0; /* set to 1 for strict linear processing of the input */ |
| 86 |
|
| 87 |
static int32_t *page_loc = NULL; |
| 88 |
static unsigned int num_pages = 0; |
| 89 |
|
| 90 |
static uint32_t dvi_file_size = 0; |
| 91 |
|
| 92 |
static struct dvi_header |
| 93 |
{ |
| 94 |
uint32_t unit_num; |
| 95 |
uint32_t unit_den; |
| 96 |
uint32_t mag; |
| 97 |
uint32_t media_width, media_height; |
| 98 |
unsigned int stackdepth; |
| 99 |
char comment[257]; |
| 100 |
} dvi_info = { |
| 101 |
25400000 , /* num */ |
| 102 |
473628672, /* den */ |
| 103 |
1000, /* mag */ |
| 104 |
0, 0, /* media width and height */ |
| 105 |
0, /* stackdepth */ |
| 106 |
{'\0'} /* comment */ |
| 107 |
}; |
| 108 |
|
| 109 |
static double dev_origin_x = 72.0, dev_origin_y = 770.0; |
| 110 |
|
| 111 |
double get_origin (int x) |
| 112 |
{ |
| 113 |
return x ? dev_origin_x : dev_origin_y; |
| 114 |
} |
| 115 |
|
| 116 |
#define LTYPESETTING 0 /* typesetting from left to right */ |
| 117 |
#define RTYPESETTING 1 /* typesetting from right to left */ |
| 118 |
#define SKIMMING 2 /* skimming through reflected segment measuring its width */ |
| 119 |
#define REVERSE(MODE) (LTYPESETTING + RTYPESETTING - MODE) |
| 120 |
|
| 121 |
struct dvi_lr |
| 122 |
{ |
| 123 |
int state, font; |
| 124 |
unsigned int buf_index; |
| 125 |
}; |
| 126 |
|
| 127 |
static struct dvi_lr lr_state; /* state at start of current skimming */ |
| 128 |
static int lr_mode; /* current direction or skimming depth */ |
| 129 |
static uint32_t lr_width; /* total width of reflected segment */ |
| 130 |
static uint32_t lr_width_stack[DVI_STACK_DEPTH_MAX]; |
| 131 |
static unsigned lr_width_stack_depth = 0; |
| 132 |
|
| 133 |
#define PHYSICAL 1 |
| 134 |
#define VIRTUAL 2 |
| 135 |
#define SUBFONT 3 |
| 136 |
#define NATIVE 4 |
| 137 |
#define DVI 1 |
| 138 |
#define VF 2 |
| 139 |
|
| 140 |
#define ENC_UNICODE 1 |
| 141 |
#define ENC_UTF16 2 |
| 142 |
|
| 143 |
struct gm { |
| 144 |
spt_t advance, ascent, descent; |
| 145 |
}; |
| 146 |
|
| 147 |
static struct loaded_font |
| 148 |
{ |
| 149 |
int type; /* Type is physical or virtual */ |
| 150 |
int font_id; /* id returned by dev (for PHYSICAL fonts) |
| 151 |
* or by vf module for (VIRTUAL fonts) |
| 152 |
*/ |
| 153 |
int subfont_id; /* id returned by subfont_locate_font() */ |
| 154 |
int tfm_id; |
| 155 |
spt_t size; |
| 156 |
int source; /* Source is either DVI or VF */ |
| 157 |
uint32_t rgba_color; |
| 158 |
uint8_t rgba_used; |
| 159 |
/* Indicates that rgba_color is used or not. |
| 160 |
* It enables full range of opacity: 0-255. |
| 161 |
*/ |
| 162 |
int xgs_id; /* Transparency ExtGState */ |
| 163 |
|
| 164 |
/* XeTeX requires glyph metrics */ |
| 165 |
struct gm *gm; |
| 166 |
int shift_gid; |
| 167 |
uint16_t num_glyphs; |
| 168 |
|
| 169 |
int layout_dir; |
| 170 |
|
| 171 |
float extend; |
| 172 |
float slant; |
| 173 |
float embolden; |
| 174 |
|
| 175 |
int is_unicode; |
| 176 |
int minbytes; |
| 177 |
char padbytes[4]; |
| 178 |
} *loaded_fonts = NULL; |
| 179 |
static int num_loaded_fonts = 0, max_loaded_fonts = 0; |
| 180 |
|
| 181 |
static void |
| 182 |
need_more_fonts (unsigned n) |
| 183 |
{ |
| 184 |
if (num_loaded_fonts + n > max_loaded_fonts) { |
| 185 |
max_loaded_fonts += TEX_FONTS_ALLOC_SIZE; |
| 186 |
loaded_fonts = RENEW (loaded_fonts, max_loaded_fonts, struct loaded_font); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
static struct font_def |
| 191 |
{ |
| 192 |
int32_t tex_id; |
| 193 |
spt_t point_size; |
| 194 |
spt_t design_size; |
| 195 |
char *font_name; |
| 196 |
int font_id; /* index of _loaded_ font in loaded_fonts array */ |
| 197 |
int used; |
| 198 |
int native; /* boolean */ |
| 199 |
uint32_t rgba_color; /* only used for native fonts in XeTeX */ |
| 200 |
uint8_t rgba_used; /* Indicates that rgba_color is used or not. |
| 201 |
* It enables full range of opacity: 0-255. |
| 202 |
*/ |
| 203 |
uint32_t face_index; |
| 204 |
int layout_dir; /* 1 = vertical, 0 = horizontal */ |
| 205 |
int extend; |
| 206 |
int slant; |
| 207 |
int embolden; |
| 208 |
} *def_fonts = NULL; |
| 209 |
|
| 210 |
#define XDV_FLAG_VERTICAL 0x0100 |
| 211 |
#define XDV_FLAG_COLORED 0x0200 |
| 212 |
#define XDV_FLAG_FEATURES 0x0400 |
| 213 |
#define XDV_FLAG_EXTEND 0x1000 |
| 214 |
#define XDV_FLAG_SLANT 0x2000 |
| 215 |
#define XDV_FLAG_EMBOLDEN 0x4000 |
| 216 |
|
| 217 |
static int num_def_fonts = 0, max_def_fonts = 0; |
| 218 |
static int compute_boxes = 0, link_annot = 1; |
| 219 |
/* If "catch_phantom" is non-zero, dvipdfmx try to catch phantom texts. |
| 220 |
* Dvipdfmx treats DVI horizontal movement instructions (x, w, right) differently |
| 221 |
* when "catch_phantom" is non-zero. Amount of horizontal move will be added to |
| 222 |
* the current annotation rectangle with "height" and "depth" estimated as, |
| 223 |
* |
| 224 |
* catch_phantom=1: with current font size |
| 225 |
* catch_phantom=2: with specified height and depth |
| 226 |
*/ |
| 227 |
static int catch_phantom = 0; |
| 228 |
static double phantom_height = 0.0; |
| 229 |
static double phantom_depth = 0.0; |
| 230 |
|
| 231 |
#define DVI_PAGE_BUF_CHUNK 0x10000U /* 64K should be plenty for most pages */ |
| 232 |
|
| 233 |
static unsigned char* dvi_page_buffer; |
| 234 |
static unsigned int dvi_page_buf_size; |
| 235 |
static unsigned int dvi_page_buf_index; |
| 236 |
|
| 237 |
/* functions to read numbers from the dvi file and store them in dvi_page_buffer */ |
| 238 |
static int get_and_buffer_unsigned_byte (FILE *file) |
| 239 |
{ |
| 240 |
int ch; |
| 241 |
if ((ch = fgetc (file)) < 0) |
| 242 |
ERROR ("File ended prematurely\n"); |
| 243 |
if (dvi_page_buf_index >= dvi_page_buf_size) { |
| 244 |
dvi_page_buf_size += DVI_PAGE_BUF_CHUNK; |
| 245 |
dvi_page_buffer = RENEW(dvi_page_buffer, dvi_page_buf_size, unsigned char); |
| 246 |
} |
| 247 |
dvi_page_buffer[dvi_page_buf_index++] = ch; |
| 248 |
return ch; |
| 249 |
} |
| 250 |
|
| 251 |
static unsigned int get_and_buffer_unsigned_pair (FILE *file) |
| 252 |
{ |
| 253 |
unsigned int pair = get_and_buffer_unsigned_byte(file); |
| 254 |
pair = (pair << 8) | get_and_buffer_unsigned_byte(file); |
| 255 |
return pair; |
| 256 |
} |
| 257 |
|
| 258 |
static void get_and_buffer_bytes(FILE *file, unsigned int count) |
| 259 |
{ |
| 260 |
if (dvi_page_buf_index + count >= dvi_page_buf_size) { |
| 261 |
dvi_page_buf_size = dvi_page_buf_index + count + DVI_PAGE_BUF_CHUNK; |
| 262 |
dvi_page_buffer = RENEW(dvi_page_buffer, dvi_page_buf_size, unsigned char); |
| 263 |
} |
| 264 |
if (fread(dvi_page_buffer + dvi_page_buf_index, 1, count, file) != count) |
| 265 |
ERROR ("File ended prematurely\n"); |
| 266 |
dvi_page_buf_index += count; |
| 267 |
} |
| 268 |
|
| 269 |
/* functions to fetch values from dvi_page_buffer */ |
| 270 |
|
| 271 |
static int get_buffered_unsigned_byte (void) |
| 272 |
{ |
| 273 |
return dvi_page_buffer[dvi_page_buf_index++]; |
| 274 |
} |
| 275 |
|
| 276 |
static unsigned int get_buffered_unsigned_pair (void) |
| 277 |
{ |
| 278 |
unsigned int pair = dvi_page_buffer[dvi_page_buf_index++]; |
| 279 |
pair = (pair << 8) | dvi_page_buffer[dvi_page_buf_index++]; |
| 280 |
return pair; |
| 281 |
} |
| 282 |
|
| 283 |
static int32_t get_buffered_signed_quad(void) |
| 284 |
{ |
| 285 |
int i; |
| 286 |
int32_t quad = dvi_page_buffer[dvi_page_buf_index++]; |
| 287 |
/* Check sign on first byte before reading others */ |
| 288 |
if (quad >= 0x80) |
| 289 |
quad -= 0x100; |
| 290 |
for (i=0; i<3; i++) { |
| 291 |
quad = (quad << 8) | dvi_page_buffer[dvi_page_buf_index++]; |
| 292 |
} |
| 293 |
return quad; |
| 294 |
} |
| 295 |
|
| 296 |
static int32_t get_buffered_signed_num(unsigned char num) |
| 297 |
{ |
| 298 |
int32_t quad = dvi_page_buffer[dvi_page_buf_index++]; |
| 299 |
if (quad > 0x7f) |
| 300 |
quad -= 0x100; |
| 301 |
switch (num) { |
| 302 |
case 3: quad = (quad << 8) | dvi_page_buffer[dvi_page_buf_index++]; |
| 303 |
case 2: quad = (quad << 8) | dvi_page_buffer[dvi_page_buf_index++]; |
| 304 |
case 1: quad = (quad << 8) | dvi_page_buffer[dvi_page_buf_index++]; |
| 305 |
default: break; |
| 306 |
} |
| 307 |
return quad; |
| 308 |
} |
| 309 |
|
| 310 |
static int32_t get_buffered_unsigned_num(unsigned char num) |
| 311 |
{ |
| 312 |
int32_t quad = dvi_page_buffer[dvi_page_buf_index++]; |
| 313 |
switch (num) { |
| 314 |
case 3: if (quad > 0x7f) |
| 315 |
quad -= 0x100; |
| 316 |
quad = (quad << 8) | dvi_page_buffer[dvi_page_buf_index++]; |
| 317 |
case 2: quad = (quad << 8) | dvi_page_buffer[dvi_page_buf_index++]; |
| 318 |
case 1: quad = (quad << 8) | dvi_page_buffer[dvi_page_buf_index++]; |
| 319 |
default: break; |
| 320 |
} |
| 321 |
return quad; |
| 322 |
} |
| 323 |
|
| 324 |
#define skip_bufferd_bytes(n) dvi_page_buf_index += n |
| 325 |
|
| 326 |
unsigned int |
| 327 |
dvi_npages (void) |
| 328 |
{ |
| 329 |
return num_pages; |
| 330 |
} |
| 331 |
|
| 332 |
static const char invalid_signature[] = |
| 333 |
"Something is wrong. Are you sure this is a DVI file?"; |
| 334 |
|
| 335 |
#define range_check_loc(loc) \ |
| 336 |
if ((loc) > dvi_file_size) {\ |
| 337 |
ERROR(invalid_signature); \ |
| 338 |
} |
| 339 |
|
| 340 |
static int pre_id_byte, post_id_byte, is_ptex = 0, has_ptex = 0; |
| 341 |
|
| 342 |
static void |
| 343 |
check_id_bytes (void) { |
| 344 |
if (pre_id_byte != post_id_byte && (pre_id_byte != DVI_ID || post_id_byte != DVIV_ID)) |
| 345 |
ERROR ("Inconsistent DVI id_bytes %d (pre) and %d (post)", pre_id_byte, post_id_byte); |
| 346 |
} |
| 347 |
|
| 348 |
static void |
| 349 |
need_XeTeX (int c) { |
| 350 |
if (dpx_conf.compat_mode != dpx_mode_xdv_mode) |
| 351 |
ERROR ("DVI opcode %i only valid for XeTeX", c); |
| 352 |
} |
| 353 |
|
| 354 |
static void |
| 355 |
need_pTeX (int c) { |
| 356 |
if (!is_ptex) |
| 357 |
ERROR ("DVI opcode %i only valid for Ascii pTeX", c); |
| 358 |
has_ptex = 1; |
| 359 |
} |
| 360 |
|
| 361 |
static int32_t |
| 362 |
find_post (void) |
| 363 |
{ |
| 364 |
off_t dvi_size; |
| 365 |
int32_t current; |
| 366 |
int ch; |
| 367 |
|
| 368 |
/* First find end of file */ |
| 369 |
dvi_size = xfile_size (dvi_file, "DVI"); |
| 370 |
if (dvi_size > 0x7fffffff) |
| 371 |
ERROR("DVI file size exceeds 31-bit"); |
| 372 |
dvi_file_size = dvi_size; |
| 373 |
current = dvi_size; |
| 374 |
|
| 375 |
/* Scan backwards through PADDING */ |
| 376 |
do { |
| 377 |
xseek_absolute (dvi_file, --current, "DVI"); |
| 378 |
} while ((ch = fgetc(dvi_file)) == PADDING && |
| 379 |
current > 0); |
| 380 |
|
| 381 |
/* file_position now points to last non padding character or |
| 382 |
* beginning of file */ |
| 383 |
if (dvi_file_size - current < 4 || current == 0 || |
| 384 |
!(ch == DVI_ID || ch == DVIV_ID || ch == XDV_ID || ch == XDV_ID_OLD)) { |
| 385 |
MESG("DVI ID = %d\n", ch); |
| 386 |
ERROR(invalid_signature); |
| 387 |
} |
| 388 |
|
| 389 |
post_id_byte = ch; |
| 390 |
if (ch == XDV_ID || ch == XDV_ID_OLD) |
| 391 |
dpx_conf.compat_mode = dpx_mode_xdv_mode; |
| 392 |
dvi_ptex_with_vert = is_ptex = ch == DVIV_ID; |
| 393 |
|
| 394 |
/* Make sure post_post is really there */ |
| 395 |
current = current - 5; |
| 396 |
xseek_absolute (dvi_file, current, "DVI"); |
| 397 |
if ((ch = fgetc(dvi_file)) != POST_POST) { |
| 398 |
MESG("Found %d where post_post opcode should be\n", ch); |
| 399 |
ERROR(invalid_signature); |
| 400 |
} |
| 401 |
current = get_signed_quad(dvi_file); |
| 402 |
xseek_absolute (dvi_file, current, "DVI"); |
| 403 |
if ((ch = fgetc(dvi_file)) != POST) { |
| 404 |
MESG("Found %d where post_post opcode should be\n", ch); |
| 405 |
ERROR(invalid_signature); |
| 406 |
} |
| 407 |
|
| 408 |
/* Finally check the ID byte in the preamble */ |
| 409 |
/* An Ascii pTeX DVI file has id_byte DVI_ID in the preamble but DVIV_ID in the postamble. */ |
| 410 |
xseek_absolute (dvi_file, 0, "DVI"); |
| 411 |
if ((ch = get_unsigned_byte(dvi_file)) != PRE) { |
| 412 |
MESG("Found %d where PRE was expected\n", ch); |
| 413 |
ERROR(invalid_signature); |
| 414 |
} |
| 415 |
ch = get_unsigned_byte(dvi_file); |
| 416 |
if (!(ch == DVI_ID || ch == XDV_ID || ch == XDV_ID_OLD)) { |
| 417 |
MESG("DVI ID = %d\n", ch); |
| 418 |
ERROR(invalid_signature); |
| 419 |
} |
| 420 |
pre_id_byte = ch; |
| 421 |
check_id_bytes(); |
| 422 |
|
| 423 |
return current; |
| 424 |
} |
| 425 |
|
| 426 |
static void |
| 427 |
get_page_info (int32_t post_location) |
| 428 |
{ |
| 429 |
int i; |
| 430 |
|
| 431 |
xseek_absolute (dvi_file, post_location + 27, "DVI"); |
| 432 |
num_pages = get_unsigned_pair(dvi_file); |
| 433 |
if (num_pages == 0) { |
| 434 |
ERROR("Page count is 0!"); |
| 435 |
} |
| 436 |
if (dpx_conf.verbose_level > 2) { |
| 437 |
MESG("Page count:\t %4d\n", num_pages); |
| 438 |
} |
| 439 |
|
| 440 |
page_loc = NEW(num_pages, int32_t); |
| 441 |
|
| 442 |
xseek_absolute (dvi_file, post_location + 1, "DVI"); |
| 443 |
page_loc[num_pages-1] = get_unsigned_quad(dvi_file); |
| 444 |
range_check_loc(page_loc[num_pages-1] + 41); |
| 445 |
for (i = num_pages - 2; i >= 0; i--) { |
| 446 |
xseek_absolute (dvi_file, page_loc[i+1] + 41, "DVI"); |
| 447 |
page_loc[i] = get_unsigned_quad(dvi_file); |
| 448 |
range_check_loc(page_loc[num_pages-1] + 41); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
/* Following are computed "constants" used for unit conversion */ |
| 453 |
static double dvi2pts = 1.52018, total_mag = 1.0; |
| 454 |
|
| 455 |
double |
| 456 |
dvi_tell_mag (void) |
| 457 |
{ |
| 458 |
return total_mag; |
| 459 |
} |
| 460 |
|
| 461 |
static void |
| 462 |
do_scales (double mag) |
| 463 |
{ |
| 464 |
total_mag = (double) dvi_info.mag / 1000.0 * mag; |
| 465 |
dvi2pts = (double) dvi_info.unit_num / (double) dvi_info.unit_den; |
| 466 |
dvi2pts *= (72.0 / 254000.0); |
| 467 |
} |
| 468 |
|
| 469 |
static void |
| 470 |
get_dvi_info (int32_t post_location) |
| 471 |
{ |
| 472 |
xseek_absolute (dvi_file, post_location + 5, "DVI"); |
| 473 |
|
| 474 |
dvi_info.unit_num = get_unsigned_quad(dvi_file); |
| 475 |
dvi_info.unit_den = get_unsigned_quad(dvi_file); |
| 476 |
dvi_info.mag = get_unsigned_quad(dvi_file); |
| 477 |
|
| 478 |
dvi_info.media_height = get_unsigned_quad(dvi_file); |
| 479 |
dvi_info.media_width = get_unsigned_quad(dvi_file); |
| 480 |
|
| 481 |
dvi_info.stackdepth = get_unsigned_pair(dvi_file); |
| 482 |
|
| 483 |
if (dvi_info.stackdepth > DVI_STACK_DEPTH_MAX) { |
| 484 |
WARN("DVI need stack depth of %d,", dvi_info.stackdepth); |
| 485 |
WARN("but DVI_STACK_DEPTH_MAX is %d.", DVI_STACK_DEPTH_MAX); |
| 486 |
ERROR("Capacity exceeded."); |
| 487 |
} |
| 488 |
|
| 489 |
if (dpx_conf.verbose_level > 2) { |
| 490 |
MESG("DVI File Info\n"); |
| 491 |
MESG("Unit: %ld / %ld\n", dvi_info.unit_num, dvi_info.unit_den); |
| 492 |
MESG("Magnification: %ld\n", dvi_info.mag); |
| 493 |
MESG("Media Height: %ld\n", dvi_info.media_height); |
| 494 |
MESG("Media Width: %ld\n", dvi_info.media_width); |
| 495 |
MESG("Stack Depth: %d\n", dvi_info.stackdepth); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
static void |
| 500 |
get_preamble_dvi_info (void) |
| 501 |
{ |
| 502 |
int ch; |
| 503 |
|
| 504 |
ch = get_unsigned_byte(dvi_file); |
| 505 |
if (ch != PRE) { |
| 506 |
MESG("Found %d where PRE was expected\n", ch); |
| 507 |
ERROR(invalid_signature); |
| 508 |
} |
| 509 |
|
| 510 |
/* An Ascii pTeX DVI file has id_byte DVI_ID in the preamble but DVIV_ID in the postamble. */ |
| 511 |
ch = get_unsigned_byte(dvi_file); |
| 512 |
if (!(ch == DVI_ID || ch == XDV_ID || ch == XDV_ID_OLD)) { |
| 513 |
MESG("DVI ID = %d\n", ch); |
| 514 |
ERROR(invalid_signature); |
| 515 |
} |
| 516 |
|
| 517 |
pre_id_byte = ch; |
| 518 |
if (ch == XDV_ID || ch == XDV_ID_OLD) |
| 519 |
dpx_conf.compat_mode = dpx_mode_xdv_mode; |
| 520 |
is_ptex = ch == DVI_ID; /* maybe */ |
| 521 |
|
| 522 |
dvi_info.unit_num = get_positive_quad(dvi_file, "DVI", "unit_num"); |
| 523 |
dvi_info.unit_den = get_positive_quad(dvi_file, "DVI", "unit_den"); |
| 524 |
dvi_info.mag = get_positive_quad(dvi_file, "DVI", "mag"); |
| 525 |
|
| 526 |
ch = get_unsigned_byte(dvi_file); |
| 527 |
if (fread(dvi_info.comment, |
| 528 |
1, ch, dvi_file) != ch) { |
| 529 |
ERROR(invalid_signature); |
| 530 |
} |
| 531 |
dvi_info.comment[ch] = '\0'; |
| 532 |
|
| 533 |
if (dpx_conf.verbose_level > 2) { |
| 534 |
MESG("DVI File Info\n"); |
| 535 |
MESG("Unit: %ld / %ld\n", dvi_info.unit_num, dvi_info.unit_den); |
| 536 |
MESG("Magnification: %ld\n", dvi_info.mag); |
| 537 |
} |
| 538 |
|
| 539 |
if (dpx_conf.verbose_level > 0) { |
| 540 |
MESG("DVI Comment: %s\n", dvi_info.comment); |
| 541 |
} |
| 542 |
|
| 543 |
num_pages = 0x7FFFFFFU; /* for linear processing: we just keep going! */ |
| 544 |
} |
| 545 |
|
| 546 |
const char * |
| 547 |
dvi_comment (void) |
| 548 |
{ |
| 549 |
return dvi_info.comment; |
| 550 |
} |
| 551 |
|
| 552 |
static void |
| 553 |
proc_dvilua_font_record (int32_t tex_id, const char *font_name, uint32_t point_size, uint32_t design_size) |
| 554 |
{ |
| 555 |
char *file_name, *p, *q, *endptr; |
| 556 |
uint32_t index = 0; |
| 557 |
uint32_t embolden = 0; |
| 558 |
int32_t slant = 0, extend = 0x00010000; |
| 559 |
|
| 560 |
ASSERT(font_name[0] == '['); |
| 561 |
|
| 562 |
/* redundant check but leave this here anyway */ |
| 563 |
if (num_def_fonts >= max_def_fonts) { |
| 564 |
max_def_fonts += TEX_FONTS_ALLOC_SIZE; |
| 565 |
def_fonts = RENEW (def_fonts, max_def_fonts, struct font_def); |
| 566 |
} |
| 567 |
|
| 568 |
file_name = NEW(strlen(font_name) + 1, char); |
| 569 |
strcpy(file_name, font_name + 1); |
| 570 |
endptr = file_name + strlen(file_name); |
| 571 |
q = strchr(file_name, ']'); |
| 572 |
if (q == NULL) { |
| 573 |
ERROR("Syntax error in dvilua fnt_def: no ']' found in font name."); |
| 574 |
} |
| 575 |
*q = '\0'; |
| 576 |
|
| 577 |
p = q + 1; |
| 578 |
if (p < endptr && *p == ':') { |
| 579 |
for (p++; *p != '\0' && p < endptr; ) { |
| 580 |
char *kvsep, *delim; |
| 581 |
|
| 582 |
delim = strchr(p, ';'); |
| 583 |
kvsep = strchr(p, '='); |
| 584 |
if (delim == NULL) |
| 585 |
delim = endptr; |
| 586 |
if (kvsep == NULL || kvsep >= delim) { |
| 587 |
ERROR("Syntax error in dvilua fnt_def: not in key=value format: %s", font_name); |
| 588 |
} |
| 589 |
*kvsep = '\0'; |
| 590 |
if (!strcmp(p, "index")) { |
| 591 |
uint32_t value = strtoul(kvsep + 1, &q, 10); |
| 592 |
if (q != delim) { |
| 593 |
WARN("Syntax error in dvilua fnt_def: invalid value specified for \"%s\": %s", p, font_name); |
| 594 |
} else { |
| 595 |
index = value; |
| 596 |
} |
| 597 |
} else if (!strcmp(p, "embolden")) { |
| 598 |
int32_t value = strtol(kvsep + 1, &q, 10); |
| 599 |
if (q != delim) { |
| 600 |
WARN("Syntax error in dvilua fnt_def: invalid value specified for \"%s\": %s", p, font_name); |
| 601 |
} else { |
| 602 |
embolden = value; |
| 603 |
} |
| 604 |
} else if (!strcmp(p, "slant")) { |
| 605 |
int32_t value = strtol(kvsep + 1, &q, 10); |
| 606 |
if (q != delim) { |
| 607 |
WARN("Syntax error in dvilua fnt_def: invalid value specified for \"%s\": %s", p, font_name); |
| 608 |
} else { |
| 609 |
slant = value; |
| 610 |
} |
| 611 |
} else if (!strcmp(p, "extend")) { |
| 612 |
int32_t value = strtol(kvsep + 1, &q, 10); |
| 613 |
if (q != delim) { |
| 614 |
WARN("Syntax error in dvilua fnt_def: invalid value specified for \"%s\": %s", p, font_name); |
| 615 |
} else { |
| 616 |
extend = value; |
| 617 |
} |
| 618 |
} else { |
| 619 |
WARN("Ignoring unrecognized/unsupported key \"%s\" in dvilua fnt_def: %s", p, font_name); |
| 620 |
} |
| 621 |
p = delim + 1; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
def_fonts[num_def_fonts].tex_id = tex_id; |
| 626 |
def_fonts[num_def_fonts].font_name = file_name; |
| 627 |
def_fonts[num_def_fonts].face_index = index; |
| 628 |
def_fonts[num_def_fonts].point_size = point_size; |
| 629 |
def_fonts[num_def_fonts].design_size = design_size; |
| 630 |
def_fonts[num_def_fonts].used = 0; |
| 631 |
def_fonts[num_def_fonts].native = 1; |
| 632 |
|
| 633 |
def_fonts[num_def_fonts].layout_dir = 0; |
| 634 |
def_fonts[num_def_fonts].rgba_color = 0xffffffff; |
| 635 |
def_fonts[num_def_fonts].rgba_used = 0; |
| 636 |
def_fonts[num_def_fonts].extend = extend; |
| 637 |
def_fonts[num_def_fonts].slant = slant; |
| 638 |
def_fonts[num_def_fonts].embolden = embolden; |
| 639 |
|
| 640 |
num_def_fonts++; |
| 641 |
|
| 642 |
return; |
| 643 |
} |
| 644 |
|
| 645 |
#define SIG_DVILUA_FNT_DEF ('L' << 24|'u' << 16|'a' << 8|'F') |
| 646 |
|
| 647 |
static void |
| 648 |
read_font_record (int32_t tex_id) |
| 649 |
{ |
| 650 |
int dir_length, name_length; |
| 651 |
uint32_t checksum; |
| 652 |
uint32_t point_size, design_size; |
| 653 |
char *directory, *font_name; |
| 654 |
|
| 655 |
if (num_def_fonts >= max_def_fonts) { |
| 656 |
max_def_fonts += TEX_FONTS_ALLOC_SIZE; |
| 657 |
def_fonts = RENEW (def_fonts, max_def_fonts, struct font_def); |
| 658 |
} |
| 659 |
checksum = get_unsigned_quad(dvi_file); |
| 660 |
point_size = get_positive_quad(dvi_file, "DVI", "point_size"); |
| 661 |
design_size = get_positive_quad(dvi_file, "DVI", "design_size"); |
| 662 |
dir_length = get_unsigned_byte(dvi_file); |
| 663 |
name_length = get_unsigned_byte(dvi_file); |
| 664 |
|
| 665 |
directory = NEW(dir_length + 1, char); |
| 666 |
if (fread(directory, 1, dir_length, dvi_file) != dir_length) { |
| 667 |
ERROR(invalid_signature); |
| 668 |
} |
| 669 |
directory[dir_length] = '\0'; |
| 670 |
RELEASE(directory); /* unused */ |
| 671 |
|
| 672 |
font_name = NEW(name_length + 1, char); |
| 673 |
if (fread(font_name, 1, name_length, dvi_file) != name_length) { |
| 674 |
ERROR(invalid_signature); |
| 675 |
} |
| 676 |
font_name[name_length] = '\0'; |
| 677 |
|
| 678 |
if (checksum == SIG_DVILUA_FNT_DEF && name_length > 0 && font_name[0] == '[') { |
| 679 |
proc_dvilua_font_record(tex_id, font_name, point_size, design_size); |
| 680 |
RELEASE(font_name); |
| 681 |
return; |
| 682 |
} |
| 683 |
def_fonts[num_def_fonts].tex_id = tex_id; |
| 684 |
def_fonts[num_def_fonts].font_name = font_name; |
| 685 |
def_fonts[num_def_fonts].point_size = point_size; |
| 686 |
def_fonts[num_def_fonts].design_size = design_size; |
| 687 |
def_fonts[num_def_fonts].used = 0; |
| 688 |
def_fonts[num_def_fonts].native = 0; |
| 689 |
def_fonts[num_def_fonts].rgba_color = 0xffffffff; |
| 690 |
def_fonts[num_def_fonts].rgba_used = 0; |
| 691 |
def_fonts[num_def_fonts].face_index = 0; |
| 692 |
def_fonts[num_def_fonts].layout_dir = 0; |
| 693 |
def_fonts[num_def_fonts].extend = 0x00010000; /* 1.0 */ |
| 694 |
def_fonts[num_def_fonts].slant = 0; |
| 695 |
def_fonts[num_def_fonts].embolden = 0; |
| 696 |
num_def_fonts++; |
| 697 |
|
| 698 |
return; |
| 699 |
} |
| 700 |
|
| 701 |
static void |
| 702 |
read_native_font_record (int32_t tex_id) |
| 703 |
{ |
| 704 |
unsigned int flags; |
| 705 |
uint32_t point_size; |
| 706 |
char *font_name; |
| 707 |
int len; |
| 708 |
uint32_t index; |
| 709 |
|
| 710 |
if (num_def_fonts >= max_def_fonts) { |
| 711 |
max_def_fonts += TEX_FONTS_ALLOC_SIZE; |
| 712 |
def_fonts = RENEW (def_fonts, max_def_fonts, struct font_def); |
| 713 |
} |
| 714 |
point_size = get_positive_quad(dvi_file, "DVI", "point_size"); |
| 715 |
flags = get_unsigned_pair(dvi_file); |
| 716 |
|
| 717 |
len = (int) get_unsigned_byte(dvi_file); /* font name length */ |
| 718 |
font_name = NEW(len + 1, char); |
| 719 |
if (fread(font_name, 1, len, dvi_file) != len) { |
| 720 |
ERROR(invalid_signature); |
| 721 |
} |
| 722 |
font_name[len] = '\0'; |
| 723 |
|
| 724 |
index = get_positive_quad(dvi_file, "DVI", "index"); |
| 725 |
|
| 726 |
def_fonts[num_def_fonts].tex_id = tex_id; |
| 727 |
def_fonts[num_def_fonts].font_name = font_name; |
| 728 |
def_fonts[num_def_fonts].face_index = index; |
| 729 |
def_fonts[num_def_fonts].point_size = point_size; |
| 730 |
def_fonts[num_def_fonts].design_size = 655360; /* hard-code as 10pt for now, not used anyway */ |
| 731 |
def_fonts[num_def_fonts].used = 0; |
| 732 |
def_fonts[num_def_fonts].native = 1; |
| 733 |
|
| 734 |
def_fonts[num_def_fonts].layout_dir = 0; |
| 735 |
def_fonts[num_def_fonts].rgba_color = 0xffffffff; |
| 736 |
def_fonts[num_def_fonts].rgba_used = 0; |
| 737 |
def_fonts[num_def_fonts].extend = 0x00010000; |
| 738 |
def_fonts[num_def_fonts].slant = 0; |
| 739 |
def_fonts[num_def_fonts].embolden = 0; |
| 740 |
|
| 741 |
if (flags & XDV_FLAG_VERTICAL) |
| 742 |
def_fonts[num_def_fonts].layout_dir = 1; |
| 743 |
|
| 744 |
if (flags & XDV_FLAG_COLORED) { |
| 745 |
def_fonts[num_def_fonts].rgba_color = get_unsigned_quad(dvi_file); |
| 746 |
def_fonts[num_def_fonts].rgba_used = 1; |
| 747 |
} |
| 748 |
|
| 749 |
if (flags & XDV_FLAG_EXTEND) |
| 750 |
def_fonts[num_def_fonts].extend = get_signed_quad(dvi_file); |
| 751 |
|
| 752 |
if (flags & XDV_FLAG_SLANT) |
| 753 |
def_fonts[num_def_fonts].slant = get_signed_quad(dvi_file); |
| 754 |
|
| 755 |
if (flags & XDV_FLAG_EMBOLDEN) |
| 756 |
def_fonts[num_def_fonts].embolden = get_signed_quad(dvi_file); |
| 757 |
|
| 758 |
num_def_fonts++; |
| 759 |
|
| 760 |
return; |
| 761 |
} |
| 762 |
|
| 763 |
static void |
| 764 |
get_dvi_fonts (int32_t post_location) |
| 765 |
{ |
| 766 |
int code; |
| 767 |
|
| 768 |
xseek_absolute (dvi_file, post_location + 29, "DVI"); |
| 769 |
while ((code = get_unsigned_byte(dvi_file)) != POST_POST) { |
| 770 |
switch (code) { |
| 771 |
case FNT_DEF1: case FNT_DEF2: case FNT_DEF3: case FNT_DEF4: |
| 772 |
read_font_record(get_unsigned_num(dvi_file, code-FNT_DEF1)); |
| 773 |
break; |
| 774 |
case XDV_NATIVE_FONT_DEF: |
| 775 |
need_XeTeX(code); |
| 776 |
read_native_font_record(get_signed_quad(dvi_file)); |
| 777 |
break; |
| 778 |
default: |
| 779 |
MESG("Unexpected op code: %3d\n", code); |
| 780 |
ERROR(invalid_signature); |
| 781 |
} |
| 782 |
} |
| 783 |
if (dpx_conf.verbose_level > 2) { |
| 784 |
unsigned i; |
| 785 |
|
| 786 |
MESG("\n"); |
| 787 |
MESG("DVI file font info\n"); |
| 788 |
for (i = 0; i < num_def_fonts; i++) { |
| 789 |
MESG("TeX Font: %10s loaded at ID=%5d, ", |
| 790 |
def_fonts[i].font_name, def_fonts[i].tex_id); |
| 791 |
MESG("size=%5.2fpt (scaled %4.1f%%)", |
| 792 |
def_fonts[i].point_size * dvi2pts, |
| 793 |
100.0 * ((double) def_fonts[i].point_size / def_fonts[i].design_size)); |
| 794 |
MESG("\n"); |
| 795 |
} |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
static void get_comment (void) |
| 800 |
{ |
| 801 |
int length; |
| 802 |
|
| 803 |
xseek_absolute (dvi_file, 14, "DVI"); |
| 804 |
length = get_unsigned_byte(dvi_file); |
| 805 |
if (fread(dvi_info.comment, |
| 806 |
1, length, dvi_file) != length) { |
| 807 |
ERROR(invalid_signature); |
| 808 |
} |
| 809 |
dvi_info.comment[length] = '\0'; |
| 810 |
if (dpx_conf.verbose_level > 0) { |
| 811 |
MESG("DVI Comment: %s\n", dvi_info.comment); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
/* |
| 816 |
* The section below this line deals with the actual processing of the |
| 817 |
* dvi file. |
| 818 |
* |
| 819 |
* The dvi file processor state is contained in the following variables: |
| 820 |
*/ |
| 821 |
|
| 822 |
struct dvi_registers |
| 823 |
{ |
| 824 |
int32_t h, v, w, x, y, z; |
| 825 |
unsigned int d; |
| 826 |
}; |
| 827 |
|
| 828 |
static struct dvi_registers dvi_state; |
| 829 |
static struct dvi_registers dvi_stack[DVI_STACK_DEPTH_MAX]; |
| 830 |
static unsigned dvi_stack_depth = 0 ; |
| 831 |
static int current_font = -1; |
| 832 |
static int processing_page = 0 ; |
| 833 |
|
| 834 |
static void |
| 835 |
clear_state (void) |
| 836 |
{ |
| 837 |
dvi_state.h = 0; dvi_state.v = 0; dvi_state.w = 0; |
| 838 |
dvi_state.x = 0; dvi_state.y = 0; dvi_state.z = 0; |
| 839 |
dvi_state.d = 0; /* direction */ |
| 840 |
pdf_dev_set_dirmode(0); |
| 841 |
dvi_stack_depth = 0; |
| 842 |
current_font = -1; |
| 843 |
} |
| 844 |
|
| 845 |
/* Migrated from pdfdev.c: |
| 846 |
* The following codes are originally put into pdfdev.c. |
| 847 |
* But they are moved to here to make PDF output independent |
| 848 |
* from DVI input. |
| 849 |
* pdfdoc, pdfspecial and htex are also modified. pdfspecial |
| 850 |
* and htex does tag/untag depth. pdfdev and pdfdoc now does |
| 851 |
* not care about line-breaking at all. |
| 852 |
*/ |
| 853 |
static unsigned marked_depth = 0; |
| 854 |
static int tagged_depth = -1; |
| 855 |
|
| 856 |
static void |
| 857 |
dvi_mark_depth (void) |
| 858 |
{ |
| 859 |
/* If decreasing below tagged_depth */ |
| 860 |
if (link_annot && |
| 861 |
marked_depth == tagged_depth && |
| 862 |
dvi_stack_depth == tagged_depth - 1) { |
| 863 |
/* |
| 864 |
* See if this appears to be the end of a "logical unit" |
| 865 |
* that's been broken. If so, flush the logical unit. |
| 866 |
*/ |
| 867 |
pdf_doc_break_annot(); |
| 868 |
} |
| 869 |
marked_depth = dvi_stack_depth; |
| 870 |
} |
| 871 |
|
| 872 |
/* |
| 873 |
* The following routines setup and tear down a callback at a |
| 874 |
* certain stack depth. This is used to handle broken (linewise) |
| 875 |
* links. |
| 876 |
*/ |
| 877 |
void |
| 878 |
dvi_tag_depth (void) |
| 879 |
{ |
| 880 |
tagged_depth = marked_depth; |
| 881 |
dvi_compute_boxes(1); |
| 882 |
} |
| 883 |
|
| 884 |
void |
| 885 |
dvi_untag_depth (void) |
| 886 |
{ |
| 887 |
tagged_depth = -1; |
| 888 |
dvi_compute_boxes(0); |
| 889 |
} |
| 890 |
|
| 891 |
void |
| 892 |
dvi_compute_boxes (int flag) |
| 893 |
{ |
| 894 |
compute_boxes = flag; |
| 895 |
} |
| 896 |
|
| 897 |
void |
| 898 |
dvi_link_annot (int flag) |
| 899 |
{ |
| 900 |
link_annot = flag; |
| 901 |
} |
| 902 |
|
| 903 |
int |
| 904 |
dvi_is_tracking_boxes(void) |
| 905 |
{ |
| 906 |
return (compute_boxes && link_annot && marked_depth >= tagged_depth); |
| 907 |
} |
| 908 |
|
| 909 |
void |
| 910 |
dvi_set_linkmode (int mode) |
| 911 |
{ |
| 912 |
catch_phantom = mode != 0 ? 1 : 0; |
| 913 |
} |
| 914 |
|
| 915 |
void |
| 916 |
dvi_set_phantom_height (double height, double depth) |
| 917 |
{ |
| 918 |
phantom_height = height; |
| 919 |
phantom_depth = depth; |
| 920 |
catch_phantom = 2; |
| 921 |
} |
| 922 |
|
| 923 |
/* All those things related to "compensation" is intruduced due to |
| 924 |
* bcontent/econtent specials. They are originally implemented with |
| 925 |
* modification to very important part of pdfdev functions such as |
| 926 |
* pdf_dev_set_string(), and it made things very unclear. |
| 927 |
* |
| 928 |
* It is not good idea to modify the core part of PDF output routines |
| 929 |
* just for implementing DVI "special" command. They do not make sense |
| 930 |
* and just look odd when PDF output routines are separated from dvipdfmx. |
| 931 |
*/ |
| 932 |
static struct spt_coord { |
| 933 |
spt_t x, y; |
| 934 |
} compensation = { 0, 0 }; |
| 935 |
|
| 936 |
void dvi_set_compensation (double x, double y) |
| 937 |
{ |
| 938 |
compensation.x = (spt_t) round(x / dvi2pts); |
| 939 |
compensation.y = (spt_t) round(y / dvi2pts); |
| 940 |
} |
| 941 |
|
| 942 |
static void |
| 943 |
set_string (spt_t xpos, |
| 944 |
spt_t ypos, |
| 945 |
const void *instr_ptr, |
| 946 |
int instr_len, |
| 947 |
spt_t width, |
| 948 |
int font_id) |
| 949 |
{ |
| 950 |
xpos -= compensation.x; |
| 951 |
ypos -= compensation.y; |
| 952 |
pdf_dev_set_string(xpos, ypos, instr_ptr, instr_len, width, font_id); |
| 953 |
} |
| 954 |
|
| 955 |
static void |
| 956 |
set_rule (spt_t xpos, spt_t ypos, spt_t width, spt_t height) |
| 957 |
{ |
| 958 |
xpos -= compensation.x; |
| 959 |
ypos -= compensation.y; |
| 960 |
pdf_dev_set_rule(xpos, ypos, width, height); |
| 961 |
} |
| 962 |
|
| 963 |
static void |
| 964 |
calc_rect (pdf_rect *r, spt_t xpos, spt_t ypos, spt_t width, spt_t height, spt_t depth) |
| 965 |
{ |
| 966 |
xpos -= compensation.x; |
| 967 |
ypos -= compensation.y; |
| 968 |
pdf_dev_set_rect(r, xpos, ypos, width, height, depth); |
| 969 |
} |
| 970 |
|
| 971 |
void |
| 972 |
dvi_do_special (const void *buffer, int32_t size) |
| 973 |
{ |
| 974 |
double x_user, y_user, mag; |
| 975 |
const char *p; |
| 976 |
int is_drawable = 0; |
| 977 |
pdf_rect rect = {0.0, 0.0, 0.0, 0.0}; |
| 978 |
|
| 979 |
graphics_mode(); |
| 980 |
|
| 981 |
p = (const char *) buffer; |
| 982 |
|
| 983 |
x_user = dvi_state.h * dvi2pts; |
| 984 |
y_user = -dvi_state.v * dvi2pts; |
| 985 |
mag = dvi_tell_mag(); |
| 986 |
|
| 987 |
if (spc_exec_special(p, size, x_user, y_user, mag, &is_drawable, &rect) < 0) { |
| 988 |
if (dpx_conf.verbose_level > 0) { |
| 989 |
dump(p, p + size); |
| 990 |
} |
| 991 |
return; |
| 992 |
} |
| 993 |
|
| 994 |
if (dvi_is_tracking_boxes() && is_drawable) { |
| 995 |
pdf_doc_expand_box(&rect); |
| 996 |
} |
| 997 |
|
| 998 |
return; |
| 999 |
} |
| 1000 |
|
| 1001 |
double |
| 1002 |
dvi_unit_size (void) |
| 1003 |
{ |
| 1004 |
return dvi2pts; |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
int |
| 1009 |
dvi_locate_font (const char *tfm_name, spt_t ptsize) |
| 1010 |
{ |
| 1011 |
int cur_id = -1; |
| 1012 |
const char *name = tfm_name; |
| 1013 |
int subfont_id = -1, font_id; /* VF or device font ID */ |
| 1014 |
fontmap_rec *mrec; |
| 1015 |
|
| 1016 |
if (dpx_conf.verbose_level > 0) |
| 1017 |
MESG("<%s@%.2fpt", tfm_name, ptsize * dvi2pts); |
| 1018 |
|
| 1019 |
need_more_fonts(1); |
| 1020 |
|
| 1021 |
/* This routine needs to be recursive/reentrant. Load current high water |
| 1022 |
* mark into an automatic variable. |
| 1023 |
*/ |
| 1024 |
cur_id = num_loaded_fonts++; |
| 1025 |
|
| 1026 |
mrec = pdf_lookup_fontmap_record(tfm_name); |
| 1027 |
/* Load subfont mapping table */ |
| 1028 |
if (mrec && mrec->charmap.sfd_name && mrec->charmap.subfont_id) { |
| 1029 |
subfont_id = sfd_load_record(mrec->charmap.sfd_name, mrec->charmap.subfont_id); |
| 1030 |
} |
| 1031 |
|
| 1032 |
memset(&loaded_fonts[cur_id], 0, sizeof (struct loaded_font)); |
| 1033 |
|
| 1034 |
/* TFM must exist here. */ |
| 1035 |
loaded_fonts[cur_id].tfm_id = tfm_open(tfm_name, 1); |
| 1036 |
loaded_fonts[cur_id].subfont_id = subfont_id; |
| 1037 |
loaded_fonts[cur_id].size = ptsize; |
| 1038 |
/* This will be reset later if it was really generated by the dvi file. */ |
| 1039 |
loaded_fonts[cur_id].source = VF; |
| 1040 |
|
| 1041 |
/* The order of searching fonts is as follows: |
| 1042 |
* |
| 1043 |
* 1. If mrec is null, that is, there is no map entry matching |
| 1044 |
* with tfm_name, then search a virtual font matching with |
| 1045 |
* tfm_name at first. If no virtual font is found, search a |
| 1046 |
* PK font matching with tfm_name. |
| 1047 |
* |
| 1048 |
* 2. If mrec is non-null, search a physical scalable font. |
| 1049 |
* |
| 1050 |
* 3. Notice that every subfont gets non-null mrec. In this case, |
| 1051 |
* enc_name corresponding to mrec will be used instead of mrec. |
| 1052 |
* That is enc_name is NULL, search a virtual font for Omega (.ovf) |
| 1053 |
* matching with the base name of the subfont. If no virtual font |
| 1054 |
* for Omega is found, it is a fatal error because there is no PK font |
| 1055 |
* for Omega. |
| 1056 |
*/ |
| 1057 |
if (!mrec) { |
| 1058 |
font_id = vf_locate_font(tfm_name, ptsize); |
| 1059 |
if (font_id >= 0) { |
| 1060 |
loaded_fonts[cur_id].type = VIRTUAL; |
| 1061 |
loaded_fonts[cur_id].font_id = font_id; |
| 1062 |
if (dpx_conf.verbose_level > 0) |
| 1063 |
MESG("(VF)>"); |
| 1064 |
return cur_id; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
#if 1 |
| 1068 |
/* Sorry, I don't understand this well... Please fix. |
| 1069 |
* The purpose of this seems to be: |
| 1070 |
* |
| 1071 |
* Map 8-bit char codes in subfont to 16-bit code with SFD mapping |
| 1072 |
* and map subfonts to single OVF font. |
| 1073 |
* |
| 1074 |
* But it apparently only does TFM -> OVF mapping but no character |
| 1075 |
* code mapping. Please see dvi_set(), you can't have both font->type |
| 1076 |
* VIRTUAL and font->subfont_id >= 0. Am I missing something? |
| 1077 |
*/ |
| 1078 |
else if (subfont_id >= 0 && mrec->map_name) |
| 1079 |
{ |
| 1080 |
fontmap_rec *mrec1 = pdf_lookup_fontmap_record(mrec->map_name); |
| 1081 |
/* enc_name=NULL should be used only for 'built-in' encoding. |
| 1082 |
* Please fix this! |
| 1083 |
*/ |
| 1084 |
if (mrec1 && !mrec1->enc_name) { |
| 1085 |
font_id = vf_locate_font(mrec1->font_name, ptsize); |
| 1086 |
if (font_id < 0) |
| 1087 |
WARN("Could not locate Omega Virtual Font \"%s\" for \"%s\".", |
| 1088 |
mrec1->font_name, tfm_name); |
| 1089 |
else { |
| 1090 |
loaded_fonts[cur_id].type = VIRTUAL; |
| 1091 |
loaded_fonts[cur_id].font_id = font_id; |
| 1092 |
if (dpx_conf.verbose_level > 0) |
| 1093 |
MESG("(OVF)>"); |
| 1094 |
return cur_id; |
| 1095 |
} |
| 1096 |
} |
| 1097 |
} |
| 1098 |
#endif /* 1 */ |
| 1099 |
|
| 1100 |
/* Failed to load a virtual font so we try to load a physical font. */ |
| 1101 |
|
| 1102 |
/* If mrec->map_name is not NULL, font name identified in PDF output |
| 1103 |
* is different than tfm_name, this can happen for subfonts grouped |
| 1104 |
* into a single "intermediate" font foo@SFD@. |
| 1105 |
* This is necessary for optimal output; to avoid unnecessary creation |
| 1106 |
* of multiple instances of a same font, to avoid frequent font selection |
| 1107 |
* and break of string_mode. |
| 1108 |
*/ |
| 1109 |
if (mrec && mrec->map_name) { |
| 1110 |
name = mrec->map_name; |
| 1111 |
} else { |
| 1112 |
name = tfm_name; |
| 1113 |
} |
| 1114 |
|
| 1115 |
/* We need ptsize for PK font creation. */ |
| 1116 |
font_id = pdf_dev_locate_font(name, ptsize); |
| 1117 |
if (font_id < 0) { |
| 1118 |
WARN("Could not locate a virtual/physical font for TFM \"%s\".", tfm_name); |
| 1119 |
if (mrec && mrec->map_name) { /* has map_name */ |
| 1120 |
fontmap_rec *mrec1 = pdf_lookup_fontmap_record(mrec->map_name); |
| 1121 |
WARN(">> This font is mapped to an intermediate 16-bit font \"%s\" with SFD charmap=<%s,%s>,", |
| 1122 |
mrec->map_name, mrec->charmap.sfd_name, mrec->charmap.subfont_id); |
| 1123 |
if (!mrec1) |
| 1124 |
WARN(">> but I couldn't find font mapping for \"%s\".", mrec->map_name); |
| 1125 |
else { |
| 1126 |
WARN(">> and then mapped to a physical font \"%s\" by fontmap.", mrec1->font_name); |
| 1127 |
WARN(">> Please check if kpathsea library can find this font: %s", mrec1->font_name); |
| 1128 |
} |
| 1129 |
} else if (mrec && !mrec->map_name) { |
| 1130 |
char *finaldot = strrchr(mrec->font_name, '.'); |
| 1131 |
if (finaldot && strcasecmp(finaldot, ".pfa") == 0) { |
| 1132 |
/* type1 fonts with pfa format are not supported */ |
| 1133 |
WARN("This font is mapped to a physical font \"%s\".", mrec->font_name); |
| 1134 |
ERROR("Sorry, pfa format not supported; please convert the font to pfb, e.g., with t1binary."); |
| 1135 |
} else { |
| 1136 |
WARN(">> This font is mapped to a physical font \"%s\".", mrec->font_name); |
| 1137 |
WARN(">> Please check if kpathsea library can find this font: %s", mrec->font_name); |
| 1138 |
} |
| 1139 |
} else { |
| 1140 |
WARN(">> There are no valid font mapping entry for this font."); |
| 1141 |
WARN(">> Font file name \"%s\" was assumed but failed to locate that font.", tfm_name); |
| 1142 |
} |
| 1143 |
ERROR("Cannot proceed without .vf or \"physical\" font for PDF output..."); |
| 1144 |
} |
| 1145 |
memset(loaded_fonts[cur_id].padbytes, 0, 4); |
| 1146 |
if (mrec) { |
| 1147 |
if (mrec->opt.mapc >= 0) { |
| 1148 |
loaded_fonts[cur_id].padbytes[2] = (mrec->opt.mapc >> 8) & 0xff; |
| 1149 |
} |
| 1150 |
if (mrec->enc_name && !strcmp(mrec->enc_name, "unicode")) { |
| 1151 |
loaded_fonts[cur_id].is_unicode = ENC_UNICODE; |
| 1152 |
if (mrec->opt.mapc >= 0) { |
| 1153 |
loaded_fonts[cur_id].padbytes[0] = (mrec->opt.mapc >> 24) & 0xff; |
| 1154 |
loaded_fonts[cur_id].padbytes[1] = (mrec->opt.mapc >> 16) & 0xff; |
| 1155 |
} |
| 1156 |
} else if (mrec->enc_name && strstr(mrec->enc_name, "UTF16") != NULL) { |
| 1157 |
loaded_fonts[cur_id].is_unicode = ENC_UTF16; |
| 1158 |
} |
| 1159 |
} |
| 1160 |
loaded_fonts[cur_id].minbytes = pdf_dev_font_minbytes(font_id); |
| 1161 |
loaded_fonts[cur_id].type = PHYSICAL; |
| 1162 |
loaded_fonts[cur_id].font_id = font_id; |
| 1163 |
if (loaded_fonts[cur_id].minbytes > 4) { |
| 1164 |
WARN("Input encoding requries more than 4 bytes per char... (unsupported)"); |
| 1165 |
loaded_fonts[cur_id].minbytes = 4; |
| 1166 |
} |
| 1167 |
|
| 1168 |
if (dpx_conf.verbose_level > 0) |
| 1169 |
MESG(">"); |
| 1170 |
|
| 1171 |
return cur_id; |
| 1172 |
} |
| 1173 |
|
| 1174 |
#if defined(LIBDPX) |
| 1175 |
int |
| 1176 |
#else |
| 1177 |
static int |
| 1178 |
#endif /* LIBDPX */ |
| 1179 |
dvi_locate_native_font (const char *filename, uint32_t index, |
| 1180 |
spt_t ptsize, int layout_dir, int extend, int slant, int embolden) |
| 1181 |
{ |
| 1182 |
int cur_id = -1; |
| 1183 |
fontmap_rec *mrec; |
| 1184 |
char *fontmap_key; |
| 1185 |
FILE *fp; |
| 1186 |
char *path; |
| 1187 |
sfnt *sfont; |
| 1188 |
ULONG offset = 0; |
| 1189 |
int is_dfont = 0, is_type1 = 0; |
| 1190 |
|
| 1191 |
if (dpx_conf.verbose_level > 0) |
| 1192 |
MESG("<%s@%.2fpt", filename, ptsize * dvi2pts); |
| 1193 |
|
| 1194 |
if ((path = dpx_find_dfont_file(filename)) != NULL && |
| 1195 |
(fp = MFOPEN(path, "rb")) != NULL) |
| 1196 |
is_dfont = 1; |
| 1197 |
else if ((path = dpx_find_type1_file(filename)) != NULL) |
| 1198 |
is_type1 = 1; |
| 1199 |
else if (((path = dpx_find_opentype_file(filename)) == NULL |
| 1200 |
&& (path = dpx_find_truetype_file(filename)) == NULL) |
| 1201 |
|| (fp = MFOPEN(path, "rb")) == NULL) { |
| 1202 |
ERROR("Cannot proceed without the font: %s", filename); |
| 1203 |
} |
| 1204 |
need_more_fonts(1); |
| 1205 |
|
| 1206 |
cur_id = num_loaded_fonts++; |
| 1207 |
|
| 1208 |
fontmap_key = malloc(strlen(path) + 40); // CHECK this is enough |
| 1209 |
sprintf(fontmap_key, "%s/%u/%c/%d/%d/%d", path, index, layout_dir == 0 ? 'H' : 'V', extend, slant, embolden); |
| 1210 |
mrec = pdf_lookup_fontmap_record(fontmap_key); |
| 1211 |
if (mrec == NULL) { |
| 1212 |
if ((mrec = pdf_insert_native_fontmap_record(path, index, layout_dir, extend, slant, embolden)) == NULL) { |
| 1213 |
ERROR("Failed to insert font record for font: %s", filename); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
memset(&loaded_fonts[cur_id], 0, sizeof (struct loaded_font)); |
| 1218 |
|
| 1219 |
loaded_fonts[cur_id].font_id = pdf_dev_locate_font(fontmap_key, ptsize); |
| 1220 |
loaded_fonts[cur_id].size = ptsize; |
| 1221 |
loaded_fonts[cur_id].type = NATIVE; |
| 1222 |
loaded_fonts[cur_id].minbytes = pdf_dev_font_minbytes(loaded_fonts[cur_id].font_id); |
| 1223 |
if (loaded_fonts[cur_id].minbytes > 4) { |
| 1224 |
WARN("Input encoding requries more than 4 bytes per char... (unsupprted)"); |
| 1225 |
loaded_fonts[cur_id].minbytes = 4; |
| 1226 |
} |
| 1227 |
RELEASE(fontmap_key); |
| 1228 |
|
| 1229 |
if (is_type1) { |
| 1230 |
cff_font *cffont; |
| 1231 |
char *enc_vec[256]; |
| 1232 |
uint16_t num_glyphs; |
| 1233 |
int i; |
| 1234 |
|
| 1235 |
fp = DPXFOPEN(filename, DPX_RES_TYPE_T1FONT); |
| 1236 |
if (!fp) |
| 1237 |
return -1; |
| 1238 |
|
| 1239 |
if (!is_pfb(fp)) |
| 1240 |
ERROR("Failed to read Type 1 font \"%s\".", filename); |
| 1241 |
|
| 1242 |
memset(enc_vec, 0, 256 * sizeof(char *)); |
| 1243 |
cffont = t1_load_font(enc_vec, 0, fp); |
| 1244 |
if (!cffont) |
| 1245 |
ERROR("Failed to read Type 1 font \"%s\".", filename); |
| 1246 |
for (i = 0; i < 256; i++) { |
| 1247 |
if (enc_vec[i]) |
| 1248 |
RELEASE(enc_vec[i]); |
| 1249 |
} |
| 1250 |
loaded_fonts[cur_id].shift_gid = cffont->is_notdef_notzero; |
| 1251 |
loaded_fonts[cur_id].num_glyphs = num_glyphs = cffont->num_glyphs; |
| 1252 |
loaded_fonts[cur_id].gm = NEW(num_glyphs + 1, struct gm); |
| 1253 |
for (i = 0; i < num_glyphs; i++) { |
| 1254 |
double advance, ascent, descent; |
| 1255 |
cff_index *cstrings = cffont->cstrings; |
| 1256 |
t1_ginfo gm; |
| 1257 |
uint16_t gid; |
| 1258 |
|
| 1259 |
/* If .notdef is not the 1st glyph in CharStrings, glyph_id given by |
| 1260 |
* FreeType should be increased by 1 |
| 1261 |
*/ |
| 1262 |
gid = (cffont->is_notdef_notzero) ? i + 1 : i; |
| 1263 |
if (gid == num_glyphs) |
| 1264 |
break; |
| 1265 |
t1char_get_metrics(cstrings->data + cstrings->offset[gid] - 1, |
| 1266 |
cstrings->offset[gid + 1] - cstrings->offset[gid], |
| 1267 |
cffont->subrs[0], &gm); |
| 1268 |
|
| 1269 |
advance = layout_dir == 0 ? gm.wx : gm.wy; |
| 1270 |
ascent = gm.bbox.ury; |
| 1271 |
descent = gm.bbox.lly; |
| 1272 |
loaded_fonts[cur_id].gm[gid].advance = (spt_t) (ptsize * ((advance / 1000.0) * mrec->opt.extend)); |
| 1273 |
loaded_fonts[cur_id].gm[gid].ascent = (spt_t) (ptsize * ((ascent / 1000.0))); |
| 1274 |
loaded_fonts[cur_id].gm[gid].descent = (spt_t) (ptsize * ((descent / 1000.0))); |
| 1275 |
} |
| 1276 |
cff_close(cffont); |
| 1277 |
DPXFCLOSE(fp); |
| 1278 |
} else { |
| 1279 |
struct tt_head_table *head; |
| 1280 |
struct tt_maxp_table *maxp; |
| 1281 |
struct tt_hhea_table *hhea; |
| 1282 |
struct tt_longMetrics *gm; |
| 1283 |
spt_t ascent, descent; |
| 1284 |
uint16_t num_glyphs; |
| 1285 |
int i; |
| 1286 |
|
| 1287 |
if (is_dfont) |
| 1288 |
sfont = dfont_open(fp, index); |
| 1289 |
else |
| 1290 |
sfont = sfnt_open(fp); |
| 1291 |
if (sfont->type == SFNT_TYPE_TTC) |
| 1292 |
offset = ttc_read_offset(sfont, index); |
| 1293 |
else if (sfont->type == SFNT_TYPE_DFONT) |
| 1294 |
offset = sfont->offset; |
| 1295 |
|
| 1296 |
sfnt_read_table_directory(sfont, offset); |
| 1297 |
head = tt_read_head_table(sfont); |
| 1298 |
maxp = tt_read_maxp_table(sfont); |
| 1299 |
hhea = tt_read_hhea_table(sfont); |
| 1300 |
ascent = (spt_t) (ptsize * ((double) hhea->ascent / (double) head->unitsPerEm)); |
| 1301 |
descent = (spt_t) (ptsize * ((double) hhea->descent / (double) head->unitsPerEm)); |
| 1302 |
loaded_fonts[cur_id].num_glyphs = num_glyphs = maxp->numGlyphs; |
| 1303 |
if (layout_dir == 1 && sfnt_find_table_pos(sfont, "vmtx") > 0) { |
| 1304 |
struct tt_vhea_table *vhea = tt_read_vhea_table(sfont); |
| 1305 |
sfnt_locate_table(sfont, "vmtx"); |
| 1306 |
gm = tt_read_longMetrics(sfont, maxp->numGlyphs, vhea->numOfLongVerMetrics, vhea->numOfExSideBearings); |
| 1307 |
RELEASE(vhea); |
| 1308 |
} else { |
| 1309 |
sfnt_locate_table(sfont, "hmtx"); |
| 1310 |
gm = tt_read_longMetrics(sfont, maxp->numGlyphs, hhea->numOfLongHorMetrics, hhea->numOfExSideBearings); |
| 1311 |
} |
| 1312 |
if (!gm) |
| 1313 |
ERROR("Failed to read TrueType/OpenType glyph metrics table."); |
| 1314 |
loaded_fonts[cur_id].gm = NEW(num_glyphs, struct gm); |
| 1315 |
for (i = 0; i < num_glyphs; i++) { |
| 1316 |
loaded_fonts[cur_id].gm[i].advance = (spt_t) (ptsize * ((double) gm[i].advance / (double) head->unitsPerEm) * mrec->opt.extend); |
| 1317 |
loaded_fonts[cur_id].gm[i].ascent = ascent; |
| 1318 |
loaded_fonts[cur_id].gm[i].descent = descent; |
| 1319 |
|
| 1320 |
} |
| 1321 |
RELEASE(gm); |
| 1322 |
RELEASE(hhea); |
| 1323 |
RELEASE(maxp); |
| 1324 |
RELEASE(head); |
| 1325 |
sfnt_close(sfont); |
| 1326 |
DPXFCLOSE(fp); |
| 1327 |
} |
| 1328 |
|
| 1329 |
RELEASE(path); |
| 1330 |
|
| 1331 |
loaded_fonts[cur_id].layout_dir = layout_dir; |
| 1332 |
loaded_fonts[cur_id].extend = mrec->opt.extend; |
| 1333 |
loaded_fonts[cur_id].slant = mrec->opt.slant; |
| 1334 |
loaded_fonts[cur_id].embolden = mrec->opt.bold; |
| 1335 |
|
| 1336 |
if (dpx_conf.verbose_level > 0) |
| 1337 |
MESG(">"); |
| 1338 |
|
| 1339 |
return cur_id; |
| 1340 |
} |
| 1341 |
|
| 1342 |
double |
| 1343 |
dvi_dev_xpos (void) |
| 1344 |
{ |
| 1345 |
return dvi_state.h * dvi2pts; |
| 1346 |
} |
| 1347 |
|
| 1348 |
double |
| 1349 |
dvi_dev_ypos (void) |
| 1350 |
{ |
| 1351 |
return -(dvi_state.v * dvi2pts); |
| 1352 |
} |
| 1353 |
|
| 1354 |
static void do_moveto (int32_t x, int32_t y) |
| 1355 |
{ |
| 1356 |
dvi_state.h = x; |
| 1357 |
dvi_state.v = y; |
| 1358 |
} |
| 1359 |
|
| 1360 |
void dvi_right (int32_t x) |
| 1361 |
{ |
| 1362 |
spt_t save_h, save_v; |
| 1363 |
|
| 1364 |
if (lr_mode >= SKIMMING) { |
| 1365 |
lr_width += x; |
| 1366 |
return; |
| 1367 |
} |
| 1368 |
|
| 1369 |
if (lr_mode == RTYPESETTING) |
| 1370 |
x = -x; |
| 1371 |
|
| 1372 |
save_h = dvi_state.h; |
| 1373 |
save_v = dvi_state.v; |
| 1374 |
|
| 1375 |
switch (dvi_state.d) { |
| 1376 |
case 0: |
| 1377 |
dvi_state.h += x; break; |
| 1378 |
case 1: |
| 1379 |
dvi_state.v += x; break; |
| 1380 |
case 3: |
| 1381 |
dvi_state.v -= x; break; |
| 1382 |
} |
| 1383 |
|
| 1384 |
if (dvi_is_tracking_boxes() && catch_phantom > 0) { |
| 1385 |
pdf_rect rect; |
| 1386 |
spt_t width, height, depth; |
| 1387 |
if (catch_phantom == 1) { |
| 1388 |
if (current_font >= 0 && current_font < num_loaded_fonts) { |
| 1389 |
height = loaded_fonts[current_font].size; |
| 1390 |
} else { |
| 1391 |
if (dpx_conf.verbose_level > 0) { |
| 1392 |
WARN("Don't know how to calculate the box height since current font is not set..."); |
| 1393 |
} |
| 1394 |
height = 0.0; |
| 1395 |
} |
| 1396 |
depth = 0.0; |
| 1397 |
} else { |
| 1398 |
height = phantom_height / dvi2pts; |
| 1399 |
depth = phantom_depth / dvi2pts; |
| 1400 |
} |
| 1401 |
switch (dvi_state.d) { |
| 1402 |
case 0: |
| 1403 |
width = dvi_state.h - save_h; |
| 1404 |
break; |
| 1405 |
case 1: |
| 1406 |
case 2: |
| 1407 |
width = dvi_state.v - save_v; |
| 1408 |
break; |
| 1409 |
default: |
| 1410 |
width = dvi_state.h - save_h; |
| 1411 |
} |
| 1412 |
calc_rect(&rect, save_h, -save_v, width, height, depth); |
| 1413 |
pdf_doc_expand_box(&rect); |
| 1414 |
} |
| 1415 |
} |
| 1416 |
|
| 1417 |
void dvi_down (int32_t y) |
| 1418 |
{ |
| 1419 |
if (lr_mode < SKIMMING) { |
| 1420 |
switch (dvi_state.d) { |
| 1421 |
case 0: |
| 1422 |
dvi_state.v += y; break; |
| 1423 |
case 1: |
| 1424 |
dvi_state.h -= y; break; |
| 1425 |
case 3: |
| 1426 |
dvi_state.h += y; break; |
| 1427 |
} |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
/* _FIXME_ |
| 1432 |
* CMap decoder wants multibyte strings as input but |
| 1433 |
* how DVI char codes are converted to multibyte sting |
| 1434 |
* is not clear. |
| 1435 |
*/ |
| 1436 |
void |
| 1437 |
dvi_set (int32_t ch) |
| 1438 |
{ |
| 1439 |
struct loaded_font *font; |
| 1440 |
spt_t width, height, depth; |
| 1441 |
unsigned char wbuf[4]; |
| 1442 |
int n, cbytes; |
| 1443 |
|
| 1444 |
if (current_font < 0) { |
| 1445 |
ERROR("No font selected!"); |
| 1446 |
} |
| 1447 |
/* The division by dvi2pts seems strange since we actually know the |
| 1448 |
* "dvi" size of the fonts contained in the DVI file. In other |
| 1449 |
* words, we converted from DVI units to pts and back again! |
| 1450 |
* The problem comes from fonts defined in VF files where we don't know |
| 1451 |
* the DVI size. It's keeping me sane to keep *point sizes* of *all* |
| 1452 |
* fonts in the dev.c file and convert them back if necessary. |
| 1453 |
*/ |
| 1454 |
font = &loaded_fonts[current_font]; |
| 1455 |
|
| 1456 |
if (font->type == NATIVE) { |
| 1457 |
if (ch >= 0 && ch < font->num_glyphs) { |
| 1458 |
width = font->gm[ch].advance; |
| 1459 |
} else { |
| 1460 |
WARN("Invalid char for dvilua font (ignored): %04x", ch); |
| 1461 |
return; |
| 1462 |
} |
| 1463 |
} else { |
| 1464 |
width = tfm_get_fw_width(font->tfm_id, ch); |
| 1465 |
width = sqxfw(font->size, width); |
| 1466 |
} |
| 1467 |
|
| 1468 |
if (lr_mode >= SKIMMING) { |
| 1469 |
lr_width += width; |
| 1470 |
return; |
| 1471 |
} |
| 1472 |
|
| 1473 |
if (lr_mode == RTYPESETTING) |
| 1474 |
dvi_right(width); /* Will actually move left */ |
| 1475 |
|
| 1476 |
memcpy(wbuf, font->padbytes, 4); |
| 1477 |
switch (font->type) { |
| 1478 |
case PHYSICAL: |
| 1479 |
if (ch > 65535) { |
| 1480 |
if (font->is_unicode == ENC_UTF16 && tfm_is_jfm(font->tfm_id)) { |
| 1481 |
wbuf[0] = (UTF32toUTF16HS(ch) >> 8) & 0xff; |
| 1482 |
wbuf[1] = UTF32toUTF16HS(ch) & 0xff; |
| 1483 |
wbuf[2] = (UTF32toUTF16LS(ch) >> 8) & 0xff; |
| 1484 |
wbuf[3] = UTF32toUTF16LS(ch) & 0xff; |
| 1485 |
} else { |
| 1486 |
wbuf[0] = (ch >> 24) & 0xff; |
| 1487 |
wbuf[1] = (ch >> 16) & 0xff; |
| 1488 |
wbuf[2] = (ch >> 8) & 0xff; |
| 1489 |
wbuf[3] = ch & 0xff; |
| 1490 |
} |
| 1491 |
n = 4; |
| 1492 |
} else if (ch > 255) { |
| 1493 |
wbuf[2] = (ch >> 8) & 0xff; |
| 1494 |
wbuf[3] = ch & 0xff; |
| 1495 |
n = 2; |
| 1496 |
} else if (font->subfont_id >= 0) { |
| 1497 |
uint16_t uch = lookup_sfd_record(font->subfont_id, (unsigned char) ch); |
| 1498 |
wbuf[2] = (uch >> 8) & 0xff; |
| 1499 |
wbuf[3] = uch & 0xff; |
| 1500 |
n = 2; |
| 1501 |
} else { |
| 1502 |
wbuf[3] = (unsigned char) ch; |
| 1503 |
n = 1; |
| 1504 |
} |
| 1505 |
cbytes = font->minbytes > n ? font->minbytes : n; |
| 1506 |
set_string(dvi_state.h, -dvi_state.v, wbuf + 4 - cbytes, cbytes, width, font->font_id); |
| 1507 |
if (dvi_is_tracking_boxes()) { |
| 1508 |
pdf_rect rect; |
| 1509 |
|
| 1510 |
height = tfm_get_fw_height(font->tfm_id, ch); |
| 1511 |
depth = tfm_get_fw_depth (font->tfm_id, ch); |
| 1512 |
height = sqxfw(font->size, height); |
| 1513 |
depth = sqxfw(font->size, depth); |
| 1514 |
|
| 1515 |
calc_rect(&rect, dvi_state.h, -dvi_state.v, width, height, depth); |
| 1516 |
pdf_doc_expand_box(&rect); |
| 1517 |
} |
| 1518 |
break; |
| 1519 |
case NATIVE: |
| 1520 |
wbuf[0] = (ch >> 8) & 0xff; |
| 1521 |
wbuf[1] = ch & 0xff; |
| 1522 |
set_string(dvi_state.h, -dvi_state.v, wbuf, 2, width, font->font_id); |
| 1523 |
if (dvi_is_tracking_boxes()) { |
| 1524 |
pdf_rect rect; |
| 1525 |
|
| 1526 |
height = font->gm[ch].ascent; |
| 1527 |
depth = font->gm[ch].descent; |
| 1528 |
calc_rect(&rect, dvi_state.h, -dvi_state.v, width, height, depth); |
| 1529 |
pdf_doc_expand_box(&rect); |
| 1530 |
} |
| 1531 |
break; |
| 1532 |
case VIRTUAL: |
| 1533 |
#if 0 |
| 1534 |
/* See comment in locate_font() */ |
| 1535 |
if (font->subfont_id >= 0) |
| 1536 |
ch = lookup_sfd_record(font->subfont_id, (unsigned char) ch); |
| 1537 |
#endif /* 0 */ |
| 1538 |
vf_set_char(ch, font->font_id); /* push/pop invoked */ |
| 1539 |
break; |
| 1540 |
} |
| 1541 |
|
| 1542 |
if (lr_mode == LTYPESETTING) |
| 1543 |
dvi_right(width); |
| 1544 |
|
| 1545 |
} |
| 1546 |
|
| 1547 |
void |
| 1548 |
dvi_put (int32_t ch) |
| 1549 |
{ |
| 1550 |
struct loaded_font *font; |
| 1551 |
spt_t width, height, depth; |
| 1552 |
unsigned char wbuf[4]; |
| 1553 |
int n, cbytes; |
| 1554 |
|
| 1555 |
if (current_font < 0) { |
| 1556 |
ERROR("No font selected!"); |
| 1557 |
} |
| 1558 |
|
| 1559 |
font = &loaded_fonts[current_font]; |
| 1560 |
|
| 1561 |
memcpy(wbuf, font->padbytes, 4); |
| 1562 |
switch (font->type) { |
| 1563 |
case PHYSICAL: |
| 1564 |
width = tfm_get_fw_width(font->tfm_id, ch); |
| 1565 |
width = sqxfw(font->size, width); |
| 1566 |
/* Treat a single character as a one byte string and use the |
| 1567 |
* string routine. |
| 1568 |
*/ |
| 1569 |
if (ch > 65535) { |
| 1570 |
if (font->is_unicode == ENC_UTF16 && tfm_is_jfm(font->tfm_id)) { |
| 1571 |
wbuf[0] = (UTF32toUTF16HS(ch) >> 8) & 0xff; |
| 1572 |
wbuf[1] = UTF32toUTF16HS(ch) & 0xff; |
| 1573 |
wbuf[2] = (UTF32toUTF16LS(ch) >> 8) & 0xff; |
| 1574 |
wbuf[3] = UTF32toUTF16LS(ch) & 0xff; |
| 1575 |
} else { |
| 1576 |
wbuf[0] = (ch >> 24) & 0xff; |
| 1577 |
wbuf[1] = (ch >> 16) & 0xff; |
| 1578 |
wbuf[2] = (ch >> 8) & 0xff; |
| 1579 |
wbuf[3] = ch & 0xff; |
| 1580 |
} |
| 1581 |
n = 4; |
| 1582 |
} else if (ch > 255) { |
| 1583 |
wbuf[2] = (ch >> 8) & 0xff; |
| 1584 |
wbuf[3] = ch & 0xff; |
| 1585 |
n = 2; |
| 1586 |
} else if (font->subfont_id >= 0) { |
| 1587 |
uint16_t uch = lookup_sfd_record(font->subfont_id, (unsigned char) ch); |
| 1588 |
wbuf[2] = (uch >> 8) & 0xff; |
| 1589 |
wbuf[3] = uch & 0xff; |
| 1590 |
n = 2; |
| 1591 |
} else { |
| 1592 |
wbuf[3] = (unsigned char) ch; |
| 1593 |
n = 1; |
| 1594 |
} |
| 1595 |
cbytes = font->minbytes > n ? font->minbytes : n; |
| 1596 |
set_string(dvi_state.h, -dvi_state.v, wbuf + 4 - cbytes, cbytes, width, font->font_id); |
| 1597 |
if (dvi_is_tracking_boxes()) { |
| 1598 |
pdf_rect rect; |
| 1599 |
|
| 1600 |
height = tfm_get_fw_height(font->tfm_id, ch); |
| 1601 |
depth = tfm_get_fw_depth (font->tfm_id, ch); |
| 1602 |
height = sqxfw(font->size, height); |
| 1603 |
depth = sqxfw(font->size, depth); |
| 1604 |
|
| 1605 |
calc_rect(&rect, dvi_state.h, -dvi_state.v, width, height, depth); |
| 1606 |
pdf_doc_expand_box(&rect); |
| 1607 |
} |
| 1608 |
break; |
| 1609 |
case VIRTUAL: |
| 1610 |
#if 0 |
| 1611 |
/* See comment in locate_font() */ |
| 1612 |
if (font->subfont_id >= 0) |
| 1613 |
ch = lookup_sfd_record(font->subfont_id, (unsigned char) ch); |
| 1614 |
#endif /* 0 */ |
| 1615 |
vf_set_char(ch, font->font_id); |
| 1616 |
break; |
| 1617 |
} |
| 1618 |
|
| 1619 |
return; |
| 1620 |
} |
| 1621 |
|
| 1622 |
|
| 1623 |
void |
| 1624 |
dvi_rule (int32_t width, int32_t height) |
| 1625 |
{ |
| 1626 |
if (width > 0 && height > 0) { |
| 1627 |
do_moveto(dvi_state.h, dvi_state.v); |
| 1628 |
|
| 1629 |
switch (dvi_state.d) { |
| 1630 |
case 0: |
| 1631 |
set_rule(dvi_state.h, -dvi_state.v, width, height); |
| 1632 |
break; |
| 1633 |
case 1: |
| 1634 |
set_rule(dvi_state.h, -dvi_state.v - width, height, width); |
| 1635 |
break; |
| 1636 |
case 3: |
| 1637 |
set_rule(dvi_state.h - height, -dvi_state.v , height, width); |
| 1638 |
break; |
| 1639 |
} |
| 1640 |
} |
| 1641 |
} |
| 1642 |
|
| 1643 |
void |
| 1644 |
dvi_dirchg (unsigned char dir) |
| 1645 |
{ |
| 1646 |
if (dpx_conf.verbose_level > 0) |
| 1647 |
fprintf(stderr, " > dvi_dirchg %d\n", dir); |
| 1648 |
dvi_state.d = dir; |
| 1649 |
pdf_dev_set_dirmode(dvi_state.d); /* 0: horizontal, 1,3: vertical */ |
| 1650 |
} |
| 1651 |
|
| 1652 |
static void |
| 1653 |
do_setrule (void) |
| 1654 |
{ |
| 1655 |
int32_t width, height; |
| 1656 |
|
| 1657 |
height = get_buffered_signed_quad(); |
| 1658 |
width = get_buffered_signed_quad(); |
| 1659 |
switch (lr_mode) { |
| 1660 |
case LTYPESETTING: |
| 1661 |
dvi_rule(width, height); |
| 1662 |
dvi_right(width); |
| 1663 |
break; |
| 1664 |
case RTYPESETTING: |
| 1665 |
dvi_right(width); |
| 1666 |
dvi_rule(width, height); |
| 1667 |
break; |
| 1668 |
default: |
| 1669 |
lr_width += width; |
| 1670 |
break; |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
static void |
| 1675 |
do_putrule (void) |
| 1676 |
{ |
| 1677 |
int32_t width, height; |
| 1678 |
|
| 1679 |
height = get_buffered_signed_quad (); |
| 1680 |
width = get_buffered_signed_quad (); |
| 1681 |
switch (lr_mode) { |
| 1682 |
case LTYPESETTING: |
| 1683 |
dvi_rule(width, height); |
| 1684 |
break; |
| 1685 |
case RTYPESETTING: |
| 1686 |
dvi_right(width); |
| 1687 |
dvi_rule(width, height); |
| 1688 |
dvi_right(-width); |
| 1689 |
break; |
| 1690 |
default: |
| 1691 |
break; |
| 1692 |
} |
| 1693 |
} |
| 1694 |
|
| 1695 |
void |
| 1696 |
dvi_push (void) |
| 1697 |
{ |
| 1698 |
if (dvi_stack_depth >= DVI_STACK_DEPTH_MAX) |
| 1699 |
ERROR("DVI stack exceeded limit."); |
| 1700 |
|
| 1701 |
dvi_stack[dvi_stack_depth++] = dvi_state; |
| 1702 |
} |
| 1703 |
|
| 1704 |
void |
| 1705 |
dvi_pop (void) |
| 1706 |
{ |
| 1707 |
if (dvi_stack_depth <= 0) |
| 1708 |
ERROR ("Tried to pop an empty stack."); |
| 1709 |
|
| 1710 |
dvi_state = dvi_stack[--dvi_stack_depth]; |
| 1711 |
do_moveto(dvi_state.h, dvi_state.v); |
| 1712 |
pdf_dev_set_dirmode(dvi_state.d); /* 0: horizontal, 1,3: vertical */ |
| 1713 |
} |
| 1714 |
|
| 1715 |
|
| 1716 |
void |
| 1717 |
dvi_w (int32_t ch) |
| 1718 |
{ |
| 1719 |
dvi_state.w = ch; |
| 1720 |
dvi_right(ch); |
| 1721 |
} |
| 1722 |
|
| 1723 |
void |
| 1724 |
dvi_w0 (void) |
| 1725 |
{ |
| 1726 |
dvi_right(dvi_state.w); |
| 1727 |
} |
| 1728 |
|
| 1729 |
void |
| 1730 |
dvi_x (int32_t ch) |
| 1731 |
{ |
| 1732 |
dvi_state.x = ch; |
| 1733 |
dvi_right(ch); |
| 1734 |
} |
| 1735 |
|
| 1736 |
void |
| 1737 |
dvi_x0 (void) |
| 1738 |
{ |
| 1739 |
dvi_right(dvi_state.x); |
| 1740 |
} |
| 1741 |
|
| 1742 |
void |
| 1743 |
dvi_y (int32_t ch) |
| 1744 |
{ |
| 1745 |
dvi_state.y = ch; |
| 1746 |
dvi_down(ch); |
| 1747 |
} |
| 1748 |
|
| 1749 |
void |
| 1750 |
dvi_y0 (void) |
| 1751 |
{ |
| 1752 |
dvi_down(dvi_state.y); |
| 1753 |
} |
| 1754 |
|
| 1755 |
void |
| 1756 |
dvi_z (int32_t ch) |
| 1757 |
{ |
| 1758 |
dvi_state.z = ch; |
| 1759 |
dvi_down(ch); |
| 1760 |
} |
| 1761 |
|
| 1762 |
void |
| 1763 |
dvi_z0 (void) |
| 1764 |
{ |
| 1765 |
dvi_down(dvi_state.z); |
| 1766 |
} |
| 1767 |
|
| 1768 |
static void |
| 1769 |
skip_fntdef (void) |
| 1770 |
{ |
| 1771 |
int area_len, name_len; |
| 1772 |
|
| 1773 |
skip_bytes(12, dvi_file); |
| 1774 |
area_len = get_unsigned_byte(dvi_file); |
| 1775 |
name_len = get_unsigned_byte(dvi_file); |
| 1776 |
skip_bytes(area_len + name_len, dvi_file); |
| 1777 |
} |
| 1778 |
|
| 1779 |
/* when pre-scanning the page, we process fntdef |
| 1780 |
and remove the fntdef opcode from the buffer */ |
| 1781 |
static void |
| 1782 |
do_fntdef (int32_t tex_id) |
| 1783 |
{ |
| 1784 |
if (linear) |
| 1785 |
read_font_record(tex_id); |
| 1786 |
else |
| 1787 |
skip_fntdef(); |
| 1788 |
--dvi_page_buf_index; |
| 1789 |
} |
| 1790 |
|
| 1791 |
void |
| 1792 |
dvi_set_font (int font_id) |
| 1793 |
{ |
| 1794 |
current_font = font_id; |
| 1795 |
} |
| 1796 |
|
| 1797 |
static void |
| 1798 |
do_fnt (int32_t tex_id) |
| 1799 |
{ |
| 1800 |
int i; |
| 1801 |
|
| 1802 |
for (i = 0; i < num_def_fonts; i++) { |
| 1803 |
if (def_fonts[i].tex_id == tex_id) |
| 1804 |
break; |
| 1805 |
} |
| 1806 |
|
| 1807 |
if (i == num_def_fonts) { |
| 1808 |
ERROR("Tried to select a font that hasn't been defined: id=%d", tex_id); |
| 1809 |
} |
| 1810 |
|
| 1811 |
if (!def_fonts[i].used) { |
| 1812 |
int font_id; |
| 1813 |
|
| 1814 |
if (def_fonts[i].native) { |
| 1815 |
font_id = dvi_locate_native_font(def_fonts[i].font_name, |
| 1816 |
def_fonts[i].face_index, |
| 1817 |
def_fonts[i].point_size, |
| 1818 |
def_fonts[i].layout_dir, |
| 1819 |
def_fonts[i].extend, |
| 1820 |
def_fonts[i].slant, |
| 1821 |
def_fonts[i].embolden); |
| 1822 |
} else { |
| 1823 |
font_id = dvi_locate_font(def_fonts[i].font_name, |
| 1824 |
def_fonts[i].point_size); |
| 1825 |
} |
| 1826 |
loaded_fonts[font_id].rgba_color = def_fonts[i].rgba_color; |
| 1827 |
loaded_fonts[font_id].rgba_used = def_fonts[i].rgba_used; |
| 1828 |
/* if rgba_used == 0, not a colored font */ |
| 1829 |
if (loaded_fonts[font_id].rgba_used == 0) { |
| 1830 |
loaded_fonts[font_id].xgs_id = -1; |
| 1831 |
} else { |
| 1832 |
pdf_obj *xgs_dict; |
| 1833 |
int a = loaded_fonts[font_id].rgba_color & 0xff; |
| 1834 |
|
| 1835 |
/* Inefficient but don't care as transparency is not expected to be frequently used. */ |
| 1836 |
xgs_dict = pdf_new_dict(); |
| 1837 |
pdf_add_dict(xgs_dict, pdf_new_name("Type"), pdf_new_name("ExtGState")); |
| 1838 |
pdf_add_dict(xgs_dict, pdf_new_name("ca"), pdf_new_number(a/255.0)); |
| 1839 |
pdf_add_dict(xgs_dict, pdf_new_name("CA"), pdf_new_number(a/255.0)); |
| 1840 |
loaded_fonts[font_id].xgs_id = pdf_defineresource("ExtGState", NULL, xgs_dict, 0); |
| 1841 |
} |
| 1842 |
loaded_fonts[font_id].source = DVI; |
| 1843 |
def_fonts[i].used = 1; |
| 1844 |
def_fonts[i].font_id = font_id; |
| 1845 |
} |
| 1846 |
current_font = def_fonts[i].font_id; |
| 1847 |
} |
| 1848 |
|
| 1849 |
static void |
| 1850 |
do_xxx (int32_t size) |
| 1851 |
{ |
| 1852 |
if (lr_mode < SKIMMING) |
| 1853 |
dvi_do_special(dvi_page_buffer + dvi_page_buf_index, size); |
| 1854 |
dvi_page_buf_index += size; |
| 1855 |
} |
| 1856 |
|
| 1857 |
static void |
| 1858 |
do_bop (void) |
| 1859 |
{ |
| 1860 |
int i; |
| 1861 |
|
| 1862 |
if (processing_page) |
| 1863 |
ERROR("Got a bop in the middle of a page!"); |
| 1864 |
|
| 1865 |
/* For now, ignore TeX's count registers */ |
| 1866 |
for (i = 0; i < 10; i++) { |
| 1867 |
skip_bufferd_bytes(4); |
| 1868 |
} |
| 1869 |
/* Ignore previous page pointer since we have already |
| 1870 |
* saved this information |
| 1871 |
*/ |
| 1872 |
skip_bufferd_bytes(4); |
| 1873 |
clear_state(); |
| 1874 |
processing_page = 1; |
| 1875 |
|
| 1876 |
pdf_doc_begin_page(dvi_tell_mag(), dev_origin_x, dev_origin_y); |
| 1877 |
spc_exec_at_begin_page(); |
| 1878 |
|
| 1879 |
return; |
| 1880 |
} |
| 1881 |
|
| 1882 |
static void |
| 1883 |
do_eop (void) |
| 1884 |
{ |
| 1885 |
processing_page = 0; |
| 1886 |
|
| 1887 |
if (dvi_stack_depth != 0) { |
| 1888 |
ERROR("DVI stack depth is not zero at end of page"); |
| 1889 |
} |
| 1890 |
spc_exec_at_end_page(); |
| 1891 |
|
| 1892 |
pdf_doc_end_page(); |
| 1893 |
|
| 1894 |
return; |
| 1895 |
} |
| 1896 |
|
| 1897 |
static void |
| 1898 |
do_dir (void) |
| 1899 |
{ |
| 1900 |
dvi_state.d = get_buffered_unsigned_byte(); |
| 1901 |
pdf_dev_set_dirmode(dvi_state.d); /* 0: horizontal, 1,3: vertical */ |
| 1902 |
} |
| 1903 |
|
| 1904 |
static void |
| 1905 |
lr_width_push (void) |
| 1906 |
{ |
| 1907 |
if (lr_width_stack_depth >= DVI_STACK_DEPTH_MAX) |
| 1908 |
ERROR("Segment width stack exceeded limit."); |
| 1909 |
|
| 1910 |
lr_width_stack[lr_width_stack_depth++] = lr_width; |
| 1911 |
} |
| 1912 |
|
| 1913 |
static void |
| 1914 |
lr_width_pop (void) |
| 1915 |
{ |
| 1916 |
if (lr_width_stack_depth <= 0) |
| 1917 |
ERROR("Tried to pop an empty segment width stack."); |
| 1918 |
|
| 1919 |
lr_width = lr_width_stack[--lr_width_stack_depth]; |
| 1920 |
} |
| 1921 |
|
| 1922 |
static void |
| 1923 |
dvi_begin_reflect (void) |
| 1924 |
{ |
| 1925 |
if (lr_mode >= SKIMMING) { |
| 1926 |
++lr_mode; |
| 1927 |
} else { |
| 1928 |
lr_state.buf_index = dvi_page_buf_index; |
| 1929 |
lr_state.font = current_font; |
| 1930 |
lr_state.state = lr_mode; |
| 1931 |
lr_mode = SKIMMING; |
| 1932 |
lr_width = 0; |
| 1933 |
} |
| 1934 |
} |
| 1935 |
|
| 1936 |
static void |
| 1937 |
dvi_end_reflect (void) |
| 1938 |
{ |
| 1939 |
switch (lr_mode) { |
| 1940 |
case SKIMMING: |
| 1941 |
current_font = lr_state.font; |
| 1942 |
dvi_page_buf_index = lr_state.buf_index; |
| 1943 |
lr_mode = REVERSE(lr_state.state); /* must precede dvi_right */ |
| 1944 |
dvi_right(-(int32_t)lr_width); |
| 1945 |
lr_width_push(); |
| 1946 |
break; |
| 1947 |
case LTYPESETTING: |
| 1948 |
case RTYPESETTING: |
| 1949 |
lr_width_pop(); |
| 1950 |
dvi_right(-(int32_t)lr_width); |
| 1951 |
lr_mode = REVERSE(lr_mode); |
| 1952 |
break; |
| 1953 |
default: /* lr_mode > SKIMMING */ |
| 1954 |
lr_mode--; |
| 1955 |
} |
| 1956 |
} |
| 1957 |
|
| 1958 |
static void |
| 1959 |
skip_native_font_def (void) |
| 1960 |
{ |
| 1961 |
unsigned int flags; |
| 1962 |
int name_length; |
| 1963 |
|
| 1964 |
skip_bytes(4, dvi_file); /* skip point size */ |
| 1965 |
flags = get_unsigned_pair(dvi_file); |
| 1966 |
name_length = get_unsigned_byte(dvi_file); |
| 1967 |
skip_bytes(name_length + 4, dvi_file); |
| 1968 |
|
| 1969 |
if (flags & XDV_FLAG_COLORED) |
| 1970 |
skip_bytes(4, dvi_file); |
| 1971 |
|
| 1972 |
if (flags & XDV_FLAG_EXTEND) |
| 1973 |
skip_bytes(4, dvi_file); |
| 1974 |
|
| 1975 |
if (flags & XDV_FLAG_SLANT) |
| 1976 |
skip_bytes(4, dvi_file); |
| 1977 |
|
| 1978 |
if (flags & XDV_FLAG_EMBOLDEN) |
| 1979 |
skip_bytes(4, dvi_file); |
| 1980 |
} |
| 1981 |
|
| 1982 |
static void |
| 1983 |
do_native_font_def (int32_t tex_id) |
| 1984 |
{ |
| 1985 |
if (linear) |
| 1986 |
read_native_font_record(tex_id); |
| 1987 |
else |
| 1988 |
skip_native_font_def(); |
| 1989 |
--dvi_page_buf_index; /* don't buffer the opcode */ |
| 1990 |
} |
| 1991 |
|
| 1992 |
static void |
| 1993 |
skip_glyphs (void) |
| 1994 |
{ |
| 1995 |
unsigned int i, slen = 0; |
| 1996 |
slen = (unsigned int) get_buffered_unsigned_pair(); |
| 1997 |
for (i = 0; i < slen; i++) { |
| 1998 |
skip_bufferd_bytes(4); |
| 1999 |
skip_bufferd_bytes(4); |
| 2000 |
skip_bufferd_bytes(2); |
| 2001 |
} |
| 2002 |
} |
| 2003 |
|
| 2004 |
static void |
| 2005 |
do_glyphs (int do_actual_text) |
| 2006 |
{ |
| 2007 |
struct loaded_font *font; |
| 2008 |
spt_t width, height, depth, *xloc, *yloc; |
| 2009 |
unsigned char wbuf[2]; |
| 2010 |
int32_t i; |
| 2011 |
uint16_t glyph_id, slen = 0; |
| 2012 |
|
| 2013 |
if (current_font < 0) |
| 2014 |
ERROR("No font selected!"); |
| 2015 |
|
| 2016 |
font = &loaded_fonts[current_font]; |
| 2017 |
|
| 2018 |
if (do_actual_text) { |
| 2019 |
slen = (unsigned int) get_buffered_unsigned_pair(); |
| 2020 |
if (lr_mode >= SKIMMING) { |
| 2021 |
for (i = 0; i < slen; i++) { |
| 2022 |
skip_bufferd_bytes(2); |
| 2023 |
} |
| 2024 |
} else { |
| 2025 |
uint16_t *unicodes = NEW(slen, uint16_t); |
| 2026 |
for (i = 0; i < slen; i++) { |
| 2027 |
unicodes[i] = (uint16_t) get_buffered_unsigned_pair(); |
| 2028 |
} |
| 2029 |
pdf_dev_begin_actualtext (unicodes, slen); |
| 2030 |
RELEASE(unicodes); |
| 2031 |
} |
| 2032 |
} |
| 2033 |
|
| 2034 |
width = get_buffered_signed_quad(); |
| 2035 |
|
| 2036 |
if (lr_mode >= SKIMMING) { |
| 2037 |
lr_width += width; |
| 2038 |
skip_glyphs(); |
| 2039 |
return; |
| 2040 |
} |
| 2041 |
|
| 2042 |
if (lr_mode == RTYPESETTING) |
| 2043 |
dvi_right(width); /* Will actually move left */ |
| 2044 |
|
| 2045 |
slen = (unsigned int) get_buffered_unsigned_pair(); |
| 2046 |
xloc = NEW(slen, spt_t); |
| 2047 |
yloc = NEW(slen, spt_t); |
| 2048 |
for (i = 0; i < slen; i++) { |
| 2049 |
xloc[i] = get_buffered_signed_quad(); |
| 2050 |
yloc[i] = get_buffered_signed_quad(); |
| 2051 |
} |
| 2052 |
|
| 2053 |
if (font->rgba_used == 1) { |
| 2054 |
pdf_color color; |
| 2055 |
pdf_color_rgbcolor(&color, |
| 2056 |
(double)((unsigned char)(font->rgba_color >> 24) & 0xff) / 255, |
| 2057 |
(double)((unsigned char)(font->rgba_color >> 16) & 0xff) / 255, |
| 2058 |
(double)((unsigned char)(font->rgba_color >> 8) & 0xff) / 255); |
| 2059 |
pdf_color_push(&color, &color); |
| 2060 |
/* Opacity: |
| 2061 |
* Enter graphics_mode and then enclose with save/resotre |
| 2062 |
* since pdf_color_pop() may not restore graphics state. |
| 2063 |
*/ |
| 2064 |
if (font->xgs_id >= 0) { |
| 2065 |
pdf_obj *ref; |
| 2066 |
char resname[16]; |
| 2067 |
char content[22]; |
| 2068 |
|
| 2069 |
sprintf(resname, "Xtx_Gs_%08x", current_font); |
| 2070 |
ref = pdf_get_resource_reference(font->xgs_id); |
| 2071 |
pdf_doc_add_page_resource("ExtGState", resname, ref); |
| 2072 |
graphics_mode(); |
| 2073 |
pdf_dev_gsave(); |
| 2074 |
sprintf(content, " /%s gs ", resname); |
| 2075 |
pdf_doc_add_page_content(content, strlen(content)); |
| 2076 |
} |
| 2077 |
} |
| 2078 |
|
| 2079 |
for (i = 0; i < slen; i++) { |
| 2080 |
spt_t advance = 0; |
| 2081 |
|
| 2082 |
glyph_id = get_buffered_unsigned_pair(); /* freetype glyph index */ |
| 2083 |
if (glyph_id < font->num_glyphs) { |
| 2084 |
if (font->shift_gid) |
| 2085 |
glyph_id++; |
| 2086 |
advance = font->gm[glyph_id].advance; |
| 2087 |
if (dvi_is_tracking_boxes()) { |
| 2088 |
pdf_rect rect; |
| 2089 |
height = font->gm[glyph_id].ascent ; |
| 2090 |
depth = -font->gm[glyph_id].descent; |
| 2091 |
calc_rect(&rect, dvi_state.h + xloc[i], -dvi_state.v - yloc[i], advance, height, depth); |
| 2092 |
pdf_doc_expand_box(&rect); |
| 2093 |
} |
| 2094 |
} |
| 2095 |
wbuf[0] = glyph_id >> 8; |
| 2096 |
wbuf[1] = glyph_id & 0xff; |
| 2097 |
set_string(dvi_state.h + xloc[i], -dvi_state.v - yloc[i], wbuf, 2, advance, font->font_id); |
| 2098 |
} |
| 2099 |
|
| 2100 |
if (font->rgba_used == 1) { |
| 2101 |
if (font->xgs_id >= 0) { |
| 2102 |
graphics_mode(); |
| 2103 |
pdf_dev_grestore(); |
| 2104 |
} |
| 2105 |
pdf_color_pop(); |
| 2106 |
} |
| 2107 |
RELEASE(xloc); |
| 2108 |
RELEASE(yloc); |
| 2109 |
|
| 2110 |
if (do_actual_text) { |
| 2111 |
pdf_dev_end_actualtext(); |
| 2112 |
} |
| 2113 |
|
| 2114 |
if (lr_mode == LTYPESETTING) |
| 2115 |
dvi_right(width); |
| 2116 |
|
| 2117 |
return; |
| 2118 |
} |
| 2119 |
|
| 2120 |
static void |
| 2121 |
check_postamble (void) |
| 2122 |
{ |
| 2123 |
int code; |
| 2124 |
|
| 2125 |
skip_bytes(28, dvi_file); |
| 2126 |
while ((code = get_unsigned_byte(dvi_file)) != POST_POST) { |
| 2127 |
switch (code) { |
| 2128 |
case FNT_DEF1: case FNT_DEF2: case FNT_DEF3: case FNT_DEF4: |
| 2129 |
skip_bytes(code + 1 - FNT_DEF1, dvi_file); |
| 2130 |
skip_fntdef(); |
| 2131 |
break; |
| 2132 |
case XDV_NATIVE_FONT_DEF: |
| 2133 |
skip_bytes(4, dvi_file); |
| 2134 |
skip_native_font_def(); |
| 2135 |
break; |
| 2136 |
default: |
| 2137 |
ERROR("Unexpected op code (%u) in postamble", code); |
| 2138 |
} |
| 2139 |
} |
| 2140 |
skip_bytes(4, dvi_file); |
| 2141 |
post_id_byte = get_unsigned_byte(dvi_file); |
| 2142 |
if (!(post_id_byte == DVI_ID || post_id_byte == DVIV_ID || post_id_byte == XDV_ID || post_id_byte == XDV_ID_OLD)) { |
| 2143 |
MESG("DVI ID = %d\n", post_id_byte); |
| 2144 |
ERROR(invalid_signature); |
| 2145 |
} |
| 2146 |
check_id_bytes(); |
| 2147 |
if (has_ptex && post_id_byte != DVIV_ID) |
| 2148 |
ERROR ("Saw opcode %i in DVI file not for Ascii pTeX", PTEXDIR); |
| 2149 |
|
| 2150 |
num_pages = 0; /* force loop to terminate */ |
| 2151 |
} |
| 2152 |
|
| 2153 |
/* Most of the work of actually interpreting |
| 2154 |
* the dvi file is here. |
| 2155 |
*/ |
| 2156 |
void |
| 2157 |
dvi_do_page (double page_paper_height, double hmargin, double vmargin) |
| 2158 |
{ |
| 2159 |
unsigned char opcode; |
| 2160 |
|
| 2161 |
/* before this is called, we have scanned the page for papersize specials |
| 2162 |
and the complete DVI data is now in dvi_page_buffer */ |
| 2163 |
dvi_page_buf_index = 0; |
| 2164 |
|
| 2165 |
/* DVI coordinate */ |
| 2166 |
dev_origin_x = hmargin; |
| 2167 |
dev_origin_y = page_paper_height - vmargin; |
| 2168 |
|
| 2169 |
dvi_stack_depth = 0; |
| 2170 |
for (;;) { |
| 2171 |
opcode = get_buffered_unsigned_byte(); |
| 2172 |
|
| 2173 |
if (opcode <= SET_CHAR_127) { |
| 2174 |
dvi_set(opcode); |
| 2175 |
continue; |
| 2176 |
} |
| 2177 |
|
| 2178 |
/* If we are here, we have an opcode that is something |
| 2179 |
* other than SET_CHAR. |
| 2180 |
*/ |
| 2181 |
if (opcode >= FNT_NUM_0 && opcode <= FNT_NUM_63) { |
| 2182 |
do_fnt(opcode-FNT_NUM_0); |
| 2183 |
continue; |
| 2184 |
} |
| 2185 |
|
| 2186 |
switch (opcode) { |
| 2187 |
case SET1: case SET2: case SET3: |
| 2188 |
dvi_set(get_buffered_unsigned_num(opcode-SET1)); break; |
| 2189 |
case SET4: |
| 2190 |
ERROR("Multibyte (>24 bits) character not supported!"); |
| 2191 |
break; |
| 2192 |
|
| 2193 |
case SET_RULE: |
| 2194 |
do_setrule(); |
| 2195 |
break; |
| 2196 |
|
| 2197 |
case PUT1: case PUT2: case PUT3: |
| 2198 |
dvi_put(get_buffered_unsigned_num(opcode-PUT1)); break; |
| 2199 |
case PUT4: |
| 2200 |
ERROR("Multibyte (>24 bits) character not supported!"); |
| 2201 |
break; |
| 2202 |
|
| 2203 |
case PUT_RULE: |
| 2204 |
do_putrule(); |
| 2205 |
break; |
| 2206 |
|
| 2207 |
case NOP: |
| 2208 |
break; |
| 2209 |
|
| 2210 |
case BOP: |
| 2211 |
do_bop(); |
| 2212 |
break; |
| 2213 |
case EOP: |
| 2214 |
do_eop(); |
| 2215 |
if (linear) { |
| 2216 |
if ((opcode = get_unsigned_byte(dvi_file)) == POST) |
| 2217 |
check_postamble(); |
| 2218 |
else |
| 2219 |
ungetc(opcode, dvi_file); |
| 2220 |
} |
| 2221 |
return; |
| 2222 |
|
| 2223 |
case PUSH: |
| 2224 |
dvi_push(); |
| 2225 |
if (lr_mode >= SKIMMING) |
| 2226 |
lr_width_push(); |
| 2227 |
/* The following line needs to go here instead of in |
| 2228 |
* dvi_push() since logical structure of document is |
| 2229 |
* oblivous to virtual fonts. For example the last line on a |
| 2230 |
* page could be at stack level 3 and the page footer should |
| 2231 |
* be at stack level 3. However, if the page footer contains |
| 2232 |
* virtual fonts (or other nested constructions), it could |
| 2233 |
* fool the link breaker into thinking it was a continuation |
| 2234 |
* of the link */ |
| 2235 |
dvi_mark_depth(); |
| 2236 |
break; |
| 2237 |
case POP: |
| 2238 |
dvi_pop(); |
| 2239 |
if (lr_mode >= SKIMMING) |
| 2240 |
lr_width_pop(); |
| 2241 |
/* Above explanation holds for following line too */ |
| 2242 |
dvi_mark_depth(); |
| 2243 |
break; |
| 2244 |
|
| 2245 |
case RIGHT1: case RIGHT2: case RIGHT3: case RIGHT4: |
| 2246 |
dvi_right(get_buffered_signed_num(opcode-RIGHT1)); break; |
| 2247 |
|
| 2248 |
case W0: dvi_w0(); break; |
| 2249 |
case W1: case W2: case W3: case W4: |
| 2250 |
dvi_w(get_buffered_signed_num(opcode-W1)); break; |
| 2251 |
|
| 2252 |
case X0: dvi_x0(); break; |
| 2253 |
case X1: case X2: case X3: case X4: |
| 2254 |
dvi_x(get_buffered_signed_num(opcode-X1)); break; |
| 2255 |
|
| 2256 |
case DOWN1: case DOWN2: case DOWN3: case DOWN4: |
| 2257 |
dvi_down(get_buffered_signed_num(opcode-DOWN1)); break; |
| 2258 |
|
| 2259 |
case Y0: dvi_y0(); break; |
| 2260 |
case Y1: case Y2: case Y3: case Y4: |
| 2261 |
dvi_y(get_buffered_signed_num(opcode-Y1)); break; |
| 2262 |
|
| 2263 |
case Z0: dvi_z0(); break; |
| 2264 |
case Z1: case Z2: case Z3: case Z4: |
| 2265 |
dvi_z(get_buffered_signed_num(opcode-Z1)); break; |
| 2266 |
|
| 2267 |
case FNT1: case FNT2: case FNT3: case FNT4: |
| 2268 |
do_fnt(get_buffered_unsigned_num(opcode-FNT1)); break; |
| 2269 |
|
| 2270 |
/* Specials */ |
| 2271 |
case XXX1: case XXX2: case XXX3: case XXX4: |
| 2272 |
{ |
| 2273 |
int32_t size = get_buffered_unsigned_num(opcode-XXX1); |
| 2274 |
if (size < 0) |
| 2275 |
WARN("DVI: Special with %d bytes???", size); |
| 2276 |
else |
| 2277 |
do_xxx(size); |
| 2278 |
break; |
| 2279 |
} |
| 2280 |
|
| 2281 |
/* These should not occur - processed during pre-scanning */ |
| 2282 |
case FNT_DEF1: case FNT_DEF2: case FNT_DEF3: case FNT_DEF4: |
| 2283 |
break; |
| 2284 |
|
| 2285 |
/* pTeX extension */ |
| 2286 |
case PTEXDIR: |
| 2287 |
need_pTeX(opcode); |
| 2288 |
do_dir(); |
| 2289 |
break; |
| 2290 |
|
| 2291 |
/* XeTeX extensions */ |
| 2292 |
case XDV_GLYPHS: |
| 2293 |
need_XeTeX(opcode); |
| 2294 |
do_glyphs(0); |
| 2295 |
break; |
| 2296 |
case XDV_TEXT_AND_GLYPHS: |
| 2297 |
need_XeTeX(opcode); |
| 2298 |
do_glyphs(1); |
| 2299 |
break; |
| 2300 |
/* should not occur - processed during pre-scanning */ |
| 2301 |
case XDV_NATIVE_FONT_DEF: |
| 2302 |
need_XeTeX(opcode); |
| 2303 |
break; |
| 2304 |
case BEGIN_REFLECT: |
| 2305 |
need_XeTeX(opcode); |
| 2306 |
dvi_begin_reflect(); |
| 2307 |
break; |
| 2308 |
case END_REFLECT: |
| 2309 |
need_XeTeX(opcode); |
| 2310 |
dvi_end_reflect(); |
| 2311 |
break; |
| 2312 |
|
| 2313 |
case POST: |
| 2314 |
if (linear && !processing_page) { |
| 2315 |
/* for linear processing, this means there are no more pages */ |
| 2316 |
num_pages = 0; /* force loop to terminate */ |
| 2317 |
return; |
| 2318 |
} |
| 2319 |
/* else fall through to error case */ |
| 2320 |
case PRE: case POST_POST: |
| 2321 |
ERROR("Unexpected preamble or postamble in dvi file"); |
| 2322 |
break; |
| 2323 |
default: |
| 2324 |
ERROR("Unexpected opcode or DVI file ended prematurely"); |
| 2325 |
} |
| 2326 |
} |
| 2327 |
} |
| 2328 |
|
| 2329 |
double |
| 2330 |
dvi_init (char *dvi_filename, double mag) |
| 2331 |
{ |
| 2332 |
int32_t post_location; |
| 2333 |
|
| 2334 |
if (!dvi_filename) { /* no filename: reading from stdin, probably a pipe */ |
| 2335 |
int ch; |
| 2336 |
#ifdef WIN32 |
| 2337 |
setmode(fileno(stdin), _O_BINARY); |
| 2338 |
#endif |
| 2339 |
dvi_file = stdin; |
| 2340 |
linear = 1; |
| 2341 |
|
| 2342 |
get_preamble_dvi_info(); |
| 2343 |
do_scales(mag); |
| 2344 |
if ((ch = get_unsigned_byte(dvi_file)) == POST) |
| 2345 |
check_postamble(); |
| 2346 |
else |
| 2347 |
ungetc(ch, dvi_file); |
| 2348 |
} else { |
| 2349 |
char *p, *saved_orig_name; |
| 2350 |
dvi_file = NULL; |
| 2351 |
saved_orig_name = xstrdup(dvi_filename); |
| 2352 |
p = strrchr(dvi_filename, '.'); |
| 2353 |
if (p == NULL || (!FILESTRCASEEQ(p, ".dvi") && |
| 2354 |
!FILESTRCASEEQ(p, ".xdv"))) { |
| 2355 |
strcat(dvi_filename, ".xdv"); |
| 2356 |
dvi_file = MFOPEN(dvi_filename, FOPEN_RBIN_MODE); |
| 2357 |
if (!dvi_file) { |
| 2358 |
dvi_filename[strlen(dvi_filename) - 4] = '\0'; |
| 2359 |
strcat(dvi_filename, ".dvi"); |
| 2360 |
dvi_file = MFOPEN(dvi_filename, FOPEN_RBIN_MODE); |
| 2361 |
} |
| 2362 |
} |
| 2363 |
if (!dvi_file) |
| 2364 |
dvi_file = MFOPEN(saved_orig_name, FOPEN_RBIN_MODE); |
| 2365 |
free(saved_orig_name); |
| 2366 |
|
| 2367 |
if (!dvi_file) { |
| 2368 |
ERROR("Could not open specified DVI (or XDV) file: %s", |
| 2369 |
dvi_filename); |
| 2370 |
return 0.0; |
| 2371 |
} |
| 2372 |
|
| 2373 |
/* DVI files are most easily read backwards by |
| 2374 |
* searching for post_post and then post opcode. |
| 2375 |
*/ |
| 2376 |
post_location = find_post(); |
| 2377 |
get_dvi_info(post_location); |
| 2378 |
do_scales(mag); |
| 2379 |
get_page_info(post_location); |
| 2380 |
get_comment(); |
| 2381 |
get_dvi_fonts(post_location); |
| 2382 |
} |
| 2383 |
clear_state(); |
| 2384 |
|
| 2385 |
dvi_page_buf_size = DVI_PAGE_BUF_CHUNK; |
| 2386 |
dvi_page_buffer = NEW(dvi_page_buf_size, unsigned char); |
| 2387 |
|
| 2388 |
return dvi2pts; |
| 2389 |
} |
| 2390 |
|
| 2391 |
void |
| 2392 |
dvi_close (void) |
| 2393 |
{ |
| 2394 |
int i; |
| 2395 |
|
| 2396 |
if (linear) { |
| 2397 |
/* probably reading a pipe from xetex; consume any remaining data */ |
| 2398 |
while (fgetc(dvi_file) != EOF) |
| 2399 |
; |
| 2400 |
} |
| 2401 |
|
| 2402 |
/* We add comment in dvi_close instead of dvi_init so user |
| 2403 |
* has a change to overwrite it. The docinfo dictionary is |
| 2404 |
* treated as a write-once record. |
| 2405 |
*/ |
| 2406 |
|
| 2407 |
/* Do some house cleaning */ |
| 2408 |
MFCLOSE(dvi_file); |
| 2409 |
dvi_file = NULL; |
| 2410 |
|
| 2411 |
if (def_fonts) { |
| 2412 |
for (i = 0; i < num_def_fonts; i++) { |
| 2413 |
if (def_fonts[i].font_name) |
| 2414 |
RELEASE(def_fonts[i].font_name); |
| 2415 |
def_fonts[i].font_name = NULL; |
| 2416 |
} |
| 2417 |
RELEASE(def_fonts); |
| 2418 |
} |
| 2419 |
def_fonts = NULL; |
| 2420 |
|
| 2421 |
if (page_loc) |
| 2422 |
RELEASE(page_loc); |
| 2423 |
page_loc = NULL; |
| 2424 |
num_pages = 0; |
| 2425 |
|
| 2426 |
for (i = 0; i < num_loaded_fonts; i++) |
| 2427 |
{ |
| 2428 |
if (loaded_fonts[i].gm != NULL) |
| 2429 |
RELEASE(loaded_fonts[i].gm); |
| 2430 |
|
| 2431 |
loaded_fonts[i].gm = NULL; |
| 2432 |
} |
| 2433 |
|
| 2434 |
if (loaded_fonts) |
| 2435 |
RELEASE(loaded_fonts); |
| 2436 |
loaded_fonts = NULL; |
| 2437 |
num_loaded_fonts = 0; |
| 2438 |
|
| 2439 |
vf_close_all_fonts(); |
| 2440 |
tfm_close_all (); |
| 2441 |
|
| 2442 |
if (dvi_page_buffer) { |
| 2443 |
RELEASE(dvi_page_buffer); |
| 2444 |
dvi_page_buffer = NULL; |
| 2445 |
dvi_page_buf_size = 0; |
| 2446 |
} |
| 2447 |
} |
| 2448 |
|
| 2449 |
/* The following are need to implement virtual fonts |
| 2450 |
According to documentation, the vf "subroutine" |
| 2451 |
must have state pushed and must have |
| 2452 |
w,v,y, and z set to zero. The current font |
| 2453 |
is determined by the virtual font header, which |
| 2454 |
may be undefined */ |
| 2455 |
|
| 2456 |
static int saved_dvi_font[VF_NESTING_MAX]; |
| 2457 |
static int num_saved_fonts = 0; |
| 2458 |
|
| 2459 |
void |
| 2460 |
dvi_vf_init (int dev_font_id) |
| 2461 |
{ |
| 2462 |
dvi_push(); |
| 2463 |
|
| 2464 |
dvi_state.w = 0; dvi_state.x = 0; |
| 2465 |
dvi_state.y = 0; dvi_state.z = 0; |
| 2466 |
|
| 2467 |
/* do not reset dvi_state.d. */ |
| 2468 |
if (num_saved_fonts < VF_NESTING_MAX) { |
| 2469 |
saved_dvi_font[num_saved_fonts++] = current_font; |
| 2470 |
} else |
| 2471 |
ERROR("Virtual fonts nested too deeply!"); |
| 2472 |
current_font = dev_font_id; |
| 2473 |
} |
| 2474 |
|
| 2475 |
/* After VF subroutine is finished, we simply pop the DVI stack */ |
| 2476 |
void |
| 2477 |
dvi_vf_finish (void) |
| 2478 |
{ |
| 2479 |
dvi_pop(); |
| 2480 |
if (num_saved_fonts > 0) |
| 2481 |
current_font = saved_dvi_font[--num_saved_fonts]; |
| 2482 |
else { |
| 2483 |
ERROR("Tried to pop an empty font stack"); |
| 2484 |
} |
| 2485 |
} |
| 2486 |
|
| 2487 |
|
| 2488 |
/* Scan various specials */ |
| 2489 |
#include "dpxutil.h" |
| 2490 |
/* For MAX_PWD_LEN */ |
| 2491 |
#include "pdfencrypt.h" |
| 2492 |
|
| 2493 |
static int |
| 2494 |
scan_special_encrypt (int *key_bits, int32_t *permission, char *opassword, char *upassword, |
| 2495 |
const char **curptr, const char *endptr) |
| 2496 |
{ |
| 2497 |
int error = 0; |
| 2498 |
const char *p = *curptr; |
| 2499 |
|
| 2500 |
skip_white(&p, endptr); |
| 2501 |
|
| 2502 |
opassword[0] = '\0'; |
| 2503 |
upassword[0] = '\0'; |
| 2504 |
while (!error && p < endptr) { |
| 2505 |
char *kp = parse_c_ident(&p, endptr); |
| 2506 |
if (!kp) |
| 2507 |
break; |
| 2508 |
else { |
| 2509 |
pdf_obj *obj; |
| 2510 |
skip_white(&p, endptr); |
| 2511 |
if (!strcmp(kp, "ownerpw")) { |
| 2512 |
if ((obj = parse_pdf_string(&p, endptr))) { |
| 2513 |
if (pdf_string_value(obj)) { |
| 2514 |
int str_length = (MAX_PWD_LEN - 1 > pdf_string_length(obj) |
| 2515 |
? pdf_string_length(obj) : MAX_PWD_LEN - 1); |
| 2516 |
strncpy(opassword, pdf_string_value(obj), str_length); |
| 2517 |
opassword[str_length] = '\0'; |
| 2518 |
} |
| 2519 |
pdf_release_obj(obj); |
| 2520 |
} else |
| 2521 |
error = -1; |
| 2522 |
} else if (!strcmp(kp, "userpw")) { |
| 2523 |
if ((obj = parse_pdf_string(&p, endptr))) { |
| 2524 |
if (pdf_string_value(obj)) { |
| 2525 |
int str_length = (MAX_PWD_LEN - 1 > pdf_string_length(obj) |
| 2526 |
? pdf_string_length(obj) : MAX_PWD_LEN - 1); |
| 2527 |
strncpy(upassword, pdf_string_value(obj), str_length); |
| 2528 |
upassword[str_length] = '\0'; |
| 2529 |
} |
| 2530 |
pdf_release_obj(obj); |
| 2531 |
} else |
| 2532 |
error = -1; |
| 2533 |
} else if (!strcmp(kp, "length")) { |
| 2534 |
if ((obj = parse_pdf_number(&p, endptr)) && PDF_OBJ_NUMBERTYPE(obj)) { |
| 2535 |
*key_bits = (unsigned) pdf_number_value(obj); |
| 2536 |
} else |
| 2537 |
error = -1; |
| 2538 |
if (obj) |
| 2539 |
pdf_release_obj(obj); |
| 2540 |
} else if (!strcmp(kp, "perm")) { |
| 2541 |
if ((obj = parse_pdf_number(&p, endptr)) && PDF_OBJ_NUMBERTYPE(obj)) { |
| 2542 |
*permission = (unsigned) pdf_number_value(obj); |
| 2543 |
} else |
| 2544 |
error = -1; |
| 2545 |
if (obj) |
| 2546 |
pdf_release_obj(obj); |
| 2547 |
} else |
| 2548 |
error = -1; |
| 2549 |
RELEASE(kp); |
| 2550 |
} |
| 2551 |
skip_white(&p, endptr); |
| 2552 |
} |
| 2553 |
*curptr = p; |
| 2554 |
|
| 2555 |
return error; |
| 2556 |
} |
| 2557 |
|
| 2558 |
static int |
| 2559 |
scan_special_trailerid (unsigned char *id1, unsigned char *id2, |
| 2560 |
const char **curptr, const char *endptr) |
| 2561 |
{ |
| 2562 |
int error = 0; |
| 2563 |
pdf_obj *id_array; |
| 2564 |
const char *p = *curptr; |
| 2565 |
|
| 2566 |
skip_white(&p, endptr); |
| 2567 |
id_array = parse_pdf_array(&p, endptr, NULL); |
| 2568 |
if (id_array) { |
| 2569 |
if (pdf_array_length(id_array) == 2) { |
| 2570 |
pdf_obj *tmp1, *tmp2; |
| 2571 |
tmp1 = pdf_get_array(id_array, 0); |
| 2572 |
tmp2 = pdf_get_array(id_array, 1); |
| 2573 |
if (PDF_OBJ_STRINGTYPE(tmp1) && pdf_string_length(tmp1) == 16 && |
| 2574 |
PDF_OBJ_STRINGTYPE(tmp2) && pdf_string_length(tmp2) == 16) { |
| 2575 |
memcpy(id1, pdf_string_value(tmp1), 16); |
| 2576 |
memcpy(id2, pdf_string_value(tmp2), 16); |
| 2577 |
} else { |
| 2578 |
error = -1; |
| 2579 |
} |
| 2580 |
} else { |
| 2581 |
error = -1; |
| 2582 |
} |
| 2583 |
pdf_release_obj(id_array); |
| 2584 |
} else { |
| 2585 |
error = -1; |
| 2586 |
} |
| 2587 |
skip_white(&p, endptr); |
| 2588 |
*curptr = p; |
| 2589 |
|
| 2590 |
return error; |
| 2591 |
} |
| 2592 |
|
| 2593 |
static int |
| 2594 |
scan_special (double *wd, double *ht, double *xo, double *yo, int *lm, |
| 2595 |
int *majorversion, int *minorversion, |
| 2596 |
int *enable_encryption, int *key_bits, int32_t *permission, |
| 2597 |
char *opassword, char *upassword, |
| 2598 |
int *has_id, unsigned char *id1, unsigned char *id2, |
| 2599 |
const char *buf, uint32_t size) |
| 2600 |
{ |
| 2601 |
char *q; |
| 2602 |
const char *p = buf, *endptr; |
| 2603 |
int ns_pdf = 0, ns_dvipdfmx = 0, error = 0; |
| 2604 |
double tmp; |
| 2605 |
|
| 2606 |
endptr = p + size; |
| 2607 |
|
| 2608 |
skip_white(&p, endptr); |
| 2609 |
|
| 2610 |
q = parse_c_ident(&p, endptr); |
| 2611 |
if (q && !strcmp(q, "pdf")) { |
| 2612 |
skip_white(&p, endptr); |
| 2613 |
if (p < endptr && *p == ':') { |
| 2614 |
p++; |
| 2615 |
skip_white(&p, endptr); |
| 2616 |
RELEASE(q); |
| 2617 |
q = parse_c_ident(&p, endptr); ns_pdf = 1; |
| 2618 |
} |
| 2619 |
} |
| 2620 |
else if (q && !strcmp(q, "x")) { |
| 2621 |
skip_white(&p, endptr); |
| 2622 |
if (p < endptr && *p == ':') { |
| 2623 |
p++; |
| 2624 |
skip_white(&p, endptr); |
| 2625 |
RELEASE(q); |
| 2626 |
q = parse_c_ident(&p, endptr); |
| 2627 |
} |
| 2628 |
} |
| 2629 |
else if (q && !strcmp(q, "dvipdfmx")) { |
| 2630 |
skip_white(&p, endptr); |
| 2631 |
if (p < endptr && *p == ':') { |
| 2632 |
p++; |
| 2633 |
skip_white(&p, endptr); |
| 2634 |
RELEASE(q); |
| 2635 |
q = parse_c_ident(&p, endptr); ns_dvipdfmx = 1; |
| 2636 |
} |
| 2637 |
} |
| 2638 |
skip_white(&p, endptr); |
| 2639 |
|
| 2640 |
if (q) { |
| 2641 |
if (!strcmp(q, "landscape")) { |
| 2642 |
*lm = 1; |
| 2643 |
} else if (ns_pdf && !strcmp(q, "pagesize")) { |
| 2644 |
while (!error && p < endptr) { |
| 2645 |
char *kp = parse_c_ident(&p, endptr); |
| 2646 |
if (!kp) |
| 2647 |
break; |
| 2648 |
else { |
| 2649 |
skip_white(&p, endptr); |
| 2650 |
if (!strcmp(kp, "width")) { |
| 2651 |
error = dpx_util_read_length(&tmp, dvi_tell_mag(), &p, endptr); |
| 2652 |
if (!error) |
| 2653 |
*wd = tmp * dvi_tell_mag(); |
| 2654 |
} else if (!strcmp(kp, "height")) { |
| 2655 |
error = dpx_util_read_length(&tmp, dvi_tell_mag(), &p, endptr); |
| 2656 |
if (!error) |
| 2657 |
*ht = tmp * dvi_tell_mag(); |
| 2658 |
} else if (!strcmp(kp, "xoffset")) { |
| 2659 |
error = dpx_util_read_length(&tmp, dvi_tell_mag(), &p, endptr); |
| 2660 |
if (!error) |
| 2661 |
*xo = tmp * dvi_tell_mag(); |
| 2662 |
} else if (!strcmp(kp, "yoffset")) { |
| 2663 |
error = dpx_util_read_length(&tmp, dvi_tell_mag(), &p, endptr); |
| 2664 |
if (!error) |
| 2665 |
*yo = tmp * dvi_tell_mag(); |
| 2666 |
} else if (!strcmp(kp, "default")) { |
| 2667 |
*wd = paper_width; |
| 2668 |
*ht = paper_height; |
| 2669 |
*lm = landscape_mode; |
| 2670 |
*xo = *yo = 72.0; |
| 2671 |
} |
| 2672 |
RELEASE(kp); |
| 2673 |
} |
| 2674 |
skip_white(&p, endptr); |
| 2675 |
} |
| 2676 |
} else if (!strcmp(q, "papersize")) { |
| 2677 |
char qchr = 0; |
| 2678 |
if (*p == '=') p++; |
| 2679 |
skip_white(&p, endptr); |
| 2680 |
if (p < endptr && (*p == '\'' || *p == '\"')) { |
| 2681 |
qchr = *p; p++; |
| 2682 |
skip_white(&p, endptr); |
| 2683 |
} |
| 2684 |
error = dpx_util_read_length(&tmp, 1.0, &p, endptr); |
| 2685 |
if (!error) { |
| 2686 |
double tmp1; |
| 2687 |
|
| 2688 |
skip_white(&p, endptr); |
| 2689 |
if (p < endptr && *p == ',') { |
| 2690 |
p++; skip_white(&p, endptr); |
| 2691 |
} |
| 2692 |
error = dpx_util_read_length(&tmp1, 1.0, &p, endptr); |
| 2693 |
if (!error) { |
| 2694 |
*wd = tmp; |
| 2695 |
*ht = tmp1; |
| 2696 |
skip_white(&p, endptr); |
| 2697 |
} |
| 2698 |
} |
| 2699 |
if (!error && qchr) { /* Check if properly quoted */ |
| 2700 |
if (p >= endptr || *p != qchr) |
| 2701 |
error = -1; |
| 2702 |
} |
| 2703 |
if (error == 0) { |
| 2704 |
paper_width = *wd; |
| 2705 |
paper_height = *ht; |
| 2706 |
} |
| 2707 |
} else if (minorversion && ns_pdf && !strcmp(q, "minorversion")) { |
| 2708 |
char *kv; |
| 2709 |
if (*p == '=') p++; |
| 2710 |
skip_white(&p, endptr); |
| 2711 |
kv = parse_float_decimal(&p, endptr); |
| 2712 |
if (kv) { |
| 2713 |
*minorversion = (int)strtol(kv, NULL, 10); |
| 2714 |
RELEASE(kv); |
| 2715 |
} |
| 2716 |
} else if (majorversion && ns_pdf && !strcmp(q, "majorversion")) { |
| 2717 |
char *kv; |
| 2718 |
if (*p == '=') p++; |
| 2719 |
skip_white(&p, endptr); |
| 2720 |
kv = parse_float_decimal(&p, endptr); |
| 2721 |
if (kv) { |
| 2722 |
*majorversion = (int)strtol(kv, NULL, 10); |
| 2723 |
RELEASE(kv); |
| 2724 |
} |
| 2725 |
} else if (enable_encryption && ns_pdf && !strcmp(q, "encrypt")) { |
| 2726 |
*enable_encryption = 1; |
| 2727 |
error = scan_special_encrypt(key_bits, permission, opassword, upassword, &p, endptr); |
| 2728 |
} else if (ns_dvipdfmx && !strcmp(q, "config")) { |
| 2729 |
read_config_special(&p, endptr); |
| 2730 |
} else if (has_id && id1 && id2 && ns_pdf && !strcmp(q, "trailerid")) { |
| 2731 |
error = scan_special_trailerid(id1, id2, &p, endptr); |
| 2732 |
if (error) { |
| 2733 |
WARN("Invalid argument for pdf:trailerid special."); |
| 2734 |
*has_id = 0; |
| 2735 |
} else { |
| 2736 |
*has_id = 1; |
| 2737 |
} |
| 2738 |
} |
| 2739 |
RELEASE(q); |
| 2740 |
} |
| 2741 |
|
| 2742 |
return error; |
| 2743 |
} |
| 2744 |
|
| 2745 |
|
| 2746 |
void |
| 2747 |
dvi_scan_specials (int page_no, |
| 2748 |
double *page_width, double *page_height, |
| 2749 |
double *x_offset, double *y_offset, int *landscape, |
| 2750 |
int *majorversion, int *minorversion, |
| 2751 |
int *do_enc, int *key_bits, int32_t *permission, |
| 2752 |
char *owner_pw, char *user_pw, |
| 2753 |
int *has_id, unsigned char *id1, unsigned char *id2) |
| 2754 |
{ |
| 2755 |
FILE *fp = dvi_file; |
| 2756 |
int32_t offset; |
| 2757 |
unsigned char opcode; |
| 2758 |
static int buffered_page = -1; |
| 2759 |
unsigned int len; |
| 2760 |
|
| 2761 |
if (page_no == buffered_page || num_pages == 0) |
| 2762 |
return; /* because dvipdfmx wants to scan first page twice! */ |
| 2763 |
buffered_page = page_no; |
| 2764 |
|
| 2765 |
dvi_page_buf_index = 0; |
| 2766 |
|
| 2767 |
if (!linear) { |
| 2768 |
if (page_no >= num_pages) |
| 2769 |
ERROR("Invalid page number: %u", page_no); |
| 2770 |
offset = page_loc[page_no]; |
| 2771 |
|
| 2772 |
xseek_absolute (fp, offset, "DVI"); |
| 2773 |
} |
| 2774 |
|
| 2775 |
while ((opcode = get_and_buffer_unsigned_byte(fp)) != EOP) { |
| 2776 |
if (opcode <= SET_CHAR_127 || |
| 2777 |
(opcode >= FNT_NUM_0 && opcode <= FNT_NUM_63)) |
| 2778 |
continue; |
| 2779 |
else if (opcode == XXX1 || opcode == XXX2 || |
| 2780 |
opcode == XXX3 || opcode == XXX4) { |
| 2781 |
uint32_t size = get_and_buffer_unsigned_byte(fp); |
| 2782 |
switch (opcode) { |
| 2783 |
case XXX4: size = size * 0x100u + get_and_buffer_unsigned_byte(fp); |
| 2784 |
if (size > 0x7fff) |
| 2785 |
WARN("Unsigned number starting with %x exceeds 0x7fffffff", size); |
| 2786 |
case XXX3: size = size * 0x100u + get_and_buffer_unsigned_byte(fp); |
| 2787 |
case XXX2: size = size * 0x100u + get_and_buffer_unsigned_byte(fp); |
| 2788 |
default: break; |
| 2789 |
} |
| 2790 |
if (dvi_page_buf_index + size >= dvi_page_buf_size) { |
| 2791 |
dvi_page_buf_size = (dvi_page_buf_index + size + DVI_PAGE_BUF_CHUNK); |
| 2792 |
dvi_page_buffer = RENEW(dvi_page_buffer, dvi_page_buf_size, unsigned char); |
| 2793 |
} |
| 2794 |
#define buf ((char*)(dvi_page_buffer + dvi_page_buf_index)) |
| 2795 |
if (fread(buf, sizeof(char), size, fp) != size) |
| 2796 |
ERROR("Reading DVI file failed!"); |
| 2797 |
if (scan_special(page_width, page_height, x_offset, y_offset, landscape, |
| 2798 |
majorversion, minorversion, |
| 2799 |
do_enc, key_bits, permission, owner_pw, user_pw, |
| 2800 |
has_id, id1, id2, |
| 2801 |
buf, size)) |
| 2802 |
WARN("Reading special command failed: \"%.*s\"", size, buf); |
| 2803 |
#undef buf |
| 2804 |
dvi_page_buf_index += size; |
| 2805 |
continue; |
| 2806 |
} |
| 2807 |
|
| 2808 |
/* Skipping... */ |
| 2809 |
switch (opcode) { |
| 2810 |
case BOP: |
| 2811 |
get_and_buffer_bytes(fp, 44); |
| 2812 |
break; |
| 2813 |
case NOP: case PUSH: case POP: |
| 2814 |
case W0: case X0: case Y0: case Z0: |
| 2815 |
break; |
| 2816 |
case SET1: case PUT1: case RIGHT1: case DOWN1: |
| 2817 |
case W1: case X1: case Y1: case Z1: case FNT1: |
| 2818 |
get_and_buffer_bytes(fp, 1); |
| 2819 |
break; |
| 2820 |
|
| 2821 |
case SET2: case PUT2: case RIGHT2: case DOWN2: |
| 2822 |
case W2: case X2: case Y2: case Z2: case FNT2: |
| 2823 |
get_and_buffer_bytes(fp, 2); |
| 2824 |
break; |
| 2825 |
|
| 2826 |
case SET3: case PUT3: case RIGHT3: case DOWN3: |
| 2827 |
case W3: case X3: case Y3: case Z3: case FNT3: |
| 2828 |
get_and_buffer_bytes(fp, 3); |
| 2829 |
break; |
| 2830 |
|
| 2831 |
case SET4: case PUT4: case RIGHT4: case DOWN4: |
| 2832 |
case W4: case X4: case Y4: case Z4: case FNT4: |
| 2833 |
get_and_buffer_bytes(fp, 4); |
| 2834 |
break; |
| 2835 |
|
| 2836 |
case SET_RULE: case PUT_RULE: |
| 2837 |
get_and_buffer_bytes(fp, 8); |
| 2838 |
break; |
| 2839 |
|
| 2840 |
case FNT_DEF1: case FNT_DEF2: case FNT_DEF3: case FNT_DEF4: |
| 2841 |
do_fntdef(get_unsigned_num(fp, opcode-FNT_DEF1)); |
| 2842 |
break; |
| 2843 |
case XDV_GLYPHS: |
| 2844 |
need_XeTeX(opcode); |
| 2845 |
get_and_buffer_bytes(fp, 4); /* width */ |
| 2846 |
len = get_and_buffer_unsigned_pair(fp); /* glyph count */ |
| 2847 |
get_and_buffer_bytes(fp, len * 10); /* 2 bytes ID + 8 bytes x,y-location per glyph */ |
| 2848 |
break; |
| 2849 |
case XDV_TEXT_AND_GLYPHS: |
| 2850 |
need_XeTeX(opcode); |
| 2851 |
len = get_and_buffer_unsigned_pair(fp); /* utf16 code unit count */ |
| 2852 |
get_and_buffer_bytes(fp, len * 2); /* 2 bytes per code unit */ |
| 2853 |
get_and_buffer_bytes(fp, 4); /* width */ |
| 2854 |
len = get_and_buffer_unsigned_pair(fp); /* glyph count */ |
| 2855 |
get_and_buffer_bytes(fp, len * 10); /* 2 bytes ID + 8 bytes x,y-location per glyph */ |
| 2856 |
break; |
| 2857 |
case XDV_NATIVE_FONT_DEF: |
| 2858 |
need_XeTeX(opcode); |
| 2859 |
do_native_font_def(get_signed_quad(dvi_file)); |
| 2860 |
break; |
| 2861 |
case BEGIN_REFLECT: |
| 2862 |
case END_REFLECT: |
| 2863 |
need_XeTeX(opcode); |
| 2864 |
break; |
| 2865 |
|
| 2866 |
case PTEXDIR: |
| 2867 |
need_pTeX(opcode); |
| 2868 |
get_and_buffer_bytes(fp, 1); |
| 2869 |
break; |
| 2870 |
|
| 2871 |
case POST: |
| 2872 |
if (linear && dvi_page_buf_index == 1) { |
| 2873 |
/* this is actually an indication that we've reached the end of the input */ |
| 2874 |
return; |
| 2875 |
} |
| 2876 |
/* else fall through to error case */ |
| 2877 |
default: /* case PRE: case POST_POST: and others */ |
| 2878 |
ERROR("Unexpected opcode %d at pos=0x%x", opcode, tell_position(fp)); |
| 2879 |
break; |
| 2880 |
} |
| 2881 |
} |
| 2882 |
|
| 2883 |
return; |
| 2884 |
} |
| 2885 |
#if defined(LIBDPX) |
| 2886 |
#include "dvi_ng.c" |
| 2887 |
#endif /* LIBDPX */ |