Add Stroke::from_pair

This commit is contained in:
Laurenz 2024-01-12 10:58:34 +01:00
parent 0340497e00
commit bc2a4f802c
6 changed files with 48 additions and 44 deletions

View File

@ -6,7 +6,7 @@ use ecow::eco_format;
use crate::diag::{bail, At, SourceResult, StrResult}; use crate::diag::{bail, At, SourceResult, StrResult};
use crate::eval::{access_dict, Access, Eval, Vm}; use crate::eval::{access_dict, Access, Eval, Vm};
use crate::foundations::{format_str, Datetime, IntoValue, Regex, Repr, Smart, Value}; use crate::foundations::{format_str, Datetime, IntoValue, Regex, Repr, Value};
use crate::layout::{Align, Length, Rel}; use crate::layout::{Align, Length, Rel};
use crate::syntax::ast::{self, AstNode}; use crate::syntax::ast::{self, AstNode};
use crate::text::TextElem; use crate::text::TextElem;
@ -217,28 +217,15 @@ pub fn add(lhs: Value, rhs: Value) -> StrResult<Value> {
(Array(a), Array(b)) => Array(a + b), (Array(a), Array(b)) => Array(a + b),
(Dict(a), Dict(b)) => Dict(a + b), (Dict(a), Dict(b)) => Dict(a + b),
(Color(color), Length(thickness)) | (Length(thickness), Color(color)) => Stroke { (Color(color), Length(thickness)) | (Length(thickness), Color(color)) => {
paint: Smart::Custom(color.into()), Stroke::from_pair(color, thickness).into_value()
thickness: Smart::Custom(thickness),
..Stroke::default()
} }
.into_value(),
(Gradient(gradient), Length(thickness)) (Gradient(gradient), Length(thickness))
| (Length(thickness), Gradient(gradient)) => Stroke { | (Length(thickness), Gradient(gradient)) => {
paint: Smart::Custom(gradient.into()), Stroke::from_pair(gradient, thickness).into_value()
thickness: Smart::Custom(thickness),
..Stroke::default()
} }
.into_value(),
(Pattern(pattern), Length(thickness)) | (Length(thickness), Pattern(pattern)) => { (Pattern(pattern), Length(thickness)) | (Length(thickness), Pattern(pattern)) => {
Stroke { Stroke::from_pair(pattern, thickness).into_value()
paint: Smart::Custom(pattern.into()),
thickness: Smart::Custom(thickness),
..Stroke::default()
}
.into_value()
} }
(Duration(a), Duration(b)) => Duration(a + b), (Duration(a), Duration(b)) => Duration(a + b),

View File

@ -384,11 +384,8 @@ impl Frame {
1, 1,
Point::with_y(self.baseline()), Point::with_y(self.baseline()),
FrameItem::Shape( FrameItem::Shape(
Geometry::Line(Point::with_x(self.size.x)).stroked(FixedStroke { Geometry::Line(Point::with_x(self.size.x))
paint: Color::RED.into(), .stroked(FixedStroke::from_pair(Color::RED, Abs::pt(1.0))),
thickness: Abs::pt(1.0),
..FixedStroke::default()
}),
Span::detached(), Span::detached(),
), ),
); );
@ -411,11 +408,8 @@ impl Frame {
self.push( self.push(
Point::with_y(y), Point::with_y(y),
FrameItem::Shape( FrameItem::Shape(
Geometry::Line(Point::with_x(self.size.x)).stroked(FixedStroke { Geometry::Line(Point::with_x(self.size.x))
paint: Color::GREEN.into(), .stroked(FixedStroke::from_pair(Color::GREEN, Abs::pt(1.0))),
thickness: Abs::pt(1.0),
..FixedStroke::default()
}),
Span::detached(), Span::detached(),
), ),
); );

View File

@ -152,11 +152,12 @@ fn layout(
frame.push( frame.push(
line_pos, line_pos,
FrameItem::Shape( FrameItem::Shape(
Geometry::Line(Point::with_x(line_width)).stroked(FixedStroke { Geometry::Line(Point::with_x(line_width)).stroked(
paint: TextElem::fill_in(ctx.styles()).as_decoration(), FixedStroke::from_pair(
thickness, TextElem::fill_in(ctx.styles()).as_decoration(),
..FixedStroke::default() thickness,
}), ),
),
span, span,
), ),
); );

View File

@ -131,11 +131,12 @@ fn layout(
frame.push( frame.push(
line_pos, line_pos,
FrameItem::Shape( FrameItem::Shape(
Geometry::Line(Point::with_x(radicand.width())).stroked(FixedStroke { Geometry::Line(Point::with_x(radicand.width())).stroked(
paint: TextElem::fill_in(ctx.styles()).as_decoration(), FixedStroke::from_pair(
thickness, TextElem::fill_in(ctx.styles()).as_decoration(),
..FixedStroke::default() thickness,
}), ),
),
span, span,
), ),
); );

View File

@ -410,11 +410,10 @@ pub(crate) fn decorate(
}; };
let offset = offset.unwrap_or(-metrics.position.at(text.size)) - shift; let offset = offset.unwrap_or(-metrics.position.at(text.size)) - shift;
let stroke = stroke.clone().unwrap_or(FixedStroke { let stroke = stroke.clone().unwrap_or(FixedStroke::from_pair(
paint: text.fill.as_decoration(), text.fill.as_decoration(),
thickness: metrics.thickness.at(text.size), metrics.thickness.at(text.size),
..FixedStroke::default() ));
});
let gap_padding = 0.08 * text.size; let gap_padding = 0.08 * text.size;
let min_width = 0.162 * text.size; let min_width = 0.162 * text.size;

View File

@ -66,6 +66,17 @@ pub struct Stroke<T: Numeric = Length> {
pub miter_limit: Smart<Scalar>, pub miter_limit: Smart<Scalar>,
} }
impl Stroke {
/// Create a stroke from a paint and a thickness.
pub fn from_pair(paint: impl Into<Paint>, thickness: Length) -> Self {
Self {
paint: Smart::Custom(paint.into()),
thickness: Smart::Custom(thickness),
..Default::default()
}
}
}
#[scope] #[scope]
impl Stroke { impl Stroke {
/// Converts a value to a stroke or constructs a stroke with the given /// Converts a value to a stroke or constructs a stroke with the given
@ -583,6 +594,17 @@ pub struct FixedStroke {
pub miter_limit: Scalar, pub miter_limit: Scalar,
} }
impl FixedStroke {
/// Create a stroke from a paint and a thickness.
pub fn from_pair(paint: impl Into<Paint>, thickness: Abs) -> Self {
Self {
paint: paint.into(),
thickness,
..Default::default()
}
}
}
impl Default for FixedStroke { impl Default for FixedStroke {
fn default() -> Self { fn default() -> Self {
Self { Self {