diff --git a/manual.pdf b/manual.pdf new file mode 100644 index 0000000..33f271c Binary files /dev/null and b/manual.pdf differ diff --git a/manual.typ b/manual.typ new file mode 100644 index 0000000..ed1bbb2 --- /dev/null +++ b/manual.typ @@ -0,0 +1,28 @@ +#import "@preview/tidy:0.3.0" +#import "@preview/cetz:0.2.2": draw +#import "src/circuit.typ": circuit +#import "src/util.typ" +#import "src/wire.typ" + +#let circuit-docs = tidy.parse-module( + read("src/circuit.typ"), + name: "circuit", + require-all-parameters: true +) +#tidy.show-module(circuit-docs) + +#let util-docs = tidy.parse-module( + read("src/util.typ"), + name: "util", + require-all-parameters: true, + scope: (util: util) +) +#tidy.show-module(util-docs) + +#let wire-docs = tidy.parse-module( + read("src/wire.typ"), + name: "wire", + require-all-parameters: true, + scope: (wire: wire, circuit: circuit, draw: draw) +) +#tidy.show-module(wire-docs) \ No newline at end of file diff --git a/src/circuit.typ b/src/circuit.typ index a45de68..84fb819 100644 --- a/src/circuit.typ +++ b/src/circuit.typ @@ -1,5 +1,11 @@ #import "@preview/cetz:0.2.2": canvas +#import "@preview/tidy:0.3.0" +/// Draws a block circuit diagram +/// +/// - body (none, array, element): A code block in which draw functions have been called +/// - length (length, ratio): Optional base unit +/// -> none #let circuit(body, length: 2em) = { set text(font: "Source Sans 3") canvas(length: length, body) diff --git a/src/util.typ b/src/util.typ index 5180f5a..62da0f5 100644 --- a/src/util.typ +++ b/src/util.typ @@ -1,3 +1,4 @@ +/// Predefined color palette #let colors = ( orange: rgb(245, 180, 147), yellow: rgb(250, 225, 127), @@ -6,11 +7,24 @@ purple: rgb(189, 151, 255) ) +/// Pads a string on the left with 0s to the given length +/// +/// #example(`#util.lpad("0100", 8)`, mode: "markup") +/// +/// - s (str): The string to pad +/// - len (int): The target length +/// -> str #let lpad(s, len) = { let res = "0" * len + s return res.slice(-len) } +/// Returns the anchor on the opposite side of the given one +/// +/// #example(`#util.opposite-anchor("west")`, mode: "markup") +/// +/// - anchor (str): The input anchor +/// -> str #let opposite-anchor(anchor) = { return ( north: "south", @@ -25,6 +39,11 @@ ).at(anchor) } +/// Returns the anchor rotated 90 degrees clockwise relative to the given one +/// +/// #example(`#util.rotate-anchor("west")`, mode: "markup") +/// - anchor (str): The anchor to rotate +/// -> str #let rotate-anchor(anchor) = { return ( north: "east", diff --git a/src/wire.typ b/src/wire.typ index 9b2323c..3e3ff82 100644 --- a/src/wire.typ +++ b/src/wire.typ @@ -1,6 +1,23 @@ #import "@preview/cetz:0.2.2": draw, coordinate #import "util.typ": opposite-anchor +/// List of valid wire styles +/// #box(width: 100%, align(center)[ +/// #circuit({ +/// draw.circle((0, 0), radius: .1, name: "p1") +/// draw.circle((2, 1), radius: .1, name: "p2") +/// draw.circle((3, 0), radius: .1, name: "p3") +/// draw.circle((5, 1), radius: .1, name: "p4") +/// draw.circle((6, 0), radius: .1, name: "p5") +/// draw.circle((8, 1), radius: .1, name: "p6") +/// wire.wire("w1", ("p1", "p2"), style: "direct") +/// wire.wire("w2", ("p3", "p4"), style: "zigzag") +/// wire.wire("w3", ("p5", "p6"), style: "dodge", dodge-y: -0.5, dodge-margins: (0.5, 0.5)) +/// draw.content((1, -1), [`direct`]) +/// draw.content((4, -1), [`zigzag`]) +/// draw.content((7, -1), [`dodge`]) +/// }) +/// ]) #let wire-styles = ("direct", "zigzag", "dodge") #let signal-width = 1pt #let bus-width = 1.5pt @@ -89,6 +106,21 @@ return (points, anchors) } +/// 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 +/// - style (str): The wire's style (see `wire-styles` for possible values) +/// - 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) +/// - zigzag-ratio (ratio): Position of the zigzag vertical relative to the horizontal span (only with style "zigzag") +/// - dodge-y (int, float, length, ratio): Y position to dodge the wire to (only with style "dodge") +/// - 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") #let wire( id, pts, bus: false, @@ -199,6 +231,20 @@ } }) +/// Draws a wire stub (useful for unlinked ports) +/// +/// #box(width: 100%, align(center)[ +/// #circuit({ +/// draw.circle((0, 0), radius: .1, name: "p") +/// wire.stub("p", "east", name: "port") +/// }) +/// ]) +/// +/// - 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 #let stub(port-id, side, name: none, vertical: false, length: 1em) = { let offset = ( north: (0, length),