Skip to content

Commit

Permalink
fix stroke thickness calculation and add tests
Browse files Browse the repository at this point in the history
It didn't support negative numbers properly before.
  • Loading branch information
PgBiel committed Dec 31, 2023
1 parent 04cdfe6 commit eda2457
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/utilities.typ
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Typst 0.9.0 uses a minus sign ("−"; U+2212 MINUS SIGN) for negative numbers.
// Before that, it used a hyphen minus ("-"; U+002D HYPHEN MINUS), so we use
// regex alternation to match either of those.
#let NUMBER-REGEX-STRING = "(−|-)?\\d*\\.?\\d+"
#let NUMBER-REGEX-STRING = "(?:−|-)?\\d*\\.?\\d+"

// Which positions does a cell occupy
// (Usually just its own, but increases if colspan / rowspan
Expand Down Expand Up @@ -305,15 +305,11 @@
} else if is-color(stroke) {
1pt
} else if type(stroke) == _stroke-type {
// support:
// - 5
// - 5.5
let maybe-float-regex = "(?:\\d+(?:\\.\\d+)?)"
// support:
// - 2pt / 2em / 2cm / 2in + color
// - 2.5pt / 2.5em / ... + color
// - 2pt + 3em + color
let len-regex = "(?:" + maybe-float-regex + "(?:em|pt|cm|in|%)(?:\\s+\\+\\s+" + maybe-float-regex + "em)?)"
let len-regex = "(?:" + NUMBER-REGEX-STRING + "(?:em|pt|cm|in|%)(?:\\s+\\+\\s+" + NUMBER-REGEX-STRING + "em)?)"
let r = regex("^" + len-regex)
let s = repr(stroke).find(r)

Expand Down
38 changes: 37 additions & 1 deletion tablex-test.typ
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#import "src/common.typ": calc-mod, _length-type
#import "src/utilities.typ": default-if-auto, convert-length-to-pt
#import "src/utilities.typ": default-if-auto, convert-length-to-pt, stroke-len
#import "tablex.typ": *

*Test*
Expand Down Expand Up @@ -860,6 +860,42 @@ Combining em and pt (with a stroke object):
#convert-length-to-pt-test(-0.005% - 0.005pt + 0em, -0.01pt)
#convert-length-to-pt-test(-0.005% - 0.005pt - 0.005em, -0.015pt)

// Stroke thickness calculation
#let stroke-thickness-test(
value, expected,
compare-repr: false,
) = {
set text(size: 1pt) // Set 1em to 1pt
style(styles => {
let actual = stroke-len(
value,
styles: styles,
)

assert(type(actual) == _length-type)

// Re-assign so we can modify the variable
let expected = expected
if compare-repr {
expected = repr(expected)
actual = repr(actual)
}
assert(expected == actual, message: "Expected " + repr(expected) + ", found " + repr(actual))
})
}

#stroke-thickness-test(2pt, 2pt)
#stroke-thickness-test(2pt + 1em, 3pt)
#stroke-thickness-test(2pt + red, 2pt)
#stroke-thickness-test(2pt + 2em + red, 4pt)
#stroke-thickness-test(2.2pt - 2.2em + red, 0pt)
#stroke-thickness-test(0.005em + black, 0.005pt)
#stroke-thickness-test(red, 1pt)
#stroke-thickness-test((does-not-specify-thickness: 5), 1pt)
#stroke-thickness-test((thickness: 5pt + 2em, what: 55%), 7pt)
#stroke-thickness-test((thickness: 5pt + 2.005em, what: 55%), 7.005pt)
#stroke-thickness-test(rect(stroke: 2.002pt - 3.003em + red).stroke, -1.001pt, compare-repr: true)

*Line expansion - issue \#74:*

#let wrap-for-linex-expansion-test(tabx) = {
Expand Down

0 comments on commit eda2457

Please sign in to comment.