use crate::geometry::{Color, Point, Rect, Size, Vec2};
use crate::scene::{Element, Fill, FillStrokeStyle, Stroke};
use crate::text::{TextStyle, measure_text};
use kurbo::BezPath;
#[derive(Debug, Clone)]
pub struct Ctx {
pub fill: Option<Color>,
pub stroke: Option<Stroke>,
pub font: TextStyle,
}
impl Default for Ctx {
fn default() -> Self {
Self {
fill: Some(Color::BLACK),
stroke: None,
font: TextStyle::new(Color::BLACK, 12.0, "sans-serif"),
}
}
}
impl Ctx {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_fill(mut self, color: Color) -> Self {
self.fill = Some(color);
self
}
#[must_use]
pub fn with_stroke(mut self, stroke: Stroke) -> Self {
self.stroke = Some(stroke);
self
}
#[must_use]
pub fn with_font(mut self, font: TextStyle) -> Self {
self.font = font;
self
}
#[must_use]
pub fn with_text_color(mut self, color: Color) -> Self {
self.font.color = color;
self
}
fn text_color(&self) -> Color {
self.font.color
}
fn shape_fill(&self) -> Color {
self.fill.unwrap_or(self.font.color)
}
fn fill_stroke(&self) -> FillStrokeStyle {
FillStrokeStyle {
fill: self.fill.map(Fill::Solid),
stroke: self.stroke.clone(),
}
}
#[must_use]
pub fn fill_text(&self, x: f64, y: f64, text: impl Into<String>) -> Element {
let mut style = self.font.clone();
style.color = self.text_color();
Element::text(text, Point::new(x, y), style)
}
#[must_use]
pub fn measure_text(&self, text: impl AsRef<str>) -> Size {
let style = self.font.clone();
measure_text(
std::slice::from_ref(&crate::text::RichSpan::new(text.as_ref(), style)),
None,
)
.size
}
#[must_use]
pub fn fill_rect(&self, x: f64, y: f64, w: f64, h: f64) -> Element {
Element::rect(
Rect::new(x, y, x + w, y + h),
FillStrokeStyle::fill(self.shape_fill()),
)
}
#[must_use]
pub fn stroke_rect(&self, x: f64, y: f64, w: f64, h: f64) -> Element {
Element::rect(
Rect::new(x, y, x + w, y + h),
FillStrokeStyle::stroke(
self.stroke
.as_ref()
.map(|s| s.color)
.unwrap_or(self.shape_fill()),
self.stroke.as_ref().map(|s| s.width).unwrap_or(1.0),
),
)
}
#[must_use]
pub fn fill_circle(&self, x: f64, y: f64, r: f64) -> Element {
Element::circle(
Point::new(x, y),
r,
FillStrokeStyle::fill(self.shape_fill()),
)
}
#[must_use]
pub fn fill_ellipse(&self, x: f64, y: f64, rx: f64, ry: f64, rotation: f64) -> Element {
Element::ellipse(
Point::new(x, y),
Vec2::new(rx, ry),
rotation,
FillStrokeStyle::fill(self.shape_fill()),
)
}
#[must_use]
pub fn line(&self, x0: f64, y0: f64, x1: f64, y1: f64) -> Element {
Element::line(
Point::new(x0, y0),
Point::new(x1, y1),
self.stroke
.clone()
.unwrap_or(Stroke::new(self.shape_fill(), 1.0)),
)
}
#[must_use]
pub fn polyline(&self, pts: &[(f64, f64)]) -> Element {
Element::poly(
pts.iter().map(|&(x, y)| Point::new(x, y)).collect(),
self.stroke
.clone()
.unwrap_or(Stroke::new(self.shape_fill(), 1.0)),
)
}
#[must_use]
pub fn polygon(&self, pts: &[(f64, f64)]) -> Element {
Element::polygon(
pts.iter().map(|&(x, y)| Point::new(x, y)).collect(),
self.fill_stroke(),
)
}
#[must_use]
pub fn path(&self, path: BezPath, closed: bool) -> Element {
Element::Path {
path,
style: self.fill_stroke(),
closed,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn default_ctx() -> Ctx {
Ctx::new().with_fill(Color::rgb(0x33, 0x66, 0x99))
}
#[test]
fn fill_text_anchors_and_color() {
let ctx =
default_ctx().with_font(TextStyle::new(Color::rgb(0xff, 0, 0), 24.0, "sans-serif"));
let el = ctx.fill_text(50.0, 50.0, "Hi");
match el {
Element::Text {
spans,
position,
style,
layout,
} => {
assert_eq!(spans.len(), 1);
assert_eq!(spans[0].text, "Hi");
assert_eq!(spans[0].style.color, Color::rgb(0xff, 0, 0));
assert_eq!(position, Point::new(50.0, 50.0));
assert_eq!(style.font_size, 24.0);
assert!(layout.is_none());
}
_ => panic!("expected Text"),
}
}
#[test]
fn text_color_independent_of_shape_fill() {
let ctx = default_ctx().with_text_color(Color::rgb(0xff, 0, 0));
match ctx.fill_text(0.0, 0.0, "x") {
Element::Text { spans, .. } => assert_eq!(spans[0].style.color, Color::rgb(0xff, 0, 0)),
_ => panic!("expected Text"),
}
match ctx.fill_rect(0.0, 0.0, 1.0, 1.0) {
Element::Rect { style, .. } => {
assert_eq!(style.fill, Some(Fill::Solid(Color::rgb(0x33, 0x66, 0x99))));
}
_ => panic!("expected Rect"),
}
}
#[test]
fn rect_builders() {
let ctx = default_ctx();
match ctx.fill_rect(10.0, 20.0, 30.0, 40.0) {
Element::Rect { rect, style } => {
assert_eq!(rect, Rect::new(10.0, 20.0, 40.0, 60.0));
assert!(style.fill.is_some() && style.stroke.is_none());
}
_ => panic!("expected Rect"),
}
let ctx = Ctx::new().with_stroke(Stroke::new(Color::BLACK, 2.0));
match ctx.stroke_rect(0.0, 0.0, 5.0, 5.0) {
Element::Rect { rect, style } => {
assert_eq!(rect, Rect::new(0.0, 0.0, 5.0, 5.0));
assert_eq!(style.stroke.as_ref().unwrap().width, 2.0);
}
_ => panic!("expected Rect"),
}
}
#[test]
fn shape_builders() {
let ctx = Ctx::new()
.with_fill(Color::BLACK)
.with_stroke(Stroke::new(Color::BLACK, 1.0));
assert!(matches!(ctx.line(0.0, 0.0, 1.0, 1.0), Element::Line { .. }));
assert!(matches!(
ctx.polyline(&[(0.0, 0.0), (1.0, 1.0), (2.0, 0.0)]),
Element::Polyline { .. }
));
assert!(matches!(
ctx.polygon(&[(0.0, 0.0), (1.0, 1.0), (0.0, 1.0)]),
Element::Polygon { .. }
));
assert!(matches!(
ctx.fill_circle(0.0, 0.0, 5.0),
Element::Circle { .. }
));
assert!(matches!(
ctx.fill_ellipse(0.0, 0.0, 4.0, 2.0, 0.0),
Element::Ellipse { .. }
));
}
#[test]
fn path_builder() {
let ctx = Ctx::new()
.with_fill(Color::BLACK)
.with_stroke(Stroke::new(Color::BLACK, 1.0));
let mut p = BezPath::new();
p.move_to((0.0, 0.0));
p.line_to((1.0, 0.0));
p.line_to((1.0, 1.0));
p.close_path();
match ctx.path(p, true) {
Element::Path {
style,
closed,
path,
} => {
assert!(closed);
assert!(style.fill.is_some() && style.stroke.is_some());
assert!(!path.is_empty());
}
_ => panic!("expected Path"),
}
}
#[test]
fn measure_returns_size() {
let ctx = default_ctx().with_font(TextStyle::new(Color::BLACK, 20.0, "sans-serif"));
let size = ctx.measure_text("Hello");
assert!(size.width > 0.0 && size.height > 0.0);
}
#[test]
fn elements_are_pushable() {
use crate::scene::Scene;
let ctx = default_ctx();
let mut scene = Scene::new(100.0, 100.0);
scene.push(ctx.fill_rect(0.0, 0.0, 10.0, 10.0));
scene.push(ctx.fill_text(5.0, 5.0, "ok"));
assert_eq!(scene.nodes.len(), 2);
}
}