2024-05-16 21:35:53 +00:00
#import "@preview/cetz:0.2.2": draw, coordinate
#import "util.typ": opposite-anchor
2024-05-17 07:36:38 +00:00
/// List of valid wire styles
2024-05-17 14:52:43 +00:00
/// #examples.wires
2024-05-16 21:35:53 +00:00
#let wire-styles = ("direct", "zigzag", "dodge")
#let signal-width = 1pt
#let bus-width = 1.5pt
2024-05-18 10:39:47 +00:00
#let intersection(pt, radius: .2, fill: black) = {
draw.circle(pt, radius: radius, stroke: none, fill: fill)
2024-05-16 21:35:53 +00:00
}
#let get-direct-wire(pts) = {
let anchors = (
"start": pts.first(),
"end": pts.last()
)
return (pts, anchors)
}
2024-05-18 10:39:47 +00:00
#let get-zigzag-wire(pts, ratio, dir) = {
2024-05-16 21:35:53 +00:00
let start = pts.first()
let end = pts.last()
let mid = (start, ratio, end)
2024-05-18 10:39:47 +00:00
let points = if dir == "vertical" {
(
start,
(horizontal: mid, vertical: ()),
(horizontal: (), vertical: end),
end
)
} else {
(
start,
(horizontal: (), vertical: mid),
(horizontal: end, vertical: ()),
end
)
}
2024-05-16 21:35:53 +00:00
let anchors = (
"start": start,
"zig": points.at(1),
"zag": points.at(2),
"end": end
)
return (points, anchors)
}
#let get-dodge-wire(pts, dodge-y, margins, sides, ctx) = {
let start = pts.first()
let end = pts.last()
let (margin-start, margin-end) = margins
let (side-start, side-end) = sides
let p1 = (start, margin-start, end)
let p2 = (end, margin-end, start)
let (ctx, p0) = coordinate.resolve(ctx, start)
let (ctx, p3) = coordinate.resolve(ctx, end)
p0 = (x: p0.first(), y: p0.last())
p3 = (x: p3.first(), y: p3.last())
let dx1 = margin-start
let dx2 = margin-end
if type(margin-start) == ratio {
dx1 = calc.abs(p3.x - p0.x) * margin-start / 100%
}
if type(margin-end) == ratio {
dx2 = calc.abs(p3.x - p0.x) * margin-end / 100%
}
if side-start == "west" {
dx1 *= -1
}
if side-end == "east" {
dx2 *= -1
}
p1 = (p0.x + dx1, p0.y)
p2 = (p3.x - dx2, p0.y)
let points = (
start,
(horizontal: p1, vertical: ()),
(horizontal: (), vertical: (0, dodge-y)),
(horizontal: p2, vertical: ()),
(horizontal: (), vertical: end),
end
)
let anchors = (
"start": start,
"start2": points.at(1),
"dodge-start": points.at(2),
"dodge-end": points.at(3),
"end2": points.at(4),
"end": end
)
return (points, anchors)
}
2024-05-17 07:36:38 +00:00
/// Draws a wire between two points
/// - id (str): The wire's id, for future reference (anchors)
/// - pts (array): The two points (as CeTZ compatible coordinates, i.e. XY, relative positions, ids, etc.)
/// - bus (bool): Whether the wire is a bus (multiple bits) or a simple signal (single bit)
/// - name (none, str, array): Optional name of the wire. If it is an array, the first name will be put at the start of the wire, and the second at the end
/// - name-pos (str): Position of the name. One of: "middle", "start" or "end"
/// - slice (none, array): Optional bits slice (start and end bit indices). If set, it will be displayed at the start of the wire
/// - color (color): The stroke color
/// - dashed (bool): Whether the stroke is dashed or not
2024-05-17 16:12:25 +00:00
/// - style (str): The wire's style (see #doc-ref("wire.wire-styles", var: true) for possible values)
2024-05-17 07:36:38 +00:00
/// - reverse (bool): If true, the start and end points will be swapped (useful in cases where the start point depends on the end point, for example with perpendiculars)
2024-05-18 10:39:47 +00:00
/// - directed (bool): If true, the wire will be directed, meaning an arrow will be drawn at the endpoint
/// - rotate-name (bool): If true, the name will be rotated according to the wire's slope
2024-05-17 07:36:38 +00:00
/// - zigzag-ratio (ratio): Position of the zigzag vertical relative to the horizontal span (only with style "zigzag")
2024-05-18 10:39:47 +00:00
/// - zigzag-dir (str): The zigzag's direction. As either "vertical" or "horizontal" (only with dstyle "zigzag")
2024-05-17 12:18:33 +00:00
/// - dodge-y (number): Y position to dodge the wire to (only with style "dodge")
2024-05-17 07:36:38 +00:00
/// - dodge-sides (array): The start and end sides (going out of the connected element) of the wire (only with style "dodge")
/// - dodge-margins (array): The start and end margins (i.e. space before dodging) of the wire (only with style "dodge")
2024-05-16 21:35:53 +00:00
#let wire(
id, pts,
bus: false,
name: none,
name-pos: "middle",
slice: none,
color: black,
dashed: false,
style: "direct",
reverse: false,
2024-05-18 10:39:47 +00:00
directed: false,
rotate-name: true,
2024-05-16 21:35:53 +00:00
zigzag-ratio: 50%,
2024-05-18 10:39:47 +00:00
zigzag-dir: "vertical",
2024-05-16 21:35:53 +00:00
dodge-y: 0,
dodge-sides: ("east", "west"),
dodge-margins: (5%, 5%)
) = draw.get-ctx(ctx => {
if not style in wire-styles {
panic("Invalid wire style '" + style + "'")
}
if pts.len() != 2 {
panic("Wrong number of points (got " + str(pts.len()) + " instead of 2)")
}
let stroke = (
paint: color,
thickness: if bus {bus-width} else {signal-width}
)
if dashed {
stroke.insert("dash", "dashed")
}
let points = ()
let anchors = ()
if style == "direct" {
(points, anchors) = get-direct-wire(pts)
} else if style == "zigzag" {
2024-05-18 10:39:47 +00:00
(points, anchors) = get-zigzag-wire(pts, zigzag-ratio, zigzag-dir)
2024-05-16 21:35:53 +00:00
} else if style == "dodge" {
(points, anchors) = get-dodge-wire(
pts,
dodge-y,
dodge-margins,
dodge-sides,
ctx
)
}
2024-05-18 10:39:47 +00:00
let mark = (fill: color)
if directed {
mark = (end: ">", fill: color)
}
2024-05-16 21:35:53 +00:00
draw.group(name: id, {
2024-05-18 10:39:47 +00:00
draw.line(..points, stroke: stroke, mark: mark)
2024-05-16 21:35:53 +00:00
for (anchor-name, anchor-pos) in anchors {
draw.anchor(anchor-name, anchor-pos)
}
})
let first-pt = id + ".start"
let last-pt = id + ".end"
2024-05-18 10:39:47 +00:00
let first-pos = points.first()
let second-pos = points.at(1)
2024-05-16 21:35:53 +00:00
if reverse {
(first-pt, last-pt) = (last-pt, first-pt)
2024-05-18 10:39:47 +00:00
(first-pos, second-pos) = (second-pos, first-pos)
}
let angle = 0deg
if rotate-name {
(ctx, first-pos) = coordinate.resolve(ctx, first-pos)
(ctx, second-pos) = coordinate.resolve(ctx, second-pos)
let (x1, y1, _) = first-pos
let (x2, y2, _) = second-pos
angle = calc.atan2(x2 - x1, y2 - y1)
2024-05-16 21:35:53 +00:00
}
if name != none {
let names = ()
if type(name) == str {
names = ((name, name-pos),)
} else if type(name) == array {
names = (
(name.at(0), "start"),
(name.at(1), "end")
)
}
for (name, pos) in names {
let point
let anchor
if pos == "middle" {
point = (first-pt, 50%, last-pt)
anchor = "south"
} else if pos == "start" {
point = first-pt
anchor = "south-west"
} else if pos == "end" {
point = last-pt
anchor = "south-east"
}
2024-05-18 10:39:47 +00:00
draw.content(point, anchor: anchor, padding: 3pt, angle: angle, name)
2024-05-16 21:35:53 +00:00
}
}
if slice != none {
let (start, end) = slice
let slice-txt = "[" + str(start) + ":" + str(end) + "]"
draw.content(
first-pt,
anchor: "south-west",
padding: 3pt,
text(slice-txt, size: 0.75em)
)
}
})
2024-05-17 07:36:38 +00:00
/// Draws a wire stub (useful for unlinked ports)
///
2024-05-17 14:52:43 +00:00
/// #examples.stub
2024-05-17 07:36:38 +00:00
/// - port-id (str): The port anchor
/// - side (str): The side on which the port is (one of "north", "east", "south", "west")
/// - name (none, str): Optional name displayed at the end of the stub
/// - vertical (bool): Whether the name should be displayed vertically
/// - length (number): The length of the stub
2024-05-17 21:52:43 +00:00
/// - name-offset (number): The name offset, perpendicular to the stub
#let stub(port-id, side, name: none, vertical: false, length: 1em, name-offset: 0) = {
let end-offset = (
2024-05-16 21:35:53 +00:00
north: (0, length),
east: (length, 0),
south: (0, -length),
west: (-length, 0)
).at(side)
2024-05-17 21:52:43 +00:00
let name-offset = (
north: (name-offset, length),
east: (length, name-offset),
south: (name-offset, -length),
west: (-length, name-offset)
).at(side)
2024-05-16 21:35:53 +00:00
draw.line(
port-id,
2024-05-17 21:52:43 +00:00
(rel: end-offset, to: port-id)
2024-05-16 21:35:53 +00:00
)
if name != none {
let text-anchor = if vertical {
(
"north": "west",
"south": "east",
"west": "south",
"east": "north"
).at(side)
} else { opposite-anchor(side) }
draw.content(
anchor: text-anchor,
padding: 0.2em,
angle: if vertical {90deg} else {0deg},
2024-05-17 21:52:43 +00:00
(rel: name-offset, to: port-id),
2024-05-16 21:35:53 +00:00
name
)
}
}