Skip to content

Commit

Permalink
Add fill-rule style key (#722)
Browse files Browse the repository at this point in the history
* line: Add fill-rule style key

* changelog

* tests: Update ref images

* docs: Mention new fill-rule style
  • Loading branch information
johannes-wolf authored Oct 18, 2024
1 parent 36da635 commit 1661c17
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 5 deletions.
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# 0.3.1

CeTZ 0.3.1 requires Typst 0.12.0.

- Added a new `padding` parameter to the canvas element.
- Some elements now support Typst 0.12.0 `fill-rule` style.

# 0.3.0

CeTZ 0.3.0 requires Typst 0.11.0
CeTZ 0.3.0 requires Typst 0.11.0.
The licence changed from Apache-2.0 to LGPLv3.

CeTZ' plotting and charting functionality has been moved to a separate
Expand Down
4 changes: 4 additions & 0 deletions docs/basics/styling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ You can style draw elements by passing the relevant named arguments to their dra
documentation for more
details.](https://typst.app/docs/reference/visualize/line/#parameters-stroke)
</Parameter>
<Parameter name="fill-rule" types="string" default_value="&quot;non-zero&quot;">
How to fill self-intersecting paths. Can be "non-zero" or "even-odd".
[See Typst's path documentation for more details.](https://typst.app/docs/reference/visualize/path/#parameters-fill-rule)
</Parameter>



Expand Down
1 change: 1 addition & 0 deletions src/canvas.typ
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
path(
stroke: drawable.stroke,
fill: drawable.fill,
fill-rule: drawable.at("fill-rule", default: "non-zero"),
closed: drawable.at("close", default: false),
..vertices,
)
Expand Down
6 changes: 5 additions & 1 deletion src/draw/shapes.typ
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@
let drawables = drawable.path(
(path-util.line-segment(pts),),
fill: style.fill,
fill-rule: style.fill-rule,
stroke: style.stroke,
close: close
)
Expand Down Expand Up @@ -1223,6 +1224,7 @@
let drawables = drawable.path(
(path-util.cubic-segment(start, end, ..ctrl),),
fill: style.fill,
fill-rule: style.fill-rule,
stroke: style.stroke,
)

Expand Down Expand Up @@ -1325,6 +1327,7 @@
let drawables = drawable.path(
segments,
fill: style.fill,
fill-rule: style.fill-rule,
stroke: style.stroke,
close: close)

Expand Down Expand Up @@ -1402,6 +1405,7 @@
let drawables = drawable.path(
segments,
fill: style.fill,
fill-rule: style.fill-rule,
stroke: style.stroke,
close: close)

Expand Down Expand Up @@ -1492,7 +1496,7 @@
}

let style = styles.resolve(ctx.style, merge: style)
let drawables = drawable.path(fill: style.fill, stroke: style.stroke, close: close, segments)
let drawables = drawable.path(fill: style.fill, fill-rule: style.fill-rule, stroke: style.stroke, close: close, segments)

let (transform, anchors) = anchor_.setup(
name => {
Expand Down
4 changes: 3 additions & 1 deletion src/drawable.typ
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
/// - segments (array): The segments to create the path from.
/// - close (bool): If `true` the path will be closed.
/// - fill (color,none): The color to fill the path with.
/// - fill-rule (string): One of "even-odd" or "non-zero".
/// - stroke (stroke): The stroke of the path.
/// -> drawable
#let path(close: false, fill: none, stroke: none, segments) = {
#let path(close: false, fill: none, stroke: none, fill-rule: "non-zero", segments) = {
let segments = segments
// Handle case where only one segment has been passed
if type(segments.first()) == str {
Expand All @@ -58,6 +59,7 @@
close: close,
segments: segments,
fill: fill,
fill-rule: fill-rule,
stroke: stroke,
hidden: false,
bounds: true,
Expand Down
9 changes: 7 additions & 2 deletions src/styles.typ
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#let default = (
fill: none,
fill-rule: "non-zero",
stroke: black + 1pt,
radius: 1,
/// Bezier shortening mode:
Expand Down Expand Up @@ -69,11 +70,13 @@
line: (
mark: auto,
fill: auto,
fill-rule: auto,
stroke: auto,
),
bezier: (
stroke: auto,
fill: auto,
fill-rule: auto,
mark: auto,
shorten: auto,
),
Expand All @@ -82,15 +85,17 @@
mark: auto,
shorten: auto,
stroke: auto,
fill: auto
fill: auto,
fill-rule: auto,
),
hobby: (
/// Curve start and end omega (curlyness)
omega: (1,1),
mark: auto,
shorten: auto,
stroke: auto,
fill: auto
fill: auto,
fill-rule: auto,
),
rect: (
/// Rect corner radius that supports the following types:
Expand Down
Binary file added tests/line/fill-rule/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.
24 changes: 24 additions & 0 deletions tests/line/fill-rule/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#set page(width: auto, height: auto)

#import "/src/lib.typ": *
#import "/tests/helper.typ": *

#test-case({
import draw: *

line((25pt, 0pt),
(10pt, 50pt),
(50pt, 20pt),
(0pt, 20pt),
(40pt, 50pt), close: true, fill: blue, fill-rule: "non-zero")
})

#test-case({
import draw: *

line((25pt, 0pt),
(10pt, 50pt),
(50pt, 20pt),
(0pt, 20pt),
(40pt, 50pt), close: true, fill: blue, fill-rule: "even-odd")
})

0 comments on commit 1661c17

Please sign in to comment.