nano-ui-0.2.0.0: test/integration/Cases/Paths.hs
{-# OPTIONS_GHC -O0 #-}
module Cases.Paths (tests) where
import Spec
import Data.Foldable (toList)
import Data.IntMap.Strict qualified as IM
import Data.Primitive.PrimArray (PrimArray, indexPrimArray, primArrayFromList, primArrayToList, sizeofPrimArray)
import GHC.Stack (HasCallStack)
import NanoUI.Internal.Context (Context (..), DrawingCacheState (..), setDrawSnapScale)
import NanoUI.Internal.Canvas (emitOp)
import NanoUI.Internal.Path (fillPathOps)
import NanoUI.Internal.Context.Types (CustomDrawOpCacheEntry (..))
import NanoUI.Path qualified as P
tests :: [Spec]
tests =
[ spec "path-curves" runCurvesTest
, spec "path-strokes" runStrokeTest
, spec "path-fill-triangulation" runFillTriangulationTest
, spec "path-transforms" runTransformTest
, spec "path-degenerate" runDegenerateTest
, spec "path-fill-holes" runFillHolesTest
, spec "path-stroke-dashes" runDashTest
, spec "path-stroke-joins" runJoinGeometryTest
, spec "path-gradients" runGradientTest
, spec "path-shapes-and-clip" runShapesAndClipTest
, spec "path-canvas-frames" runCanvasFramesTest
]
red, black, blue, green, white :: Color
red = colorRGBA 220 40 40 255
black = colorBlack
blue = colorRGBA 0 0 255 255
green = colorRGBA 0 255 0 255
white = colorWhite
-- | A canvas block's ops, flattened at @scale@ device pixels per logical pixel.
opsAt :: Float -> CanvasM () -> [DrawOp]
opsAt scale = toList . runCanvasFor (CustomDrawContext False False False False False defaultTheme (monospaceMetrics 16) {fmSnapScale = scale})
pairs :: PrimArray Float -> [V2]
pairs pts = [V2 (indexPrimArray pts (2 * i)) (indexPrimArray pts (2 * i + 1)) | i <- [0 .. sizeofPrimArray pts `div` 2 - 1]]
-- | Each stroke's points and whether it closes.
strokes :: [DrawOp] -> [([V2], Bool)]
strokes ops = [(pairs pts, closed) | StrokePolyline pts _ closed _ _ _ _ <- ops]
-- | Each filled polygon's outline and triangles.
fills :: [DrawOp] -> [([V2], [Int])]
fills ops = [(pairs pts, primArrayToList tris) | FillPolygon pts _ tris _ <- ops]
-- | A path's strokes (width 1) and fills, at scale 1.
stroked :: P.Path -> [([V2], Bool)]
stroked path = strokes (opsAt 1 (drawStrokePath path 1 black))
filled :: P.Path -> [([V2], [Int])]
filled path = fills (opsAt 1 (drawPath path red))
-- | The ops of a stroke with this style and a solid paint.
styledStroke :: P.Stroke -> P.Path -> [DrawOp]
styledStroke st path = opsAt 1 (drawStrokePathWith st path (P.Solid black))
-- | Check the single element; fail if there is not exactly one.
single :: HasCallStack => IORef Int -> [a] -> (a -> IO ()) -> IO ()
single failed xs k = case xs of [x] -> k x; _ -> assertEq failed 1 (length xs)
-- | Require points pairwise within @eps@, showing both lists otherwise.
assertNear :: HasCallStack => IORef Int -> Float -> [V2] -> [V2] -> IO ()
assertNear failed eps want got = unless (length want == length got && and (zipWith (near eps) want got)) (assertEq failed want got)
dist :: V2 -> V2 -> Float
dist (V2 x0 y0) (V2 x1 y1) = sqrt ((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0))
near :: Float -> V2 -> V2 -> Bool
near eps a b = dist a b <= eps
xsOf, ysOf :: [V2] -> [Float]
xsOf pts = [x | V2 x _ <- pts]
ysOf pts = [y | V2 _ y <- pts]
-- | A line's first and last points.
ends :: [V2] -> [V2]
ends pts = take 1 pts ++ drop (length pts - 1) pts
triples :: [a] -> [(a, a, a)]
triples (a : b : c : rest) = (a, b, c) : triples rest
triples _ = []
-- | Distance from a point to an open polyline.
polyDist :: [V2] -> V2 -> Float
polyDist ps p@(V2 px py) = minimum (zipWith seg ps (drop 1 ps))
where
seg (V2 ax ay) (V2 bx by) =
let (dx, dy) = (bx - ax, by - ay)
t = max 0 (min 1 (((px - ax) * dx + (py - ay) * dy) / max 1e-12 (dx * dx + dy * dy)))
in dist p (V2 (ax + t * dx) (ay + t * dy))
-- | Signed shoelace area, positive for a ring that is clockwise on screen.
polyArea :: [V2] -> Float
polyArea ps = sum [x0 * y1 - x1 * y0 | (V2 x0 y0, V2 x1 y1) <- zip ps (drop 1 ps ++ take 1 ps)] / 2
-- | Total triangle area, and whether the index list is well formed.
covered :: [V2] -> [Int] -> (Float, Bool)
covered ps tris = (sum [area (ps !! a, ps !! b, ps !! c) | (a, b, c) <- triples tris], all (\i -> i >= 0 && i < length ps) tris && length tris `mod` 3 == 0)
where
area (V2 x0 y0, V2 x1 y1, V2 x2 y2) = abs ((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)) / 2
-- | Deterministic pseudo-random number in [0, 1).
rnd :: Int -> Float
rnd k = let v = sin (fromIntegral k * 12.9898) * 43758.5453 in v - fromIntegral (floor v :: Int)
-- | Evaluate the Bezier curve with these control points at @t@.
bezier :: [V2] -> Float -> V2
bezier [p] _ = p
bezier ps t = bezier (zipWith (\(V2 x0 y0) (V2 x1 y1) -> V2 (x0 + t * (x1 - x0)) (y0 + t * (y1 - y0))) ps (drop 1 ps)) t
-- | Whether a point lies on the axis-aligned ellipse centred at @c@ with radii @rx@ and @ry@.
onEllipse :: V2 -> Float -> Float -> V2 -> Bool
onEllipse (V2 cx cy) rx ry (V2 x y) = abs (((x - cx) / rx) ^ (2 :: Int) + ((y - cy) / ry) ^ (2 :: Int) - 1) < 1e-3
-- | Curves flatten to within a quarter device pixel at every scale (and not
-- much finer), keeping their ends; SVG arc flags pick the side and size.
runCurvesTest :: Context -> IORef Int -> IO ()
runCurvesTest _ failed = do
let centre = V2 100 100
ring scale = strokes (opsAt scale (drawStrokePath (P.circle centre 50) 1 black))
count scale = sum [length pts | (pts, _) <- ring scale]
forM_ [1, 2, 3] $ \scale -> single failed (ring scale) $ \(pts, closed) -> do
let tol = 0.25 / scale
sags = [50 - dist centre (V2 ((ax + bx) / 2) ((ay + by) / 2)) | (V2 ax ay, V2 bx by) <- zip pts (drop 1 pts ++ take 1 pts)]
assert failed (closed && all (\p -> abs (dist centre p - 50) < 1e-3) pts && all (<= tol + 1e-3) sags)
assertGt failed (maximum sags) (tol / 2)
assert failed (count 3 > count 2 && count 2 > count 1)
let (p0, c1, c2, q, p3) = (V2 10 10, V2 400 (-100), V2 (-200) 300, V2 150 (-200), V2 300 300)
samples = [fromIntegral k / 2000 | k <- [0 .. 2000 :: Int]]
forM_ [(s, c) | s <- [1, 2], c <- [(P.cubicTo c1 c2 p3, [p0, c1, c2, p3]), (P.quadTo q p3, [p0, q, p3])]] $ \(scale, (seg, ctrl)) ->
single failed (strokes (opsAt scale (drawStrokePath (P.moveTo p0 <> seg) 1 black))) $ \(pts, closed) -> do
assertEq failed (False, [p0, p3]) (closed, ends pts)
assertLt failed (maximum (map (polyDist pts . bezier ctrl) samples)) (0.25 / scale + 1e-3)
-- Every emitted point lies on the curve.
assert failed (all (\p -> minimum (map (dist p . bezier ctrl) samples) < 1) pts)
-- A straight "curve" is one chord.
single failed (stroked (P.moveTo p0 <> P.cubicTo (V2 20 10) (V2 30 10) (V2 40 10))) $ \(pts, _) -> assertEq failed [p0, V2 40 10] pts
let c = V2 50 50
onCircle a = V2 (50 + 30 * cos a) (50 + 30 * sin a)
svgArc radius large clockwise = map fst (stroked (P.moveTo (V2 0 0) <> P.arcTo (V2 radius radius) 0 large clockwise (V2 20 0)))
onSmallCircle = all (\p -> abs (dist (V2 10 0) p - 10) < 1e-3)
single failed (stroked (P.arc c 30 0.3 1.2)) $ \(pts, closed) -> do
assert failed (not closed)
assertNear failed 1e-3 [onCircle 0.3, onCircle 1.5] (ends pts)
-- A negative sweep turns the other way.
single failed (stroked (P.arc c 30 0.3 (-1.2))) $ \(pts, _) -> do
assertNear failed 1e-3 [onCircle (-0.9)] (drop 1 (ends pts))
assert failed (all (<= 50 + 30 * sin 0.3 + 1e-3) (ysOf pts))
-- An arc after a moveTo draws a line to the arc's start (a pie slice).
single failed (filled (P.moveTo c <> P.arc c 30 0 (pi / 2) <> P.close)) $ \(pts, _) -> do
assertEq failed [c] (take 1 pts)
assertLt failed (abs (abs (polyArea pts) - 30 * 30 * pi / 4)) 30
-- Clockwise goes over the top; small radii scale up to span the ends; the
-- large arc bulges further; a zero radius is a line.
single failed (svgArc 10 False True) $ \pts -> do
assertEq failed [V2 0 0, V2 20 0] (ends pts)
assert failed (onSmallCircle pts && minimum (ysOf pts) < -9.7)
single failed (svgArc 10 False False) $ \pts -> assertGt failed (maximum (ysOf pts)) 9.7
single failed (svgArc 2 False True) $ assert failed . onSmallCircle
single failed (svgArc 20 True True) $ \pts -> assertLt failed (minimum (ysOf pts)) (-37)
single failed (svgArc 20 False True) $ \pts -> assertGt failed (minimum (ysOf pts)) (-2.7)
single failed (svgArc 0 False True) $ assertEq failed [V2 0 0, V2 20 0]
-- An elliptical arc follows the ellipse's rotation.
single failed (stroked (P.ellipticalArc c (V2 40 20) (pi / 2) 0 pi)) $ \(pts, _) -> do
assertNear failed 1e-3 [V2 50 90, V2 50 10] (ends pts)
assert failed (all (onEllipse c 20 40) pts)
assertLt failed (abs (minimum (xsOf pts) - 30)) 0.3
-- | Open and closed strokes, subpaths, and line caps.
runStrokeTest :: Context -> IORef Int -> IO ()
runStrokeTest _ failed = do
let (a, b, c) = (V2 0 0, V2 10 0, V2 10 10)
plain ops = [(primArrayToList pts, w, closed) | StrokePolyline pts w closed _ _ _ _ <- ops]
line w path = plain (opsAt 1 (drawStrokePath path w black))
capped cap = styledStroke (P.stroke 4) {P.strokeCap = cap}
caps ops = [cap | StrokePolyline _ _ _ cap _ _ _ <- ops]
assertEq failed [([0, 0, 10, 0, 10, 10], 3, False)] (line 3 (P.polyline [a, b, c]))
-- Closing drops a last point that repeats the first.
assertEq failed [([0, 0, 10, 0, 10, 10], 2, True)] (line 2 (P.moveTo a <> P.lineTo b <> P.lineTo c <> P.lineTo a <> P.close))
-- Returning to the start without closing leaves the line open.
assertEq failed [([0, 0, 10, 0, 10, 10, 0, 0], 2, False)] (line 2 (P.moveTo a <> P.lineTo b <> P.lineTo c <> P.lineTo a))
-- Each subpath is a separate polyline. A close returns to the subpath's start.
assertEq failed [([0, 0, 10, 0, 10, 10], 1, True), ([0, 0, 0, 20], 1, False)] (line 1 (P.polygon [a, b, c] <> P.lineTo (V2 0 20)))
assertEq failed 2 (length (opsAt 1 (drawStrokePath (P.polyline [a, b] <> P.polyline [c, V2 20 20]) 1 black)))
-- Caps are carried on the polyline op. A closed subpath has none, and a
-- closed two-point subpath becomes an open line with butt caps.
forM_ [P.ButtCap, P.SquareCap, P.RoundCap] $ \cap -> do
let ops = capped cap (P.polyline [a, b])
assertEq failed ([([0, 0, 10, 0], 4, False)], [cap]) (plain ops, caps ops)
assertEq failed [([0, 0, 10, 0, 10, 10], 4, True)] (plain (capped P.RoundCap (P.polygon [a, b, c])))
assertEq failed [P.ButtCap] (caps (capped P.RoundCap (P.moveTo a <> P.lineTo b <> P.close)))
-- A fill closes an open subpath.
assertEq failed [([a, b, c], [0, 1, 2])] (filled (P.polyline [a, b, c]))
-- | Each subpath fills as one polygon whose triangles cover exactly its area, in either winding.
runFillTriangulationTest :: Context -> IORef Int -> IO ()
runFillTriangulationTest _ failed = do
let c = V2 60 60
-- @k@ corners around the centre; corner @i@ is at distance @rad i@.
ring k rad = [V2 (60 + rad i * cos a) (60 + rad i * sin a) | i <- [0 .. k - 1 :: Int], let a = 2 * pi * fromIntegral i / fromIntegral k]
star = ring 10 (\i -> if even i then 50 else 20)
ell = [V2 0 0, V2 60 0, V2 60 20, V2 20 20, V2 20 60, V2 0 60]
comb = [V2 0 0, V2 100 0, V2 100 40, V2 80 40, V2 80 10, V2 60 10, V2 60 40, V2 40 40, V2 40 10, V2 20 10, V2 20 40, V2 0 40]
shapes =
[ ("circle", P.circle c 50), ("ellipse", P.ellipse c (V2 50 20)), ("rounded rect", P.roundedRect (Rect 0 0 100 60) 12)
, ("hexagon", P.polygon (ring 6 (const 40))), ("star", P.polygon star), ("star anticlockwise", P.polygon (reverse star))
, ("L", P.polygon ell), ("comb", P.polygon comb)
, ("band", P.arc c 50 0 (1.5 * pi) <> P.arc c 25 (1.5 * pi) (-1.5 * pi) <> P.close)
, ("blob", P.moveTo (V2 0 0) <> P.cubicTo (V2 60 (-30)) (V2 100 30) (V2 100 60) <> P.quadTo (V2 50 100) (V2 0 60) <> P.close)
-- A one-slice pie chart: out to the rim and back along the same radius.
, ("whole pie", P.moveTo c <> P.arc c 50 (-pi / 2) (2 * pi) <> P.close)
, ("pie over half", P.moveTo c <> P.arc c 50 0.3 (1.6 * pi) <> P.close)
]
-- Random simple star polygons with 5 to 64 corners.
<> [("random star " <> show seed, P.polygon (ring (5 + seed `mod` 60) (\i -> 5 + 50 * rnd (seed * 1000 + i)))) | seed <- [1 .. 60 :: Int]]
-- Large rings, where ear clipping looks up blockers in its grid:
-- stars, and an area chart's top edge closed along a baseline.
<> [("random star of " <> show k, P.polygon (ring k (\i -> 5 + 50 * rnd (k + i)))) | k <- [300, 2000]]
<> [("area of 2000 samples", P.polygon ([V2 (fromIntegral i * 0.05) (10 + 30 * rnd (7 * i)) | i <- [0 .. 1999 :: Int]] <> [V2 99.95 60, V2 0 60]))]
forM_ shapes $ \(name, shape) ->
assertEq failed (name, [True]) . (,) name $
[ inRange && length tris == 3 * (length pts - 2) && abs (area - want) <= 1e-3 * want
| (pts, tris) <- filled shape
, let (area, inRange) = covered pts tris
want = abs (polyArea pts)
]
-- Filled area matches the shape: exactly for straight sides, within tolerance for curves.
let areaOf shape = sum [abs (polyArea pts) | (pts, _) <- filled shape]
assertLt failed (abs (areaOf (P.polygon ell) - 2000)) 1e-3
assertLt failed (abs (areaOf (P.polygon comb) - 2800)) 1e-3
assertLt failed (abs (areaOf (P.circle c 50) - pi * 50 * 50)) (2 * pi * 50 * 0.25)
assertLt failed (abs (areaOf (P.roundedRect (Rect 0 0 100 60) 12) - (6000 - (4 - pi) * 144))) (2 * pi * 12 * 0.25)
-- Disjoint subpaths fill as separate polygons. Under the non-zero rule, a
-- subpath inside another with the same winding merges into it.
assertEq failed [2, 1] [length (filled (P.rect r0 <> P.rect r1)) | (r0, r1) <- [(Rect 0 0 10 10, Rect 20 0 10 10), (Rect 0 0 100 100, Rect 20 20 10 10)]]
-- | Transforms compose inside out, apply before flattening, and scale stroke
-- widths. Non-path canvas ops follow a transform as far as their shape allows.
runTransformTest :: Context -> IORef Int -> IO ()
runTransformTest _ failed = do
let tri = [V2 1 0, V2 2 0, V2 2 1]
triangle = drawPath (P.polygon tri) red
outline block = concat [pts | (pts, _) <- fills (opsAt 1 block)]
at = map . P.transformPoint
-- Rotate a quarter turn, (x, y) to (-y, x), then scale.
rotatedThenScaled = [V2 (-2 * y) (3 * x) | V2 x y <- tri]
points block = sum [length pts | (pts, _) <- strokes (opsAt 1 block)]
circle r = drawStrokePath (P.circle (V2 0 0) r) 1 black
widths t w = [w' | StrokePolyline _ w' _ _ _ _ _ <- opsAt 1 (withTransform t (drawStrokePath (P.polyline [V2 0 0, V2 1 1]) w black))]
forM_
[ (rotatedThenScaled, outline (withTransform (P.scale 2 3) (withTransform (P.rotate (pi / 2)) triangle)))
, (rotatedThenScaled, outline (withTransform (P.scale 2 3 <> P.rotate (pi / 2)) triangle))
, ([V2 (-3 * y) (2 * x) | V2 x y <- tri], outline (withTransform (P.rotate (pi / 2) <> P.scale 2 3) triangle))
, (rotatedThenScaled, at (P.scale 2 3 <> P.rotate (pi / 2)) tri)
, ([V2 9 9], at (P.rotateAround (V2 9 9) 1.1) [V2 9 9]), ([V2 13 4], at (P.translate 3 4 <> P.affine 1 0 0 1 10 0) [V2 0 0])
, (tri, outline (withTransform mempty triangle))
, -- A transform applies only inside its block.
(at (P.translate 5 0) tri <> tri, outline (withTransform (P.translate 5 0) triangle >> triangle))
]
$ uncurry (assertNear failed 1e-4)
-- Flattening uses the transformed size.
assertGt failed (points (withTransform (P.scale 10 10) (circle 10))) (points (circle 10))
assertEq failed (points (circle 100)) (points (withTransform (P.scale 10 10) (circle 10)))
assertEq failed (points (circle 100)) (points (withTransform (P.scale 10 1) (circle 10)))
-- A stroke's width scales with the transform.
assertEq failed ([6], [4]) (widths (P.scale 3 3) 2, widths (P.scale 8 2) 1)
let under t block = opsAt 1 (withTransform t block)
r0 = Rect 10 20 10 20
corners = [[tl, tr, br, bl] | [FillQuadGradient _ tl tr br bl] <- [under (P.rotate pi) (drawQuadGradient (Rect 0 0 10 10) red green blue white)]]
-- If the block is a single rotated image: its centre, size, angle and UVs.
turned block = [([V2 (x + w / 2) (y + h / 2), V2 w h], angle, (u0, v0, u1, v1)) | [DrawImage (Rect x y w h) angle _ u0 v0 u1 v1 _] <- [block], angle /= 0]
turnedImage r angle = drawImageWith (imageDraw r (ImageId 7)) {imageAngle = angle, imageTint = red}
hi = drawText (V2 4 0) AlignCenter AlignMiddle "hi" red
-- A flip keeps rect sizes positive and mirrors gradients and images.
assertEq failed [] . map fst . filter (not . snd) . zip [0 :: Int ..] $
[ [FillRect (Rect 15 25 10 20) red] == under (P.translate 5 5) (drawRect r0 red)
, [FillRect (Rect (-20) 20 10 20) red] == under (P.scale (-1) 1) (drawRect r0 red)
, [FillRoundedRect (Rect 20 40 20 40) 8 red] == under (P.scale 2 2) (drawRoundedRect r0 4 red)
, [DrawText 9 5 0.5 0.5 "hi" red] == under (P.translate 5 5) hi
-- A rotation leaves text as plain 'DrawText'; a scale scales the text.
, case under (P.rotate 0.3) hi of [DrawText {}] -> True; _ -> False
, [DrawTextAligned 17 5 0.5 0.5 3 defaultTextFont "hi" red] == under (P.translate 5 5 <> P.scale 3 3) hi
, [DrawTextAligned 17 5 0 1 6 defaultTextFont {textFontSize = 12} "hi" red]
== under (P.translate 5 5 <> P.scale 3 3) (withTransform (P.scale 2 2) (drawTextWith defaultTextFont {textFontSize = 12} (V2 2 0) AlignStart AlignTop "hi" red))
, [FillQuadGradient (Rect (-10) 0 10 10) blue red red blue] == under (P.scale (-1) 1) (drawLinearGradientH (Rect 0 0 10 10) red blue)
, [FillQuadGradient (Rect 0 (-10) 10 10) blue blue red red] == under (P.scale 1 (-1)) (drawLinearGradientV (Rect 0 0 10 10) red blue)
, [DrawImageRect (Rect (-10) 0 10 10) 7 1 0 0 1 red] == under (P.scale (-1) 1) (drawImage (Rect 0 0 10 10) (ImageId 7) red)
-- A rect of negative size draws as it does untransformed: a quad
-- gradient not at all, an image mirrored.
, null (under (P.translate 5 0) (drawQuadGradient (Rect 50 0 (-20) 10) red green blue white))
, [DrawImageRect (Rect 25 0 40 40) 7 1 0 0 1 red] == under (P.translate 5 0) (drawImage (Rect 60 0 (-40) 40) (ImageId 7) red)
, [DrawImageRect (Rect (-60) 0 40 40) 7 0 0 1 1 red] == under (P.scale (-1) 1) (drawImage (Rect 60 0 (-40) 40) (ImageId 7) red)
-- A half turn rotates a gradient's corner colours, and so does a
-- quarter turn of a linear gradient.
, corners == [[blue, white, red, green]]
, [[red, red, blue, blue]] == [[tl, tr, br, bl] | [FillQuadGradient _ tl tr br bl] <- [under (P.rotate (pi / 2)) (drawLinearGradientH (Rect 0 0 10 10) red blue)]]
-- A quarter turn would move the quad's diagonal, which changes a blend
-- of four colours, so that quad becomes a polygon with its corner colours.
, [[red, green, blue, white]]
== [map Color (primArrayToList cs) | [FillPolygon _ _ _ (Shaded cs)] <- [under (P.rotate (pi / 2)) (drawQuadGradient (Rect 0 0 10 10) red green blue white)]]
]
-- A quarter turn keeps rects as rects.
assertNear failed 1e-4 [V2 (-20) 0, V2 20 10] $
concat [[V2 x y, V2 w h] | [FillRect (Rect x y w h) col] <- [under (P.rotate (pi / 2)) (drawRect (Rect 0 0 10 20) red)], col == red]
assertNear failed 1e-4 [V2 2 0, V2 2 20, V2 4 0] $
concat [[V2 x0 y0, V2 x1 y1, V2 w 0] | [StrokeLineAA x0 y0 x1 y1 w col] <- [under (P.translate 2 0 <> P.rotate (pi / 2) <> P.scale 4 4) (drawStrokeAA (V2 0 0) (V2 5 0) 1 red)], col == red]
-- A rect at any other angle becomes a polygon of the same area.
single failed (fills (under (P.rotateAround (V2 15 30) (pi / 5)) (drawRect r0 red))) $ \(pts, tris) -> do
assertEq failed 4 (length pts)
assertLt failed (abs (fst (covered pts tris) - 200)) 1e-2
assertNear failed 1e-3 [V2 15 30] [V2 (sum (xsOf pts) / 4) (sum (ysOf pts) / 4)]
-- A circle stays a circle under rotation and uniform scale; a non-uniform scale makes an ellipse.
assertNear failed 1e-4 [V2 20 0, V2 10 0] $
concat [[V2 x y, V2 r 0] | [FillCircle x y r _] <- [under (P.rotate (pi / 2) <> P.scale 2 2) (drawCircle (V2 0 (-10)) 5 red)]]
single failed (fills (under (P.scale 2 1) (drawCircle (V2 10 10) 5 red))) $ assert failed . all (onEllipse (V2 20 10) 10 5) . fst
single failed (strokes (under (P.scale 1 2) (drawStrokeCircle (V2 0 0) 10 2 red))) $ \(pts, closed) ->
assert failed (closed && all (onEllipse (V2 0 0) 9 18) pts)
-- A rotation rotates an image, adding to its own angle; a flip mirrors it.
forM_
[ (under (P.translate 30 0 <> P.rotate (pi / 2)) (drawImage (Rect 0 0 20 10) (ImageId 7) red), [V2 25 10, V2 20 10], pi / 2, (0, 0, 1, 1))
, (under (P.rotate 0.3 <> P.scale 2 2) (turnedImage (Rect 0 0 10 10) 0.2), [V2 (10 * cos 0.3 - 10 * sin 0.3) (10 * sin 0.3 + 10 * cos 0.3), V2 20 20], 0.5, (0, 0, 1, 1))
, (under (P.scale (-1) 1) (turnedImage (Rect 0 0 10 10) 0.2), [V2 (-5) 5, V2 10 10], pi - 0.2, (0, 1, 1, 0))
]
$ \(block, want, wantAngle, uvs) -> single failed (turned block) $ \(got, angle, uv) -> do
assertNear failed 1e-3 want got
assert failed (abs (angle - wantAngle) < 1e-5 && uv == uvs)
-- | Degenerate paths draw nothing, or only their valid parts. Output never
-- contains NaN or infinity, and point counts stay bounded.
runDegenerateTest :: Context -> IORef Int -> IO ()
runDegenerateTest _ failed = do
let nan = 0 / 0 :: Float
inf = 1 / 0 :: Float
p = V2 5 5
numbersIn op = case op of
FillPolygon pts _ _ _ -> primArrayToList pts
StrokePolyline pts w _ _ _ _ _ -> w : primArrayToList pts
FillRect (Rect x y w h) _ -> [x, y, w, h]
FillCircle x y r _ -> [x, y, r]
_ -> []
sound ops = all (\v -> not (isNaN v || isInfinite v)) (concatMap numbersIn ops)
both path = opsAt 1 (drawPath path red >> drawStrokePath path 2 black)
lines' = map fst . stroked
segment w = opsAt 1 (drawStrokePath (P.polyline [V2 0 0, V2 10 0]) w black)
-- Zero lengths and radii, collinear points, NaNs, bad widths and NaN transforms draw nothing.
assertEq failed [] . map fst . filter (not . null . snd) . zip [0 :: Int ..] $
[ both (P.moveTo p <> P.lineTo p <> P.lineTo p), both (P.moveTo p <> P.lineTo p <> P.close), both (P.circle p 0)
, both (P.arc p 0 0 pi), both (P.moveTo p), both mempty, both (P.polygon [])
, opsAt 1 (drawPath (P.polygon [V2 0 0, V2 5 0, V2 10 0]) red)
, both (P.circle p nan), both (P.arc p 10 0 nan), segment nan, segment 0, segment (-1)
, opsAt 1 (withTransform (P.scale nan 1) (drawPath (P.circle p 5) red >> drawRect (Rect 0 0 5 5) red >> drawCircle p 3 red))
, opsAt 1 (withTransform (P.rotate inf) (drawPath (P.circle p 5) red))
]
-- A zero-length subpath is a dot with round or square caps, and a lone
-- move draws nothing.
let capped cap path = [(pairs pts, c) | StrokePolyline pts _ _ c _ _ _ <- styledStroke (P.stroke 2) {P.strokeCap = cap} path]
forM_ [P.RoundCap, P.SquareCap] $ \cap ->
forM_ [P.moveTo p <> P.lineTo p, P.moveTo p <> P.close, P.circle p 0] $ \path ->
single failed (capped cap path) $ \(ps, c) -> do
assertEq failed cap c
assert failed (case ps of [a, b] -> a == p && dist a b > 0 && dist a b < 0.01; _ -> False)
assertEq failed [] (capped P.ButtCap (P.moveTo p <> P.lineTo p))
assertEq failed [[V2 0 0, V2 10 0]] (map fst (capped P.RoundCap (P.moveTo p <> P.moveTo (V2 0 0) <> P.lineTo (V2 10 0) <> P.moveTo p)))
-- Repeated points are dropped, and segments with NaN or infinity are skipped.
assertEq failed [[V2 0 0, V2 10 0, V2 10 10]] (lines' (P.polyline [V2 0 0, V2 0 0, V2 10 0, V2 10 0, V2 10 10]))
assertEq failed [[V2 0 0, V2 10 0]] (lines' (P.polyline [V2 0 0, V2 nan 3, V2 inf 0, V2 10 0]))
assertEq failed [[V2 0 0, V2 10 0]] (lines' (P.moveTo (V2 0 0) <> P.cubicTo (V2 nan 0) (V2 1 1) (V2 2 2) <> P.arcTo (V2 inf 1) 0 False False (V2 3 3) <> P.lineTo (V2 10 0)))
-- Huge shapes get a bounded point count; a sweep of many turns draws one turn.
forM_ [P.circle p 1e7, P.moveTo (V2 0 0) <> P.cubicTo (V2 1e9 0) (V2 (-1e9) 1e9) (V2 1e9 1e9)] $ \path ->
let n = sum (map length (lines' path)) in assert failed (n > 4 && n <= 1025)
assert failed (opsAt 1 (drawPath (P.moveTo (V2 30 0) <> P.arc (V2 0 0) 30 0 (2 * pi)) red) == opsAt 1 (drawPath (P.moveTo (V2 30 0) <> P.arc (V2 0 0) 30 0 1e9) red))
assert failed (opsAt 1 (drawPath (P.circle (V2 0 0) (-30)) red) == opsAt 1 (drawPath (P.circle (V2 0 0) 30) red))
-- An arc whose radius dwarfs its chord becomes a line, and the subpath continues.
forM_ [1e7, 1e9, 1e12, 1e20] $ \radius ->
assertEq failed [[V2 0 0, V2 20 0, V2 20 20]] (lines' (P.moveTo (V2 0 0) <> P.arcTo (V2 radius radius) 0 False True (V2 20 0) <> P.lineTo (V2 20 20)))
assertEq failed [2] (map length (lines' (P.arc (V2 0 1e8) 1e8 (-pi / 2) 1e-6)))
-- A full circle closes exactly at its start, with no sliver edge at the seam.
forM_ [(V2 0 0, 3000), (V2 123.4 (-56.7), 777)] $ \(centre, radius) ->
single failed (strokes (opsAt 2 (drawStrokePath (P.circle centre radius) 1 black))) $ \(pts, closed) -> do
assert failed closed
assertGt failed (minimum (zipWith dist pts (drop 1 pts ++ take 1 pts))) 1
-- An invalid display scale flattens as scale 1.
forM_ [0, -2, nan, inf] $ \scale -> assert failed (opsAt 1 (drawPath (P.circle p 40) red) == opsAt scale (drawPath (P.circle p 40) red))
-- A self-intersecting pentagram fills partially, with valid output.
let crossing = opsAt 1 (drawPath (P.polygon [V2 (50 * cos a) (50 * sin a) | i <- [0 .. 4 :: Int], let a = 4 * pi * fromIntegral i / 5]) red)
assert failed (sound crossing && all (\(pts, tris) -> snd (covered pts tris)) (fills crossing))
assert failed (sound (both (P.polyline [V2 0 0, V2 nan nan, V2 1e30 1e30, V2 (-1e30) 0])))
-- | Points, ring starts and triangles of each filled polygon under a fill rule.
polygons :: P.FillRule -> P.Path -> [([V2], [Int], [Int])]
polygons rule path = [(pairs pts, primArrayToList rings, primArrayToList tris) | FillPolygon pts rings tris _ <- opsAt 1 (drawPathWith rule path (P.Solid red))]
-- | Area covered by a polygon's triangles, or 'Nothing' if an index is out of range.
coverage :: ([V2], [Int], [Int]) -> Maybe Float
coverage (pts, _, tris) = let (a, ok) = covered pts tris in if ok then Just a else Nothing
-- | A square outline, clockwise on screen ('square') or anticlockwise ('squareBack').
square, squareBack :: Float -> Float -> Float -> P.Path
square x y s = P.polygon [V2 x y, V2 (x + s) y, V2 (x + s) (y + s), V2 x (y + s)]
squareBack x y s = P.polygon [V2 x y, V2 x (y + s), V2 (x + s) (y + s), V2 (x + s) y]
-- | Whether any triangle covers a point.
coversPoint :: ([V2], [Int], [Int]) -> V2 -> Bool
coversPoint (pts, _, tris) p = any inside [(pts !! a, pts !! b, pts !! c) | (a, b, c) <- triples tris]
where
side (V2 x0 y0) (V2 x1 y1) (V2 x y) = (x1 - x0) * (y - y0) - (y1 - y0) * (x - x0)
inside (a, b, c) = let ds = [side a b p, side b c p, side c a p] in all (> 0) ds || all (< 0) ds
-- | A nested subpath is a hole when the fill rule leaves it unfilled, and
-- the triangles cover exactly the filled area.
runFillHolesTest :: Context -> IORef Int -> IO ()
runFillHolesTest _ failed = do
let c = V2 60 60
-- A circle wound the other way.
circleBack r = P.moveTo (V2 (60 + r) 60) <> P.arc c r 0 (-2 * pi) <> P.close
areaNear want got = abs (got - want) <= 1e-3 * want
check :: String -> P.FillRule -> P.Path -> [Float] -> IO ()
check name rule path wantAreas =
let got = map coverage (polygons rule path)
in unless (length got == length wantAreas && and (zipWith (\w a -> maybe False (areaNear w) a) wantAreas got)) $
assertEq failed (name, map Just wantAreas) (name, got)
nestedSquare k = square (5 * fromIntegral k) (5 * fromIntegral k) (200 - 10 * fromIntegral k)
nestedBack k = (if even k then square else squareBack) (5 * fromIntegral k) (5 * fromIntegral k) (200 - 10 * fromIntegral k)
bands = [s * s - (s - 10) * (s - 10) | j <- [0 .. 9 :: Int], let s = 200 - 20 * fromIntegral j]
cells = foldMap (\k -> square (10 * fromIntegral (k `mod` 10)) (10 * fromIntegral (k `div` 10)) 5) [0 .. 99 :: Int]
forM_
-- Holes by rule and winding, islands, crossings, repeated loops, and
-- many subpaths each finding its parent.
[ ("even-odd hole", P.EvenOdd, square 0 0 100 <> square 20 20 60, [6400])
, ("non-zero hole", P.NonZero, square 0 0 100 <> squareBack 20 20 60, [6400])
, ("non-zero same way", P.NonZero, square 0 0 100 <> square 20 20 60, [10000])
, ("two holes", P.EvenOdd, square 0 0 100 <> square 10 10 30 <> squareBack 60 10 30, [8200])
, ("island", P.EvenOdd, square 0 0 100 <> square 20 20 60 <> square 40 40 20, [6400, 400])
, ("island non-zero", P.NonZero, square 0 0 100 <> squareBack 20 20 60 <> square 40 40 20, [6400, 400])
, ("crossing", P.EvenOdd, square 0 0 60 <> square 40 40 60, [3600, 3600])
, ("same loop twice", P.EvenOdd, square 0 0 50 <> square 0 0 50, [])
, ("same loop three times", P.EvenOdd, square 0 0 50 <> square 0 0 50 <> square 0 0 50, [2500])
, ("loop and its reverse", P.NonZero, square 0 0 50 <> squareBack 0 0 50, [])
, ("same loop twice, non-zero", P.NonZero, square 0 0 50 <> square 0 0 50, [2500])
, ("copies inside a square", P.EvenOdd, square 0 0 100 <> square 20 20 60 <> square 20 20 60, [6400, 3600])
, ("20 nested, even-odd", P.EvenOdd, foldMap nestedSquare [0 .. 19 :: Int], bands)
, ("20 nested, non-zero alternating", P.NonZero, foldMap nestedBack [0 .. 19 :: Int], bands)
, ("20 nested, non-zero same way", P.NonZero, foldMap nestedSquare [0 .. 19 :: Int], [40000])
, ("100 apart", P.NonZero, cells, replicate 100 25)
, ("100 holes", P.EvenOdd, square (-10) (-10) 120 <> cells, [14400 - 2500])
]
$ \(name, rule, path, want) -> check name rule path want
-- Concentric circles: an annulus, like a glyph with a curved counter.
let ringArea = sum (map (maybe 0 id . coverage) (polygons P.EvenOdd (P.circle c 50 <> P.circle c 30)))
assertLt failed (abs (ringArea - pi * (50 * 50 - 30 * 30))) (2 * pi * 80 * 0.25)
assertEq failed [3] [length rings | (_, rings, _) <- polygons P.NonZero (P.circle c 50 <> circleBack 30)]
single failed (polygons P.EvenOdd (P.circle c 50 <> P.circle c 30)) $ \poly@(_, rings, _) -> do
assertEq failed 3 (length rings)
assert failed (not (coversPoint poly c) && coversPoint poly (V2 60 20))
-- Many holes in a row, each a random convex polygon.
let blob i = [V2 (25 + 50 * fromIntegral i + 12 * cos a) (50 + 12 * sin a) | k <- [0 .. 7 :: Int], let a = 2 * pi * (fromIntegral k + 0.4 * rnd (i * 10 + k)) / 8]
blobs = map blob [0 .. 7 :: Int]
single failed (polygons P.EvenOdd (square 0 0 400 <> foldMap P.polygon blobs)) $ \poly ->
assertLt failed (abs (maybe 0 id (coverage poly) - (160000 - sum (map (abs . polyArea) blobs)))) 1
-- A single subpath is one polygon with one ring.
assertEq failed [[0, 4]] [rings | (_, rings, _) <- polygons P.EvenOdd (square 0 0 10)]
-- 'fillPathOps', which backends use, matches the canvas output.
assert failed (opsAt 1 (drawPathWith P.EvenOdd (square 0 0 10) (P.Solid red)) == fillPathOps 0.25 mempty P.EvenOdd (square 0 0 10) (P.Solid red))
-- | Dashes split a stroke by the pattern, measured in transformed lengths.
runDashTest :: Context -> IORef Int -> IO ()
runDashTest _ failed = do
let dashedWith st pattern off path = [(pairs pts, cap) | StrokePolyline pts _ closed cap _ _ _ <- opsAt 1 (st (drawStrokePathWith (P.stroke 2) {P.strokeDash = pattern, P.strokeDashOffset = off, P.strokeCap = P.RoundCap} path (P.Solid black))), not closed]
dashed = dashedWith id
line = P.polyline [V2 0 0, V2 100 0]
spans' = map (\(ps, _) -> xsOf (ends ps))
assertEq failed [[0, 10], [15, 25], [30, 40], [45, 55], [60, 70], [75, 85], [90, 100]] (spans' (dashed [10, 5] 0 line))
-- An offset starts partway into the pattern; an odd-length pattern is repeated twice.
assertEq failed [[0, 5], [10, 20], [25, 35], [40, 50], [55, 65], [70, 80], [85, 95]] (spans' (dashed [10, 5] 5 line))
assertEq failed [[0, 10], [20, 30], [40, 50], [60, 70], [80, 90]] (spans' (dashed [10] 0 line))
-- Each dash is capped.
assert failed (all ((== P.RoundCap) . snd) (dashed [10, 5] 0 line))
-- A dash turns a corner with its line.
assertEq failed [[V2 0 0, V2 10 0, V2 10 5]] (map fst (dashed [15, 100] 0 (P.polyline [V2 0 0, V2 10 0, V2 10 10])))
-- Dashes on a closed subpath run around it.
assertEq failed [[V2 0 0, V2 5 0], [V2 10 0, V2 10 5], [V2 10 10, V2 5 10], [V2 0 10, V2 0 5]] (map fst (dashed [5, 5] 0 (square 0 0 10)))
-- A closed two-point subpath runs there and back, dashed both ways.
assertEq failed [[V2 0 0, V2 5 0], [V2 10 0, V2 5 0]] (map fst (dashed [5, 5] 0 (P.moveTo (V2 0 0) <> P.lineTo (V2 10 0) <> P.close)))
-- Zero-length dashes become very short dots.
let dots = dashed [0, 10] 0 line
assertEq failed 11 (length dots)
assert failed (all (\(ps, _) -> case ps of [V2 x0 _, V2 x1 _] -> x1 - x0 > 0 && x1 - x0 < 0.01; _ -> False) dots)
-- Dashes too short to move off a point at its float precision do not
-- repeat forever: the segment is solid, from a dash or a gap.
forM_ [0, 0.007] $ \off -> assertEq failed [[V2 131072 0, V2 131082 0]] (map fst (dashed [0.006, 0.004] off (P.polyline [V2 131072 0, V2 131082 0])))
-- An invalid or too fine pattern draws a solid line.
forM_ [[-1, 2], [0, 0], [0.001]] $ \pattern -> assertEq failed [[V2 0 0, V2 100 0]] (map fst (dashed pattern 0 line))
-- A transform scales the pattern with the line.
assertEq failed [[0, 20], [30, 50], [60, 80], [90, 100]] (spans' (dashedWith (withTransform (P.scale 2 2)) [10, 5] 0 (P.polyline [V2 0 0, V2 50 0])))
-- | Vertex positions (relative to the canvas corner) and alphas from one
-- frame that draws a canvas at scale 1.
canvasVertices :: Context -> (Rect -> CanvasM ()) -> IO [(V2, Float)]
canvasVertices ctx draw = do
setDrawSnapScale ctx 1
(resp, dd, _) <- runFrame ctx (withInput 300 300) (canvas (fixedWH 200 200) draw)
setDrawSnapScale ctx 0
let Rect ox oy _ _ = respRect resp
map (\v -> (V2 (vX v - ox) (vY v - oy), vA v)) <$> drawnVertices dd
-- | How far stroke vertices reach outward past a corner (miter tip, round
-- arc or bevel edge), and past a line end for each cap.
runJoinGeometryTest :: Context -> IORef Int -> IO ()
runJoinGeometryTest ctx failed = do
let corner = V2 100 100
-- The vertices of a polyline through these points, relative to the canvas.
strokeVerts st pts = map fst <$> canvasVertices ctx (\(Rect x y _ _) -> drawStrokePathWith st (P.polyline [V2 (x + px) (y + py) | V2 px py <- pts]) (P.Solid black))
-- How far the vertices near @p@ reach along @dir@.
reachFrom p dir vs = maximum [v2Dot (v2Sub q p) dir | q <- vs, dist q p < 30]
-- A right-angle corner whose outside faces the top right.
reach st = reachFrom corner (V2 (sqrt 0.5) (negate (sqrt 0.5))) <$> strokeVerts st [V2 40 100, corner, V2 100 160]
edge = 5 + 0.5
roundJoin = (P.stroke 10) {P.strokeJoin = P.RoundJoin}
miter <- reach (P.stroke 10)
bevel <- reach (P.stroke 10) {P.strokeJoin = P.BevelJoin}
roundJ <- reach roundJoin
-- A right angle's miter is 1.41 times the width, so a limit of 1.3 bevels it.
limited <- reach (P.stroke 10) {P.strokeMiterLimit = 1.3}
assertLt failed (abs (miter - edge * sqrt 2)) 0.01
assert failed (roundJ > edge * 0.95 && roundJ < edge + 0.01)
assertLt failed (abs (bevel - edge * sqrt 0.5)) 0.01
assertLt failed (abs (limited - bevel)) 0.01
-- A round join where a line turns back on itself rounds the tip, the way
-- the line was heading, whichever way it points.
forM_ [V2 60 0, V2 60 80] $ \(V2 dx dy) -> do
let len = sqrt (dx * dx + dy * dy)
tip = V2 (40 + dx) (100 + dy)
tipReach <- reachFrom tip (V2 (dx / len) (dy / len)) <$> strokeVerts roundJoin [V2 40 100, tip, V2 40 100]
assert failed (tipReach > edge * 0.95 && tipReach < edge + 0.01)
-- Each cap extends past the line end by its expected amount.
let capReach cap = (\vs -> maximum [px - 100 | V2 px py <- vs, abs (py - 100) < 20, px > 90, px < 130]) <$> strokeVerts (P.stroke 10) {P.strokeCap = cap} [V2 40 100, corner]
butt <- capReach P.ButtCap
sq <- capReach P.SquareCap
roundC <- capReach P.RoundCap
assertEq failed [True, True, True] [abs (butt - 0) < 0.01, abs (sq - edge + 0.5) < 0.01, abs (roundC - edge) < 0.01]
-- An end repeated in a polyline op still gets its cap.
vs <- canvasVertices ctx $ \(Rect x y _ _) ->
let a = [x + 40, y + 100]
b = [x + 100, y + 100]
in emitOp (StrokePolyline (primArrayFromList (a ++ a ++ b ++ b)) 10 False P.SquareCap P.MiterJoin 4 (Flat black))
let near100 = [q | (q@(V2 _ py), _) <- vs, abs (py - 100) < 20]
assertLt failed (abs (maximum [px - 100 | V2 px _ <- near100] - 5)) 0.01
assertLt failed (abs (maximum [40 - px | V2 px _ <- near100] - 5)) 0.01
where
v2Dot (V2 a b) (V2 c d) = a * c + b * d
-- | A linear gradient colours points by position, splits geometry at its
-- stops, and follows transforms.
runGradientTest :: Context -> IORef Int -> IO ()
runGradientTest _ failed = do
let shaded ops = [(pairs pts, primArrayToList tris, map Color (primArrayToList cs)) | FillPolygon pts _ tris (Shaded cs) <- ops]
gradient p0 p1 stops path = shaded (opsAt 1 (drawPathWith P.NonZero path (P.Linear p0 p1 stops)))
bar = P.rect (Rect 0 0 100 10)
channel f c = fromIntegral (f c) :: Float
flat paint = [c | FillPolygon _ _ _ (Flat c) <- opsAt 1 (drawPathWith P.NonZero bar paint)]
-- Two stops spanning the shape: corner colours only, no extra vertices.
single failed (gradient (V2 0 0) (V2 100 0) [(0, red), (1, blue)] bar) $ \(pts, _, cols) ->
assertEq failed (4, [red, blue, blue, red]) (length pts, cols)
-- A gradient over part of the shape splits it at every stop, and no
-- triangle crosses a stop.
single failed (gradient (V2 25 0) (V2 75 0) [(0, red), (0.5, green), (1, blue)] bar) $ \(pts, tris, cols) -> do
let xs = xsOf pts
straddles v (a, b, c) = let gs = [xs !! i | i <- [a, b, c]] in minimum gs < v - 1e-3 && maximum gs > v + 1e-3
assert failed (all (\v -> any (\x -> abs (x - v) < 1e-3) xs) [25, 50, 75])
assert failed (not (or [straddles v t | v <- [25, 50, 75], t <- triples tris]))
assertLt failed (abs (fst (covered pts tris) - 1000)) 1e-2
assert failed (and [c == (if x <= 25 then red else if x >= 75 then blue else c) | (V2 x _, c) <- zip pts cols])
assert failed (and [c == green | (V2 x _, c) <- zip pts cols, abs (x - 50) < 1e-3])
-- Two stops at one offset are a hard edge: each side keeps its own
-- colour up to the pixel across the edge.
single failed (gradient (V2 0 0) (V2 100 0) [(0, red), (0.5, red), (0.5, blue), (1, blue)] bar) $ \(pts, _, cols) -> do
assert failed (any (\x -> abs (x - 49.5) < 1e-3) (xsOf pts) && any (\x -> abs (x - 50.5) < 1e-3) (xsOf pts))
assert failed (and [c == (if x < 50 then red else blue) | (V2 x _, c) <- zip pts cols])
-- Rotated with its shape, each point keeps its unrotated colour.
let turn = P.rotateAround (V2 50 5) 0.7
single failed (shaded (opsAt 1 (withTransform turn (drawPathWith P.NonZero bar (P.Linear (V2 0 0) (V2 100 0) [(0, red), (1, blue)]))))) $ \(pts, _, cols) ->
forM_ (zip pts cols) $ \(p, c) -> case P.invert turn of
Just back ->
let V2 x _ = P.transformPoint back p
in assertLt failed (abs (channel colorB c - (channel colorB red + (255 - channel colorB red) * x / 100))) 1.5
Nothing -> assert failed False
-- A zero-length gradient, or one with a single stop, is a flat fill.
assert failed (null (gradient (V2 5 5) (V2 5 5) [(0, red), (1, blue)] bar))
assertEq failed ([blue], [red]) (flat (P.Linear (V2 5 5) (V2 5 5) [(0, red), (1, blue)]), flat (P.Linear (V2 0 0) (V2 100 0) [(0, red)]))
-- A stroke takes the colours along its centre line.
assertEq failed [[red, blue]] [map Color (primArrayToList cs) | StrokePolyline _ _ _ _ _ _ (Shaded cs) <- opsAt 1 (drawStrokePathWith (P.stroke 4) (P.polyline [V2 0 0, V2 100 0]) (P.Linear (V2 0 0) (V2 100 0) [(0, red), (1, blue)]))]
-- A four-corner gradient at a non-quarter angle becomes a shaded polygon
-- with its corner colours.
single failed [(pts, cs) | FillPolygon pts _ _ (Shaded cs) <- opsAt 1 (withTransform (P.rotate 0.5) (drawQuadGradient (Rect 0 0 10 10) red green blue black))] $ \(pts, cs) -> do
assertNear failed 1e-4 (map (P.transformPoint (P.rotate 0.5)) [V2 0 0, V2 10 0, V2 10 10, V2 0 10]) (pairs pts)
assertEq failed [red, green, blue, black] (map Color (primArrayToList cs))
-- | Per-corner rounded rects, inverse transforms, and clips the frame applies.
runShapesAndClipTest :: Context -> IORef Int -> IO ()
runShapesAndClipTest ctx failed = do
let areaOf path = sum [abs (polyArea pts) | (pts, _) <- filled path]
quarterCut r = (4 - pi) / 4 * r * r
-- Area that flattening a quarter circle of radius @r@ may lose.
flat r = pi / 2 * r * 0.25
assertLt failed (abs (areaOf (P.roundedRectCorners (Rect 0 0 100 60) 0 20 0 10) - (6000 - quarterCut 20 - quarterCut 10))) (flat 20 + flat 10)
-- Oversized radii scale down together until they fit, as in CSS.
assertLt failed (abs (areaOf (P.roundedRectCorners (Rect 0 0 100 60) 60 60 0 0) - (6000 - 2 * quarterCut 50))) (2 * flat 50)
-- An infinite radius is as large as the corners allow: a pill, or a lone
-- corner as wide as the shorter side.
assertLt failed (abs (areaOf (P.roundedRect (Rect 10 10 80 40) (1 / 0)) - (3200 - 4 * quarterCut 20))) (4 * flat 20)
assertLt failed (abs (areaOf (P.roundedRectCorners (Rect 10 10 80 40) (1 / 0) 0 0 0) - (3200 - quarterCut 40))) (flat 40)
assert failed (filled (P.roundedRectCorners (Rect 0 0 10 10) 0 0 (-3) 0) == filled (P.rect (Rect 0 0 10 10)))
assert failed (filled (P.roundedRect (Rect 0 0 40 20) 6) == filled (P.roundedRectCorners (Rect 0 0 40 20) 6 6 6 6))
-- A negative width or height spans the same rectangle.
forM_ [Rect 50 0 (-50) 30, Rect 0 30 50 (-30), Rect 50 30 (-50) (-30)] $ \r ->
assert failed (filled (P.roundedRectCorners r 5 8 0 3) == filled (P.roundedRectCorners (Rect 0 0 50 30) 5 8 0 3))
let t = P.translate 3 (-2) <> P.rotate 0.4 <> P.scale 2 (-0.5) <> P.affine 1 0.3 0 1 0 0
pts = [V2 1 2, V2 (-40) 7, V2 0 0]
case P.invert t of
Just back -> assertNear failed 1e-3 pts (map (P.transformPoint back . P.transformPoint t) pts)
Nothing -> assert failed False
assertEq failed [True, True] [P.invert (P.scale 0 1) == Nothing, P.invert (P.scale (0 / 0) 1) == Nothing]
let clipOps block = [op | op <- opsAt 1 block, case op of PushClip _ -> True; PopClip -> True; _ -> False]
assert failed (clipOps (withClip (Rect 1 2 3 4) (drawRect (Rect 0 0 10 10) red)) == [PushClip (Rect 1 2 3 4), PopClip])
assert failed (clipOps (withTransform (P.translate 5 5) (withClip (Rect 1 2 3 4) (pure ()))) == [PushClip (Rect 6 7 3 4), PopClip])
-- Under rotation, the clip is the bounding box of the rotated rect.
case clipOps (withTransform (P.rotate (pi / 4)) (withClip (Rect 0 0 10 10) (pure ()))) of
[PushClip (Rect x y w h), PopClip] -> assertNear failed 1e-3 [V2 (-10 * sqrt 0.5) 0, V2 (20 * sqrt 0.5) (20 * sqrt 0.5)] [V2 x y, V2 w h]
_ -> assert failed False
-- In a frame, the content is clipped and later draws are not.
(resp, dd, _) <- runFrame ctx (withInput 300 300) . canvas (fixedWH 100 100) $ \(Rect x y _ _) -> do
withClip (Rect (x + 10) (y + 10) 20 20) (drawRect (Rect x y 100 100) red)
drawRect (Rect x y 50 50) black
let Rect x y _ _ = respRect resp
clips = [(cmdClipX c - x, cmdClipY c - y, cmdClipW c, cmdClipH c) | c <- drawCmdElems dd, cmdIndexCount c > 0]
assert failed ((10, 10, 20, 20) `elem` clips && any (\(_, _, w, _) -> w >= 100) clips)
-- | A canvas repaints only when its drawing changes and flattens for the
-- display scale; a configured one sees hover and sets its cursor.
runCanvasFramesTest :: Context -> IORef Int -> IO ()
runCanvasFramesTest ctx failed = do
let inp = withInputOff 400 300
ui turn = column $ do
label "Other"
drawn <- canvas (fixedWH 120 120) $ \(Rect x y w h) ->
withTransform (P.rotateAround (V2 (x + w / 2) (y + h / 2)) turn) $ do
drawPath (P.roundedRect (Rect (x + 20) (y + 20) 80 80) 10) red
drawStrokePath (P.circle (V2 (x + 60) (y + 60)) 30) 2 black
(keyed, ()) <- customWidget defaultCustomWidgetSpec
{ widgetLayout = fixedWH 60 60
, widgetContent = 1
, widgetDraw = \cdc (Rect x y _ _) -> runCanvasFor cdc (drawPath (P.circle (V2 (x + 30) (y + 30)) 25) red)
}
pure (drawn, keyed)
cached c = IM.elems . dcsCustomDrawOpCache <$> readIORef (ctxDrawingCache c)
-- Filled point count of each cached canvas.
pointsIn c = map (\e -> sum [sizeofPrimArray pts `div` 2 | FillPolygon pts _ _ _ <- toList (cdeOps e)]) <$> cached c
(drawn, keyed) <- warmup2 ctx inp (ui 0)
_ <- runFrame ctx inp (ui 0)
takeDamage ctx >>= \same -> assert failed (not (any (damageCovers same . respRect) [drawn, keyed]))
_ <- runFrame ctx inp (ui 0.4)
takeDamage ctx >>= assert failed . (`clipCovers` respRect drawn)
-- Both flatten finer at a higher display scale, including the keyed one whose key is unchanged.
before <- pointsIn ctx
let dense = withFontMetrics ctx (ctxFontMetrics ctx) {fmSnapScale = 3}
_ <- warmup2 dense inp (ui 0.4)
after <- pointsIn dense
assertEq failed (2, 2, True) (length before, length after, and (zipWith (<) before after))
configured <- newContext
let cfg key = defaultCanvasConfig {canvasLayout = fixedWH 60 40, canvasContent = key, canvasCursor = Just (\_ _ _ -> UiCursorPointer)}
hovered key = canvasConfigured (cfg key) $ \r -> drawContext >>= \cdc -> drawRect r (if cdcHovered cdc then red else black)
resp <- warmup2 configured (withInput 300 300) (hovered 1)
let hoverAt = (withInput 300 300) {inputMousePos = centerOf resp}
_ <- run2Frames configured hoverAt (hovered 1)
cached configured >>= \es -> assertEq failed [red] [c | e <- es, FillRect _ c <- toList (cdeOps e)]
assertEq failed UiCursorPointer =<< uiCursorKind configured hoverAt