Skip to content

Commit

Permalink
tests: Add anchor tests for all elements
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Oct 30, 2023
1 parent ba49713 commit e7eba3e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 24 deletions.
35 changes: 12 additions & 23 deletions src/anchor.typ
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,17 @@
#import "matrix.typ"
#import "vector.typ"

// #let calculate(func, anchor-names, anchor, transform: none, name: none, default: none) = {
// if anchor == () {
// return anchor-names
// }
// if anchor == "default" {
// anchor = default
// }
// assert(
// anchor in anchor-names,
// message: strfmt("Anchor '{}' not in anchors {}", anchor, repr(anchor-names)) + if name != none { strfmt(" for element '{}'", name) }
// )

// let out = func(anchor)
// return if transform != none {
// util.apply-transform(
// transform,
// out
// )
// } else {
// out
// }
// }
// Compass direction to angle
#let compass-angle = (
east: 0deg,
north-east: 45deg,
north: 90deg,
north-west: 135deg,
west: 180deg,
south-west: 225deg,
south: 270deg,
south-east: 315deg,
)

/// Setup an anchor calculation and handling function for an element. Unifies anchor error checking and calculation of the offset transform.
///
Expand Down Expand Up @@ -127,4 +116,4 @@
}
assert(pts.len() > 0, message: strfmt("{} {} {}", test-path, drawables, angle))
return pts.first()
}
}
2 changes: 1 addition & 1 deletion src/draw/shapes.typ
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@
end: pts.last(),
)
for (i, pt) in pts.enumerate() {
a.insert("pt-" + str(i + 1), pt)
a.insert("pt-" + str(i), pt)
}
anchor_.setup(
anchor => {
Expand Down
Binary file added tests/anchor/element-anchors/ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions tests/anchor/element-anchors/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#set page(width: auto, height: auto)
#import "/src/lib.typ": *

#let display(body, ..args) = {
import draw: *
(body)(..args, name: "elem");
get-ctx(ctx => {
for-each-anchor("elem", o => {
let n = "elem." + o

group({
rotate(45deg)
set-style(stroke: blue)
line((rel: (-.1, 0), to: n), (rel: (.2, 0)))
line((rel: (0, -.1), to: n), (rel: (0, .2)))
})

let (_, nv) = coordinate.resolve(ctx, n)
let (x, y, ..) = nv
let anchor = (
(if y < 0 { "north" } else if y > 0 { "south" },) +
(if x < 0 { "east" } else if x > 0 { "west" },)).filter(p => p != none).join("-")
if anchor == none {
anchor = "south"
}

content(n, text(8pt, o), anchor: anchor, padding: .2)
})
})
}

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(circle, ())
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(circle-through, (-1,0), (0,1), (1,0))
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(arc, (0,0), start: 225deg, stop: 135deg, radius: 5, mode: "OPEN")
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(arc, (0,0), start: 225deg, stop: 135deg, radius: 5, mode: "PIE")
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(line, (-1,0), (0,1), (1,0))
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(grid, (-1,-1), (1,1), step: 1)
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(content, (), text(2cm)[Text])
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(rect, (-1,-1), (1,1))
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(bezier, (-2,0), (2,0), (-1,1), (1,-1))
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(bezier, (-1,-1), (1,-1), (0,2))
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(bezier-through, (-1,-1), (0,1), (1,-1))
}))

#box(stroke: 2pt + red, canvas(length: 1cm, {
import draw: *
display(catmull, (-2,0), (-1,-1), (0,1), (1,-1), (2,0))
}))

0 comments on commit e7eba3e

Please sign in to comment.