Fix naming inconsistency for strokes

This commit is contained in:
Laurenz 2023-12-28 13:51:32 +01:00
parent e215f22965
commit 67ead94cc2
7 changed files with 65 additions and 103 deletions

View File

@ -516,26 +516,18 @@ impl PageContext<'_, '_> {
Some(Paint::Gradient(_)) Some(Paint::Gradient(_))
) )
{ {
let FixedStroke { let FixedStroke { paint, thickness, cap, join, dash, miter_limit } = stroke;
paint,
thickness,
line_cap,
line_join,
dash_pattern,
miter_limit,
} = stroke;
paint.set_as_stroke(self, on_text, transforms); paint.set_as_stroke(self, on_text, transforms);
self.content.set_line_width(thickness.to_f32()); self.content.set_line_width(thickness.to_f32());
if self.state.stroke.as_ref().map(|s| &s.line_cap) != Some(line_cap) { if self.state.stroke.as_ref().map(|s| &s.cap) != Some(cap) {
self.content.set_line_cap(to_pdf_line_cap(*line_cap)); self.content.set_line_cap(to_pdf_line_cap(*cap));
} }
if self.state.stroke.as_ref().map(|s| &s.line_join) != Some(line_join) { if self.state.stroke.as_ref().map(|s| &s.join) != Some(join) {
self.content.set_line_join(to_pdf_line_join(*line_join)); self.content.set_line_join(to_pdf_line_join(*join));
} }
if self.state.stroke.as_ref().map(|s| &s.dash_pattern) != Some(dash_pattern) { if self.state.stroke.as_ref().map(|s| &s.dash) != Some(dash) {
if let Some(pattern) = dash_pattern { if let Some(pattern) = dash {
self.content.set_dash_pattern( self.content.set_dash_pattern(
pattern.array.iter().map(|l| l.to_f32()), pattern.array.iter().map(|l| l.to_f32()),
pattern.phase.to_f32(), pattern.phase.to_f32(),

View File

@ -410,17 +410,11 @@ fn render_outline_glyph(
); );
canvas.fill_path(&path, &paint, rule, ts, state.mask); canvas.fill_path(&path, &paint, rule, ts, state.mask);
if let Some(FixedStroke { if let Some(FixedStroke { paint, thickness, cap, join, dash, miter_limit }) =
paint, &text.stroke
thickness,
line_cap,
line_join,
dash_pattern,
miter_limit,
}) = &text.stroke
{ {
if thickness.to_f32() > 0.0 { if thickness.to_f32() > 0.0 {
let dash = dash_pattern.as_ref().and_then(to_sk_dash_pattern); let dash = dash.as_ref().and_then(to_sk_dash_pattern);
let paint = to_sk_paint( let paint = to_sk_paint(
paint, paint,
@ -433,8 +427,8 @@ fn render_outline_glyph(
); );
let stroke = sk::Stroke { let stroke = sk::Stroke {
width: thickness.to_f32() / scale, // When we scale the path, we need to scale the stroke width, too. width: thickness.to_f32() / scale, // When we scale the path, we need to scale the stroke width, too.
line_cap: to_sk_line_cap(*line_cap), line_cap: to_sk_line_cap(*cap),
line_join: to_sk_line_join(*line_join), line_join: to_sk_line_join(*join),
dash, dash,
miter_limit: miter_limit.get() as f32, miter_limit: miter_limit.get() as f32,
}; };
@ -607,20 +601,14 @@ fn render_shape(canvas: &mut sk::Pixmap, state: State, shape: &Shape) -> Option<
canvas.fill_path(&path, &paint, rule, ts, state.mask); canvas.fill_path(&path, &paint, rule, ts, state.mask);
} }
if let Some(FixedStroke { if let Some(FixedStroke { paint, thickness, cap, join, dash, miter_limit }) =
paint, &shape.stroke
thickness,
line_cap,
line_join,
dash_pattern,
miter_limit,
}) = &shape.stroke
{ {
let width = thickness.to_f32(); let width = thickness.to_f32();
// Don't draw zero-pt stroke. // Don't draw zero-pt stroke.
if width > 0.0 { if width > 0.0 {
let dash = dash_pattern.as_ref().and_then(to_sk_dash_pattern); let dash = dash.as_ref().and_then(to_sk_dash_pattern);
let bbox = shape.geometry.bbox_size(); let bbox = shape.geometry.bbox_size();
let offset_bbox = (!matches!(shape.geometry, Geometry::Line(..))) let offset_bbox = (!matches!(shape.geometry, Geometry::Line(..)))
@ -661,8 +649,8 @@ fn render_shape(canvas: &mut sk::Pixmap, state: State, shape: &Shape) -> Option<
); );
let stroke = sk::Stroke { let stroke = sk::Stroke {
width, width,
line_cap: to_sk_line_cap(*line_cap), line_cap: to_sk_line_cap(*cap),
line_join: to_sk_line_join(*line_join), line_join: to_sk_line_join(*join),
dash, dash,
miter_limit: miter_limit.get() as f32, miter_limit: miter_limit.get() as f32,
}; };

View File

@ -657,7 +657,7 @@ impl SVGRenderer {
self.xml.write_attribute("stroke-width", &stroke.thickness.to_pt()); self.xml.write_attribute("stroke-width", &stroke.thickness.to_pt());
self.xml.write_attribute( self.xml.write_attribute(
"stroke-linecap", "stroke-linecap",
match stroke.line_cap { match stroke.cap {
LineCap::Butt => "butt", LineCap::Butt => "butt",
LineCap::Round => "round", LineCap::Round => "round",
LineCap::Square => "square", LineCap::Square => "square",
@ -665,7 +665,7 @@ impl SVGRenderer {
); );
self.xml.write_attribute( self.xml.write_attribute(
"stroke-linejoin", "stroke-linejoin",
match stroke.line_join { match stroke.join {
LineJoin::Miter => "miter", LineJoin::Miter => "miter",
LineJoin::Round => "round", LineJoin::Round => "round",
LineJoin::Bevel => "bevel", LineJoin::Bevel => "bevel",
@ -673,7 +673,7 @@ impl SVGRenderer {
); );
self.xml self.xml
.write_attribute("stroke-miterlimit", &stroke.miter_limit.get()); .write_attribute("stroke-miterlimit", &stroke.miter_limit.get());
if let Some(pattern) = &stroke.dash_pattern { if let Some(pattern) = &stroke.dash {
self.xml.write_attribute("stroke-dashoffset", &pattern.phase.to_pt()); self.xml.write_attribute("stroke-dashoffset", &pattern.phase.to_pt());
self.xml.write_attribute( self.xml.write_attribute(
"stroke-dasharray", "stroke-dasharray",

View File

@ -37,9 +37,9 @@ pub(crate) fn field(value: &Value, field: &str) -> StrResult<Value> {
match field { match field {
"paint" => stroke.paint.clone().into_value(), "paint" => stroke.paint.clone().into_value(),
"thickness" => stroke.thickness.into_value(), "thickness" => stroke.thickness.into_value(),
"cap" => stroke.line_cap.into_value(), "cap" => stroke.cap.into_value(),
"join" => stroke.line_join.into_value(), "join" => stroke.join.into_value(),
"dash" => stroke.dash_pattern.clone().into_value(), "dash" => stroke.dash.clone().into_value(),
"miter-limit" => { "miter-limit" => {
stroke.miter_limit.map(|limit| limit.get()).into_value() stroke.miter_limit.map(|limit| limit.get()).into_value()
} }

View File

@ -412,7 +412,7 @@ fn layout_mat_body(
let default_stroke = FixedStroke { let default_stroke = FixedStroke {
thickness: default_stroke_thickness, thickness: default_stroke_thickness,
paint: TextElem::fill_in(ctx.styles()).as_decoration(), paint: TextElem::fill_in(ctx.styles()).as_decoration(),
line_cap: LineCap::Square, cap: LineCap::Square,
..Default::default() ..Default::default()
}; };

View File

@ -709,7 +709,7 @@ fn corners_control_points(
strokes.get_ref(corner.side_ccw()), strokes.get_ref(corner.side_ccw()),
strokes.get_ref(corner.side_cw()), strokes.get_ref(corner.side_cw()),
) { ) {
(Some(a), Some(b)) => a.paint == b.paint && a.dash_pattern == b.dash_pattern, (Some(a), Some(b)) => a.paint == b.paint && a.dash == b.dash,
(None, None) => true, (None, None) => true,
_ => false, _ => false,
}, },
@ -870,7 +870,7 @@ fn segment(
} }
let solid = stroke let solid = stroke
.dash_pattern .dash
.as_ref() .as_ref()
.map(|pattern| pattern.array.is_empty()) .map(|pattern| pattern.array.is_empty())
.unwrap_or(true); .unwrap_or(true);

View File

@ -57,11 +57,11 @@ pub struct Stroke<T: Numeric = Length> {
/// The stroke's thickness. /// The stroke's thickness.
pub thickness: Smart<T>, pub thickness: Smart<T>,
/// The stroke's line cap. /// The stroke's line cap.
pub line_cap: Smart<LineCap>, pub cap: Smart<LineCap>,
/// The stroke's line join. /// The stroke's line join.
pub line_join: Smart<LineJoin>, pub join: Smart<LineJoin>,
/// The stroke's line dash pattern. /// The stroke's line dash pattern.
pub dash_pattern: Smart<Option<DashPattern<T>>>, pub dash: Smart<Option<DashPattern<T>>>,
/// The miter limit. /// The miter limit.
pub miter_limit: Smart<Scalar>, pub miter_limit: Smart<Scalar>,
} }
@ -182,19 +182,12 @@ impl Stroke {
let paint = take::<Paint>(args, "paint")?; let paint = take::<Paint>(args, "paint")?;
let thickness = take::<Length>(args, "thickness")?; let thickness = take::<Length>(args, "thickness")?;
let line_cap = take::<LineCap>(args, "cap")?; let cap = take::<LineCap>(args, "cap")?;
let line_join = take::<LineJoin>(args, "join")?; let join = take::<LineJoin>(args, "join")?;
let dash_pattern = take::<Option<DashPattern>>(args, "dash")?; let dash = take::<Option<DashPattern>>(args, "dash")?;
let miter_limit = take::<f64>(args, "miter-limit")?.map(Scalar::new); let miter_limit = take::<f64>(args, "miter-limit")?.map(Scalar::new);
Ok(Self { Ok(Self { paint, thickness, cap, join, dash, miter_limit })
paint,
thickness,
line_cap,
line_join,
dash_pattern,
miter_limit,
})
} }
} }
@ -207,9 +200,9 @@ impl<T: Numeric> Stroke<T> {
Stroke { Stroke {
paint: self.paint, paint: self.paint,
thickness: self.thickness.map(&f), thickness: self.thickness.map(&f),
line_cap: self.line_cap, cap: self.cap,
line_join: self.line_join, join: self.join,
dash_pattern: self.dash_pattern.map(|pattern| { dash: self.dash.map(|pattern| {
pattern.map(|pattern| DashPattern { pattern.map(|pattern| DashPattern {
array: pattern array: pattern
.array .array
@ -231,8 +224,8 @@ impl Stroke<Abs> {
/// Unpack the stroke, filling missing fields from the `default`. /// Unpack the stroke, filling missing fields from the `default`.
pub fn unwrap_or(self, default: FixedStroke) -> FixedStroke { pub fn unwrap_or(self, default: FixedStroke) -> FixedStroke {
let thickness = self.thickness.unwrap_or(default.thickness); let thickness = self.thickness.unwrap_or(default.thickness);
let dash_pattern = self let dash = self
.dash_pattern .dash
.map(|pattern| { .map(|pattern| {
pattern.map(|pattern| DashPattern { pattern.map(|pattern| DashPattern {
array: pattern array: pattern
@ -243,14 +236,14 @@ impl Stroke<Abs> {
phase: pattern.phase, phase: pattern.phase,
}) })
}) })
.unwrap_or(default.dash_pattern); .unwrap_or(default.dash);
FixedStroke { FixedStroke {
paint: self.paint.unwrap_or(default.paint), paint: self.paint.unwrap_or(default.paint),
thickness, thickness,
line_cap: self.line_cap.unwrap_or(default.line_cap), cap: self.cap.unwrap_or(default.cap),
line_join: self.line_join.unwrap_or(default.line_join), join: self.join.unwrap_or(default.join),
dash_pattern, dash,
miter_limit: self.miter_limit.unwrap_or(default.miter_limit), miter_limit: self.miter_limit.unwrap_or(default.miter_limit),
} }
} }
@ -266,19 +259,8 @@ impl Stroke<Abs> {
impl<T: Numeric + Repr> Repr for Stroke<T> { impl<T: Numeric + Repr> Repr for Stroke<T> {
fn repr(&self) -> EcoString { fn repr(&self) -> EcoString {
let mut r = EcoString::new(); let mut r = EcoString::new();
let Self { let Self { paint, thickness, cap, join, dash, miter_limit } = &self;
paint, if cap.is_auto() && join.is_auto() && dash.is_auto() && miter_limit.is_auto() {
thickness,
line_cap,
line_join,
dash_pattern,
miter_limit,
} = &self;
if line_cap.is_auto()
&& line_join.is_auto()
&& dash_pattern.is_auto()
&& miter_limit.is_auto()
{
match (&self.paint, &self.thickness) { match (&self.paint, &self.thickness) {
(Smart::Custom(paint), Smart::Custom(thickness)) => { (Smart::Custom(paint), Smart::Custom(thickness)) => {
r.push_str(&thickness.repr()); r.push_str(&thickness.repr());
@ -304,19 +286,19 @@ impl<T: Numeric + Repr> Repr for Stroke<T> {
r.push_str(&thickness.repr()); r.push_str(&thickness.repr());
sep = ", "; sep = ", ";
} }
if let Smart::Custom(cap) = &line_cap { if let Smart::Custom(cap) = &cap {
r.push_str(sep); r.push_str(sep);
r.push_str("cap: "); r.push_str("cap: ");
r.push_str(&cap.repr()); r.push_str(&cap.repr());
sep = ", "; sep = ", ";
} }
if let Smart::Custom(join) = &line_join { if let Smart::Custom(join) = &join {
r.push_str(sep); r.push_str(sep);
r.push_str("join: "); r.push_str("join: ");
r.push_str(&join.repr()); r.push_str(&join.repr());
sep = ", "; sep = ", ";
} }
if let Smart::Custom(dash) = &dash_pattern { if let Smart::Custom(dash) = &dash {
r.push_str(sep); r.push_str(sep);
r.push_str("cap: "); r.push_str("cap: ");
if let Some(dash) = dash { if let Some(dash) = dash {
@ -344,9 +326,9 @@ impl Resolve for Stroke {
Stroke { Stroke {
paint: self.paint, paint: self.paint,
thickness: self.thickness.resolve(styles), thickness: self.thickness.resolve(styles),
line_cap: self.line_cap, cap: self.cap,
line_join: self.line_join, join: self.join,
dash_pattern: self.dash_pattern.resolve(styles), dash: self.dash.resolve(styles),
miter_limit: self.miter_limit, miter_limit: self.miter_limit,
} }
} }
@ -359,9 +341,9 @@ impl Fold for Stroke<Abs> {
Self { Self {
paint: self.paint.or(outer.paint), paint: self.paint.or(outer.paint),
thickness: self.thickness.or(outer.thickness), thickness: self.thickness.or(outer.thickness),
line_cap: self.line_cap.or(outer.line_cap), cap: self.cap.or(outer.cap),
line_join: self.line_join.or(outer.line_join), join: self.join.or(outer.join),
dash_pattern: self.dash_pattern.or(outer.dash_pattern), dash: self.dash.or(outer.dash),
miter_limit: self.miter_limit.or(outer.miter_limit), miter_limit: self.miter_limit.or(outer.miter_limit),
} }
} }
@ -394,18 +376,18 @@ cast! {
let paint = take::<Paint>(&mut dict, "paint")?; let paint = take::<Paint>(&mut dict, "paint")?;
let thickness = take::<Length>(&mut dict, "thickness")?; let thickness = take::<Length>(&mut dict, "thickness")?;
let line_cap = take::<LineCap>(&mut dict, "cap")?; let cap = take::<LineCap>(&mut dict, "cap")?;
let line_join = take::<LineJoin>(&mut dict, "join")?; let join = take::<LineJoin>(&mut dict, "join")?;
let dash_pattern = take::<Option<DashPattern>>(&mut dict, "dash")?; let dash = take::<Option<DashPattern>>(&mut dict, "dash")?;
let miter_limit = take::<f64>(&mut dict, "miter-limit")?; let miter_limit = take::<f64>(&mut dict, "miter-limit")?;
dict.finish(&["paint", "thickness", "cap", "join", "dash", "miter-limit"])?; dict.finish(&["paint", "thickness", "cap", "join", "dash", "miter-limit"])?;
Self { Self {
paint, paint,
thickness, thickness,
line_cap, cap,
line_join, join,
dash_pattern, dash,
miter_limit: miter_limit.map(Scalar::new), miter_limit: miter_limit.map(Scalar::new),
} }
}, },
@ -592,11 +574,11 @@ pub struct FixedStroke {
/// The stroke's thickness. /// The stroke's thickness.
pub thickness: Abs, pub thickness: Abs,
/// The stroke's line cap. /// The stroke's line cap.
pub line_cap: LineCap, pub cap: LineCap,
/// The stroke's line join. /// The stroke's line join.
pub line_join: LineJoin, pub join: LineJoin,
/// The stroke's line dash pattern. /// The stroke's line dash pattern.
pub dash_pattern: Option<DashPattern<Abs, Abs>>, pub dash: Option<DashPattern<Abs, Abs>>,
/// The miter limit. Defaults to 4.0, same as `tiny-skia`. /// The miter limit. Defaults to 4.0, same as `tiny-skia`.
pub miter_limit: Scalar, pub miter_limit: Scalar,
} }
@ -606,9 +588,9 @@ impl Default for FixedStroke {
Self { Self {
paint: Paint::Solid(Color::BLACK), paint: Paint::Solid(Color::BLACK),
thickness: Abs::pt(1.0), thickness: Abs::pt(1.0),
line_cap: LineCap::Butt, cap: LineCap::Butt,
line_join: LineJoin::Miter, join: LineJoin::Miter,
dash_pattern: None, dash: None,
miter_limit: Scalar::new(4.0), miter_limit: Scalar::new(4.0),
} }
} }