skrifa/outline/glyf/hint/round.rs
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
//! Rounding state.
use super::{super::F26Dot6, graphics::GraphicsState};
/// Rounding strategies supported by the interpreter.
#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
pub enum RoundMode {
/// Distances are rounded to the closest grid line.
///
/// Set by `RTG` instruction.
#[default]
Grid,
/// Distances are rounded to the nearest half grid line.
///
/// Set by `RTHG` instruction.
HalfGrid,
/// Distances are rounded to the closest half or integer pixel.
///
/// Set by `RTDG` instruction.
DoubleGrid,
/// Distances are rounded down to the closest integer grid line.
///
/// Set by `RDTG` instruction.
DownToGrid,
/// Distances are rounded up to the closest integer pixel boundary.
///
/// Set by `RUTG` instruction.
UpToGrid,
/// Rounding is turned off.
///
/// Set by `ROFF` instruction.
Off,
/// Allows fine control over the effects of the round state variable by
/// allowing you to set the values of three components of the round_state:
/// period, phase, and threshold.
///
/// More formally, maps the domain of 26.6 fixed point numbers into a set
/// of discrete values that are separated by equal distances.
///
/// Set by `SROUND` instruction.
Super,
/// Analogous to `Super`. The grid period is sqrt(2)/2 pixels rather than 1
/// pixel. It is useful for measuring at a 45 degree angle with the
/// coordinate axes.
///
/// Set by `S45ROUND` instruction.
Super45,
}
/// Graphics state that controls rounding.
///
/// See <https://developer.apple.com/fonts/TrueType-Reference-Manual/RM04/Chap4.html#round%20state>
#[derive(Copy, Clone, Debug)]
pub struct RoundState {
pub mode: RoundMode,
pub threshold: i32,
pub phase: i32,
pub period: i32,
}
impl Default for RoundState {
fn default() -> Self {
Self {
mode: RoundMode::Grid,
threshold: 0,
phase: 0,
period: 64,
}
}
}
impl RoundState {
pub fn round(&self, distance: F26Dot6) -> F26Dot6 {
use super::math;
use RoundMode::*;
let distance = distance.to_bits();
let result = match self.mode {
// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L1958>
HalfGrid => {
if distance >= 0 {
(math::floor(distance) + 32).max(0)
} else {
(-(math::floor(-distance) + 32)).min(0)
}
}
// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L1913>
Grid => {
if distance >= 0 {
math::round(distance).max(0)
} else {
(-math::round(-distance)).min(0)
}
}
// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L2094>
DoubleGrid => {
if distance >= 0 {
math::round_pad(distance, 32).max(0)
} else {
(-math::round_pad(-distance, 32)).min(0)
}
}
// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L2005>
DownToGrid => {
if distance >= 0 {
math::floor(distance).max(0)
} else {
(-math::floor(-distance)).min(0)
}
}
// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L2049>
UpToGrid => {
if distance >= 0 {
math::ceil(distance).max(0)
} else {
(-math::ceil(-distance)).min(0)
}
}
// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L2145>
Super => {
if distance >= 0 {
let val =
((distance + (self.threshold - self.phase)) & -self.period) + self.phase;
if val < 0 {
self.phase
} else {
val
}
} else {
let val =
-(((self.threshold - self.phase) - distance) & -self.period) - self.phase;
if val > 0 {
-self.phase
} else {
val
}
}
}
// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L2199>
Super45 => {
if distance >= 0 {
let val = (((distance + (self.threshold - self.phase)) / self.period)
* self.period)
+ self.phase;
if val < 0 {
self.phase
} else {
val
}
} else {
let val = -((((self.threshold - self.phase) - distance) / self.period)
* self.period)
- self.phase;
if val > 0 {
-self.phase
} else {
val
}
}
}
// <https://gitlab.freedesktop.org/freetype/freetype/-/blob/57617782464411201ce7bbc93b086c1b4d7d84a5/src/truetype/ttinterp.c#L1870>
Off => distance,
};
F26Dot6::from_bits(result)
}
}
impl GraphicsState<'_> {
pub fn round(&self, distance: F26Dot6) -> F26Dot6 {
self.round_state.round(distance)
}
}
#[cfg(test)]
mod tests {
use super::{F26Dot6, RoundMode, RoundState};
#[test]
fn round_to_grid() {
round_cases(
RoundMode::Grid,
&[(0, 0), (32, 64), (-32, -64), (64, 64), (50, 64)],
);
}
#[test]
fn round_to_half_grid() {
round_cases(
RoundMode::HalfGrid,
&[(0, 32), (32, 32), (-32, -32), (64, 96), (50, 32)],
);
}
#[test]
fn round_to_double_grid() {
round_cases(
RoundMode::DoubleGrid,
&[(0, 0), (32, 32), (-32, -32), (64, 64), (50, 64)],
);
}
#[test]
fn round_down_to_grid() {
round_cases(
RoundMode::DownToGrid,
&[(0, 0), (32, 0), (-32, 0), (64, 64), (50, 0)],
);
}
#[test]
fn round_up_to_grid() {
round_cases(
RoundMode::UpToGrid,
&[(0, 0), (32, 64), (-32, -64), (64, 64), (50, 64)],
);
}
#[test]
fn round_off() {
round_cases(
RoundMode::Off,
&[(0, 0), (32, 32), (-32, -32), (64, 64), (50, 50)],
);
}
fn round_cases(mode: RoundMode, cases: &[(i32, i32)]) {
for (value, expected) in cases.iter().copied() {
let value = F26Dot6::from_bits(value);
let expected = F26Dot6::from_bits(expected);
let state = RoundState {
mode,
..Default::default()
};
let result = state.round(value);
assert_eq!(
result, expected,
"mismatch in rounding: {mode:?}({value}) = {result} (expected {expected})"
);
}
}
}