109 lines
3.8 KiB
Rust
109 lines
3.8 KiB
Rust
//! Day 3 mandatory test 4: a font whose licence forbids embedding (OS/2 fsType) is refused for PDF export, and the
|
|
//! error names the font and the reason. The test font is a system font with its fsType patched in memory.
|
|
use pictor_doc::export::{self, ExportError};
|
|
use pictor_doc::text::Embedding;
|
|
use pictor_doc::*;
|
|
|
|
fn system_font() -> Vec<u8> {
|
|
for p in [r"C:\Windows\Fonts\arial.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"] {
|
|
if let Ok(d) = std::fs::read(p) {
|
|
return d;
|
|
}
|
|
}
|
|
panic!("no system font to patch");
|
|
}
|
|
|
|
/// The font with OS/2 fsType set to `fs_type` (and version ≥ 2 so the bits mean what they say).
|
|
fn with_fs_type(mut data: Vec<u8>, fs_type: u16) -> Vec<u8> {
|
|
let n = u16::from_be_bytes([data[4], data[5]]) as usize;
|
|
for i in 0..n {
|
|
let rec = 12 + i * 16;
|
|
if &data[rec..rec + 4] == b"OS/2" {
|
|
let off = u32::from_be_bytes(data[rec + 8..rec + 12].try_into().unwrap()) as usize;
|
|
let version = u16::from_be_bytes([data[off], data[off + 1]]);
|
|
assert!(version >= 2, "the OS/2 table is too old for this test");
|
|
data[off + 8..off + 10].copy_from_slice(&fs_type.to_be_bytes());
|
|
return data;
|
|
}
|
|
}
|
|
panic!("no OS/2 table");
|
|
}
|
|
|
|
fn family_of(data: &[u8]) -> String {
|
|
let book = FontBook::empty();
|
|
assert!(book.add(data.to_vec()));
|
|
book.families().remove(0)
|
|
}
|
|
|
|
fn doc_with_text(family: &str) -> Document {
|
|
let mut d = Document::a4();
|
|
let id = d.new_id();
|
|
if let LayerKind::Vector { objects } = &mut d.pages[0].layers[0].kind {
|
|
objects.push(Object {
|
|
id,
|
|
name: String::new(),
|
|
transform: [1.0, 0.0, 0.0, 1.0, 50.0, 50.0],
|
|
opacity: 1.0,
|
|
visible: true,
|
|
locked: false,
|
|
fill: None,
|
|
stroke: None,
|
|
shape: Shape::Text {
|
|
w: 0.0,
|
|
text: "Embedding test".into(),
|
|
style: TextStyle {
|
|
family: family.into(),
|
|
weight: 400,
|
|
italic: false,
|
|
size: 14.0,
|
|
color: Rgba::BLACK,
|
|
align: Align::Left,
|
|
line_height: 1.2,
|
|
letter_spacing: 0.0,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
d
|
|
}
|
|
|
|
fn export_with(font: Vec<u8>) -> (String, Result<Vec<u8>, ExportError>) {
|
|
let family = family_of(&font);
|
|
let book = FontBook::empty();
|
|
assert!(book.add(font));
|
|
let r = export::pdf(&doc_with_text(&family), &book);
|
|
(family, r)
|
|
}
|
|
|
|
#[test]
|
|
fn a_restricted_font_is_refused_with_its_name_and_the_reason() {
|
|
let (family, r) = export_with(with_fs_type(system_font(), 0x0002));
|
|
match r {
|
|
Err(e @ ExportError::FontNotEmbeddable { .. }) => {
|
|
let msg = e.to_string();
|
|
assert!(msg.contains(&family), "{msg}");
|
|
assert!(msg.contains("forbids embedding"), "{msg}");
|
|
}
|
|
other => panic!("a restricted font was embedded: {:?}", other.map(|b| b.len())),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_bitmap_only_font_is_refused() {
|
|
let (_, r) = export_with(with_fs_type(system_font(), 0x0200));
|
|
assert!(matches!(r, Err(ExportError::FontNotEmbeddable { reason, .. }) if reason.contains("bitmap")));
|
|
}
|
|
|
|
#[test]
|
|
fn installable_editable_and_print_fonts_are_embedded() {
|
|
for fs in [0x0000u16, 0x0004, 0x0008] {
|
|
let (_, r) = export_with(with_fs_type(system_font(), fs));
|
|
let pdf = r.unwrap_or_else(|e| panic!("fsType {fs:#06x}: {e}"));
|
|
assert!(String::from_utf8_lossy(&pdf).contains("/FontFile2"));
|
|
}
|
|
let book = FontBook::empty();
|
|
assert!(book.add(with_fs_type(system_font(), 0x0004)));
|
|
let k = book.resolve(&book.families()[0], 400, false).unwrap();
|
|
assert_eq!(book.font(k).embedding, Embedding::PrintOnly);
|
|
}
|