use f_office_units::scale::THOUSANDTHS;
use f_office_units::{Length, div_round_i128, lowest_terms};
use super::from_line_start::FromLineStart;
use super::from_restart::FromRestart;
use super::{Advances, Device, Rounding};
use crate::params;
use crate::quirks::{AlignedTabEnd, GlyphEdgeCheck, NoteMarkPlace, NoteStartSteps};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Segment {
pub first: Length,
pub end: Length,
pub counted: Length,
}
impl Segment {
#[must_use]
pub fn counted_place(self, device: &Device, x: Length) -> Length {
let (numerator, denominator) = device.exact_scale();
self.counted + ((x - self.first) * numerator).div_round(denominator)
}
}
fn drawn_distance(scale: Option<(Length, Length)>, distance: Length) -> Length {
match scale {
Some((size, drawn)) => (distance * drawn.units()).div_round(size.units()),
None => distance,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Restart {
pub place: Length,
pub segment: Segment,
pub carried: Option<Length>,
pub rounded_up: bool,
}
pub(super) trait SegmentRule {
fn next(
&self,
device: &Device,
segment: Segment,
glyph: (Length, Length),
length: Length,
start: Length,
) -> Option<Restart>;
fn after_gap(&self, segment: Option<Segment>) -> Option<Segment>;
}
fn rule(device: &Device) -> &'static dyn SegmentRule {
match (device.advances(), device.quirks.glyph_segments) {
(Advances::RightEdge { .. }, crate::quirks::GlyphSegments::FromRestart) => &FromRestart,
_ => &FromLineStart,
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct EdgeSteps {
scale: Option<(Length, Length)>,
before_scale: Option<(Length, Length)>,
previous: Option<(Length, Length)>,
shift: Length,
anchor: (Length, Length),
drift: Exact,
rounding: Exact,
segment: Option<Segment>,
extra: GlyphExtra,
start: Length,
line_hands_on: bool,
measured: Option<(i64, Length)>,
widened: Length,
stretched: GlyphExtra,
without_bias: bool,
before_without_bias: bool,
nudge: Length,
stepped: u32,
checked: Option<EdgeCheck>,
start_place: Option<Length>,
next_place: NextPlace,
spacing: Length,
counted_extra: Exact,
short_ease: Length,
counted_excess: Exact,
share: Option<Exact>,
share_count: ShareCount,
segment_rounding: Exact,
segment_share: Exact,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
enum ShareCount {
#[default]
Before,
Counting,
Ended,
}
impl ShareCount {
const fn restarted(self) -> Self {
match self {
Self::Before => Self::Before,
Self::Counting | Self::Ended => Self::Ended,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
enum NextPlace {
#[default]
Stepped,
NearestExact,
Restepped,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EdgeCheck {
pub edge: Length,
pub edge_thousandths: i64,
pub rounding_thousandths: i64,
pub moves: i64,
}
impl EdgeSteps {
#[must_use]
pub fn alone_step(
device: &Device,
(size, advance): (Length, Length),
measured: Option<i64>,
bias: Length,
) -> Length {
measured.map_or_else(
|| {
let drawn = device.snap(size);
let scale = (size > Length::ZERO && drawn != size).then_some((size, drawn));
device.snap(drawn_distance(scale, advance) + bias)
},
|dots| device.dots(dots),
)
}
pub fn with_advance_rounding(&mut self, rounding: Option<(i64, i64)>) {
self.rounding = rounding.map_or(Exact::ZERO, |(lost, per)| Exact::new(lost, per));
}
pub fn with_device_width(&mut self, measured: Option<(i64, Length)>) {
self.measured = measured;
}
pub const fn without_bias(&mut self, without: bool) {
self.without_bias = without;
}
pub const fn step_to_nearest_dot(&mut self) {
self.widened = Length::ZERO;
self.before_without_bias = true;
}
pub fn after_extra(&mut self, extra: GlyphExtra) {
self.extra = extra;
}
pub fn pending_extra(&self, device: &Device) -> Length {
extra_dots(device, self.extra)
}
pub const fn with_character_spacing(&mut self, spacing: Length) {
self.spacing = spacing;
}
pub fn with_counted_extra(&mut self, extra: Option<(i64, i64)>) {
self.counted_extra = extra.map_or(Exact::ZERO, |(lost, per)| Exact::new(lost, per));
}
pub fn with_counted_share(&mut self, excess: Option<(i128, i128)>) {
self.share = excess.map(|(lost, per)| {
if per > 0 {
Exact::reduced(lost, per)
} else {
Exact::ZERO
}
});
}
pub const fn with_short_ease(&mut self, ease: Length) {
self.short_ease = ease;
}
pub fn with_extra(&mut self, extra: GlyphExtra) {
self.stretched = extra;
}
pub const fn hands_on_start(&mut self, hands_on: bool) {
self.line_hands_on = hands_on;
}
#[must_use]
pub const fn started(&self) -> bool {
self.stepped == 0
}
#[must_use]
pub const fn when_starting(&self, by: Length) -> Length {
if self.previous.is_none() {
by
} else {
Length::ZERO
}
}
pub fn move_exact(&mut self, by: Length) {
if let Some((x, at)) = self.previous {
self.previous = Some((x + by, at));
}
self.anchor.0 += by;
if let Some(segment) = self.segment.as_mut() {
segment.first += by;
segment.counted += by;
}
if let Some(place) = self.start_place.as_mut() {
*place += by;
}
self.start -= by;
self.shift -= by;
}
pub const fn take_checked(&mut self) -> Option<EdgeCheck> {
self.checked.take()
}
pub const fn take_start_place(&mut self) -> Option<Length> {
self.start_place.take()
}
pub const fn at_nearest_exact(&mut self) {
self.next_place = NextPlace::NearestExact;
}
pub const fn restart(&mut self) {
self.previous = None;
}
pub const fn restart_at_step(&mut self) {
self.next_place = NextPlace::Restepped;
self.segment = None;
}
pub const fn at_note_mark(&mut self, device: &Device) {
match device.quirks.note_mark_place {
NoteMarkPlace::Stepped => {}
NoteMarkPlace::NearestExact => self.at_nearest_exact(),
}
}
pub const fn after_note_mark(&mut self, device: &Device) {
match device.quirks.note_start_steps {
NoteStartSteps::Continued => {}
NoteStartSteps::SteppedAfterMark => self.restart_at_step(),
}
}
pub const fn tab(&mut self) {
self.previous = None;
self.share_count = self.share_count.restarted();
self.segment = None;
}
pub const fn with_stop_nudge(&mut self, nudge: Length) {
self.nudge = nudge;
}
pub fn gap(&mut self, device: &Device) {
self.previous = None;
self.share_count = self.share_count.restarted();
self.nudge = Length::ZERO;
self.segment = rule(device).after_gap(self.segment);
}
pub fn at_size(&mut self, size: Length, drawn: Length) {
self.scale = (size > Length::ZERO && drawn != size).then_some((size, drawn));
}
#[must_use]
pub fn end(&self, device: &Device, x: Length) -> Option<Length> {
let first = self.segment?.first;
match device.glyph_edge_check() {
GlyphEdgeCheck::RoundedWithTie => {
Some(device.snap(tied_place(device, first, x) + self.shift))
}
GlyphEdgeCheck::Exact => {
let (anchor, at) = self.anchor;
let (counted, per) = self.drift.counted(x - anchor, device.exact_scale());
let place = i128::from(at.units()) * per + counted;
device.count_exact(place, per).map(|dots| device.dots(dots))
}
}
}
#[must_use]
pub fn stop_end(&self, device: &Device, x: Length) -> Length {
device.snap(x + self.nudge)
}
#[must_use]
pub fn tab_end(&self, device: &Device, written: Length) -> Option<Length> {
match device.quirks.aligned_tab_end {
AlignedTabEnd::StopDot => Some(self.stop_end(device, written)),
AlignedTabEnd::Unended => None,
}
}
fn next_segment(
&mut self,
device: &Device,
(x, advance): (Length, Length),
segment: Length,
) -> (Option<Restart>, Length, Exact) {
if self.segment.is_none() {
self.counted_excess = Exact::ZERO;
self.segment_rounding = Exact::ZERO;
self.segment_share = Exact::ZERO;
}
let current = *self.segment.get_or_insert(Segment {
first: x,
end: segment,
counted: x,
});
let own_excess = spacing_excess(device, std::mem::take(&mut self.spacing))
.plus(std::mem::take(&mut self.counted_extra));
let excess_before = self.counted_excess.rounded();
let excess_after = self.counted_excess.plus(own_excess).rounded();
let restarted = rule(device)
.next(
device,
current,
(x + excess_before, advance + excess_after - excess_before),
segment,
self.start,
)
.map(|mut restart| {
restart.segment.first -= excess_before;
if device.quirks.restart_count == crate::quirks::RestartCount::EdgeCheckPlaces {
let added = self.segment_rounding.plus(self.segment_share);
let (place, rounded_up) =
exact_restart_place(device, current, x + excess_before, added)
.unwrap_or((restart.place, false));
restart.place = place;
restart.rounded_up = rounded_up;
restart.segment.counted = place;
}
restart
});
let first = restarted.map_or(current.first, |restart| restart.segment.first);
if let Some(restart) = restarted {
self.segment = Some(restart.segment);
self.restart();
self.counted_excess = own_excess;
self.segment_rounding = Exact::ZERO;
self.segment_share = Exact::ZERO;
} else {
self.counted_excess = self.counted_excess.plus(own_excess);
}
(restarted, first, own_excess)
}
pub fn place(
&mut self,
device: &Device,
(bias, limit, segment): (Length, Length, Length),
x: Length,
advance: Length,
) -> Length {
let starts_line = self.segment.is_none() && self.line_hands_on;
let (restarted, first, own_excess) = self.next_segment(device, (x, advance), segment);
let scale = self.scale;
let at_drawn_size = move |distance: Length| drawn_distance(scale, distance);
let before_scale = std::mem::replace(&mut self.before_scale, scale);
let out_scale = match device.quirks.device_advance {
crate::quirks::DeviceAdvance::ExactAdvances => scale,
crate::quirks::DeviceAdvance::ExtrasOnTheirOwn => before_scale,
};
let step_with =
move |distance: Length, bias: Length| device.snap(at_drawn_size(distance) + bias);
let step = |distance: Length| step_with(distance, bias);
let without_bias = std::mem::take(&mut self.without_bias);
let before_without_bias = std::mem::replace(&mut self.before_without_bias, without_bias);
let unbiased = |without: bool| if without { Length::ZERO } else { bias };
let advance_rounding = std::mem::take(&mut self.rounding);
self.segment_rounding = self.segment_rounding.plus(advance_rounding);
let rounding = advance_rounding.plus(own_excess);
let share = self.counted_share();
self.segment_share = self.segment_share.plus(share);
let counted = rounding.plus(share);
let extra = std::mem::take(&mut self.extra);
let stretched = std::mem::take(&mut self.stretched);
let widens = std::mem::take(&mut self.measured).map_or(Length::ZERO, |(width, alone)| {
device.dots(width) - step(alone)
});
let widened = std::mem::replace(&mut self.widened, widens);
let nudge = std::mem::take(&mut self.nudge);
let next_place = std::mem::take(&mut self.next_place);
let short_ease = std::mem::take(&mut self.short_ease);
self.stepped = if self.previous.is_none() {
0
} else {
self.stepped.saturating_add(1)
};
let at = match self.previous {
None => {
let exact = restarted.map_or(x, |restart| restart.place);
let carried = restarted.and_then(|restart| restart.carried);
let nudge = if restarted.is_none() {
nudge
} else {
Length::ZERO
};
let start_place = exact + carried.unwrap_or(Length::ZERO) + nudge;
let at = if restarted.is_some_and(|restart| restart.rounded_up) {
device.snap(start_place - Length::from_units(1))
} else {
device.snap(start_place)
};
self.start_place = Some(start_place);
if carried.is_some() || starts_line {
self.start = at - exact;
}
self.shift = at - tied_place(device, first, x);
self.anchor = (x, at);
self.drift = counted;
self.checked = None;
at
}
Some((exact, drawn)) => {
let justified = extra_dots(device, extra);
let out = device.snap(
drawn_distance(out_scale, x - exact - extra.length())
+ unbiased(before_without_bias),
);
let at = drawn + out + justified + widened;
let own = step_with(advance - stretched.length(), unbiased(without_bias))
+ extra_dots(device, stretched);
if next_place == NextPlace::Restepped {
self.restepped(device, first, (x, at), counted)
} else {
let right = at + own + widens;
let near = next_place == NextPlace::NearestExact;
self.checked_place(
device,
(first, x, advance),
(at, right),
(limit, short_ease),
(counted, rounding, near),
)
}
}
};
self.previous = Some((x, at));
at
}
fn checked_place(
&mut self,
device: &Device,
(first, x, advance): (Length, Length, Length),
(at, right): (Length, Length),
(limit, short_ease): (Length, Length),
(counted, rounding, near): (Exact, Exact, bool),
) -> Length {
let nearest = near.then(|| self.end(device, x)).flatten();
let (moves, edge) = match device.glyph_edge_check() {
GlyphEdgeCheck::RoundedWithTie => {
self.tied_moves(device, first, (x, advance), (right, limit, short_ease))
}
GlyphEdgeCheck::Exact => {
self.exact_moves(device, (x, advance), counted, (right, limit, short_ease))
}
};
let moves = nearest.map_or(moves, |nearest| {
device.count(nearest - at, Rounding::HalfUp)
});
self.start_place = None;
self.checked = edge.map(|(edge, edge_thousandths)| EdgeCheck {
edge,
edge_thousandths,
rounding_thousandths: rounding.thousandths(),
moves,
});
at + device.dots(moves)
}
fn restepped(
&mut self,
device: &Device,
first: Length,
(x, at): (Length, Length),
counted: Exact,
) -> Length {
self.shift = at - tied_place(device, first, x);
self.anchor = (x, at);
self.drift = counted;
self.checked = None;
self.start_place = None;
self.stepped = 0;
at
}
fn counted_share(&mut self) -> Exact {
match (self.share.take(), self.share_count) {
(Some(excess), ShareCount::Before | ShareCount::Counting) => {
self.share_count = ShareCount::Counting;
excess
}
_ => Exact::ZERO,
}
}
fn tied_moves(
&self,
device: &Device,
first: Length,
(x, advance): (Length, Length),
(right, limit, ease): (Length, Length, Length),
) -> (i64, Option<(Length, i64)>) {
let (numerator, denominator) = device.exact_scale();
let exact = tied_place(device, first, x) + (advance * numerator).div_round(denominator);
let edge = right - (exact + self.shift);
let moves = moves(
edge > limit + params::GLYPH_EDGE_TIE,
edge <= ease - limit + params::GLYPH_EDGE_TIE,
);
(
moves,
Some((edge, edge.units().saturating_mul(THOUSANDTHS))),
)
}
fn exact_moves(
&mut self,
device: &Device,
(x, advance): (Length, Length),
rounding: Exact,
(right, limit, ease): (Length, Length, Length),
) -> (i64, Option<(Length, i64)>) {
let (anchor, from) = self.anchor;
let lost = self.drift.plus(rounding);
let (counted, per) = lost.counted(x - anchor + advance, device.exact_scale());
let edge = i128::from((right - from).units()) * per - counted;
self.drift = lost;
let near = i64::try_from(div_round_i128(edge, per))
.ok()
.map(Length::from_units);
let thousandths = i64::try_from(div_round_i128(edge * i128::from(THOUSANDTHS), per)).ok();
let short = i128::from((limit - ease).units()) * per;
let limit = i128::from(limit.units()) * per;
(moves(edge > limit, edge <= -short), near.zip(thousandths))
}
}
fn exact_restart_place(
device: &Device,
segment: Segment,
x: Length,
rounding: Exact,
) -> Option<(Length, bool)> {
let (counted, per) = rounding.counted(x - segment.first, device.exact_scale());
let distance = div_round_i128(counted, per);
let rounded_up = counted < distance * per;
i64::try_from(distance)
.ok()
.map(|distance| (segment.counted + Length::from_units(distance), rounded_up))
}
fn extra_dots(device: &Device, extra: GlyphExtra) -> Length {
match extra {
GlyphExtra::Justified(length) => share_dots(device, length),
GlyphExtra::SpacedShare { share, spacing } => match device.quirks.device_advance {
crate::quirks::DeviceAdvance::ExactAdvances => share_dots(device, share + spacing),
crate::quirks::DeviceAdvance::ExtrasOnTheirOwn => {
share_dots(device, share) + spacing_dots(device, spacing)
}
},
GlyphExtra::Spacing(length) => spacing_dots(device, length),
}
}
fn share_dots(device: &Device, length: Length) -> Length {
let (numerator, denominator) = device.exact_scale();
device.snap((length * numerator).div_round(denominator))
}
fn spacing_dots(device: &Device, length: Length) -> Length {
match device.quirks.device_advance {
crate::quirks::DeviceAdvance::ExactAdvances => device.snap(length),
crate::quirks::DeviceAdvance::ExtrasOnTheirOwn => device.snap_half_away(length),
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GlyphExtra {
Spacing(Length),
Justified(Length),
SpacedShare {
share: Length,
spacing: Length,
},
}
impl GlyphExtra {
pub const NONE: Self = Self::Spacing(Length::ZERO);
#[must_use]
pub fn of(stretch: Length, spacing: Length) -> Self {
if stretch == Length::ZERO {
Self::Spacing(spacing)
} else if spacing == Length::ZERO {
Self::Justified(stretch)
} else {
Self::SpacedShare {
share: stretch,
spacing,
}
}
}
#[must_use]
pub fn length(self) -> Length {
match self {
Self::Spacing(length) | Self::Justified(length) => length,
Self::SpacedShare { share, spacing } => share + spacing,
}
}
}
impl Default for GlyphExtra {
fn default() -> Self {
Self::NONE
}
}
fn spacing_excess(device: &Device, spacing: Length) -> Exact {
match device.quirks.character_spacing_count {
crate::quirks::CharacterSpacingCount::Exact => Exact::ZERO,
crate::quirks::CharacterSpacingCount::NearestUnit => {
let per_point = i128::from(Length::UNITS_PER_POINT);
let units = i128::from(params::CHARACTER_SPACING_UNITS_PER_POINT);
let scaled = i128::from(spacing.units()) * units;
let whole = (scaled.abs() * 2 + per_point).div_euclid(per_point * 2);
let whole = if scaled < 0 { -whole } else { whole };
Exact::reduced(whole * per_point - scaled, units)
}
}
}
#[must_use]
pub fn counted_spacing_excess(device: &Device, spacing: Length) -> (i64, i64) {
let excess = spacing_excess(device, spacing);
match (i64::try_from(excess.lost), i64::try_from(excess.per)) {
(Ok(lost), Ok(per)) => (lost, per),
_ => (0, 1),
}
}
fn tied_place(device: &Device, first: Length, x: Length) -> Length {
let (numerator, denominator) = device.exact_scale();
first + ((x - first) * numerator).div_round(denominator)
}
const fn moves(past: bool, short: bool) -> i64 {
if past {
-1
} else if short {
1
} else {
0
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Exact {
lost: i128,
per: i128,
}
impl Default for Exact {
fn default() -> Self {
Self::ZERO
}
}
impl Exact {
const ZERO: Self = Self { lost: 0, per: 1 };
fn new(lost: i64, per: i64) -> Self {
if per <= 0 {
return Self::ZERO;
}
Self::reduced(i128::from(lost), i128::from(per))
}
fn reduced(lost: i128, per: i128) -> Self {
let (lost, per) = lowest_terms(lost, per);
Self { lost, per }
}
fn thousandths(self) -> i64 {
i64::try_from(div_round_i128(
self.lost * i128::from(THOUSANDTHS),
self.per,
))
.unwrap_or(0)
}
fn rounded(self) -> Length {
Length::from_units(i64::try_from(div_round_i128(self.lost, self.per)).unwrap_or(0))
}
fn plus(self, other: Self) -> Self {
Self::reduced(
self.lost * other.per + other.lost * self.per,
self.per * other.per,
)
}
fn counted(self, length: Length, (numerator, denominator): (i64, i64)) -> (i128, i128) {
let numerator = i128::from(numerator) * (i128::from(length.units()) * self.per + self.lost);
(numerator, i128::from(denominator) * self.per)
}
}
#[cfg(test)]
pub(in crate::grid) mod tests {
use crate::grid::{Device, EdgeSteps, GlyphExtra};
use crate::quirks::{CharacterSpacingCount, DeviceAdvance, GlyphEdgeCheck};
use f_office_units::Length;
#[test]
fn ends_a_line_at_its_exact_end_counted_from_the_segments_first_dot() {
let device = Device::per_inch(600).unwrap();
let unit = device.dots(1).units();
let u = Length::from_units;
let mut edge = EdgeSteps::default();
assert_eq!(edge.end(&device, u(unit * 5)), None);
let start = unit * 10 + unit * 4 / 10;
let numbers = (u(254), u(9525), u(unit * 1024));
let at = edge.place(&device, numbers, u(start), u(unit * 20));
assert_eq!(at, u(unit * 10));
let end = edge.end(&device, u(start + unit * 52 + unit * 2 / 10));
assert_eq!(end, Some(u(unit * 62)));
}
pub(in crate::grid) const SEGMENT: i64 = 256 * Length::UNITS_PER_POINT;
pub(in crate::grid) const NUMBERS_AT_600_DPI: (Length, Length, Length) = (
Length::from_units(127),
Length::from_units(4_572),
Length::from_units(1_024 * 3_048),
);
pub(in crate::grid) const fn quarter_numbers(segment: i64) -> (Length, Length, Length) {
(
Length::from_units(254),
Length::from_units(9525),
Length::from_units(segment),
)
}
pub(in crate::grid) fn quarter_places(advance: i64, count: usize, segment: i64) -> Vec<i64> {
checked_quarter_places(
GlyphEdgeCheck::RoundedWithTie,
(advance, None),
count,
segment,
)
}
fn checked_quarter_places(
check: GlyphEdgeCheck,
(advance, rounding): (i64, Option<(i64, i64)>),
count: usize,
segment: i64,
) -> Vec<i64> {
let device = Device::per_inch_with(288, |p| p.with_variant(check));
let mut edge = EdgeSteps::default();
let start = 72 * Length::UNITS_PER_POINT;
let u = Length::from_units;
let places: Vec<i64> = (0..count)
.map(|k| {
let x = start + i64::try_from(k).unwrap() * advance;
edge.with_advance_rounding(rounding);
edge.place(&device, quarter_numbers(segment), u(x), u(advance))
.units()
})
.collect();
places
.iter()
.map(|p| (p - places[0]) * 100 / Length::UNITS_PER_POINT)
.collect()
}
#[test]
fn puts_a_told_glyph_on_the_dot_nearest_to_its_exact_place() {
let device = Device::per_inch_with(600, |p| p.with_variant(GlyphEdgeCheck::Exact));
let dot = device.dots(1).units();
let advance = dot * 484 / 10;
let u = Length::from_units;
let places = |told: Option<i64>| {
let mut edge = EdgeSteps::default();
(0..4)
.map(|k| {
if Some(k) == told {
edge.at_nearest_exact();
}
let at = edge.place(&device, NUMBERS_AT_600_DPI, u(k * advance), u(advance));
at.units() / dot
})
.collect::<Vec<_>>()
};
assert_eq!(places(None), [0, 48, 96, 145]);
assert_eq!(places(Some(2)), [0, 48, 97, 145]);
}
#[test]
fn counts_a_stretched_spaces_excess_until_a_tab_or_gap() {
let device = Device::per_inch_with(600, |p| p.with_variant(GlyphEdgeCheck::Exact));
let dot = device.dots(1).units();
let advance = dot * 484 / 10;
let u = Length::from_units;
let places = |shares: [Option<(i128, i128)>; 4], start: Option<(usize, bool)>| {
let mut edge = EdgeSteps::default();
shares
.iter()
.enumerate()
.map(|(k, share)| {
match start {
Some((at, false)) if at == k => edge.restart(),
Some((at, true)) if at == k => edge.tab(),
_ => {}
}
edge.with_counted_share(*share);
let x = i64::try_from(k).unwrap() * advance;
edge.place(&device, NUMBERS_AT_600_DPI, u(x), u(advance))
.units()
/ dot
})
.collect::<Vec<_>>()
};
let narrow = Some((-i128::from(dot) / 4, 1));
assert_eq!(places([None; 4], None), [0, 48, 96, 145]);
assert_eq!(places([None, narrow, None, None], None), [0, 48, 96, 144]);
let wide = Some((i128::from(dot) * 4 / 5 + 1, 1));
assert_eq!(
places([None, None, None, wide], Some((2, false))),
[0, 48, 97, 146]
);
assert_eq!(
places([None, narrow, None, wide], Some((2, false))),
[0, 48, 97, 146]
);
assert_eq!(
places([None, None, None, wide], Some((2, true))),
[0, 48, 97, 146]
);
assert_eq!(
places([None, narrow, None, wide], Some((2, true))),
[0, 48, 97, 145]
);
}
#[test]
fn steps_glyphs_as_word_for_the_web_does() {
let carried = [
0, 175, 350, 525, 700, 875, 1050, 1225, 1400, 1575, 1750, 1950, 2125, 2300, 2475, 2650,
2825, 3000, 3175, 3375, 3550, 3725, 3900, 4075,
];
assert_eq!(quarter_places(45_244, 24, SEGMENT), carried);
let calibri = [
0, 175, 350, 525, 725, 900, 1075, 1275, 1450, 1625, 1825, 2000, 2175,
];
assert_eq!(quarter_places(46_633, 13, SEGMENT), calibri);
}
#[test]
fn moves_a_glyph_whose_exact_edge_lies_exactly_on_the_limit() {
let places = [
0, 625, 1250, 1875, 2500, 3125, 3775, 4400, 5025, 5650, 6275, 6925, 7550, 8175, 8800,
9450, 10075,
];
assert_eq!(quarter_places(160_139, 17, SEGMENT), places);
let n = (160_139, Some((1, 16)));
let exact = GlyphEdgeCheck::Exact;
assert_eq!(checked_quarter_places(exact, n, 17, SEGMENT), places);
let tied = GlyphEdgeCheck::RoundedWithTie;
let mut edge = EdgeSteps::default();
let device = Device::per_inch_with(288, |p| p.with_variant(tied));
let numbers = quarter_numbers(SEGMENT);
let checks: Vec<_> = (0..17)
.map(|k| {
let u = Length::from_units;
edge.place(&device, numbers, u(k * 160_139), u(160_139));
edge.take_checked()
.map(|check| (check.edge.units(), check.moves))
})
.collect();
assert_eq!(checks[0], None);
assert_eq!(checks[15], Some((-numbers.1.units() + 1, 1)));
let rounded = checked_quarter_places(exact, (160_139, None), 17, SEGMENT);
assert_eq!(rounded[..15], places[..15]);
assert_eq!(rounded[15], 9425);
}
#[test]
fn keeps_a_glyph_whose_exact_edge_lies_just_inside_the_limit() {
let size = 12 * Length::UNITS_PER_POINT;
let units = [
909, 682, 512, 569, 1024, 512, 682, 1024, 1024, 909, 682, 1024, 1366, 682,
];
let device = Device::per_inch_with(600, |p| p.with_variant(GlyphEdgeCheck::Exact));
let mut edge = EdgeSteps::default();
let mut x = 0;
let numbers = NUMBERS_AT_600_DPI;
let places = units.map(|u| {
let advance = f_office_units::div_round(u * size, 2048);
edge.with_advance_rounding(Some((u * size - 2048 * advance, 2048)));
let at = edge.place(
&device,
numbers,
Length::from_units(x),
Length::from_units(advance),
);
x += advance;
at.units() / device.dots(1).units()
});
assert_eq!(
places,
[
0, 44, 77, 102, 130, 180, 205, 238, 288, 338, 382, 415, 465, 532
]
);
let checked = edge
.take_checked()
.map(|check| (check.edge.units(), check.moves));
assert_eq!(checked, Some((-4_435, 0)));
assert_eq!(edge.take_checked(), None);
}
#[test]
fn steps_a_glyph_by_its_measured_device_width() {
let device = Device::per_inch(288).unwrap();
let advance = Length::from_units(93_042);
let numbers = quarter_numbers(SEGMENT);
let dots = |measured: Option<(i64, Length)>| -> Vec<i64> {
let mut edge = EdgeSteps::default();
(0..6)
.map(|k| {
edge.with_device_width(measured);
edge.place(&device, numbers, advance * k, advance).units()
/ device.dots(1).units()
})
.collect()
};
assert_eq!(dots(None), [0, 15, 30, 45, 59, 74]);
assert_eq!(dots(Some((14, advance))), [0, 14, 29, 44, 58, 73]);
let mut edge = EdgeSteps::default();
edge.with_device_width(Some((14, advance)));
let first = edge.place(&device, numbers, Length::ZERO, advance);
let second = edge.place(&device, numbers, advance, advance);
let third = edge.place(&device, numbers, advance * 2, advance);
assert_eq!(
[first, second, third].map(|p| p.units() / device.dots(1).units()),
[0, 14, 29]
);
}
#[test]
fn steps_to_the_nearest_dot_without_the_widening_of_the_glyph_before() {
let device = Device::per_inch(288).unwrap();
let advance = Length::from_units(91_440);
let numbers = quarter_numbers(SEGMENT);
let second = |nearest: bool| -> i64 {
let mut edge = EdgeSteps::default();
edge.with_device_width(Some((15, advance)));
edge.place(&device, numbers, Length::ZERO, advance);
if nearest {
edge.step_to_nearest_dot();
edge.without_bias(true);
}
edge.place(&device, numbers, advance, advance).units() / device.dots(1).units()
};
assert_eq!(second(false), 15);
assert_eq!(second(true), 14);
}
#[test]
fn steps_a_glyph_without_the_bias_by_its_distance_at_the_nearest_dot() {
let device = Device::per_inch(288).unwrap();
let advance = Length::from_units(148_977);
let dots = |without: &[bool]| -> Vec<i64> {
let mut edge = EdgeSteps::default();
(0..)
.zip(without)
.map(|(k, &without)| {
edge.without_bias(without);
edge.place(&device, quarter_numbers(SEGMENT), advance * k, advance)
.units()
/ device.dots(1).units()
})
.collect()
};
assert_eq!(dots(&[false; 6]), [0, 24, 47, 71, 94, 118]);
assert_eq!(dots(&[true; 6]), [0, 23, 46, 70, 93, 117]);
assert_eq!(dots(&[true, false, false]), [0, 23, 47]);
assert_eq!(dots(&[false, true, true]), [0, 24, 47]);
}
#[test]
fn steps_a_glyph_by_its_distance_at_the_drawn_size() {
let device = Device::per_inch(600).unwrap();
let u = Length::from_units;
let numbers = NUMBERS_AT_600_DPI;
let dot = device.dots(1).units();
let x = Length::from_points(72).unwrap();
let advance = u(159_715);
let mut nominal = EdgeSteps::default();
nominal.place(&device, numbers, x, advance);
let at = nominal.place(&device, numbers, x + advance, advance);
assert_eq!(at - x, u(52 * dot));
let mut drawn = EdgeSteps::default();
drawn.at_size(Length::from_points(11).unwrap(), u(280_416));
drawn.place(&device, numbers, x, advance);
let at = drawn.place(&device, numbers, x + advance, advance);
assert_eq!(at - x, u(53 * dot));
}
#[test]
fn steps_out_of_a_glyph_at_its_own_drawn_size_where_the_profile_says_so() {
let u = Length::from_units;
let (x, advance) = (Length::from_points(72).unwrap(), u(159_715));
let step = |form: DeviceAdvance| {
let device = Device::per_inch_with(600, |p| p.with_variant(form));
let mut edge = EdgeSteps::default();
edge.at_size(Length::from_points(11).unwrap(), u(280_416));
edge.place(&device, NUMBERS_AT_600_DPI, x, advance);
let twelve = Length::from_points(12).unwrap();
edge.at_size(twelve, twelve);
let at = edge.place(&device, NUMBERS_AT_600_DPI, x + advance, advance);
(at - x).units() / device.dots(1).units()
};
assert_eq!(step(DeviceAdvance::ExactAdvances), 52);
assert_eq!(step(DeviceAdvance::ExtrasOnTheirOwn), 53);
}
#[test]
fn starts_a_line_of_glyph_steps_at_the_nearest_dot() {
let device = Device::per_inch(288).unwrap();
let u = Length::from_units;
let mut edge = EdgeSteps::default();
let numbers = quarter_numbers(SEGMENT);
let at = edge.place(&device, numbers, u(1_831_340), u(46_633));
assert_eq!(at, Length::from_points(72).unwrap());
edge.restart();
let at = edge.place(&device, numbers, u(1_833_880), u(46_633));
assert_eq!(at, u(1_835_150));
}
#[test]
fn adds_a_justified_spaces_extra_in_whole_dots() {
let device = Device::per_inch(288).unwrap();
let u = Length::from_units;
let numbers = quarter_numbers(SEGMENT);
let extra = u(15_034);
let place = |justified: bool| {
let mut edge = EdgeSteps::default();
edge.place(&device, numbers, u(2_863_912), u(120_104));
edge.place(&device, numbers, u(2_984_016), u(51_681) + extra);
if justified {
edge.after_extra(GlyphExtra::Justified(extra));
}
edge.place(&device, numbers, u(3_050_731), u(76_572))
};
assert_eq!(place(true), Length::from_points(120).unwrap());
assert_eq!(place(false), u(3_054_350));
}
#[test]
fn counts_a_character_spacing_as_the_nearest_whole_unit() {
let device =
|count: CharacterSpacingCount| Device::per_inch_with(600, |p| p.with_variant(count));
let (nearest, exact) = (
device(CharacterSpacingCount::NearestUnit),
device(CharacterSpacingCount::Exact),
);
let excess = |device: &Device, twips: i64| {
super::spacing_excess(device, Length::from_units(twips * 1_270))
};
assert_eq!(excess(&nearest, -3), super::Exact::reduced(10_160, 3_576));
assert_eq!(excess(&nearest, 1), super::Exact::reduced(5_080, 3_576));
assert_eq!(excess(&nearest, 5), super::Exact::ZERO);
assert_eq!(excess(&exact, -3), super::Exact::ZERO);
}
#[test]
fn keeps_a_glyph_whose_exact_edge_the_counted_spacing_widens() {
let line = |count: CharacterSpacingCount, width: i64, spacing: i64| -> Vec<Length> {
let device = Device::per_inch_with(600, |p| {
p.with_variant(GlyphEdgeCheck::Exact).with_variant(count)
});
let spacing = Length::from_units(spacing);
let advance = Length::from_units(width) + spacing;
let mut edge = EdgeSteps::default();
(0..3)
.map(|k| {
edge.with_extra(GlyphExtra::Spacing(spacing));
edge.with_character_spacing(spacing);
let at = edge.place(&device, NUMBERS_AT_600_DPI, advance * k, advance);
edge.after_extra(GlyphExtra::Spacing(spacing));
at
})
.collect()
};
let differ = |spacing: i64| {
(30_000..36_000)
.filter(|&width| {
line(CharacterSpacingCount::Exact, width, spacing)
!= line(CharacterSpacingCount::NearestUnit, width, spacing)
})
.count()
};
assert!(differ(-3_810) > 0);
assert_eq!(differ(6_350), 0);
}
#[test]
fn counts_a_synthetic_bold_extra_in_the_exact_edge_and_moves_it_right_one_dot_sooner() {
let device = Device::per_inch_with(600, |p| p.with_variant(GlyphEdgeCheck::Exact));
let size = 304_800;
let line = |width: i64, extra: bool, ease: bool| -> Vec<Length> {
let advance = Length::from_units(width);
let mut edge = EdgeSteps::default();
(0..3)
.map(|k| {
edge.with_counted_extra(extra.then_some((size, 2048)));
if ease {
edge.with_short_ease(device.dots(1));
}
edge.place(&device, NUMBERS_AT_600_DPI, advance * k, advance)
})
.collect()
};
let widths = 30_000..36_000;
assert!(
widths
.clone()
.any(|width| line(width, true, false) != line(width, false, false))
);
let second = |width: i64, ease: bool| line(width, false, ease).get(1).copied();
let eased: Vec<i64> = widths
.filter_map(|width| Some((second(width, true)? - second(width, false)?).units()))
.collect();
assert!(eased.contains(&device.dots(1).units()));
assert!(
eased
.iter()
.all(|&moved| moved == 0 || moved == device.dots(1).units())
);
}
#[test]
fn steps_a_glyphs_character_spacing_in_whole_dots_on_its_own() {
let device = Device::per_inch(288).unwrap();
let (numbers, dot) = (quarter_numbers(SEGMENT), device.dots(1));
let (width, spacing) = (Length::from_units(146_609), Length::from_units(-3_810));
let place = |extra: bool| {
let mut edge = EdgeSteps::default();
if extra {
edge.with_extra(GlyphExtra::Spacing(spacing));
}
edge.place(&device, numbers, Length::ZERO, width + spacing);
if extra {
edge.after_extra(GlyphExtra::Spacing(spacing));
}
edge.place(&device, numbers, width + spacing, width)
};
assert_eq!(place(true), dot * 22);
assert_eq!(place(false), dot * 23);
}
#[test]
fn checks_a_stretched_space_by_its_width_and_its_share_in_whole_dots() {
let device = Device::per_inch(288).unwrap();
let (numbers, dot) = (quarter_numbers(SEGMENT), device.dots(1));
let u = Length::from_units;
let place = |stretched: bool| {
let mut edge = EdgeSteps::default();
for k in 0..2 {
edge.place(&device, numbers, u(k * 66_040), u(66_040));
}
if stretched {
edge.with_extra(GlyphExtra::Justified(u(15_240)));
}
edge.place(&device, numbers, u(2 * 66_040), u(30_480))
};
assert_eq!(place(true), dot * 21);
assert_eq!(place(false), dot * 20);
}
#[test]
fn steps_an_extra_of_a_half_dot_away_from_zero() {
let device = |form: DeviceAdvance| Device::per_inch_with(288, |p| p.with_variant(form));
let numbers = quarter_numbers(SEGMENT);
let width = Length::from_units(63_500);
let place = |device: &Device, extra: GlyphExtra| {
let mut edge = EdgeSteps::default();
edge.with_extra(extra);
edge.place(device, numbers, Length::ZERO, width + extra.length());
edge.after_extra(extra);
edge.place(device, numbers, width + extra.length(), width)
};
let spacing = |units: i64| GlyphExtra::Spacing(Length::from_units(units));
let up = device(DeviceAdvance::ExactAdvances);
let away = device(DeviceAdvance::ExtrasOnTheirOwn);
let dot = up.dots(1);
assert_eq!(dot, Length::from_units(6_350));
assert_eq!(place(&up, spacing(-15_875)), dot * 8);
assert_eq!(place(&away, spacing(-15_875)), dot * 7);
assert_eq!(place(&up, spacing(15_875)), dot * 13);
assert_eq!(place(&away, spacing(15_875)), dot * 13);
let share = GlyphExtra::Justified(Length::from_units(-15_875));
assert_eq!(place(&up, share), dot * 8);
assert_eq!(place(&away, share), dot * 8);
}
#[test]
fn rounds_a_spaced_share_summed_or_apart() {
let device = |form: DeviceAdvance| Device::per_inch_with(288, |p| p.with_variant(form));
let numbers = quarter_numbers(SEGMENT);
let width = Length::from_units(63_500);
let extra = GlyphExtra::of(Length::from_units(33_909), Length::from_units(-5_271));
let place = |device: &Device| {
let mut edge = EdgeSteps::default();
edge.with_extra(extra);
edge.place(device, numbers, Length::ZERO, width + extra.length());
edge.after_extra(extra);
edge.place(device, numbers, width + extra.length(), width)
};
let summed = device(DeviceAdvance::ExactAdvances);
let dot = summed.dots(1);
assert_eq!(place(&summed), dot * 15);
assert_eq!(place(&device(DeviceAdvance::ExtrasOnTheirOwn)), dot * 14);
}
}