PgBiel / typst-tablex

More powerful and customizable tables in Typst
Apache License 2.0
376 stars 13 forks source link

Gradients and patterns are not allowed as cell fills #86

Closed ntjess closed 11 months ago

ntjess commented 11 months ago
#import "@preview/tablex:0.0.6": tablex, cellx

#tablex(cellx(fill: gradient.linear(red, green))[Aloha])

Raises the following error:

error: panicked with: "Tablex error: Invalid fill specified (must be either a function (column, row) -> fill, a color, an array of valid fill values, or 'none')."

However, the following diff allows compilation without any issue:

diff --git "a/tablex.typ" "b/tablex.typ"
index a3a1692..661c17e 100644
--- "a/tablex.typ"
+++ "b/tablex.typ"
@@ -17,7 +17,7 @@
 #let _array_type = type(())
 #let _dict_type = type((a: 5))
 #let _str_type = type("")
-#let _color_type = type(red)
+#let _color_types = (type(red), type(gradient.linear(red, blue)))
 #let _stroke_type = type(red + 5pt)
 #let _length_type = type(5pt)
 #let _rel_len_type = type(100% + 5pt)
@@ -462,7 +462,7 @@
         convert-length-to-pt(stroke, styles: styles)
     } else if type(stroke) in (_rel_len_type, _ratio_type) {
         panic(no-ratio-error)
-    } else if type(stroke) == _color_type {
+    } else if type(stroke) in _color_types {
         1pt
     } else if type(stroke) == _stroke_type {
         // support:
@@ -920,7 +920,7 @@
         }
     }

-    if cell_fill != none and type(cell_fill) != _color_type {
+    if cell_fill != none and type(cell_fill) not in _color_types {
         panic("Tablex error: Invalid fill specified (must be either a function (column, row) -> fill, a color, an array of valid fill values, or 'none').")
     }

@@ -1604,7 +1604,7 @@
 // -- drawing --

 #let parse-stroke(stroke) = {
-    if type(stroke) == _color_type {
+    if type(stroke) in _color_types {
         stroke + 1pt
     } else if type(stroke) in (_length_type, _rel_len_type, _ratio_type, _stroke_type, _dict_type) or stroke in (none, auto) {
         stroke

So it seems there is no inherent limitation that prevents gradient fills. Is it possible to include this in the next release?

PgBiel commented 11 months ago

Thanks for reporting! I didn't know gradient wasn't also considered a color type. Your proposed diff is a bit problematic because we target Typst 0.2.0+ (I plan on lifting that restriction on a future version), so I will think of something else.

PgBiel commented 11 months ago

I think I can work around this by creating some is-color method instead, which just checks if either the type is color_type or str(type(...)) is equal to one of ("gradient", "pattern"). I should be working on this soon.

PgBiel commented 11 months ago

Can confirm the fix is working. Should be there in 0.0.7 (which I'll likely release within a week). In the meantime, you can use tablex.typ from the 0.0.x branch. :+1: