Skip to content

Commit 8807ca1

Browse files
committed
shapes: Add svg-path element
1 parent f22e375 commit 8807ca1

File tree

3 files changed

+126
-2
lines changed

3 files changed

+126
-2
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
leaks elements (like `scope` does) (#1004)
1515
- Fixed a bug that prevented the leakage of elements of `scope` elements inside
1616
a `group` (#930)
17+
- Added a new element `svg-path` that accepts a list of a subset of SVG
18+
commands to construct paths
1719

1820
# 0.4.2
1921
- The `tree` element now has a `anchor:` argument to position the tree (#929)
@@ -65,6 +67,7 @@
6567
- Added support for user-defined coordinate systems. See `register-coordinate-resolver(<callback>)`
6668
- The default style value for `mark.transform-shape` changed to `false`
6769
- Harpoon marks changed sides (on a line from (0, 0) to (1, 0), the end mark now appears on the left side/top)
70+
- Brace styling changed, see the documention of `decorations.brace`
6871

6972
# 0.3.4
7073
- Fixed a bug with rendering curves with Typst 0.13.1

src/draw.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#import "draw/grouping.typ": intersections, group, scope, anchor, copy-anchors, set-ctx, get-ctx, for-each-anchor, on-layer, hide, floating
22
#import "draw/transformations.typ": set-transform, rotate, translate, scale, set-origin, move-to, set-viewport
33
#import "draw/styling.typ": set-style, fill, stroke, register-mark
4-
#import "draw/shapes.typ": circle, circle-through, arc, arc-through, mark, line, grid, content, rect, bezier, bezier-through, catmull, hobby, merge-path, polygon, compound-path, n-star, rect-around
4+
#import "draw/shapes.typ": circle, circle-through, arc, arc-through, mark, line, grid, content, rect, bezier, bezier-through, catmull, hobby, merge-path, polygon, compound-path, n-star, rect-around, svg-path
55
#import "draw/projection.typ": ortho, on-xy, on-xz, on-yz
66
#import "draw/util.typ": assert-version, register-coordinate-resolver

src/draw/shapes.typ

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@
917917

918918
let (from-x, from-y, ..) = from
919919
let (to-x, to-y, ..) = to
920-
920+
921921
// Resolve shift parameter
922922
let shift = style.at("shift", default: 0)
923923
if type(shift) == dictionary {
@@ -2022,3 +2022,124 @@
20222022
})
20232023
return ctx
20242024
}
2025+
2026+
/// Create a new path from a SVG-like list of commands.
2027+
///
2028+
/// The following commands are supported (uppercase command names use absolute coordinates, lowercase use relative coordinates)
2029+
/// - `("l", coordinate)` line to `coordinate`
2030+
/// - `("h", number)` Horizontal line
2031+
/// - `("v", number)` Vertical line
2032+
/// - `("m", coordinate)` Move to `coordinate`
2033+
/// - `("c", ctrl-coordinate-a, ctrl-coordinate-b, coordinate)` Cubic bezier curve to `coordinate` with two control points a and b
2034+
/// - `("q", ctrl-coordinate, coordinate)` Quadratic bezier curve
2035+
/// - `("z",)` Close the current path
2036+
///
2037+
/// ```example
2038+
/// svg-path(("h", 2),
2039+
/// ("c", (0, 1), (0, 0), (-1, 0)),
2040+
/// ("v", -0.5),
2041+
/// ("h", -1),
2042+
/// ("z",))
2043+
/// ```
2044+
#let svg-path(name: none, anchor: none, ..commands-style) = {
2045+
let style = commands-style.named()
2046+
let commands = commands-style.pos().map(cmd => {
2047+
if type(cmd) == str {
2048+
(cmd,)
2049+
} else {
2050+
cmd
2051+
}
2052+
})
2053+
return (ctx => {
2054+
let paths = ()
2055+
2056+
let origin = (0, 0, 0)
2057+
let current = ()
2058+
2059+
for ((cmd, ..args)) in commands {
2060+
assert(cmd in ("m", "M", "l", "L", "c", "C", "h", "H", "v", "V", "z", "Z", "q", "Q"),
2061+
message: "Unknown svg-path command: " + repr(cmd))
2062+
2063+
let is-relative = cmd in ("m", "l", "c", "h", "v", "q")
2064+
let wrap-coordinate = if is-relative {
2065+
x => (rel: x)
2066+
} else {
2067+
x => x
2068+
}
2069+
2070+
if cmd in ("h", "H") {
2071+
assert.eq(args.len(), 1)
2072+
let (x, ..rest) = args
2073+
args = ((x, 0.0, 0.0),)
2074+
cmd = if cmd == "h" { "l" } else { "L" }
2075+
} else if cmd in ("v", "V") {
2076+
assert.eq(args.len(), 1)
2077+
let (y, ..rest) = args
2078+
args = ((0.0, y, 0.0),)
2079+
cmd = if cmd == "v" { "l" } else { "L" }
2080+
}
2081+
2082+
(ctx, ..args) = coordinate.resolve(ctx, ..args.map(wrap-coordinate))
2083+
2084+
if cmd in ("z", "Z") {
2085+
assert.eq(args.len(), 0)
2086+
if current != () {
2087+
paths.push(path-util.make-subpath(origin, current, closed: cmd == "z"))
2088+
}
2089+
2090+
current = ()
2091+
}
2092+
2093+
if cmd in ("m", "M", "l", "L") {
2094+
if cmd in ("m", "M") {
2095+
assert.eq(args.len(), 1)
2096+
origin = args.at(0, default: (0, 0, 0))
2097+
args.pop()
2098+
}
2099+
2100+
current += args.map(pt => ("l", pt))
2101+
} else if cmd in ("c", "C") {
2102+
assert.eq(args.len(), 3)
2103+
let (c1, c2, pt) = args
2104+
current.push(("c", c1, c2, pt))
2105+
} else if cmd in ("q", "Q") {
2106+
assert.eq(args.len(), 2)
2107+
let (c1, pt) = args
2108+
let (_, pt, c1, c2) = bezier_.quadratic-to-cubic(ctx.prev.pt, pt, c1)
2109+
current.push(("c", c1, c2, pt))
2110+
}
2111+
}
2112+
2113+
if current != () {
2114+
paths.push(path-util.make-subpath(origin, current, closed: false))
2115+
}
2116+
2117+
let style = styles.resolve(ctx.style, merge: style)
2118+
let drawables = drawable.path(paths, stroke: style.stroke, fill: style.fill, fill-rule: style.fill-rule)
2119+
2120+
let (transform, anchors) = anchor_.setup(
2121+
(_) => none,
2122+
(),
2123+
default: none,
2124+
name: name,
2125+
offset-anchor: anchor,
2126+
transform: ctx.transform,
2127+
//border-anchors: true,
2128+
path-anchors: true,
2129+
path: drawables,
2130+
)
2131+
2132+
if mark_.check-mark(style.mark) {
2133+
drawables = mark_.place-marks-along-path(ctx, style.mark, transform, drawables)
2134+
} else {
2135+
drawables = drawable.apply-transform(transform, drawables)
2136+
}
2137+
2138+
return (
2139+
ctx: ctx,
2140+
name: name,
2141+
anchors: anchors,
2142+
drawables: drawables,
2143+
)
2144+
},)
2145+
}

0 commit comments

Comments
 (0)