fontoxide-ttlib 0.2.0

Read and write OpenType/TrueType fonts and TTX; a port of fontTools.ttLib
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! `fontTools.varLib.iup`: interpolation of untouched points (IUP) and the
//! optimizer that leaves out the deltas IUP can infer.
//!
//! It lives in ttlib, rather than in `fontoxide-varlib`, because ttLib needs it:
//! glyph sets apply `iup_delta`, and `TupleVariation.optimize` calls
//! `iup_delta_optimize`. fontTools' `iup` module does not depend on the rest of
//! varLib. `fontoxide_varlib::iup` re-exports this module.

use std::collections::BTreeSet;

use fontoxide_misc::array_tools::Point;

pub use crate::var::{iup_contour, iup_delta, iup_segment};
use crate::{Error, Result};

const MAX_LOOKBACK: i64 = 8;

/// Python's `seq[i]`, where `-1` is the last item.
fn at(points: &[Point], i: i64) -> Point {
	points[if i < 0 { points.len() as i64 + i } else { i } as usize]
}

/// The rounded deltas and coordinates of a contour (`rounded_deltas`,
/// `rounded_coords`), when any of them is fractional.
#[derive(Clone, Copy)]
struct Rounded<'a> {
	deltas: &'a [Point],
	coords: &'a [Point],
}

/// `_iup_round_points`: the points rounded with `floor(v + 0.5)`, or `None` if
/// they are all integral already (fontTools returns the same list).
///
/// This follows the compiled (Cython) module, which uses the C `floor`; pure
/// Python's `math.floor` raises on infinite and NaN values.
fn round_points(points: &[Point]) -> Option<Vec<Point>> {
	points
		.iter()
		.any(|&(x, y)| x != x.floor() || y != y.floor())
		.then(|| {
			points
				.iter()
				.map(|&(x, y)| ((x + 0.5).floor(), (y + 0.5).floor()))
				.collect()
		})
}

/// Whether the deltas between `i` and `j` interpolate from theirs within
/// `tolerance`.
fn interpolates(deltas: &[Point], coords: &[Point], i: i64, j: i64, tolerance: f64) -> bool {
	let between = (i + 1) as usize..j as usize;
	let interp = iup_segment(
		&coords[between.clone()],
		at(coords, i),
		at(deltas, i),
		at(coords, j),
		at(deltas, j),
	);
	deltas[between].iter().zip(&interp).all(|(d, p)| {
		// `abs(complex(...))`.
		(d.0 - p.0).hypot(d.1 - p.1) <= tolerance
	})
}

/// `can_iup_in_between`: whether the deltas between `i` and `j` interpolate
/// within `tolerance`, both as they are and, if given, after rounding the
/// coordinates and deltas.
fn can_iup_in_between(
	deltas: &[Point],
	coords: &[Point],
	i: i64,
	j: i64,
	tolerance: f64,
	rounded: Option<Rounded<'_>>,
) -> bool {
	if !interpolates(deltas, coords, i, j, tolerance) {
		return false;
	}
	match rounded {
		None => true,
		Some(r) => interpolates(r.deltas, r.coords, i, j, tolerance),
	}
}

/// `_iup_contour_bound_forced_set`: the points that must be encoded explicitly.
fn bound_forced_set(deltas: &[Point], coords: &[Point], tolerance: f64) -> BTreeSet<usize> {
	let n = deltas.len();
	let mut forced = BTreeSet::new();
	let get = |p: Point, j: usize| if j == 0 { p.0 } else { p.1 };
	for i in (0..n).rev() {
		let last = (i + n - 1) % n;
		let next = (i + 1) % n;
		for j in 0..2 {
			let cj = get(coords[i], j);
			let dj = get(deltas[i], j);
			let (lcj, ldj) = (get(coords[last], j), get(deltas[last], j));
			let (ncj, ndj) = (get(coords[next], j), get(deltas[next], j));
			let (c1, c2, d1, d2) = if lcj <= ncj {
				(lcj, ncj, ldj, ndj)
			} else {
				(ncj, lcj, ndj, ldj)
			};
			let mut force = false;
			if c1 == c2 {
				// The interpolation gives the same delta if both are equal, and
				// zero if they differ. This test has to be before the next one.
				if (d1 - d2).abs() > tolerance && dj.abs() > tolerance {
					force = true;
				}
			} else if c1 <= cj && cj <= c2 {
				// Between the adjacent points, the delta must be between theirs.
				if !(d1.min(d2) - tolerance <= dj && dj <= d1.max(d2) + tolerance) {
					force = true;
				}
			} else if d1 != d2 {
				// Otherwise, the delta should either match the closest, or have the
				// same sign as the interpolation of the two deltas.
				if cj < c1 {
					if dj.abs() > tolerance
						&& (dj - d1).abs() > tolerance
						&& ((dj - tolerance < d1) != (d1 < d2))
					{
						force = true;
					}
				} else if dj.abs() > tolerance
					&& (dj - d2).abs() > tolerance
					&& ((d2 < dj + tolerance) != (d1 < d2))
				{
					force = true;
				}
			}
			if force {
				forced.insert(i);
				break;
			}
		}
	}
	forced
}

