Skip to main content

fontoxide_misc/
fixed_tools.rs

1//! Port of `fontTools.misc.fixedTools`: conversions between fixed-point numbers,
2//! floats and strings.
3//!
4//! The OpenType specification defines `Fixed` (16.16) and `F2DOT14` (2.14). As in
5//! fontTools, fixed-point values are held as `f64` in table objects and converted
6//! when compiling.
7
8use crate::round_tools::{nearest_multiple_shortest_repr, ot_round};
9
10/// The largest value that still fits in an F2Dot14 (`MAX_F2DOT14`).
11pub const MAX_F2DOT14: f64 = 0x7FFF as f64 / (1 << 14) as f64;
12
13fn scale(precision_bits: u32) -> f64 {
14	(1u64 << precision_bits) as f64
15}
16
17/// Converts a fixed-point number to a float (`fixedToFloat`).
18pub fn fixed_to_float(value: i64, precision_bits: u32) -> f64 {
19	value as f64 / scale(precision_bits)
20}
21
22/// Converts a float to a fixed-point number (`floatToFixed`).
23pub fn float_to_fixed(value: f64, precision_bits: u32) -> i64 {
24	ot_round(value * scale(precision_bits))
25}
26
27/// Rounds a float to the nearest value representable in fixed point
28/// (`floatToFixedToFloat`).
29pub fn float_to_fixed_to_float(value: f64, precision_bits: u32) -> f64 {
30	let scale = scale(precision_bits);
31	ot_round(value * scale) as f64 / scale
32}
33
34/// Converts a fixed-point number to the shortest decimal string that identifies it
35/// (`fixedToStr`).
36pub fn fixed_to_str(value: i64, precision_bits: u32) -> String {
37	let scale = scale(precision_bits);
38	nearest_multiple_shortest_repr(value as f64 / scale, 1.0 / scale)
39}
40
41/// Converts a decimal string to a fixed-point number (`strToFixed`).
42pub fn str_to_fixed(string: &str, precision_bits: u32) -> Option<i64> {
43	let value = crate::py::parse_float(string)?;
44	Some(ot_round(value * scale(precision_bits)))
45}
46
47/// Converts a decimal string to a float, rounded to fixed-point precision
48/// (`strToFixedToFloat`).
49pub fn str_to_fixed_to_float(string: &str, precision_bits: u32) -> Option<f64> {
50	let value = crate::py::parse_float(string)?;
51	let scale = scale(precision_bits);
52	Some(ot_round(value * scale) as f64 / scale)
53}
54
55/// Converts a float to the shortest decimal string identifying the nearest
56/// fixed-point value (`floatToFixedToStr`).
57pub fn float_to_fixed_to_str(value: f64, precision_bits: u32) -> String {
58	nearest_multiple_shortest_repr(value, 1.0 / scale(precision_bits))
59}
60
61/// Ensures a table version is a 32-bit fixed number (`ensureVersionIsLong`): values
62/// below 0x10000 are taken as floats and converted, with a warning.
63pub fn ensure_version_is_long(value: f64) -> i64 {
64	if value < 65536.0 {
65		let new_value = float_to_fixed(value, 16);
66		log::warn!(
67			"Table version value is a float: {value:.4}; fix to use hex instead: 0x{new_value:08x}"
68		);
69		new_value
70	} else {
71		value as i64
72	}
73}
74
75/// Parses a table version from TTX (`versionToFixed`): strings starting with `0`
76/// are integers (e.g. `0x00010000`), anything else is a float.
77pub fn version_to_fixed(value: &str) -> Option<i64> {
78	let v = if value.starts_with('0') {
79		crate::text_tools::parse_int_literal(value)? as f64
80	} else {
81		crate::py::parse_float(value)?
82	};
83	Some(ensure_version_is_long(v))
84}
85
86#[cfg(test)]
87mod tests {
88	//! Port of Tests/misc/fixedTools_test.py.
89
90	use super::*;
91
92	fn assert_almost_eq(a: f64, b: f64) {
93		// unittest's assertAlmostEqual: round(a - b, 7) == 0
94		assert!((a - b).abs() < 5e-8, "{a} != {b}");
95	}
96
97	#[test]
98	fn test_roundtrip() {
99		for bits in 0..15 {
100			for value in -(1i64 << (bits + 1))..(1i64 << (bits + 1)) {
101				assert_eq!(value, float_to_fixed(fixed_to_float(value, bits), bits));
102			}
103		}
104	}
105
106	#[test]
107	fn test_fixed_to_float_precision14() {
108		assert_almost_eq(0.7999878, fixed_to_float(13107, 14));
109		assert_eq!(0.0, fixed_to_float(0, 14));
110		assert_eq!(1.0, fixed_to_float(16384, 14));
111		assert_eq!(-1.0, fixed_to_float(-16384, 14));
112		assert_almost_eq(0.999939, fixed_to_float(16383, 14));
113		assert_almost_eq(-0.999939, fixed_to_float(-16383, 14));
114	}
115
116	#[test]
117	fn test_fixed_to_float_precision6() {
118		assert_almost_eq(-9.984375, fixed_to_float(-639, 6));
119		assert_almost_eq(-10.0, fixed_to_float(-640, 6));
120		assert_almost_eq(9.984375, fixed_to_float(639, 6));
121		assert_almost_eq(10.0, fixed_to_float(640, 6));
122	}
123
124	#[test]
125	fn test_fixed_to_str_precision14() {
126		assert_eq!("0.8", fixed_to_str(13107, 14));
127		assert_eq!("0.0", fixed_to_str(0, 14));
128		assert_eq!("1.0", fixed_to_str(16384, 14));
129		assert_eq!("-1.0", fixed_to_str(-16384, 14));
130		assert_eq!("0.99994", fixed_to_str(16383, 14));
131		assert_eq!("-0.99994", fixed_to_str(-16383, 14));
132	}
133
134	#[test]
135	fn test_fixed_to_str_precision6() {
136		assert_eq!("-9.98", fixed_to_str(-639, 6));
137		assert_eq!("-10.0", fixed_to_str(-640, 6));
138		assert_eq!("9.98", fixed_to_str(639, 6));
139		assert_eq!("10.0", fixed_to_str(640, 6));
140	}
141
142	#[test]
143	fn test_float_to_fixed_precision14() {
144		assert_eq!(13107, float_to_fixed(0.8, 14));
145		assert_eq!(16384, float_to_fixed(1.0, 14));
146		assert_eq!(-16384, float_to_fixed(-1.0, 14));
147		assert_eq!(0, float_to_fixed(0.0, 14));
148	}
149
150	#[test]
151	fn test_str_to_fixed_precision14() {
152		assert_eq!(Some(13107), str_to_fixed("0.8", 14));
153		assert_eq!(Some(16384), str_to_fixed("1.0", 14));
154		assert_eq!(Some(16384), str_to_fixed("1", 14));
155		assert_eq!(Some(-16384), str_to_fixed("-1.0", 14));
156		assert_eq!(Some(-16384), str_to_fixed("-1", 14));
157		assert_eq!(Some(0), str_to_fixed("0", 14));
158	}
159
160	#[test]
161	fn test_str_to_fixed_to_float_precision14() {
162		assert_almost_eq(
163			0.7999878,
164			str_to_fixed_to_float("0.8", 14).unwrap_or_default(),
165		);
166		assert_eq!(Some(0.0), str_to_fixed_to_float("0", 14));
167		assert_eq!(Some(1.0), str_to_fixed_to_float("1.0", 14));
168		assert_eq!(Some(-1.0), str_to_fixed_to_float("-1.0", 14));
169		assert_almost_eq(
170			0.999939,
171			str_to_fixed_to_float("0.99994", 14).unwrap_or_default(),
172		);
173		assert_almost_eq(
174			-0.999939,
175			str_to_fixed_to_float("-0.99994", 14).unwrap_or_default(),
176		);
177	}
178
179	#[test]
180	fn test_float_to_fixed_to_str_precision14() {
181		assert_eq!("0.8", float_to_fixed_to_str(0.7999878, 14));
182		assert_eq!("1.0", float_to_fixed_to_str(1.0, 14));
183		assert_eq!("-1.0", float_to_fixed_to_str(-1.0, 14));
184		assert_eq!("0.0", float_to_fixed_to_str(0.0, 14));
185	}
186
187	// Doctests in Lib/fontTools/misc/fixedTools.py.
188	#[test]
189	fn doctests() {
190		assert_eq!(float_to_fixed(-0.61883544921875, 14), -10139);
191		assert_eq!(float_to_fixed(-0.61884, 14), -10139);
192		assert_eq!(fixed_to_str(-10139, 14), "-0.61884");
193		assert_eq!(str_to_fixed("-0.61884", 14), Some(-10139));
194		assert_eq!(float_to_fixed_to_str(-0.61883544921875, 14), "-0.61884");
195		let f2 = float_to_fixed_to_float(-0.61884, 14);
196		assert_ne!(f2, -0.61884);
197		assert_almost_eq(f2, -0.61883544921875);
198	}
199}