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
//! The system font-size setting, as the curve it is and not the multiplier it
//! looks like.
//!
//! On Android 13 and below the platform resolves a size in `Sp` as
//! `sp * font_scale` dp. Android 14 does not: above a threshold setting it runs
//! the sp value through a piecewise-linear table, so small text grows by the
//! full setting and large text grows by less.
//! `TypedValue.applyDimension(COMPLEX_UNIT_SP, ..)` answers from that table, and
//! Jetpack Compose carries its own copy so its `Density` answers the same.
//!
//! Measured on a Wear OS 5 emulator at density 2.0 with the setting at 1.24
//! (`TypedValue.applyDimension` and `androidx.compose.ui.unit.Density(context)`
//! agree to the last bit on every sample):
//!
//! | sp | multiplied | platform |
//! |---|---|---|
//! | 12 | 29.76 px | 29.76 px |
//! | 13 | 32.24 px | **32.72 px** |
//! | 14 | 34.72 px | **35.68 px** |
//! | 16 | 39.68 px | **38.72 px** |
//! | 19 | 47.12 px | **43.76 px** |
//!
//! So multiplying is wrong in both directions at once, and by enough to change
//! where a line wraps. A [`FontScaleCurve`] carries the platform's answer
//! instead: the host samples the real conversion when the configuration says it
//! changed, and everything that resolves an `Sp` goes through
//! [`FontScaleCurve::sp_to_dp`].
//!
//! Off Android, and on an Android below the version that has a table, the curve
//! holds no knots and `sp_to_dp` is the multiplication again — which is what
//! those platforms actually do.
/// How many knots a curve keeps.
///
/// The platform's own table has ten, and collapsing the points that sit on a
/// straight line through their neighbours leaves fewer, so this is headroom
/// rather than a limit anyone is expected to reach.
pub const MAX_FONT_SCALE_KNOTS: usize = 12;
const COLLINEAR_EPSILON_DP: f32 = 1.0e-3;
/// The sp → dp conversion the platform performs, sampled from the platform.
///
/// With no knots this is `sp * scale`. With knots it is the piecewise-linear
/// function through them, extended past both ends by the ratio of the knot on
/// that end — which is how the platform extends its own table, so a size below
/// the first knot scales by the plain setting and one above the last keeps the
/// last knot's ratio.
///
/// A curve is `Copy` and rides in every [`crate::Density`], which each layout
/// node keeps and the measurement path passes by value, so its knots live in
/// a table shared by every curve with the same knots, which it reaches by a
/// thin pointer: a curve is 16 bytes.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct FontScaleCurve {
scale: f32,
fingerprint: u32,
knots: &'static Vec<(f32, f32)>,
}
/// The knots of every linear curve.
static NO_KNOTS: Vec<(f32, f32)> = Vec::new();
impl Default for FontScaleCurve {
fn default() -> Self {
Self::linear(1.0)
}
}
impl FontScaleCurve {
/// The plain multiplier: what every platform without a conversion table
/// does, and what Android itself did before it had one.
pub const fn linear(scale: f32) -> Self {
Self {
scale,
fingerprint: 0,
knots: &NO_KNOTS,
}
}
/// A curve through `samples`, which are `(sp, dp)` pairs read from the
/// platform in ascending sp order.
///
/// Samples that lie on the straight line between their neighbours are
/// dropped, so a densely sampled table comes back as the handful of points
/// that actually bend. Anything the platform could not have produced — an
/// empty or unsorted set, a non-finite or non-positive value, or more bends
/// than [`MAX_FONT_SCALE_KNOTS`] — is refused, and the caller gets the
/// multiplier rather than a curve nobody measured. Samples that all lie on
/// the multiplier's own line are the multiplier, and come back as it, so a
/// platform answering with the plain setting gives a curve equal to
/// [`FontScaleCurve::linear`].
pub fn from_samples(scale: f32, samples: &[(f32, f32)]) -> Self {
let Some(kept) = compress(samples) else {
return Self::linear(scale);
};
if kept
.iter()
.all(|(sp, dp)| (dp - sp * scale).abs() <= COLLINEAR_EPSILON_DP)
{
return Self::linear(scale);
}
Self {
scale,
fingerprint: fingerprint(scale, &kept),
knots: shared_knots(kept),
}
}
/// The setting itself — what the user chose, whatever the table then does
/// with an individual size. This is the number to report, not the number to
/// multiply by.
pub fn scale(self) -> f32 {
self.scale
}
/// Whether this is the plain multiplier, with no table behind it.
pub fn is_linear(self) -> bool {
self.knots.is_empty()
}
/// Whether resolving an `Sp` through this curve changes nothing at all.
///
/// Asked of the conversion and not of the representation: a platform that
/// hands back a table while the setting sits at 1.0 hands back a table
/// whose every knot maps a size to itself, and that is still nothing to do.
pub fn is_identity(self) -> bool {
if self.knots.is_empty() {
return (self.scale - 1.0).abs() <= f32::EPSILON;
}
self.knots
.iter()
.all(|(sp, dp)| (dp - sp).abs() <= COLLINEAR_EPSILON_DP)
}
/// The knots, ascending by sp. Empty for a linear curve.
pub fn knots(self) -> &'static [(f32, f32)] {
self.knots.as_slice()
}
/// How many [`FontScaleCurve::knots`] there are.
pub fn knot_count(self) -> usize {
self.knots.len()
}
/// A cheap identity for cache keys: two curves that convert identically
/// share it, and one that does not is overwhelmingly unlikely to.
pub fn fingerprint(self) -> u32 {
self.fingerprint ^ self.scale.to_bits()
}
/// A size in scale-independent pixels, in dp.
///
/// This is `FontScaleConverterImpl.convertSpToDp`: the sign is carried
/// separately, an exact hit on a knot returns that knot, a size outside the
/// table scales by the ratio of the nearest end, and everything between two
/// knots is interpolated.
pub fn sp_to_dp(self, sp: f32) -> f32 {
if !sp.is_finite() {
return sp;
}
if self.knots.is_empty() {
return sp * self.scale;
}
self.sp_to_dp_on_knots(sp)
}
// `sp_to_dp` of a curve with knots. Out of line: most hosts scale fonts
// linearly, and every text size goes through `sp_to_dp`.
#[inline(never)]
fn sp_to_dp_on_knots(self, sp: f32) -> f32 {
let knots = self.knots;
let (Some(&(first_sp, first_dp)), Some(&(last_sp, last_dp))) =
(knots.first(), knots.last())
else {
return sp * self.scale;
};
let magnitude = sp.abs();
let sign = if sp.is_sign_negative() { -1.0 } else { 1.0 };
if magnitude <= first_sp {
return sign * magnitude * (first_dp / first_sp);
}
if magnitude >= last_sp {
return sign * magnitude * (last_dp / last_sp);
}
for window in knots.windows(2) {
let (low_sp, low_dp) = window[0];
let (high_sp, high_dp) = window[1];
if magnitude <= high_sp {
let t = (magnitude - low_sp) / (high_sp - low_sp);
return sign * (low_dp + (high_dp - low_dp) * t);
}
}
sign * magnitude * (last_dp / last_sp)
}
}
fn compress(samples: &[(f32, f32)]) -> Option<Vec<(f32, f32)>> {
if samples.len() < 2 {
return None;
}
let mut previous_sp = 0.0f32;
for (sp, dp) in samples {
if !sp.is_finite() || !dp.is_finite() || *sp <= previous_sp || *dp <= 0.0 {
return None;
}
previous_sp = *sp;
}
let mut kept: Vec<(f32, f32)> = Vec::with_capacity(samples.len());
kept.push(samples[0]);
for index in 1..samples.len() - 1 {
let (low_sp, low_dp) = *kept.last().expect("the first sample was pushed");
let (sp, dp) = samples[index];
let (high_sp, high_dp) = samples[index + 1];
let t = (sp - low_sp) / (high_sp - low_sp);
let straight = low_dp + (high_dp - low_dp) * t;
if (dp - straight).abs() > COLLINEAR_EPSILON_DP {
kept.push(samples[index]);
}
}
kept.push(samples[samples.len() - 1]);
if kept.len() > MAX_FONT_SCALE_KNOTS {
return None;
}
Some(kept)
}
/// The table holding `knots`, shared with every curve that has the same
/// knots. A process meets one table per font scale setting the user picks, so
/// the tables are kept for its life.
fn shared_knots(mut knots: Vec<(f32, f32)>) -> &'static Vec<(f32, f32)> {
static TABLES: std::sync::Mutex<Vec<&'static Vec<(f32, f32)>>> =
std::sync::Mutex::new(Vec::new());
let mut tables = TABLES
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let Some(table) = tables.iter().find(|table| ***table == knots) {
return table;
}
knots.shrink_to_fit();
let table: &'static Vec<(f32, f32)> = Box::leak(Box::new(knots));
tables.push(table);
table
}
fn fingerprint(scale: f32, knots: &[(f32, f32)]) -> u32 {
let mut hash = 2166136261u32;
let mut mix = |bits: u32| {
hash ^= bits;
hash = hash.wrapping_mul(16777619);
};
mix(scale.to_bits());
for (sp, dp) in knots {
mix(sp.to_bits());
mix(dp.to_bits());
}
hash
}
#[cfg(test)]
#[path = "tests/font_scale_tests.rs"]
mod tests;