Skip to content

Commit b0d9501

Browse files
committed
stacks
1 parent d8460f5 commit b0d9501

18 files changed

+452
-552
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dpx/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ license = "GPL"
88

99
[dependencies]
1010
tectonic_bridge = { version = "0.0.1-dev", path = "../bridge" }
11+
lazy_static = "1.4"
1112
libc = "0.2"
1213
libpng-sys = "1"
1314
libz-sys = { version = "1", optional = true}

dpx/src/dpx_dvi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ unsafe extern "C" fn do_glyphs(mut do_actual_text: i32) {
27132713
if (*font).rgba_color != 0xffffffffu32 {
27142714
let mut color: pdf_color = pdf_color {
27152715
num_components: 0,
2716-
spot_color_name: 0 as *mut i8,
2716+
spot_color_name: None,
27172717
values: [0.; 4],
27182718
};
27192719
pdf_color_rgbcolor(

dpx/src/dpx_epdf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub type __off64_t = i64;
6868
pub type size_t = u64;
6969
pub type rust_input_handle_t = *mut libc::c_void;
7070

71-
use super::dpx_pdfdev::{pdf_coord, pdf_rect, pdf_tmatrix};
71+
use super::dpx_pdfdev::{pdf_coord, pdf_tmatrix};
7272

7373
use crate::dpx_pdfximage::{load_options, pdf_ximage, xform_info};
7474
pub const OP_CURVETO2: C2RustUnnamed_0 = 15;

dpx/src/dpx_mpost.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3416,7 +3416,7 @@ unsafe extern "C" fn do_operator(mut token: *const i8, mut x_user: f64, mut y_us
34163416
let mut cp = pdf_coord::new();
34173417
let mut color: pdf_color = pdf_color {
34183418
num_components: 0,
3419-
spot_color_name: 0 as *mut i8,
3419+
spot_color_name: None,
34203420
values: [0.; 4],
34213421
};
34223422
opcode = get_opcode(token);

dpx/src/dpx_pdfcolor.rs

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,25 @@ extern "C" {
7676
fn MD5_final(outbuf: *mut u8, ctx: *mut MD5_CONTEXT);
7777
}
7878
pub type size_t = u64;
79-
#[derive(Copy, Clone)]
79+
80+
use std::ffi::{CStr, CString};
81+
82+
#[derive(Clone)]
8083
#[repr(C)]
8184
pub struct pdf_color {
8285
pub num_components: i32,
83-
pub spot_color_name: *mut i8,
86+
pub spot_color_name: Option<CString>,
8487
pub values: [f64; 4],
8588
}
89+
impl pdf_color {
90+
pub const fn new() -> Self {
91+
Self {
92+
num_components: 0,
93+
spot_color_name: None,
94+
values: [0.; 4],
95+
}
96+
}
97+
}
8698
#[derive(Copy, Clone)]
8799
#[repr(C)]
88100
pub struct pdf_colorspace {
@@ -160,9 +172,9 @@ pub struct C2RustUnnamed_1 {
160172
pub major: i32,
161173
pub minor: i32,
162174
}
163-
#[derive(Copy, Clone)]
175+
#[derive(Clone)]
164176
#[repr(C)]
165-
pub struct C2RustUnnamed_2 {
177+
pub struct ColorStack {
166178
pub current: i32,
167179
pub stroke: [pdf_color; 128],
168180
pub fill: [pdf_color; 128],
@@ -225,7 +237,7 @@ pub unsafe extern "C" fn pdf_color_rgbcolor(color: &mut pdf_color, r: f64, g: f6
225237
color.values[1] = g;
226238
color.values[2] = b;
227239
color.num_components = 3i32;
228-
color.spot_color_name = 0 as *mut i8;
240+
color.spot_color_name = None;
229241
0i32
230242
}
231243
#[no_mangle]
@@ -257,20 +269,32 @@ pub unsafe extern "C" fn pdf_color_cmykcolor(
257269
color.values[2] = y;
258270
color.values[3] = k;
259271
color.num_components = 4i32;
260-
color.spot_color_name = 0 as *mut i8;
272+
color.spot_color_name = None;
261273
0i32
262274
}
263275
#[no_mangle]
264-
pub unsafe extern "C" fn pdf_color_graycolor(color: &mut pdf_color, mut g: f64) -> i32 {
276+
pub unsafe extern "C" fn pdf_color_graycolor(color: &mut pdf_color, g: f64) -> i32 {
265277
if g < 0.0f64 || g > 1.0f64 {
266278
warn!("Invalid color value specified: gray={}", g);
267279
return -1i32;
268280
}
269281
color.values[0] = g;
270282
color.num_components = 1i32;
271-
color.spot_color_name = 0 as *mut i8;
283+
color.spot_color_name = None;
272284
0i32
273285
}
286+
pub fn pdf_color_graycolor_new(g: f64) -> Result<pdf_color, i32> {
287+
if g < 0. || g > 1. {
288+
warn!("Invalid color value specified: gray={}", g);
289+
return Err(-1);
290+
}
291+
Ok(pdf_color {
292+
values: [g, 0., 0., 0.],
293+
num_components: 1,
294+
spot_color_name: None,
295+
})
296+
}
297+
274298
#[no_mangle]
275299
pub unsafe extern "C" fn pdf_color_spotcolor(
276300
color: &mut pdf_color,
@@ -284,20 +308,12 @@ pub unsafe extern "C" fn pdf_color_spotcolor(
284308
color.values[0] = c;
285309
color.values[1] = 0.0f64;
286310
color.num_components = 2i32;
287-
color.spot_color_name = name;
311+
color.spot_color_name = Some(CStr::from_ptr(name).to_owned());
288312
0i32
289313
}
290314
#[no_mangle]
291-
pub unsafe extern "C" fn pdf_color_copycolor(
292-
mut color1: *mut pdf_color,
293-
mut color2: *const pdf_color,
294-
) {
295-
assert!(!color1.is_null() && !color2.is_null());
296-
memcpy(
297-
color1 as *mut libc::c_void,
298-
color2 as *const libc::c_void,
299-
::std::mem::size_of::<pdf_color>() as u64,
300-
);
315+
pub unsafe extern "C" fn pdf_color_copycolor(color1: &mut pdf_color, color2: &pdf_color) {
316+
*color1 = color2.clone();
301317
}
302318
/* Brighten up a color. f == 0 means no change, f == 1 means white. */
303319
#[no_mangle]
@@ -367,7 +383,7 @@ pub unsafe extern "C" fn pdf_color_to_string(
367383
len = sprintf(
368384
buffer,
369385
b" /%s %c%c %g %c%c\x00" as *const u8 as *const i8,
370-
color.spot_color_name,
386+
color.spot_color_name.as_ref().unwrap().as_ptr(),
371387
'C' as i32 | mask as i32,
372388
'S' as i32 | mask as i32,
373389
(color.values[0] / 0.001f64 + 0.5f64).floor() * 0.001f64,
@@ -438,8 +454,11 @@ pub unsafe extern "C" fn pdf_color_compare(color1: &pdf_color, color2: &pdf_colo
438454
return -1i32;
439455
}
440456
}
441-
if !color1.spot_color_name.is_null() && !color2.spot_color_name.is_null() {
442-
return strcmp(color1.spot_color_name, color2.spot_color_name);
457+
if color1.spot_color_name.is_some() && color2.spot_color_name.is_some() {
458+
return strcmp(
459+
color1.spot_color_name.as_ref().unwrap().as_ptr(),
460+
color2.spot_color_name.as_ref().unwrap().as_ptr(),
461+
);
443462
}
444463
0i32
445464
}
@@ -490,28 +509,23 @@ pub unsafe extern "C" fn pdf_color_is_valid(color: &pdf_color) -> bool {
490509
}
491510
}
492511
if pdf_color_type(color) == -2i32 {
493-
if color.spot_color_name.is_null()
494-
|| *color.spot_color_name.offset(0) as i32 == '\u{0}' as i32
512+
if color.spot_color_name.is_none()
513+
|| *color.spot_color_name.as_ref().unwrap().as_ptr().offset(0) as i32 == '\u{0}' as i32
495514
{
496515
warn!("Invalid spot color: empty name");
497516
return false;
498517
}
499518
}
500519
true
501520
}
502-
static mut color_stack: C2RustUnnamed_2 = C2RustUnnamed_2 {
521+
/*static mut color_stack: ColorStack = ColorStack {
503522
current: 0,
504-
stroke: [pdf_color {
505-
num_components: 0,
506-
spot_color_name: 0 as *const i8 as *mut i8,
507-
values: [0.; 4],
508-
}; 128],
509-
fill: [pdf_color {
510-
num_components: 0,
511-
spot_color_name: 0 as *const i8 as *mut i8,
512-
values: [0.; 4],
513-
}; 128],
514-
};
523+
stroke: unsafe { core::mem::zeroed() },//[pdf_color::new(); 128],
524+
fill: unsafe { core::mem::zeroed() },//[pdf_color::new(); 128],
525+
};*/
526+
static mut color_stack: ColorStack =
527+
unsafe { std::mem::transmute([0u8; std::mem::size_of::<ColorStack>()]) };
528+
515529
#[no_mangle]
516530
pub unsafe extern "C" fn pdf_color_clear_stack() {
517531
if color_stack.current > 0 {
@@ -523,8 +537,8 @@ pub unsafe extern "C" fn pdf_color_clear_stack() {
523537
if !(fresh4 != 0) {
524538
break;
525539
}
526-
free(color_stack.stroke[color_stack.current as usize].spot_color_name as *mut libc::c_void);
527-
free(color_stack.fill[color_stack.current as usize].spot_color_name as *mut libc::c_void);
540+
// free(color_stack.stroke[color_stack.current as usize].spot_color_name as *mut libc::c_void);
541+
// free(color_stack.fill[color_stack.current as usize].spot_color_name as *mut libc::c_void);
528542
}
529543
color_stack.current = 0;
530544
pdf_color_graycolor(&mut color_stack.stroke[0], 0.0f64);

dpx/src/dpx_pdfdev.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ impl pdf_tmatrix {
236236
f: 0.,
237237
}
238238
}
239+
pub const fn identity() -> Self {
240+
Self {
241+
a: 1.,
242+
b: 0.,
243+
c: 0.,
244+
d: 1.,
245+
e: 0.,
246+
f: 0.,
247+
}
248+
}
239249
}
240250
#[derive(Copy, Clone, Default)]
241251
#[repr(C)]

dpx/src/dpx_pdfdoc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,8 +3181,8 @@ unsafe extern "C" fn pdf_doc_finish_page(mut p: *mut pdf_doc) {
31813181
static mut bgcolor: pdf_color = {
31823182
let mut init = pdf_color {
31833183
num_components: 1i32,
3184-
spot_color_name: 0 as *const i8 as *mut i8,
3185-
values: [1.0f64, 0., 0., 0.],
3184+
spot_color_name: None,
3185+
values: [1., 0., 0., 0.],
31863186
};
31873187
init
31883188
};

0 commit comments

Comments
 (0)