use f_office_paint::{FillRule, Item, PathCommand, PathItem, Point, Rect};
use f_office_units::Length;
use crate::grid::{Device, LineRounding, Rounding};
use crate::layout::measure::bar_edges;
use crate::layout::{Layout, decisions};
use crate::lines::Piece;
use crate::params;
use crate::props::{Strike, Underline};
use crate::text::{Face, FaceIndex};
impl Layout<'_> {
pub(super) fn draw_lines_of(&mut self, piece: Piece, x: Length, baseline: Length) {
match piece {
Piece::Rule { face, width } => self.rule(face, (x, width), baseline),
Piece::Bar { face, width } => self.fraction_bar(face, (x, width), baseline),
Piece::Vinculum {
face,
width,
top,
thickness,
..
} => self.vinculum(face, (x, width), (top, thickness), baseline),
Piece::Diagonal {
face,
width,
bottom,
top,
thickness,
rising,
} => self.diagonal(face, (x, width), (bottom, top, thickness, rising), baseline),
Piece::CheckBox { face, .. } => self.check_box(face, x, baseline),
_ => {}
}
}
fn fraction_bar(&mut self, face: FaceIndex, (x, width): (Length, Length), baseline: Length) {
let Some(face) = self.shaper.faces().get(face).copied() else {
return;
};
let (top, bottom) = bar_edges(&self.device, face.size);
let own = baseline - self.device.shift_dots(face.rise);
self.items.push(Item::Rect(Rect {
x,
y: own - top,
width,
height: self
.device
.line_thickness(top - bottom, LineRounding::Nearest),
color: face.color,
}));
}
fn vinculum(
&mut self,
face: FaceIndex,
(x, width): (Length, Length),
(top, thickness): (Length, Length),
baseline: Length,
) {
let Some(face) = self.shaper.faces().get(face).copied() else {
return;
};
let own = baseline - self.device.shift_dots(face.rise);
self.items.push(Item::Rect(Rect {
x,
y: own - top,
width,
height: self.device.line_thickness(thickness, LineRounding::Nearest),
color: face.color,
}));
}
fn diagonal(
&mut self,
face: FaceIndex,
(x, width): (Length, Length),
(bottom, top, thickness, rising): (Length, Length, Length, bool),
baseline: Length,
) {
let Some(face) = self.shaper.faces().get(face).copied() else {
return;
};
let own = baseline - self.device.shift_dots(face.rise);
let thick = self.device.line_thickness(thickness, LineRounding::Nearest);
let (y0, y1) = if rising {
(own - bottom, own - top)
} else {
(own - top, own - bottom)
};
let (x0, x1) = (x, x + width);
let dx = i128::from((x1 - x0).units());
let dy = i128::from((y1 - y0).units());
let len = i128::try_from((dx * dx + dy * dy).unsigned_abs().isqrt()).unwrap_or(0);
if len <= 0 {
return;
}
let half = i128::from(thick.div_toward_zero(2).units());
let scale = |v: i128| {
let num = v * half;
let rounded = if num >= 0 {
(num + len / 2) / len
} else {
(num - len / 2) / len
};
Length::from_units(i64::try_from(rounded).unwrap_or(0))
};
let (nx, ny) = (scale(-dy), scale(dx));
let pt = |px, py| Point { x: px, y: py };
self.items.push(Item::Path(PathItem {
commands: vec![
PathCommand::MoveTo(pt(x0 + nx, y0 + ny)),
PathCommand::LineTo(pt(x1 + nx, y1 + ny)),
PathCommand::LineTo(pt(x1 - nx, y1 - ny)),
PathCommand::LineTo(pt(x0 - nx, y0 - ny)),
PathCommand::Close,
],
color: face.color,
fill_rule: FillRule::NonZero,
}));
}
fn rule(&mut self, face: FaceIndex, (x, width): (Length, Length), baseline: Length) {
let Some(face) = self.shaper.faces().get(face).copied() else {
return;
};
let top = baseline - face.scale(i64::from(face.metrics.strikeout_position));
let thickness = face.scale(i64::from(face.metrics.strikeout_thickness));
self.items.push(Item::Rect(Rect {
x,
y: self.device.snap(top),
width,
height: self.device.line_thickness(thickness, LineRounding::Nearest),
color: face.color,
}));
}
fn check_box(&mut self, face: FaceIndex, x: Length, baseline: Length) {
let Some(face) = self.shaper.faces().get(face).copied() else {
return;
};
let stroke = self.device.snap(params::CHECK_BOX_STROKE);
let cell = self.device.snap(face.line_height());
let three_strokes = cell - stroke * 2 - stroke;
let side =
decisions::check_box_side(&self.context.quirks, three_strokes, self.device.dots(1));
let margin = self.device.floor((cell - side).div_toward_zero(2));
let left = self.device.snap(x) + margin;
let top = self.device.snap(baseline - face.ascent()) + margin;
let half = stroke.div_toward_zero(2);
let bars = [
(left - half, top - half, side + stroke, stroke),
(left - half, top + side - half, side + stroke, stroke),
(left - half, top + half, stroke, side - stroke),
(left + side - half, top + half, stroke, side - stroke),
];
for (x, y, width, height) in bars {
self.items.push(Item::Rect(Rect {
x,
y,
width,
height,
color: face.color,
}));
}
}
pub(super) fn tab_marks(
&mut self,
index: FaceIndex,
(x, width): (Length, Length),
baseline: Length,
) {
let Some(face) = self.shaper.faces().get(index).copied() else {
return;
};
if width <= Length::ZERO {
return;
}
if let Some(color) = face.background {
self.items.push(Item::Rect(Rect {
x,
y: baseline - face.ascent(),
width,
height: face.ascent() + face.descent(),
color,
}));
}
let single = self.single_underline(index, &face);
let rects = run_lines(&self.device, &face, single, ((x, x + width), &[]), baseline);
self.items.extend(rects.into_iter().map(Item::Rect));
}
}
impl Layout<'_> {
pub(super) fn single_underline(&self, index: FaceIndex, face: &Face) -> (Length, Length) {
let metrics = face.metrics;
decisions::single_underline(
&self.context.quirks,
&self.device,
(face.size, metrics.east_asian),
|| {
font_line(
&self.device,
face,
(metrics.underline_position, metrics.underline_thickness),
)
},
|pels| {
self.shaper
.whole_pixel_heights(index, pels)
.map(|(_, descent)| descent)
},
)
}
}
pub(super) fn run_lines(
device: &Device,
face: &Face,
single: (Length, Length),
(span, words): ((Length, Length), &[(Length, Length)]),
baseline: Length,
) -> Vec<Rect> {
let metrics = face.metrics;
let single = span_line(device, face, (span, baseline), single);
let mut rects = Vec::new();
match face.lines.under {
Underline::None => {}
Underline::Single | Underline::Wave | Underline::WavyHeavy | Underline::WavyDouble => {
rects.push(single);
}
Underline::Words => rects.extend(words.iter().map(|&(start, end)| {
let (start, end) = (device.snap(start), device.snap(end));
Rect {
x: start,
width: end - start,
..single
}
})),
Underline::Double => {
let (upper, lower) = double_lines(device, &single, baseline);
rects.extend([upper, lower]);
}
Underline::Thick => rects.push(thick_line(device, &single, baseline)),
kind => {
let (pattern, heavy) = dashes(kind);
let band = if heavy {
thick_line(device, &single, baseline)
} else {
single
};
rects.extend(dashed(device, &band, span, pattern));
}
}
if face.lines.through.is_on() {
let place = font_line(
device,
face,
(metrics.strikeout_position, metrics.strikeout_thickness),
);
let line = span_line(device, face, (span, baseline), place);
match face.lines.through {
Strike::Double => rects.extend([
Rect {
y: line.y - line.height,
..line
},
Rect {
y: line.y + line.height,
..line
},
]),
Strike::Single | Strike::None => rects.push(line),
}
}
rects
}
fn double_lines(device: &Device, single: &Rect, baseline: Length) -> (Rect, Rect) {
let in_dots = |length: Length| device.count(length, Rounding::TowardZero);
let below = in_dots(single.y - baseline);
let thick = in_dots(single.height);
let each = thick - thick.div_euclid(2);
let upper = below - below.div_euclid(2);
let gap = in_dots(device.snap(params::UNDERLINE_DOUBLE_GAP));
let bottom = (below + thick).max(upper + 2 * each + gap);
let at = |dots: i64| baseline + device.dots(dots);
(
Rect {
y: at(upper),
height: device.dots(each),
..*single
},
Rect {
y: at(bottom - each),
height: device.dots(each),
..*single
},
)
}
fn thick_line(device: &Device, single: &Rect, baseline: Length) -> Rect {
let (upper, lower) = double_lines(device, single, baseline);
Rect {
height: lower.y - upper.y,
..upper
}
}
const fn dashes(kind: Underline) -> (&'static [Length], bool) {
match kind {
Underline::DottedHeavy => (¶ms::UNDERLINE_DOTTED, true),
Underline::Dash => (¶ms::UNDERLINE_DASH, false),
Underline::DashedHeavy => (¶ms::UNDERLINE_DASH, true),
Underline::DashLong => (¶ms::UNDERLINE_DASH_LONG, false),
Underline::DashLongHeavy => (¶ms::UNDERLINE_DASH_LONG, true),
Underline::DotDash => (¶ms::UNDERLINE_DOT_DASH, false),
Underline::DashDotHeavy => (¶ms::UNDERLINE_DOT_DASH, true),
Underline::DotDotDash => (¶ms::UNDERLINE_DOT_DOT_DASH, false),
Underline::DashDotDotHeavy => (¶ms::UNDERLINE_DOT_DOT_DASH, true),
_ => (¶ms::UNDERLINE_DOTTED, false),
}
}
fn dashed(device: &Device, band: &Rect, span: (Length, Length), pattern: &[Length]) -> Vec<Rect> {
let mut rects = Vec::new();
let end = device.snap(span.1);
let mut at = device.snap(span.0);
if pattern.iter().all(|&length| length <= Length::ZERO) {
return rects;
}
for (index, &length) in pattern.iter().enumerate().cycle() {
if at >= end {
break;
}
let next = device.snap(at + length).min(end);
if index % 2 == 0 && next > at {
rects.push(Rect {
x: at,
width: next - at,
..*band
});
}
at = next.max(at + device.dots(1));
}
rects
}
fn font_line(device: &Device, face: &Face, (position, thickness): (i16, i16)) -> (Length, Length) {
(
device.snap(face.scale(i64::from(position))),
device.line_thickness(face.scale(i64::from(thickness)), LineRounding::Nearest),
)
}
fn span_line(
device: &Device,
face: &Face,
((start, end), baseline): ((Length, Length), Length),
(above, thickness): (Length, Length),
) -> Rect {
let top = baseline - above;
let (start, end) = (device.snap(start), device.snap(end));
Rect {
x: start,
y: top,
width: end - start,
height: thickness,
color: face.color,
}
}
#[cfg(test)]
mod tests {
use f_office_paint::{Rect, Rgb};
use f_office_units::Length;
use super::{dashed, double_lines, thick_line};
use crate::grid::Device;
use crate::params;
use crate::props::{Strike, Underline};
use crate::quirks::UnderlineForms;
use crate::text::RunLines;
fn device() -> Device {
Device::per_inch(600).unwrap()
}
fn single(below: i64, thick: i64) -> Rect {
let dot = device().dots(1);
Rect {
x: Length::ZERO,
y: dot * below,
width: Length::from_points(72).unwrap(),
height: dot * thick,
color: Rgb::BLACK,
}
}
fn band(rect: &Rect) -> (i64, i64) {
let dot = device().dots(1).units();
(rect.y.units() / dot, rect.height.units() / dot)
}
#[test]
fn puts_a_double_underline_in_a_box_from_half_the_single_lines_place() {
let (upper, lower) = double_lines(&device(), &single(10, 6), Length::ZERO);
assert_eq!((band(&upper), band(&lower)), ((5, 3), (16, 3)));
let (upper, lower) = double_lines(&device(), &single(22, 10), Length::ZERO);
assert_eq!((band(&upper), band(&lower)), ((11, 5), (27, 5)));
assert_eq!(
band(&thick_line(&device(), &single(10, 6), Length::ZERO)),
(5, 11)
);
assert_eq!(
band(&thick_line(&device(), &single(22, 10), Length::ZERO)),
(11, 16)
);
assert_eq!(
device().snap(params::UNDERLINE_DOUBLE_GAP),
device().dots(1) * 8
);
}
#[test]
fn draws_a_row_of_dots_from_the_runs_start_and_cuts_the_last_at_its_end() {
let ten = Length::from_points(10).unwrap();
let dots = dashed(
&device(),
&single(10, 6),
(Length::ZERO, ten),
¶ms::UNDERLINE_DOTTED,
);
let dot = device().dots(1).units();
let pieces: Vec<(i64, i64)> = dots
.iter()
.map(|rect| (rect.x.units() / dot, (rect.x + rect.width).units() / dot))
.collect();
assert_eq!(pieces, [(0, 12), (24, 36), (48, 60), (72, 83)]);
}
#[test]
fn draws_every_kind_as_a_single_line_unless_the_target_draws_each_form() {
let drawn = |lines, forms| {
crate::text::ShaperSettings::base_with(|p| p.with_variant(forms))
.run_lines(lines, false)
};
let lines = |kind, forms| drawn((Some(kind), Some(Strike::Double)), forms);
let all_single = lines(Underline::DotDash, UnderlineForms::AllSingle);
assert_eq!(
(all_single.under, all_single.through),
(Underline::Single, Strike::Single)
);
let by_kind = lines(Underline::DotDash, UnderlineForms::ByKind);
assert_eq!(
(by_kind.under, by_kind.through),
(Underline::DotDash, Strike::Double)
);
assert_eq!(
lines(Underline::WavyDouble, UnderlineForms::ByKind).under,
Underline::Single
);
assert_eq!(
drawn((None, Some(Strike::None)), UnderlineForms::ByKind),
RunLines::default()
);
}
}