mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 23:02:31 +00:00
craft-drawingml: drop theme font panose when the typeface changes; more theme tests
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
88c52213b4
commit
a2d2eefa3c
@@ -286,6 +286,38 @@ mod tests {
|
||||
assert!(back.sys_colors.is_empty() && back.fonts.minor.latin_attrs.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn font_panose_is_dropped_when_the_typeface_changes() {
|
||||
let mut t = crate::theme::read_theme(&mut Part::new(), &parse(THEME_FILE.as_bytes()).unwrap());
|
||||
t.fonts.minor.latin = "Arial".into();
|
||||
let (xml, _) = theme_round_trip(&t);
|
||||
assert!(xml.contains(r#"<a:latin typeface="Arial"/>"#), "{xml}");
|
||||
assert!(!xml.contains("020F0502020204030204"), "{xml}");
|
||||
assert!(xml.contains(r#"<a:latin typeface="Calibri Light" panose="020F0302020204030204""#), "the unchanged major font keeps it: {xml}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn system_colour_without_last_clr_gets_it_and_an_unknown_name_becomes_srgb() {
|
||||
let file = THEME_FILE
|
||||
.replace(r#"<a:sysClr val="windowText" lastClr="000000"/>"#, r#"<a:sysClr val="windowText"/>"#)
|
||||
.replace(r#"<a:sysClr val="window" lastClr="FFFFFF"/>"#, r#"<a:sysClr val="notAColor" lastClr="123456"/>"#);
|
||||
let t = crate::theme::read_theme(&mut Part::new(), &parse(file.as_bytes()).unwrap());
|
||||
let (xml, _) = theme_round_trip(&t);
|
||||
assert!(xml.contains(r#"<a:dk1><a:sysClr val="windowText" lastClr="000000"/></a:dk1>"#), "{xml}");
|
||||
assert!(xml.contains(r#"<a:lt1><a:srgbClr val="123456"/></a:lt1>"#), "{xml}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_colours_and_family_id_without_object_defaults_follow_an_empty_one() {
|
||||
let file = THEME_FILE.replace("<a:objectDefaults/><a:extraClrSchemeLst/>", "");
|
||||
let t = crate::theme::read_theme(&mut Part::new(), &parse(file.as_bytes()).unwrap());
|
||||
let (xml, back) = theme_round_trip(&t);
|
||||
let at = |n: &str| xml.find(n).unwrap_or_else(|| panic!("{n} in {xml}"));
|
||||
assert!(at("</a:themeElements>") < at("<a:objectDefaults") && at("<a:objectDefaults") < at("<a:custClrLst>"), "{xml}");
|
||||
assert!(at("<a:custClrLst>") < at("<a:extLst><a:ext uri=\"{05A4"), "{xml}");
|
||||
assert_eq!(back.raw_extra, t.raw_extra);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_styles_keep_def_ppr_in_every_place_they_are_written() {
|
||||
use crate::text::{ListStyle, TextBody};
|
||||
|
||||
@@ -28,6 +28,8 @@ pub struct FontSet {
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct TypefaceAttrs {
|
||||
/// The typeface name these attributes belong to; the writer drops them once the typeface changed.
|
||||
pub typeface: String,
|
||||
/// Ten hex byte pairs, as written.
|
||||
pub panose: Option<String>,
|
||||
pub pitch_family: Option<String>,
|
||||
|
||||
@@ -24,7 +24,7 @@ fn font_set(e: Option<&El>) -> FontSet {
|
||||
let attrs = |n: &str| {
|
||||
let c = e.and_then(|e| e.child(Local(n)));
|
||||
let at = |a: &str| c.and_then(|c| c.attr_local(a)).map(String::from);
|
||||
TypefaceAttrs { panose: at("panose"), pitch_family: at("pitchFamily"), charset: at("charset") }
|
||||
TypefaceAttrs { typeface: at("typeface").unwrap_or_default(), panose: at("panose"), pitch_family: at("pitchFamily"), charset: at("charset") }
|
||||
};
|
||||
FontSet { latin: tf("latin"), ea: tf("ea"), cs: tf("cs"), latin_attrs: attrs("latin"), ea_attrs: attrs("ea"), cs_attrs: attrs("cs"), scripts }
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@ use crate::write as dml;
|
||||
fn font_set(w: &mut Writer, tag: &str, f: &FontSet) {
|
||||
w.open(tag, &[]);
|
||||
let face = |w: &mut Writer, tag: &str, name: &str, a: &TypefaceAttrs| {
|
||||
// Panose and the like describe one font: a changed typeface does not keep them.
|
||||
let none = TypefaceAttrs::default();
|
||||
let a = if a.typeface == name { a } else { &none };
|
||||
w.tag(tag)
|
||||
.attr("typeface", name)
|
||||
.attr_opt("panose", a.panose.as_deref())
|
||||
|
||||
Reference in New Issue
Block a user