use canvist_layout::DisplayItem;
use canvist_layout::DisplayList;
use canvist_layout::TextRun;
use kurbo::Affine;
use kurbo::BezPath;
use kurbo::Rect;
use kurbo::Shape as _;
use peniko::Brush;
use peniko::Color;
use peniko::Fill;
use crate::convert;
use crate::painter::Draw;
use crate::painter::Painter;
use crate::painter::Text;
pub const GREEK_PIXELS: f64 = 4.0;
const GREEK_ALPHA: f32 = 0.35;
const GREEK_HEIGHT: f64 = 0.3;
const GREEK_RISE: f64 = 1.5;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DisplayStyle {
pub transform: Affine,
pub fill: Option<Color>,
}
impl DisplayStyle {
#[must_use]
pub const fn new(transform: Affine) -> Self {
Self {
transform,
fill: None,
}
}
fn scale(&self) -> f64 {
let [a, b, ..] = self.transform.as_coeffs();
a.hypot(b)
}
}
pub fn paint_display_list<P: Painter + ?Sized>(
painter: &mut P,
list: &DisplayList,
style: &DisplayStyle,
) {
for item in &list.items {
paint_item(painter, list, item, style);
}
}
fn paint_item<P: Painter + ?Sized>(
painter: &mut P,
list: &DisplayList,
item: &DisplayItem,
style: &DisplayStyle,
) {
match item {
DisplayItem::Text(run) => paint_run(painter, list, run, style),
DisplayItem::Rect {
rect,
color,
radius,
..
} => {
let shape = kurbo::RoundedRect::from_rect(layout_rect(*rect), f64::from(*radius))
.to_path(tolerance());
fill(painter, &shape, convert::layout_color(*color), style);
}
DisplayItem::Decoration {
x,
y,
width,
thickness,
color,
..
} => {
let half = f64::from(*thickness) / 2.0;
let line = Rect::new(
f64::from(*x),
f64::from(*y) - half,
f64::from(*x + *width),
f64::from(*y) + half,
);
let color = style.fill.unwrap_or_else(|| convert::layout_color(*color));
fill(painter, &line.to_path(tolerance()), color, style);
}
DisplayItem::Stroke {
points,
width,
color,
..
} => {
paint_stroke(
painter,
list.stroke_points(points),
*width,
convert::layout_color(*color),
style,
);
}
DisplayItem::Slot { .. } => {}
}
}
fn paint_stroke<P: Painter + ?Sized>(
painter: &mut P,
points: &[f32],
width: f32,
color: Color,
style: &DisplayStyle,
) {
let mut path = BezPath::new();
for (index, point) in points.chunks_exact(2).enumerate() {
let point = kurbo::Point::new(f64::from(point[0]), f64::from(point[1]));
if index == 0 {
path.move_to(point);
}
path.line_to(point);
}
let pen = kurbo::Stroke::new(f64::from(width))
.with_caps(kurbo::Cap::Round)
.with_join(kurbo::Join::Round);
let brush = Brush::Solid(color);
painter.stroke(
&Draw {
path: &path,
transform: style.transform,
brush: &brush,
brush_transform: None,
},
&pen,
);
}
fn paint_run<P: Painter + ?Sized>(
painter: &mut P,
list: &DisplayList,
run: &TextRun,
style: &DisplayStyle,
) {
let Some(font) = list.fonts.get(run.font.index()) else {
return;
};
let color = style
.fill
.unwrap_or_else(|| convert::layout_color(run.color));
if f64::from(font.size) * style.scale() >= GREEK_PIXELS {
painter.text(&Text {
run,
font,
transform: style.transform,
color,
});
return;
}
let height = f64::from(font.size) * GREEK_HEIGHT;
let top = f64::from(run.baseline) - height * GREEK_RISE;
let bar = Rect::new(
f64::from(run.x),
top,
f64::from(run.x + run.width),
top + height,
);
fill(
painter,
&bar.to_path(tolerance()),
color.multiply_alpha(GREEK_ALPHA),
style,
);
}
fn fill<P: Painter + ?Sized>(painter: &mut P, path: &BezPath, color: Color, style: &DisplayStyle) {
let brush = Brush::Solid(color);
painter.fill(
&Draw {
path,
transform: style.transform,
brush: &brush,
brush_transform: None,
},
Fill::NonZero,
);
}
fn layout_rect(rect: canvist_layout::Rect) -> Rect {
Rect::new(
f64::from(rect.x),
f64::from(rect.y),
f64::from(rect.x + rect.width),
f64::from(rect.y + rect.height),
)
}
fn tolerance() -> f64 {
canvist_canvas::geometry::TOLERANCE
}