From 6cce45681c1780a8e289fe1307c5188fb96eb568 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Fri, 13 Sep 2019 22:46:38 +0300 Subject: [PATCH 1/7] pdf_path --- dpx/src/dpx_epdf.rs | 8 +- dpx/src/dpx_mpost.rs | 4 +- dpx/src/dpx_pdfdev.rs | 16 +- dpx/src/dpx_pdfdraw.rs | 547 +++++++++++++++++---------------------- dpx/src/dpx_pdfximage.rs | 8 +- dpx/src/dpx_spc_dvips.rs | 2 +- dpx/src/dpx_spc_html.rs | 2 +- dpx/src/dpx_spc_misc.rs | 2 +- dpx/src/dpx_spc_pdfm.rs | 4 +- dpx/src/dpx_spc_tpic.rs | 14 +- dpx/src/dpx_spc_xtx.rs | 2 +- dpx/src/dpx_specials.rs | 6 +- engine/src/xetex_pic.rs | 8 +- 13 files changed, 283 insertions(+), 340 deletions(-) diff --git a/dpx/src/dpx_epdf.rs b/dpx/src/dpx_epdf.rs index 63d75313c..d1579792b 100644 --- a/dpx/src/dpx_epdf.rs +++ b/dpx/src/dpx_epdf.rs @@ -1076,10 +1076,10 @@ pub unsafe extern "C" fn pdf_copy_clip( } else { let mut j: u32 = 0; let mut T = pdf_tmatrix::new(); - let mut p0: pdf_coord = pdf_coord::new(); - let mut p1: pdf_coord = pdf_coord::new(); - let mut p2: pdf_coord = pdf_coord::new(); - let mut p3: pdf_coord = pdf_coord::new(); + let mut p0 = pdf_coord::new(); + let mut p1 = pdf_coord::new(); + let mut p2 = pdf_coord::new(); + let mut p3 = pdf_coord::new(); token = parse_ident(&mut clip_path, end_path); j = 0_u32; while (j as u64) diff --git a/dpx/src/dpx_mpost.rs b/dpx/src/dpx_mpost.rs index c09690b1a..906ff48e7 100644 --- a/dpx/src/dpx_mpost.rs +++ b/dpx/src/dpx_mpost.rs @@ -3081,7 +3081,7 @@ unsafe extern "C" fn do_currentfont() -> i32 { } unsafe extern "C" fn do_show() -> i32 { let mut font: *mut mp_font = 0 as *mut mp_font; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut text_str: *mut pdf_obj = 0 as *mut pdf_obj; let mut length: i32 = 0; let mut strptr: *mut u8 = 0 as *mut u8; @@ -3256,7 +3256,7 @@ unsafe extern "C" fn do_operator(mut token: *const i8, mut x_user: f64, mut y_us let mut values: [f64; 12] = [0.; 12]; let mut tmp: *mut pdf_obj = 0 as *mut pdf_obj; let mut matrix = pdf_tmatrix::new(); - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut color: pdf_color = pdf_color { num_components: 0, spot_color_name: 0 as *mut i8, diff --git a/dpx/src/dpx_pdfdev.rs b/dpx/src/dpx_pdfdev.rs index 0748560af..4c972f2b6 100644 --- a/dpx/src/dpx_pdfdev.rs +++ b/dpx/src/dpx_pdfdev.rs @@ -151,7 +151,7 @@ impl pdf_rect { } } } -#[derive(Copy, Clone, Default, PartialEq)] +#[derive(Copy, Clone, Default)] #[repr(C)] pub struct pdf_coord { pub x: f64, @@ -162,6 +162,12 @@ impl pdf_coord { Self { x: 0., y: 0. } } } +impl PartialEq for pdf_coord { + fn eq(&self, other: &Self) -> bool { + (self.x - other.x).abs() < 1e-7 && (self.y - other.y).abs() < 1e-7 + } +} + #[derive(Copy, Clone, Default)] #[repr(C)] pub struct transform_info { @@ -1959,10 +1965,10 @@ pub unsafe extern "C" fn pdf_dev_set_rect( ) { let mut dev_x: f64 = 0.; /* currentmatrix */ let mut dev_y: f64 = 0.; /* 0 for B&W */ - let mut p0: pdf_coord = pdf_coord::new(); - let mut p1: pdf_coord = pdf_coord::new(); - let mut p2: pdf_coord = pdf_coord::new(); - let mut p3: pdf_coord = pdf_coord::new(); + let mut p0 = pdf_coord::new(); + let mut p1 = pdf_coord::new(); + let mut p2 = pdf_coord::new(); + let mut p3 = pdf_coord::new(); let mut min_x: f64 = 0.; let mut min_y: f64 = 0.; let mut max_x: f64 = 0.; diff --git a/dpx/src/dpx_pdfdraw.rs b/dpx/src/dpx_pdfdraw.rs index 54af7905a..4cbe317a4 100644 --- a/dpx/src/dpx_pdfdraw.rs +++ b/dpx/src/dpx_pdfdraw.rs @@ -64,10 +64,9 @@ pub use super::dpx_pdfcolor::pdf_color; use super::dpx_pdfdev::{pdf_coord, pdf_rect, pdf_tmatrix}; /* Graphics State */ -pub type pdf_gstate = pdf_gstate_; #[derive(Copy, Clone)] #[repr(C)] -pub struct pdf_gstate_ { +pub struct pdf_gstate { pub cp: pdf_coord, pub matrix: pdf_tmatrix, pub strokecolor: pdf_color, @@ -82,10 +81,9 @@ pub struct pdf_gstate_ { pub flags: i32, pub pt_fixee: pdf_coord, } -pub type pdf_path = pdf_path_; #[derive(Copy, Clone)] #[repr(C)] -pub struct pdf_path_ { +pub struct pdf_path { pub num_paths: u32, pub max_paths: u32, pub path: *mut pa_elem, @@ -101,11 +99,10 @@ pub struct pdf_path_ { /* bookkeeping the origin of the last transform applied */ /* _PDF_DRAW_H_ */ } -pub type pa_elem = pa_elem_; #[derive(Copy, Clone)] #[repr(C)] -pub struct pa_elem_ { - pub type_0: i32, +pub struct pa_elem { + pub typ: PeType, pub p: [pdf_coord; 3], } #[derive(Copy, Clone)] @@ -150,12 +147,7 @@ unsafe extern "C" fn inversematrix(mut W: &mut pdf_tmatrix, mut M: &pdf_tmatrix) W.f = M.b * M.e - M.a * M.f; 0i32 } -extern "C" fn pdf_coord__equal(p1: &pdf_coord, p2: &pdf_coord) -> i32 { - if (p1.x - p2.x).abs() < 1.0e-7f64 && (p1.y - p2.y).abs() < 1.0e-7f64 { - return 1i32; - } - 0i32 -} + extern "C" fn pdf_coord__transform(p: &mut pdf_coord, M: &pdf_tmatrix) -> i32 { let pdf_coord { x, y } = *p; p.x = x * M.a + y * M.c + M.e; @@ -214,146 +206,111 @@ pub unsafe extern "C" fn pdf_invertmatrix(M: &mut pdf_tmatrix) { M.e = W.e; M.f = W.f; } -static mut petypes: [C2RustUnnamed_0; 7] = [ - { - let mut init = C2RustUnnamed_0 { - opchr: 'm' as i32 as i8, - n_pts: 1i32, - strkey: b"moveto\x00" as *const u8 as *const i8, - }; - init - }, - { - let mut init = C2RustUnnamed_0 { - opchr: 'l' as i32 as i8, - n_pts: 1i32, - strkey: b"lineto\x00" as *const u8 as *const i8, - }; - init - }, - { - let mut init = C2RustUnnamed_0 { - opchr: 'c' as i32 as i8, - n_pts: 3i32, - strkey: b"curveto\x00" as *const u8 as *const i8, - }; - init - }, - { - let mut init = C2RustUnnamed_0 { - opchr: 'v' as i32 as i8, - n_pts: 2i32, - strkey: b"vcurveto\x00" as *const u8 as *const i8, - }; - init - }, - { - let mut init = C2RustUnnamed_0 { - opchr: 'y' as i32 as i8, - n_pts: 2i32, - strkey: b"ycurveto\x00" as *const u8 as *const i8, - }; - init - }, - { - let mut init = C2RustUnnamed_0 { - opchr: 'h' as i32 as i8, - n_pts: 0i32, - strkey: b"closepath\x00" as *const u8 as *const i8, - }; - init - }, - { - let mut init = C2RustUnnamed_0 { - opchr: ' ' as i32 as i8, - n_pts: 0i32, - strkey: 0 as *const i8, - }; - init - }, -]; + +#[derive(Clone,Copy,Debug,PartialEq)] +pub enum PeType { + MOVETO = 0, + LINETO = 1, + CURVETO = 2, + CURVETO_V = 3, + CURVETO_Y = 4, + CLOSEPATH = 5, + TERMINATE = 6, +} + +impl PeType { + pub fn opchr(&self) -> i8 { + use PeType::*; + (match *self { + MOVETO => b'm', + LINETO => b'l', + CURVETO => b'c', + CURVETO_V => b'v', + CURVETO_Y => b'y', + CLOSEPATH => b'h', + TERMINATE => b' ', + }) as i8 + } + pub fn n_pts(&self) -> usize { + use PeType::*; + match *self { + MOVETO => 1, + LINETO => 1, + CURVETO => 3, + CURVETO_V => 2, + CURVETO_Y => 2, + CLOSEPATH => 0, + TERMINATE => 0, + } + } +} + static mut fmt_buf: [i8; 1024] = [0; 1024]; -unsafe extern "C" fn init_a_path(mut p: *mut pdf_path) { - assert!(!p.is_null()); - (*p).num_paths = 0_u32; - (*p).max_paths = 0_u32; - (*p).path = 0 as *mut pa_elem; -} -unsafe extern "C" fn pdf_path__clearpath(mut p: *mut pdf_path) { - assert!(!p.is_null()); - (*p).num_paths = 0_u32; -} -unsafe extern "C" fn pdf_path__growpath(mut p: *mut pdf_path, mut max_pe: u32) -> i32 { - if max_pe < (*p).max_paths { +unsafe extern "C" fn init_a_path(p: &mut pdf_path) { + p.num_paths = 0_u32; + p.max_paths = 0_u32; + p.path = 0 as *mut pa_elem; +} +extern "C" fn pdf_path__clearpath(mut p: &mut pdf_path) { + p.num_paths = 0_u32; +} +unsafe extern "C" fn pdf_path__growpath(p: &mut pdf_path, mut max_pe: u32) -> i32 { + if max_pe < p.max_paths { return 0i32; } - (*p).max_paths = if (*p).max_paths.wrapping_add(8_u32) > max_pe { - (*p).max_paths.wrapping_add(8_u32) + p.max_paths = if p.max_paths + 8 > max_pe { + p.max_paths + 8_u32 } else { max_pe }; - (*p).path = renew( - (*p).path as *mut libc::c_void, - ((*p).max_paths as u64).wrapping_mul(::std::mem::size_of::() as u64) as u32, + p.path = renew( + p.path as *mut libc::c_void, + (p.max_paths as u64).wrapping_mul(::std::mem::size_of::() as u64) as u32, ) as *mut pa_elem; 0i32 } -unsafe extern "C" fn clear_a_path(mut p: *mut pdf_path) { - assert!(!p.is_null()); - (*p).path = mfree((*p).path as *mut libc::c_void) as *mut pa_elem; - (*p).num_paths = 0_u32; - (*p).max_paths = 0_u32; +unsafe extern "C" fn clear_a_path(p: &mut pdf_path) { + p.path = mfree(p.path as *mut libc::c_void) as *mut pa_elem; + p.num_paths = 0_u32; + p.max_paths = 0_u32; } -unsafe extern "C" fn pdf_path__copypath(mut p1: *mut pdf_path, mut p0: *const pdf_path) -> i32 { - let mut pe0: *mut pa_elem = 0 as *mut pa_elem; - let mut pe1: *mut pa_elem = 0 as *mut pa_elem; +unsafe extern "C" fn pdf_path__copypath(p1: &mut pdf_path, p0: &pdf_path) -> i32 { let mut i: u32 = 0; - pdf_path__growpath(p1, (*p0).num_paths); + pdf_path__growpath(p1, p0.num_paths); i = 0_u32; - while i < (*p0).num_paths { - pe1 = &mut *(*p1).path.offset(i as isize) as *mut pa_elem; - pe0 = &mut *(*p0).path.offset(i as isize) as *mut pa_elem; + while i < p0.num_paths { + let pe1 = &mut *(*p1).path.offset(i as isize); + let pe0 = &mut *p0.path.offset(i as isize); /* FIXME */ - (*pe1).type_0 = (*pe0).type_0; - (*pe1).p[0].x = (*pe0).p[0].x; - (*pe1).p[0].y = (*pe0).p[0].y; - (*pe1).p[1].x = (*pe0).p[1].x; - (*pe1).p[1].y = (*pe0).p[1].y; - (*pe1).p[2].x = (*pe0).p[2].x; - (*pe1).p[2].y = (*pe0).p[2].y; - i = i.wrapping_add(1) + *pe1 = *pe0; + i = i + 1 } - (*p1).num_paths = (*p0).num_paths; + p1.num_paths = p0.num_paths; 0i32 } /* start new subpath */ unsafe extern "C" fn pdf_path__moveto( - mut pa: *mut pdf_path, - mut cp: *mut pdf_coord, - mut p0: *const pdf_coord, + pa: &mut pdf_path, + cp: &mut pdf_coord, + p0: &pdf_coord, ) -> i32 { - let mut pe: *mut pa_elem = 0 as *mut pa_elem; - pdf_path__growpath(pa, (*pa).num_paths.wrapping_add(1_u32)); - if (*pa).num_paths > 0_u32 { - pe = &mut *(*pa) + pdf_path__growpath(pa, pa.num_paths + 1); + if pa.num_paths > 0_u32 { + let pe = &mut *(*pa) .path - .offset((*pa).num_paths.wrapping_sub(1_u32) as isize) as *mut pa_elem; - if (*pe).type_0 == 0i32 { - (*cp).x = (*p0).x; - (*pe).p[0].x = (*cp).x; - (*cp).y = (*p0).y; - (*pe).p[0].y = (*cp).y; + .offset((pa.num_paths - 1) as isize); + if pe.typ == PeType::MOVETO { + *cp = *p0; + pe.p[0] = *cp; return 0i32; } } - let fresh0 = (*pa).num_paths; - (*pa).num_paths = (*pa).num_paths.wrapping_add(1); - pe = &mut *(*pa).path.offset(fresh0 as isize) as *mut pa_elem; - (*pe).type_0 = 0i32; - (*cp).x = (*p0).x; - (*pe).p[0].x = (*cp).x; - (*cp).y = (*p0).y; - (*pe).p[0].y = (*cp).y; + let fresh0 = pa.num_paths; + pa.num_paths += 1; + let pe = &mut *pa.path.offset(fresh0 as isize); + pe.typ = PeType::MOVETO; + *cp = *p0; + pe.p[0] = *cp; 0i32 } /* Do 'compression' of path while adding new path elements. @@ -364,153 +321,140 @@ unsafe extern "C" fn pdf_path__moveto( * 'moveto' must be used to enforce starting new path. * This affects how 'closepath' is treated. */ -unsafe extern "C" fn pdf_path__next_pe(mut pa: *mut pdf_path, cp: &pdf_coord) -> *mut pa_elem { +unsafe extern "C" fn pdf_path__next_pe<'a>(pa: &'a mut pdf_path, cp: &pdf_coord) -> &'a mut pa_elem { let mut pe: *mut pa_elem = 0 as *mut pa_elem; - pdf_path__growpath(pa, (*pa).num_paths.wrapping_add(2_u32)); - if (*pa).num_paths == 0_u32 { - let fresh1 = (*pa).num_paths; - (*pa).num_paths = (*pa).num_paths.wrapping_add(1); - pe = &mut *(*pa).path.offset(fresh1 as isize) as *mut pa_elem; - (*pe).type_0 = 0i32; - (*pe).p[0].x = cp.x; - (*pe).p[0].y = cp.y; - let fresh2 = (*pa).num_paths; - (*pa).num_paths = (*pa).num_paths.wrapping_add(1); - return &mut *(*pa).path.offset(fresh2 as isize) as *mut pa_elem; + pdf_path__growpath(pa, pa.num_paths + 2); + if pa.num_paths == 0_u32 { + let fresh1 = pa.num_paths; + pa.num_paths += 1; + let pe = &mut *pa.path.offset(fresh1 as isize); + pe.typ = PeType::MOVETO; + pe.p[0].x = cp.x; + pe.p[0].y = cp.y; + let fresh2 = pa.num_paths; + pa.num_paths += 1; + return &mut *pa.path.offset(fresh2 as isize); } - pe = &mut *(*pa) + let mut pe = &mut *(*pa) .path - .offset((*pa).num_paths.wrapping_sub(1_u32) as isize) as *mut pa_elem; - match (*pe).type_0 { - 0 => { - (*pe).p[0].x = cp.x; - (*pe).p[0].y = cp.y + .offset((pa.num_paths - 1) as isize); + match pe.typ { + PeType::MOVETO => { + pe.p[0].x = cp.x; + pe.p[0].y = cp.y } - 1 => { - if pdf_coord__equal(&mut *(*pe).p.as_mut_ptr().offset(0), cp) == 0 { - let fresh3 = (*pa).num_paths; - (*pa).num_paths = (*pa).num_paths.wrapping_add(1); - pe = &mut *(*pa).path.offset(fresh3 as isize) as *mut pa_elem; - (*pe).type_0 = 0i32; - (*pe).p[0].x = cp.x; - (*pe).p[0].y = cp.y + PeType::LINETO => { + if &mut *pe.p.as_mut_ptr().offset(0) != cp { + let fresh3 = pa.num_paths; + pa.num_paths += 1; + pe = &mut *pa.path.offset(fresh3 as isize); + pe.typ = PeType::MOVETO; + pe.p[0].x = cp.x; + pe.p[0].y = cp.y } } - 2 => { - if pdf_coord__equal(&mut *(*pe).p.as_mut_ptr().offset(2), cp) == 0 { - let fresh4 = (*pa).num_paths; - (*pa).num_paths = (*pa).num_paths.wrapping_add(1); - pe = &mut *(*pa).path.offset(fresh4 as isize) as *mut pa_elem; - (*pe).type_0 = 0i32; - (*pe).p[0].x = cp.x; - (*pe).p[0].y = cp.y + PeType::CURVETO => { + if &mut *pe.p.as_mut_ptr().offset(2) != cp { + let fresh4 = pa.num_paths; + pa.num_paths += 1; + pe = &mut *pa.path.offset(fresh4 as isize); + pe.typ = PeType::MOVETO; + pe.p[0].x = cp.x; + pe.p[0].y = cp.y } } - 4 | 3 => { - if pdf_coord__equal(&mut *(*pe).p.as_mut_ptr().offset(1), cp) == 0 { - let fresh5 = (*pa).num_paths; - (*pa).num_paths = (*pa).num_paths.wrapping_add(1); - pe = &mut *(*pa).path.offset(fresh5 as isize) as *mut pa_elem; - (*pe).type_0 = 0i32; - (*pe).p[0].x = cp.x; - (*pe).p[0].y = cp.y + PeType::CURVETO_Y | PeType::CURVETO_V => { + if &mut *pe.p.as_mut_ptr().offset(1) != cp { + let fresh5 = pa.num_paths; + pa.num_paths += 1; + pe = &mut *pa.path.offset(fresh5 as isize); + pe.typ = PeType::MOVETO; + pe.p[0].x = cp.x; + pe.p[0].y = cp.y } } - 5 => { - let fresh6 = (*pa).num_paths; - (*pa).num_paths = (*pa).num_paths.wrapping_add(1); - pe = &mut *(*pa).path.offset(fresh6 as isize) as *mut pa_elem; - (*pe).type_0 = 0i32; - (*pe).p[0].x = cp.x; - (*pe).p[0].y = cp.y + PeType::CLOSEPATH => { + let fresh6 = pa.num_paths; + pa.num_paths += 1; + pe = &mut *pa.path.offset(fresh6 as isize); + pe.typ = PeType::MOVETO; + pe.p[0].x = cp.x; + pe.p[0].y = cp.y } _ => {} } - let fresh7 = (*pa).num_paths; - (*pa).num_paths = (*pa).num_paths.wrapping_add(1); - &mut *(*pa).path.offset(fresh7 as isize) as *mut pa_elem + let fresh7 = pa.num_paths; + pa.num_paths += 1; + &mut *pa.path.offset(fresh7 as isize) } -unsafe extern "C" fn pdf_path__transform(mut pa: *mut pdf_path, M: &pdf_tmatrix) -> i32 { +unsafe extern "C" fn pdf_path__transform(pa: &mut pdf_path, M: &pdf_tmatrix) -> i32 { let mut pe: *mut pa_elem = 0 as *mut pa_elem; let mut n: u32 = 0_u32; let mut i: u32 = 0; - assert!(!pa.is_null()); i = 0_u32; - while i < (*pa).num_paths { - pe = &mut *(*pa).path.offset(i as isize) as *mut pa_elem; - n = (if !pe.is_null() && (*pe).type_0 > -1i32 && (*pe).type_0 < 6i32 { - petypes[(*pe).type_0 as usize].n_pts + while i < pa.num_paths { + let pe = &mut *pa.path.offset(i as isize); + n = (if pe.typ != PeType::TERMINATE { + (*pe).typ.n_pts() as i32 } else { 0i32 }) as u32; loop { let fresh8 = n; - n = n.wrapping_sub(1); + n += 1; if !(fresh8 > 0_u32) { break; } pdf_coord__transform(&mut *(*pe).p.as_mut_ptr().offset(n as isize), M); } - i = i.wrapping_add(1) + i += 1 } 0i32 } /* Path Construction */ unsafe extern "C" fn pdf_path__lineto( - mut pa: *mut pdf_path, + pa: &mut pdf_path, cp: &mut pdf_coord, p0: &pdf_coord, ) -> i32 { - let mut pe: *mut pa_elem = 0 as *mut pa_elem; - pe = pdf_path__next_pe(pa, cp); - (*pe).type_0 = 1i32; + let pe = pdf_path__next_pe(pa, cp); + pe.typ = PeType::LINETO; cp.x = p0.x; - (*pe).p[0].x = cp.x; + pe.p[0].x = cp.x; cp.y = p0.y; - (*pe).p[0].y = cp.y; + pe.p[0].y = cp.y; 0i32 } unsafe extern "C" fn pdf_path__curveto( - mut pa: *mut pdf_path, + pa: &mut pdf_path, cp: &mut pdf_coord, p0: &pdf_coord, p1: &pdf_coord, p2: &pdf_coord, ) -> i32 { - let mut pe: *mut pa_elem = 0 as *mut pa_elem; - pe = pdf_path__next_pe(pa, cp); - if pdf_coord__equal(cp, p0) != 0 { - (*pe).type_0 = 3i32; - (*pe).p[0].x = p1.x; - (*pe).p[0].y = p1.y; - cp.x = p2.x; - (*pe).p[1].x = cp.x; - cp.y = p2.y; - (*pe).p[1].y = cp.y - } else if pdf_coord__equal(p1, p2) != 0 { - (*pe).type_0 = 4i32; - (*pe).p[0].x = p0.x; - (*pe).p[0].y = p0.y; - cp.x = p1.x; - (*pe).p[1].x = cp.x; - cp.y = p1.y; - (*pe).p[1].y = cp.y + let pe = pdf_path__next_pe(pa, cp); + if cp == p0 { + pe.typ = PeType::CURVETO_V; + pe.p[0] = *p1; + *cp = *p2; + pe.p[1] = *cp; + } else if p1 == p2 { + pe.typ = PeType::CURVETO_Y; + pe.p[0] = *p0; + *cp = *p1; + pe.p[1] = *cp; } else { - (*pe).type_0 = 2i32; - (*pe).p[0].x = p0.x; - (*pe).p[0].y = p0.y; - (*pe).p[1].x = p1.x; - (*pe).p[1].y = p1.y; - cp.x = p2.x; - (*pe).p[2].x = cp.x; - cp.y = p2.y; - (*pe).p[2].y = cp.y + pe.typ = PeType::CURVETO; + pe.p[0] = *p0; + pe.p[1] = *p1; + *cp = *p2; + pe.p[2] = *cp; } 0i32 } /* This isn't specified as cp to somewhere. */ unsafe extern "C" fn pdf_path__elliptarc( - mut pa: *mut pdf_path, + pa: &mut pdf_path, cp: &mut pdf_coord, ca: &pdf_coord, mut r_x: f64, @@ -526,12 +470,12 @@ unsafe extern "C" fn pdf_path__elliptarc( let mut b_y: f64 = 0.; let mut d_a: f64 = 0.; let mut q: f64 = 0.; - let mut p0: pdf_coord = pdf_coord::new(); - let mut p1: pdf_coord = pdf_coord::new(); - let mut p2: pdf_coord = pdf_coord::new(); - let mut p3: pdf_coord = pdf_coord::new(); - let mut e0: pdf_coord = pdf_coord::new(); - let mut e1: pdf_coord = pdf_coord::new(); + let mut p0 = pdf_coord::new(); + let mut p1 = pdf_coord::new(); + let mut p2 = pdf_coord::new(); + let mut p3 = pdf_coord::new(); + let mut e0 = pdf_coord::new(); + let mut e1 = pdf_coord::new(); let mut T = pdf_tmatrix::new(); let mut n_c: i32 = 0; let mut i: i32 = 0; @@ -578,9 +522,9 @@ unsafe extern "C" fn pdf_path__elliptarc( pdf_coord__transform(&mut p0, &mut T); p0.x += ca.x; p0.y += ca.y; - if (*pa).num_paths == 0_u32 { + if pa.num_paths == 0_u32 { pdf_path__moveto(pa, cp, &mut p0); - } else if pdf_coord__equal(cp, &p0) == 0 { + } else if cp != &p0 { pdf_path__lineto(pa, cp, &mut p0); /* add line seg */ } @@ -625,15 +569,15 @@ unsafe extern "C" fn pdf_path__elliptarc( } error } -unsafe extern "C" fn pdf_path__closepath(mut pa: *mut pdf_path, cp: &mut pdf_coord) -> i32 +unsafe extern "C" fn pdf_path__closepath(pa: &mut pdf_path, cp: &mut pdf_coord) -> i32 /* no arg */ { let mut pe: *mut pa_elem = 0 as *mut pa_elem; let mut i: i32 = 0; /* search for start point of the last subpath */ - i = (*pa).num_paths.wrapping_sub(1_u32) as i32; /* No path or no start point(!) */ + i = (pa.num_paths - 1) as i32; /* No path or no start point(!) */ while i >= 0i32 { - pe = &mut *(*pa).path.offset(i as isize) as *mut pa_elem; - if (*pe).type_0 == 0i32 { + pe = &mut *pa.path.offset(i as isize) as *mut pa_elem; + if (*pe).typ == PeType::MOVETO { break; } i -= 1 @@ -642,17 +586,17 @@ unsafe extern "C" fn pdf_path__closepath(mut pa: *mut pdf_path, cp: &mut pdf_coo return -1i32; } *cp = (*pe).p[0].clone(); - pdf_path__growpath(pa, (*pa).num_paths.wrapping_add(1_u32)); + pdf_path__growpath(pa, pa.num_paths + 1); /* NOTE: * Manually closed path without closepath is not * affected by linejoin. A path with coincidental * starting and ending point is not the same as * 'closed' path. */ - let fresh9 = (*pa).num_paths; - (*pa).num_paths = (*pa).num_paths.wrapping_add(1); - pe = &mut *(*pa).path.offset(fresh9 as isize) as *mut pa_elem; - (*pe).type_0 = 5i32; + let fresh9 = pa.num_paths; + pa.num_paths += 1; + pe = &mut *pa.path.offset(fresh9 as isize) as *mut pa_elem; + (*pe).typ = PeType::CLOSEPATH; 0i32 } /* @@ -667,39 +611,34 @@ unsafe extern "C" fn pdf_path__closepath(mut pa: *mut pdf_path, cp: &mut pdf_coo * h */ /* Just for quick test */ -unsafe extern "C" fn pdf_path__isarect(mut pa: *mut pdf_path, mut f_ir: i32) -> i32 +unsafe extern "C" fn pdf_path__isarect(pa: &mut pdf_path, mut f_ir: i32) -> i32 /* fill-rule is ignorable */ { - let mut pe0: *mut pa_elem = 0 as *mut pa_elem; - let mut pe1: *mut pa_elem = 0 as *mut pa_elem; - let mut pe2: *mut pa_elem = 0 as *mut pa_elem; - let mut pe3: *mut pa_elem = 0 as *mut pa_elem; - let mut pe4: *mut pa_elem = 0 as *mut pa_elem; - if (*pa).num_paths == 5_u32 { - pe0 = &mut *(*pa).path.offset(0) as *mut pa_elem; - pe1 = &mut *(*pa).path.offset(1) as *mut pa_elem; - pe2 = &mut *(*pa).path.offset(2) as *mut pa_elem; - pe3 = &mut *(*pa).path.offset(3) as *mut pa_elem; - pe4 = &mut *(*pa).path.offset(4) as *mut pa_elem; - if (*pe0).type_0 == 0i32 - && (*pe1).type_0 == 1i32 - && (*pe2).type_0 == 1i32 - && (*pe3).type_0 == 1i32 - && (*pe4).type_0 == 5i32 + if pa.num_paths == 5_u32 { + let pe0 = &mut *pa.path.offset(0); + let pe1 = &mut *pa.path.offset(1); + let pe2 = &mut *pa.path.offset(2); + let pe3 = &mut *pa.path.offset(3); + let pe4 = &mut *pa.path.offset(4); + if pe0.typ == PeType::MOVETO + && pe1.typ == PeType::LINETO + && pe2.typ == PeType::LINETO + && pe3.typ == PeType::LINETO + && pe4.typ == PeType::CLOSEPATH { - if (*pe1).p[0].y - (*pe0).p[0].y == 0i32 as f64 - && (*pe2).p[0].x - (*pe1).p[0].x == 0i32 as f64 - && (*pe3).p[0].y - (*pe2).p[0].y == 0i32 as f64 + if pe1.p[0].y - pe0.p[0].y == 0. + && pe2.p[0].x - pe1.p[0].x == 0. + && pe3.p[0].y - pe2.p[0].y == 0. { - if (*pe1).p[0].x - (*pe0).p[0].x == (*pe2).p[0].x - (*pe3).p[0].x { + if pe1.p[0].x - pe0.p[0].x == pe2.p[0].x - pe3.p[0].x { return 1i32; } /* Winding number is different but ignore it here. */ } else if f_ir != 0 - && (*pe1).p[0].x - (*pe0).p[0].x == 0i32 as f64 - && (*pe2).p[0].y - (*pe1).p[0].y == 0i32 as f64 - && (*pe3).p[0].x - (*pe2).p[0].x == 0i32 as f64 + && pe1.p[0].x - pe0.p[0].x == 0. + && pe2.p[0].y - pe1.p[0].y == 0. + && pe3.p[0].x - pe2.p[0].x == 0. { - if (*pe1).p[0].y - (*pe0).p[0].y == (*pe2).p[0].y - (*pe3).p[0].y { + if pe1.p[0].y - pe0.p[0].y == pe2.p[0].y - pe3.p[0].y { return 1i32; } } @@ -737,7 +676,7 @@ unsafe extern "C" fn pdf_dev__rectshape( let mut buf: *mut i8 = fmt_buf.as_mut_ptr(); let mut len: i32 = 0i32; let mut isclip: i32 = 0i32; - let mut p: pdf_coord = pdf_coord::new(); + let mut p = pdf_coord::new(); let mut wd: f64 = 0.; let mut ht: f64 = 0.; assert!( @@ -837,7 +776,7 @@ unsafe extern "C" fn pdf_dev__rectshape( static mut path_added: i32 = 0i32; /* FIXME */ unsafe extern "C" fn pdf_dev__flushpath( - mut pa: *mut pdf_path, + pa: &mut pdf_path, mut opchr: i8, mut rule: i32, mut ignore_rule: i32, @@ -855,9 +794,7 @@ unsafe extern "C" fn pdf_dev__flushpath( let mut isrect: i32 = 0; let mut i: i32 = 0; let mut j: i32 = 0; - assert!( - !pa.is_null() - && (opchr as i32 == 'f' as i32 + assert!((opchr as i32 == 'f' as i32 || opchr as i32 == 'F' as i32 || opchr as i32 == 's' as i32 || opchr as i32 == 'S' as i32 @@ -871,15 +808,15 @@ unsafe extern "C" fn pdf_dev__flushpath( } else { 0i32 }; - if (*pa).num_paths <= 0_u32 && path_added == 0i32 { + if pa.num_paths <= 0_u32 && path_added == 0i32 { return 0i32; } path_added = 0i32; graphics_mode(); isrect = pdf_path__isarect(pa, ignore_rule); if isrect != 0 { - pe = &mut *(*pa).path.offset(0) as *mut pa_elem; - pe1 = &mut *(*pa).path.offset(2) as *mut pa_elem; + pe = &mut *pa.path.offset(0) as *mut pa_elem; + pe1 = &mut *pa.path.offset(2) as *mut pa_elem; r.llx = (*pe).p[0].x; r.lly = (*pe).p[0].y; r.urx = (*pe1).p[0].x - (*pe).p[0].x; @@ -900,13 +837,13 @@ unsafe extern "C" fn pdf_dev__flushpath( pdf_doc_add_page_content(b, len as u32); len = 0i32 } else { - n_seg = (*pa).num_paths as i32; + n_seg = pa.num_paths as i32; i = 0i32; len = 0i32; - pe = &mut *(*pa).path.offset(0) as *mut pa_elem; + pe = &mut *pa.path.offset(0) as *mut pa_elem; while i < n_seg { - n_pts = if !pe.is_null() && (*pe).type_0 > -1i32 && (*pe).type_0 < 6i32 { - petypes[(*pe).type_0 as usize].n_pts + n_pts = if !pe.is_null() && (*pe).typ != PeType::TERMINATE { + (*pe).typ.n_pts() as i32 } else { 0i32 }; @@ -926,11 +863,11 @@ unsafe extern "C" fn pdf_dev__flushpath( let fresh34 = len; len = len + 1; *b.offset(fresh34 as isize) = - (if !pe.is_null() && (*pe).type_0 > -1i32 && (*pe).type_0 < 6i32 { - petypes[(*pe).type_0 as usize].opchr as i32 + if !pe.is_null() && (*pe).typ != PeType::TERMINATE { + (*pe).typ.opchr() } else { - ' ' as i32 - }) as i8; + b' ' as i8 + }; if len + 128i32 > b_len { pdf_doc_add_page_content(b, len as u32); len = 0i32 @@ -1255,7 +1192,7 @@ pub unsafe extern "C" fn pdf_dev_set_color(color: &pdf_color, mut mask: i8, mut pub unsafe extern "C" fn pdf_dev_concat(M: &pdf_tmatrix) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let cpt = &mut (*gs).cp; let mut CTM = &mut (*gs).matrix; let mut W = pdf_tmatrix::new(); @@ -1422,21 +1359,21 @@ pub unsafe extern "C" fn pdf_dev_setdash( pub unsafe extern "C" fn pdf_dev_clip() -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; pdf_dev__flushpath(cpa, 'W' as i32 as i8, 0i32, 0i32) } #[no_mangle] pub unsafe extern "C" fn pdf_dev_eoclip() -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; pdf_dev__flushpath(cpa, 'W' as i32 as i8, 1i32, 0i32) } #[no_mangle] pub unsafe extern "C" fn pdf_dev_flushpath(mut p_op: i8, mut fill_rule: i32) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut error: i32 = 0i32; /* last arg 'ignore_rule' is only for single object * that can be converted to a rect where fill rule @@ -1451,8 +1388,8 @@ pub unsafe extern "C" fn pdf_dev_flushpath(mut p_op: i8, mut fill_rule: i32) -> pub unsafe extern "C" fn pdf_dev_newpath() -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut p: *mut pdf_path = &mut (*gs).path; - if (*p).num_paths > 0_u32 { + let mut p = &mut (*gs).path; + if p.num_paths > 0_u32 { pdf_path__clearpath(p); } /* The following is required for "newpath" operator in mpost.c. */ @@ -1463,9 +1400,9 @@ pub unsafe extern "C" fn pdf_dev_newpath() -> i32 { pub unsafe extern "C" fn pdf_dev_moveto(mut x: f64, mut y: f64) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; - let mut cpt: *mut pdf_coord = &mut (*gs).cp; - let mut p: pdf_coord = pdf_coord::new(); + let mut cpa = &mut (*gs).path; + let mut cpt = &mut (*gs).cp; + let mut p = pdf_coord::new(); p.x = x; p.y = y; return pdf_path__moveto(cpa, cpt, &mut p); @@ -1475,7 +1412,7 @@ pub unsafe extern "C" fn pdf_dev_moveto(mut x: f64, mut y: f64) -> i32 { pub unsafe extern "C" fn pdf_dev_rmoveto(mut x: f64, mut y: f64) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; let p = pdf_coord { x: cpt.x + x, @@ -1488,7 +1425,7 @@ pub unsafe extern "C" fn pdf_dev_rmoveto(mut x: f64, mut y: f64) -> i32 { pub unsafe extern "C" fn pdf_dev_lineto(mut x: f64, mut y: f64) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; let p0 = pdf_coord { x, y }; pdf_path__lineto(cpa, cpt, &p0) @@ -1497,7 +1434,7 @@ pub unsafe extern "C" fn pdf_dev_lineto(mut x: f64, mut y: f64) -> i32 { pub unsafe extern "C" fn pdf_dev_rlineto(mut x: f64, mut y: f64) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; let mut p0 = pdf_coord { x: x + cpt.x, @@ -1516,7 +1453,7 @@ pub unsafe extern "C" fn pdf_dev_curveto( ) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; let p0 = pdf_coord { x: x0, y: y0 }; let p1 = pdf_coord { x: x1, y: y1 }; @@ -1532,7 +1469,7 @@ pub unsafe extern "C" fn pdf_dev_vcurveto( ) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; let cpt_clone = cpt.clone(); let p0 = pdf_coord { x: x0, y: y0 }; @@ -1548,7 +1485,7 @@ pub unsafe extern "C" fn pdf_dev_ycurveto( ) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; let p0 = pdf_coord { x: x0, y: y0 }; let p1 = pdf_coord { x: x1, y: y1 }; @@ -1565,7 +1502,7 @@ pub unsafe extern "C" fn pdf_dev_rcurveto( ) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; let p0 = pdf_coord { x: x0 + cpt.x, @@ -1586,7 +1523,7 @@ pub unsafe extern "C" fn pdf_dev_closepath() -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; let mut cpt = &mut (*gs).cp; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; pdf_path__closepath(cpa, cpt) } #[no_mangle] @@ -1614,9 +1551,9 @@ pub unsafe extern "C" fn pdf_dev_transform(p: &mut pdf_coord, M: Option<&pdf_tma pub unsafe extern "C" fn pdf_dev_arc(c_x: f64, c_y: f64, r: f64, a_0: f64, a_1: f64) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; - let mut c: pdf_coord = pdf_coord { x: c_x, y: c_y }; + let mut c = pdf_coord { x: c_x, y: c_y }; pdf_path__elliptarc(cpa, cpt, &mut c, r, r, 0.0f64, a_0, a_1, 1i32) } /* *negative* arc */ @@ -1624,7 +1561,7 @@ pub unsafe extern "C" fn pdf_dev_arc(c_x: f64, c_y: f64, r: f64, a_0: f64, a_1: pub unsafe extern "C" fn pdf_dev_arcn(c_x: f64, c_y: f64, r: f64, a_0: f64, a_1: f64) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; let mut c = pdf_coord { x: c_x, y: c_y }; pdf_path__elliptarc(cpa, cpt, &mut c, r, r, 0.0f64, a_0, a_1, -1i32) @@ -1642,7 +1579,7 @@ pub unsafe extern "C" fn pdf_dev_arcx( ) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; let mut c = pdf_coord { x: c_x, y: c_y }; pdf_path__elliptarc(cpa, cpt, &mut c, r_x, r_y, xar, a_0, a_1, a_d) @@ -1659,7 +1596,7 @@ pub unsafe extern "C" fn pdf_dev_bspline( ) -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa: *mut pdf_path = &mut (*gs).path; + let mut cpa = &mut (*gs).path; let mut cpt = &mut (*gs).cp; let p1 = pdf_coord { x: x0 + 2. * (x1 - x0) / 3., diff --git a/dpx/src/dpx_pdfximage.rs b/dpx/src/dpx_pdfximage.rs index 8b3c9f7de..f16ec0f32 100644 --- a/dpx/src/dpx_pdfximage.rs +++ b/dpx/src/dpx_pdfximage.rs @@ -637,10 +637,10 @@ pub unsafe extern "C" fn pdf_ximage_set_form( mut resource: *mut pdf_obj, ) { let info = form_info; - let mut p1: pdf_coord = pdf_coord::new(); - let mut p2: pdf_coord = pdf_coord::new(); - let mut p3: pdf_coord = pdf_coord::new(); - let mut p4: pdf_coord = pdf_coord::new(); + let mut p1 = pdf_coord::new(); + let mut p2 = pdf_coord::new(); + let mut p3 = pdf_coord::new(); + let mut p4 = pdf_coord::new(); (*I).subtype = 0i32; /* Image's attribute "bbox" here is affected by /Rotate entry of included * PDF page. diff --git a/dpx/src/dpx_spc_dvips.rs b/dpx/src/dpx_spc_dvips.rs index c49ae58e8..e03041af5 100644 --- a/dpx/src/dpx_spc_dvips.rs +++ b/dpx/src/dpx_spc_dvips.rs @@ -39,7 +39,7 @@ use crate::dpx_pdfobj::pdf_obj; use crate::{ttstub_input_close, ttstub_input_open}; use super::dpx_pdfdev::{ - pdf_dev_put_image, pdf_rect, pdf_tmatrix, transform_info, transform_info_clear, + pdf_dev_put_image, pdf_tmatrix, transform_info, transform_info_clear, }; use super::dpx_spc_util::spc_util_read_dimtrns; use libc::free; diff --git a/dpx/src/dpx_spc_html.rs b/dpx/src/dpx_spc_html.rs index 93e7e2286..91adc41cf 100644 --- a/dpx/src/dpx_spc_html.rs +++ b/dpx/src/dpx_spc_html.rs @@ -492,7 +492,7 @@ unsafe extern "C" fn html_open_dest( let mut error: i32 = 0; let mut array: *mut pdf_obj = 0 as *mut pdf_obj; let mut page_ref: *mut pdf_obj = 0 as *mut pdf_obj; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); cp.x = (*spe).x_user; cp.y = (*spe).y_user; pdf_dev_transform(&mut cp, None); diff --git a/dpx/src/dpx_spc_misc.rs b/dpx/src/dpx_spc_misc.rs index 257a2c5a5..ca3a89c87 100644 --- a/dpx/src/dpx_spc_misc.rs +++ b/dpx/src/dpx_spc_misc.rs @@ -31,7 +31,7 @@ use super::dpx_mpost::mps_scan_bbox; use super::dpx_pdfdev::{ - pdf_dev_put_image, pdf_rect, pdf_tmatrix, transform_info, transform_info_clear, + pdf_dev_put_image, transform_info, transform_info_clear, }; use super::dpx_pdfximage::pdf_ximage_findresource; use crate::dpx_pdfobj::pdf_obj; diff --git a/dpx/src/dpx_spc_pdfm.rs b/dpx/src/dpx_spc_pdfm.rs index 1d7349075..eb55b1f4f 100644 --- a/dpx/src/dpx_spc_pdfm.rs +++ b/dpx/src/dpx_spc_pdfm.rs @@ -825,7 +825,7 @@ unsafe extern "C" fn spc_handler_pdfm_annot(mut spe: *mut spc_env, mut args: *mu let mut annot_dict: *mut pdf_obj = 0 as *mut pdf_obj; let mut rect = pdf_rect::new(); let mut ident: *mut i8 = 0 as *mut i8; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut ti = transform_info::new(); skip_white(&mut (*args).curptr, (*args).endptr); if *(*args).curptr.offset(0) as i32 == '@' as i32 { @@ -1177,7 +1177,7 @@ unsafe extern "C" fn spc_handler_pdfm_bead(mut spe: *mut spc_env, mut args: *mut let mut rect = pdf_rect::new(); let mut page_no: i32 = 0; let mut ti = transform_info::new(); - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); skip_white(&mut (*args).curptr, (*args).endptr); if *(*args).curptr.offset(0) as i32 != '@' as i32 { spc_warn( diff --git a/dpx/src/dpx_spc_tpic.rs b/dpx/src/dpx_spc_tpic.rs index f44039d1b..ebc973232 100644 --- a/dpx/src/dpx_spc_tpic.rs +++ b/dpx/src/dpx_spc_tpic.rs @@ -581,7 +581,7 @@ unsafe extern "C" fn spc_handler_tpic_pa(mut spe: *mut spc_env, mut ap: *mut spc unsafe extern "C" fn spc_handler_tpic_fp(mut spe: *mut spc_env, mut ap: *mut spc_arg) -> i32 /* , void *dp) */ { let mut tp: *mut spc_tpic_ = &mut _tpic_state; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut pg: i32 = 0; assert!(!spe.is_null() && !ap.is_null() && !tp.is_null()); if (*tp).num_points <= 1i32 { @@ -597,7 +597,7 @@ unsafe extern "C" fn spc_handler_tpic_fp(mut spe: *mut spc_env, mut ap: *mut spc unsafe extern "C" fn spc_handler_tpic_ip(mut spe: *mut spc_env, mut ap: *mut spc_arg) -> i32 /* , void *dp) */ { let mut tp: *mut spc_tpic_ = &mut _tpic_state; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut pg: i32 = 0; assert!(!spe.is_null() && !ap.is_null() && !tp.is_null()); if (*tp).num_points <= 1i32 { @@ -615,7 +615,7 @@ unsafe extern "C" fn spc_handler_tpic_da(mut spe: *mut spc_env, mut ap: *mut spc let mut tp: *mut spc_tpic_ = &mut _tpic_state; let mut q: *mut i8 = 0 as *mut i8; let mut da: f64 = 0.0f64; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut pg: i32 = 0; assert!(!spe.is_null() && !ap.is_null() && !tp.is_null()); skip_blank(&mut (*ap).curptr, (*ap).endptr); @@ -639,7 +639,7 @@ unsafe extern "C" fn spc_handler_tpic_dt(mut spe: *mut spc_env, mut ap: *mut spc let mut tp: *mut spc_tpic_ = &mut _tpic_state; let mut q: *mut i8 = 0 as *mut i8; let mut da: f64 = 0.0f64; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut pg: i32 = 0; assert!(!spe.is_null() && !ap.is_null() && !tp.is_null()); skip_blank(&mut (*ap).curptr, (*ap).endptr); @@ -663,7 +663,7 @@ unsafe extern "C" fn spc_handler_tpic_sp(mut spe: *mut spc_env, mut ap: *mut spc let mut tp: *mut spc_tpic_ = &mut _tpic_state; let mut q: *mut i8 = 0 as *mut i8; let mut da: f64 = 0.0f64; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut pg: i32 = 0; assert!(!spe.is_null() && !ap.is_null() && !tp.is_null()); skip_blank(&mut (*ap).curptr, (*ap).endptr); @@ -686,7 +686,7 @@ unsafe extern "C" fn spc_handler_tpic_ar(mut spe: *mut spc_env, mut ap: *mut spc /* , void *dp) */ { let mut tp: *mut spc_tpic_ = &mut _tpic_state; let mut v: [f64; 6] = [0.; 6]; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut pg: i32 = 0; let mut q: *mut i8 = 0 as *mut i8; let mut i: i32 = 0; @@ -727,7 +727,7 @@ unsafe extern "C" fn spc_handler_tpic_ia(mut spe: *mut spc_env, mut ap: *mut spc /* , void *dp) */ { let mut tp: *mut spc_tpic_ = &mut _tpic_state; let mut v: [f64; 6] = [0.; 6]; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut pg: i32 = 0; let mut q: *mut i8 = 0 as *mut i8; let mut i: i32 = 0; diff --git a/dpx/src/dpx_spc_xtx.rs b/dpx/src/dpx_spc_xtx.rs index 27c991723..50ae62bc2 100644 --- a/dpx/src/dpx_spc_xtx.rs +++ b/dpx/src/dpx_spc_xtx.rs @@ -145,7 +145,7 @@ pub unsafe extern "C" fn spc_handler_xtx_do_transform( let mut init = pdf_tmatrix::new(); init }; - let mut pt: pdf_coord = pdf_coord::new(); + let mut pt = pdf_coord::new(); /* Create transformation matrix */ M.a = a; M.b = b; diff --git a/dpx/src/dpx_specials.rs b/dpx/src/dpx_specials.rs index 656b2593c..83c5aa29d 100644 --- a/dpx/src/dpx_specials.rs +++ b/dpx/src/dpx_specials.rs @@ -329,7 +329,7 @@ unsafe extern "C" fn ispageref(mut key: *const i8) -> i32 { #[no_mangle] pub unsafe extern "C" fn spc_lookup_reference(mut key: *const i8) -> *mut pdf_obj { let mut value: *mut pdf_obj = 0 as *mut pdf_obj; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut k: i32 = 0; assert!(!named_objects.is_null()); if key.is_null() { @@ -400,7 +400,7 @@ pub unsafe extern "C" fn spc_lookup_reference(mut key: *const i8) -> *mut pdf_ob #[no_mangle] pub unsafe extern "C" fn spc_lookup_object(mut key: *const i8) -> *mut pdf_obj { let mut value: *mut pdf_obj = 0 as *mut pdf_obj; - let mut cp: pdf_coord = pdf_coord::new(); + let mut cp = pdf_coord::new(); let mut k: i32 = 0; assert!(!named_objects.is_null()); if key.is_null() { @@ -767,7 +767,7 @@ unsafe extern "C" fn print_error(mut name: *const i8, mut spe: *mut spc_env, mut let mut ebuf: [i8; 64] = [0; 64]; let mut i: i32 = 0; let mut pg: i32 = (*spe).pg; - let mut c: pdf_coord = pdf_coord::new(); + let mut c = pdf_coord::new(); c.x = (*spe).x_user; c.y = (*spe).y_user; pdf_dev_transform(&mut c, None); diff --git a/engine/src/xetex_pic.rs b/engine/src/xetex_pic.rs index 619046e83..ef88d2f27 100644 --- a/engine/src/xetex_pic.rs +++ b/engine/src/xetex_pic.rs @@ -379,10 +379,10 @@ unsafe extern "C" fn pdf_get_rect( let mut page: *mut pdf_obj = 0 as *mut pdf_obj; let mut bbox = pdf_rect::new(); let mut matrix = pdf_tmatrix::new(); - let mut p1: pdf_coord = pdf_coord::new(); - let mut p2: pdf_coord = pdf_coord::new(); - let mut p3: pdf_coord = pdf_coord::new(); - let mut p4: pdf_coord = pdf_coord::new(); + let mut p1 = pdf_coord::new(); + let mut p2 = pdf_coord::new(); + let mut p3 = pdf_coord::new(); + let mut p4 = pdf_coord::new(); pf = pdf_open(filename, handle); if pf.is_null() { /* TODO: issue warning */ From 7909a68f384d73c70405285a621965dfea98a2b2 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 14 Sep 2019 09:29:52 +0300 Subject: [PATCH 2/7] pdfpathvec --- dpx/src/dpx_pdfdraw.rs | 276 ++++++++++++++++++----------------------- 1 file changed, 120 insertions(+), 156 deletions(-) diff --git a/dpx/src/dpx_pdfdraw.rs b/dpx/src/dpx_pdfdraw.rs index 4cbe317a4..2e47330b6 100644 --- a/dpx/src/dpx_pdfdraw.rs +++ b/dpx/src/dpx_pdfdraw.rs @@ -64,7 +64,7 @@ pub use super::dpx_pdfcolor::pdf_color; use super::dpx_pdfdev::{pdf_coord, pdf_rect, pdf_tmatrix}; /* Graphics State */ -#[derive(Copy, Clone)] +#[derive(Clone)] #[repr(C)] pub struct pdf_gstate { pub cp: pdf_coord, @@ -81,12 +81,9 @@ pub struct pdf_gstate { pub flags: i32, pub pt_fixee: pdf_coord, } -#[derive(Copy, Clone)] -#[repr(C)] +#[derive(Clone)] pub struct pdf_path { - pub num_paths: u32, - pub max_paths: u32, - pub path: *mut pa_elem, + pub path: Vec /* cm, - */ /* colorspace here */ /* d, D */ @@ -99,7 +96,19 @@ pub struct pdf_path { /* bookkeeping the origin of the last transform applied */ /* _PDF_DRAW_H_ */ } -#[derive(Copy, Clone)] + +impl pdf_path { + pub fn new() -> Self { + Self { + path: vec![] + } + } + pub fn len(&self) -> usize { + self.path.len() + } +} + +#[derive(Copy, Clone, Default)] #[repr(C)] pub struct pa_elem { pub typ: PeType, @@ -199,12 +208,7 @@ pub unsafe extern "C" fn pdf_invertmatrix(M: &mut pdf_tmatrix) { W.e /= det; W.f /= det } - M.a = W.a; - M.b = W.b; - M.c = W.c; - M.d = W.d; - M.e = W.e; - M.f = W.f; + *M = W; } #[derive(Clone,Copy,Debug,PartialEq)] @@ -218,6 +222,12 @@ pub enum PeType { TERMINATE = 6, } +impl Default for PeType { + fn default() -> Self { + PeType::MOVETO + } +} + impl PeType { pub fn opchr(&self) -> i8 { use PeType::*; @@ -246,7 +256,7 @@ impl PeType { } static mut fmt_buf: [i8; 1024] = [0; 1024]; -unsafe extern "C" fn init_a_path(p: &mut pdf_path) { +/*fn init_a_path(p: &mut pdf_path) { p.num_paths = 0_u32; p.max_paths = 0_u32; p.path = 0 as *mut pa_elem; @@ -268,13 +278,14 @@ unsafe extern "C" fn pdf_path__growpath(p: &mut pdf_path, mut max_pe: u32) -> i3 (p.max_paths as u64).wrapping_mul(::std::mem::size_of::() as u64) as u32, ) as *mut pa_elem; 0i32 -} -unsafe extern "C" fn clear_a_path(p: &mut pdf_path) { - p.path = mfree(p.path as *mut libc::c_void) as *mut pa_elem; +}*/ +fn clear_a_path(p: &mut pdf_path) { + p.path = vec![]; + /*p.path = mfree(p.path as *mut libc::c_void) as *mut pa_elem; p.num_paths = 0_u32; - p.max_paths = 0_u32; + p.max_paths = 0_u32;*/ } -unsafe extern "C" fn pdf_path__copypath(p1: &mut pdf_path, p0: &pdf_path) -> i32 { +/*unsafe extern "C" fn pdf_path__copypath(p1: &mut pdf_path, p0: &pdf_path) -> i32 { let mut i: u32 = 0; pdf_path__growpath(p1, p0.num_paths); i = 0_u32; @@ -287,30 +298,28 @@ unsafe extern "C" fn pdf_path__copypath(p1: &mut pdf_path, p0: &pdf_path) -> i32 } p1.num_paths = p0.num_paths; 0i32 -} +}*/ /* start new subpath */ unsafe extern "C" fn pdf_path__moveto( pa: &mut pdf_path, cp: &mut pdf_coord, p0: &pdf_coord, ) -> i32 { - pdf_path__growpath(pa, pa.num_paths + 1); - if pa.num_paths > 0_u32 { - let pe = &mut *(*pa) - .path - .offset((pa.num_paths - 1) as isize); + if !pa.path.is_empty() { + let len = pa.len(); + let pe = &mut pa.path[len - 1]; if pe.typ == PeType::MOVETO { *cp = *p0; pe.p[0] = *cp; return 0i32; } } - let fresh0 = pa.num_paths; - pa.num_paths += 1; - let pe = &mut *pa.path.offset(fresh0 as isize); - pe.typ = PeType::MOVETO; *cp = *p0; - pe.p[0] = *cp; + let pe = pa_elem { + typ: PeType::MOVETO, + p: [*cp, pdf_coord::new(), pdf_coord::new()], + }; + pa.path.push(pe); 0i32 } /* Do 'compression' of path while adding new path elements. @@ -322,90 +331,69 @@ unsafe extern "C" fn pdf_path__moveto( * This affects how 'closepath' is treated. */ unsafe extern "C" fn pdf_path__next_pe<'a>(pa: &'a mut pdf_path, cp: &pdf_coord) -> &'a mut pa_elem { - let mut pe: *mut pa_elem = 0 as *mut pa_elem; - pdf_path__growpath(pa, pa.num_paths + 2); - if pa.num_paths == 0_u32 { - let fresh1 = pa.num_paths; - pa.num_paths += 1; - let pe = &mut *pa.path.offset(fresh1 as isize); - pe.typ = PeType::MOVETO; - pe.p[0].x = cp.x; - pe.p[0].y = cp.y; - let fresh2 = pa.num_paths; - pa.num_paths += 1; - return &mut *pa.path.offset(fresh2 as isize); + if pa.path.is_empty() { + let mut pe = pa_elem::default(); + pe.p[0] = *cp; + pa.path.push(pe); + pa.path.push(pa_elem::default()); + let len = pa.len(); + return &mut pa.path[len-1]; } - let mut pe = &mut *(*pa) - .path - .offset((pa.num_paths - 1) as isize); + let len = pa.len(); + let mut pe = &mut pa.path[len-1]; match pe.typ { PeType::MOVETO => { - pe.p[0].x = cp.x; - pe.p[0].y = cp.y + pe.p[0] = *cp; } PeType::LINETO => { - if &mut *pe.p.as_mut_ptr().offset(0) != cp { - let fresh3 = pa.num_paths; - pa.num_paths += 1; - pe = &mut *pa.path.offset(fresh3 as isize); - pe.typ = PeType::MOVETO; - pe.p[0].x = cp.x; - pe.p[0].y = cp.y + if &pe.p[0] != cp { + let mut pe = pa_elem::default(); + pe.p[0] = *cp; + pa.path.push(pe); } } PeType::CURVETO => { - if &mut *pe.p.as_mut_ptr().offset(2) != cp { - let fresh4 = pa.num_paths; - pa.num_paths += 1; - pe = &mut *pa.path.offset(fresh4 as isize); - pe.typ = PeType::MOVETO; - pe.p[0].x = cp.x; - pe.p[0].y = cp.y + if &pe.p[2] != cp { + let mut pe = pa_elem::default(); + pe.p[0] = *cp; + pa.path.push(pe); } } PeType::CURVETO_Y | PeType::CURVETO_V => { - if &mut *pe.p.as_mut_ptr().offset(1) != cp { - let fresh5 = pa.num_paths; - pa.num_paths += 1; - pe = &mut *pa.path.offset(fresh5 as isize); - pe.typ = PeType::MOVETO; - pe.p[0].x = cp.x; - pe.p[0].y = cp.y + if &pe.p[1] != cp { + let mut pe = pa_elem::default(); + pe.p[0] = *cp; + pa.path.push(pe); } } PeType::CLOSEPATH => { - let fresh6 = pa.num_paths; - pa.num_paths += 1; - pe = &mut *pa.path.offset(fresh6 as isize); - pe.typ = PeType::MOVETO; - pe.p[0].x = cp.x; - pe.p[0].y = cp.y + let mut pe = pa_elem::default(); + pe.p[0] = *cp; + pa.path.push(pe); } _ => {} } - let fresh7 = pa.num_paths; - pa.num_paths += 1; - &mut *pa.path.offset(fresh7 as isize) + pa.path.push(pa_elem::default()); + let len = pa.len(); + return &mut pa.path[len-1]; } unsafe extern "C" fn pdf_path__transform(pa: &mut pdf_path, M: &pdf_tmatrix) -> i32 { - let mut pe: *mut pa_elem = 0 as *mut pa_elem; - let mut n: u32 = 0_u32; - let mut i: u32 = 0; - i = 0_u32; - while i < pa.num_paths { - let pe = &mut *pa.path.offset(i as isize); - n = (if pe.typ != PeType::TERMINATE { - (*pe).typ.n_pts() as i32 + let mut n = 0; + let mut i = 0; + while i < pa.len() { + let pe = &mut pa.path[i]; + n = if pe.typ != PeType::TERMINATE { + (*pe).typ.n_pts() } else { - 0i32 - }) as u32; + 0 + }; loop { let fresh8 = n; n += 1; - if !(fresh8 > 0_u32) { + if !(fresh8 > 0) { break; } - pdf_coord__transform(&mut *(*pe).p.as_mut_ptr().offset(n as isize), M); + pdf_coord__transform(&mut pe.p[n], M); } i += 1 } @@ -522,7 +510,7 @@ unsafe extern "C" fn pdf_path__elliptarc( pdf_coord__transform(&mut p0, &mut T); p0.x += ca.x; p0.y += ca.y; - if pa.num_paths == 0_u32 { + if pa.path.is_empty() { pdf_path__moveto(pa, cp, &mut p0); } else if cp != &p0 { pdf_path__lineto(pa, cp, &mut p0); @@ -572,32 +560,24 @@ unsafe extern "C" fn pdf_path__elliptarc( unsafe extern "C" fn pdf_path__closepath(pa: &mut pdf_path, cp: &mut pdf_coord) -> i32 /* no arg */ { let mut pe: *mut pa_elem = 0 as *mut pa_elem; - let mut i: i32 = 0; /* search for start point of the last subpath */ - i = (pa.num_paths - 1) as i32; /* No path or no start point(!) */ - while i >= 0i32 { - pe = &mut *pa.path.offset(i as isize) as *mut pa_elem; - if (*pe).typ == PeType::MOVETO { - break; - } - i -= 1 - } - if pe.is_null() || i < 0i32 { - return -1i32; + let pe = pa.path.iter().rev().find(|pe| pe.typ == PeType::MOVETO); + + if let Some(pe) = pe { + *cp = (*pe).p[0].clone(); + /* NOTE: + * Manually closed path without closepath is not + * affected by linejoin. A path with coincidental + * starting and ending point is not the same as + * 'closed' path. + */ + let mut pe = pa_elem::default(); + pe.typ = PeType::CLOSEPATH; + pa.path.push(pe); + 0i32 + } else { + -1i32 } - *cp = (*pe).p[0].clone(); - pdf_path__growpath(pa, pa.num_paths + 1); - /* NOTE: - * Manually closed path without closepath is not - * affected by linejoin. A path with coincidental - * starting and ending point is not the same as - * 'closed' path. - */ - let fresh9 = pa.num_paths; - pa.num_paths += 1; - pe = &mut *pa.path.offset(fresh9 as isize) as *mut pa_elem; - (*pe).typ = PeType::CLOSEPATH; - 0i32 } /* * x y width height re @@ -613,12 +593,12 @@ unsafe extern "C" fn pdf_path__closepath(pa: &mut pdf_path, cp: &mut pdf_coord) /* Just for quick test */ unsafe extern "C" fn pdf_path__isarect(pa: &mut pdf_path, mut f_ir: i32) -> i32 /* fill-rule is ignorable */ { - if pa.num_paths == 5_u32 { - let pe0 = &mut *pa.path.offset(0); - let pe1 = &mut *pa.path.offset(1); - let pe2 = &mut *pa.path.offset(2); - let pe3 = &mut *pa.path.offset(3); - let pe4 = &mut *pa.path.offset(4); + if pa.len() == 5 { + let pe0 = &pa.path[0]; + let pe1 = &pa.path[1]; + let pe2 = &pa.path[2]; + let pe3 = &pa.path[3]; + let pe4 = &pa.path[4]; if pe0.typ == PeType::MOVETO && pe1.typ == PeType::LINETO && pe2.typ == PeType::LINETO @@ -808,19 +788,19 @@ unsafe extern "C" fn pdf_dev__flushpath( } else { 0i32 }; - if pa.num_paths <= 0_u32 && path_added == 0i32 { + if /*pa.num_paths <= 0_u32 &&*/ path_added == 0i32 { return 0i32; } path_added = 0i32; graphics_mode(); isrect = pdf_path__isarect(pa, ignore_rule); if isrect != 0 { - pe = &mut *pa.path.offset(0) as *mut pa_elem; - pe1 = &mut *pa.path.offset(2) as *mut pa_elem; - r.llx = (*pe).p[0].x; - r.lly = (*pe).p[0].y; - r.urx = (*pe1).p[0].x - (*pe).p[0].x; - r.ury = (*pe1).p[0].y - (*pe).p[0].y; + let pe = &pa.path[0]; + let pe1 = &pa.path[2]; + r.llx = pe.p[0].x; + r.lly = pe.p[0].y; + r.urx = pe1.p[0].x - pe.p[0].x; + r.ury = pe1.p[0].y - pe.p[0].y; let fresh28 = len; len = len + 1; *b.offset(fresh28 as isize) = ' ' as i32 as i8; @@ -837,25 +817,17 @@ unsafe extern "C" fn pdf_dev__flushpath( pdf_doc_add_page_content(b, len as u32); len = 0i32 } else { - n_seg = pa.num_paths as i32; - i = 0i32; - len = 0i32; - pe = &mut *pa.path.offset(0) as *mut pa_elem; - while i < n_seg { - n_pts = if !pe.is_null() && (*pe).typ != PeType::TERMINATE { - (*pe).typ.n_pts() as i32 + for pe in pa.path.iter_mut() { + n_pts = if /* !pe.is_null() &&*/ pe.typ != PeType::TERMINATE { + pe.typ.n_pts() as i32 } else { 0i32 }; - j = 0i32; - pt = &mut *(*pe).p.as_mut_ptr().offset(0) as *mut pdf_coord; - while j < n_pts { + for (j, pt) in (0..n_pts).zip(pe.p.iter_mut()) { let fresh32 = len; len = len + 1; *b.offset(fresh32 as isize) = ' ' as i32 as i8; len += pdf_sprint_coord(b.offset(len as isize), &mut *pt); - j += 1; - pt = pt.offset(1) } let fresh33 = len; len = len + 1; @@ -863,8 +835,8 @@ unsafe extern "C" fn pdf_dev__flushpath( let fresh34 = len; len = len + 1; *b.offset(fresh34 as isize) = - if !pe.is_null() && (*pe).typ != PeType::TERMINATE { - (*pe).typ.opchr() + if /* !pe.is_null() &&*/ pe.typ != PeType::TERMINATE { + pe.typ.opchr() } else { b' ' as i8 }; @@ -872,8 +844,6 @@ unsafe extern "C" fn pdf_dev__flushpath( pdf_doc_add_page_content(b, len as u32); len = 0i32 } - pe = pe.offset(1); - i += 1 } if len > 0i32 { pdf_doc_add_page_content(b, len as u32); @@ -972,7 +942,7 @@ unsafe extern "C" fn init_a_gstate(mut gs: *mut pdf_gstate) { (*gs).flatness = 1i32; /* Internal variables */ (*gs).flags = 0i32; - init_a_path(&mut (*gs).path); + (*gs).path = pdf_path::new(); (*gs).pt_fixee.x = 0i32 as f64; (*gs).pt_fixee.y = 0i32 as f64; } @@ -987,20 +957,14 @@ unsafe extern "C" fn clear_a_gstate(mut gs: *mut pdf_gstate) { unsafe extern "C" fn copy_a_gstate(mut gs1: *mut pdf_gstate, mut gs2: *mut pdf_gstate) { let mut i: i32 = 0; assert!(!gs1.is_null() && !gs2.is_null()); - (*gs1).cp.x = (*gs2).cp.x; - (*gs1).cp.y = (*gs2).cp.y; - (*gs1).matrix.a = (*gs2).matrix.a; - (*gs1).matrix.b = (*gs2).matrix.b; - (*gs1).matrix.c = (*gs2).matrix.c; - (*gs1).matrix.d = (*gs2).matrix.d; - (*gs1).matrix.e = (*gs2).matrix.e; - (*gs1).matrix.f = (*gs2).matrix.f; + (*gs1).cp = (*gs2).cp; + (*gs1).matrix = (*gs2).matrix; /* TODO: * Path should be linked list and gsave only * record starting point within path rather than * copying whole path. */ - pdf_path__copypath(&mut (*gs1).path, &mut (*gs2).path); /* Initial state */ + (*gs1).path = (*gs2).path.clone(); /* Initial state */ (*gs1).linedash.num_dash = (*gs2).linedash.num_dash; i = 0i32; while i < (*gs2).linedash.num_dash { @@ -1380,7 +1344,7 @@ pub unsafe extern "C" fn pdf_dev_flushpath(mut p_op: i8, mut fill_rule: i32) -> * is inessential. */ error = pdf_dev__flushpath(cpa, p_op, fill_rule, 1i32); - pdf_path__clearpath(cpa); + cpa.path.clear(); (*gs).flags &= !(1i32 << 0i32); error } @@ -1389,8 +1353,8 @@ pub unsafe extern "C" fn pdf_dev_newpath() -> i32 { let mut gss: *mut m_stack = &mut gs_stack; let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; let mut p = &mut (*gs).path; - if p.num_paths > 0_u32 { - pdf_path__clearpath(p); + if !p.path.is_empty() { + p.path.clear(); } /* The following is required for "newpath" operator in mpost.c. */ pdf_doc_add_page_content(b" n\x00" as *const u8 as *const i8, 2_u32); /* op: n */ From 65de0bb8a8e1346dfadb13768393c06eb87a9c5b Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 14 Sep 2019 15:19:26 +0300 Subject: [PATCH 3/7] stacks --- Cargo.lock | 1 + dpx/Cargo.toml | 1 + dpx/src/dpx_dvi.rs | 2 +- dpx/src/dpx_epdf.rs | 2 +- dpx/src/dpx_mpost.rs | 2 +- dpx/src/dpx_pdfcolor.rs | 90 ++--- dpx/src/dpx_pdfdev.rs | 10 + dpx/src/dpx_pdfdoc.rs | 4 +- dpx/src/dpx_pdfdraw.rs | 689 ++++++++++++++++----------------------- dpx/src/dpx_spc_color.rs | 6 +- dpx/src/dpx_spc_dvips.rs | 4 +- dpx/src/dpx_spc_misc.rs | 4 +- dpx/src/dpx_spc_pdfm.rs | 20 +- dpx/src/dpx_spc_tpic.rs | 2 +- dpx/src/dpx_spc_util.rs | 150 ++++----- dpx/src/dpx_spc_xtx.rs | 2 +- dpx/src/lib.rs | 12 +- engine/src/lib.rs | 3 +- 18 files changed, 452 insertions(+), 552 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d9fa56448..3aa25bf93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1426,6 +1426,7 @@ dependencies = [ name = "tectonic_dvipdfmx" version = "0.0.1-dev" dependencies = [ + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", "libpng-sys 1.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "libz-sys 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/dpx/Cargo.toml b/dpx/Cargo.toml index 0395e7886..3be37660d 100644 --- a/dpx/Cargo.toml +++ b/dpx/Cargo.toml @@ -8,6 +8,7 @@ license = "GPL" [dependencies] tectonic_bridge = { version = "0.0.1-dev", path = "../bridge" } +lazy_static = "1.4" libc = "0.2" libpng-sys = "1" libz-sys = { version = "1", optional = true} diff --git a/dpx/src/dpx_dvi.rs b/dpx/src/dpx_dvi.rs index ddaf098d8..698ef0239 100644 --- a/dpx/src/dpx_dvi.rs +++ b/dpx/src/dpx_dvi.rs @@ -2306,7 +2306,7 @@ unsafe extern "C" fn do_glyphs(mut do_actual_text: i32) { if (*font).rgba_color != 0xffffffffu32 { let mut color: pdf_color = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; pdf_color_rgbcolor( diff --git a/dpx/src/dpx_epdf.rs b/dpx/src/dpx_epdf.rs index d1579792b..992b00f5e 100644 --- a/dpx/src/dpx_epdf.rs +++ b/dpx/src/dpx_epdf.rs @@ -89,7 +89,7 @@ pub type __off64_t = i64; pub type size_t = u64; pub type rust_input_handle_t = *mut libc::c_void; -use super::dpx_pdfdev::{pdf_coord, pdf_rect, pdf_tmatrix}; +use super::dpx_pdfdev::{pdf_coord, pdf_tmatrix}; use crate::dpx_pdfximage::{load_options, pdf_ximage, xform_info}; pub const OP_CURVETO2: C2RustUnnamed_0 = 15; diff --git a/dpx/src/dpx_mpost.rs b/dpx/src/dpx_mpost.rs index 906ff48e7..35ccf337b 100644 --- a/dpx/src/dpx_mpost.rs +++ b/dpx/src/dpx_mpost.rs @@ -3259,7 +3259,7 @@ unsafe extern "C" fn do_operator(mut token: *const i8, mut x_user: f64, mut y_us let mut cp = pdf_coord::new(); let mut color: pdf_color = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; opcode = get_opcode(token); diff --git a/dpx/src/dpx_pdfcolor.rs b/dpx/src/dpx_pdfcolor.rs index 4d094b060..ed4014d05 100644 --- a/dpx/src/dpx_pdfcolor.rs +++ b/dpx/src/dpx_pdfcolor.rs @@ -76,13 +76,25 @@ extern "C" { fn MD5_final(outbuf: *mut u8, ctx: *mut MD5_CONTEXT); } pub type size_t = u64; -#[derive(Copy, Clone)] + +use std::ffi::{CStr, CString}; + +#[derive(Clone)] #[repr(C)] pub struct pdf_color { pub num_components: i32, - pub spot_color_name: *mut i8, + pub spot_color_name: Option, pub values: [f64; 4], } +impl pdf_color { + pub const fn new() -> Self { + Self { + num_components: 0, + spot_color_name: None, + values: [0.; 4], + } + } +} #[derive(Copy, Clone)] #[repr(C)] pub struct pdf_colorspace { @@ -160,9 +172,9 @@ pub struct C2RustUnnamed_1 { pub major: i32, pub minor: i32, } -#[derive(Copy, Clone)] +#[derive(Clone)] #[repr(C)] -pub struct C2RustUnnamed_2 { +pub struct ColorStack { pub current: i32, pub stroke: [pdf_color; 128], pub fill: [pdf_color; 128], @@ -204,7 +216,7 @@ pub unsafe extern "C" fn pdf_color_rgbcolor(color: &mut pdf_color, r: f64, g: f6 color.values[1] = g; color.values[2] = b; color.num_components = 3i32; - color.spot_color_name = 0 as *mut i8; + color.spot_color_name = None; 0i32 } #[no_mangle] @@ -236,20 +248,32 @@ pub unsafe extern "C" fn pdf_color_cmykcolor( color.values[2] = y; color.values[3] = k; color.num_components = 4i32; - color.spot_color_name = 0 as *mut i8; + color.spot_color_name = None; 0i32 } #[no_mangle] -pub unsafe extern "C" fn pdf_color_graycolor(color: &mut pdf_color, mut g: f64) -> i32 { +pub unsafe extern "C" fn pdf_color_graycolor(color: &mut pdf_color, g: f64) -> i32 { if g < 0.0f64 || g > 1.0f64 { warn!("Invalid color value specified: gray={}", g); return -1i32; } color.values[0] = g; color.num_components = 1i32; - color.spot_color_name = 0 as *mut i8; + color.spot_color_name = None; 0i32 } +pub fn pdf_color_graycolor_new(g: f64) -> Result { + if g < 0. || g > 1. { + warn!("Invalid color value specified: gray={}", g); + return Err(-1); + } + Ok(pdf_color { + values: [g, 0., 0., 0.], + num_components: 1, + spot_color_name: None, + }) +} + #[no_mangle] pub unsafe extern "C" fn pdf_color_spotcolor( color: &mut pdf_color, @@ -263,20 +287,12 @@ pub unsafe extern "C" fn pdf_color_spotcolor( color.values[0] = c; color.values[1] = 0.0f64; color.num_components = 2i32; - color.spot_color_name = name; + color.spot_color_name = Some(CStr::from_ptr(name).to_owned()); 0i32 } #[no_mangle] -pub unsafe extern "C" fn pdf_color_copycolor( - mut color1: *mut pdf_color, - mut color2: *const pdf_color, -) { - assert!(!color1.is_null() && !color2.is_null()); - memcpy( - color1 as *mut libc::c_void, - color2 as *const libc::c_void, - ::std::mem::size_of::() as u64, - ); +pub unsafe extern "C" fn pdf_color_copycolor(color1: &mut pdf_color, color2: &pdf_color) { + *color1 = color2.clone(); } /* Brighten up a color. f == 0 means no change, f == 1 means white. */ #[no_mangle] @@ -346,7 +362,7 @@ pub unsafe extern "C" fn pdf_color_to_string( len = sprintf( buffer, b" /%s %c%c %g %c%c\x00" as *const u8 as *const i8, - color.spot_color_name, + color.spot_color_name.as_ref().unwrap().as_ptr(), 'C' as i32 | mask as i32, 'S' as i32 | mask as i32, (color.values[0] / 0.001f64 + 0.5f64).floor() * 0.001f64, @@ -417,8 +433,11 @@ pub unsafe extern "C" fn pdf_color_compare(color1: &pdf_color, color2: &pdf_colo return -1i32; } } - if !color1.spot_color_name.is_null() && !color2.spot_color_name.is_null() { - return strcmp(color1.spot_color_name, color2.spot_color_name); + if color1.spot_color_name.is_some() && color2.spot_color_name.is_some() { + return strcmp( + color1.spot_color_name.as_ref().unwrap().as_ptr(), + color2.spot_color_name.as_ref().unwrap().as_ptr(), + ); } 0i32 } @@ -469,8 +488,8 @@ pub unsafe extern "C" fn pdf_color_is_valid(color: &pdf_color) -> bool { } } if pdf_color_type(color) == -2i32 { - if color.spot_color_name.is_null() - || *color.spot_color_name.offset(0) as i32 == '\u{0}' as i32 + if color.spot_color_name.is_none() + || *color.spot_color_name.as_ref().unwrap().as_ptr().offset(0) as i32 == '\u{0}' as i32 { warn!("Invalid spot color: empty name"); return false; @@ -478,19 +497,14 @@ pub unsafe extern "C" fn pdf_color_is_valid(color: &pdf_color) -> bool { } true } -static mut color_stack: C2RustUnnamed_2 = C2RustUnnamed_2 { +/*static mut color_stack: ColorStack = ColorStack { current: 0, - stroke: [pdf_color { - num_components: 0, - spot_color_name: 0 as *const i8 as *mut i8, - values: [0.; 4], - }; 128], - fill: [pdf_color { - num_components: 0, - spot_color_name: 0 as *const i8 as *mut i8, - values: [0.; 4], - }; 128], -}; + stroke: unsafe { core::mem::zeroed() },//[pdf_color::new(); 128], + fill: unsafe { core::mem::zeroed() },//[pdf_color::new(); 128], +};*/ +static mut color_stack: ColorStack = + unsafe { std::mem::transmute([0u8; std::mem::size_of::()]) }; + #[no_mangle] pub unsafe extern "C" fn pdf_color_clear_stack() { if color_stack.current > 0 { @@ -502,8 +516,8 @@ pub unsafe extern "C" fn pdf_color_clear_stack() { if !(fresh4 != 0) { break; } - free(color_stack.stroke[color_stack.current as usize].spot_color_name as *mut libc::c_void); - free(color_stack.fill[color_stack.current as usize].spot_color_name as *mut libc::c_void); + // free(color_stack.stroke[color_stack.current as usize].spot_color_name as *mut libc::c_void); + // free(color_stack.fill[color_stack.current as usize].spot_color_name as *mut libc::c_void); } color_stack.current = 0; pdf_color_graycolor(&mut color_stack.stroke[0], 0.0f64); diff --git a/dpx/src/dpx_pdfdev.rs b/dpx/src/dpx_pdfdev.rs index 4c972f2b6..a3c43db70 100644 --- a/dpx/src/dpx_pdfdev.rs +++ b/dpx/src/dpx_pdfdev.rs @@ -132,6 +132,16 @@ impl pdf_tmatrix { f: 0., } } + pub const fn identity() -> Self { + Self { + a: 1., + b: 0., + c: 0., + d: 1., + e: 0., + f: 0., + } + } } #[derive(Copy, Clone, Default)] #[repr(C)] diff --git a/dpx/src/dpx_pdfdoc.rs b/dpx/src/dpx_pdfdoc.rs index 528810541..420194740 100644 --- a/dpx/src/dpx_pdfdoc.rs +++ b/dpx/src/dpx_pdfdoc.rs @@ -3034,8 +3034,8 @@ unsafe extern "C" fn pdf_doc_finish_page(mut p: *mut pdf_doc) { static mut bgcolor: pdf_color = { let mut init = pdf_color { num_components: 1i32, - spot_color_name: 0 as *const i8 as *mut i8, - values: [1.0f64, 0., 0., 0.], + spot_color_name: None, + values: [1., 0., 0., 0.], }; init }; diff --git a/dpx/src/dpx_pdfdraw.rs b/dpx/src/dpx_pdfdraw.rs index 2e47330b6..0c13d39b9 100644 --- a/dpx/src/dpx_pdfdraw.rs +++ b/dpx/src/dpx_pdfdraw.rs @@ -29,15 +29,14 @@ unused_mut )] -use crate::mfree; use crate::warn; use super::dpx_pdfcolor::{ - pdf_color_compare, pdf_color_copycolor, pdf_color_graycolor, pdf_color_is_valid, + pdf_color_compare, pdf_color_copycolor, pdf_color_graycolor_new, pdf_color_is_valid, pdf_color_to_string, pdf_color_type, }; use super::dpx_pdfdev::{pdf_sprint_coord, pdf_sprint_matrix, pdf_sprint_rect}; -use libc::free; + extern "C" { #[no_mangle] fn graphics_mode(); @@ -52,10 +51,6 @@ extern "C" { #[no_mangle] fn sprintf(_: *mut i8, _: *const i8, _: ...) -> i32; #[no_mangle] - fn new(size: u32) -> *mut libc::c_void; - #[no_mangle] - fn renew(p: *mut libc::c_void, size: u32) -> *mut libc::c_void; - #[no_mangle] fn pdf_doc_add_page_content(buffer: *const i8, length: u32); } @@ -71,7 +66,7 @@ pub struct pdf_gstate { pub matrix: pdf_tmatrix, pub strokecolor: pdf_color, pub fillcolor: pdf_color, - pub linedash: C2RustUnnamed, + pub linedash: LineDash, pub linewidth: f64, pub linecap: i32, pub linejoin: i32, @@ -83,25 +78,22 @@ pub struct pdf_gstate { } #[derive(Clone)] pub struct pdf_path { - pub path: Vec - /* cm, - */ - /* colorspace here */ - /* d, D */ - /* w, LW */ - /* J, LC */ - /* j, LJ */ - /* M, ML */ - /* i, FL, 0 to 100 (0 for use device-default) */ - /* internal */ - /* bookkeeping the origin of the last transform applied */ - /* _PDF_DRAW_H_ */ + pub path: Vec, /* cm, - */ + /* colorspace here */ + /* d, D */ + /* w, LW */ + /* J, LC */ + /* j, LJ */ + /* M, ML */ + /* i, FL, 0 to 100 (0 for use device-default) */ + /* internal */ + /* bookkeeping the origin of the last transform applied */ + /* _PDF_DRAW_H_ */ } impl pdf_path { pub fn new() -> Self { - Self { - path: vec![] - } + Self { path: vec![] } } pub fn len(&self) -> usize { self.path.len() @@ -114,33 +106,14 @@ pub struct pa_elem { pub typ: PeType, pub p: [pdf_coord; 3], } -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Default)] #[repr(C)] -pub struct C2RustUnnamed { +pub struct LineDash { pub num_dash: i32, pub pattern: [f64; 16], pub offset: f64, } -#[derive(Copy, Clone)] -#[repr(C)] -pub struct m_stack { - pub size: i32, - pub top: *mut m_stack_elem, - pub bottom: *mut m_stack_elem, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct m_stack_elem { - pub data: *mut libc::c_void, - pub prev: *mut m_stack_elem, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_0 { - pub opchr: i8, - pub n_pts: i32, - pub strkey: *const i8, -} + unsafe extern "C" fn inversematrix(mut W: &mut pdf_tmatrix, mut M: &pdf_tmatrix) -> i32 { let mut det: f64 = 0.; det = M.a * M.d - M.b * M.c; @@ -211,7 +184,7 @@ pub unsafe extern "C" fn pdf_invertmatrix(M: &mut pdf_tmatrix) { *M = W; } -#[derive(Clone,Copy,Debug,PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq)] pub enum PeType { MOVETO = 0, LINETO = 1, @@ -256,49 +229,10 @@ impl PeType { } static mut fmt_buf: [i8; 1024] = [0; 1024]; -/*fn init_a_path(p: &mut pdf_path) { - p.num_paths = 0_u32; - p.max_paths = 0_u32; - p.path = 0 as *mut pa_elem; -} -extern "C" fn pdf_path__clearpath(mut p: &mut pdf_path) { - p.num_paths = 0_u32; -} -unsafe extern "C" fn pdf_path__growpath(p: &mut pdf_path, mut max_pe: u32) -> i32 { - if max_pe < p.max_paths { - return 0i32; - } - p.max_paths = if p.max_paths + 8 > max_pe { - p.max_paths + 8_u32 - } else { - max_pe - }; - p.path = renew( - p.path as *mut libc::c_void, - (p.max_paths as u64).wrapping_mul(::std::mem::size_of::() as u64) as u32, - ) as *mut pa_elem; - 0i32 -}*/ + fn clear_a_path(p: &mut pdf_path) { p.path = vec![]; - /*p.path = mfree(p.path as *mut libc::c_void) as *mut pa_elem; - p.num_paths = 0_u32; - p.max_paths = 0_u32;*/ -} -/*unsafe extern "C" fn pdf_path__copypath(p1: &mut pdf_path, p0: &pdf_path) -> i32 { - let mut i: u32 = 0; - pdf_path__growpath(p1, p0.num_paths); - i = 0_u32; - while i < p0.num_paths { - let pe1 = &mut *(*p1).path.offset(i as isize); - let pe0 = &mut *p0.path.offset(i as isize); - /* FIXME */ - *pe1 = *pe0; - i = i + 1 - } - p1.num_paths = p0.num_paths; - 0i32 -}*/ +} /* start new subpath */ unsafe extern "C" fn pdf_path__moveto( pa: &mut pdf_path, @@ -330,23 +264,26 @@ unsafe extern "C" fn pdf_path__moveto( * 'moveto' must be used to enforce starting new path. * This affects how 'closepath' is treated. */ -unsafe extern "C" fn pdf_path__next_pe<'a>(pa: &'a mut pdf_path, cp: &pdf_coord) -> &'a mut pa_elem { +unsafe extern "C" fn pdf_path__next_pe<'a>( + pa: &'a mut pdf_path, + cp: &pdf_coord, +) -> &'a mut pa_elem { if pa.path.is_empty() { let mut pe = pa_elem::default(); pe.p[0] = *cp; pa.path.push(pe); pa.path.push(pa_elem::default()); let len = pa.len(); - return &mut pa.path[len-1]; + return &mut pa.path[len - 1]; } let len = pa.len(); - let mut pe = &mut pa.path[len-1]; + let mut pe = &mut pa.path[len - 1]; match pe.typ { PeType::MOVETO => { pe.p[0] = *cp; } PeType::LINETO => { - if &pe.p[0] != cp { + if &pe.p[0] != cp { let mut pe = pa_elem::default(); pe.p[0] = *cp; pa.path.push(pe); @@ -375,7 +312,7 @@ unsafe extern "C" fn pdf_path__next_pe<'a>(pa: &'a mut pdf_path, cp: &pdf_coord) } pa.path.push(pa_elem::default()); let len = pa.len(); - return &mut pa.path[len-1]; + return &mut pa.path[len - 1]; } unsafe extern "C" fn pdf_path__transform(pa: &mut pdf_path, M: &pdf_tmatrix) -> i32 { let mut n = 0; @@ -659,17 +596,8 @@ unsafe extern "C" fn pdf_dev__rectshape( let mut p = pdf_coord::new(); let mut wd: f64 = 0.; let mut ht: f64 = 0.; - assert!( - (opchr as i32 == 'f' as i32 - || opchr as i32 == 'F' as i32 - || opchr as i32 == 's' as i32 - || opchr as i32 == 'S' as i32 - || opchr as i32 == 'b' as i32 - || opchr as i32 == 'B' as i32 - || opchr as i32 == 'W' as i32 - || opchr as i32 == ' ' as i32) - ); - isclip = if opchr as i32 == 'W' as i32 || opchr as i32 == ' ' as i32 { + assert!([b'f', b'F', b's', b'S', b'b', b'B', b'W', b' '].contains(&(opchr as u8))); + isclip = if opchr == b'W' as i8 || opchr == b' ' as i8 { 1i32 } else { 0i32 @@ -717,38 +645,38 @@ unsafe extern "C" fn pdf_dev__rectshape( ht = r.ury - r.lly; let fresh18 = len; len = len + 1; - *buf.offset(fresh18 as isize) = ' ' as i32 as i8; + *buf.offset(fresh18 as isize) = b' ' as i8; len += pdf_sprint_coord(buf.offset(len as isize), &mut p); let fresh19 = len; len = len + 1; - *buf.offset(fresh19 as isize) = ' ' as i32 as i8; + *buf.offset(fresh19 as isize) = b' ' as i8; len += pdf_sprint_length(buf.offset(len as isize), wd); let fresh20 = len; len = len + 1; - *buf.offset(fresh20 as isize) = ' ' as i32 as i8; + *buf.offset(fresh20 as isize) = b' ' as i8; len += pdf_sprint_length(buf.offset(len as isize), ht); let fresh21 = len; len = len + 1; - *buf.offset(fresh21 as isize) = ' ' as i32 as i8; + *buf.offset(fresh21 as isize) = b' ' as i8; let fresh22 = len; len = len + 1; - *buf.offset(fresh22 as isize) = 'r' as i32 as i8; + *buf.offset(fresh22 as isize) = b'r' as i8; let fresh23 = len; len = len + 1; - *buf.offset(fresh23 as isize) = 'e' as i32 as i8; - if opchr as i32 != ' ' as i32 { + *buf.offset(fresh23 as isize) = b'e' as i8; + if opchr != b' ' as i8 { let fresh24 = len; len = len + 1; - *buf.offset(fresh24 as isize) = ' ' as i32 as i8; + *buf.offset(fresh24 as isize) = b' ' as i8; let fresh25 = len; len = len + 1; *buf.offset(fresh25 as isize) = opchr; let fresh26 = len; len = len + 1; - *buf.offset(fresh26 as isize) = ' ' as i32 as i8; + *buf.offset(fresh26 as isize) = b' ' as i8; let fresh27 = len; len = len + 1; - *buf.offset(fresh27 as isize) = (if isclip != 0 { 'n' as i32 } else { 'Q' as i32 }) as i8 + *buf.offset(fresh27 as isize) = (if isclip != 0 { b'n' } else { b'Q' }) as i8 } pdf_doc_add_page_content(buf, len as u32); 0i32 @@ -774,21 +702,11 @@ unsafe extern "C" fn pdf_dev__flushpath( let mut isrect: i32 = 0; let mut i: i32 = 0; let mut j: i32 = 0; - assert!((opchr as i32 == 'f' as i32 - || opchr as i32 == 'F' as i32 - || opchr as i32 == 's' as i32 - || opchr as i32 == 'S' as i32 - || opchr as i32 == 'b' as i32 - || opchr as i32 == 'B' as i32 - || opchr as i32 == 'W' as i32 - || opchr as i32 == ' ' as i32) - ); - isclip = if opchr as i32 == 'W' as i32 { - 1i32 - } else { - 0i32 - }; - if /*pa.num_paths <= 0_u32 &&*/ path_added == 0i32 { + assert!([b'f', b'F', b's', b'S', b'b', b'B', b'W', b' '].contains(&(opchr as u8))); + isclip = if opchr == b'W' as i8 { 1i32 } else { 0i32 }; + if + /*pa.num_paths <= 0_u32 &&*/ + path_added == 0i32 { return 0i32; } path_added = 0i32; @@ -818,12 +736,14 @@ unsafe extern "C" fn pdf_dev__flushpath( len = 0i32 } else { for pe in pa.path.iter_mut() { - n_pts = if /* !pe.is_null() &&*/ pe.typ != PeType::TERMINATE { + n_pts = if + /* !pe.is_null() &&*/ + pe.typ != PeType::TERMINATE { pe.typ.n_pts() as i32 } else { 0i32 }; - for (j, pt) in (0..n_pts).zip(pe.p.iter_mut()) { + for (_j, pt) in (0..n_pts).zip(pe.p.iter_mut()) { let fresh32 = len; len = len + 1; *b.offset(fresh32 as isize) = ' ' as i32 as i8; @@ -834,12 +754,13 @@ unsafe extern "C" fn pdf_dev__flushpath( *b.offset(fresh33 as isize) = ' ' as i32 as i8; let fresh34 = len; len = len + 1; - *b.offset(fresh34 as isize) = - if /* !pe.is_null() &&*/ pe.typ != PeType::TERMINATE { - pe.typ.opchr() - } else { - b' ' as i8 - }; + *b.offset(fresh34 as isize) = if + /* !pe.is_null() &&*/ + pe.typ != PeType::TERMINATE { + pe.typ.opchr() + } else { + b' ' as i8 + }; if len + 128i32 > b_len { pdf_doc_add_page_content(b, len as u32); len = 0i32 @@ -872,233 +793,176 @@ unsafe extern "C" fn pdf_dev__flushpath( pdf_doc_add_page_content(b, len as u32); 0i32 } -unsafe extern "C" fn m_stack_init(mut stack: *mut m_stack) { - assert!(!stack.is_null()); - (*stack).size = 0i32; - (*stack).top = 0 as *mut m_stack_elem; - (*stack).bottom = 0 as *mut m_stack_elem; -} -unsafe extern "C" fn m_stack_push(mut stack: *mut m_stack, mut data: *mut libc::c_void) { - let mut elem: *mut m_stack_elem = 0 as *mut m_stack_elem; - assert!(!stack.is_null()); - elem = new((1_u64).wrapping_mul(::std::mem::size_of::() as u64) as u32) - as *mut m_stack_elem; - (*elem).prev = (*stack).top; - (*elem).data = data; - (*stack).top = elem; - if (*stack).size == 0i32 { - (*stack).bottom = elem - } - (*stack).size += 1; -} -unsafe extern "C" fn m_stack_pop(mut stack: *mut m_stack) -> *mut libc::c_void { - let mut elem: *mut m_stack_elem = 0 as *mut m_stack_elem; - let mut data: *mut libc::c_void = 0 as *mut libc::c_void; - assert!(!stack.is_null()); - if (*stack).size == 0i32 { - return 0 as *mut libc::c_void; - } - data = (*(*stack).top).data; - elem = (*stack).top; - (*stack).top = (*elem).prev; - if (*stack).size == 1i32 { - (*stack).bottom = 0 as *mut m_stack_elem + +trait Top { + fn top(&mut self) -> &mut T; +} + +impl Top for Vec { + fn top(&mut self) -> &mut T { + let last = self.len() - 1; + &mut self[last] } - free(elem as *mut libc::c_void); - (*stack).size -= 1; - data -} -unsafe extern "C" fn m_stack_top(mut stack: *mut m_stack) -> *mut libc::c_void { - let mut data: *mut libc::c_void = 0 as *mut libc::c_void; - assert!(!stack.is_null()); - if (*stack).size == 0i32 { - return 0 as *mut libc::c_void; +} + +use lazy_static::lazy_static; +use std::sync::Mutex; + +lazy_static! { + static ref gs_stack: Mutex> = Mutex::new({ + let mut v = vec![]; + v + }); +} + +impl pdf_gstate { + pub fn init() -> Self { + Self { + cp: pdf_coord::new(), + matrix: pdf_tmatrix::identity(), + strokecolor: pdf_color_graycolor_new(0.).unwrap(), + fillcolor: pdf_color_graycolor_new(0.).unwrap(), + linedash: LineDash::default(), + linecap: 0, + linejoin: 0, + linewidth: 1., + miterlimit: 10., + flatness: 1, + /* Internal variables */ + flags: 0, + path: pdf_path::new(), + pt_fixee: pdf_coord::new(), + } } - data = (*(*stack).top).data; - data } -static mut gs_stack: m_stack = m_stack { - size: 0, - top: 0 as *const m_stack_elem as *mut m_stack_elem, - bottom: 0 as *const m_stack_elem as *mut m_stack_elem, -}; -unsafe extern "C" fn init_a_gstate(mut gs: *mut pdf_gstate) { - (*gs).cp.x = 0.0f64; - (*gs).cp.y = 0.0f64; - (*gs).matrix.a = 1.0f64; - (*gs).matrix.b = 0.0f64; - (*gs).matrix.c = 0.0f64; - (*gs).matrix.d = 1.0f64; - (*gs).matrix.e = 0.0f64; - (*gs).matrix.f = 0.0f64; - pdf_color_graycolor(&mut (*gs).strokecolor, 0.0f64); - pdf_color_graycolor(&mut (*gs).fillcolor, 0.0f64); - (*gs).linedash.num_dash = 0i32; - (*gs).linedash.offset = 0i32 as f64; - (*gs).linecap = 0i32; - (*gs).linejoin = 0i32; - (*gs).linewidth = 1.0f64; - (*gs).miterlimit = 10.0f64; - (*gs).flatness = 1i32; - /* Internal variables */ - (*gs).flags = 0i32; - (*gs).path = pdf_path::new(); - (*gs).pt_fixee.x = 0i32 as f64; - (*gs).pt_fixee.y = 0i32 as f64; -} -unsafe extern "C" fn clear_a_gstate(mut gs: *mut pdf_gstate) { - clear_a_path(&mut (*gs).path); +/*unsafe extern "C" fn clear_a_gstate(mut gs: *mut pdf_gstate) { + //clear_a_path(&mut (*gs).path); memset( gs as *mut libc::c_void, 0i32, ::std::mem::size_of::() as u64, ); -} -unsafe extern "C" fn copy_a_gstate(mut gs1: *mut pdf_gstate, mut gs2: *mut pdf_gstate) { +}*/ +unsafe extern "C" fn copy_a_gstate(gs1: &mut pdf_gstate, gs2: &pdf_gstate) { let mut i: i32 = 0; - assert!(!gs1.is_null() && !gs2.is_null()); - (*gs1).cp = (*gs2).cp; - (*gs1).matrix = (*gs2).matrix; + gs1.cp = gs2.cp; + gs1.matrix = gs2.matrix; /* TODO: * Path should be linked list and gsave only * record starting point within path rather than * copying whole path. */ - (*gs1).path = (*gs2).path.clone(); /* Initial state */ - (*gs1).linedash.num_dash = (*gs2).linedash.num_dash; + gs1.path = gs2.path.clone(); /* Initial state */ + gs1.linedash.num_dash = gs2.linedash.num_dash; i = 0i32; - while i < (*gs2).linedash.num_dash { - (*gs1).linedash.pattern[i as usize] = (*gs2).linedash.pattern[i as usize]; + while i < gs2.linedash.num_dash { + gs1.linedash.pattern[i as usize] = gs2.linedash.pattern[i as usize]; i += 1 } - (*gs1).linedash.offset = (*gs2).linedash.offset; - (*gs1).linecap = (*gs2).linecap; - (*gs1).linejoin = (*gs2).linejoin; - (*gs1).linewidth = (*gs2).linewidth; - (*gs1).miterlimit = (*gs2).miterlimit; - (*gs1).flatness = (*gs2).flatness; - pdf_color_copycolor(&mut (*gs1).fillcolor, &mut (*gs2).fillcolor); - pdf_color_copycolor(&mut (*gs1).strokecolor, &mut (*gs2).strokecolor); - (*gs1).pt_fixee.x = (*gs2).pt_fixee.x; - (*gs1).pt_fixee.y = (*gs2).pt_fixee.y; + gs1.linedash.offset = gs2.linedash.offset; + gs1.linecap = gs2.linecap; + gs1.linejoin = gs2.linejoin; + gs1.linewidth = gs2.linewidth; + gs1.miterlimit = gs2.miterlimit; + gs1.flatness = gs2.flatness; + pdf_color_copycolor(&mut gs1.fillcolor, &gs2.fillcolor); + pdf_color_copycolor(&mut gs1.strokecolor, &gs2.strokecolor); + gs1.pt_fixee.x = gs2.pt_fixee.x; + gs1.pt_fixee.y = gs2.pt_fixee.y; } #[no_mangle] pub unsafe extern "C" fn pdf_dev_init_gstates() { - let mut gs: *mut pdf_gstate = 0 as *mut pdf_gstate; - m_stack_init(&mut gs_stack); - gs = new((1_u64).wrapping_mul(::std::mem::size_of::() as u64) as u32) - as *mut pdf_gstate; - init_a_gstate(gs); - m_stack_push(&mut gs_stack, gs as *mut libc::c_void); + let mut stack = gs_stack.lock().unwrap(); + *stack = vec![]; + let gs = pdf_gstate::init(); + stack.push(gs); } #[no_mangle] pub unsafe extern "C" fn pdf_dev_clear_gstates() { - let mut gs: *mut pdf_gstate = 0 as *mut pdf_gstate; - if gs_stack.size > 1i32 { + let mut stack = gs_stack.lock().unwrap(); + + if stack.len() > 1 { /* at least 1 elem. */ warn!("GS stack depth is not zero at the end of the document."); /* op: q */ } - loop { - gs = m_stack_pop(&mut gs_stack) as *mut pdf_gstate; - if gs.is_null() { - break; - } - clear_a_gstate(gs); - free(gs as *mut libc::c_void); - } + *stack = vec![]; } #[no_mangle] pub unsafe extern "C" fn pdf_dev_gsave() -> i32 { - let mut gs0: *mut pdf_gstate = 0 as *mut pdf_gstate; - let mut gs1: *mut pdf_gstate = 0 as *mut pdf_gstate; - gs0 = m_stack_top(&mut gs_stack) as *mut pdf_gstate; - gs1 = new((1_u64).wrapping_mul(::std::mem::size_of::() as u64) as u32) - as *mut pdf_gstate; - init_a_gstate(gs1); - copy_a_gstate(gs1, gs0); - m_stack_push(&mut gs_stack, gs1 as *mut libc::c_void); + let mut stack = gs_stack.lock().unwrap(); + let gs0 = stack.top(); + + let mut gs1 = pdf_gstate::init(); + copy_a_gstate(&mut gs1, gs0); + stack.push(gs1); + pdf_doc_add_page_content(b" q\x00" as *const u8 as *const i8, 2_u32); 0i32 } #[no_mangle] pub unsafe extern "C" fn pdf_dev_grestore() -> i32 { - let mut gs: *mut pdf_gstate = 0 as *mut pdf_gstate; - if gs_stack.size <= 1i32 { + let mut stack = gs_stack.lock().unwrap(); + if stack.len() <= 1 { /* Initial state at bottom */ warn!("Too many grestores."); /* op: Q */ return -1i32; } - gs = m_stack_pop(&mut gs_stack) as *mut pdf_gstate; - clear_a_gstate(gs); - free(gs as *mut libc::c_void); + let _gs = stack.pop(); pdf_doc_add_page_content(b" Q\x00" as *const u8 as *const i8, 2_u32); pdf_dev_reset_fonts(0i32); 0i32 } #[no_mangle] pub unsafe extern "C" fn pdf_dev_push_gstate() -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs0: *mut pdf_gstate = 0 as *mut pdf_gstate; - gs0 = new((1_u64).wrapping_mul(::std::mem::size_of::() as u64) as u32) - as *mut pdf_gstate; - init_a_gstate(gs0); - m_stack_push(gss, gs0 as *mut libc::c_void); + let mut stack = gs_stack.lock().unwrap(); + + let gs0 = pdf_gstate::init(); + stack.push(gs0); + 0i32 } #[no_mangle] pub unsafe extern "C" fn pdf_dev_pop_gstate() -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = 0 as *mut pdf_gstate; - if (*gss).size <= 1i32 { + let mut gss = gs_stack.lock().unwrap(); + if gss.len() <= 1 { /* Initial state at bottom */ warn!("Too many grestores."); return -1i32; } - gs = m_stack_pop(gss) as *mut pdf_gstate; - clear_a_gstate(gs); - free(gs as *mut libc::c_void); + let _gs = gss.pop(); 0i32 } #[no_mangle] -pub unsafe extern "C" fn pdf_dev_current_depth() -> i32 { - return gs_stack.size - 1i32; +pub extern "C" fn pdf_dev_current_depth() -> usize { + let stack = gs_stack.lock().unwrap(); + stack.len() - 1 /* 0 means initial state */ } #[no_mangle] -pub unsafe extern "C" fn pdf_dev_grestore_to(mut depth: i32) { - let mut gss: *mut m_stack = &mut gs_stack; /* op: Q */ - let mut gs: *mut pdf_gstate = 0 as *mut pdf_gstate; - assert!(depth >= 0i32); - if (*gss).size > depth + 1i32 { +pub unsafe extern "C" fn pdf_dev_grestore_to(mut depth: usize) { + let mut gss = gs_stack.lock().unwrap(); /* op: Q */ + assert!(depth >= 0); + if gss.len() > depth + 1 { warn!("Closing pending transformations at end of page/XObject."); } - while (*gss).size > depth + 1i32 { + while gss.len() > depth + 1 { pdf_doc_add_page_content(b" Q\x00" as *const u8 as *const i8, 2_u32); - gs = m_stack_pop(gss) as *mut pdf_gstate; - clear_a_gstate(gs); - free(gs as *mut libc::c_void); + let _gs = gss.pop(); } pdf_dev_reset_fonts(0i32); } #[no_mangle] pub unsafe extern "C" fn pdf_dev_currentpoint(p: &mut pdf_coord) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpt = &mut (*gs).cp; - *p = cpt.clone(); + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + *p = gs.cp.clone(); 0i32 } #[no_mangle] pub unsafe extern "C" fn pdf_dev_currentmatrix(M: &mut pdf_tmatrix) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let CTM = &mut (*gs).matrix; - M.a = CTM.a; - M.b = CTM.b; - M.c = CTM.c; - M.d = CTM.d; - M.e = CTM.e; - M.f = CTM.f; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + *M = gs.matrix.clone(); 0i32 } /* @@ -1109,12 +973,13 @@ pub unsafe extern "C" fn pdf_dev_currentmatrix(M: &mut pdf_tmatrix) -> i32 { */ #[no_mangle] pub unsafe extern "C" fn pdf_dev_set_color(color: &pdf_color, mut mask: i8, mut force: i32) { + let mut stack = gs_stack.lock().unwrap(); let mut len: i32 = 0; - let mut gs: *mut pdf_gstate = m_stack_top(&mut gs_stack) as *mut pdf_gstate; - let mut current = if mask as i32 != 0 { - &mut (*gs).fillcolor + let mut gs = stack.top(); + let current = if mask as i32 != 0 { + &mut gs.fillcolor } else { - &mut (*gs).strokecolor + &mut gs.strokecolor }; assert!(pdf_color_is_valid(color)); if !(pdf_dev_get_param(2i32) != 0 && (force != 0 || pdf_color_compare(color, current) != 0)) { @@ -1154,11 +1019,11 @@ pub unsafe extern "C" fn pdf_dev_set_color(color: &pdf_color, mut mask: i8, mut } #[no_mangle] pub unsafe extern "C" fn pdf_dev_concat(M: &pdf_tmatrix) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let cpt = &mut (*gs).cp; - let mut CTM = &mut (*gs).matrix; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; + let CTM = &mut gs.matrix; let mut W = pdf_tmatrix::new(); let mut buf: *mut i8 = fmt_buf.as_mut_ptr(); let mut len: i32 = 0i32; @@ -1222,11 +1087,11 @@ pub unsafe extern "C" fn pdf_dev_concat(M: &pdf_tmatrix) -> i32 { */ #[no_mangle] pub unsafe extern "C" fn pdf_dev_setmiterlimit(mut mlimit: f64) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; /* op: M */ - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; /* op: J */ + let mut gss = gs_stack.lock().unwrap(); /* op: M */ + let gs = gss.top(); /* op: J */ let mut len: i32 = 0i32; /* op: j */ let mut buf: *mut i8 = fmt_buf.as_mut_ptr(); /* op: w */ - if (*gs).miterlimit != mlimit { + if gs.miterlimit != mlimit { let fresh49 = len; /* op: */ len = len + 1; /* op: */ *buf.offset(fresh49 as isize) = ' ' as i32 as i8; /* op: */ @@ -1238,43 +1103,43 @@ pub unsafe extern "C" fn pdf_dev_setmiterlimit(mut mlimit: f64) -> i32 { len = len + 1; *buf.offset(fresh51 as isize) = 'M' as i32 as i8; pdf_doc_add_page_content(buf, len as u32); - (*gs).miterlimit = mlimit + gs.miterlimit = mlimit } 0i32 } #[no_mangle] pub unsafe extern "C" fn pdf_dev_setlinecap(mut capstyle: i32) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); let mut len: i32 = 0i32; let mut buf: *mut i8 = fmt_buf.as_mut_ptr(); - if (*gs).linecap != capstyle { + if gs.linecap != capstyle { len = sprintf(buf, b" %d J\x00" as *const u8 as *const i8, capstyle); pdf_doc_add_page_content(buf, len as u32); - (*gs).linecap = capstyle + gs.linecap = capstyle } 0i32 } #[no_mangle] pub unsafe extern "C" fn pdf_dev_setlinejoin(mut joinstyle: i32) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); let mut len: i32 = 0i32; let mut buf: *mut i8 = fmt_buf.as_mut_ptr(); - if (*gs).linejoin != joinstyle { + if gs.linejoin != joinstyle { len = sprintf(buf, b" %d j\x00" as *const u8 as *const i8, joinstyle); pdf_doc_add_page_content(buf, len as u32); - (*gs).linejoin = joinstyle + gs.linejoin = joinstyle } 0i32 } #[no_mangle] pub unsafe extern "C" fn pdf_dev_setlinewidth(mut width: f64) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); let mut len: i32 = 0i32; let mut buf: *mut i8 = fmt_buf.as_mut_ptr(); - if (*gs).linewidth != width { + if gs.linewidth != width { let fresh52 = len; len = len + 1; *buf.offset(fresh52 as isize) = ' ' as i32 as i8; @@ -1286,7 +1151,7 @@ pub unsafe extern "C" fn pdf_dev_setlinewidth(mut width: f64) -> i32 { len = len + 1; *buf.offset(fresh54 as isize) = 'w' as i32 as i8; pdf_doc_add_page_content(buf, len as u32); - (*gs).linewidth = width + gs.linewidth = width } 0i32 } @@ -1296,20 +1161,20 @@ pub unsafe extern "C" fn pdf_dev_setdash( mut pattern: *mut f64, mut offset: f64, ) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); let mut len: i32 = 0i32; let mut buf: *mut i8 = fmt_buf.as_mut_ptr(); let mut i: i32 = 0; - (*gs).linedash.num_dash = count; - (*gs).linedash.offset = offset; + gs.linedash.num_dash = count; + gs.linedash.offset = offset; pdf_doc_add_page_content(b" [\x00" as *const u8 as *const i8, 2_u32); i = 0i32; while i < count { *buf.offset(0) = ' ' as i32 as i8; len = pdf_sprint_length(buf.offset(1), *pattern.offset(i as isize)); pdf_doc_add_page_content(buf, (len + 1i32) as u32); - (*gs).linedash.pattern[i as usize] = *pattern.offset(i as isize); + gs.linedash.pattern[i as usize] = *pattern.offset(i as isize); i += 1 } pdf_doc_add_page_content(b"] \x00" as *const u8 as *const i8, 2_u32); @@ -1321,23 +1186,23 @@ pub unsafe extern "C" fn pdf_dev_setdash( /* ZSYUEDVEDEOF */ #[no_mangle] pub unsafe extern "C" fn pdf_dev_clip() -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; pdf_dev__flushpath(cpa, 'W' as i32 as i8, 0i32, 0i32) } #[no_mangle] pub unsafe extern "C" fn pdf_dev_eoclip() -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; pdf_dev__flushpath(cpa, 'W' as i32 as i8, 1i32, 0i32) } #[no_mangle] pub unsafe extern "C" fn pdf_dev_flushpath(mut p_op: i8, mut fill_rule: i32) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; let mut error: i32 = 0i32; /* last arg 'ignore_rule' is only for single object * that can be converted to a rect where fill rule @@ -1350,9 +1215,9 @@ pub unsafe extern "C" fn pdf_dev_flushpath(mut p_op: i8, mut fill_rule: i32) -> } #[no_mangle] pub unsafe extern "C" fn pdf_dev_newpath() -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut p = &mut (*gs).path; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let p = &mut gs.path; if !p.path.is_empty() { p.path.clear(); } @@ -1362,10 +1227,10 @@ pub unsafe extern "C" fn pdf_dev_newpath() -> i32 { } #[no_mangle] pub unsafe extern "C" fn pdf_dev_moveto(mut x: f64, mut y: f64) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let mut p = pdf_coord::new(); p.x = x; p.y = y; @@ -1374,10 +1239,10 @@ pub unsafe extern "C" fn pdf_dev_moveto(mut x: f64, mut y: f64) -> i32 { } #[no_mangle] pub unsafe extern "C" fn pdf_dev_rmoveto(mut x: f64, mut y: f64) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let p = pdf_coord { x: cpt.x + x, y: cpt.y + y, @@ -1387,20 +1252,20 @@ pub unsafe extern "C" fn pdf_dev_rmoveto(mut x: f64, mut y: f64) -> i32 { } #[no_mangle] pub unsafe extern "C" fn pdf_dev_lineto(mut x: f64, mut y: f64) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let p0 = pdf_coord { x, y }; pdf_path__lineto(cpa, cpt, &p0) } #[no_mangle] pub unsafe extern "C" fn pdf_dev_rlineto(mut x: f64, mut y: f64) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; - let mut p0 = pdf_coord { + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; + let p0 = pdf_coord { x: x + cpt.x, y: y + cpt.y, }; @@ -1415,10 +1280,10 @@ pub unsafe extern "C" fn pdf_dev_curveto( mut x2: f64, mut y2: f64, ) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let p0 = pdf_coord { x: x0, y: y0 }; let p1 = pdf_coord { x: x1, y: y1 }; let p2 = pdf_coord { x: x2, y: y2 }; @@ -1431,10 +1296,10 @@ pub unsafe extern "C" fn pdf_dev_vcurveto( mut x1: f64, mut y1: f64, ) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let cpt_clone = cpt.clone(); let p0 = pdf_coord { x: x0, y: y0 }; let p1 = pdf_coord { x: x1, y: y1 }; @@ -1447,10 +1312,10 @@ pub unsafe extern "C" fn pdf_dev_ycurveto( mut x1: f64, mut y1: f64, ) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let p0 = pdf_coord { x: x0, y: y0 }; let p1 = pdf_coord { x: x1, y: y1 }; pdf_path__curveto(cpa, cpt, &p0, &p1, &p1) @@ -1464,10 +1329,10 @@ pub unsafe extern "C" fn pdf_dev_rcurveto( mut x2: f64, mut y2: f64, ) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let p0 = pdf_coord { x: x0 + cpt.x, y: y0 + cpt.y, @@ -1484,49 +1349,58 @@ pub unsafe extern "C" fn pdf_dev_rcurveto( } #[no_mangle] pub unsafe extern "C" fn pdf_dev_closepath() -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpt = &mut (*gs).cp; - let mut cpa = &mut (*gs).path; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpt = &mut gs.cp; + let cpa = &mut gs.path; pdf_path__closepath(cpa, cpt) } #[no_mangle] pub unsafe extern "C" fn pdf_dev_dtransform(p: &mut pdf_coord, mut M: Option<&pdf_tmatrix>) { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut CTM = &mut (*gs).matrix; - pdf_coord__dtransform(p, if let Some(m) = M { m } else { CTM }); + if let Some(m) = M { + pdf_coord__dtransform(p, m); + } else { + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + pdf_coord__dtransform(p, &mut gs.matrix); + } } #[no_mangle] pub unsafe extern "C" fn pdf_dev_idtransform(p: &mut pdf_coord, M: Option<&pdf_tmatrix>) { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut CTM = &mut (*gs).matrix; - pdf_coord__idtransform(p, if let Some(m) = M { m } else { CTM }); + if let Some(m) = M { + pdf_coord__idtransform(p, m); + } else { + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + pdf_coord__idtransform(p, &mut gs.matrix); + } } #[no_mangle] pub unsafe extern "C" fn pdf_dev_transform(p: &mut pdf_coord, M: Option<&pdf_tmatrix>) { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut CTM = &mut (*gs).matrix; - pdf_coord__transform(p, if let Some(m) = M { m } else { CTM }); + if let Some(m) = M { + pdf_coord__transform(p, m); + } else { + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + pdf_coord__transform(p, &mut gs.matrix); + } } #[no_mangle] pub unsafe extern "C" fn pdf_dev_arc(c_x: f64, c_y: f64, r: f64, a_0: f64, a_1: f64) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let mut c = pdf_coord { x: c_x, y: c_y }; pdf_path__elliptarc(cpa, cpt, &mut c, r, r, 0.0f64, a_0, a_1, 1i32) } /* *negative* arc */ #[no_mangle] pub unsafe extern "C" fn pdf_dev_arcn(c_x: f64, c_y: f64, r: f64, a_0: f64, a_1: f64) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let mut c = pdf_coord { x: c_x, y: c_y }; pdf_path__elliptarc(cpa, cpt, &mut c, r, r, 0.0f64, a_0, a_1, -1i32) } @@ -1541,10 +1415,10 @@ pub unsafe extern "C" fn pdf_dev_arcx( a_d: i32, xar: f64, ) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let mut c = pdf_coord { x: c_x, y: c_y }; pdf_path__elliptarc(cpa, cpt, &mut c, r_x, r_y, xar, a_0, a_1, a_d) } @@ -1558,10 +1432,10 @@ pub unsafe extern "C" fn pdf_dev_bspline( x2: f64, y2: f64, ) -> i32 { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - let mut cpa = &mut (*gs).path; - let mut cpt = &mut (*gs).cp; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + let cpa = &mut gs.path; + let cpt = &mut gs.cp; let p1 = pdf_coord { x: x0 + 2. * (x1 - x0) / 3., y: y0 + 2. * (y1 - y0) / 3., @@ -1606,10 +1480,10 @@ pub unsafe extern "C" fn pdf_dev_rectadd(x: f64, y: f64, w: f64, h: f64) -> i32 } #[no_mangle] pub unsafe extern "C" fn pdf_dev_set_fixed_point(mut x: f64, mut y: f64) { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - (*gs).pt_fixee.x = x; - (*gs).pt_fixee.y = y; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + gs.pt_fixee.x = x; + gs.pt_fixee.y = y; } /* m -> n x m */ /* Path Construction */ @@ -1626,8 +1500,7 @@ pub unsafe extern "C" fn pdf_dev_set_fixed_point(mut x: f64, mut y: f64) { */ #[no_mangle] pub unsafe extern "C" fn pdf_dev_get_fixed_point(p: &mut pdf_coord) { - let mut gss: *mut m_stack = &mut gs_stack; - let mut gs: *mut pdf_gstate = m_stack_top(gss) as *mut pdf_gstate; - p.x = (*gs).pt_fixee.x; - p.y = (*gs).pt_fixee.y; + let mut gss = gs_stack.lock().unwrap(); + let gs = gss.top(); + *p = gs.pt_fixee.clone(); } diff --git a/dpx/src/dpx_spc_color.rs b/dpx/src/dpx_spc_color.rs index 9eea1d7da..7f5c22669 100644 --- a/dpx/src/dpx_spc_color.rs +++ b/dpx/src/dpx_spc_color.rs @@ -73,7 +73,7 @@ unsafe extern "C" fn spc_handler_color_push(mut spe: *mut spc_env, mut args: *mu let mut error: i32 = 0; let mut colorspec: pdf_color = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; error = spc_util_read_colorspec(spe, &mut colorspec, args, 1i32); @@ -97,7 +97,7 @@ unsafe extern "C" fn spc_handler_color_default( let mut error: i32 = 0; let mut colorspec: pdf_color = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; error = spc_util_read_colorspec(spe, &mut colorspec, args, 1i32); @@ -112,7 +112,7 @@ unsafe extern "C" fn spc_handler_background(mut spe: *mut spc_env, mut args: *mu let mut error: i32 = 0; let mut colorspec = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; error = spc_util_read_colorspec(spe, &mut colorspec, args, 1i32); diff --git a/dpx/src/dpx_spc_dvips.rs b/dpx/src/dpx_spc_dvips.rs index e03041af5..e43510494 100644 --- a/dpx/src/dpx_spc_dvips.rs +++ b/dpx/src/dpx_spc_dvips.rs @@ -38,9 +38,7 @@ use super::dpx_pdfximage::pdf_ximage_findresource; use crate::dpx_pdfobj::pdf_obj; use crate::{ttstub_input_close, ttstub_input_open}; -use super::dpx_pdfdev::{ - pdf_dev_put_image, pdf_tmatrix, transform_info, transform_info_clear, -}; +use super::dpx_pdfdev::{pdf_dev_put_image, pdf_tmatrix, transform_info, transform_info_clear}; use super::dpx_spc_util::spc_util_read_dimtrns; use libc::free; extern "C" { diff --git a/dpx/src/dpx_spc_misc.rs b/dpx/src/dpx_spc_misc.rs index ca3a89c87..b78d92b77 100644 --- a/dpx/src/dpx_spc_misc.rs +++ b/dpx/src/dpx_spc_misc.rs @@ -30,9 +30,7 @@ )] use super::dpx_mpost::mps_scan_bbox; -use super::dpx_pdfdev::{ - pdf_dev_put_image, transform_info, transform_info_clear, -}; +use super::dpx_pdfdev::{pdf_dev_put_image, transform_info, transform_info_clear}; use super::dpx_pdfximage::pdf_ximage_findresource; use crate::dpx_pdfobj::pdf_obj; use crate::{ttstub_input_close, ttstub_input_open}; diff --git a/dpx/src/dpx_spc_pdfm.rs b/dpx/src/dpx_spc_pdfm.rs index eb55b1f4f..77c72e92a 100644 --- a/dpx/src/dpx_spc_pdfm.rs +++ b/dpx/src/dpx_spc_pdfm.rs @@ -952,19 +952,19 @@ unsafe extern "C" fn spc_handler_pdfm_bcolor(mut spe: *mut spc_env, mut ap: *mut let mut error: i32 = 0; let mut fc: pdf_color = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; let mut sc: pdf_color = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; let (psc, pfc) = pdf_color_get_current(); - error = spc_util_read_pdfcolor(spe, &mut fc, ap, pfc); + error = spc_util_read_pdfcolor(spe, &mut fc, ap, Some(pfc)); if error == 0 { if (*ap).curptr < (*ap).endptr { - error = spc_util_read_pdfcolor(spe, &mut sc, ap, psc) + error = spc_util_read_pdfcolor(spe, &mut sc, ap, Some(psc)) } else { pdf_color_copycolor(&mut sc, &mut fc); } @@ -988,19 +988,19 @@ unsafe extern "C" fn spc_handler_pdfm_scolor(mut spe: *mut spc_env, mut ap: *mut let mut error: i32 = 0; let mut fc: pdf_color = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; let mut sc: pdf_color = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; let (psc, pfc) = pdf_color_get_current(); - error = spc_util_read_pdfcolor(spe, &mut fc, ap, pfc); + error = spc_util_read_pdfcolor(spe, &mut fc, ap, Some(pfc)); if error == 0 { if (*ap).curptr < (*ap).endptr { - error = spc_util_read_pdfcolor(spe, &mut sc, ap, psc) + error = spc_util_read_pdfcolor(spe, &mut sc, ap, Some(psc)) } else { pdf_color_copycolor(&mut sc, &mut fc); } @@ -2105,10 +2105,10 @@ unsafe extern "C" fn spc_handler_pdfm_bgcolor( let mut error: i32 = 0; let mut colorspec = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; - error = spc_util_read_pdfcolor(spe, &mut colorspec, args, 0 as *mut pdf_color); + error = spc_util_read_pdfcolor(spe, &mut colorspec, args, None); if error != 0 { spc_warn( spe, diff --git a/dpx/src/dpx_spc_tpic.rs b/dpx/src/dpx_spc_tpic.rs index ebc973232..c48ed128e 100644 --- a/dpx/src/dpx_spc_tpic.rs +++ b/dpx/src/dpx_spc_tpic.rs @@ -284,7 +284,7 @@ unsafe extern "C" fn set_fillstyle(mut g: f64, mut a: f64, mut f_ais: i32) -> i3 } /* get stroking and fill colors */ let mut new_fc: pdf_color = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; let (sc, fc) = pdf_color_get_current(); diff --git a/dpx/src/dpx_spc_util.rs b/dpx/src/dpx_spc_util.rs index 874173e30..eb06dc71d 100644 --- a/dpx/src/dpx_spc_util.rs +++ b/dpx/src/dpx_spc_util.rs @@ -64,7 +64,7 @@ use super::dpx_pdfdev::{pdf_tmatrix, transform_info}; use super::dpx_specials::{spc_arg, spc_env}; /* Color names */ -#[derive(Copy, Clone)] +#[derive(Clone)] #[repr(C)] pub struct colordef_ { pub key: *const i8, @@ -368,7 +368,7 @@ pub unsafe extern "C" fn spc_util_read_pdfcolor( mut spe: *mut spc_env, colorspec: &mut pdf_color, mut ap: *mut spc_arg, - mut defaultcolor: *mut pdf_color, + defaultcolor: Option<&pdf_color>, ) -> i32 { let mut error: i32 = 0i32; assert!(!spe.is_null() && !ap.is_null()); @@ -377,9 +377,11 @@ pub unsafe extern "C" fn spc_util_read_pdfcolor( return -1i32; } error = spc_read_color_pdf(spe, colorspec, ap); - if error < 0i32 && !defaultcolor.is_null() { - pdf_color_copycolor(colorspec, defaultcolor); - error = 0i32 + if error < 0i32 { + if let Some(dc) = defaultcolor { + pdf_color_copycolor(colorspec, dc); + error = 0i32 + } } error } @@ -1112,7 +1114,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.15f64, 0.00f64, 0.69f64, 0.00f64], }; init @@ -1126,7 +1128,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.00f64, 1.00f64, 0.00f64], }; init @@ -1140,7 +1142,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.10f64, 0.84f64, 0.00f64], }; init @@ -1154,7 +1156,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.29f64, 0.84f64, 0.00f64], }; init @@ -1168,7 +1170,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.32f64, 0.52f64, 0.00f64], }; init @@ -1182,7 +1184,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.50f64, 0.70f64, 0.00f64], }; init @@ -1196,7 +1198,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.46f64, 0.50f64, 0.00f64], }; init @@ -1210,7 +1212,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.42f64, 1.00f64, 0.00f64], }; init @@ -1224,7 +1226,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.61f64, 0.87f64, 0.00f64], }; init @@ -1238,7 +1240,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.51f64, 1.00f64, 0.00f64], }; init @@ -1252,7 +1254,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.75f64, 1.00f64, 0.24f64], }; init @@ -1266,7 +1268,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.77f64, 0.87f64, 0.00f64], }; init @@ -1280,7 +1282,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.85f64, 0.87f64, 0.35f64], }; init @@ -1294,7 +1296,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.87f64, 0.68f64, 0.32f64], }; init @@ -1308,7 +1310,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.89f64, 0.94f64, 0.28f64], }; init @@ -1322,7 +1324,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 1.00f64, 1.00f64, 0.00f64], }; init @@ -1336,7 +1338,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 1.00f64, 0.50f64, 0.00f64], }; init @@ -1350,7 +1352,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 1.00f64, 0.13f64, 0.00f64], }; init @@ -1364,7 +1366,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.96f64, 0.39f64, 0.00f64], }; init @@ -1378,7 +1380,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.53f64, 0.38f64, 0.00f64], }; init @@ -1392,7 +1394,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.63f64, 0.00f64, 0.00f64], }; init @@ -1406,7 +1408,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 1.00f64, 0.00f64, 0.00f64], }; init @@ -1420,7 +1422,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.81f64, 0.00f64, 0.00f64], }; init @@ -1434,7 +1436,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.82f64, 0.00f64, 0.00f64], }; init @@ -1448,7 +1450,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.34f64, 0.90f64, 0.00f64, 0.02f64], }; init @@ -1462,7 +1464,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.07f64, 0.90f64, 0.00f64, 0.34f64], }; init @@ -1476,7 +1478,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.47f64, 0.91f64, 0.00f64, 0.08f64], }; init @@ -1490,7 +1492,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.48f64, 0.00f64, 0.00f64], }; init @@ -1504,7 +1506,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.12f64, 0.59f64, 0.00f64, 0.00f64], }; init @@ -1518,7 +1520,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.32f64, 0.64f64, 0.00f64, 0.00f64], }; init @@ -1532,7 +1534,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.40f64, 0.80f64, 0.20f64, 0.00f64], }; init @@ -1546,7 +1548,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.45f64, 0.86f64, 0.00f64, 0.00f64], }; init @@ -1560,7 +1562,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.50f64, 1.00f64, 0.00f64, 0.00f64], }; init @@ -1574,7 +1576,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.79f64, 0.88f64, 0.00f64, 0.00f64], }; init @@ -1588,7 +1590,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.75f64, 0.90f64, 0.00f64, 0.00f64], }; init @@ -1602,7 +1604,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.86f64, 0.91f64, 0.00f64, 0.04f64], }; init @@ -1616,7 +1618,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.57f64, 0.55f64, 0.00f64, 0.00f64], }; init @@ -1630,7 +1632,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.62f64, 0.57f64, 0.23f64, 0.00f64], }; init @@ -1644,7 +1646,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.65f64, 0.13f64, 0.00f64, 0.00f64], }; init @@ -1658,7 +1660,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.98f64, 0.13f64, 0.00f64, 0.43f64], }; init @@ -1672,7 +1674,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.94f64, 0.54f64, 0.00f64, 0.00f64], }; init @@ -1686,7 +1688,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [1.00f64, 0.50f64, 0.00f64, 0.00f64], }; init @@ -1700,7 +1702,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [1.00f64, 1.00f64, 0.00f64, 0.00f64], }; init @@ -1714,7 +1716,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.94f64, 0.11f64, 0.00f64, 0.00f64], }; init @@ -1728,7 +1730,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [1.00f64, 0.00f64, 0.00f64, 0.00f64], }; init @@ -1742,7 +1744,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.96f64, 0.00f64, 0.00f64, 0.00f64], }; init @@ -1756,7 +1758,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.62f64, 0.00f64, 0.12f64, 0.00f64], }; init @@ -1770,7 +1772,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.85f64, 0.00f64, 0.20f64, 0.00f64], }; init @@ -1784,7 +1786,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.86f64, 0.00f64, 0.34f64, 0.02f64], }; init @@ -1798,7 +1800,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.82f64, 0.00f64, 0.30f64, 0.00f64], }; init @@ -1812,7 +1814,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.85f64, 0.00f64, 0.33f64, 0.00f64], }; init @@ -1826,7 +1828,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [1.00f64, 0.00f64, 0.50f64, 0.00f64], }; init @@ -1840,7 +1842,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.99f64, 0.00f64, 0.52f64, 0.00f64], }; init @@ -1854,7 +1856,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.69f64, 0.00f64, 0.50f64, 0.00f64], }; init @@ -1868,7 +1870,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [1.00f64, 0.00f64, 1.00f64, 0.00f64], }; init @@ -1882,7 +1884,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.91f64, 0.00f64, 0.88f64, 0.12f64], }; init @@ -1896,7 +1898,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.92f64, 0.00f64, 0.59f64, 0.25f64], }; init @@ -1910,7 +1912,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.50f64, 0.00f64, 1.00f64, 0.00f64], }; init @@ -1924,7 +1926,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.44f64, 0.00f64, 0.74f64, 0.00f64], }; init @@ -1938,7 +1940,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.26f64, 0.00f64, 0.76f64, 0.00f64], }; init @@ -1952,7 +1954,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.64f64, 0.00f64, 0.95f64, 0.40f64], }; init @@ -1966,7 +1968,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.72f64, 1.00f64, 0.45f64], }; init @@ -1980,7 +1982,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.83f64, 1.00f64, 0.70f64], }; init @@ -1994,7 +1996,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.00f64, 0.81f64, 1.00f64, 0.60f64], }; init @@ -2008,7 +2010,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 4i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.14f64, 0.42f64, 0.56f64, 0.00f64], }; init @@ -2022,7 +2024,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 1i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.5f64, 0., 0., 0.], }; init @@ -2036,7 +2038,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 1i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.0f64, 0., 0., 0.], }; init @@ -2050,7 +2052,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 1i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [1.0f64, 0., 0., 0.], }; init @@ -2064,7 +2066,7 @@ static mut colordefs: [colordef_; 69] = [ color: { let mut init = pdf_color { num_components: 0i32, - spot_color_name: 0 as *const i8 as *mut i8, + spot_color_name: None, values: [0.0f64, 0., 0., 0.], }; init diff --git a/dpx/src/dpx_spc_xtx.rs b/dpx/src/dpx_spc_xtx.rs index 50ae62bc2..78006becf 100644 --- a/dpx/src/dpx_spc_xtx.rs +++ b/dpx/src/dpx_spc_xtx.rs @@ -283,7 +283,7 @@ unsafe extern "C" fn spc_handler_xtx_backgroundcolor( let mut error: i32 = 0; let mut colorspec = pdf_color { num_components: 0, - spot_color_name: 0 as *mut i8, + spot_color_name: None, values: [0.; 4], }; error = spc_util_read_colorspec(spe, &mut colorspec, args, 0i32); diff --git a/dpx/src/lib.rs b/dpx/src/lib.rs index 358f034e3..64e3d8c4c 100644 --- a/dpx/src/lib.rs +++ b/dpx/src/lib.rs @@ -1,6 +1,8 @@ #![feature(extern_types)] #![feature(ptr_wrapping_offset_from)] #![feature(c_variadic)] +#![feature(const_transmute)] +#![allow(unused_unsafe)] extern crate tectonic_bridge as bridge; use bridge::*; @@ -8,9 +10,9 @@ use bridge::*; #[macro_export] macro_rules! info( ($($arg:tt)*) => { - if !(crate::dpx_error::_dpx_quietness > 0) { + if !(unsafe{crate::dpx_error::_dpx_quietness} > 0) { print!($($arg)*); - crate::dpx_error::_last_message_type = crate::dpx_error::DPX_MESG_INFO; + unsafe{crate::dpx_error::_last_message_type = crate::dpx_error::DPX_MESG_INFO;} } }; ); @@ -18,13 +20,13 @@ macro_rules! info( #[macro_export] macro_rules! warn( ($($arg:tt)*) => { - if !(crate::dpx_error::_dpx_quietness > 1) { - if crate::dpx_error::_last_message_type as u32 == crate::dpx_error::DPX_MESG_INFO as u32 { + if !(unsafe{crate::dpx_error::_dpx_quietness} > 1) { + if unsafe{crate::dpx_error::_last_message_type as u32 == crate::dpx_error::DPX_MESG_INFO as u32} { println!(""); } print!("warning: "); println!($($arg)*); - crate::dpx_error::_last_message_type = crate::dpx_error::DPX_MESG_WARN; + unsafe{crate::dpx_error::_last_message_type = crate::dpx_error::DPX_MESG_WARN;} } }; ); diff --git a/engine/src/lib.rs b/engine/src/lib.rs index db15a1158..f11140b87 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -8,7 +8,8 @@ non_snake_case, non_upper_case_globals, unused_assignments, - unused_mut + unused_mut, + unused_unsafe )] #[macro_use] extern crate tectonic_bridge as bridge; From a70c783c4265c9a20ad87b1237a144c6b5765181 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sun, 15 Sep 2019 06:45:15 +0300 Subject: [PATCH 4/7] restore pdf_coord__equal --- dpx/src/dpx_pdfdev.rs | 5 ----- dpx/src/dpx_pdfdraw.rs | 20 ++++++++++++-------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/dpx/src/dpx_pdfdev.rs b/dpx/src/dpx_pdfdev.rs index a3c43db70..94bf76243 100644 --- a/dpx/src/dpx_pdfdev.rs +++ b/dpx/src/dpx_pdfdev.rs @@ -172,11 +172,6 @@ impl pdf_coord { Self { x: 0., y: 0. } } } -impl PartialEq for pdf_coord { - fn eq(&self, other: &Self) -> bool { - (self.x - other.x).abs() < 1e-7 && (self.y - other.y).abs() < 1e-7 - } -} #[derive(Copy, Clone, Default)] #[repr(C)] diff --git a/dpx/src/dpx_pdfdraw.rs b/dpx/src/dpx_pdfdraw.rs index 0c13d39b9..31c170443 100644 --- a/dpx/src/dpx_pdfdraw.rs +++ b/dpx/src/dpx_pdfdraw.rs @@ -114,6 +114,10 @@ pub struct LineDash { pub offset: f64, } +fn pdf_coord__equal(p1: &pdf_coord, p2: &pdf_coord) -> bool { + ((p1.x - p2.x).abs() < 1e-7) && ((p1.y - p2.y).abs() < 1e-7) +} + unsafe extern "C" fn inversematrix(mut W: &mut pdf_tmatrix, mut M: &pdf_tmatrix) -> i32 { let mut det: f64 = 0.; det = M.a * M.d - M.b * M.c; @@ -283,21 +287,21 @@ unsafe extern "C" fn pdf_path__next_pe<'a>( pe.p[0] = *cp; } PeType::LINETO => { - if &pe.p[0] != cp { + if !pdf_coord__equal(&pe.p[0], cp) { let mut pe = pa_elem::default(); pe.p[0] = *cp; pa.path.push(pe); } } PeType::CURVETO => { - if &pe.p[2] != cp { + if !pdf_coord__equal(&pe.p[2], cp) { let mut pe = pa_elem::default(); pe.p[0] = *cp; pa.path.push(pe); } } PeType::CURVETO_Y | PeType::CURVETO_V => { - if &pe.p[1] != cp { + if !pdf_coord__equal(&pe.p[1], cp) { let mut pe = pa_elem::default(); pe.p[0] = *cp; pa.path.push(pe); @@ -358,12 +362,12 @@ unsafe extern "C" fn pdf_path__curveto( p2: &pdf_coord, ) -> i32 { let pe = pdf_path__next_pe(pa, cp); - if cp == p0 { + if pdf_coord__equal(cp, p0) { pe.typ = PeType::CURVETO_V; pe.p[0] = *p1; *cp = *p2; pe.p[1] = *cp; - } else if p1 == p2 { + } else if pdf_coord__equal(p1, p2) { pe.typ = PeType::CURVETO_Y; pe.p[0] = *p0; *cp = *p1; @@ -449,7 +453,7 @@ unsafe extern "C" fn pdf_path__elliptarc( p0.y += ca.y; if pa.path.is_empty() { pdf_path__moveto(pa, cp, &mut p0); - } else if cp != &p0 { + } else if !pdf_coord__equal(cp, &p0) { pdf_path__lineto(pa, cp, &mut p0); /* add line seg */ } @@ -823,8 +827,8 @@ impl pdf_gstate { strokecolor: pdf_color_graycolor_new(0.).unwrap(), fillcolor: pdf_color_graycolor_new(0.).unwrap(), linedash: LineDash::default(), - linecap: 0, - linejoin: 0, + linecap: 0, // TODO make enum + linejoin: 0, // TODO make enum linewidth: 1., miterlimit: 10., flatness: 1, From f6c3eaa14b21bc361a947318b0a559849ff60762 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sun, 15 Sep 2019 06:53:38 +0300 Subject: [PATCH 5/7] move mutex to start of file --- dpx/src/dpx_pdfdraw.rs | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/dpx/src/dpx_pdfdraw.rs b/dpx/src/dpx_pdfdraw.rs index 31c170443..52e12aaa9 100644 --- a/dpx/src/dpx_pdfdraw.rs +++ b/dpx/src/dpx_pdfdraw.rs @@ -37,6 +37,16 @@ use super::dpx_pdfcolor::{ }; use super::dpx_pdfdev::{pdf_sprint_coord, pdf_sprint_matrix, pdf_sprint_rect}; +use lazy_static::lazy_static; +use std::sync::Mutex; + +lazy_static! { // TODO move to context structure + static ref gs_stack: Mutex> = Mutex::new({ + let mut v = vec![]; + v + }); +} + extern "C" { #[no_mangle] fn graphics_mode(); @@ -809,16 +819,6 @@ impl Top for Vec { } } -use lazy_static::lazy_static; -use std::sync::Mutex; - -lazy_static! { - static ref gs_stack: Mutex> = Mutex::new({ - let mut v = vec![]; - v - }); -} - impl pdf_gstate { pub fn init() -> Self { Self { @@ -839,14 +839,7 @@ impl pdf_gstate { } } } -/*unsafe extern "C" fn clear_a_gstate(mut gs: *mut pdf_gstate) { - //clear_a_path(&mut (*gs).path); - memset( - gs as *mut libc::c_void, - 0i32, - ::std::mem::size_of::() as u64, - ); -}*/ + unsafe extern "C" fn copy_a_gstate(gs1: &mut pdf_gstate, gs2: &pdf_gstate) { let mut i: i32 = 0; gs1.cp = gs2.cp; From a52a109c713461ce548ea14971419578c8d9f4e3 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sun, 15 Sep 2019 08:17:11 +0300 Subject: [PATCH 6/7] fix some unused warnings --- dpx/src/dpx_cidtype0.rs | 95 +----------- dpx/src/dpx_cidtype2.rs | 18 +-- dpx/src/dpx_cmap_read.rs | 17 +-- dpx/src/dpx_cmap_write.rs | 16 +- dpx/src/dpx_cs_type2.rs | 24 +++ dpx/src/dpx_dpxfile.rs | 8 +- dpx/src/dpx_dvi.rs | 47 +----- dpx/src/dpx_otl_conf.rs | 8 +- dpx/src/dpx_pdfdev.rs | 18 +-- dpx/src/dpx_pdfdraw.rs | 20 +-- dpx/src/dpx_pdfobj.rs | 14 +- dpx/src/dpx_pdfparse.rs | 6 +- dpx/src/dpx_pngimage.rs | 303 +++++++++++++++----------------------- dpx/src/dpx_spc_pdfm.rs | 1 - dpx/src/dpx_specials.rs | 1 - dpx/src/dpx_t1_char.rs | 22 +++ dpx/src/dpx_type0.rs | 8 - dpx/src/dpx_type1.rs | 67 +-------- dpx/src/dpx_type1c.rs | 47 +----- dpx/src/lib.rs | 8 +- 20 files changed, 226 insertions(+), 522 deletions(-) diff --git a/dpx/src/dpx_cidtype0.rs b/dpx/src/dpx_cidtype0.rs index a5d360dab..a6e4e2b6e 100644 --- a/dpx/src/dpx_cidtype0.rs +++ b/dpx/src/dpx_cidtype0.rs @@ -218,35 +218,10 @@ pub type rust_input_handle_t = *mut libc::c_void; use super::dpx_cid::{cid_opt, CIDFont, CIDSysInfo}; use super::dpx_sfnt::sfnt; +use super::dpx_cs_type2::cs_ginfo; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct cs_ginfo { - pub flags: i32, - pub wx: f64, - pub wy: f64, - pub bbox: C2RustUnnamed_0, - pub seac: C2RustUnnamed, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed { - pub asb: f64, - pub adx: f64, - pub ady: f64, - pub bchar: card8, - pub achar: card8, -} -pub type card8 = u8; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_0 { - pub llx: f64, - pub lly: f64, - pub urx: f64, - pub ury: f64, -} use super::dpx_cff::cff_charsets; +pub type card8 = u8; pub type card16 = u16; pub type s_SID = u16; @@ -315,32 +290,8 @@ use super::dpx_cmap::CMap; /* Upper bounds of valid input code */ use super::dpx_agl::agl_name; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct t1_ginfo { - pub use_seac: i32, - pub wx: f64, - pub wy: f64, - pub bbox: C2RustUnnamed_7, - pub seac: C2RustUnnamed_6, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_6 { - pub asb: f64, - pub adx: f64, - pub ady: f64, - pub bchar: card8, - pub achar: card8, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_7 { - pub llx: f64, - pub lly: f64, - pub urx: f64, - pub ury: f64, -} +use super::dpx_t1_char::t1_ginfo; + /* * CID-Keyed Font support: * @@ -2237,24 +2188,7 @@ unsafe extern "C" fn get_font_attr(mut font: *mut CIDFont, mut cffont: *mut cff_ b"lambda\x00" as *const u8 as *const i8, 0 as *const i8, ]; - let mut gm: t1_ginfo = t1_ginfo { - use_seac: 0, - wx: 0., - wy: 0., - bbox: C2RustUnnamed_7 { - llx: 0., - lly: 0., - urx: 0., - ury: 0., - }, - seac: C2RustUnnamed_6 { - asb: 0., - adx: 0., - ady: 0., - bchar: 0, - achar: 0, - }, - }; + let mut gm = t1_ginfo::new(); defaultwidth = 500.0f64; nominalwidth = 0.0f64; /* @@ -2837,24 +2771,7 @@ pub unsafe extern "C" fn CIDFont_type0_t1dofont(mut font: *mut CIDFont) { 0.0f64, ); let mut cstring: *mut cff_index = 0 as *mut cff_index; - let mut gm: t1_ginfo = t1_ginfo { - use_seac: 0, - wx: 0., - wy: 0., - bbox: C2RustUnnamed_7 { - llx: 0., - lly: 0., - urx: 0., - ury: 0., - }, - seac: C2RustUnnamed_6 { - asb: 0., - adx: 0., - ady: 0., - bchar: 0, - achar: 0, - }, - }; + let mut gm = t1_ginfo::new(); let mut max: i32 = 0i32; let mut widths: *mut f64 = 0 as *mut f64; let mut w_stat: [i32; 1001] = [0; 1001]; diff --git a/dpx/src/dpx_cidtype2.rs b/dpx/src/dpx_cidtype2.rs index 75b779bcb..446322343 100644 --- a/dpx/src/dpx_cidtype2.rs +++ b/dpx/src/dpx_cidtype2.rs @@ -182,27 +182,11 @@ use super::dpx_cid::{cid_opt, CIDFont, CIDSysInfo}; use super::dpx_cmap::CMap; use super::dpx_tt_cmap::tt_cmap; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed { - pub minBytesIn: size_t, - pub maxBytesIn: size_t, - pub minBytesOut: size_t, - pub maxBytesOut: size_t, -} + /* 2 for CID, variable for Code.. */ /* CID (as 16-bit BE), Code ... */ /* Next Subtbl for LOOKUP_CONTINUE */ -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_0 { - pub num: u32, - pub max: u32, - pub ranges: *mut rangeDef, -} -use super::dpx_cmap::rangeDef; - use super::dpx_sfnt::sfnt; pub type CID = u16; diff --git a/dpx/src/dpx_cmap_read.rs b/dpx/src/dpx_cmap_read.rs index c420247f5..8e6830e44 100644 --- a/dpx/src/dpx_cmap_read.rs +++ b/dpx/src/dpx_cmap_read.rs @@ -90,23 +90,8 @@ use super::dpx_cid::CIDSysInfo; /* DEBUG */ /* Codespacerange */ -use super::dpx_cmap::rangeDef; use super::dpx_cmap::CMap; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed { - pub minBytesIn: size_t, - pub maxBytesIn: size_t, - pub minBytesOut: size_t, - pub maxBytesOut: size_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_0 { - pub num: u32, - pub max: u32, - pub ranges: *mut rangeDef, -} + pub type CID = u16; #[derive(Copy, Clone)] #[repr(C)] diff --git a/dpx/src/dpx_cmap_write.rs b/dpx/src/dpx_cmap_write.rs index ad03608d9..a9a68087a 100644 --- a/dpx/src/dpx_cmap_write.rs +++ b/dpx/src/dpx_cmap_write.rs @@ -81,21 +81,7 @@ use super::dpx_cid::CIDSysInfo; use super::dpx_cmap::mapDef; use super::dpx_cmap::rangeDef; use super::dpx_cmap::CMap; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed { - pub minBytesIn: size_t, - pub maxBytesIn: size_t, - pub minBytesOut: size_t, - pub maxBytesOut: size_t, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_0 { - pub num: u32, - pub max: u32, - pub ranges: *mut rangeDef, -} + /* * References: * diff --git a/dpx/src/dpx_cs_type2.rs b/dpx/src/dpx_cs_type2.rs index aaf616166..ae65168f1 100644 --- a/dpx/src/dpx_cs_type2.rs +++ b/dpx/src/dpx_cs_type2.rs @@ -50,6 +50,30 @@ pub struct cs_ginfo { pub bbox: C2RustUnnamed_0, pub seac: C2RustUnnamed, } + +impl cs_ginfo { + pub fn new() -> Self { + Self { + flags: 0, + wx: 0., + wy: 0., + bbox: C2RustUnnamed_0 { + llx: 0., + lly: 0., + urx: 0., + ury: 0., + }, + seac: C2RustUnnamed { + asb: 0., + adx: 0., + ady: 0., + bchar: 0, + achar: 0, + }, + } + } +} + #[derive(Copy, Clone)] #[repr(C)] pub struct C2RustUnnamed { diff --git a/dpx/src/dpx_dpxfile.rs b/dpx/src/dpx_dpxfile.rs index 95d8e65ac..6ffda6cb4 100644 --- a/dpx/src/dpx_dpxfile.rs +++ b/dpx/src/dpx_dpxfile.rs @@ -382,10 +382,10 @@ pub unsafe extern "C" fn dpx_delete_temp_file(mut tmp: *mut i8, mut force: i32) */ #[no_mangle] pub unsafe extern "C" fn dpx_file_apply_filter( - mut cmdtmpl: *const i8, - mut input: *const i8, - mut output: *const i8, - mut version: u8, + mut _cmdtmpl: *const i8, + mut _input: *const i8, + mut _output: *const i8, + mut _version: u8, ) -> i32 { /* Tectonic: defused */ -1i32 diff --git a/dpx/src/dpx_dvi.rs b/dpx/src/dpx_dvi.rs index 698ef0239..02db510f4 100644 --- a/dpx/src/dpx_dvi.rs +++ b/dpx/src/dpx_dvi.rs @@ -341,32 +341,8 @@ pub struct dvi_lr { pub font: i32, pub buf_index: u32, } -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_3 { - pub llx: f64, - pub lly: f64, - pub urx: f64, - pub ury: f64, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct t1_ginfo { - pub use_seac: i32, - pub wx: f64, - pub wy: f64, - pub bbox: C2RustUnnamed_3, - pub seac: C2RustUnnamed_4, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_4 { - pub asb: f64, - pub adx: f64, - pub ady: f64, - pub bchar: card8, - pub achar: card8, -} + +use super::dpx_t1_char::t1_ginfo; use super::dpx_sfnt::sfnt; use super::dpx_tt_table::{tt_head_table, tt_hhea_table, tt_maxp_table}; @@ -2327,24 +2303,7 @@ unsafe extern "C" fn do_glyphs(mut do_actual_text: i32) { let mut descent: f64 = (*font).descent as f64; if !(*font).cffont.is_null() { let mut cstrings: *mut cff_index = (*(*font).cffont).cstrings; - let mut gm: t1_ginfo = t1_ginfo { - use_seac: 0, - wx: 0., - wy: 0., - bbox: C2RustUnnamed_3 { - llx: 0., - lly: 0., - urx: 0., - ury: 0., - }, - seac: C2RustUnnamed_4 { - asb: 0., - adx: 0., - ady: 0., - bchar: 0, - achar: 0, - }, - }; + let mut gm = t1_ginfo::new(); /* If .notdef is not the 1st glyph in CharStrings, glyph_id given by FreeType should be increased by 1 */ if (*(*font).cffont).is_notdef_notzero != 0 { diff --git a/dpx/src/dpx_otl_conf.rs b/dpx/src/dpx_otl_conf.rs index 48609c690..d19cfdc42 100644 --- a/dpx/src/dpx_otl_conf.rs +++ b/dpx/src/dpx_otl_conf.rs @@ -619,10 +619,10 @@ unsafe extern "C" fn otl_read_conf(mut conf_name: *const i8) -> *mut pdf_obj { static mut otl_confs: *mut pdf_obj = 0 as *const pdf_obj as *mut pdf_obj; #[no_mangle] pub unsafe extern "C" fn otl_find_conf(mut conf_name: *const i8) -> *mut pdf_obj { - let mut rule: *mut pdf_obj = 0 as *mut pdf_obj; - let mut script: *mut pdf_obj = 0 as *mut pdf_obj; - let mut language: *mut pdf_obj = 0 as *mut pdf_obj; - let mut options: *mut pdf_obj = 0 as *mut pdf_obj; + let mut _rule: *mut pdf_obj = 0 as *mut pdf_obj; + let mut _script: *mut pdf_obj = 0 as *mut pdf_obj; + let mut _language: *mut pdf_obj = 0 as *mut pdf_obj; + let mut _options: *mut pdf_obj = 0 as *mut pdf_obj; 0 as *mut pdf_obj } #[no_mangle] diff --git a/dpx/src/dpx_pdfdev.rs b/dpx/src/dpx_pdfdev.rs index 94bf76243..312cc0202 100644 --- a/dpx/src/dpx_pdfdev.rs +++ b/dpx/src/dpx_pdfdev.rs @@ -227,7 +227,7 @@ pub type card8 = u8; */ #[derive(Copy, Clone)] #[repr(C)] -pub struct C2RustUnnamed_0 { +pub struct DevUnit { pub dvi2pts: f64, pub min_bp_val: i32, pub precision: i32, @@ -235,7 +235,7 @@ pub struct C2RustUnnamed_0 { } #[derive(Copy, Clone)] #[repr(C)] -pub struct C2RustUnnamed_1 { +pub struct TextState { pub font_id: i32, pub offset: spt_t, pub ref_x: spt_t, @@ -257,7 +257,7 @@ pub struct C2RustUnnamed_2 { } #[derive(Copy, Clone)] #[repr(C)] -pub struct C2RustUnnamed_3 { +pub struct DevParam { pub autorotate: i32, pub colormode: i32, } @@ -285,8 +285,8 @@ pub unsafe extern "C" fn pdf_dev_set_verbose(mut level: i32) { pub unsafe extern "C" fn pdf_dev_scale() -> f64 { 1.0f64 } -static mut dev_unit: C2RustUnnamed_0 = { - let mut init = C2RustUnnamed_0 { +static mut dev_unit: DevUnit = { + let mut init = DevUnit { dvi2pts: 0.0f64, min_bp_val: 658i32, precision: 2i32, @@ -522,8 +522,8 @@ pub unsafe extern "C" fn pdf_sprint_number(mut buf: *mut i8, mut value: f64) -> *buf.offset(len as isize) = '\u{0}' as i32 as i8; len } -static mut dev_param: C2RustUnnamed_3 = { - let mut init = C2RustUnnamed_3 { +static mut dev_param: DevParam = { + let mut init = DevParam { autorotate: 1i32, colormode: 1i32, }; @@ -531,8 +531,8 @@ static mut dev_param: C2RustUnnamed_3 = { }; static mut motion_state: i32 = 1i32; static mut format_buffer: [i8; 4096] = [0; 4096]; -static mut text_state: C2RustUnnamed_1 = { - let mut init = C2RustUnnamed_1 { +static mut text_state: TextState = { + let mut init = TextState { font_id: -1i32, offset: 0i32, ref_x: 0i32, diff --git a/dpx/src/dpx_pdfdraw.rs b/dpx/src/dpx_pdfdraw.rs index 52e12aaa9..75719d978 100644 --- a/dpx/src/dpx_pdfdraw.rs +++ b/dpx/src/dpx_pdfdraw.rs @@ -151,8 +151,6 @@ extern "C" fn pdf_coord__transform(p: &mut pdf_coord, M: &pdf_tmatrix) -> i32 { 0i32 } extern "C" fn pdf_coord__dtransform(p: &mut pdf_coord, M: &pdf_tmatrix) -> i32 { - let mut x: f64 = 0.; - let mut y: f64 = 0.; let pdf_coord { x, y } = *p; p.x = x * M.a + y * M.c; p.y = x * M.b + y * M.d; @@ -160,8 +158,6 @@ extern "C" fn pdf_coord__dtransform(p: &mut pdf_coord, M: &pdf_tmatrix) -> i32 { } unsafe extern "C" fn pdf_coord__idtransform(p: &mut pdf_coord, M: &pdf_tmatrix) -> i32 { let mut W = pdf_tmatrix::new(); - let mut x: f64 = 0.; - let mut y: f64 = 0.; let mut error: i32 = 0; error = inversematrix(&mut W, M); if error != 0 { @@ -510,7 +506,6 @@ unsafe extern "C" fn pdf_path__elliptarc( } unsafe extern "C" fn pdf_path__closepath(pa: &mut pdf_path, cp: &mut pdf_coord) -> i32 /* no arg */ { - let mut pe: *mut pa_elem = 0 as *mut pa_elem; /* search for start point of the last subpath */ let pe = pa.path.iter().rev().find(|pe| pe.typ == PeType::MOVETO); @@ -703,21 +698,16 @@ unsafe extern "C" fn pdf_dev__flushpath( mut rule: i32, mut ignore_rule: i32, ) -> i32 { - let mut pe: *mut pa_elem = 0 as *mut pa_elem; /* FIXME */ - let mut pe1: *mut pa_elem = 0 as *mut pa_elem; /* width... */ let mut b: *mut i8 = fmt_buf.as_mut_ptr(); /* height... */ let mut b_len: i32 = 1024i32; /* op: re */ let mut r = pdf_rect::new(); /* op: m l c v y h */ - let mut pt: *mut pdf_coord = 0 as *mut pdf_coord; /* op: m l c v y h */ - let mut n_pts: i32 = 0; /* op: f F s S b B W f* F* s* S* b* B* W* */ let mut n_seg: i32 = 0; /* default to 1 in PDF */ let mut len: i32 = 0i32; - let mut isclip: i32 = 0i32; let mut isrect: i32 = 0; let mut i: i32 = 0; let mut j: i32 = 0; assert!([b'f', b'F', b's', b'S', b'b', b'B', b'W', b' '].contains(&(opchr as u8))); - isclip = if opchr == b'W' as i8 { 1i32 } else { 0i32 }; + let isclip = if opchr == b'W' as i8 { true } else { false }; if /*pa.num_paths <= 0_u32 &&*/ path_added == 0i32 { @@ -750,14 +740,14 @@ unsafe extern "C" fn pdf_dev__flushpath( len = 0i32 } else { for pe in pa.path.iter_mut() { - n_pts = if - /* !pe.is_null() &&*/ + /* op: f F s S b B W f* F* s* S* b* B* W* */ + let n_pts = if pe.typ != PeType::TERMINATE { pe.typ.n_pts() as i32 } else { 0i32 }; - for (_j, pt) in (0..n_pts).zip(pe.p.iter_mut()) { + for (_j, pt) in (0..n_pts).zip(pe.p.iter_mut()) { /* op: m l c v y h */ let fresh32 = len; len = len + 1; *b.offset(fresh32 as isize) = ' ' as i32 as i8; @@ -796,7 +786,7 @@ unsafe extern "C" fn pdf_dev__flushpath( len = len + 1; *b.offset(fresh37 as isize) = '*' as i32 as i8 } - if isclip != 0 { + if isclip { let fresh38 = len; len = len + 1; *b.offset(fresh38 as isize) = ' ' as i32 as i8; diff --git a/dpx/src/dpx_pdfobj.rs b/dpx/src/dpx_pdfobj.rs index 8eb6205bb..eb775e16c 100644 --- a/dpx/src/dpx_pdfobj.rs +++ b/dpx/src/dpx_pdfobj.rs @@ -1849,7 +1849,7 @@ unsafe extern "C" fn filter_PNG15_apply_filter( as u32 as u32; /* Type 3 -- Average */ let mut tmp: libc::c_int = - (((up + left) / 2i32) as libc::c_double).floor() as libc::c_int; + (((up + left) / 2i32) as f64).floor() as libc::c_int; sum[3] = (sum[3] as libc::c_uint) .wrapping_add((*p.offset(i as isize) as libc::c_int - tmp).abs() as libc::c_uint) as u32 as u32; @@ -1934,7 +1934,7 @@ unsafe extern "C" fn filter_PNG15_apply_filter( 0i32 }; let mut tmp_0: libc::c_int = - (((up_1 + left_1) / 2i32) as libc::c_double).floor() as libc::c_int; + (((up_1 + left_1) / 2i32) as f64).floor() as libc::c_int; *pp.offset((i + 1i32) as isize) = (*p.offset(i as isize) as libc::c_int - tmp_0) as libc::c_uchar; i += 1 @@ -2203,22 +2203,22 @@ unsafe extern "C" fn filter_create_predictor_dict( pdf_add_dict( parms, pdf_new_name(b"BitsPerComponent\x00" as *const u8 as *const i8), - pdf_new_number(bpc as libc::c_double), + pdf_new_number(bpc as f64), ); pdf_add_dict( parms, pdf_new_name(b"Colors\x00" as *const u8 as *const i8), - pdf_new_number(colors as libc::c_double), + pdf_new_number(colors as f64), ); pdf_add_dict( parms, pdf_new_name(b"Columns\x00" as *const u8 as *const i8), - pdf_new_number(columns as libc::c_double), + pdf_new_number(columns as f64), ); pdf_add_dict( parms, pdf_new_name(b"Predictor\x00" as *const u8 as *const i8), - pdf_new_number(predictor as libc::c_double), + pdf_new_number(predictor as f64), ); return parms; } @@ -2950,7 +2950,7 @@ unsafe extern "C" fn filter_decoded( 0i32 }; let mut tmp: libc::c_int = - (((up + left) / 2i32) as libc::c_double).floor() as libc::c_int; + (((up + left) / 2i32) as f64).floor() as libc::c_int; *buf.offset(i as isize) = (*p.offset(i as isize) as libc::c_int + tmp & 0xffi32) as libc::c_uchar; diff --git a/dpx/src/dpx_pdfparse.rs b/dpx/src/dpx_pdfparse.rs index d2f71482d..2d95f93d5 100644 --- a/dpx/src/dpx_pdfparse.rs +++ b/dpx/src/dpx_pdfparse.rs @@ -79,11 +79,11 @@ pub type size_t = u64; /* PDF */ #[derive(Copy, Clone)] #[repr(C)] -pub struct C2RustUnnamed_0 { +pub struct ParserState { pub tainted: i32, } -static mut parser_state: C2RustUnnamed_0 = { - let mut init = C2RustUnnamed_0 { tainted: 0i32 }; +static mut parser_state: ParserState = { + let mut init = ParserState { tainted: 0i32 }; init }; static mut save: *const i8 = 0 as *const i8; diff --git a/dpx/src/dpx_pngimage.rs b/dpx/src/dpx_pngimage.rs index d26cec7f1..032651f0d 100644 --- a/dpx/src/dpx_pngimage.rs +++ b/dpx/src/dpx_pngimage.rs @@ -19,6 +19,15 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ +#![allow( + dead_code, + mutable_transmutes, + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_assignments, + unused_mut +)] use libpng_sys::ffi::*; use std::convert::TryInto; @@ -124,8 +133,7 @@ unsafe extern "C" fn _png_warning_callback( unsafe extern "C" fn _png_read(mut png_ptr: *mut png_struct, mut outbytes: *mut u8, mut n: usize) { let mut png = png_ptr.as_ref().unwrap(); let mut handle: rust_input_handle_t = png_get_io_ptr(png); - let mut r: ssize_t = 0; - r = ttstub_input_read(handle, outbytes as *mut i8, n.try_into().unwrap()); + let r = ttstub_input_read(handle, outbytes as *mut i8, n.try_into().unwrap()); if r < 0i32 as ssize_t || r as size_t != n.try_into().unwrap() { panic!("error reading PNG"); }; @@ -135,26 +143,14 @@ pub unsafe extern "C" fn png_include_image( mut ximage: *mut pdf_ximage, mut handle: rust_input_handle_t, ) -> i32 { - let mut stream: *mut pdf_obj = 0 as *mut pdf_obj; - let mut stream_dict: *mut pdf_obj = 0 as *mut pdf_obj; - let mut colorspace: *mut pdf_obj = 0 as *mut pdf_obj; - let mut mask: *mut pdf_obj = 0 as *mut pdf_obj; - let mut intent: *mut pdf_obj = 0 as *mut pdf_obj; - let mut stream_data_ptr: png_bytep = 0 as *mut png_byte; - let mut trans_type: i32 = 0; let mut info = ximage_info::default(); /* Libpng stuff */ - let mut bpc: png_byte = 0; - let mut color_type: png_byte = 0; - let mut width: png_uint_32 = 0; - let mut height: png_uint_32 = 0; - let mut rowbytes: png_uint_32 = 0; pdf_ximage_init_image_info(&mut info); - stream = 0 as *mut pdf_obj; - stream_dict = 0 as *mut pdf_obj; - intent = 0 as *mut pdf_obj; - mask = intent; - colorspace = mask; + let mut stream = 0 as *mut pdf_obj; + let mut stream_dict = 0 as *mut pdf_obj; + let mut intent = 0 as *mut pdf_obj; + let mut mask = intent; + let mut colorspace = mask; ttstub_input_seek(handle, 0i32 as ssize_t, 0i32); let png = if let Some(png) = png_create_read_struct( @@ -190,10 +186,10 @@ pub unsafe extern "C" fn png_include_image( /* NOTE: could use png_set_sig_bytes() to tell libpng if we started at non-zero file offset */ /* Read PNG info-header and get some info. */ png_read_info(png, png_info); - color_type = png_get_color_type(png, png_info); - width = png_get_image_width(png, png_info); - height = png_get_image_height(png, png_info); - bpc = png_get_bit_depth(png, png_info); + let color_type = png_get_color_type(png, png_info); + let width = png_get_image_width(png, png_info); + let height = png_get_image_height(png, png_info); + let mut bpc = png_get_bit_depth(png, png_info); if bpc as libc::c_int > 8i32 { if pdf_get_version() < 5i32 as libc::c_uint { /* Ask libpng to convert down to 8-bpc. */ @@ -220,14 +216,14 @@ pub unsafe extern "C" fn png_include_image( && png_get_valid(png, png_info, 0x4u32) == 0 && png_get_valid(png, png_info, 0x1u32) != 0 { - let mut G: libc::c_double = 1.0f64; + let mut G: f64 = 1.0f64; png_get_gAMA(png, png_info, &mut G); png_set_gamma(png, 2.2f64, G); } - trans_type = check_transparency(png, png_info); + let trans_type = check_transparency(png, png_info); /* check_transparency() does not do updata_info() */ png_read_update_info(png, png_info); - rowbytes = png_get_rowbytes(png, png_info) as png_uint_32; + let mut rowbytes = png_get_rowbytes(png, png_info) as png_uint_32; /* Values listed below will not be modified in the remaining process. */ info.width = width as libc::c_int; info.height = height as libc::c_int; @@ -235,14 +231,14 @@ pub unsafe extern "C" fn png_include_image( let mut xppm: png_uint_32 = png_get_x_pixels_per_meter(png, png_info); let mut yppm: png_uint_32 = png_get_y_pixels_per_meter(png, png_info); if xppm > 0i32 as libc::c_uint { - info.xdensity = 72.0f64 / 0.0254f64 / xppm as libc::c_double + info.xdensity = 72.0f64 / 0.0254f64 / xppm as f64 } if yppm > 0_u32 { info.ydensity = 72.0f64 / 0.0254f64 / yppm as f64 } stream = pdf_new_stream(1i32 << 0i32); stream_dict = pdf_stream_dict(stream); - stream_data_ptr = new((rowbytes.wrapping_mul(height) as libc::c_ulong) + let stream_data_ptr = new((rowbytes.wrapping_mul(height) as libc::c_ulong) .wrapping_mul(::std::mem::size_of::() as libc::c_ulong) as u32) as *mut png_byte; read_image_data(png, stream_data_ptr, height, rowbytes); @@ -373,8 +369,6 @@ pub unsafe extern "C" fn png_include_image( */ if pdf_get_version() >= 4i32 as libc::c_uint { let mut text_ptr = 0 as *mut png_text; - let mut XMP_stream: *mut pdf_obj = 0 as *mut pdf_obj; - let mut XMP_stream_dict: *mut pdf_obj = 0 as *mut pdf_obj; let mut i: libc::c_int = 0; let mut num_text: libc::c_int = 0; let mut have_XMP: libc::c_int = 0i32; @@ -407,8 +401,8 @@ pub unsafe extern "C" fn png_include_image( * application programs that only want PDF document global XMP metadata * and scan for that. */ - XMP_stream = pdf_new_stream(1i32 << 0i32); - XMP_stream_dict = pdf_stream_dict(XMP_stream); + let XMP_stream = pdf_new_stream(1i32 << 0i32); + let XMP_stream_dict = pdf_stream_dict(XMP_stream); pdf_add_dict( XMP_stream_dict, pdf_new_name(b"Type\x00" as *const u8 as *const i8), @@ -488,13 +482,11 @@ unsafe extern "C" fn check_transparency( mut info: &mut png_info, ) -> libc::c_int { let mut trans_type: libc::c_int = 0; - let mut pdf_version: libc::c_uint = 0; - let mut color_type: png_byte = 0; let mut trans_values = 0 as *mut png_color_16; let mut trans: png_bytep = 0 as *mut png_byte; let mut num_trans: i32 = 0; - pdf_version = pdf_get_version(); - color_type = png_get_color_type(png, info); + let pdf_version = pdf_get_version(); + let color_type = png_get_color_type(png, info); /* * First we set trans_type to appropriate value for PNG image. */ @@ -598,26 +590,24 @@ unsafe extern "C" fn get_rendering_intent( mut png: &mut png_struct, mut info: &mut png_info, ) -> *mut pdf_obj { - let mut intent: *mut pdf_obj = 0 as *mut pdf_obj; let mut srgb_intent: libc::c_int = 0; if png_get_valid(png, info, 0x800u32) != 0 && png_get_sRGB(png, info, &mut srgb_intent) != 0 { match srgb_intent { - 2 => intent = pdf_new_name(b"Saturation\x00" as *const u8 as *const i8), - 0 => intent = pdf_new_name(b"Perceptual\x00" as *const u8 as *const i8), - 3 => intent = pdf_new_name(b"AbsoluteColorimetric\x00" as *const u8 as *const i8), - 1 => intent = pdf_new_name(b"RelativeColorimetric\x00" as *const u8 as *const i8), + 2 => pdf_new_name(b"Saturation\x00" as *const u8 as *const i8), + 0 => pdf_new_name(b"Perceptual\x00" as *const u8 as *const i8), + 3 => pdf_new_name(b"AbsoluteColorimetric\x00" as *const u8 as *const i8), + 1 => pdf_new_name(b"RelativeColorimetric\x00" as *const u8 as *const i8), _ => { warn!( "{}: Invalid value in PNG sRGB chunk: {}", "PNG", srgb_intent, ); - intent = 0 as *mut pdf_obj + 0 as *mut pdf_obj } } } else { - intent = 0 as *mut pdf_obj + 0 as *mut pdf_obj } - intent } /* sRGB: * @@ -627,18 +617,15 @@ unsafe extern "C" fn get_rendering_intent( */ /* Approximated sRGB */ unsafe extern "C" fn create_cspace_sRGB(mut png: &png_struct, mut info: &png_info) -> *mut pdf_obj { - let mut colorspace: *mut pdf_obj = 0 as *mut pdf_obj; - let mut cal_param: *mut pdf_obj = 0 as *mut pdf_obj; - let mut color_type: png_byte = 0; - color_type = png_get_color_type(png, info); + let color_type = png_get_color_type(png, info); /* Parameters taken from PNG spec. section 4.2.2.3. */ - cal_param = make_param_Cal( + let cal_param = make_param_Cal( color_type, 2.2f64, 0.3127f64, 0.329f64, 0.64f64, 0.33f64, 0.3f64, 0.6f64, 0.15f64, 0.06f64, ); if cal_param.is_null() { return 0 as *mut pdf_obj; } - colorspace = pdf_new_array(); + let colorspace = pdf_new_array(); match color_type as i32 { 2 | 6 | 3 => { pdf_add_array( @@ -668,12 +655,6 @@ unsafe extern "C" fn create_cspace_ICCBased( mut png: &mut png_struct, mut png_info: &mut png_info, ) -> *mut pdf_obj { - let mut colorspace: *mut pdf_obj = 0 as *mut pdf_obj; /* Manual page for libpng does not - * clarify whether profile data is inflated by libpng. - */ - let mut csp_id: i32 = 0; - let mut colortype: i32 = 0; - let mut color_type: png_byte = 0; let mut name = 0 as *mut i8; let mut compression_type: libc::c_int = 0; let mut profile: png_bytep = 0 as *mut png_byte; @@ -690,28 +671,29 @@ unsafe extern "C" fn create_cspace_ICCBased( { return 0 as *mut pdf_obj; } - color_type = png_get_color_type(png, png_info); - if color_type as libc::c_int & 2i32 != 0 { - colortype = -3i32 + let color_type = png_get_color_type(png, png_info); + let colortype = if color_type as libc::c_int & 2i32 != 0 { + -3i32 } else { - colortype = -1i32 - } + -1i32 + }; if iccp_check_colorspace(colortype, profile as *const libc::c_void, proflen as i32) < 0i32 { - colorspace = 0 as *mut pdf_obj + 0 as *mut pdf_obj/* Manual page for libpng does not + * clarify whether profile data is inflated by libpng. + */ } else { - csp_id = iccp_load_profile( + let csp_id = iccp_load_profile( name as *const i8, profile as *const libc::c_void, proflen as i32, ); if csp_id < 0i32 { - colorspace = 0 as *mut pdf_obj + 0 as *mut pdf_obj } else { - colorspace = pdf_get_colorspace_reference(csp_id) + pdf_get_colorspace_reference(csp_id) } } /* Rendering intent ... */ - colorspace } /* CIE-Based: CalRGB/CalGray */ /* @@ -724,17 +706,15 @@ unsafe extern "C" fn create_cspace_CalRGB( mut png: &mut png_struct, mut png_info: &mut png_info, ) -> *mut pdf_obj { - let mut colorspace: *mut pdf_obj = 0 as *mut pdf_obj; - let mut cal_param: *mut pdf_obj = 0 as *mut pdf_obj; - let mut xw: libc::c_double = 0.; - let mut yw: libc::c_double = 0.; - let mut xr: libc::c_double = 0.; - let mut yr: libc::c_double = 0.; - let mut xg: libc::c_double = 0.; - let mut yg: libc::c_double = 0.; - let mut xb: libc::c_double = 0.; - let mut yb: libc::c_double = 0.; - let mut G: libc::c_double = 0.; + let mut xw: f64 = 0.; + let mut yw: f64 = 0.; + let mut xr: f64 = 0.; + let mut yr: f64 = 0.; + let mut xg: f64 = 0.; + let mut yg: f64 = 0.; + let mut xb: f64 = 0.; + let mut yb: f64 = 0.; + let mut G: f64 = 0.; if png_get_valid(png, png_info, 0x4u32) == 0 || png_get_cHRM( png, png_info, &mut xw, &mut yw, &mut xr, &mut yr, &mut xg, &mut yg, &mut xb, &mut yb, @@ -764,11 +744,11 @@ unsafe extern "C" fn create_cspace_CalRGB( } else { G = 2.2f64 } - cal_param = make_param_Cal(2i32 as png_byte, G, xw, yw, xr, yr, xg, yg, xb, yb); + let cal_param = make_param_Cal(2i32 as png_byte, G, xw, yw, xr, yr, xg, yg, xb, yb); if cal_param.is_null() { return 0 as *mut pdf_obj; } - colorspace = pdf_new_array(); + let colorspace = pdf_new_array(); pdf_add_array( colorspace, pdf_new_name(b"CalRGB\x00" as *const u8 as *const i8), @@ -780,17 +760,15 @@ unsafe extern "C" fn create_cspace_CalGray( mut png: &mut png_struct, mut info: &mut png_info, ) -> *mut pdf_obj { - let mut colorspace: *mut pdf_obj = 0 as *mut pdf_obj; - let mut cal_param: *mut pdf_obj = 0 as *mut pdf_obj; - let mut xw: libc::c_double = 0.; - let mut yw: libc::c_double = 0.; - let mut xr: libc::c_double = 0.; - let mut yr: libc::c_double = 0.; - let mut xg: libc::c_double = 0.; - let mut yg: libc::c_double = 0.; - let mut xb: libc::c_double = 0.; - let mut yb: libc::c_double = 0.; - let mut G: libc::c_double = 0.; + let mut xw: f64 = 0.; + let mut yw: f64 = 0.; + let mut xr: f64 = 0.; + let mut yr: f64 = 0.; + let mut xg: f64 = 0.; + let mut yg: f64 = 0.; + let mut xb: f64 = 0.; + let mut yb: f64 = 0.; + let mut G: f64 = 0.; if png_get_valid(png, info, 0x4u32) == 0 || png_get_cHRM( png, info, &mut xw, &mut yw, &mut xr, &mut yr, &mut xg, &mut yg, &mut xb, &mut yb, @@ -820,11 +798,11 @@ unsafe extern "C" fn create_cspace_CalGray( } else { G = 2.2f64 } /* Yw = 1.0 */ - cal_param = make_param_Cal(0i32 as png_byte, G, xw, yw, xr, yr, xg, yg, xb, yb); + let cal_param = make_param_Cal(0i32 as png_byte, G, xw, yw, xr, yr, xg, yg, xb, yb); if cal_param.is_null() { return 0 as *mut pdf_obj; } - colorspace = pdf_new_array(); + let colorspace = pdf_new_array(); pdf_add_array( colorspace, pdf_new_name(b"CalGray\x00" as *const u8 as *const i8), @@ -833,33 +811,17 @@ unsafe extern "C" fn create_cspace_CalGray( colorspace } unsafe extern "C" fn make_param_Cal( - mut color_type: png_byte, - mut G: f64, - mut xw: f64, - mut yw: f64, - mut xr: f64, - mut yr: f64, - mut xg: f64, - mut yg: f64, - mut xb: f64, - mut yb: f64, + color_type: png_byte, + G: f64, + xw: f64, + yw: f64, + xr: f64, + yr: f64, + xg: f64, + yg: f64, + xb: f64, + yb: f64, ) -> *mut pdf_obj { - let mut cal_param: *mut pdf_obj = 0 as *mut pdf_obj; - let mut white_point: *mut pdf_obj = 0 as *mut pdf_obj; - let mut matrix: *mut pdf_obj = 0 as *mut pdf_obj; - let mut dev_gamma: *mut pdf_obj = 0 as *mut pdf_obj; - let mut Xw: f64 = 0.; - let mut Yw: f64 = 0.; - let mut Zw: f64 = 0.; - let mut Xr: f64 = 0.; - let mut Xg: f64 = 0.; - let mut Xb: f64 = 0.; - let mut Yr: f64 = 0.; - let mut Yb: f64 = 0.; - let mut Yg: f64 = 0.; - let mut Zr: f64 = 0.; - let mut Zg: f64 = 0.; - let mut Zb: f64 = 0.; /* * TODO: Check validity * @@ -871,47 +833,39 @@ unsafe extern "C" fn make_param_Cal( * http://www.sixlegs.com/software/png/ * */ - let mut zw: f64 = 0.; - let mut zr: f64 = 0.; - let mut zg: f64 = 0.; - let mut zb: f64 = 0.; - let mut fr: f64 = 0.; - let mut fg: f64 = 0.; - let mut fb: f64 = 0.; - let mut det: f64 = 0.; /* WhitePoint */ - zw = 1i32 as f64 - (xw + yw); - zr = 1i32 as f64 - (xr + yr); - zg = 1i32 as f64 - (xg + yg); - zb = 1i32 as f64 - (xb + yb); - Xw = xw / yw; - Yw = 1.0f64; - Zw = zw / yw; + let zw = 1. - (xw + yw); + let zr = 1. - (xr + yr); + let zg = 1. - (xg + yg); + let zb = 1. - (xb + yb); + let Xw = xw / yw; + let Yw = 1.0f64; + let Zw = zw / yw; /* Matrix */ - det = xr * (yg * zb - zg * yb) - xg * (yr * zb - zr * yb) + xb * (yr * zg - zr * yg); + let det = xr * (yg * zb - zg * yb) - xg * (yr * zb - zr * yb) + xb * (yr * zg - zr * yg); if (if det < 0i32 as f64 { -det } else { det }) < 1.0e-10f64 { warn!("Non invertible matrix: Maybe invalid value(s) specified in cHRM chunk."); return 0 as *mut pdf_obj; } - fr = (Xw * (yg * zb - zg * yb) - xg * (zb - Zw * yb) + xb * (zg - Zw * yg)) / det; - fg = (xr * (zb - Zw * yb) - Xw * (yr * zb - zr * yb) + xb * (yr * Zw - zr)) / det; - fb = (xr * (yg * Zw - zg) - xg * (yr * Zw - zr) + Xw * (yr * zg - zr * yg)) / det; - Xr = fr * xr; - Yr = fr * yr; - Zr = fr * zr; - Xg = fg * xg; - Yg = fg * yg; - Zg = fg * zg; - Xb = fb * xb; - Yb = fb * yb; - Zb = fb * zb; + let fr = (Xw * (yg * zb - zg * yb) - xg * (zb - Zw * yb) + xb * (zg - Zw * yg)) / det; + let fg = (xr * (zb - Zw * yb) - Xw * (yr * zb - zr * yb) + xb * (yr * Zw - zr)) / det; + let fb = (xr * (yg * Zw - zg) - xg * (yr * Zw - zr) + Xw * (yr * zg - zr * yg)) / det; + let Xr = fr * xr; + let Yr = fr * yr; + let Zr = fr * zr; + let Xg = fg * xg; + let Yg = fg * yg; + let Zg = fg * zg; + let Xb = fb * xb; + let Yb = fb * yb; + let Zb = fb * zb; if G < 1.0e-2f64 { warn!("Unusual Gamma specified: 1.0 / {}", G,); return 0 as *mut pdf_obj; } - cal_param = pdf_new_dict(); + let cal_param = pdf_new_dict(); /* White point is always required. */ - white_point = pdf_new_array(); + let white_point = pdf_new_array(); pdf_add_array( white_point, pdf_new_number((Xw / 0.00001f64 + 0.5f64).floor() * 0.00001f64), @@ -932,7 +886,7 @@ unsafe extern "C" fn make_param_Cal( /* Matrix - default: Identity */ if color_type as i32 & 2i32 != 0 { if G != 1.0f64 { - dev_gamma = pdf_new_array(); /* Gray */ + let dev_gamma = pdf_new_array(); /* Gray */ pdf_add_array( dev_gamma, pdf_new_number((G / 0.00001f64 + 0.5f64).floor() * 0.00001f64), @@ -951,7 +905,7 @@ unsafe extern "C" fn make_param_Cal( dev_gamma, ); } - matrix = pdf_new_array(); + let matrix = pdf_new_array(); pdf_add_array( matrix, pdf_new_number((Xr / 0.00001f64 + 0.5f64).floor() * 0.00001f64), @@ -1015,13 +969,9 @@ unsafe extern "C" fn create_cspace_Indexed( mut png: &mut png_struct, mut info: &mut png_info, ) -> *mut pdf_obj { - let mut colorspace: *mut pdf_obj = 0 as *mut pdf_obj; let mut base: *mut pdf_obj = 0 as *mut pdf_obj; - let mut lookup: *mut pdf_obj = 0 as *mut pdf_obj; - let mut data_ptr: *mut png_byte = 0 as *mut png_byte; let mut plte = 0 as *mut png_color; let mut num_plte: libc::c_int = 0; - let mut i: libc::c_int = 0; if png_get_valid(png, info, 0x8u32) == 0 || png_get_PLTE(png, info, &mut plte, &mut num_plte) == 0 { @@ -1029,7 +979,7 @@ unsafe extern "C" fn create_cspace_Indexed( return 0 as *mut pdf_obj; } /* Order is important. */ - colorspace = pdf_new_array(); + let colorspace = pdf_new_array(); pdf_add_array( colorspace, pdf_new_name(b"Indexed\x00" as *const u8 as *const i8), @@ -1046,17 +996,17 @@ unsafe extern "C" fn create_cspace_Indexed( } pdf_add_array(colorspace, base); pdf_add_array(colorspace, pdf_new_number((num_plte - 1i32) as f64)); - data_ptr = new(((num_plte * 3i32) as u32 as u64) + let data_ptr = new(((num_plte * 3i32) as u32 as u64) .wrapping_mul(::std::mem::size_of::() as u64) as u32) as *mut png_byte; - i = 0i32; + let mut i = 0i32; while i < num_plte { *data_ptr.offset((3i32 * i) as isize) = (*plte.offset(i as isize)).red; *data_ptr.offset((3i32 * i + 1i32) as isize) = (*plte.offset(i as isize)).green; *data_ptr.offset((3i32 * i + 2i32) as isize) = (*plte.offset(i as isize)).blue; i += 1 } - lookup = pdf_new_string(data_ptr as *const libc::c_void, (num_plte * 3i32) as size_t); + let lookup = pdf_new_string(data_ptr as *const libc::c_void, (num_plte * 3i32) as size_t); free(data_ptr as *mut libc::c_void); pdf_add_array(colorspace, lookup); colorspace @@ -1072,8 +1022,6 @@ unsafe extern "C" fn create_ckey_mask( mut png: &png_struct_def, mut png_info: &mut png_info, ) -> *mut pdf_obj { - let mut colorkeys: *mut pdf_obj = 0 as *mut pdf_obj; - let mut color_type: png_byte = 0; let mut trans: png_bytep = 0 as *mut png_byte; let mut num_trans: libc::c_int = 0; let mut i: libc::c_int = 0; @@ -1084,8 +1032,8 @@ unsafe extern "C" fn create_ckey_mask( warn!("{}: PNG does not have valid tRNS chunk!", "PNG"); return 0 as *mut pdf_obj; } - colorkeys = pdf_new_array(); - color_type = png_get_color_type(png, png_info); + let mut colorkeys = pdf_new_array(); + let color_type = png_get_color_type(png, png_info); match color_type as libc::c_int { 3 => { i = 0i32; @@ -1146,9 +1094,6 @@ unsafe extern "C" fn create_soft_mask( mut width: png_uint_32, mut height: png_uint_32, ) -> *mut pdf_obj { - let mut smask: *mut pdf_obj = 0 as *mut pdf_obj; - let mut dict: *mut pdf_obj = 0 as *mut pdf_obj; - let mut smask_data_ptr: png_bytep = 0 as *mut png_byte; let mut trans: png_bytep = 0 as *mut png_byte; let mut num_trans: i32 = 0; let mut i: png_uint_32 = 0; @@ -1167,9 +1112,9 @@ unsafe extern "C" fn create_soft_mask( ); return 0 as *mut pdf_obj; } - smask = pdf_new_stream(1i32 << 0i32); - dict = pdf_stream_dict(smask); - smask_data_ptr = new((width.wrapping_mul(height) as u64) + let smask = pdf_new_stream(1i32 << 0i32); + let dict = pdf_stream_dict(smask); + let smask_data_ptr = new((width.wrapping_mul(height) as u64) .wrapping_mul(::std::mem::size_of::() as u64) as u32) as *mut png_byte; pdf_add_dict( @@ -1229,14 +1174,9 @@ unsafe extern "C" fn strip_soft_mask( mut width: png_uint_32, mut height: png_uint_32, ) -> *mut pdf_obj { - let mut smask: *mut pdf_obj = 0 as *mut pdf_obj; - let mut dict: *mut pdf_obj = 0 as *mut pdf_obj; - let mut color_type: png_byte = 0; - let mut bpc: png_byte = 0; - let mut smask_data_ptr: png_bytep = 0 as *mut png_byte; let mut i: png_uint_32 = 0; - color_type = png_get_color_type(png, png_info); - bpc = png_get_bit_depth(png, png_info); + let color_type = png_get_color_type(png, png_info); + let bpc = png_get_bit_depth(png, png_info); if color_type as libc::c_int & 2i32 != 0 { let mut bps: libc::c_int = if bpc as libc::c_int == 8i32 { 4i32 @@ -1262,8 +1202,8 @@ unsafe extern "C" fn strip_soft_mask( return 0 as *mut pdf_obj; } } - smask = pdf_new_stream(1i32 << 0i32); - dict = pdf_stream_dict(smask); + let smask = pdf_new_stream(1i32 << 0i32); + let dict = pdf_stream_dict(smask); pdf_add_dict( dict, pdf_new_name(b"Type\x00" as *const u8 as *const i8), @@ -1294,7 +1234,7 @@ unsafe extern "C" fn strip_soft_mask( pdf_new_name(b"BitsPerComponent\x00" as *const u8 as *const i8), pdf_new_number(bpc as f64), ); - smask_data_ptr = new((((bpc as i32 / 8i32) as u32) + let mut smask_data_ptr = new((((bpc as i32 / 8i32) as u32) .wrapping_mul(width) .wrapping_mul(height) as u64) .wrapping_mul(::std::mem::size_of::() as u64) as u32) @@ -1397,9 +1337,8 @@ unsafe extern "C" fn read_image_data( mut height: png_uint_32, mut rowbytes: png_uint_32, ) { - let mut rows_p = 0 as *mut *mut png_byte; let mut i: png_uint_32 = 0; - rows_p = new((height as libc::c_ulong) + let mut rows_p = new((height as libc::c_ulong) .wrapping_mul(::std::mem::size_of::() as libc::c_ulong) as u32) as *mut png_bytep; i = 0i32 as png_uint_32; @@ -1416,8 +1355,8 @@ pub unsafe extern "C" fn png_get_bbox( mut handle: rust_input_handle_t, mut width: *mut u32, mut height: *mut u32, - mut xdensity: *mut libc::c_double, - mut ydensity: *mut libc::c_double, + mut xdensity: *mut f64, + mut ydensity: *mut f64, ) -> libc::c_int { ttstub_input_seek(handle, 0i32 as ssize_t, 0i32); let mut png = png_create_read_struct( diff --git a/dpx/src/dpx_spc_pdfm.rs b/dpx/src/dpx_spc_pdfm.rs index 77c72e92a..46ac3f2a4 100644 --- a/dpx/src/dpx_spc_pdfm.rs +++ b/dpx/src/dpx_spc_pdfm.rs @@ -242,7 +242,6 @@ pub struct tounicode { use super::dpx_dpxutil::ht_table; -use super::dpx_dpxutil::ht_entry; pub type hval_free_func = Option ()>; use super::dpx_fontmap::fontmap_rec; diff --git a/dpx/src/dpx_specials.rs b/dpx/src/dpx_specials.rs index 83c5aa29d..e3f5d836c 100644 --- a/dpx/src/dpx_specials.rs +++ b/dpx/src/dpx_specials.rs @@ -228,7 +228,6 @@ pub struct spc_handler { pub exec: spc_handler_fn_ptr, } -use super::dpx_dpxutil::ht_entry; use super::dpx_dpxutil::ht_table; pub type hval_free_func = Option ()>; diff --git a/dpx/src/dpx_t1_char.rs b/dpx/src/dpx_t1_char.rs index 081f653be..01a45585d 100644 --- a/dpx/src/dpx_t1_char.rs +++ b/dpx/src/dpx_t1_char.rs @@ -63,6 +63,28 @@ pub struct t1_ginfo { pub bbox: C2RustUnnamed_0, pub seac: C2RustUnnamed, } +impl t1_ginfo { + pub fn new() -> Self { + Self { + use_seac: 0, + wx: 0., + wy: 0., + bbox: C2RustUnnamed_0 { + llx: 0., + lly: 0., + urx: 0., + ury: 0., + }, + seac: C2RustUnnamed { + asb: 0., + adx: 0., + ady: 0., + bchar: 0, + achar: 0, + }, + } + } +} #[derive(Copy, Clone)] #[repr(C)] pub struct C2RustUnnamed { diff --git a/dpx/src/dpx_type0.rs b/dpx/src/dpx_type0.rs index 91e8a9647..98c8df382 100644 --- a/dpx/src/dpx_type0.rs +++ b/dpx/src/dpx_type0.rs @@ -152,14 +152,6 @@ pub struct font_cache { pub fonts: *mut Type0Font, } use super::dpx_cmap::CMap; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed { - pub minBytesIn: size_t, - pub maxBytesIn: size_t, - pub minBytesOut: size_t, - pub maxBytesOut: size_t, -} static mut __verbose: i32 = 0i32; #[no_mangle] diff --git a/dpx/src/dpx_type1.rs b/dpx/src/dpx_type1.rs index be4f56ee2..104f3c8d4 100644 --- a/dpx/src/dpx_type1.rs +++ b/dpx/src/dpx_type1.rs @@ -159,32 +159,9 @@ pub type s_SID = u16; use super::dpx_cff::cff_encoding; use super::dpx_cff::cff_map; use super::dpx_cff::cff_range1; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct t1_ginfo { - pub use_seac: i32, - pub wx: f64, - pub wy: f64, - pub bbox: C2RustUnnamed_3, - pub seac: C2RustUnnamed_2, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_2 { - pub asb: f64, - pub adx: f64, - pub ady: f64, - pub bchar: card8, - pub achar: card8, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_3 { - pub llx: f64, - pub lly: f64, - pub urx: f64, - pub ury: f64, -} + +use super::dpx_t1_char::t1_ginfo; + /* tectonic/core-strutils.h: miscellaneous C string utilities Copyright 2016-2018 the Tectonic Project Licensed under the MIT License. @@ -290,24 +267,7 @@ unsafe extern "C" fn get_font_attr(mut font: *mut pdf_font, mut cffont: *mut cff b"lambda\x00" as *const u8 as *const i8, 0 as *const i8, ]; - let mut gm: t1_ginfo = t1_ginfo { - use_seac: 0, - wx: 0., - wy: 0., - bbox: C2RustUnnamed_3 { - llx: 0., - lly: 0., - urx: 0., - ury: 0., - }, - seac: C2RustUnnamed_2 { - asb: 0., - adx: 0., - ady: 0., - bchar: 0, - achar: 0, - }, - }; + let mut gm = t1_ginfo::new(); defaultwidth = 500.0f64; nominalwidth = 0.0f64; /* @@ -1181,24 +1141,7 @@ pub unsafe extern "C" fn pdf_font_load_type1(mut font: *mut pdf_font) -> i32 { * used already). */ let mut cstring: *mut cff_index = 0 as *mut cff_index; - let mut gm: t1_ginfo = t1_ginfo { - use_seac: 0, - wx: 0., - wy: 0., - bbox: C2RustUnnamed_3 { - llx: 0., - lly: 0., - urx: 0., - ury: 0., - }, - seac: C2RustUnnamed_2 { - asb: 0., - adx: 0., - ady: 0., - bchar: 0, - achar: 0, - }, - }; + let mut gm = t1_ginfo::new(); let mut gid_0: card16 = 0; let mut gid_orig: card16 = 0; let mut dstlen_max: i32 = 0; diff --git a/dpx/src/dpx_type1c.rs b/dpx/src/dpx_type1c.rs index 418e6fcb7..cfa5e34b7 100644 --- a/dpx/src/dpx_type1c.rs +++ b/dpx/src/dpx_type1c.rs @@ -212,32 +212,8 @@ use super::dpx_cff::cff_charsets; use super::dpx_cff::cff_font; use super::dpx_sfnt::sfnt; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct cs_ginfo { - pub flags: i32, - pub wx: f64, - pub wy: f64, - pub bbox: C2RustUnnamed_3, - pub seac: C2RustUnnamed_2, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_2 { - pub asb: f64, - pub adx: f64, - pub ady: f64, - pub bchar: card8, - pub achar: card8, -} -#[derive(Copy, Clone)] -#[repr(C)] -pub struct C2RustUnnamed_3 { - pub llx: f64, - pub lly: f64, - pub urx: f64, - pub ury: f64, -} +use super::dpx_cs_type2::cs_ginfo; + /* tectonic/core-strutils.h: miscellaneous C string utilities Copyright 2016-2018 the Tectonic Project Licensed under the MIT License. @@ -486,24 +462,7 @@ pub unsafe extern "C" fn pdf_font_load_type1c(mut font: *mut pdf_font) -> i32 { let mut num_glyphs: card16 = 0; let mut cs_count: card16 = 0; let mut code: card16 = 0; - let mut ginfo: cs_ginfo = cs_ginfo { - flags: 0, - wx: 0., - wy: 0., - bbox: C2RustUnnamed_3 { - llx: 0., - lly: 0., - urx: 0., - ury: 0., - }, - seac: C2RustUnnamed_2 { - asb: 0., - adx: 0., - ady: 0., - bchar: 0, - achar: 0, - }, - }; + let mut ginfo = cs_ginfo::new(); let mut nominal_width: f64 = 0.; let mut default_width: f64 = 0.; let mut notdef_width: f64 = 0.; diff --git a/dpx/src/lib.rs b/dpx/src/lib.rs index 64e3d8c4c..c5383374b 100644 --- a/dpx/src/lib.rs +++ b/dpx/src/lib.rs @@ -2,7 +2,13 @@ #![feature(ptr_wrapping_offset_from)] #![feature(c_variadic)] #![feature(const_transmute)] -#![allow(unused_unsafe)] +#![allow( + non_camel_case_types, + non_snake_case, + non_upper_case_globals, + unused_mut, + unused_unsafe +)] extern crate tectonic_bridge as bridge; use bridge::*; From 3e4f3592fce96539728005bdc4c914dc7db12d29 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sun, 15 Sep 2019 08:59:06 +0300 Subject: [PATCH 7/7] iccbased_cdata refs --- dpx/src/dpx_pdfcolor.rs | 93 ++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 62 deletions(-) diff --git a/dpx/src/dpx_pdfcolor.rs b/dpx/src/dpx_pdfcolor.rs index ed4014d05..54f0afa19 100644 --- a/dpx/src/dpx_pdfcolor.rs +++ b/dpx/src/dpx_pdfcolor.rs @@ -122,7 +122,7 @@ pub struct iccbased_cdata { } #[derive(Copy, Clone)] #[repr(C)] -pub struct C2RustUnnamed_0 { +pub struct CspcCache { pub count: u32, pub capacity: u32, pub colorspaces: *mut pdf_colorspace, @@ -681,39 +681,23 @@ unsafe extern "C" fn iccp_init_iccHeader(mut icch: *mut iccHeader) { 16i32 as u64, ); } -unsafe extern "C" fn init_iccbased_cdata(mut cdata: *mut iccbased_cdata) { - assert!(!cdata.is_null()); - (*cdata).sig = - ('i' as i32) << 24i32 | ('c' as i32) << 16i32 | ('c' as i32) << 8i32 | 'b' as i32; +unsafe extern "C" fn init_iccbased_cdata(cdata: &mut iccbased_cdata) { + cdata.sig = i32::from_be_bytes(*b"iccb"); memset( - (*cdata).checksum.as_mut_ptr() as *mut libc::c_void, + cdata.checksum.as_mut_ptr() as *mut libc::c_void, 0i32, - 16i32 as u64, + 16_u64, ); - (*cdata).colorspace = 0i32; - (*cdata).alternate = -1i32; + cdata.colorspace = 0i32; + cdata.alternate = -1i32; } -unsafe extern "C" fn release_iccbased_cdata(mut cdata: *mut iccbased_cdata) { - assert!( - !cdata.is_null() - && (*cdata).sig - == ('i' as i32) << 24i32 - | ('c' as i32) << 16i32 - | ('c' as i32) << 8i32 - | 'b' as i32 - ); - free(cdata as *mut libc::c_void); +unsafe extern "C" fn release_iccbased_cdata(cdata: &mut iccbased_cdata) { + assert!(cdata.sig == i32::from_be_bytes(*b"iccb")); + free(cdata as *mut iccbased_cdata as *mut libc::c_void); } -unsafe extern "C" fn get_num_components_iccbased(mut cdata: *const iccbased_cdata) -> i32 { +unsafe extern "C" fn get_num_components_iccbased(cdata: &iccbased_cdata) -> i32 { let mut num_components: i32 = 0i32; - assert!( - !cdata.is_null() - && (*cdata).sig - == ('i' as i32) << 24i32 - | ('c' as i32) << 16i32 - | ('c' as i32) << 8i32 - | 'b' as i32 - ); + assert!(cdata.sig == i32::from_be_bytes(*b"iccb")); match (*cdata).colorspace { -3 => num_components = 3i32, -4 => num_components = 4i32, @@ -725,46 +709,32 @@ unsafe extern "C" fn get_num_components_iccbased(mut cdata: *const iccbased_cdat } unsafe extern "C" fn compare_iccbased( mut ident1: *const i8, - mut cdata1: *const iccbased_cdata, + mut cdata1: Option<&iccbased_cdata>, mut ident2: *const i8, - mut cdata2: *const iccbased_cdata, + mut cdata2: Option<&iccbased_cdata>, ) -> i32 { - if !cdata1.is_null() && !cdata2.is_null() { - assert!( - !cdata1.is_null() - && (*cdata1).sig - == ('i' as i32) << 24i32 - | ('c' as i32) << 16i32 - | ('c' as i32) << 8i32 - | 'b' as i32 - ); - assert!( - !cdata2.is_null() - && (*cdata2).sig - == ('i' as i32) << 24i32 - | ('c' as i32) << 16i32 - | ('c' as i32) << 8i32 - | 'b' as i32 - ); + if let (Some(cdata1), Some(cdata2)) = (cdata1, cdata2) { + assert!(cdata1.sig == i32::from_be_bytes(*b"iccb")); + assert!(cdata2.sig == i32::from_be_bytes(*b"iccb")); if memcmp( (*cdata1).checksum.as_ptr() as *const libc::c_void, nullbytes16.as_mut_ptr() as *const libc::c_void, 16i32 as u64, ) != 0 && memcmp( - (*cdata2).checksum.as_ptr() as *const libc::c_void, + cdata2.checksum.as_ptr() as *const libc::c_void, nullbytes16.as_mut_ptr() as *const libc::c_void, 16i32 as u64, ) != 0 { return memcmp( - (*cdata1).checksum.as_ptr() as *const libc::c_void, - (*cdata2).checksum.as_ptr() as *const libc::c_void, + cdata1.checksum.as_ptr() as *const libc::c_void, + cdata2.checksum.as_ptr() as *const libc::c_void, 16i32 as u64, ); } - if (*cdata1).colorspace != (*cdata2).colorspace { - return (*cdata1).colorspace - (*cdata2).colorspace; + if cdata1.colorspace != cdata2.colorspace { + return cdata1.colorspace - cdata2.colorspace; } /* Continue if checksum unknown and colorspace is same. */ } @@ -1295,7 +1265,6 @@ pub unsafe extern "C" fn iccp_load_profile( }; let mut colorspace: i32 = 0; let mut checksum: [u8; 16] = [0; 16]; - let mut cdata: *mut iccbased_cdata = 0 as *mut iccbased_cdata; iccp_init_iccHeader(&mut icch); if iccp_unpack_header(&mut icch, profile, proflen, 1i32) < 0i32 { /* check size */ @@ -1355,9 +1324,9 @@ pub unsafe extern "C" fn iccp_load_profile( print_iccp_header(&mut icch, checksum.as_mut_ptr()); return -1i32; } - cdata = new((1_u64).wrapping_mul(::std::mem::size_of::() as u64) as u32) + let cdata = new((1_u64).wrapping_mul(::std::mem::size_of::() as u64) as u32) as *mut iccbased_cdata; - init_iccbased_cdata(cdata); + init_iccbased_cdata(&mut *cdata); (*cdata).colorspace = colorspace; memcpy( (*cdata).checksum.as_mut_ptr() as *mut libc::c_void, @@ -1369,7 +1338,7 @@ pub unsafe extern "C" fn iccp_load_profile( if verbose != 0 { info!("(ICCP:[id={}])", cspc_id); } - release_iccbased_cdata(cdata); + release_iccbased_cdata(&mut *cdata); return cspc_id; } if verbose > 1i32 { @@ -1386,15 +1355,15 @@ pub unsafe extern "C" fn iccp_load_profile( pdf_add_dict( stream_dict, pdf_new_name(b"N\x00" as *const u8 as *const i8), - pdf_new_number(get_num_components_iccbased(cdata) as f64), + pdf_new_number(get_num_components_iccbased(&*cdata) as f64), ); pdf_add_stream(stream, profile, proflen); pdf_release_obj(stream); cspc_id = pdf_colorspace_defineresource(ident, 4i32, cdata as *mut libc::c_void, resource); cspc_id } -static mut cspc_cache: C2RustUnnamed_0 = { - let mut init = C2RustUnnamed_0 { +static mut cspc_cache: CspcCache = { + let mut init = CspcCache { count: 0_u32, capacity: 0_u32, colorspaces: 0 as *const pdf_colorspace as *mut pdf_colorspace, @@ -1417,9 +1386,9 @@ unsafe extern "C" fn pdf_colorspace_findresource( 4 => { cmp = compare_iccbased( ident, - cdata as *const iccbased_cdata, + Some(&*(cdata as *const iccbased_cdata)), (*colorspace).ident, - (*colorspace).cdata as *const iccbased_cdata, + Some(&*((*colorspace).cdata as *const iccbased_cdata)), ) } _ => {} @@ -1451,7 +1420,7 @@ unsafe extern "C" fn pdf_clean_colorspace_struct(mut colorspace: *mut pdf_colors if !(*colorspace).cdata.is_null() { match (*colorspace).subtype { 4 => { - release_iccbased_cdata((*colorspace).cdata as *mut iccbased_cdata); + release_iccbased_cdata(&mut *((*colorspace).cdata as *mut iccbased_cdata)); } _ => {} }