/// The chain and costs of `_iup_contour_optimize_dp`, for indices from -1.
struct Dp {
	chain: Vec<i64>,
	costs: Vec<i64>,
}

impl Dp {
	fn chain(&self, i: i64) -> i64 {
		self.chain[(i + 1) as usize]
	}

	fn cost(&self, i: i64) -> i64 {
		self.costs[(i + 1) as usize]
	}
}

/// `_iup_contour_optimize_dp`: for each index, the least costly encoding of the
/// points up to it, with that point explicit.
fn optimize_dp(
	deltas: &[Point],
	coords: &[Point],
	forced: &BTreeSet<usize>,
	tolerance: f64,
	lookback: Option<usize>,
	rounded: Option<Rounded<'_>>,
) -> Dp {
	let n = deltas.len() as i64;
	let lookback = (lookback.map_or(n, |l| l as i64)).min(MAX_LOOKBACK);
	let is_forced = |i: i64| i >= 0 && forced.contains(&(i as usize));
	let mut dp = Dp {
		chain: vec![i64::MIN],
		costs: vec![0],
	};
	for i in 0..n {
		let mut best_cost = dp.cost(i - 1) + 1;
		dp.costs.push(best_cost);
		dp.chain.push(i - 1);
		if is_forced(i - 1) {
			continue;
		}
		let stop = (i - lookback).max(-2);
		let mut j = i - 2;
		while j > stop {
			let cost = dp.cost(j) + 1;
			if cost < best_cost && can_iup_in_between(deltas, coords, j, i, tolerance, rounded) {
				best_cost = cost;
				dp.costs[(i + 1) as usize] = cost;
				dp.chain[(i + 1) as usize] = j;
			}
			if is_forced(j) {
				break;
			}
			j -= 1;
		}
	}
	dp
}

/// `_iup_contour_bound_forced_set`, for tests.
#[doc(hidden)]
pub fn iup_contour_bound_forced_set(
	deltas: &[Point],
	coords: &[Point],
	tolerance: f64,
) -> BTreeSet<usize> {
	bound_forced_set(deltas, coords, tolerance)
}

/// `_iup_contour_optimize_dp`, for tests: the chain and costs by index from -1.
#[doc(hidden)]
pub fn iup_contour_optimize_dp(
	deltas: &[Point],
	coords: &[Point],
	forced: &BTreeSet<usize>,
	tolerance: f64,
) -> (Vec<i64>, Vec<i64>) {
	let dp = optimize_dp(deltas, coords, forced, tolerance, None, None);
	(dp.chain, dp.costs)
}

/// `_rot_list`: item 0 goes to position `k`.
fn rot_list(l: &[Point], k: usize) -> Vec<Point> {
	let n = l.len();
	let k = k % n;
	let mut out = l[n - k..].to_vec();
	out.extend_from_slice(&l[..n - k]);
	out
}

/// `iup_contour_optimize`: which deltas of a contour must be explicit.
///
/// Omitted deltas must be reconstructible within tolerance both before and
/// after OpenType rounding of the coordinates and explicit deltas.
pub fn iup_contour_optimize(
	deltas: &[Point],
	coords: &[Point],
	tolerance: f64,
) -> Result<Vec<bool>> {
	let n = deltas.len();
	// If all are within tolerance distance of 0, encode nothing.
	let negligible = |p: &Point| {
		(*p == (0.0, 0.0) && tolerance >= 0.0)
			|| (p.0.hypot(p.1) <= tolerance
				&& (tolerance < 0.5 || (p.0 + 0.5).floor().hypot((p.1 + 0.5).floor()) <= tolerance))
	};
	if deltas.iter().all(negligible) {
		return Ok(vec![false; n]);
	}
	// If there's exactly one point, return it.
	if n == 1 {
		return Ok(vec![true]);
	}
	// If all deltas are exactly the same, return just the first one.
	if deltas.iter().all(|d| *d == deltas[0]) {
		let mut keep = vec![false; n];
		keep[0] = true;
		return Ok(keep);
	}

	let (rounded_deltas, rounded_coords) = match (round_points(deltas), round_points(coords)) {
		(None, None) => (None, None),
		(d, c) => (
			Some(d.unwrap_or_else(|| deltas.to_vec())),
			Some(c.unwrap_or_else(|| coords.to_vec())),
		),
	};

	let mut forced = bound_forced_set(deltas, coords, tolerance);
	if let (Some(rd), Some(rc)) = (&rounded_deltas, &rounded_coords) {
		forced.extend(bound_forced_set(rd, rc, tolerance));
	}
	let not_subset = || Error::Assertion("forced <= solution".into());
	if let Some(&last) = forced.last() {
		// Rotate the contour such that the last point is a forced one.
		let k = n - 1 - last;
		let rotated_deltas = rot_list(deltas, k);
		let rotated_coords = rot_list(coords, k);
		let rotated_forced: BTreeSet<usize> = forced.iter().map(|v| (v + k) % n).collect();
		let rotated_rounded = rounded_deltas
			.as_deref()
			.zip(rounded_coords.as_deref())
			.map(|(d, c)| (rot_list(d, k), rot_list(c, k)));
		let dp = optimize_dp(
			&rotated_deltas,
			&rotated_coords,
			&rotated_forced,
			tolerance,
			None,
			rotated_rounded.as_ref().map(|(d, c)| Rounded {
				deltas: d,
				coords: c,
			}),
		);
		let mut solution = BTreeSet::new();
		let mut i = n as i64 - 1;
		while i != -1 {
			solution.insert(i as usize);
			i = dp.chain(i);
		}
		if !rotated_forced.is_subset(&solution) {
			return Err(not_subset());
		}
		let mut keep = vec![false; n];
		for i in solution {
			keep[(i + n - k) % n] = true;
		}
		Ok(keep)
	} else {
		// Repeat the contour, solve the new case, then look for solutions of the
		// circular problem in the solution for the linear one.
		let doubled_deltas = [deltas, deltas].concat();
		let doubled_coords = [coords, coords].concat();
		let doubled_rounded = rounded_deltas
			.as_deref()
			.zip(rounded_coords.as_deref())
			.map(|(d, c)| ([d, d].concat(), [c, c].concat()));
		let dp = optimize_dp(
			&doubled_deltas,
			&doubled_coords,
			&forced,
			tolerance,
			Some(n),
			doubled_rounded.as_ref().map(|(d, c)| Rounded {
				deltas: d,
				coords: c,
			}),
		);
		let n = n as i64;
		let mut best: Option<BTreeSet<usize>> = None;
		let mut best_cost = n + 1;
		for start in n - 1..2 * n - 1 {
			let mut solution = BTreeSet::new();
			let mut i = start;
			while i > start - n {
				solution.insert((i % n) as usize);
				i = dp.chain(i);
			}
			if i == start - n {
				let cost = dp.cost(start) - dp.cost(start - n);
				if cost <= best_cost {
					best = Some(solution);
					best_cost = cost;
				}
			}
		}
		let best = best.ok_or_else(not_subset)?;
		Ok((0..n as usize).map(|i| best.contains(&i)).collect())
	}
}

