Skip to content

Commit

Permalink
g_format
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Apr 1, 2021
1 parent 86abdda commit f31a0af
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 218 deletions.
72 changes: 0 additions & 72 deletions dpx/build.rs

This file was deleted.

30 changes: 0 additions & 30 deletions dpx/shims/dpx_shims.c

This file was deleted.

7 changes: 1 addition & 6 deletions dpx/src/dpx_cff_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
)]

use super::dpx_cff::{cff_add_string, cff_get_string};
use crate::shims::sprintf;
use crate::warn;

pub(crate) type s_SID = u16;
Expand Down Expand Up @@ -412,11 +411,7 @@ unsafe fn pack_real(dest: &mut [u8], mut value: f64) -> usize {
/* To avoid the problem with Mac OS X 10.4 Quartz,
* change the presion of the real numbers
* on June 27, 2007 for musix20.pfb */
sprintf(
buffer.as_mut_ptr() as *mut i8,
b"%.13g\x00" as *const u8 as *const i8,
value,
);
crate::g_format::write_engineering(&mut buffer.as_mut(), value, Some(13)).unwrap();
let mut i = 0;
while buffer[i] != '\u{0}' as u8 {
let ch = if buffer[i] == b'.' {
Expand Down
13 changes: 3 additions & 10 deletions dpx/src/dpx_pdfcolor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use super::dpx_pdfdev::{pdf_dev_get_param, pdf_dev_reset_color};
use crate::dpx_pdfobj::{
pdf_get_version, pdf_link_obj, pdf_obj, pdf_stream, IntoObj, IntoRef, PushObj, STREAM_COMPRESS,
};
use crate::shims::sprintf;
use crate::{info, warn, FromBEByteSlice};
use md5::{Digest, Md5};
use std::error::Error;
Expand Down Expand Up @@ -250,15 +249,9 @@ impl PdfColor {

pub(crate) unsafe fn to_string(&self, mask: u8) -> String {
let format_float_with_printf_g = |value: f64| {
// TODO: refactor this ugly hack while preserving sematics of printf %g
let mut buf = String::from_utf8_lossy(&[0x41; 256]).into_owned();
let len = sprintf(
buf.as_mut_ptr() as *mut i8,
b"%g\0" as *const u8 as *const i8,
value,
) as usize;
buf.truncate(len);
buf
let mut buf = Vec::<u8>::new();
crate::g_format::write_engineering(&mut buf, value, None).unwrap();
String::from_utf8(buf).unwrap()
};

let values_to_string = |values: &[f64]| {
Expand Down
8 changes: 3 additions & 5 deletions dpx/src/dpx_pdfdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ use std::rc::Rc;

use euclid::point2;

use crate::bridge::DisplayExt;
use crate::{info, warn};
use std::ffi::CStr;
use std::ptr;

use super::dpx_cff::cff_charsets_lookup_cid;
Expand Down Expand Up @@ -1580,12 +1578,12 @@ pub(crate) unsafe fn pdf_dev_put_image(
if p.flags & 1 << 3 != 0 {
pdf_dev_rectclip(&r); /* op: Do */
}
let res_name = CStr::from_ptr(pdf_ximage_get_resname(id));
let content = format!(" /{} Do", res_name.display());
let res_name = pdf_ximage_get_resname(id);
let content = format!(" /{} Do", res_name);
let doc = pdf_doc_mut();
doc.add_page_content(content.as_bytes());
pdf_dev_grestore();
doc.add_page_resource("XObject", res_name.to_bytes(), pdf_ximage_get_reference(id));
doc.add_page_resource("XObject", res_name.as_bytes(), pdf_ximage_get_reference(id));
if dvi_is_tracking_boxes() {
let mut rect = Rect::zero();
let mut corner: [Point; 4] = [Point::zero(); 4];
Expand Down
33 changes: 8 additions & 25 deletions dpx/src/dpx_pdfximage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ use crate::dpx_pdfdoc::PdfPageBoundary;
use crate::dpx_pdfobj::{
check_for_pdf, pdf_link_obj, pdf_obj, pdf_ref_obj, IntoObj, IntoRef, Object,
};
use crate::shims::sprintf;

use std::io::{Read, Seek, SeekFrom};

Expand Down Expand Up @@ -109,7 +108,7 @@ pub(crate) struct load_options {
#[derive(Clone)]
pub(crate) struct pdf_ximage {
pub(crate) ident: String,
pub(crate) res_name: [i8; 16],
pub(crate) res_name: String,
pub(crate) subtype: PdfXObjectType,
pub(crate) attr: attr_,
pub(crate) filename: String,
Expand Down Expand Up @@ -154,7 +153,7 @@ impl pdf_ximage {
ident: String::new(),
filename: String::new(),
subtype: PdfXObjectType::None,
res_name: [0; 16],
res_name: String::new(),
reference: ptr::null_mut(),
resource: ptr::null_mut(),
attr: attr_ {
Expand Down Expand Up @@ -322,18 +321,10 @@ unsafe fn load_image(

match I.subtype {
PdfXObjectType::Image => {
sprintf(
I.res_name.as_mut_ptr(),
b"Im%d\x00" as *const u8 as *const i8,
id,
);
I.res_name = format!("Im{}", id);
}
PdfXObjectType::Form => {
sprintf(
I.res_name.as_mut_ptr(),
b"Fm%d\x00" as *const u8 as *const i8,
id,
);
I.res_name = format!("Fm{}", id);
}
_ => {
panic!("Unknown XObject subtype: {}", -1);
Expand Down Expand Up @@ -571,31 +562,23 @@ pub(crate) unsafe fn pdf_ximage_defineresource(
match info {
XInfo::Image(info) => {
I.set_image1(&info, resource);
sprintf(
I.res_name.as_mut_ptr(),
b"Im%d\x00" as *const u8 as *const i8,
id,
);
I.res_name = format!("Im{}", id);
}
XInfo::Form(info) => {
I.set_form1(&info, resource);
sprintf(
I.res_name.as_mut_ptr(),
b"Fm%d\x00" as *const u8 as *const i8,
id,
);
I.res_name = format!("Fm{}", id);
}
}
ximages.push(I);
id as i32
}

pub(crate) unsafe fn pdf_ximage_get_resname(id: i32) -> *const i8 {
pub(crate) unsafe fn pdf_ximage_get_resname(id: i32) -> &'static str {
if id < 0 || id >= ximages.len() as i32 {
panic!("Invalid XObject ID: {}", id);
}
let I = &ximages[id as usize];
I.res_name.as_ptr()
&I.res_name
}

/*pub(crate) unsafe fn pdf_ximage_get_subtype(id: i32) -> PdfXObjectType {
Expand Down
11 changes: 3 additions & 8 deletions dpx/src/dpx_pst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use super::dpx_dpxutil::{is_delim, is_space, skip_white_spaces, xtoi};
use crate::bridge::stub_errno as errno;
use crate::warn;

use crate::shims::sprintf;
use libc::{strtod, strtol};
use std::ptr;

Expand Down Expand Up @@ -96,13 +95,9 @@ pub(crate) unsafe fn pst_parse_null(inbuf: &mut &[u8]) -> Option<PstObj> {
/* NUMBERS */
/* REAL */
unsafe fn pst_real_SV(obj: f64) -> String {
let mut fmt_buf: [u8; 15] = [0; 15];
let len = sprintf(
fmt_buf.as_mut_ptr() as *mut i8,
b"%.5g\x00" as *const u8 as *const i8,
obj,
) as usize;
String::from_utf8_lossy(&fmt_buf[..len]).into()
let mut buf = Vec::<u8>::new();
crate::g_format::write_engineering(&mut buf, obj, Some(5)).unwrap();
String::from_utf8(buf).unwrap()
}
/* NOTE: the input buffer must be null-terminated, i.e., *inbufend == 0 */
/* leading white-space is ignored */
Expand Down
38 changes: 4 additions & 34 deletions dpx/src/dpx_type0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ use crate::dpx_pdfobj::{
pdf_dict, pdf_get_version, pdf_link_obj, pdf_name, pdf_obj, pdf_ref_obj, pdf_stream, IntoObj,
STREAM_COMPRESS,
};
use crate::shims::sprintf;

#[derive(Clone)]
pub(crate) struct Type0Font {
Expand Down Expand Up @@ -424,54 +423,25 @@ pub(crate) unsafe fn Type0Font_cache_close() {
}
/* ******************************* COMPAT ********************************/
unsafe fn create_dummy_CMap() -> pdf_stream {
let mut buf: [u8; 32] = [0; 32];
let mut stream = pdf_stream::new(STREAM_COMPRESS);
stream.add_str("%!PS-Adobe-3.0 Resource-CMap\n%%DocumentNeededResources: ProcSet (CIDInit)\n%%IncludeResource: ProcSet (CIDInit)\n%%BeginResource: CMap (Adobe-Identity-UCS2)\n%%Title: (Adobe-Identity-UCS2 Adobe UCS2 0)\n%%Version: 1.0\n%%Copyright:\n%% ---\n%%EndComments\n\n");
stream.add_str("/CIDInit /ProcSet findresource begin\n\n12 dict begin\n\nbegincmap\n\n/CIDSystemInfo 3 dict dup begin\n /Registry (Adobe) def\n /Ordering (UCS2) def\n /Supplement 0 def\nend def\n\n/CMapName /Adobe-Identity-UCS2 def\n/CMapVersion 1.0 def\n/CMapType 2 def\n\n2 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n");
stream.add_str("\n100 beginbfrange\n");
for i in 0..0x64 {
let n = sprintf(
buf.as_mut_ptr() as *mut i8,
b"<%02X00> <%02XFF> <%02X00>\n\x00" as *const u8 as *const i8,
i,
i,
i,
) as usize;
stream.add_slice(&buf[..n]);
stream.add_str(&format!("<{0:02X}00> <{0:02X}FF> <{0:02X}00>\n", i));
}
stream.add_str("endbfrange\n\n");
stream.add_str("\n100 beginbfrange\n");
for i in 0x64..0xc8 {
let n = sprintf(
buf.as_mut_ptr() as *mut i8,
b"<%02X00> <%02XFF> <%02X00>\n\x00" as *const u8 as *const i8,
i,
i,
i,
) as usize;
stream.add_slice(&buf[..n]);
stream.add_str(&format!("<{0:02X}00> <{0:02X}FF> <{0:02X}00>\n", i));
}
stream.add_str("endbfrange\n\n");
stream.add_str("\n48 beginbfrange\n");
for i in 0xc8..=0xd7 {
let n = sprintf(
buf.as_mut_ptr() as *mut i8,
b"<%02X00> <%02XFF> <%02X00>\n\x00" as *const u8 as *const i8,
i,
i,
i,
) as usize;
stream.add_slice(&buf[..n]);
stream.add_str(&format!("<{0:02X}00> <{0:02X}FF> <{0:02X}00>\n", i));
}
for i in 0xe0..=0xff {
let n = sprintf(
buf.as_mut_ptr() as *mut i8,
b"<%02X00> <%02XFF> <%02X00>\n\x00" as *const u8 as *const i8,
i,
i,
i,
) as usize;
stream.add_slice(&buf[..n]);
stream.add_str(&format!("<{0:02X}00> <{0:02X}FF> <{0:02X}00>\n", i));
}
stream.add_str("endbfrange\n\n");
stream.add_str("endcmap\n\nCMapName currentdict /CMap defineresource pop\n\nend\nend\n\n%%EndResource\n%%EOF\n");
Expand Down
Loading

0 comments on commit f31a0af

Please sign in to comment.