Skip to content

Commit

Permalink
canvas: Add a padding parameter to canvas (#704)
Browse files Browse the repository at this point in the history
* canvas: Add a padding parameter to canvas

* changes: Update changelog

* changes: Update changelog
  • Loading branch information
johannes-wolf authored Oct 16, 2024
1 parent 2c3f253 commit e323b54
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.3.1

- Added a new `padding` parameter to the canvas element.

# 0.3.0

CeTZ 0.3.0 requires Typst 0.11.0
Expand Down Expand Up @@ -243,3 +247,4 @@ CeTZ requires Typst 0.8.0.

## Plot
- Added new library `plot` for drawing line charts (of functions), replacing `typst-plot`.

13 changes: 9 additions & 4 deletions src/canvas.typ
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

/// Sets up a canvas for drawing on.
///
/// - length (length,ratio): Used to specify what 1 coordinate unit is. If given a ratio, that ratio is relative to the containing elements width!
/// - body (none,array,element): A code block in which functions from the `draw` module have been called.
/// - background (none,color): A color to be used for the background of the canvas.
/// - length (length, ratio): Used to specify what 1 coordinate unit is. If given a ratio, that ratio is relative to the containing elements width!
/// - body (none, array, element): A code block in which functions from the `draw` module have been called.
/// - background (none, color): A color to be used for the background of the canvas.
/// - padding (none, number, array, dictionary) = none: How much padding to add to the canvas. `none` applies no padding. A number applies padding to all sides equally. A dictionary applies padding following Typst's `pad` function: https://typst.app/docs/reference/layout/pad/. An array follows CSS like padding: `(y, x)`, `(top, x, bottom)` or `(top, right, bottom, left)`.
/// - debug (bool): Shows the bounding boxes of each element when `true`.
/// -> content
#let canvas(length: 1cm, debug: false, background: none, body) = context { layout(ly => {
#let canvas(length: 1cm, debug: false, background: none, padding: none, body) = context { layout(ly => {
if body == none {
return []
}
Expand Down Expand Up @@ -73,6 +74,10 @@
return cmd.at("z-index", default: 0)
})

// Apply padding
let padding = util.as-padding-dict(padding)
bounds = aabb.padded(bounds, padding)

// Final canvas size
let (width, height, ..) = vector.scale(aabb.size(bounds), length)

Expand Down
Binary file added tests/padding/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tests/padding/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#set page(width: auto, height: auto)
#import "/src/lib.typ": *
#import "/tests/helper.typ": *

#test-case(padding: 1, background: gray, {
import draw: *
circle(())
})

#test-case(padding: (top: 1, left: 2), background: gray, {
import draw: *
scale(x: -1, y: -.5)
circle(())
})

0 comments on commit e323b54

Please sign in to comment.