Skip to content

Commit 3c4dbf5

Browse files
authored
Merge pull request #953 from vlasakm/pdfio_fixes
Fix low level dvipdfmx issues
2 parents e36e7d4 + bb4aa0e commit 3c4dbf5

5 files changed

Lines changed: 104 additions & 28 deletions

File tree

crates/engine_xdvipdfmx/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021 the Tectonic Project
1+
# Copyright 2021-2022 the Tectonic Project
22
# Licensed under the MIT License.
33

44
# See README.md for discussion of features (or lack thereof) in this crate.
@@ -30,4 +30,4 @@ cc = "^1.0.66"
3030
[package.metadata.internal_dep_versions]
3131
tectonic_bridge_core = "thiscommit:2021-01-17:fohCh1sh"
3232
tectonic_errors = "5c9ba661edf5ef669f24f9904f99cca369d999e7"
33-
tectonic_pdf_io = "thiscommit:2022-03-29:6nizWic"
33+
tectonic_pdf_io = "thiscommit:2022-10-21:pkYKcMI"

crates/engine_xdvipdfmx/xdvipdfmx/dvipdfmx.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,14 @@ typedef struct page_range
6666
int last;
6767
} PageRange;
6868

69+
/* Compatibility options */
6970
#define OPT_TPIC_TRANSPARENT_FILL (1 << 1)
7071
#define OPT_CIDFONT_FIXEDPITCH (1 << 2)
7172
#define OPT_FONTMAP_FIRST_MATCH (1 << 3)
7273
#define OPT_PDFDOC_NO_DEST_REMOVE (1 << 4)
7374
#define OPT_PDFOBJ_NO_PREDICTOR (1 << 5)
7475
#define OPT_PDFOBJ_NO_OBJSTM (1 << 6)
7576