/// `iup_delta_optimize`: which deltas of an outline, whose contours end at `ends`
/// and whose last four points are the phantom points, must be explicit.
pub fn iup_delta_optimize(
	deltas: &[Point],
	coords: &[Point],
	ends: &[usize],
	tolerance: f64,
) -> Result<Vec<bool>> {
	let expected = ends.last().map_or(0, |e| e + 1) + 4;
	if !ends.is_sorted() || coords.len() != expected || deltas.len() != coords.len() {
		return Err(Error::Assertion(
			"sorted(ends) == ends and len(coords) == (ends[-1] + 1 if ends else 0) + 4".into(),
		));
	}
	let n = coords.len();
	let mut out = Vec::with_capacity(n);
	let mut start = 0;
	for end in ends.iter().copied().chain([n - 4, n - 3, n - 2, n - 1]) {
		out.extend(iup_contour_optimize(
			&deltas[start..=end],
			&coords[start..=end],
			tolerance,
		)?);
		start = end + 1;
	}
	Ok(out)
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn optimize_keeps_what_interpolation_cannot_give() -> Result<()> {
		let coords = [(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)];
		// All the same.
		assert_eq!(
			iup_contour_optimize(&[(5.0, 5.0); 4], &coords, 0.5)?,
			[true, false, false, false]
		);
		// All within tolerance.
		assert_eq!(
			iup_contour_optimize(&[(0.1, 0.1); 4], &coords, 0.5)?,
			[false; 4]
		);
		// One point.
		assert_eq!(
			iup_contour_optimize(&[(1.0, 2.0)], &[(0.0, 0.0)], 0.5)?,
			[true]
		);
		// Values from fontTools.
		let coords = [(0.0, 0.0), (50.0, 0.0), (100.0, 0.0), (50.0, 50.0)];
		let deltas = [(0.0, 0.0), (5.0, 0.0), (10.0, 0.0), (20.0, 30.0)];
		assert_eq!(
			iup_contour_optimize(&deltas, &coords, 0.5)?,
			[false, true, true, true]
		);
		let coords = [(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)];
		let deltas = [(1.0, 0.0), (2.0, 0.0), (3.0, 5.0), (0.0, 9.0)];
		assert_eq!(iup_contour_optimize(&deltas, &coords, 0.5)?, [true; 4]);
		let coords = [
			(0.0, 0.0),
			(10.0, 0.0),
			(20.0, 0.0),
			(30.0, 0.0),
			(40.0, 5.0),
			(30.0, 10.0),
			(20.0, 10.0),
			(10.0, 10.0),
			(0.0, 10.0),
		];
		let deltas = [
			(0.0, 0.0),
			(1.0, 0.0),
			(2.0, 0.0),
			(3.0, 0.0),
			(9.0, 9.0),
			(3.0, 1.0),
			(2.0, 1.0),
			(1.0, 1.0),
			(0.0, 1.0),
		];
		assert_eq!(
			iup_contour_optimize(&deltas, &coords, 0.5)?,
			[true, false, false, true, true, true, false, false, false]
		);
		Ok(())
	}
}