mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-27 19:27:21 -06:00
M3.36: SVG and PDF export draw arrowheads, width profiles, brushes and aligned strokes as the canvas does
Both writers now ask the shared stroke geometry how to write each stroke (effects::stroke::for_writer): a plain stroke where that is exact, else the canvas's filled outlines (arrowheads, width profiles, dashes fitted to corners, dots with round or projecting caps), grouped under the stroke opacity when the line and its heads overlap. Brushed strokes are written as their brush art. Inside/outside alignment only applies to closed paths, so open paths export as centred strokes. The PDF outside-stroke clip frame and the SVG outside mask now cover the whole stroke reach (miter spikes, projecting caps). SVG stroke gradients use the same paint box as the canvas. The svg crate docs no longer claim arrowheads, profiles, brushes and effects are dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
18d870c7df
commit
f363cdb4ee
+86
-72
@@ -15,6 +15,7 @@ use kurbo::{PathEl, Shape, Vec2};
|
||||
use vectorcraft_color::{BlendMode, Color, GradientKind, Paint};
|
||||
use vectorcraft_doc::appearance::stroke_paint_bounds;
|
||||
use vectorcraft_doc::{AppearanceItem, Document, LineCap, LineJoin, Node, NodeKind, StrokeAlign, StrokeLayer, TextObject};
|
||||
use vectorcraft_effects::stroke::{self, WrittenShape};
|
||||
use vectorcraft_geom::{Affine, BezPath, FillRule, Rect};
|
||||
|
||||
use crate::{Compatibility, ExportReport, PdfError, PdfOptions};
|
||||
@@ -70,7 +71,7 @@ pub fn export_with_report(doc: &Document, opts: &PdfOptions) -> Result<ExportRep
|
||||
}
|
||||
pdf.set_metadata(meta);
|
||||
|
||||
let mut ex = Exporter { doc, warnings: vec![], images: HashMap::new() };
|
||||
let mut ex = Exporter { doc, warnings: vec![], images: HashMap::new(), brushes: None };
|
||||
for i in indices {
|
||||
let ab = &doc.artboards[i];
|
||||
let r = ab.rect;
|
||||
@@ -129,6 +130,8 @@ struct Exporter<'a> {
|
||||
doc: &'a Document,
|
||||
warnings: Vec<String>,
|
||||
images: HashMap<String, Option<Image>>,
|
||||
/// The brush library, parsed when the first brushed stroke is written.
|
||||
brushes: Option<Vec<vectorcraft_brush::Brush>>,
|
||||
}
|
||||
|
||||
fn xf(a: Affine) -> Transform {
|
||||
@@ -406,7 +409,7 @@ impl Exporter<'_> {
|
||||
}
|
||||
NodeKind::Path { path, rule, guide, .. } => {
|
||||
if !*guide {
|
||||
self.shape(s, n, &path.to_bezpath(), *rule);
|
||||
self.shape(s, n, &path.to_bezpath(), *rule, page);
|
||||
}
|
||||
}
|
||||
NodeKind::Compound { children, rule } => {
|
||||
@@ -416,7 +419,7 @@ impl Exporter<'_> {
|
||||
bp.extend(p.to_bezpath());
|
||||
}
|
||||
}
|
||||
self.shape(s, n, &bp, *rule);
|
||||
self.shape(s, n, &bp, *rule, page);
|
||||
}
|
||||
NodeKind::Text(t) => self.text(s, n, t),
|
||||
NodeKind::Image(im) => self.image(s, im),
|
||||
@@ -440,7 +443,7 @@ impl Exporter<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
fn shape(&mut self, s: &mut Surface, n: &Node, bp: &BezPath, r: FillRule) {
|
||||
fn shape(&mut self, s: &mut Surface, n: &Node, bp: &BezPath, r: FillRule, page: Rect) {
|
||||
let Some(path) = to_path(bp) else { return };
|
||||
let bounds = bp.bounding_box();
|
||||
for item in &n.appearance.items {
|
||||
@@ -486,7 +489,7 @@ impl Exporter<'_> {
|
||||
if !st.visible || st.paint.is_none() || st.width <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
self.stroke(s, bp, &path, r, st, bounds);
|
||||
self.stroke(s, bp, &path, r, st, page);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -494,43 +497,27 @@ impl Exporter<'_> {
|
||||
s.set_stroke(None);
|
||||
}
|
||||
|
||||
fn stroke(&mut self, s: &mut Surface, bp: &BezPath, path: &Path, r: FillRule, st: &StrokeLayer, bounds: Rect) {
|
||||
if st.profile.is_some() || st.brush.is_some() {
|
||||
self.warn("variable-width profiles and brushes are exported as uniform strokes");
|
||||
fn stroke(&mut self, s: &mut Surface, bp: &BezPath, path: &Path, r: FillRule, st: &StrokeLayer, page: Rect) {
|
||||
if !stroke::is_plain(st) && self.brush_art(s, bp, st, page) {
|
||||
return;
|
||||
}
|
||||
let Some(paint) = self.paint(&st.paint, bounds.inflate(st.width / 2.0, st.width / 2.0)) else { return };
|
||||
let closed = bp.elements().last().is_some_and(|e| matches!(e, PathEl::ClosePath));
|
||||
let bounds = bp.bounding_box();
|
||||
let Some(paint) = self.paint(&st.paint, st.paint_bounds(bounds)) else { return };
|
||||
let w = stroke::for_writer(bp, st);
|
||||
let mut pushes = 0;
|
||||
if st.blend != BlendMode::Normal {
|
||||
s.push_blend_mode(blend(st.blend));
|
||||
pushes += 1;
|
||||
}
|
||||
// The line ends under its arrowheads; they overlap, so both take the opacity once, as a group.
|
||||
let pieces = vectorcraft_effects::stroke::stroke_pieces(bp, st);
|
||||
let grouped = !pieces.heads.is_empty() && st.opacity < 1.0;
|
||||
if grouped {
|
||||
s.push_opacity(norm(st.opacity));
|
||||
pushes += 1;
|
||||
}
|
||||
let opacity = if grouped { NormalizedF32::ONE } else { norm(st.opacity) };
|
||||
let trimmed = match &pieces.line {
|
||||
std::borrow::Cow::Owned(line) => Some(to_path(line)),
|
||||
std::borrow::Cow::Borrowed(_) => None,
|
||||
};
|
||||
let width = match st.align {
|
||||
StrokeAlign::Center => st.width,
|
||||
_ if closed => st.width * 2.0,
|
||||
_ => st.width,
|
||||
};
|
||||
match st.align {
|
||||
StrokeAlign::Inside if closed => {
|
||||
match w.side {
|
||||
Some(StrokeAlign::Inside) => {
|
||||
s.push_clip_path(path, &rule(r));
|
||||
pushes += 1;
|
||||
}
|
||||
StrokeAlign::Outside if closed => {
|
||||
// Clip to everything outside the path: a big frame plus the path, even-odd.
|
||||
let big = bounds.inflate(width * 2.0 + 10.0, width * 2.0 + 10.0);
|
||||
let mut outside = big.to_path(0.1);
|
||||
Some(StrokeAlign::Outside) => {
|
||||
// Clip to everything outside the path: a frame around all the stroke reaches
|
||||
// (miter spikes included) plus the path, even-odd.
|
||||
let mut outside = w.reach(st, bounds).inflate(1.0, 1.0).to_path(0.1);
|
||||
outside.extend(bp.iter());
|
||||
if let Some(p) = to_path(&outside) {
|
||||
s.push_clip_path(&p, &krilla::paint::FillRule::EvenOdd);
|
||||
@@ -539,55 +526,82 @@ impl Exporter<'_> {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let dash = st.dash.as_ref().filter(|d| d.is_dashed()).map(|d| {
|
||||
let mut pat: Vec<f32> = d.pattern.iter().map(|v| *v as f32).collect();
|
||||
if pat.len() % 2 == 1 {
|
||||
pat.extend(pat.clone());
|
||||
match &w.shape {
|
||||
WrittenShape::Stroke { width } => {
|
||||
let dash = st.dash.as_ref().filter(|d| d.is_dashed()).map(|d| {
|
||||
let mut pat: Vec<f32> = d.pattern.iter().map(|v| *v as f32).collect();
|
||||
if pat.len() % 2 == 1 {
|
||||
pat.extend(pat.clone());
|
||||
}
|
||||
StrokeDash { array: pat, offset: d.offset as f32 }
|
||||
});
|
||||
s.set_fill(None);
|
||||
s.set_stroke(Some(Stroke {
|
||||
paint,
|
||||
width: *width as f32,
|
||||
miter_limit: st.miter_limit.max(1.0) as f32,
|
||||
line_cap: match st.cap {
|
||||
LineCap::Butt => krilla::paint::LineCap::Butt,
|
||||
LineCap::Round => krilla::paint::LineCap::Round,
|
||||
LineCap::Square => krilla::paint::LineCap::Square,
|
||||
},
|
||||
line_join: match st.join {
|
||||
LineJoin::Miter => krilla::paint::LineJoin::Miter,
|
||||
LineJoin::Round => krilla::paint::LineJoin::Round,
|
||||
LineJoin::Bevel => krilla::paint::LineJoin::Bevel,
|
||||
},
|
||||
opacity: norm(st.opacity),
|
||||
dash,
|
||||
}));
|
||||
s.draw_path(path);
|
||||
s.set_stroke(None);
|
||||
}
|
||||
StrokeDash { array: pat, offset: d.offset as f32 }
|
||||
});
|
||||
s.set_fill(None);
|
||||
s.set_stroke(Some(Stroke {
|
||||
paint: paint.clone(),
|
||||
width: width as f32,
|
||||
miter_limit: st.miter_limit.max(1.0) as f32,
|
||||
line_cap: match st.cap {
|
||||
LineCap::Butt => krilla::paint::LineCap::Butt,
|
||||
LineCap::Round => krilla::paint::LineCap::Round,
|
||||
LineCap::Square => krilla::paint::LineCap::Square,
|
||||
},
|
||||
line_join: match st.join {
|
||||
LineJoin::Miter => krilla::paint::LineJoin::Miter,
|
||||
LineJoin::Round => krilla::paint::LineJoin::Round,
|
||||
LineJoin::Bevel => krilla::paint::LineJoin::Bevel,
|
||||
},
|
||||
opacity,
|
||||
dash,
|
||||
}));
|
||||
// A line fully covered by its heads (Tip alignment) has nothing left to stroke.
|
||||
if let Some(line) = trimmed.as_ref().map_or(Some(path), Option::as_ref) {
|
||||
s.draw_path(line);
|
||||
}
|
||||
s.set_stroke(None);
|
||||
// Pop the alignment clip before drawing arrowheads.
|
||||
if matches!(st.align, StrokeAlign::Inside | StrokeAlign::Outside) && closed && pushes > 0 {
|
||||
s.pop();
|
||||
pushes -= 1;
|
||||
}
|
||||
if !pieces.heads.is_empty() {
|
||||
s.set_fill(Some(Fill { paint, opacity, rule: krilla::paint::FillRule::NonZero }));
|
||||
for head in &pieces.heads {
|
||||
if let Some(p) = to_path(&head.outline) {
|
||||
WrittenShape::Fill(outlines) => {
|
||||
// The line and its arrowheads overlap: they take the opacity once, as a group.
|
||||
let grouped = outlines.len() > 1 && st.opacity < 1.0;
|
||||
if grouped {
|
||||
s.push_opacity(norm(st.opacity));
|
||||
pushes += 1;
|
||||
}
|
||||
let opacity = if grouped { NormalizedF32::ONE } else { norm(st.opacity) };
|
||||
s.set_stroke(None);
|
||||
s.set_fill(Some(Fill { paint, opacity, rule: krilla::paint::FillRule::NonZero }));
|
||||
for p in outlines.iter().filter_map(to_path) {
|
||||
s.draw_path(&p);
|
||||
}
|
||||
s.set_fill(None);
|
||||
}
|
||||
s.set_fill(None);
|
||||
}
|
||||
for _ in 0..pushes {
|
||||
s.pop();
|
||||
}
|
||||
}
|
||||
|
||||
/// Paint stroke `st` of the shape `bp` with its brush art (the stroke's opacity and blend mode
|
||||
/// over all of it). False when it has no known brush.
|
||||
fn brush_art(&mut self, s: &mut Surface, bp: &BezPath, st: &StrokeLayer, page: Rect) -> bool {
|
||||
let doc = self.doc;
|
||||
let brushes = self.brushes.get_or_insert_with(|| vectorcraft_brush::library(doc));
|
||||
let Some(b) = st.brush.as_deref().and_then(|name| brushes.iter().find(|b| b.name == name)) else { return false };
|
||||
let art = vectorcraft_brush::stroke_pieces(b, bp, st);
|
||||
let mut pushes = 0;
|
||||
if st.blend != BlendMode::Normal {
|
||||
s.push_blend_mode(blend(st.blend));
|
||||
pushes += 1;
|
||||
}
|
||||
if st.opacity < 1.0 {
|
||||
s.push_opacity(norm(st.opacity));
|
||||
pushes += 1;
|
||||
}
|
||||
for piece in &art {
|
||||
self.node(s, piece, page, false);
|
||||
}
|
||||
for _ in 0..pushes {
|
||||
s.pop();
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn text(&mut self, s: &mut Surface, n: &Node, t: &TextObject) {
|
||||
let layout = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t);
|
||||
let tb = t.xf.transform_rect_bbox(layout.bounds);
|
||||
|
||||
Reference in New Issue
Block a user