From cef46e6c40fed0089a20e44ff2f251c06878891c Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sat, 20 Nov 2021 15:51:07 +0100 Subject: Strokes --- src/library/deco.rs | 27 ++++++++---------- src/library/page.rs | 8 +++--- src/library/shape.rs | 76 +++++++++++++++++++++++++++++--------------------- src/library/text.rs | 32 +++++++-------------- src/library/utility.rs | 4 +-- 5 files changed, 71 insertions(+), 76 deletions(-) (limited to 'src/library') diff --git a/src/library/deco.rs b/src/library/deco.rs index 2722fd68..1f8c051f 100644 --- a/src/library/deco.rs +++ b/src/library/deco.rs @@ -17,20 +17,13 @@ pub fn overline(_: &mut EvalContext, args: &mut Args) -> TypResult { } fn line_impl(args: &mut Args, kind: LineKind) -> TypResult { - let stroke = args.named("stroke")?.or_else(|| args.find()); + let stroke = args.named("stroke")?.or_else(|| args.find()).map(Paint::Solid); let thickness = args.named::("thickness")?.or_else(|| args.find()); let offset = args.named("offset")?; let extent = args.named("extent")?.unwrap_or_default(); let body: Template = args.expect("body")?; - Ok(Value::Template(body.decorate(Decoration::Line( - LineDecoration { - kind, - stroke: stroke.map(Paint::Color), - thickness, - offset, - extent, - }, + LineDecoration { kind, stroke, thickness, offset, extent }, )))) } @@ -112,12 +105,15 @@ impl LineDecoration { LineKind::Overline => face.overline, }; - let stroke = self.stroke.unwrap_or(text.fill); - let thickness = self .thickness .map(|s| s.resolve(text.size)) - .unwrap_or(metrics.strength.to_length(text.size)); + .unwrap_or(metrics.thickness.to_length(text.size)); + + let stroke = Stroke { + paint: self.stroke.unwrap_or(text.fill), + thickness, + }; let offset = self .offset @@ -127,10 +123,9 @@ impl LineDecoration { let extent = self.extent.resolve(text.size); let subpos = Point::new(pos.x - extent, pos.y + offset); - let vector = Point::new(text.width + 2.0 * extent, Length::zero()); - let line = Geometry::Line(vector, thickness); - - frame.push(subpos, Element::Geometry(line, stroke)); + let target = Point::new(text.width + 2.0 * extent, Length::zero()); + let shape = Shape::stroked(Geometry::Line(target), stroke); + frame.push(subpos, Element::Shape(shape)); } } } diff --git a/src/library/page.rs b/src/library/page.rs index b760e76a..20871bd9 100644 --- a/src/library/page.rs +++ b/src/library/page.rs @@ -18,7 +18,7 @@ pub fn page(ctx: &mut EvalContext, args: &mut Args) -> TypResult { let right = args.named("right")?; let bottom = args.named("bottom")?; let flip = args.named("flip")?; - let fill = args.named("fill")?; + let fill = args.named("fill")?.map(Paint::Solid); ctx.template.modify(move |style| { let page = style.page_mut(); @@ -63,7 +63,7 @@ pub fn page(ctx: &mut EvalContext, args: &mut Args) -> TypResult { } if let Some(fill) = fill { - page.fill = Some(Paint::Color(fill)); + page.fill = Some(fill); } }); @@ -105,8 +105,8 @@ impl PageNode { // Add background fill if requested. if let Some(fill) = self.fill { for frame in &mut frames { - let element = Element::Geometry(Geometry::Rect(frame.size), fill); - Rc::make_mut(frame).prepend(Point::zero(), element); + let shape = Shape::filled(Geometry::Rect(frame.size), fill); + Rc::make_mut(frame).prepend(Point::zero(), Element::Shape(shape)); } } diff --git a/src/library/shape.rs b/src/library/shape.rs index d0df5f48..abf927e4 100644 --- a/src/library/shape.rs +++ b/src/library/shape.rs @@ -7,9 +7,7 @@ use crate::util::RcExt; pub fn rect(_: &mut EvalContext, args: &mut Args) -> TypResult { let width = args.named("width")?; let height = args.named("height")?; - let fill = args.named("fill")?; - let body = args.find(); - Ok(shape_impl(ShapeKind::Rect, width, height, fill, body)) + shape_impl(args, ShapeKind::Rect, width, height) } /// `square`: A square with optional content. @@ -23,18 +21,14 @@ pub fn square(_: &mut EvalContext, args: &mut Args) -> TypResult { None => args.named("height")?, size => size, }; - let fill = args.named("fill")?; - let body = args.find(); - Ok(shape_impl(ShapeKind::Square, width, height, fill, body)) + shape_impl(args, ShapeKind::Square, width, height) } /// `ellipse`: An ellipse with optional content. pub fn ellipse(_: &mut EvalContext, args: &mut Args) -> TypResult { let width = args.named("width")?; let height = args.named("height")?; - let fill = args.named("fill")?; - let body = args.find(); - Ok(shape_impl(ShapeKind::Ellipse, width, height, fill, body)) + shape_impl(args, ShapeKind::Ellipse, width, height) } /// `circle`: A circle with optional content. @@ -48,30 +42,44 @@ pub fn circle(_: &mut EvalContext, args: &mut Args) -> TypResult { None => args.named("height")?, diameter => diameter, }; - let fill = args.named("fill")?; - let body = args.find(); - Ok(shape_impl(ShapeKind::Circle, width, height, fill, body)) + shape_impl(args, ShapeKind::Circle, width, height) } fn shape_impl( + args: &mut Args, kind: ShapeKind, width: Option, height: Option, - fill: Option, - body: Option