Skip to content

Commit

Permalink
[refactor] use context instead of styles (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mc-Zen authored Oct 15, 2024
1 parent 209ab4a commit cebb825
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 57 deletions.
6 changes: 3 additions & 3 deletions docs/guide/quill-guide.typ
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,13 @@ Let's look at an example:
let brace = math.lr($#block(height: x2 - x1)}$)
place(dx: x1, dy: y, rotate(brace, -90deg, origin: top))
let content = [Readout circuit]
style(styles => {
let size = measure(content, styles)
context {
let size = measure(content)
place(
dx: x1 + (x2 - x1) / 2 - size.width / 2,
dy: y - .6em - size.height, content
)
})
}
})
)
```
Expand Down
6 changes: 3 additions & 3 deletions docs/images/create-image.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#let scale-factor = 100%
#let content = include("/examples/composition.typ")

#style(styles => {
let size = measure(content, styles)
#context {
let size = measure(content)
rect(
stroke: none,
radius: 3pt,
Expand All @@ -17,4 +17,4 @@
)
)

})
}
16 changes: 8 additions & 8 deletions src/draw-functions.typ
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@
let draw-inouts(inouts, alignment) = {

if inouts != none and dy != 0pt {
let width = measure(line(length: gate.width), draw-params.styles).width
let width = measure(line(length: gate.width)).width
let y0 = -(dy + extent) - draw-params.center-y-coords.at(0)
let get-wire-y(qubit) = { draw-params.center-y-coords.at(qubit) + y0 }

set text(size: .8em)
style(styles => {
context {
for inout in inouts {
let size = measure(inout.label, styles)
let size = measure(inout.label)
let y = get-wire-y(inout.qubit)
let label-x = draw-params.padding
if "n" in inout and inout.n > 1 {
let y2 = get-wire-y(inout.qubit + inout.n - 1)
let brace = utility.create-brace(auto, alignment, y2 - y + draw-params.padding)
let brace-x = 0pt
let size = measure(brace, styles)
let size = measure(brace)
if alignment == right { brace-x += width - size.width }

place(brace, dy: y - 0.5 * draw-params.padding, dx: brace-x)
Expand All @@ -78,7 +78,7 @@
box(inout.label, width: width, inset: (x: label-x))
))
}
})
}

}

Expand Down Expand Up @@ -154,7 +154,7 @@

#let draw-nwire(gate, draw-params) = {
set text(size: .7em)
let size = measure(gate.content, draw-params.styles)
let size = measure(gate.content)
let extent = 2.5pt + size.height
box(height: 2 * extent, { // box is solely for height hint
place(dx: 1pt, dy: 0pt, gate.content)
Expand Down Expand Up @@ -201,7 +201,7 @@
assert(gate.data.align in (left, right), message: "`lstick`/`rstick`: Only left and right are allowed for parameter align")

let content = box(inset: draw-params.padding, gate.content)
let size = measure(content, draw-params.styles)
let size = measure(content)
let brace = none

if gate.data.brace != none {
Expand All @@ -218,7 +218,7 @@
brace = utility.create-brace(brace-symbol, gate.data.align, brace-height)
}

let brace-size = measure(brace, draw-params.styles)
let brace-size = measure(brace)
let width = size.width + brace-size.width + gate.data.pad
let height = size.height
let brace-offset-y
Expand Down
38 changes: 18 additions & 20 deletions src/layout.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
/// Update bounds to contain the given rectangle
/// - bounds (array): Current bound coordinates x0, y0, x1, y1
/// - rect (array): Bounds rectangle x0, y0, x1, y1
#let update-bounds(bounds, rect, em) = (
calc.min(bounds.at(0), convert-em-length(rect.at(0), em)),
calc.min(bounds.at(1), convert-em-length(rect.at(1), em)),
calc.max(bounds.at(2), convert-em-length(rect.at(2), em)),
calc.max(bounds.at(3), convert-em-length(rect.at(3), em)),
#let update-bounds(bounds, rect) = (
calc.min(bounds.at(0), rect.at(0).to-absolute()),
calc.min(bounds.at(1), rect.at(1).to-absolute()),
calc.max(bounds.at(2), rect.at(2).to-absolute()),
calc.max(bounds.at(3), rect.at(3).to-absolute()),
)

#let offset-bounds(bounds, offset) = (
Expand All @@ -17,11 +17,11 @@
bounds.at(3) + offset.at(1),
)

#let make-bounds(x0: 0pt, y0: 0pt, width: 0pt, height: 0pt, x1: none, y1: none, em) = (
convert-em-length(x0, em),
convert-em-length(y0, em),
convert-em-length(if x1 != none { x1 } else { x0 + width }, em),
convert-em-length(if y1 != none { y1 } else { y0 + height }, em),
#let make-bounds(x0: 0pt, y0: 0pt, width: 0pt, height: 0pt, x1: none, y1: none) = (
x0.to-absolute(),
y0.to-absolute(),
(if x1 != none { x1 } else { x0 + width }).to-absolute(),
(if y1 != none { y1 } else { y0 + height }).to-absolute(),
)


Expand Down Expand Up @@ -51,7 +51,7 @@

#let default-size-hint(item, draw-params) = {
let content = (item.draw-function)(item, draw-params)
let hint = measure(content, draw-params.styles)
let hint = measure(content)
hint.offset = auto
return hint
}
Expand All @@ -62,7 +62,7 @@

#let lrstick-size-hint(item, draw-params) = {
let content = (item.draw-function)(item, draw-params)
let hint = measure(content, draw-params.styles)
let hint = measure(content)
let dx = 0pt
if item.data.align == right { dx = hint.width }
hint.offset = (x: dx, y: auto)
Expand All @@ -86,8 +86,7 @@
/// - labels (array): An array of labels which in turn are dictionaries that must
/// specify values for the keys `content` (content), `pos` (strictly 2d
/// alignment), `dx` and `dy` (both length, ratio or relative length).
/// - draw-params (dictionary): Drawing parameters. Must contain a styles object at
/// the key `styles` and an absolute length at the key `em`.
/// - draw-params (dictionary): Drawing parameters.
/// -> pair
#let place-with-labels(
content,
Expand All @@ -97,9 +96,9 @@
labels: (),
draw-params: none,
) = {
if size == auto { size = measure(content, draw-params.styles) }
if size == auto { size = measure(content) }
let bounds = make-bounds(
x0: dx, y0: dy, width: size.width, height: size.height, draw-params.em
x0: dx, y0: dy, width: size.width, height: size.height
)
if labels.len() == 0 {
return (place(content, dx: dx, dy: dy), bounds)
Expand All @@ -108,7 +107,7 @@
let placed-labels = place(dx: dx, dy: dy,
box({
for label in labels {
let label-size = measure(label.content, draw-params.styles)
let label-size = measure(label.content)
let ldx = get-length(label.dx, size.width)
let ldy = get-length(label.dy, size.height)

Expand All @@ -123,10 +122,9 @@
let placed-label = place(label.content, dx: ldx, dy: ldy)
let label-bounds = make-bounds(
x0: ldx + dx, y0: ldy + dy,
width: label-size.width, height: label-size.height,
draw-params.em
width: label-size.width, height: label-size.height
)
bounds = update-bounds(bounds, label-bounds, draw-params.em)
bounds = update-bounds(bounds, label-bounds)
placed-label
}
})
Expand Down
6 changes: 0 additions & 6 deletions src/length-helpers.typ
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@


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

#let get-length(len, container-length) = {
if type(len) == length { return len }
if type(len) == ratio { return len * container-length}
Expand Down
28 changes: 13 additions & 15 deletions src/quantum-circuit.typ
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,16 @@
set text(color, size: font-size)
set math.equation(numbering: none)

style(styles => {
context {

// Parameter object to pass to draw-function containing current style info
let draw-params = (
wire: wire,
padding: measure(line(length: gate-padding), styles).width,
padding: measure(line(length: gate-padding)).width,
background: fill,
color: color,
styles: styles,
x-gate-size: none,
multi: (wire-distance: 0pt),
em: measure(line(length: 1em), styles).width
multi: (wire-distance: 0pt)
)

draw-params.x-gate-size = layout.default-size-hint(gate($X$), draw-params)
Expand All @@ -98,10 +96,10 @@

/////////// First part: Layout (and spacing) ///////////
let column-spacing = length-helpers.convert-em-length(column-spacing, draw-params.em)
let row-spacing = length-helpers.convert-em-length(row-spacing, draw-params.em)
let min-row-height = length-helpers.convert-em-length(min-row-height, draw-params.em)
let min-column-width = length-helpers.convert-em-length(min-column-width, draw-params.em)
let column-spacing = column-spacing.to-absolute()
let row-spacing = row-spacing.to-absolute()
let min-row-height = min-row-height.to-absolute()
let min-column-width = min-column-width.to-absolute()
// All these arrays are gonna be filled up in the loop over `items`
let matrix = ((),)
Expand Down Expand Up @@ -336,7 +334,7 @@
}
}
if decoration-bounds != none {
bounds = layout.update-bounds(bounds, decoration-bounds, draw-params.em)
bounds = layout.update-bounds(bounds, decoration-bounds)
}
if item.at("z", default: "below") == "below" { layer-below-circuit += the-content }
else { layer-above-circuit += the-content }
Expand Down Expand Up @@ -429,7 +427,7 @@
draw-params: draw-params
)
result
bounds = layout.update-bounds(bounds, gate-bounds, draw-params.em)
bounds = layout.update-bounds(bounds, gate-bounds)
}
}

Expand All @@ -445,7 +443,7 @@
dx: dx, dy: dy,
labels: gate.labels, draw-params: draw-params
)
bounds = layout.update-bounds(bounds, gate-bounds, draw-params.em)
bounds = layout.update-bounds(bounds, gate-bounds)
result
}

Expand All @@ -472,7 +470,7 @@
dx: dx, dy: dy,
labels: gate.labels, draw-params: draw-params
)
bounds = layout.update-bounds(bounds, gate-bounds, draw-params.em)
bounds = layout.update-bounds(bounds, gate-bounds)
result
}

Expand Down Expand Up @@ -504,7 +502,7 @@

let thebaseline = baseline
if type(thebaseline) in (content, str) {
thebaseline = height/2 - measure(thebaseline, styles).height/2
thebaseline = height/2 - measure(thebaseline).height/2
}
if type(thebaseline) == fraction {
thebaseline = 100% - layout.get-cell-coords1(center-y-coords, row-heights, thebaseline / 1fr) + bounds.at(1)
Expand All @@ -522,5 +520,5 @@
)))
)

})
}
}
2 changes: 1 addition & 1 deletion src/utility.typ
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if is-gate(item) {
return (item.size-hint)(item, draw-params)
}
measure(item, draw-params.styles)
measure(item)
}


Expand Down
2 changes: 1 addition & 1 deletion typst.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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"
compiler = "0.11.0"

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

0 comments on commit cebb825

Please sign in to comment.