76-
static int pdf_version_major = 1;
77-
static int pdf_version_minor = 5;
78-
static int compression_level = 9;
79-
80-
static double annot_grow_x = 0.0;
81-
static double annot_grow_y = 0.0;
8277
static char ignore_colors = 0;
8378
static int bookmark_open = 0;
8479
static double mag = 1.0;
@@ -248,10 +243,18 @@ do_dvi_pages (void)
248243
xo = x_offset; yo = y_offset;
249244
dvi_scan_specials(page_no,
250245
&w, &h, &xo, &yo, &lm,
246+
/* No PDF version */
247+
NULL, NULL,
248+
/* No compression */
249+
NULL,
250+
/* No annotation grow */
251+
NULL, NULL,
251252
/* No need for encryption options */
252-
NULL, NULL, NULL, NULL, NULL, NULL, NULL,
253+
NULL, NULL, NULL, NULL, NULL,
253254
/* No trailer IDs */
254-
NULL, NULL, NULL);
255+
NULL, NULL, NULL,
256+
/* No opt_flags */
257+
NULL);
255258
if (lm != landscape_mode) { /* already swapped for the first page */
256259
SWAP(w, h);
257260
landscape_mode = lm;
@@ -336,6 +339,11 @@ dvipdfmx_main (
336339
int has_id = 0;
337340
unsigned char id1[16], id2[16];
338341
struct pdf_setting settings;
342+
int pdf_version_major = 1;
343+
int pdf_version_minor = 5;
344+
int compression_level = 9;
345+
double annot_grow_x = 0;
346+
double annot_grow_y = 0;
339347

340348
assert(pdf_filename);
341349
assert(dvi_filename);
@@ -369,8 +377,7 @@ dvipdfmx_main (
369377
* code bits. */
370378

371379
select_paper(paperspec);
372-
annot_grow_x = 0;
373-
annot_grow_y = 0;
380+
374381
bookmark_open = 0;
375382
key_bits = 40;
376383
permission = 0x003C;
@@ -397,8 +404,10 @@ dvipdfmx_main (
397404
&paper_width, &paper_height,
398405
&x_offset, &y_offset, &landscape_mode,
399406
&pdf_version_major, &pdf_version_minor,
407+
&compression_level,
408+
&annot_grow_x, &annot_grow_y,
400409
&do_encryption, &key_bits, &permission, oplain, uplain,
401-
&has_id, id1, id2);
410+
&has_id, id1, id2, &opt_flags);
402411
}
403412

404413
/*kpse_init_prog("", font_dpi, NULL, NULL);
@@ -518,4 +527,4 @@ tt_engine_xdvipdfmx_main(
518527

519528
ttbc_global_engine_exit();
520529
return rv;
521-
}
530+
}

crates/pdf_io/pdf_io/dpx-dvi.c

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,12 +2490,79 @@ scan_special_trailerid (unsigned char *id1, unsigned char *id2,
24902490
return error;
24912491
}
24922492

2493+
static void
2494+
scan_special_config (const char **start, const char *end, int *opt_flags,
2495+
int *compression_level,
2496+
double *annot_grow_x, double *annot_grow_y)
2497+
{
2498+
/* This section of code mirrors read_config_special() in `dvipdfm-x/dvipdfmx.c`. */
2499+
2500+
skip_white(start, end);
2501+
if (*start >= end)
2502+
return;
2503+
2504+
char *option = parse_ident(start, end);
2505+
if (!option)
2506+
return;
2507+
2508+
char *arg = NULL;
2509+
skip_white(start, end);
2510+
if (*start < end) {
2511+
if (**start == '"')
2512+
arg = parse_c_string(start, end);
2513+
else
2514+
arg = parse_ident(start, end);
2515+
}
2516+
2517+
/* This section of code implements a subset of do_args_second_pass() in the same file. */
2518+
2519+
if (streq_ptr(option, "C") && arg && opt_flags) {
2520+
char *num_end;
2521+
int flags = (unsigned) strtol(arg, &num_end, 0);
2522+
if (num_end == arg)
2523+
dpx_warning("Invalid dvipdfmx compatibility flag: '%s'", arg);
2524+
else if (flags < 0)
2525+
*opt_flags = -flags;
2526+
else
2527+
*opt_flags |= flags;
2528+
} else if (streq_ptr(option, "z") && arg && compression_level) {
2529+
*compression_level = atoi(arg);
2530+
} else if (streq_ptr(option, "g") && arg && annot_grow_x && annot_grow_y) {
2531+
const char *comma = strchr(arg, ',');
2532+
const char *arg_ptr = arg; /* dpx_until_read_length changes the pointer */
2533+
const char *arg_end = arg + strlen(arg);
2534+
int error;
2535+
if (comma) {
2536+
error = dpx_util_read_length(annot_grow_x, 1.0, &arg_ptr, comma);
2537+
arg_ptr = comma + 1;
2538+
if (!error)
2539+
error = dpx_util_read_length(annot_grow_y, 1.0, &arg_ptr, arg_end);
2540+
} else {
2541+
error = dpx_util_read_length(annot_grow_x, 1.0, &arg_ptr, arg_end);
2542+
if (!error)
2543+
*annot_grow_y = *annot_grow_x;
2544+
}
2545+
if (error) {
2546+
dpx_warning("Error reading argument for \"-g\" option: %s", arg);
2547+
}
2548+
} else {
2549+
dpx_warning("Tectonic doesn't support '%s' config special"
2550+
" or the argument is missing", option);
2551+
}
2552+
2553+
free(arg);
2554+
free(option);
2555+
}
2556+
24932557
static int
24942558
scan_special (double *wd, double *ht, double *xo, double *yo, int *lm,
24952559
int *majorversion, int *minorversion,
2560+
int *compression_level,
2561+
double *annot_grow_x, double *annot_grow_y,
24962562
int *enable_encryption, int *key_bits, int32_t *permission,
24972563
char *opassword, char *upassword,
24982564
int *has_id, unsigned char *id1, unsigned char *id2,
2565+
int *opt_flags,
24992566
const char *buf, uint32_t size)
25002567
{
25012568
char *q;
@@ -2626,7 +2693,7 @@ scan_special (double *wd, double *ht, double *xo, double *yo, int *lm,
26262693
*enable_encryption = 1;
26272694
error = scan_special_encrypt(key_bits, permission, opassword, upassword, &p, endptr);
26282695
} else if (ns_dvipdfmx && streq_ptr(q, "config")) {
2629-
dpx_warning("Tectonic does not support `config' special. Ignored.");
2696+
scan_special_config(&p, endptr, opt_flags, compression_level, annot_grow_x, annot_grow_y);
26302697
} else if (has_id && id1 && id2 && ns_pdf && !strcmp(q, "trailerid")) {
26312698
error = scan_special_trailerid(id1, id2, &p, endptr);
26322699
if (error) {
@@ -2648,9 +2715,12 @@ dvi_scan_specials (int page_no,
26482715
double *page_width, double *page_height,
26492716
double *x_offset, double *y_offset, int *landscape,
26502717
int *majorversion, int *minorversion,
2718+
int *compression_level,
2719+
double *annot_grow_x, double *annot_grow_y,
26512720
int *do_enc, int *key_bits, int32_t *permission,
26522721
char *owner_pw, char *user_pw,
2653-
int *has_id, unsigned char *id1, unsigned char *id2)
2722+
int *has_id, unsigned char *id1, unsigned char *id2,
2723+
int *opt_flags)
26542724
{
26552725
uint32_t offset;
26562726
unsigned char opcode;
@@ -2694,8 +2764,11 @@ dvi_scan_specials (int page_no,
26942764
_tt_abort("Reading DVI file failed!");
26952765
if (scan_special(page_width, page_height, x_offset, y_offset, landscape,
26962766
majorversion, minorversion,
2767+
compression_level,
2768+
annot_grow_x, annot_grow_y,
26972769
do_enc, key_bits, permission, owner_pw, user_pw,
26982770
has_id, id1, id2,
2771+
opt_flags,
26992772
buf, size))
27002773
dpx_warning("Reading special command failed: \"%.*s\"", size, buf);
27012774
#undef buf

crates/pdf_io/pdf_io/dpx-dvi.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ void dvi_scan_specials (int page_no,
7575
double *width, double *height,
7676
double *x_offset, double *y_offset, int *landscape,
7777
int *majorversion, int *minorversion,
78+
int *compression_level,
79+
double *annot_grow_x, double *annot_grow_y,
7880
int *do_enc, int *keybits, int32_t *perm,
79-
char *opasswd, char *upasswd, int *has_id, unsigned char *id1, unsigned char *id2);
81+
char *opasswd, char *upasswd, int *has_id, unsigned char *id1, unsigned char *id2,
82+
int *opt_flags);
8083
unsigned int dvi_locate_font (const char *name, spt_t ptsize);
8184

8285
/* link or nolink:

crates/pdf_io/pdf_io/dpx-spc_pdfm.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,7 @@ spc_handler_pdfm_stream_with_type (struct spc_env *spe, struct spc_arg *args, in
16191619
{
16201620
pdf_obj *fstream;
16211621
ssize_t nb_read;
1622-
char *ident, *instring, *fullname;
1622+
char *ident, *instring;
16231623
pdf_obj *tmp;
16241624
rust_input_handle_t handle = NULL;
16251625

@@ -1655,27 +1655,18 @@ spc_handler_pdfm_stream_with_type (struct spc_env *spe, struct spc_arg *args, in
16551655
free(ident);
16561656
return -1;
16571657
}
1658-
fullname = NULL; /*kpse_find_pict(instring);*/
1659-
if (!fullname) {
1660-
spc_warn(spe, "File \"%s\" not found.", instring);
1661-
pdf_release_obj(tmp);
1662-
free(ident);
1663-
return -1;
1664-
}
1665-
handle = ttstub_input_open(fullname, TTBC_FILE_FORMAT_PICT, 0);
1658+
handle = ttstub_input_open(instring, TTBC_FILE_FORMAT_PICT, 0);
16661659
if (handle == NULL) {
16671660
spc_warn(spe, "Could not open file: %s", instring);
16681661
pdf_release_obj(tmp);
16691662
free(ident);
1670-
free(fullname);
16711663
return -1;
16721664
}
16731665
fstream = pdf_new_stream(STREAM_COMPRESS);
16741666
while ((nb_read =
16751667
ttstub_input_read(handle, work_buffer, WORK_BUFFER_SIZE)) > 0)
16761668
pdf_add_stream(fstream, work_buffer, nb_read);
16771669
ttstub_input_close(handle);
1678-
free(fullname);
16791670
break;
16801671
case STRING_STREAM:
16811672
fstream = pdf_new_stream(STREAM_COMPRESS);

0 commit comments

Comments
 (0)