fix: write numbers as IDML does at every length

num() rounded ties to even only at 17 significant digits. IDML takes
the decimal nearest to the exact value at the shortest length, with
ties to even, at any length, and writes numbers below 1e-4 with an
exponent (1e-05). The rule gives the IDML text for all but 67 of the
1,090,937 distinct numeric texts of the reference IDMLs (those 67 are
text values).

diff_outputs.py: 2,672 outputs differ, no change in failures or
warnings.

compare.py --exclude own/:
- trustworthy 97.72% (+77 values): PDF GraphicBounds 849 -> 913 of
  913, EPS 323 -> 329, SVG 140 -> 146
- all pairs +99 values: PDF, EPS and SVG GraphicBounds all equal
- extra values unchanged
This commit is contained in:
aiongg committed 2026-10-08 13:51:34 +00:00
1 parent d2ad1e611a
commit 0a01b3c6d4
2 files changed
+64 -46

No files matched your search

+17 -12
View File
@@ -489,19 +489,24 @@ every other font.
## Number format
IDML writes a number as the shortest decimal that reads back as the same
64-bit value, in plain notation (no exponent). When that decimal needs 17
significant digits, there can be two 17-digit candidates equally close to
the value; IDML then takes the one with the even last digit. For example
the value 150.235992431640625 is written `150.23599243164062`, not
`…063`. This is the 17-digit decimal nearest to the exact value, with
ties to even.
64-bit value. When two decimals of that length read back as the value,
IDML takes the one nearest to the exact binary value, and of two equally
near (the value lies exactly halfway, common for values that came from
32-bit floats) the one with the even last digit. For example the value
150.235992431640625 is written `150.23599243164062`, not `…063`.
Numbers below 1e-4 are written with an exponent of at least two digits
(`5.684341886080802e-14`, `1.8897456811828306e-05`); all others are
written in plain notation.
Evidence: all 20,524 `GraphicBounds` values of images, PDF, EPS, SVG and
WMF graphics and imported pages in the trustworthy pairs follow this
rule (compared with the text Python's `repr` gives for the same value).
The INDD values (chunk 0x1633) match 1,001 of 1,001 PDF and 338 of 338
EPS graphics with this format; Rust's shortest form differs in the last
digit for 208 of these values. The converter writes every number this
Evidence: of the 1,090,937 distinct numeric texts in the attributes of
the 654 reference IDMLs of the corpus before 2026-10, the rule gives the
same text for all but 67, which are text values rather than numbers
(`DOMVersion="17.0"`, `2.000`, `0.80`). Rust's shortest form differs on
7,674 of them: ties written with the odd digit, and small numbers
written without an exponent. The INDD values of `GraphicBounds` (chunk
0x1633), written this way, equal the IDML text in all 1,774 PDF, 407 EPS
and 233 SVG graphics of the 803 pairs (`compare.py` compares
`Properties` children as text). The converter writes every number this
way.
## Preferences
+47 -34
View File
@@ -142,60 +142,67 @@ pub(super) fn ui_color_property(tag: &str, rgb: [f64; 3]) -> Option<Node> {
}
/// A number as IDML text: the shortest decimal that reads back as the same
/// value, in plain notation. When that needs 17 significant digits, IDML
/// takes the 17-digit decimal nearest to the value, with ties to the even
/// digit (`150.23599243164062`, where Rust's shortest form has `…063`);
/// see `docs/format/idml-values.md`, number format. IDML keeps negative
/// zero (`1 -0 -0 1 0 0`). IDML has no text for NaN or infinity; they are
/// value. When two decimals of that length read back as the value, IDML
/// takes the one nearest to the exact value, with ties to the even digit
/// (`150.23599243164062`, where Rust's shortest form has `…063`). Numbers
/// below 1e-4 are written with an exponent of at least two digits
/// (`5.684341886080802e-14`), all others in plain notation; see
/// `docs/format/idml-values.md`, number format. IDML keeps negative zero
/// (`1 -0 -0 1 0 0`). IDML has no text for NaN or infinity; they are
/// written as 0, which keeps the package valid.
pub fn num(v: f64) -> String {
if !v.is_finite() {
return "0".to_string();
}
let short = format!("{v}");
if significant_digits(&short) < 17 {
return short;
if v == 0.0 {
return if v.is_sign_negative() { "-0" } else { "0" }.into();
}
// `{:.16e}` rounds the exact value to 17 digits, ties to even.
let sci = format!("{v:.16e}");
// The shortest digits that read back as v, then the decimal of that
// length nearest to the exact value (`{:.*e}` rounds ties to even),
// if it also reads back as v.
let shortest = format!("{v:e}");
let Some((mantissa, _)) = shortest.split_once('e') else {
return format!("{v}");
};
let len = mantissa.chars().filter(char::is_ascii_digit).count();
let nearest = format!("{:.*e}", len.saturating_sub(1), v);
let sci = if nearest.parse::<f64>() == Ok(v) {
nearest
} else {
shortest
};
let Some((mantissa, exp)) = sci.split_once('e') else {
return short;
return format!("{v}");
};
let Ok(exp) = exp.parse::<i32>() else {
return short;
return format!("{v}");
};
let (sign, mantissa) = match mantissa.strip_prefix('-') {
Some(m) => ("-", m),
None => ("", mantissa),
};
let digits: String = mantissa.chars().filter(|c| c.is_ascii_digit()).collect();
let digits: String = mantissa.chars().filter(char::is_ascii_digit).collect();
let digits = digits.trim_end_matches('0');
// The decimal point goes after `exp + 1` digits.
let point = exp + 1;
let text = if point <= 0 {
format!("0.{}{digits}", "0".repeat(point.unsigned_abs() as usize))
} else if point as usize >= digits.len() {
format!("{digits}{}", "0".repeat(point as usize - digits.len()))
let digits = if digits.is_empty() { "0" } else { digits };
let text = if exp < -4 {
let (first, rest) = digits.split_at(1);
let point = if rest.is_empty() { "" } else { "." };
format!("{first}{point}{rest}e-{:02}", -exp)
} else if exp < 0 {
format!("0.{}{digits}", "0".repeat((-exp - 1) as usize))
} else {
let (int, frac) = digits.split_at(point as usize);
format!("{int}.{frac}")
// The decimal point goes after `exp + 1` digits.
let point = exp as usize + 1;
if point >= digits.len() {
format!("{digits}{}", "0".repeat(point - digits.len()))
} else {
let (int, frac) = digits.split_at(point);
format!("{int}.{frac}")
}
};
format!("{sign}{text}")
}
/// The number of significant digits of a number in plain notation.
fn significant_digits(text: &str) -> usize {
let digits: String = text.chars().filter(|c| c.is_ascii_digit()).collect();
let digits = digits.trim_start_matches('0');
// Trailing zeros of an integer are not significant either; `{}` writes
// no trailing zeros after a decimal point.
if text.contains('.') {
digits.len()
} else {
digits.trim_end_matches('0').len()
}
}
/// A link time (FILETIME: 100 ns intervals since 1601-01-01 UTC) as IDML
/// writes it, `YYYY-MM-DDTHH:MM:SS` in the local time of the computer that
/// exported the IDML. The INDD does not store that time zone; the offset
@@ -380,6 +387,12 @@ mod tests {
assert_eq!(num(0.2779084995276255), "0.2779084995276255");
assert_eq!(num(1e20), "100000000000000000000");
assert_eq!(num(0.1 + 0.2), "0.30000000000000004");
// Ties at fewer digits, and small numbers with an exponent.
assert_eq!(num(0.125), "0.125");
assert_eq!(num(5.684341886080802e-14), "5.684341886080802e-14");
assert_eq!(num(1.8897456811828306e-05), "1.8897456811828306e-05");
assert_eq!(num(1e-5), "1e-05");
assert_eq!(num(0.0001), "0.0001");
for v in [1.0 / 3.0, 2.0 / 3.0, 1e-7 / 3.0, 123456.789e3 / 7.0] {
assert_eq!(num(v).parse::<f64>().unwrap(), v);
}