Skip to content

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

Merge PR #1027: idml: one unusable CJK metric no longer refuses the whole file

aiongg910c59f10894
diff --cc crates/idml/src/import.rs
index d08c7c324,108df9430..fc17dd282
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@@ -627,12 -497,14 +627,14 @@@ impl<'r> Importer<'r>
.map(|c| designcraft_doc::cjk::CompositeFontEntry {
name: c.get("Name").unwrap_or("").into(),
characters: c.get("CustomCharacters").unwrap_or("").into(),
- family: c.prop("AppliedFont").unwrap_or_default(),
+ family: c.prop("AppliedFont").as_deref().and_then(names::font_family_in).unwrap_or_default(),
style: c.get("FontStyle").unwrap_or("Regular").trim_start_matches("$ID/").into(),
- relative_size: c.num("RelativeSize").unwrap_or(100.0) / 100.0,
- horizontal_scale: c.num("HorizontalScale").unwrap_or(100.0) / 100.0,
- vertical_scale: c.num("VerticalScale").unwrap_or(100.0) / 100.0,
- baseline_shift: c.num("BaselineShift").unwrap_or(0.0) / 100.0,
+ // A metric `Document::check` refuses would make the whole file fail to
+ // open, so one that is unusable here falls back to full size / no shift.
+ relative_size: c.num("RelativeSize").filter(|v| v.is_finite() && *v > 0.0).unwrap_or(100.0) / 100.0,
+ horizontal_scale: c.num("HorizontalScale").filter(|v| v.is_finite() && *v > 0.0).unwrap_or(100.0) / 100.0,
+ vertical_scale: c.num("VerticalScale").filter(|v| v.is_finite() && *v > 0.0).unwrap_or(100.0) / 100.0,
+ baseline_shift: c.num("BaselineShift").filter(|v| v.is_finite()).unwrap_or(0.0) / 100.0,
scale_option: c.boolean("ScaleOption").unwrap_or(true),
})
.collect();
diff --cc crates/idml/src/tests.rs
index a1c32f879,cc5067313..b76233d22
--- a/crates/idml/src/tests.rs
+++ b/crates/idml/src/tests.rs
@@@ -3936,79 -2411,64 +3936,99 @@@ fn too_many_entries_are_refused()
assert!(err.to_string().contains("entries"), "{err}");
}
-/// InDesign writes `LeadingModel="LeadingModelAkiBelow"` on [No paragraph style] of every
-/// document, but only its Japanese composers use the leading model: with the Paragraph Composer,
-/// a 24 pt paragraph after a 32 pt one sits a Roman 28.8 pt below it (Montilla Design's Flex Layout
-/// file: frames fitted to that came out overset).
+/// A package whose [No paragraph style] carries the space-below leading model
+/// (`LeadingModelAkiBelow`, written on every IDML) and applies `composer` in the
+/// `<Properties><Composer>` form, with a 32 pt paragraph above a 24 pt one.
+fn aki_below_doc(composer: &str) -> Document {
+ let styles = STYLES.replace(
+ r#"Name="$ID/[No paragraph style]" PointSize="12">
+ <Properties>"#,
+ &format!(
+ r#"Name="$ID/[No paragraph style]" PointSize="12" LeadingModel="LeadingModelAkiBelow">
+ <Properties><Composer type="string">{composer}</Composer>"#
+ ),
+ );
+ let story = r#"<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging"><Story Self="s1">
+ <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/$ID/[No paragraph style]">
+ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]" PointSize="32"><Content>Big</Content><Br/></CharacterStyleRange>
+ </ParagraphStyleRange>
+ <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/$ID/[No paragraph style]">
+ <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]" PointSize="24"><Content>Small</Content></CharacterStyleRange>
+ </ParagraphStyleRange></Story></idPkg:Story>"#;
+ let pkg = zip_files(&[
+ ("designmap.xml", DESIGNMAP),
+ ("Resources/Graphic.xml", GRAPHIC),
+ ("Resources/Styles.xml", &styles),
+ ("Resources/Preferences.xml", PREFS),
+ ("MasterSpreads/MasterSpread_m1.xml", MASTER),
+ ("Spreads/Spread_sp1.xml", SPREAD),
+ ("Stories/Story_s1.xml", story),
+ ]);
+ import_idml(&pkg).unwrap()
+}
+
+/// The distance from the first baseline of [`aki_below_doc`]'s story to the second.
+fn second_line_gap(d: &Document) -> f64 {
+ let sid = d.stories.values().find(|s| s.text.contains("Big")).unwrap().id;
+ let cs = designcraft_compose::compose_story(d, sid, &Default::default());
+ let baselines: Vec<f64> = cs.frames.iter().flat_map(|f| &f.lines).map(|l| l.baseline).collect();
+ assert_eq!(baselines.len(), 2, "{baselines:?}");
+ baselines[1] - baselines[0]
+}
+
+/// The leading model on [No paragraph style] of every IDML takes effect only under the Japanese
+/// composers: with the others, a 24 pt paragraph after a 32 pt one sits a Roman 28.8 pt (its auto
+/// leading) below it.
#[test]
fn the_default_cjk_leading_model_applies_only_with_a_japanese_composer() {
- let styles = |composer: &str| {
- STYLES.replace(
- r#"Name="$ID/[No paragraph style]" PointSize="12">"#,
- &format!(r#"Name="$ID/[No paragraph style]" PointSize="12" LeadingModel="LeadingModelAkiBelow" Composer="{composer}">"#),
- )
- };
- let model = |composer: &str| {
- let s = styles(composer);
- let pkg = zip_files(&[
- ("designmap.xml", DESIGNMAP),
- ("Resources/Graphic.xml", GRAPHIC),
- ("Resources/Styles.xml", &s),
- ("Resources/Preferences.xml", PREFS),
- ("MasterSpreads/MasterSpread_m1.xml", MASTER),
- ("Spreads/Spread_sp1.xml", SPREAD),
- ("Stories/Story_s1.xml", STORY),
- ]);
- let d = import_idml(&pkg).unwrap();
- d.styles.para(designcraft_doc::NO_PARA_STYLE).unwrap().chars.leading_model
- };
- assert_eq!(model("HL Composer"), None, "the Paragraph Composer leads baseline to baseline");
- assert_eq!(model("HL Single"), None);
- assert_eq!(model("$ID/HL Composer J"), Some(designcraft_doc::cjk::LeadingModel::AkiBelow));
- assert_eq!(model("$ID/HL Single J"), Some(designcraft_doc::cjk::LeadingModel::AkiBelow));
+ for composer in ["$ID/HL Composer", "$ID/HL Single", "$ID/HL Composer Optyca"] {
+ let d = aki_below_doc(composer);
+ let root = d.styles.para(designcraft_doc::NO_PARA_STYLE).unwrap();
+ assert_eq!(root.chars.leading_model, Some(designcraft_doc::cjk::LeadingModel::AkiBelow), "the import keeps the file's model");
+ let gap = second_line_gap(&d);
+ assert!((gap - 28.8).abs() < 0.01, "{composer}: {gap}");
+ }
+ for composer in ["$ID/HL Composer J", "$ID/HL Single J"] {
+ let gap = second_line_gap(&aki_below_doc(composer));
+ assert!(gap > 28.8 + 1.0, "{composer}: the upper line's leading sits below it, not the lower line's ({gap})");
+ }
+}
+
+/// A Japanese-composer document keeps its composer and leading model through export and
+/// reimport, and lays out the same.
+#[test]
+fn a_japanese_composer_and_its_leading_model_round_trip() {
+ use designcraft_doc::Composer as C;
+ let d = aki_below_doc("$ID/HL Composer J");
+ let back = import_idml(&export_idml(&d)).unwrap();
+ for doc in [&d, &back] {
+ let root = doc.styles.para(designcraft_doc::NO_PARA_STYLE).unwrap();
+ assert_eq!(root.para.composer, Some(C::Japanese));
+ assert_eq!(root.chars.leading_model, Some(designcraft_doc::cjk::LeadingModel::AkiBelow));
+ }
+ assert!((second_line_gap(&d) - second_line_gap(&back)).abs() < 0.01);
}
-/// A composite-font or mojikumi metric `Document::check` refuses (zero, NaN, infinity) must
++/// A composite-font metric `Document::check` refuses (zero, NaN, infinity) must
+ /// not make the whole package fail to open: the entry falls back to a usable value instead.
+ #[test]
+ fn unusable_cjk_metrics_do_not_refuse_the_whole_file() {
+ let designmap = r#"<Document xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" Self="d">
+ <CompositeFont Self="cf" Name="Rubies">
+ <CompositeFontEntry Self="e1" Name="Dot" AppliedFont="Myriad Pro" RelativeSize="0"/>
+ <CompositeFontEntry Self="e2" Name="Bar" AppliedFont="Myriad Pro" BaselineShift="NaN"/>
+ </CompositeFont>
- <MojikumiTable Self="mt" Name="Table">
- <Properties><OverrideMojikumiAkiList>
- <OverrideMojikumiAkiType Minimum="NaN" Desired="1" Maximum="2"/>
- </OverrideMojikumiAkiList></Properties>
- </MojikumiTable>
+ <idPkg:Spread src="Spreads/Spread_s.xml"/>
+ </Document>"#;
+ let spread = r#"<idPkg:Spread xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging">
+ <Spread Self="s"><Page Self="p" GeometricBounds="0 0 100 100" ItemTransform="1 0 0 1 0 0"/></Spread>
+ </idPkg:Spread>"#;
+ let d = import_idml(&zip_files(&[("designmap.xml", &designmap), ("Spreads/Spread_s.xml", &spread)])).unwrap();
+ let entries = &d.styles.composite_fonts[0].entries;
+ assert_eq!(entries[0].relative_size, 1.0, "an unusable relative size falls back to full size");
+ assert_eq!(entries[1].baseline_shift, 0.0, "an unusable shift falls back to none");
- assert_eq!(d.styles.mojikumi_tables[0].overrides[0].minimum, 0.0);
+ }
+
mod liquid_page_rules {
use super::*;
use designcraft_doc::LiquidRule as R;