Skip to content

Commit f826020

Browse files
committed
shapes: Add svg-path function
1 parent 6960d42 commit f826020

File tree

4 files changed

+90
-9
lines changed

4 files changed

+90
-9
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Changed the internal path model to support multiple subpaths
66
- Braces are now drawn tapered by default (#828)
77
- Brace styling changed, see the documention of `decorations.brace`
8+
- Added a new element `svg-path` that accepts a list of a subset of SVG commands to construct paths
89

910
# 0.3.4
1011
- 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, multi-path
4+
#import "draw/shapes.typ": circle, circle-through, arc, arc-through, mark, line, grid, content, rect, bezier, bezier-through, catmull, hobby, merge-path, polygon, multi-path, svg-path
55
#import "draw/projection.typ": ortho, on-xy, on-xz, on-yz
66
#import "draw/util.typ": assert-version

src/draw/shapes.typ

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,82 @@
15511551
},)
15521552
}
15531553

1554+
/// Create a new path from a SVG-like list of commands.
1555+
///
1556+
/// The following commands are supported (uppercase command names use absolute coordinates, lowercase use relative coordinates)
1557+
/// - `("l", pt)` line to `pt`
1558+
/// - `("m", pt)` Move to `pt`
1559+
/// - `("c", ctrl-1, ctrl-2, pt)` Bezier curve to `pt` with control points `ctrl-1` and `ctrl-2`
1560+
/// - `("z")` Close the current path
1561+
#let svg-path(name: none, ..commands-style) = {
1562+
let style = commands-style.named()
1563+
let commands = commands-style.pos().map(cmd => {
1564+
if type(cmd) == str {
1565+
(cmd,)
1566+
} else {
1567+
cmd
1568+
}
1569+
})
1570+
return (ctx => {
1571+
let paths = ()
1572+
1573+
let origin = (0, 0, 0)
1574+
let current = ()
1575+
1576+
for ((cmd, ..args)) in commands {
1577+
assert(cmd in ("m", "M", "l", "L", "c", "C", "h", "H", "v", "V", "z", "Z"),
1578+
message: "Unknown svg-path command: " + repr(cmd))
1579+
1580+
let is-relative = cmd in ("m", "l", "c", "h", "v")
1581+
let wrap-coordinate = if is-relative {
1582+
x => (rel: x)
1583+
} else {
1584+
x => x
1585+
}
1586+
1587+
if cmd in ("h", "H") {
1588+
let (x, ..rest) = args
1589+
args = ((x, 0, 0),)
1590+
} else if cmd in ("v", "V") {
1591+
let (y, ..rest) = args
1592+
args = ((0, y, 0),)
1593+
}
1594+
1595+
(ctx, ..args) = coordinate.resolve(ctx, ..args.map(wrap-coordinate))
1596+
1597+
if cmd in ("z", "Z") {
1598+
if current != () {
1599+
paths.push(path-util.make-subpath(origin, current, closed: cmd == "z"))
1600+
}
1601+
1602+
current = ()
1603+
}
1604+
1605+
if cmd in ("m", "M", "l", "L") {
1606+
if cmd in ("m", "M") {
1607+
origin = args.at(0, default: (0, 0, 0))
1608+
args.pop(0)
1609+
}
1610+
1611+
current += args.map(pt => ("l", pt))
1612+
} else if cmd in ("c", "C") {
1613+
let (c1, c2, pt) = args
1614+
current.push(("c", c1, c2, pt))
1615+
}
1616+
}
1617+
1618+
let style = styles.resolve(ctx.style, merge: style)
1619+
let drawables = drawable.path(paths, stroke: style.stroke, fill: style.fill, fill-rule: style.fill-rule)
1620+
1621+
return (
1622+
ctx: ctx,
1623+
name: name,
1624+
//anchors: anchors,
1625+
drawables: drawables,
1626+
)
1627+
},)
1628+
}
1629+
15541630
/// Create a new path with one or more elments used as sub-paths.
15551631
/// This can be used to create paths with holes.
15561632
///

src/draw/transformations.typ

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
///
151151
/// Note that content like text does not scale automatically. See `auto-scale` styling of content for that.
152152
///
153-
/// - ..args (float, ratio): A single value to scale the transformation matrix by or per axis
153+
/// - ..args (float,ratio,length): A single value to scale the transformation matrix by or per axis
154154
/// scaling factors. Accepts a single float or ratio value or any combination of the named arguments
155155
/// `x`, `y` and `z` to set per axis scaling factors. A ratio of 100% is the same as the value $1$.
156156
/// - origin (none,coordinate): Origin to rotate around, or (0, 0, 0) if set to `none`.
@@ -171,14 +171,18 @@
171171
(factor, factor, factor)
172172
}
173173

174-
// Allow scaling using ratio values
175-
vec = vec.map(v => if type(v) == ratio {
176-
v / 100%
177-
} else {
178-
v
179-
})
180-
181174
(ctx => {
175+
let vec = vec.enumerate().map(((i, v)) => {
176+
if type(v) == ratio {
177+
v / 100%
178+
} else if type(v) == length {
179+
let s = matrix.mul4x4-vec3(ctx.transform, vector.one(3, i)).at(i)
180+
s * v / ctx.length
181+
} else {
182+
v
183+
}
184+
})
185+
182186
let mat = matrix.transform-scale(vec)
183187
ctx.transform = matrix.mul-mat(ctx.transform,
184188
_transform-around-origin(ctx, mat, origin))

0 commit comments

Comments
 (0)