Skip to content

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

fix: combine inline decoration overrides with the character style in EPUB export

NetterSilvere5621e16e0fd
diff --git a/crates/epub/src/css.rs b/crates/epub/src/css.rs
index 3a8639cec..f066d6b3d 100644
--- a/crates/epub/src/css.rs
+++ b/crates/epub/src/css.rs
@@ -40,23 +40,7 @@ pub(crate) fn char_css(doc: &Document, c: &CharProps, base_size: f64, generics:
let (weight, style) = css_weight_style(&c.font_style);
let _ = write!(out, "font-weight: {weight}; font-style: {style}; ");
let _ = write!(out, "font-size: {:.3}em; ", c.size / base_size);
- if c.tracking != 0.0 {
- let _ = write!(out, "letter-spacing: {:.3}em; ", c.tracking / 1000.0);
- }
- match c.capitalization {
- designcraft_doc::Capitalization::AllCaps => out.push_str("text-transform: uppercase; "),
- designcraft_doc::Capitalization::SmallCaps | designcraft_doc::Capitalization::OpenTypeAllSmallCaps => {
- out.push_str("font-variant: small-caps; ")
- }
- _ => {}
- }
- // One declaration: a later `text-decoration` would replace the earlier one.
- match (c.underline, c.strikethrough) {
- (true, true) => out.push_str("text-decoration: underline line-through; "),
- (true, false) => out.push_str("text-decoration: underline; "),
- (false, true) => out.push_str("text-decoration: line-through; "),
- (false, false) => {}
- }
+ decor_css(c.tracking, c.capitalization, c.underline, c.strikethrough, out);
if let Some(h) = hex(doc, &c.fill, c.fill_tint) {
let _ = write!(out, "color: {h}; ");
}
@@ -150,7 +134,7 @@ pub(crate) fn stylesheet_with(doc: &Document, generics: &BTreeMap<String, String
if let Some(sz) = c.size {
let _ = write!(rule, "font-size: {:.3}em; ", sz / base);
}
- attrs_decor_css(c, &mut rule);
+ attrs_decor_css(&st.char_style_attrs(&cs.name), &mut rule);
let (tag, class) = export_mapping(st, &cs.name, true, &cc);
if let Some(selector) = class_selector(tag, &class) {
let _ = writeln!(css, "{selector} {{ {rule}}}");
@@ -161,20 +145,23 @@ pub(crate) fn stylesheet_with(doc: &Document, generics: &BTreeMap<String, String
/// Tracking, capitalization and line decorations of a style or override, as CSS declarations.
fn attrs_decor_css(c: &CharAttrs, out: &mut String) {
- if let Some(t) = c.tracking
- && t != 0.0
- {
- let _ = write!(out, "letter-spacing: {:.3}em; ", t / 1000.0);
+ decor_css(c.tracking.unwrap_or(0.0), c.capitalization.unwrap_or_default(), c.underline == Some(true), c.strikethrough == Some(true), out);
+}
+
+/// Tracking, capitalization and line decorations as CSS declarations; defaults write nothing.
+fn decor_css(tracking: f64, caps: designcraft_doc::Capitalization, underline: bool, strikethrough: bool, out: &mut String) {
+ if tracking != 0.0 {
+ let _ = write!(out, "letter-spacing: {:.3}em; ", tracking / 1000.0);
}
- match c.capitalization {
- Some(designcraft_doc::Capitalization::AllCaps) => out.push_str("text-transform: uppercase; "),
- Some(designcraft_doc::Capitalization::SmallCaps | designcraft_doc::Capitalization::OpenTypeAllSmallCaps) => {
+ match caps {
+ designcraft_doc::Capitalization::AllCaps => out.push_str("text-transform: uppercase; "),
+ designcraft_doc::Capitalization::SmallCaps | designcraft_doc::Capitalization::OpenTypeAllSmallCaps => {
out.push_str("font-variant: small-caps; ")
}
_ => {}
}
// One declaration: a later `text-decoration` would replace the earlier one.
- match (c.underline == Some(true), c.strikethrough == Some(true)) {
+ match (underline, strikethrough) {
(true, true) => out.push_str("text-decoration: underline line-through; "),
(true, false) => out.push_str("text-decoration: underline; "),
(false, true) => out.push_str("text-decoration: line-through; "),
@@ -182,7 +169,11 @@ fn attrs_decor_css(c: &CharAttrs, out: &mut String) {
}
}
-pub(crate) fn override_css(doc: &Document, o: &CharAttrs) -> String {
+/// Inline CSS for the local override `o` of a run in character style `style`. Tracking, caps and
+/// line decorations are resolved against the style (and its Based On chain), so an override that
+/// sets only one of underline/strikethrough keeps the other, and one that clears a value the
+/// style sets writes the reset.
+pub(crate) fn override_css(doc: &Document, style: &str, o: &CharAttrs) -> String {
let mut s = String::new();
if let Some(f) = &o.font_style {
let f = f.to_lowercase();
@@ -198,7 +189,24 @@ pub(crate) fn override_css(doc: &Document, o: &CharAttrs) -> String {
{
let _ = write!(s, "color: {h}; ");
}
- attrs_decor_css(o, &mut s);
+ let base = doc.styles.char_style_attrs(style);
+ let mut merged = base.clone();
+ merged.merge(o);
+ let tracking = if o.tracking.is_some() { merged.tracking.unwrap_or(0.0) } else { 0.0 };
+ let caps = if o.capitalization.is_some() { merged.capitalization.unwrap_or_default() } else { Default::default() };
+ let lines = o.underline.is_some() || o.strikethrough.is_some();
+ let (underline, strikethrough) = (lines && merged.underline == Some(true), lines && merged.strikethrough == Some(true));
+ if o.tracking.is_some() && tracking == 0.0 && base.tracking.is_some_and(|t| t != 0.0) {
+ s.push_str("letter-spacing: 0em; ");
+ }
+ // The class rule's other caps property would still apply, so clear both before the new mode.
+ if o.capitalization.is_some() && caps != base.capitalization.unwrap_or_default() && base.capitalization.is_some_and(|c| c != Default::default()) {
+ s.push_str("text-transform: none; font-variant: normal; ");
+ }
+ decor_css(tracking, caps, underline, strikethrough, &mut s);
+ if lines && !underline && !strikethrough && (base.underline == Some(true) || base.strikethrough == Some(true)) {
+ s.push_str("text-decoration: none; ");
+ }
s.trim_end().to_string()
}
diff --git a/crates/epub/src/story_writer.rs b/crates/epub/src/story_writer.rs
index aed443d0b..40c95f135 100644
--- a/crates/epub/src/story_writer.rs
+++ b/crates/epub/src/story_writer.rs
@@ -223,7 +223,7 @@ impl<'a, 'f> StoryWriter<'a, 'f> {
} else {
("span", String::new())
};
- let style = override_css(doc, &f.over);
+ let style = override_css(doc, &f.style, &f.over);
let style_attr = if style.is_empty() { String::new() } else { format!(" style=\"{}\"", xml_attr(&style)) };
if cls.is_empty() && style_attr.is_empty() {
out.push_str(&t);
diff --git a/crates/epub/src/tests.rs b/crates/epub/src/tests.rs
index 6cae6159f..4b0531967 100644
--- a/crates/epub/src/tests.rs
+++ b/crates/epub/src/tests.rs
@@ -943,5 +943,37 @@ fn character_styles_keep_decorations_caps_and_tracking() {
assert!(css.contains("text-transform: uppercase;"), "{css}");
assert!(css.contains("letter-spacing: 0.100em;"), "{css}");
let o = CharAttrs { strikethrough: Some(true), ..Default::default() };
- assert_eq!(css::override_css(&d, &o), "text-decoration: line-through;");
+ assert_eq!(css::override_css(&d, story::NO_CHAR_STYLE, &o), "text-decoration: line-through;");
+}
+
+fn char_style(name: &str, based_on: Option<&str>, chars: CharAttrs) -> designcraft_doc::CharacterStyle {
+ designcraft_doc::CharacterStyle { name: name.into(), based_on: based_on.map(Into::into), chars, shortcut: String::new() }
+}
+
+#[test]
+fn override_keeps_style_decorations_and_resets_tracking_and_caps() {
+ use designcraft_doc::Capitalization;
+ let mut d = Document::new(&NewDocument::default());
+ let styles = d.styles_mut();
+ styles.character.push(char_style(
+ "Base",
+ None,
+ CharAttrs { underline: Some(true), tracking: Some(100.0), capitalization: Some(Capitalization::AllCaps), ..Default::default() },
+ ));
+ styles.character.push(char_style("Child", Some("Base"), CharAttrs::default()));
+ let strike = CharAttrs { strikethrough: Some(true), ..Default::default() };
+ for style in ["Base", "Child"] {
+ assert_eq!(css::override_css(&d, style, &strike), "text-decoration: underline line-through;", "{style}");
+ }
+ let off = CharAttrs { underline: Some(false), ..Default::default() };
+ assert_eq!(css::override_css(&d, "Child", &off), "text-decoration: none;");
+ let reset = CharAttrs { tracking: Some(0.0), capitalization: Some(Capitalization::Normal), ..Default::default() };
+ assert_eq!(css::override_css(&d, "Child", &reset), "letter-spacing: 0em; text-transform: none; font-variant: normal;");
+ assert_eq!(css::override_css(&d, story::NO_CHAR_STYLE, &reset), "");
+ let styles = d.styles_mut();
+ styles.character.push(char_style("Small", None, CharAttrs { capitalization: Some(Capitalization::SmallCaps), ..Default::default() }));
+ let small = CharAttrs { capitalization: Some(Capitalization::SmallCaps), ..Default::default() };
+ let all = CharAttrs { capitalization: Some(Capitalization::AllCaps), ..Default::default() };
+ assert_eq!(css::override_css(&d, "Base", &small), "text-transform: none; font-variant: normal; font-variant: small-caps;");
+ assert_eq!(css::override_css(&d, "Small", &all), "text-transform: none; font-variant: normal; text-transform: uppercase;");
}