mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
fix(type): resolve exact PostScript names in type.create
type.create stored the font field as a family name, so an exact PostScript face such as Arial-Black warned and fell back. Resolve only an exact PostScript match and keep that face's family, weight, slant, and name. Unknown families still warn. Fixes #8063 Signed-off-by: Gabrieljr42 <[email protected]>
This commit is contained in:
1 parent
872748fb5b
commit
99a234d2e2
2 files changed
+75
No files matched your search
@@ -243,6 +243,18 @@ pub fn apply_char_props(s: &mut CharStyle, p: &Value) -> bool {
|
||||
if let Some(v) = p.get("font").or_else(|| p.get("family")).and_then(Value::as_str) {
|
||||
s.font_family = v.to_string();
|
||||
s.postscript_name = None;
|
||||
// Photoshop accepts either a family name or a face's PostScript name in the font field.
|
||||
// Resolve only exact PostScript matches here so an unknown family remains untouched.
|
||||
let mut text = photocraft_text::shared().lock().unwrap_or_else(|e| e.into_inner());
|
||||
if !text.fonts.has_family(v) {
|
||||
let resolved = text.fonts.resolve_postscript(v);
|
||||
if resolved.exact {
|
||||
s.font_family = resolved.family;
|
||||
s.weight = resolved.weight;
|
||||
s.italic = resolved.italic;
|
||||
s.postscript_name = Some(v.to_string());
|
||||
}
|
||||
}
|
||||
hit(true);
|
||||
}
|
||||
if let Some(v) = p.get("fontStyle").and_then(Value::as_str) {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
//! `type.create` accepts an exact PostScript face name in its font field.
|
||||
|
||||
use photocraft_doc::{LayerContent, LayerId};
|
||||
use photocraft_engine::Session;
|
||||
use serde_json::json;
|
||||
|
||||
fn font_with_postscript_name(source: &[u8], postscript: &str) -> Vec<u8> {
|
||||
let mut bytes = source.to_vec();
|
||||
let count = u16::from_be_bytes(bytes[4..6].try_into().unwrap()) as usize;
|
||||
let name_record = (0..count).map(|i| 12 + i * 16).find(|r| &bytes[*r..r + 4] == b"name").unwrap();
|
||||
for i in 0..count {
|
||||
let r = 12 + i * 16;
|
||||
let offset = u32::from_be_bytes(bytes[r + 8..r + 12].try_into().unwrap()) as usize;
|
||||
if &bytes[r..r + 4] == b"OS/2" {
|
||||
bytes[offset + 4..offset + 6].copy_from_slice(&900u16.to_be_bytes());
|
||||
}
|
||||
}
|
||||
let names = [(1u16, "PhotoCraft Black Test"), (2, "Black"), (6, postscript), (16, "PhotoCraft Black Test"), (17, "Black")];
|
||||
let mut table = Vec::new();
|
||||
let mut strings = Vec::new();
|
||||
for value in [0u16, names.len() as u16, (6 + names.len() * 12) as u16] {
|
||||
table.extend(value.to_be_bytes());
|
||||
}
|
||||
for (id, value) in names {
|
||||
let encoded: Vec<_> = value.encode_utf16().flat_map(u16::to_be_bytes).collect();
|
||||
for n in [3u16, 1, 0x409, id, encoded.len() as u16, strings.len() as u16] {
|
||||
table.extend(n.to_be_bytes());
|
||||
}
|
||||
strings.extend(encoded);
|
||||
}
|
||||
table.extend(strings);
|
||||
let offset = bytes.len() as u32;
|
||||
bytes[name_record + 8..name_record + 12].copy_from_slice(&offset.to_be_bytes());
|
||||
bytes[name_record + 12..name_record + 16].copy_from_slice(&(table.len() as u32).to_be_bytes());
|
||||
bytes.extend(table);
|
||||
bytes
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn type_create_resolves_exact_postscript_font_names() {
|
||||
let ps = "PhotoCraft-Black-Test";
|
||||
photocraft_text::shared()
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.fonts
|
||||
.register_font_data(font_with_postscript_name(photocraft_text::fonts::INTER_REGULAR.as_slice(), ps));
|
||||
|
||||
let mut session = Session::new();
|
||||
session.execute("file.new", json!({"width": 200, "height": 100, "background": "transparent"})).unwrap();
|
||||
let response = session.execute("type.create", json!({"text": "Test", "font": ps})).unwrap();
|
||||
assert!(response["warnings"].as_array().is_none_or(Vec::is_empty), "{response}");
|
||||
let id = LayerId(response["layer"].as_u64().unwrap());
|
||||
let LayerContent::Text(text) = &session.active().unwrap().doc.layer(id).unwrap().content else { panic!("not a text layer") };
|
||||
let style = &text.char_runs()[0].style;
|
||||
assert_eq!(style.font_family, "PhotoCraft Black Test");
|
||||
assert_eq!(style.postscript_name.as_deref(), Some(ps));
|
||||
assert_eq!(style.weight, 900);
|
||||
|
||||
let regular = session.execute("type.create", json!({"text": "Test", "font": "Inter"})).unwrap();
|
||||
assert!(regular["warnings"].as_array().is_none_or(Vec::is_empty), "{regular}");
|
||||
let missing = session.execute("type.create", json!({"text": "Test", "font": "No Such Family 123"})).unwrap();
|
||||
assert_eq!(missing["warnings"], json!(["font 'No Such Family 123' is not installed; text may render using a fallback face"]));
|
||||
}
|
||||
Reference in new issue
Block a user