Skip to content

Commit

Permalink
Actual fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Aug 15, 2024
1 parent bac2f51 commit 3a8ae26
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
41 changes: 30 additions & 11 deletions src/cff/dict/font_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub fn rewrite_font_dict_index(
offsets: &mut Offsets,
metadata: &CIDMetadata,
w: &mut Writer,
top_dict_is_missing_font_matrix: bool,
) -> Result<()> {
let mut dicts = vec![];

Expand All @@ -77,15 +78,34 @@ pub fn rewrite_font_dict_index(
let dict = metadata.font_dicts.get(old_df as usize).ok_or(SubsetError)?;
let mut w = Writer::new();

// See comment in `generate_font_dict_index`.
w.write(dict.font_matrix.as_ref().unwrap_or(&[
Number::one(),
Number::zero(),
Number::zero(),
Number::one(),
Number::zero(),
Number::zero(),
]));
// See comment in `rewrite_top_dict_index`.
w.write(
dict.font_matrix
.map(|m| {
if top_dict_is_missing_font_matrix {
[
// TODO: Is that the proper way to scale x1000? But all font matrices only seem to have
// a scale, anyway
Number::from_f32((m[0].as_f64() * 1000.0) as f32),
Number::zero(),
Number::zero(),
Number::from_f32((m[3].as_f64() * 1000.0) as f32),
Number::zero(),
Number::zero(),
]
} else {
m
}
})
.unwrap_or([
Number::one(),
Number::zero(),
Number::zero(),
Number::one(),
Number::zero(),
Number::zero(),
]),
);
w.write(FONT_MATRIX);

// Write the length and offset of the private dict.
Expand Down Expand Up @@ -126,8 +146,7 @@ pub fn rewrite_font_dict_index(
pub fn generate_font_dict_index(offsets: &mut Offsets, w: &mut Writer) -> Result<()> {
let mut sub_w = Writer::new();

// Similarly to ghostscript, write a default font matrix. Fixes issues for some printers
// https://leahneukirchen.org/blog/archive/2022/10/50-blank-pages-or-black-box-debugging-of-pdf-rendering-in-printers.html
// See comment in `rewrite_top_dict_index`
sub_w.write([
Number::one(),
Number::zero(),
Expand Down
7 changes: 6 additions & 1 deletion src/cff/dict/top_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ pub fn rewrite_top_dict_index(
sub_w.write(FONT_NAME);
}

// Write a default font matrix, if it does not exist.
// See https://bugs.ghostscript.com/show_bug.cgi?id=690724#c12 and https://leahneukirchen.org/blog/archive/2022/10/50-blank-pages-or-black-box-debugging-of-pdf-rendering-in-printers.html
// We assume that if at least one font dict has a matrix, all of them do.
// Case 1: Top DICT MATRIX is some, FONT DICT MATRIX is some -> supplied, supplied
// Case 2: Top DICT MATRIX is none, FONT DICT MATRIX is some -> (0.001, 0, 0, 0.001, 0, 0), supplied * 1000
// Case 3: Top DICT MATRIX is some, FONT DICT MATRIX is none -> supplied, (1, 0, 0, 1, 0, 0)
// Case 4: Top DICT MATRIX is none, FONT DICT MATRIX is none -> (0.001, 0, 0, 0.001, 0 0), (1, 0, 0, 1, 0, 0)
sub_w.write(top_dict_data.font_matrix.as_ref().unwrap_or(&[
Number::from_f32(0.001),
Number::zero(),
Expand Down
1 change: 1 addition & 0 deletions src/cff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ pub fn subset(ctx: &mut Context<'_>) -> Result<()> {
&mut offsets,
cid_metadata,
&mut w,
table.top_dict_data.font_matrix.is_none(),
)?,
}

Expand Down

0 comments on commit 3a8ae26

Please sign in to comment.