Files
craft-libs/crates/odf/src/text/writer/props.rs
T
Kevin JosephandNghiathan13 6242f65262 craft-docx: a table border without w:sz is width 0, and ODT and RTF keep it (#2203)
* craft-docx: a DOCX border without w:sz is an eighth of a point

* craft-docx: a table border without w:sz is width 0, and ODT and RTF keep it

A table, cell or table-style border without w:sz gives the same output in
Word as w:sz="0" and takes no room; read as half a point, rows below a
pandoc table's header rule moved down half a point. PropCtx::table_borders
now reads it as 0, as PropCtx::borders does since #1488.

ODT and RTF have no border that is drawn and takes no room, so a drawn
width-0 cell side is written at Border::THINNEST instead of being dropped.
Where two ODT cells share an edge, a line with a width wins over a width-0
one, whichever cell states it.

Table half of storytold/wordcraft#5764.

---------

Co-authored-by: Nghiathan13 <201675788+Nghiathan13@users.noreply.github.com>
2026-11-18 15:35:29 -05:00

428 lines
19 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Style properties written for text and paragraphs (ODF 1.3 §17.6, §17.8): character
//! formatting, scripts and languages, spacing, tab stops, alignment and borders.
use super::{Align, Border, BorderStyle, Borders, Fmt, Kind, LineSpacing, Rgb, model, pt, x};
/// `style:text-properties` attributes for what only [`Fmt::extra`] holds (underline and strike
/// are written with the rest); `size` is the text's size, which a raised position is a share of.
pub(in crate::text) fn extra_props(x: &craft_wordmodel::props::CharProps, size: Option<f32>) -> String {
let mut p = String::new();
let on = |v: Option<bool>| v == Some(true);
if on(x.caps) {
p.push_str(" fo:text-transform=\"uppercase\"");
} else if x.caps == Some(false) {
p.push_str(" fo:text-transform=\"none\"");
}
if on(x.small_caps) {
p.push_str(" fo:font-variant=\"small-caps\"");
} else if x.small_caps == Some(false) {
p.push_str(" fo:font-variant=\"normal\"");
}
if on(x.hidden) {
p.push_str(" text:display=\"none\"");
}
if let Some(k) = x.kern.filter(|k| k.is_finite()) {
p.push_str(&format!(" style:letter-kerning=\"{}\"", k > 0.0));
}
if let Some(s) = x.spacing.filter(|s| s.is_finite() && *s != 0.0) {
p.push_str(&format!(" fo:letter-spacing=\"{}\"", pt(s)));
}
if let Some(s) = x.scale.filter(|s| s.is_finite() && *s > 0.0 && *s != 100.0) {
p.push_str(&format!(" style:text-scale=\"{}%\"", s.round()));
}
if let Some(m) = x.emphasis {
use craft_wordmodel::props::EmphasisMark as E;
p.push_str(match m {
E::None => " style:text-emphasize=\"none\"",
E::Dot => " style:text-emphasize=\"dot above\"",
E::Comma => " style:text-emphasize=\"accent above\"",
E::Circle => " style:text-emphasize=\"circle above\"",
E::UnderDot => " style:text-emphasize=\"dot below\"",
});
}
if let Some(r) = x.position.filter(|r| r.is_finite() && *r != 0.0) {
let size = size.filter(|s| s.is_finite() && *s >= 1.0).unwrap_or(12.0);
let pct = r / size * 100.0;
p.push_str(&format!(" style:text-position=\"{}% 100%\"", (pct * 100.0).round() / 100.0));
}
if on(x.outline) {
p.push_str(" style:text-outline=\"true\"");
}
if on(x.shadow) {
p.push_str(" fo:text-shadow=\"1pt 1pt\"");
}
if on(x.emboss) {
p.push_str(" style:font-relief=\"embossed\"");
} else if on(x.engrave) {
p.push_str(" style:font-relief=\"engraved\"");
}
p.push_str(&script_attrs(x));
if let Some(l) = x.lang.as_deref() {
p.push_str(&lang_attrs(l, ""));
}
p
}
/// `style:text-properties` attributes for character properties `cp` (a style's look).
pub(in crate::text) fn props_text(cp: &craft_wordmodel::props::CharProps) -> String {
use craft_wordmodel::props::{TextColor, Underline, VertAlign};
let f = Fmt {
bold: cp.bold == Some(true),
italic: cp.italic == Some(true),
underline: cp.underline.is_some_and(|u| u != Underline::None),
strike: cp.strike == Some(true),
sup: cp.vert_align == Some(VertAlign::Superscript),
sub: cp.vert_align == Some(VertAlign::Subscript),
color: match cp.color {
Some(TextColor::Rgb(c)) => Some(c),
_ => None,
},
size: cp.size,
font: cp.font.as_deref().map(std::sync::Arc::from),
// A style's own properties, with its text border at the width it is drawn at: the flow
// model does that for direct formatting, and a style does not go through it
// (storytold/wordcraft#5065).
extra: Some(Box::new(craft_wordmodel::props::CharProps { border: cp.border.map(|b| b.as_drawn()), ..cp.clone() })),
..Default::default()
};
text_props(&f)
}
/// `fo:text-align` for `a` (ODF 1.3 §20.223).
pub(in crate::text) fn align_attr(a: Option<Align>) -> &'static str {
match a {
Some(Align::Center) => " fo:text-align=\"center\"",
Some(Align::Right) => " fo:text-align=\"end\"",
Some(Align::Justify) | Some(Align::Distribute) => " fo:text-align=\"justify\"",
Some(Align::Left) => " fo:text-align=\"start\"",
None => "",
}
}
/// `fo:language`, `fo:script` and `fo:country` (with `suffix`: `-asian`, `-complex` on `style:`)
/// for BCP 47 tag `tag`. A script is the four-letter subtag after the language.
pub(in crate::text) fn lang_attrs(tag: &str, suffix: &str) -> String {
let mut parts = tag.split(['-', '_']);
let ok = |s: &&str| (1..=8).contains(&s.len()) && s.chars().all(|c| c.is_ascii_alphanumeric());
let ns = if suffix.is_empty() { "fo" } else { "style" };
let mut p = String::new();
if let Some(lang) = parts.next().filter(ok) {
p.push_str(&format!(" {ns}:language{suffix}=\"{lang}\""));
let mut next = parts.next().filter(ok);
if let Some(script) = next.filter(|s| is_script(s)) {
p.push_str(&format!(" {ns}:script{suffix}=\"{script}\""));
next = parts.next().filter(ok);
}
if let Some(country) = next {
p.push_str(&format!(" {ns}:country{suffix}=\"{country}\""));
}
}
p
}
/// Whether a language tag's subtag is a script (BCP 47: four letters).
pub(in crate::text) fn is_script(subtag: &str) -> bool {
subtag.len() == 4 && subtag.chars().all(|c| c.is_ascii_alphabetic())
}
/// The Asian and complex-script attributes of `x`: fonts, complex size, weight, posture and the
/// two languages.
pub(in crate::text) fn script_attrs(x: &craft_wordmodel::props::CharProps) -> String {
let mut p = String::new();
if let Some(f) = x.font_ea.as_deref() {
p.push_str(&format!(" style:font-family-asian=\"'{}'\"", crate::text::writer::x(&f.replace('\'', ""))));
}
if let Some(f) = x.font_cs.as_deref() {
p.push_str(&format!(" style:font-family-complex=\"'{}'\"", crate::text::writer::x(&f.replace('\'', ""))));
}
if let Some(s) = x.size_cs.filter(|s| s.is_finite()) {
p.push_str(&format!(" style:font-size-complex=\"{}\"", pt(s)));
}
if let Some(b) = x.bold_cs {
p.push_str(if b { " style:font-weight-complex=\"bold\"" } else { " style:font-weight-complex=\"normal\"" });
}
if let Some(i) = x.italic_cs {
p.push_str(if i { " style:font-style-complex=\"italic\"" } else { " style:font-style-complex=\"normal\"" });
}
if let Some(l) = x.lang_ea.as_deref() {
p.push_str(&lang_attrs(l, "-asian"));
}
if let Some(l) = x.lang_bidi.as_deref() {
p.push_str(&lang_attrs(l, "-complex"));
}
p
}
pub(in crate::text) fn text_props(f: &Fmt) -> String {
let mut p = String::new();
// Complex-script weight and posture of their own are written with the other script extras.
let own_cs = |pick: fn(&craft_wordmodel::props::CharProps) -> Option<bool>| f.extra.as_deref().and_then(pick).is_some();
let extra = f.extra.as_deref();
// What it turns off that its style, or the style that is based on, may turn on: said so, or
// that bold, italic, underline or strike-through would show through.
let off = |pick: fn(&craft_wordmodel::props::CharProps) -> Option<bool>| extra.and_then(pick) == Some(false);
if f.bold {
p.push_str(" fo:font-weight=\"bold\" style:font-weight-asian=\"bold\"");
if !own_cs(|x| x.bold_cs) {
p.push_str(" style:font-weight-complex=\"bold\"");
}
} else if off(|x| x.bold) {
p.push_str(" fo:font-weight=\"normal\" style:font-weight-asian=\"normal\"");
if !own_cs(|x| x.bold_cs) {
p.push_str(" style:font-weight-complex=\"normal\"");
}
}
if f.italic {
p.push_str(" fo:font-style=\"italic\" style:font-style-asian=\"italic\"");
if !own_cs(|x| x.italic_cs) {
p.push_str(" style:font-style-complex=\"italic\"");
}
} else if off(|x| x.italic) {
p.push_str(" fo:font-style=\"normal\" style:font-style-asian=\"normal\"");
if !own_cs(|x| x.italic_cs) {
p.push_str(" style:font-style-complex=\"normal\"");
}
}
if f.underline {
use craft_wordmodel::props::Underline as U;
let (style, kind, width, mode) = match extra.and_then(|e| e.underline) {
Some(U::Double) => ("solid", "double", "auto", ""),
Some(U::Thick) => ("solid", "single", "bold", ""),
Some(U::Dotted) => ("dotted", "single", "auto", ""),
Some(U::Dash) => ("dash", "single", "auto", ""),
Some(U::DotDash) => ("dot-dash", "single", "auto", ""),
Some(U::DotDotDash) => ("dot-dot-dash", "single", "auto", ""),
Some(U::Wave) => ("wave", "single", "auto", ""),
Some(U::DoubleWave) => ("wave", "double", "auto", ""),
Some(U::Words) => ("solid", "single", "auto", " style:text-underline-mode=\"skip-white-space\""),
_ => ("solid", "single", "auto", ""),
};
let color =
extra.and_then(|e| e.underline_color).map(|c| format!("#{}", c.hex().to_ascii_lowercase())).unwrap_or_else(|| "font-color".into());
p.push_str(&format!(
" style:text-underline-style=\"{style}\" style:text-underline-type=\"{kind}\" style:text-underline-width=\"{width}\" style:text-underline-color=\"{color}\"{mode}"
));
}
if !f.underline && extra.is_some_and(|e| e.underline == Some(craft_wordmodel::props::Underline::None)) {
p.push_str(" style:text-underline-style=\"none\"");
}
if f.strike || extra.is_some_and(|e| e.double_strike == Some(true)) {
p.push_str(" style:text-line-through-style=\"solid\"");
if extra.is_some_and(|e| e.double_strike == Some(true)) {
p.push_str(" style:text-line-through-type=\"double\"");
}
} else if off(|x| x.strike) {
p.push_str(" style:text-line-through-style=\"none\"");
}
if let Some(e) = extra {
if (f.sup || f.sub) && e.position.is_some() {
// One `style:text-position` only (a repeated attribute isn't well-formed XML):
// superscript or subscript, written below, wins over a raised or lowered position.
let e = craft_wordmodel::props::CharProps { position: None, ..e.clone() };
p.push_str(&extra_props(&e, f.size));
} else {
p.push_str(&extra_props(e, f.size));
}
}
// A border around the text (Word's `w:bdr`): LibreOffice's text border, which ODF 1.3 has no
// attribute of its own for (`loext:border`, `loext:padding`).
if let Some(b) = extra.and_then(|e| e.border).filter(|b| b.is_visible()) {
p.push_str(&format!(" loext:border=\"{}\" loext:padding=\"{}\"", odf_border(Some(b)), pt(b.space.clamp(0.0, 31.0))));
}
if f.sup {
p.push_str(" style:text-position=\"super 58%\"");
} else if f.sub {
p.push_str(" style:text-position=\"sub 58%\"");
}
if let Some(c) = f.color {
p.push_str(&format!(" fo:color=\"#{}\"", c.hex().to_ascii_lowercase()));
}
if let Some(c) = f.background {
p.push_str(&format!(" fo:background-color=\"#{}\"", c.hex().to_ascii_lowercase()));
}
// One size in WordCraft's model is every script's in ODF's, as bold is: without these,
// LibreOffice shows Asian and complex-script text at the default size whatever was chosen.
if let Some(s) = f.size {
p.push_str(&format!(" fo:font-size=\"{0}\" style:font-size-asian=\"{0}\"", pt(s)));
if f.extra.as_deref().and_then(|x| x.size_cs).is_none() {
p.push_str(&format!(" style:font-size-complex=\"{}\"", pt(s)));
}
}
if f.code {
p.push_str(&format!(" fo:font-family=\"'{}'\" style:font-family-generic=\"modern\" style:font-pitch=\"fixed\"", model::MONO_FONT));
} else if let Some(fam) = &f.font {
p.push_str(&format!(" fo:font-family=\"'{}'\"", x(&fam.replace('\'', ""))));
}
p
}
/// `style:paragraph-properties` attributes for the spacing, indents and line spacing `sp` sets
/// (ODF 1.3 §20.204–209, §20.225, §20.317).
pub(in crate::text) fn spacing_props(sp: &model::Spacing) -> String {
let mut p = String::new();
for (k, v) in [
("fo:margin-top", sp.before),
("fo:margin-bottom", sp.after),
("fo:margin-left", sp.left),
("fo:margin-right", sp.right),
("fo:text-indent", sp.first),
] {
if let Some(v) = v.filter(|v| v.is_finite()) {
p.push_str(&format!(" {k}=\"{}\"", pt(v)));
}
}
match sp.line {
Some(LineSpacing::Multiple(m)) if m.is_finite() => p.push_str(&format!(" fo:line-height=\"{}%\"", (m * 100.0).round().max(1.0))),
Some(LineSpacing::Exactly(h)) if h.is_finite() => p.push_str(&format!(" fo:line-height=\"{}\"", pt(h))),
Some(LineSpacing::AtLeast(h)) if h.is_finite() => p.push_str(&format!(" style:line-height-at-least=\"{}\"", pt(h))),
_ => {}
}
p
}
/// `style:tab-stops` (ODF 1.3 §17.7) for `tabs`, measured from the margin (the package's
/// settings say so); bar and clearing stops have no ODF form and are left out.
pub(in crate::text) fn tab_stops(tabs: &[craft_wordmodel::props::TabStop]) -> String {
use craft_wordmodel::props::{TabAlign, TabLeader};
let mut s = String::from("<style:tab-stops>");
for t in tabs.iter().filter(|t| t.pos.is_finite()) {
let kind = match t.align {
TabAlign::Left => "",
TabAlign::Center => " style:type=\"center\"",
TabAlign::Right => " style:type=\"right\"",
TabAlign::Decimal => " style:type=\"char\" style:char=\".\"",
TabAlign::Bar | TabAlign::Clear => continue,
};
let leader = match t.leader {
TabLeader::None => "",
TabLeader::Dot => " style:leader-style=\"dotted\" style:leader-text=\".\"",
TabLeader::Hyphen => " style:leader-style=\"solid\" style:leader-text=\"-\"",
TabLeader::Underscore => " style:leader-style=\"solid\" style:leader-text=\"_\"",
TabLeader::MiddleDot => " style:leader-style=\"dotted\" style:leader-text=\"·\"",
};
s.push_str(&format!("<style:tab-stop style:position=\"{}\"{kind}{leader}/>", pt(t.pos)));
}
s.push_str("</style:tab-stops>");
s
}
/// An ODF style name (an XML NCName, ODF 1.3 §19.75) for document style id `id`: letters,
/// digits and `-`, `.` kept, anything else as `_xHH_` (its code point in hex), and a leading
/// digit, `-` or `.` escaped too.
pub(in crate::text) fn odf_name(id: &str) -> String {
if let Some((name, _)) = generated_style(id) {
return name;
}
let mut out = String::new();
for (i, c) in id.chars().enumerate() {
let ok = c.is_alphanumeric() || (i > 0 && (c == '-' || c == '.' || c.is_ascii_digit()));
if ok && !(i == 0 && c.is_ascii_digit()) {
out.push(c);
} else {
out.push_str(&format!("_x{:X}_", c as u32));
}
}
if out.is_empty() { "Custom".into() } else { out }
}
/// `fo:border-*` and `fo:padding-*` attributes for paragraph borders `b`, when a side shows.
pub(in crate::text) fn border_attrs(b: Option<Borders>) -> String {
let mut p = String::new();
// A side without a line may still hold padding (read from ODF), which keeps its room.
let padded = |l: &Border| !l.is_visible() && l.space.is_finite() && l.space > 0.0;
let Some(b) = b.filter(|b| b.any_visible() || [b.top, b.left, b.bottom, b.right].iter().flatten().any(padded)) else { return p };
for (side, line) in [("top", b.top), ("left", b.left), ("bottom", b.bottom), ("right", b.right)] {
let shown = line.filter(|l| l.is_visible());
p.push_str(&format!(" fo:border-{side}=\"{}\"", odf_border(shown)));
if let Some(l) = shown {
p.push_str(&format!(" fo:padding-{side}=\"{}\"", pt(l.space.clamp(0.0, 31.0))));
} else if let Some(l) = line.filter(padded) {
p.push_str(&format!(" fo:padding-{side}=\"{}\"", pt(l.space.min(1584.0))));
}
}
p
}
/// An ODF border value (ODF 1.3 §20.183) for `b`; `none` for no line. A drawn line of width 0
/// (a DOCX table border without `w:sz`) goes out at [`Border::THINNEST`]: ODF has no border that
/// is drawn and takes no room.
pub(in crate::text) fn odf_border(b: Option<Border>) -> String {
let Some(b) = b.filter(Border::is_drawn).map(|b| b.as_drawn()) else { return "none".into() };
let style = match b.style {
BorderStyle::Double | BorderStyle::Triple => "double",
BorderStyle::Dotted => "dotted",
BorderStyle::Dashed => "dashed",
BorderStyle::DotDash => "dot-dash",
_ => "solid",
};
let color = b.color.unwrap_or(Rgb(0, 0, 0)).hex().to_ascii_lowercase();
format!("{} {style} #{color}", pt(b.width.clamp(0.05, 12.0)))
}
pub(in crate::text) fn parent_style(k: Kind) -> &'static str {
match k {
Kind::Heading(1) => "Heading_20_1",
Kind::Heading(2) => "Heading_20_2",
Kind::Heading(3) => "Heading_20_3",
Kind::Heading(4) => "Heading_20_4",
Kind::Heading(5) => "Heading_20_5",
Kind::Heading(_) => "Heading_20_6",
Kind::Title => "Title",
Kind::Quote => "Quotations",
Kind::Code => "Preformatted_20_Text",
Kind::Normal | Kind::Rule => "Standard",
}
}
/// The LibreOffice style (name and display name) standing for WordCraft's table of contents,
/// table of figures and index heading styles, which LibreOffice's indexes are generated in:
/// TOC 1–9 are Contents 1–9, TOC Heading is Contents Heading, Table of Figures is Figure Index 1.
pub(in crate::text) fn generated_style(id: &str) -> Option<(String, String)> {
let n = id.strip_prefix("TOC").and_then(|n| n.parse::<u8>().ok()).filter(|n| (1..=9).contains(n));
Some(match (id, n) {
(_, Some(n)) => (format!("Contents_20_{n}"), format!("Contents {n}")),
("TOCHeading", _) => ("Contents_20_Heading".into(), "Contents Heading".into()),
("TableofFigures", _) => ("Figure_20_Index_20_1".into(), "Figure Index 1".into()),
_ => return None,
})
}
/// The WordCraft style (id and name) a LibreOffice index style `odt` stands for (the reverse
/// of [`generated_style`]).
pub(in crate::text) fn generating_style(odt: &str) -> Option<(String, String)> {
let n = odt.strip_prefix("Contents_20_").and_then(|n| n.parse::<u8>().ok()).filter(|n| (1..=10).contains(n));
Some(match (odt, n) {
(_, Some(n)) => (format!("TOC{}", n.min(9)), format!("TOC {}", n.min(9))),
("Contents_20_Heading", _) => ("TOCHeading".into(), "TOC Heading".into()),
("Figure_20_Index_20_1", _) => ("TableofFigures".into(), "Table of Figures".into()),
_ => return None,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn superscript_raised_text_has_one_text_position() {
for (sup, sub) in [(true, false), (false, true)] {
let f = Fmt {
sup,
sub,
size: Some(12.0),
extra: Some(Box::new(craft_wordmodel::props::CharProps { position: Some(3.0), ..Default::default() })),
..Default::default()
};
let p = text_props(&f);
assert_eq!(p.matches("style:text-position=").count(), 1, "{p}");
}
// Without superscript or subscript the position is still written.
let f = Fmt {
size: Some(12.0),
extra: Some(Box::new(craft_wordmodel::props::CharProps { position: Some(3.0), ..Default::default() })),
..Default::default()
};
assert!(text_props(&f).contains("style:text-position=\"25% 100%\""), "{}", text_props(&f));
}
}