Skip to content

Commit

Permalink
[refactor] replace type checks against strings with type checks again…
Browse files Browse the repository at this point in the history
…st types (#11)

e.g., `var == "string"` becomes  `var == str`

WIth this change, quill requires Typst 0.8.0
  • Loading branch information
Mc-Zen authored Sep 17, 2024
1 parent a9c951a commit 5353932
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
9 changes: 4 additions & 5 deletions src/layout.typ
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
/// Take an alignment or 2d alignment and return a 2d alignment with the possibly
/// unspecified axis set to a default value.
#let make-2d-alignment(alignment, default-vertical: horizon, default-horizontal: center) = {
if type(alignment) == "2d alignment" { return alignment }
let axis = alignment.axis()
if axis == none { return alignment }
if alignment.axis() == "horizontal" { return alignment + default-vertical }
Expand Down Expand Up @@ -167,8 +166,8 @@
let cell-width = cell-sizes.at(integral, default: 0pt)
return center-coords.at(integral, default: last) + cell-width * (fractional - 0.5)
}
if type(cells) in ("integer", "float") { get(cells) }
else if type(cells) == "array" { cells.map(x => get(x)) }
if type(cells) in (int, float) { get(cells) }
else if type(cells) == array { cells.map(x => get(x)) }
else { panic("Unsupported coordinate type") }
}

Expand All @@ -182,8 +181,8 @@
let c2 = center-coords.at(integral + 1, default: last)
return c1 + (c2 - c1) * (fractional)
}
if type(cells) in ("integer", "float") { get(cells) }
else if type(cells) == "array" { cells.map(x => get(x)) }
if type(cells) in (int, float) { get(cells) }
else if type(cells) == array { cells.map(x => get(x)) }
else { panic("Unsupported coordinate type") }
}

Expand Down
10 changes: 5 additions & 5 deletions src/length-helpers.typ
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

#let convert-em-length(length, em) = {
if length.em == 0pt { return length }
if type(length.em) == "float" { return length.abs + length.em * em }
if type(length.em) == float { return length.abs + length.em * em }
return length.abs + length.em / 1em * em
}

