Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

fix(pptx): refuse to embed fonts whose licence forbids outline embedding

Philipp Krügerfc31170346bf
diff --git a/Cargo.lock b/Cargo.lock
index 82c9f7cc8..c2af98737 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1082,7 +1082,7 @@ dependencies = [
[[package]]
name = "designcraft-pptx"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"designcraft-color",
"designcraft-compose",
diff --git a/crates/pptx/Cargo.toml b/crates/pptx/Cargo.toml
index fc8fbac71..c6e1c876f 100644
--- a/crates/pptx/Cargo.toml
+++ b/crates/pptx/Cargo.toml
@@ -21,4 +21,5 @@ thiserror = { workspace = true }
zip = { workspace = true }
[dev-dependencies]
+designcraft-fonts = { workspace = true, features = ["testing"] }
quick-xml = { workspace = true }
diff --git a/crates/pptx/src/fonts.rs b/crates/pptx/src/fonts.rs
index 4dceb6ea6..3d5eb4c9b 100644
--- a/crates/pptx/src/fonts.rs
+++ b/crates/pptx/src/fonts.rs
@@ -158,7 +158,7 @@ fn utf16_name(out: &mut Vec<u8>, s: &str) {
/// Why a face can't be embedded.
#[derive(Debug, PartialEq, Eq)]
pub enum NoEmbed {
- /// The font's licence (OS/2 `fsType`) forbids embedding.
+ /// The font's licence (OS/2 `fsType`) forbids embedding it, or embedding its outlines.
Restricted,
/// PowerPoint embeds TrueType outlines only (not CFF, collections or variable instances).
Format,
@@ -180,8 +180,9 @@ pub fn eot(face: &FontFace) -> Result<Vec<u8>, NoEmbed> {
let os2 = f.table_data(Tag::new(b"OS/2")).map(|t| t.as_bytes().to_vec()).unwrap_or_default();
let head = f.table_data(Tag::new(b"head")).map(|t| t.as_bytes().to_vec()).unwrap_or_default();
let fs_type = be16(&os2, 8).unwrap_or(0);
- // Restricted License embedding (bit 1 alone): may not be embedded.
- if fs_type & 0x000F == 0x0002 {
+ // Restricted License embedding, or Bitmap embedding only (bit 9): PowerPoint embeds the
+ // outlines, so neither may be embedded.
+ if face.restricted_licence() || fs_type & 0x0200 != 0 {
return Err(NoEmbed::Restricted);
}
let mut h: Vec<u8> = Vec::with_capacity(256);
diff --git a/crates/pptx/src/tests.rs b/crates/pptx/src/tests.rs
index 0ee9a61ce..98e78384e 100644
--- a/crates/pptx/src/tests.rs
+++ b/crates/pptx/src/tests.rs
@@ -269,3 +269,21 @@ fn fonts_embed_as_embedded_opentype() {
let ct = part(&mut z, "[Content_Types].xml");
assert!(ct.contains("application/x-fontdata"));
}
+
+#[test]
+fn fonts_whose_licence_forbids_outline_embedding_are_not_embedded() {
+ use designcraft_fonts::testing::{font_with, with_fs_type};
+ let db = designcraft_fonts::FontDb::with_font_dirs(vec![]);
+ // Restricted (0x0002, 0x0003 with reserved bit 0) and Bitmap embedding only (bit 9, with any
+ // other bits) refuse; Installable, Preview & Print and Editable embed.
+ for (fs_type, embeds) in [(0x0000, true), (0x0004, true), (0x0008, true), (0x0002, false), (0x0003, false), (0x0200, false), (0x0208, false)] {
+ let family = format!("PPTX Licence {fs_type:04x}");
+ assert_eq!(db.add_font(with_fs_type(font_with(&family, &['l']).unwrap(), fs_type).unwrap()), 1);
+ let r = crate::fonts::eot(&db.face(&family, "Regular"));
+ if embeds {
+ assert!(r.is_ok(), "fsType {fs_type:#06x}");
+ } else {
+ assert!(matches!(r, Err(crate::fonts::NoEmbed::Restricted)), "fsType {fs_type:#06x}");
+ }
+ }
+}