Skip to content

Commit

Permalink
Fix some smaller issues
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed May 31, 2024
1 parent 6ae1b5b commit b17b6ae
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 29 deletions.
15 changes: 9 additions & 6 deletions src/cff/cid_font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,15 @@ pub fn rewrite_fd_index(

/// Write the FD INDEX for CID-keyed fonts. They all get mapped to the font DICT 0.
pub fn generate_fd_index(gid_remapper: &GlyphRemapper, w: &mut Writer) -> Result<()> {
// We always use format 0, since it's the simplest.
// Format
w.write::<u8>(3);
// nRanges
w.write::<u16>(1);
// first
w.write::<u16>(0);
// fd index
w.write::<u8>(0);

for _ in 0..gid_remapper.num_gids() {
w.write::<u8>(0);
}

// sentinel
w.write::<u16>(gid_remapper.num_gids());
Ok(())
}
14 changes: 13 additions & 1 deletion src/cff/dict/font_dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ 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
sub_w.write(&[
Number::one(),
Number::zero(),
Number::zero(),
Number::one(),
Number::zero(),
Number::zero(),
]);
sub_w.write(FONT_MATRIX);

// Write the length and offset of the private dict.
// Private dicts have already been written, so the offsets are already correct.
// This means that these two offsets are a bit special compared to the others, since
Expand All @@ -140,7 +152,7 @@ pub fn generate_font_dict_index(offsets: &mut Offsets, w: &mut Writer) -> Result
.value
.write_as_5_bytes(&mut sub_w);

sub_w.write(dict::operators::PRIVATE);
sub_w.write(PRIVATE);
w.write(create_index(vec![sub_w.finish()])?);

Ok(())
Expand Down
15 changes: 10 additions & 5 deletions src/cff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ impl Offsets {

pub fn subset(ctx: &mut Context<'_>) -> Result<()> {
let table = Table::parse(ctx).unwrap();
let sid_remapper = get_sid_remapper(&table).ok_or(SubsetError)?;

// NOTE: The charstrings are already in the new order that they need be written in.
let (char_strings, fd_remapper) = subset_charstrings(&table, &ctx.mapper)?;

let sid_remapper = get_sid_remapper(&table, &fd_remapper).ok_or(SubsetError)?;

let mut offsets = match &table.font_kind {
FontKind::Sid(_) => Offsets::new_sid(),
FontKind::Cid(_) => Offsets::new_cid(fd_remapper.len()),
Expand All @@ -145,8 +146,7 @@ pub fn subset(ctx: &mut Context<'_>) -> Result<()> {
let mut subsetted_font = {
let mut w = Writer::new();
// HEADER
// TODO: Update offsize
w.write(table.header);
w.write([1u8, 0, 4, 4]);
// Name INDEX
w.write(table.names);
// Top DICT INDEX
Expand Down Expand Up @@ -301,7 +301,10 @@ fn subset_charstrings<'a>(
}

/// Get the SID remapper
fn get_sid_remapper<'a>(table: &Table<'a>) -> Option<SidRemapper<'a>> {
fn get_sid_remapper<'a>(
table: &Table<'a>,
fd_remapper: &FontDictRemapper,
) -> Option<SidRemapper<'a>> {
let mut sid_remapper = SidRemapper::new();
sid_remapper.remap(b"Adobe");
sid_remapper.remap(b"Identity");
Expand Down Expand Up @@ -334,7 +337,9 @@ fn get_sid_remapper<'a>(table: &Table<'a>) -> Option<SidRemapper<'a>> {
// Since we turn SIDs into GIDs, nothing to do here.
FontKind::Sid(_) => {}
FontKind::Cid(ref cid) => {
for font_dict in &cid.font_dicts {
for font_dict in fd_remapper.sorted_iter() {
let font_dict = cid.font_dicts.get(font_dict as usize)?;

if let Some(font_name) = font_dict.font_name {
remap_sid(font_name)?;
}
Expand Down
12 changes: 1 addition & 11 deletions src/cff/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Writeable for RealNumber {
fn write(&self, w: &mut Writer) {
let mut nibbles = vec![];

let string_form = format!("{:e}", self.0);
let string_form = format!("{}", self.0);
let mut r = Reader::new(string_form.as_bytes());

while !r.at_end() {
Expand All @@ -111,16 +111,6 @@ impl Writeable for RealNumber {
match byte {
b'0'..=b'9' => nibbles.push(byte - 48),
b'.' => nibbles.push(0xA),
b'e' => {
let next = r.peak::<u8>().unwrap();

if next == b'-' {
r.read::<u8>().unwrap();
nibbles.push(0xC);
} else {
nibbles.push(0xb);
}
}
b'-' => nibbles.push(0xE),
_ => unreachable!(),
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod font_tools;
type Result<T> = std::result::Result<T, Box<dyn Error>>;

const FONT_TOOLS_REF: bool = false;
const OVERWRITE_REFS: bool = true;
const OVERWRITE_REFS: bool = false;

struct TestContext {
font: Vec<u8>,
Expand Down
5 changes: 3 additions & 2 deletions tests/ttx/LatinModernRoman-Regular_1.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="2.004"/>
<checkSumAdjustment value="0x3a41a448"/>
<checkSumAdjustment value="0xbb4babf0"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00000011"/>
<unitsPerEm value="1000"/>
Expand Down Expand Up @@ -112,9 +112,10 @@
<CIDFontType value="0"/>
<CIDCount value="65535"/>
<!-- charset is dumped separately as the 'GlyphOrder' element -->
<FDSelect format="0"/>
<FDSelect format="3"/>
<FDArray>
<FontDict index="0">
<FontMatrix value="1 0 0 1 0 0"/>
<Private>
<BlueValues value="-22 0 431 448 666 677 683 705"/>
<BlueScale value="0.04546"/>
Expand Down
5 changes: 3 additions & 2 deletions tests/ttx/NewCMMath-Regular_1.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="2.0"/>
<checkSumAdjustment value="0xf7280eca"/>
<checkSumAdjustment value="0xc9a635b6"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00001011"/>
<unitsPerEm value="1000"/>
Expand Down Expand Up @@ -120,9 +120,10 @@ This work is released under the GUST Font License -- see http://tug.org/fonts/li
<CIDFontType value="0"/>
<CIDCount value="65535"/>
<!-- charset is dumped separately as the 'GlyphOrder' element -->
<FDSelect format="0"/>
<FDSelect format="3"/>
<FDArray>
<FontDict index="0">
<FontMatrix value="1 0 0 1 0 0"/>
<Private>
<BlueValues value="-22 0 431 448 666 666 677 705"/>
<OtherBlues value="-206 -205"/>
Expand Down
2 changes: 1 addition & 1 deletion tests/ttx/NotoSansCJKsc-Regular_1.ttx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<!-- Most of this table will be recalculated by the compiler -->
<tableVersion value="1.0"/>
<fontRevision value="2.004"/>
<checkSumAdjustment value="0x7627ceb7"/>
<checkSumAdjustment value="0xf75f3f09"/>
<magicNumber value="0x5f0f3cf5"/>
<flags value="00000000 00000011"/>
<unitsPerEm value="1000"/>
Expand Down

0 comments on commit b17b6ae

Please sign in to comment.