mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-05 19:51:56 -05:00
address review: shadings stay only for the names resourceless Type 3 glyphs of the holder's fonts draw
This commit is contained in:
@@ -1260,7 +1260,7 @@ fn xobject(
|
||||
}
|
||||
if let Some(data) = &new_data {
|
||||
let content = std::slice::from_ref(data);
|
||||
match prune_properties(doc, &mut scope.budget, &mut cleaned_res, content, inner.keeps_all_patterns) {
|
||||
match prune_properties(doc, &mut scope.budget, &mut cleaned_res, content) {
|
||||
Ok(retired) => out.retired_properties.extend(retired),
|
||||
Err(error) => {
|
||||
out.error = Some(error);
|
||||
|
||||
@@ -383,12 +383,13 @@ fn prune_retired(doc: &Document, res: &mut Dict, names: &[(&[u8], Vec<u8>)], con
|
||||
// Each is one page's content or one form's, with the allowance of a page.
|
||||
ops.extend(resource_ops(doc, &mut DecodeBudget::page(), res, content)?);
|
||||
}
|
||||
let glyph_shadings = glyph_shading_names(doc, &mut DecodeBudget::page(), res)?;
|
||||
let mut pruned = false;
|
||||
for (category, used) in RETIRED {
|
||||
let mut entries = res_dict(doc, res, category);
|
||||
let mut changed = false;
|
||||
for (_, name) in names.iter().filter(|(c, _)| *c == category) {
|
||||
if !ops.iter().any(|op| used(op) == Some(name)) {
|
||||
if !ops.iter().any(|op| used(op) == Some(name)) && !(category == b"Shading" && glyph_shadings.contains(name)) {
|
||||
changed |= entries.remove(name).is_some();
|
||||
}
|
||||
}
|
||||
@@ -514,31 +515,46 @@ pub(crate) fn resource_ops(doc: &Document, budget: &mut DecodeBudget, res: &Dict
|
||||
Ok(ops)
|
||||
}
|
||||
|
||||
/// The shading names the glyph programs of `res`'s Type 3 fonts without resources of their own draw
|
||||
/// (`/Name sh`): those fonts paint through the resources of whatever shows them.
|
||||
fn glyph_shading_names(doc: &Document, budget: &mut DecodeBudget, res: &Dict) -> Result<HashSet<Vec<u8>>, CosError> {
|
||||
let mut names = HashSet::new();
|
||||
for (_, font) in res_dict(doc, res, b"Font").iter() {
|
||||
let Some(font) = doc.resolve(font).as_dict().cloned() else { continue };
|
||||
if font.name(b"Subtype") != Some(b"Type3") || font.get(b"Resources").is_some_and(|r| doc.resolve(r).as_dict().is_some()) {
|
||||
continue;
|
||||
}
|
||||
let Some(procs) = font.get(b"CharProcs").and_then(|c| doc.resolve(c).as_dict().cloned()) else { continue };
|
||||
for (_, proc) in procs.iter() {
|
||||
let Object::Stream(s) = &*doc.resolve(proc) else { continue };
|
||||
let data = budget.decode(&doc.with_direct_parms(s))?;
|
||||
names.extend(pdfcraft_content::ops(&data).filter_map(|op| shading_used(&op).map(<[u8]>::to_vec)));
|
||||
}
|
||||
}
|
||||
Ok(names)
|
||||
}
|
||||
|
||||
/// Drop the property and shading entries of `res` that the rewritten `content` (and the forms
|
||||
/// drawing through `res`) no longer uses, returning their values so other holders can be unlinked.
|
||||
///
|
||||
/// `keep_shadings`: a Type 3 glyph without resources of its own was shown, and it paints with this
|
||||
/// scope's resources, so which shadings it draws is not known and none is retired.
|
||||
pub(crate) fn prune_properties(
|
||||
doc: &Document,
|
||||
budget: &mut DecodeBudget,
|
||||
res: &mut Dict,
|
||||
content: &[Vec<u8>],
|
||||
keep_shadings: bool,
|
||||
) -> Result<Vec<Object>, CosError> {
|
||||
/// A shading a Type 3 glyph of `res`'s fonts draws (such a font without resources of its own paints
|
||||
/// through these) stays, though no content names it.
|
||||
pub(crate) fn prune_properties(doc: &Document, budget: &mut DecodeBudget, res: &mut Dict, content: &[Vec<u8>]) -> Result<Vec<Object>, CosError> {
|
||||
if RETIRED.iter().all(|(category, _)| res_dict(doc, res, category).is_empty()) {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let mut retired = Vec::new();
|
||||
let ops = resource_ops(doc, budget, res, content)?;
|
||||
let glyph_shadings = glyph_shading_names(doc, budget, res)?;
|
||||
for (category, used) in RETIRED {
|
||||
let mut entries = res_dict(doc, res, category);
|
||||
if entries.is_empty() || (keep_shadings && category == b"Shading") {
|
||||
if entries.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let names: Vec<Vec<u8>> = entries.iter().map(|(n, _)| n.to_vec()).collect();
|
||||
for name in names {
|
||||
if !ops.iter().any(|op| used(op) == Some(&name))
|
||||
&& !(category == b"Shading" && glyph_shadings.contains(&name))
|
||||
&& let Some(value) = entries.remove(&name)
|
||||
{
|
||||
retired.push(value);
|
||||
|
||||
@@ -918,7 +918,7 @@ fn redact_page_content(
|
||||
// A Type 3 glyph without resources paints with patterns nobody tracked: keep them all.
|
||||
let painted = if out.keeps_all_patterns { &out.patterns_seen } else { &out.patterns_painted };
|
||||
report.patterns_removed += interp::prune_patterns(doc, &mut res, &out.patterns_seen, painted, pruned_patterns);
|
||||
retired_properties.extend(interp::prune_properties(doc, &mut budget, &mut res, &cleaned_data, out.keeps_all_patterns)?);
|
||||
retired_properties.extend(interp::prune_properties(doc, &mut budget, &mut res, &cleaned_data)?);
|
||||
type3::prune_type3_from_resources(doc, &mut budget, &mut res, &cleaned_data, Some(&page.dict))?;
|
||||
doc.update_dict(page.obj, |d| {
|
||||
d.set(b"Contents".to_vec(), Object::Array(new_list));
|
||||
|
||||
@@ -716,7 +716,7 @@ fn content_pass(doc: &mut Document, text: bool, layers: bool, write: bool) -> Re
|
||||
crate::interp::prune(doc, &mut budget, &mut xo, &out.replaced, &page_content)?;
|
||||
res.set(b"XObject".to_vec(), Object::Dict(xo));
|
||||
}
|
||||
retired_properties.extend(crate::interp::prune_properties(doc, &mut budget, &mut res, &page_content, out.keeps_all_patterns)?);
|
||||
retired_properties.extend(crate::interp::prune_properties(doc, &mut budget, &mut res, &page_content)?);
|
||||
crate::type3::prune_type3_from_resources(doc, &mut budget, &mut res, &page_content, Some(&page.dict))?;
|
||||
doc.update_dict(page.obj, |d| {
|
||||
d.set(b"Contents".to_vec(), Object::Array(new_list));
|
||||
|
||||
@@ -150,6 +150,27 @@ fn a_shading_a_resourceless_type3_glyph_draws_stays_in_the_page_resources() {
|
||||
assert!(full_save_holds(&doc, b"GLYPH_SHADING_MESH"), "the glyph's shading was unlinked");
|
||||
}
|
||||
|
||||
/// Only the shadings a resourceless Type 3 glyph names are kept for it: another shading removed
|
||||
/// under the mark still leaves the file.
|
||||
#[test]
|
||||
fn a_resourceless_type3_glyph_does_not_keep_a_shading_it_does_not_draw() {
|
||||
let mesh = "/ShadingType 4 /ColorSpace /DeviceRGB /BitsPerCoordinate 8 /BitsPerComponent 8 /BitsPerFlag 8 /Decode [0 300 0 300 0 1 0 1 0 1]";
|
||||
let mut doc = pdf(vec![
|
||||
b"<< /Type /Catalog /Pages 2 0 R >>".to_vec(),
|
||||
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec(),
|
||||
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents 4 0 R /Resources 5 0 R >>".to_vec(),
|
||||
stream("", b"/Sh1 sh BT /T3 10 Tf 200 200 Td (A) Tj ET"),
|
||||
b"<< /Font << /T3 6 0 R >> /Shading << /Sh1 8 0 R /Sh2 9 0 R >> >>".to_vec(),
|
||||
TYPE3.as_bytes().to_vec(),
|
||||
stream("", b"/Sh2 sh"),
|
||||
stream(mesh, b"REMOVED_SHADING_MESH"),
|
||||
stream(mesh, b"GLYPH_SHADING_MESH"),
|
||||
]);
|
||||
mark(&mut doc, 0, &[[0.0, 0.0, 300.0, 300.0]], "");
|
||||
apply(&mut doc, None).unwrap();
|
||||
assert!(!full_save_holds(&doc, b"REMOVED_SHADING_MESH"), "a shading no glyph draws was kept");
|
||||
}
|
||||
|
||||
/// A Type 3 font whose displayed glyphs are redacted has its unused glyph programs unlinked from
|
||||
/// /CharProcs and exclusive graphical resources pruned, so they do not survive in a full save (F-011).
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user