#let get-length(length, container-length) = {
if type(length) == "length" { return length }
if type(length) == "ratio" { return length * container-length}
if type(length) == "relative length" { return length.length + length.ratio * container-length}
#let get-length(len, container-length) = {
if type(len) == length { return len }
if type(len) == ratio { return len * container-length}
if type(len) == relative { return len.length + len.ratio * container-length}
}
10 changes: 5 additions & 5 deletions src/process-args.typ
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#let process-padding-arg(padding) = {
let type = type(padding)
if type == "length" {
if type == length {
return (left: padding, top: padding, right: padding, bottom: padding)
}
if type == "dictionary" {
if type == dictionary {
let rest = padding.at("rest", default: 0pt)
let x = padding.at("x", default: rest)
let y = padding.at("y", default: rest)
Expand Down Expand Up @@ -38,9 +38,9 @@
) = {
if labels == none {  return () }
let type = type(labels)
if type == "dictionary" { labels = (labels,) } 
else if type in ("content", "string") { labels = ((content: labels),) } 
else if type == "dictionary" { labels = ((content: labels),) } 
if type == dictionary { labels = (labels,) } 
else if type in (content, str) { labels = ((content: labels),) } 
else if type == dictionary { labels = ((content: labels),) } 
let processed-labels = ()
for label in labels {
let alignment = layout.make-2d-alignment(label.at("pos", default: default-pos))
Expand Down
16 changes: 8 additions & 8 deletions src/quantum-circuit.typ
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
draw-params.x-gate-size = layout.default-size-hint(gate($X$), draw-params)

let items = children.pos().map( x => {
if type(x) in ("content", "string") and x != [\ ] { return gate(x) }
if type(x) in (content, str) and x != [\ ] { return gate(x) }
return x
})

Expand Down Expand Up @@ -324,14 +324,14 @@
let cols = layout.get-cell-coords(center-x-coords, col-widths, item.columns)
let annotation = (item.callback)(cols, rows)
verifications.verify-annotation-content(annotation)
if type(annotation) == "dictionary" {
if type(annotation) == dictionary {
(content, decoration-bounds) = layout.place-with-labels(
annotation.content,
dx: annotation.at("dx", default: 0pt),
dy: annotation.at("dy", default: 0pt),
draw-params: draw-params
)
} else if type(annotation) in ("content", "string") {
} else if type(annotation) in (content, str) {
layer-below-circuit += place(annotation)
}
}
Expand All @@ -353,14 +353,14 @@

if offset == auto { return (dx - width / 2, dy - height / 2) }

assert(type(offset) == "dictionary", message: "Unexpected type `" + type(offset) + "` for parameter `offset`")
assert(type(offset) == dictionary, message: "Unexpected type `" + type(offset) + "` for parameter `offset`")

let offset-x = offset.at("x", default: auto)
let offset-y = offset.at("y", default: auto)
if offset-x == auto { dx -= width / 2}
else if type(offset-x) == "length" { dx -= offset-x }
else if type(offset-x) == length { dx -= offset-x }
if offset-y == auto { dy -= height / 2}
else if type(offset-y) == "length" { dy -= offset-y }
else if type(offset-y) == length { dy -= offset-y }
return (dx, dy)
}

Expand Down Expand Up @@ -503,10 +503,10 @@
let final-width = scale * (bounds.at(2) - bounds.at(0))

let thebaseline = baseline
if type(thebaseline) in ("content", "string") {
if type(thebaseline) in (content, str) {
thebaseline = height/2 - measure(thebaseline, styles).height/2
}
if type(thebaseline) == "fraction" {
if type(thebaseline) == fraction {
thebaseline = 100% - layout.get-cell-coords1(center-y-coords, row-heights, thebaseline / 1fr) + bounds.at(1)
}
box(baseline: thebaseline,
Expand Down
6 changes: 3 additions & 3 deletions src/utility.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

#let if-none(a, b) = { if a != none { a } else { b } }
#let if-auto(a, b) = { if a != auto { a } else { b } }
#let is-gate(item) = { type(item) == "dictionary" and "gate-type" in item }
#let is-circuit-drawable(item) = { is-gate(item) or type(item) in ("string", "content") }
#let is-circuit-meta-instruction(item) = { type(item) == "dictionary" and "qc-instr" in item }
#let is-gate(item) = { type(item) == dictionary and "gate-type" in item }
#let is-circuit-drawable(item) = { is-gate(item) or type(item) in (str, content) }
#let is-circuit-meta-instruction(item) = { type(item) == dictionary and "qc-instr" in item }



Expand Down
4 changes: 2 additions & 2 deletions src/verifications.typ
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@

#let verify-annotation-content(annotation-content) = {
let content-type = type(annotation-content)
assert(content-type in ("content", "string", "dictionary"), message: "`annotate`: Unsupported callback return type `" + str(content-type) + "` (can be `dictionary` or `content`")
assert(content-type in (content, str, dictionary), message: "`annotate`: Unsupported callback return type `" + str(content-type) + "` (can be `dictionary` or `content`")

if content-type == "dictionary" {
if content-type == dictionary {
assert("content" in annotation-content, message: "`annotate`: Missing field `content` in annotation. If the callback returns a dictionary, it must contain the key `content` and may specify coordinates with `dx` and `dy`.")
if "z" in annotation-content {
let z = annotation-content.z
Expand Down
1 change: 1 addition & 0 deletions typst.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ entrypoint = "src/quill.typ"
authors = ["Mc-Zen <https://github.com/Mc-Zen>"]
license = "MIT"
description = "Effortlessly create quantum circuit diagrams."
compiler = "0.8.0"

repository = "https://github.com/Mc-Zen/quill"
keywords = ["quantum circuit diagram", "quantikz", "qcircuit"]
Expand Down

0 comments on commit 5353932

Please sign in to comment.