Something went wrong. Try again.
Something went wrong. Try again.
typst-electronics main.typ
3.9 kB · 139 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140#import "@preview/cetz:0.5.3"#let p = plugin("./typst_electronics.wasm")
#let circuit(..content) = { let data = cbor.encode(content.pos()) let data = str(p.render(data))
cetz.canvas({ eval( data, scope: dictionary(cetz.draw), ) })}
#let _directions = ("left", "right", "up", "down")
#let _one-of(value, options, name: "value") = { assert(value in options, message: name + " must be one of " + options.map(repr).join(", ") + ", got " + repr(value)) value}
#let _validate_direction(direction, name: "value") = { if type(direction) != str and direction != none { panic("Incorrect type for " + name + ": " + repr(type(direction))) }
if (type(direction) == str) { _one-of(direction, _directions, name: name) }}
#let vsource(label, voltage, direction: none, rotation: none) = { if type(label) != str { panic("Incorrect type for label: " + repr(type(label))) } if type(voltage) != int and type(voltage) != float and type(voltage) != str { panic("Incorrect type for voltage: " + repr(type(voltage))) }
if type(direction) == none and type(rotation) == none { if (direction != none and rotation != none) or (direction == none and rotation == none) { panic("Atleased one of 'direction' and 'rotation' needs to be set") } }
_validate_direction(direction, name: "direction") _validate_direction(rotation, name: "rotation")
return (type: "v-source", direction: direction, rotation: rotation, label: label, voltage: str(voltage))}
#let asource(label, current, direction: none, rotation: none) = { if type(label) != str { panic("Incorrect type for label: " + repr(type(label))) } if type(current) != int and type(current) != float and type(current) != str { panic("Incorrect type for current: " + repr(type(current))) }
if type(direction) == none and type(rotation) == none { if (direction != none and rotation != none) or (direction == none and rotation == none) { panic("Atleased one of 'direction' and 'rotation' needs to be set") } }
_validate_direction(direction, name: "direction") _validate_direction(rotation, name: "rotation")
return (type: "a-source", direction: direction, rotation: rotation, label: label, current: str(current))}
#let resistor(label, ohm, direction: none, rotation: none) = { if type(label) != str { panic("Incorrect type for label: " + repr(type(label))) } if type(ohm) != int and type(ohm) != float and type(ohm) != str { panic("Incorrect type for ohm: " + repr(type(ohm))) }
if type(direction) == none and type(rotation) == none { if (direction != none and rotation != none) or (direction == none and rotation == none) { panic("Atleased one of 'direction' and 'rotation' needs to be set") } }
_validate_direction(direction, name: "direction") _validate_direction(rotation, name: "rotation")
return (type: "resistor", direction: direction, rotation: rotation, label: label, ohm: str(ohm))}
#let terminal(label, direction: none) = { if type(label) != str { panic("Incorrect type for label: " + repr(type(label))) }
(type: "terminal", direction: direction, label: label)}
#let up(value) = { if type(value) != int { panic("Incorrect type for value: " + repr(type(value))) }
(type: "up", value: value)}
#let down(value) = { if type(value) != int { panic("Incorrect type for value: " + repr(type(value))) }
(type: "down", value: value)}
#let right(value) = { if type(value) != int { panic("Incorrect type for value: " + repr(type(value))) }
(type: "right", value: value)}
#let left(value) = { if type(value) != int { panic("Incorrect type for value: " + repr(type(value))) }
(type: "left", value: value)}
#let split(..rest) = { (type: "split", content: rest.pos())}
#let addon(..rest, offset: none) = { (type: "addon", offset: offset, content: rest.pos())}