|
1551 | 1551 | },) |
1552 | 1552 | } |
1553 | 1553 |
|
| 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 | + |
1554 | 1630 | /// Create a new path with one or more elments used as sub-paths. |
1555 | 1631 | /// This can be used to create paths with holes. |
1556 | 1632 | /// |
|
0 commit comments