mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-30 18:14:33 +01:00
feat: write kerning value 1e+11, whole-number kerning, manual kerning method, two numbering formats and DeleteEndPunctuation
This commit is contained in:
1 parent
96a84f0487
commit
e93a41dc6a
4 files changed
+126
-11
No files matched your search
+73
-2
@@ -298,8 +298,8 @@ pub(super) const BUILTIN_STROKE_STYLES: &[&str] = &[
|
||||
"Solid",
|
||||
];
|
||||
|
||||
/// Value of the kerning attribute (0x1B13) in every root paragraph style;
|
||||
/// IDML writes no `KerningValue` for it.
|
||||
/// Value of the kerning attribute (0x1B13) for no manual kerning; IDML
|
||||
/// writes it `1e+11`. Every root paragraph style stores it.
|
||||
pub(super) const KERNING_NONE: f64 = 1e8;
|
||||
|
||||
/// Text attributes: ID, IDML name, kind, written in `<Properties>`.
|
||||
@@ -317,6 +317,7 @@ pub(super) const TEXT_ATTRS: &[(u32, &str, Kind, bool)] = &[
|
||||
(15972, "$ID/Metrics"),
|
||||
(79875, "$ID/Optical"),
|
||||
(0x3E65, "$ID/Metrics - Roman Only"),
|
||||
(0x221, "$ID/None"),
|
||||
]),
|
||||
false,
|
||||
),
|
||||
@@ -644,10 +645,12 @@ pub(super) const TEXT_ATTRS: &[(u32, &str, Kind, bool)] = &[
|
||||
"NumberingFormat",
|
||||
Kind::EnumString(&[
|
||||
(0x1A477, "1, 2, 3, 4..."),
|
||||
(0x1A478, "I, II, III, IV..."),
|
||||
(0x1A479, "i, ii, iii, iv..."),
|
||||
(0x1A47A, "A, B, C, D..."),
|
||||
(0x1A47B, "a, b, c, d..."),
|
||||
(0x1A497, "01,02,03..."),
|
||||
(0x1A4A7, "$ID/Circle Numbering"),
|
||||
(0, "None"),
|
||||
]),
|
||||
true,
|
||||
@@ -1755,12 +1758,44 @@ pub(super) fn delimiter_field(d: &Delimiter) -> Field {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets `KerningMethod` in `plain` for a run list with manual kerning; see
|
||||
/// [`Writer::run_attrs`].
|
||||
fn manual_kerning(attrs: &Attrs, plain: &mut Vec<(&'static str, String)>) {
|
||||
if attrs.get(0x1B07).is_none()
|
||||
|| attrs.get(0x1B13).and_then(Value::as_f64) == Some(KERNING_NONE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
let Some(i) = plain.iter().position(|(n, _)| *n == "KerningValue") else {
|
||||
return;
|
||||
};
|
||||
let method = if plain[i].1 == "0" {
|
||||
"$ID/None"
|
||||
} else {
|
||||
"$ID/Manual"
|
||||
};
|
||||
match plain.iter_mut().find(|(n, _)| *n == "KerningMethod") {
|
||||
Some(m) => m.1 = method.into(),
|
||||
None => plain.insert(i, ("KerningMethod", method.into())),
|
||||
}
|
||||
}
|
||||
|
||||
impl Writer<'_> {
|
||||
/// IDML attributes and properties for a text attribute list.
|
||||
pub(super) fn text_attrs(&self, attrs: &Attrs) -> (Vec<(&'static str, String)>, Vec<Property>) {
|
||||
self.text_attrs_of(attrs, false)
|
||||
}
|
||||
|
||||
/// Like [`Writer::text_attrs`], for the attributes of a story run: a
|
||||
/// run that stores both a kerning method and a manual kerning value
|
||||
/// has the method `$ID/Manual`, or `$ID/None` for a value of 0,
|
||||
/// whatever method is stored (`docs/format/attributes.md`, kerning).
|
||||
pub(super) fn run_attrs(&self, attrs: &Attrs) -> (Vec<(&'static str, String)>, Vec<Property>) {
|
||||
let (mut plain, props) = self.text_attrs(attrs);
|
||||
manual_kerning(attrs, &mut plain);
|
||||
(plain, props)
|
||||
}
|
||||
|
||||
/// Like [`Writer::text_attrs`]; a character style (`character_style`)
|
||||
/// names a composite font by its family name (`docs/format/fonts.md`,
|
||||
/// composite fonts as applied fonts).
|
||||
@@ -2201,6 +2236,42 @@ mod tests {
|
||||
assert!(plain.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writes_manual_kerning_of_runs() {
|
||||
let doc = Document::default();
|
||||
let w = Writer::for_test(&doc);
|
||||
let run = |kerning: f64| {
|
||||
let mut a = Attrs::default();
|
||||
a.values = vec![
|
||||
(0x1B07, Value::Ref(79875)),
|
||||
(0x1B13, Value::Double(kerning)),
|
||||
];
|
||||
w.run_attrs(&a).0
|
||||
};
|
||||
let get = |plain: &[(&str, String)], name: &str| {
|
||||
plain
|
||||
.iter()
|
||||
.find(|(n, _)| *n == name)
|
||||
.map(|(_, v)| v.clone())
|
||||
};
|
||||
let p = run(-0.2318);
|
||||
assert_eq!(get(&p, "KerningMethod").as_deref(), Some("$ID/Manual"));
|
||||
assert_eq!(get(&p, "KerningValue").as_deref(), Some("-232"));
|
||||
let p = run(-7e-18);
|
||||
assert_eq!(get(&p, "KerningMethod").as_deref(), Some("$ID/None"));
|
||||
assert_eq!(get(&p, "KerningValue").as_deref(), Some("0"));
|
||||
// No manual kerning keeps the stored method.
|
||||
let p = run(KERNING_NONE);
|
||||
assert_eq!(get(&p, "KerningMethod").as_deref(), Some("$ID/Optical"));
|
||||
assert_eq!(get(&p, "KerningValue").as_deref(), Some("1e+11"));
|
||||
// Styles keep the stored method.
|
||||
let mut a = Attrs::default();
|
||||
a.values = vec![(0x1B07, Value::Ref(79875)), (0x1B13, Value::Double(0.05))];
|
||||
let p = w.text_attrs(&a).0;
|
||||
assert_eq!(get(&p, "KerningMethod").as_deref(), Some("$ID/Optical"));
|
||||
assert_eq!(get(&p, "KerningValue").as_deref(), Some("50"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writes_composite_fonts_and_grep_styles() {
|
||||
let mut doc = Document::default();
|
||||
|
||||
+40
-1
@@ -1314,7 +1314,9 @@ impl Writer<'_> {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if same {
|
||||
// Every corpus sample is `false`; no field is known for it
|
||||
// (text-variables.md, running headers).
|
||||
if same || v.settings.as_ref().is_some_and(|st| st.c <= 1) {
|
||||
x.attr("DeleteEndPunctuation", "false");
|
||||
}
|
||||
x.end();
|
||||
@@ -1538,3 +1540,40 @@ fn toc_separator(s: &str) -> String {
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::model::variable::Settings;
|
||||
|
||||
#[test]
|
||||
fn writes_running_header_punctuation() {
|
||||
let doc = Document::default();
|
||||
let w = Writer::for_test(&doc);
|
||||
let header = |c, as_in_samples| {
|
||||
let v = TextVariable {
|
||||
uid: 0x89,
|
||||
name: "Running Header".into(),
|
||||
kind: 0xCAAA,
|
||||
text: String::new(),
|
||||
style: None,
|
||||
as_in_samples,
|
||||
settings: Some(Settings {
|
||||
a: 0xCAC9,
|
||||
b: 0,
|
||||
c,
|
||||
before: String::new(),
|
||||
after: String::new(),
|
||||
}),
|
||||
};
|
||||
let mut x = Xml::new();
|
||||
w.variable_preference(&mut x, &v);
|
||||
x.finish()
|
||||
};
|
||||
let out = header(1, false);
|
||||
assert!(out.contains("ChangeCase=\"None\""));
|
||||
assert!(out.contains("DeleteEndPunctuation=\"false\""));
|
||||
// An unknown search strategy may be a flag for it.
|
||||
assert!(!header(0x40, false).contains("DeleteEndPunctuation"));
|
||||
}
|
||||
}
|
||||
+11
-6
@@ -24,8 +24,9 @@ pub(super) enum Kind {
|
||||
Percent,
|
||||
/// Multiplied by the factor when written.
|
||||
Scale(f64),
|
||||
/// Manual kerning in ems, written in thousandths of an em; 1e8 (the
|
||||
/// root style's value) is left out.
|
||||
/// Manual kerning in ems, written in thousandths of an em rounded to
|
||||
/// a whole number (−0 as 0); 1e8, no manual kerning, is written
|
||||
/// `1e+11`.
|
||||
Kerning,
|
||||
/// An integer, written as such.
|
||||
Integer,
|
||||
@@ -172,10 +173,14 @@ impl Writer<'_> {
|
||||
.map(|f| text("unit", num(f))),
|
||||
Kind::Percent => v.as_f64().map(|f| text("unit", num(f * 100.0))),
|
||||
Kind::Scale(k) => v.as_f64().map(|f| text("unit", num(round(f * k)))),
|
||||
Kind::Kerning => v
|
||||
.as_f64()
|
||||
.filter(|&f| f != KERNING_NONE)
|
||||
.map(|f| text("unit", num(round(f * 1000.0)))),
|
||||
Kind::Kerning => v.as_f64().map(|f| {
|
||||
if f == KERNING_NONE {
|
||||
text("unit", "1e+11".into())
|
||||
} else {
|
||||
// Adding 0 turns −0 into 0.
|
||||
text("unit", num((f * 1000.0).round() + 0.0))
|
||||
}
|
||||
}),
|
||||
Kind::Integer => v.as_u32().map(|u| text("long", u.to_string())),
|
||||
Kind::EdgeTint => {
|
||||
number().map(|f| text("unit", num(if f == -1.0 { 100.0 } else { f })))
|
||||
|
||||
+2
-2
@@ -286,7 +286,7 @@ impl Writer<'_> {
|
||||
|
||||
/// The start tag of a character range for `r`.
|
||||
pub(super) fn csr_start(&self, x: &mut Xml, r: &TextRun) {
|
||||
let (plain, props) = self.text_attrs(&r.character_attrs);
|
||||
let (plain, props) = self.run_attrs(&r.character_attrs);
|
||||
x.start("CharacterStyleRange").attr(
|
||||
"AppliedCharacterStyle",
|
||||
self.style_ref(r.character_style, false),
|
||||
@@ -508,7 +508,7 @@ impl Writer<'_> {
|
||||
outer.push(k);
|
||||
}
|
||||
let para = para_of(runs[i]);
|
||||
let (plain, props) = self.text_attrs(para.1);
|
||||
let (plain, props) = self.run_attrs(para.1);
|
||||
x.start("ParagraphStyleRange")
|
||||
.attr("AppliedParagraphStyle", self.style_ref(para.0, true));
|
||||
for (k, v) in &plain {
|
||||
|
||||
Reference in new issue
Block a user