Ship named PDF color spaces and patterns from native color stacks
This commit is contained in:
1 parent
745c33fd44
commit
e9893a5c1b
5 files changed
+123
-6
No files matched your search
@@ -11,6 +11,7 @@ pub struct PdfColorProgram {
|
||||
pub(crate) fill: Option<[u8; 3]>,
|
||||
pub(crate) stroke: Option<[u8; 3]>,
|
||||
pub(crate) graphics_states: Vec<Vec<u8>>,
|
||||
uses_pdf_color_space: bool,
|
||||
pub(crate) xdv_resources: Vec<XdvColorResource>,
|
||||
}
|
||||
|
||||
@@ -141,19 +142,59 @@ fn color_component(token: &str) -> bool {
|
||||
&& token.parse::<f64>().is_ok_and(f64::is_finite)
|
||||
}
|
||||
|
||||
fn color_tokens(literal: &str) -> impl Iterator<Item = &str> {
|
||||
let pdf_space = |c| matches!(c, '\0' | '\t' | '\n' | '\r' | '\x0c' | ' ');
|
||||
let mut rest = literal;
|
||||
std::iter::from_fn(move || {
|
||||
loop {
|
||||
rest = rest.trim_start_matches(pdf_space);
|
||||
if !rest.starts_with('%') {
|
||||
break;
|
||||
}
|
||||
rest = rest.find(['\r', '\n']).map_or("", |end| &rest[end..]);
|
||||
}
|
||||
if rest.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let start = usize::from(rest.starts_with('/'));
|
||||
let end = rest[start..]
|
||||
.find(|c| pdf_space(c) || matches!(c, '/' | '%'))
|
||||
.map_or(rest.len(), |offset| start + offset);
|
||||
let (token, tail) = rest.split_at(end);
|
||||
rest = tail;
|
||||
Some(token)
|
||||
})
|
||||
}
|
||||
|
||||
impl PdfColorProgram {
|
||||
pub(crate) fn needs_pdf_for_preview(&self) -> bool {
|
||||
self.uses_pdf_color_space || !self.xdv_resources.is_empty()
|
||||
}
|
||||
|
||||
pub(crate) fn parse(literal: String) -> Result<Self, String> {
|
||||
let mut fill = None;
|
||||
let mut stroke = None;
|
||||
let mut operands = Vec::with_capacity(4);
|
||||
let mut graphics_states = Vec::new();
|
||||
let mut uses_pdf_color_space = false;
|
||||
let mut name = None;
|
||||
for token in literal.split_ascii_whitespace() {
|
||||
for token in color_tokens(&literal) {
|
||||
if token == "gs" && operands.is_empty() {
|
||||
graphics_states.push(name.take().ok_or("PDF gs requires a resource name")?);
|
||||
} else if matches!(token, "cs" | "CS") && operands.is_empty() {
|
||||
name.take().ok_or("PDF color-space operator requires one name")?;
|
||||
uses_pdf_color_space = true;
|
||||
} else if matches!(token, "sc" | "SC" | "scn" | "SCN") {
|
||||
let allows_pattern = matches!(token, "scn" | "SCN");
|
||||
let pattern = name.take().is_some();
|
||||
if (!pattern && operands.is_empty()) || (pattern && !allows_pattern) {
|
||||
return Err(format!("invalid operands for PDF color operator {token}"));
|
||||
}
|
||||
operands.clear();
|
||||
uses_pdf_color_space = true;
|
||||
} else if name.is_some() {
|
||||
return Err("PDF resource name must be followed by gs".into());
|
||||
} else if token.starts_with('/') && operands.is_empty() {
|
||||
return Err("unexpected token after PDF color resource name".into());
|
||||
} else if token.starts_with('/') {
|
||||
name = Some(super::page_resources::parse_name(token.as_bytes())?);
|
||||
} else if matches!(token, "g" | "G" | "rg" | "RG" | "k" | "K") {
|
||||
let rgb = match (token, operands.as_slice()) {
|
||||
@@ -174,7 +215,7 @@ impl PdfColorProgram {
|
||||
operands.clear();
|
||||
} else {
|
||||
let number = token.strip_prefix(['+', '-']).unwrap_or(token);
|
||||
if operands.len() == 4
|
||||
if operands.len() == super::xdv_navigation::MAX_COLOR_COMPONENTS
|
||||
|| !number
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_digit() || byte == b'.')
|
||||
@@ -199,6 +240,7 @@ impl PdfColorProgram {
|
||||
fill,
|
||||
stroke,
|
||||
graphics_states,
|
||||
uses_pdf_color_space,
|
||||
xdv_resources: Vec::new(),
|
||||
})
|
||||
}
|
||||
@@ -209,6 +251,7 @@ impl PdfColorProgram {
|
||||
fill: None,
|
||||
stroke: None,
|
||||
graphics_states: Vec::new(),
|
||||
uses_pdf_color_space: false,
|
||||
xdv_resources,
|
||||
}
|
||||
}
|
||||
@@ -295,6 +338,7 @@ impl Default for PdfColorStacks {
|
||||
fill: Some([0; 3]),
|
||||
stroke: Some([0; 3]),
|
||||
graphics_states: Vec::new(),
|
||||
uses_pdf_color_space: false,
|
||||
xdv_resources: Vec::new(),
|
||||
}),
|
||||
saved: Vec::new(),
|
||||
|
||||
@@ -354,11 +354,11 @@ impl DisplayList {
|
||||
/// Whether this page needs the complete PDF device output for SVG preview.
|
||||
pub fn has_embedded_pdf(&self) -> bool {
|
||||
self.pdf_resources.needs_pdf_for_preview
|
||||
|| self.pdf_background.as_ref().is_some_and(|program| !program.xdv_resources.is_empty())
|
||||
|| self.pdf_background.as_ref().is_some_and(|program| program.needs_pdf_for_preview())
|
||||
|| self
|
||||
.items
|
||||
.iter()
|
||||
.any(|item| matches!(item, DisplayItem::PdfColor { program, .. } if !program.xdv_resources.is_empty())
|
||||
.any(|item| matches!(item, DisplayItem::PdfColor { program, .. } if program.needs_pdf_for_preview())
|
||||
|| matches!(item, DisplayItem::PdfImage { .. } | DisplayItem::PdfForm { .. }
|
||||
| DisplayItem::PdfGraphics {
|
||||
operation: crate::pdf_device::PdfGraphics::Literal { .. },
|
||||
|
||||
Reference in new issue
Block a user