Skip to content

Commit

Permalink
Fix the number encoding in Type 2
Browse files Browse the repository at this point in the history
IvanUkhov committed Dec 20, 2024
1 parent e2ed35e commit 29a304b
Showing 4 changed files with 39 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/type2/number.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::Result;

const FIXED_SCALING: f32 = 1f32 / (1 << 16) as f32;
const SCALE: f32 = (1 << 16) as f32;

pub fn read<T: crate::tape::Read>(tape: &mut T) -> Result<f32> {
let first = tape.take::<u8>()?;
@@ -9,7 +9,7 @@ pub fn read<T: crate::tape::Read>(tape: &mut T) -> Result<f32> {
0xf7..=0xfa => ((first as i32 - 247) * 256 + tape.take::<u8>()? as i32 + 108) as f32,
0xfb..=0xfe => (-(first as i32 - 251) * 256 - tape.take::<u8>()? as i32 - 108) as f32,
0x1c => tape.take::<u16>()? as i16 as i32 as f32,
0xff => FIXED_SCALING * (tape.take::<u32>()? as f32),
0xff => (tape.take::<i32>()? as f32) / SCALE,
_ => raise!("found a malformed number"),
})
}
Binary file added tests/fixtures/Hirakatana-Regular.otf
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/support/mod.rs
Original file line number Diff line number Diff line change
@@ -10,13 +10,15 @@ use postscript::value::Read;
macro_rules! ok(($result:expr) => ($result.unwrap()));

pub enum Fixture {
Hirakatana,
NotoSansJP,
SourceSerifPro,
}

impl Fixture {
pub fn path(&self) -> PathBuf {
let file_name = match *self {
Fixture::Hirakatana => "Hirakatana-Regular.otf",
Fixture::NotoSansJP => "NotoSansJP-Regular.otf",
Fixture::SourceSerifPro => "SourceSerifPro-Regular.otf",
};
@@ -25,6 +27,7 @@ impl Fixture {

pub fn offset(&self) -> u64 {
match *self {
Fixture::Hirakatana => 1524,
Fixture::NotoSansJP => 337316,
Fixture::SourceSerifPro => 17732,
}
36 changes: 34 additions & 2 deletions tests/type2.rs
Original file line number Diff line number Diff line change
@@ -10,14 +10,46 @@ macro_rules! operations(
});
);

mod hirakatana {
use postscript::compact1::font_set::Record;
use postscript::type2::Program;

use crate::support::{setup_font_set, Fixture};

#[test]
fn one() {
let set = setup_font_set(Fixture::Hirakatana);
let code = &set.character_strings[0][10];
let global = &set.subroutines;
let local = match &set.records[0] {
Record::CharacterNameKeyed(ref record) => &*record.subroutines,
_ => unreachable!(),
};
let mut program = Program::new(code, global, local);
let mut operations = vec![];
while let Some(operation) = ok!(program.next()) {
operations.push(operation);
}
assert_eq!(
operations,
operations!(
HStem: [372.0, 57.933594],
RMoveTo: [42.0, 401.05176],
VLineTo: [-29.051758, 186.59766],
RLineTo: [186.59668, 0.0, -0.18261719, 28.88086, -0.18261719, 28.88086, -186.41504, 0.171875, -186.41406, 0.17089844],
)
);
}
}

mod source_serif {
use postscript::compact1::font_set::Record;
use postscript::type2::Program;

use crate::support::{setup_font_set, Fixture};

#[test]
fn program_all() {
fn all() {
let set = setup_font_set(Fixture::SourceSerifPro);
let global = &set.subroutines;
let local = match &set.records[0] {
@@ -31,7 +63,7 @@ mod source_serif {
}

#[test]
fn program_one() {
fn one() {
let set = setup_font_set(Fixture::SourceSerifPro);
let code = &set.character_strings[0][134];
let global = &set.subroutines;

0 comments on commit 29a304b

Please sign in to comment.