Skip to content

Commit f179670

Browse files
committed
Add secure option for saving, add more opts + cleanup
1 parent 16ba6a6 commit f179670

File tree

8 files changed

+321
-221
lines changed

8 files changed

+321
-221
lines changed

src/cmap.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/// ToUnicode CMap parsing
2-
32
use std::collections::BTreeMap;
3+
44
use lopdf::{Dictionary, Document, Object};
5+
56
use crate::text::CMap;
67

78
/// The mapping from a CID to one or more Unicode code points.
@@ -97,7 +98,7 @@ fn parse_hex_token(token: &str) -> Result<u32, String> {
9798
return Err("Hex token too short".into());
9899
}
99100
if token.starts_with('<') && token.ends_with('>') {
100-
let inner = &token[1..token.len()-1];
101+
let inner = &token[1..token.len() - 1];
101102
u32::from_str_radix(inner, 16)
102103
.map_err(|e| format!("Failed to parse hex token {}: {}", token, e))
103104
} else {
@@ -113,7 +114,7 @@ impl CMap for ToUnicodeCMap {
113114
let mut result = String::new();
114115
let mut i = 0;
115116
while i + 1 < bytes.len() {
116-
let cid = u16::from_be_bytes([bytes[i], bytes[i+1]]) as u32;
117+
let cid = u16::from_be_bytes([bytes[i], bytes[i + 1]]) as u32;
117118
if let Some(unis) = self.mappings.get(&cid) {
118119
for &u in unis {
119120
if let Some(ch) = std::char::from_u32(u) {
@@ -133,23 +134,26 @@ pub fn get_to_unicode_cmap_from_font(
133134
font_dict: &Dictionary,
134135
doc: &Document,
135136
) -> Result<ToUnicodeCMap, String> {
136-
137-
let to_unicode_obj = font_dict.get(b"ToUnicode").ok()
138-
.ok_or("No ToUnicode entry found")?;
137+
let to_unicode_obj = font_dict
138+
.get(b"ToUnicode")
139+
.ok()
140+
.ok_or("No ToUnicode entry found")?;
139141

140142
let stream = match to_unicode_obj {
141-
Object::Reference(r) => doc.get_object(*r)
143+
Object::Reference(r) => doc
144+
.get_object(*r)
142145
.and_then(|obj| obj.as_stream().map(|s| s.clone()))
143146
.map_err(|e| format!("Error getting ToUnicode stream: {}", e))?,
144147
Object::Stream(s) => s.clone(),
145148
_ => return Err("Unexpected type for ToUnicode entry".into()),
146149
};
147150

148-
let content = stream.decompressed_content()
151+
let content = stream
152+
.decompressed_content()
149153
.map_err(|e| format!("Decompress error: {}", e))?;
150154

151-
let cmap_str = String::from_utf8(content)
152-
.map_err(|e| format!("UTF-8 conversion error: {}", e))?;
155+
let cmap_str =
156+
String::from_utf8(content).map_err(|e| format!("UTF-8 conversion error: {}", e))?;
153157

154158
ToUnicodeCMap::parse(&cmap_str)
155159
}

src/color.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,40 @@ impl Color {
8888
}
8989
}
9090

91+
pub fn get_svg_id(&self) -> String {
92+
match self {
93+
Color::Rgb(rgb) => {
94+
let r = (rgb.r * 255.0).round() as u8;
95+
let g = (rgb.g * 255.0).round() as u8;
96+
let b = (rgb.b * 255.0).round() as u8;
97+
format!("rgb({}, {}, {})", r, g, b)
98+
}
99+
Color::Cmyk(cmyk) => {
100+
let r = (1.0 - cmyk.c) * (1.0 - cmyk.k);
101+
let g = (1.0 - cmyk.m) * (1.0 - cmyk.k);
102+
let b = (1.0 - cmyk.y) * (1.0 - cmyk.k);
103+
let r = (r * 255.0).round() as u8;
104+
let g = (g * 255.0).round() as u8;
105+
let b = (b * 255.0).round() as u8;
106+
format!("rgb({}, {}, {})", r, g, b)
107+
}
108+
Color::Greyscale(gs) => {
109+
let gray = (gs.percent * 255.0).round() as u8;
110+
format!("rgb({}, {}, {})", gray, gray, gray)
111+
}
112+
Color::SpotColor(spot) => {
113+
// SpotColor is treated the same as CMYK.
114+
let r = (1.0 - spot.c) * (1.0 - spot.k);
115+
let g = (1.0 - spot.m) * (1.0 - spot.k);
116+
let b = (1.0 - spot.y) * (1.0 - spot.k);
117+
let r = (r * 255.0).round() as u8;
118+
let g = (g * 255.0).round() as u8;
119+
let b = (b * 255.0).round() as u8;
120+
format!("rgb({}, {}, {})", r, g, b)
121+
}
122+
}
123+
}
124+
91125
/// Returns if the color has an icc profile attached
92126
pub fn get_icc_profile(&self) -> Option<&Option<IccProfileId>> {
93127
match *self {

src/graphics.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,15 @@ impl LineDashPattern {
359359
.collect()
360360
}
361361

362+
pub fn get_svg_id(&self) -> String {
363+
let dash_array = self.as_array();
364+
dash_array
365+
.iter()
366+
.map(|num| num.to_string())
367+
.collect::<Vec<_>>()
368+
.join(",")
369+
}
370+
362371
/// Builds a `LineDashPattern` from a slice of up to 6 integers.
363372
///
364373
/// - The array is interpreted in dash-gap pairs:
@@ -448,6 +457,13 @@ impl LineJoinStyle {
448457
LineJoinStyle::Bevel => 2,
449458
}
450459
}
460+
pub fn to_svg_string(&self) -> &'static str {
461+
match self {
462+
LineJoinStyle::Miter => "miter",
463+
LineJoinStyle::Round => "round",
464+
LineJoinStyle::Bevel => "bevel",
465+
}
466+
}
451467
}
452468

453469
/// The text rendering mode determines how a text is drawn
@@ -520,6 +536,14 @@ impl LineCapStyle {
520536
LineCapStyle::ProjectingSquare => 2,
521537
}
522538
}
539+
540+
pub fn get_svg_id(&self) -> &'static str {
541+
match self {
542+
LineCapStyle::Butt => "butt",
543+
LineCapStyle::Round => "round",
544+
LineCapStyle::ProjectingSquare => "square",
545+
}
546+
}
523547
}
524548

525549
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use serde_derive::{Deserialize, Serialize};
66

77
/// Link / bookmark annotation handling
88
pub mod annotation;
9-
pub mod text;
109
pub mod cmap;
10+
pub mod text;
1111
pub mod wasm;
1212
pub use annotation::*;
1313
/// PDF standard handling

0 commit comments

Comments
 (0)