packages feed

kb-text-layout-0.3.0.0: test/Spec.hs

module Main (main) where

import Control.Exception (ErrorCall, try)
import Data.Foldable (for_, toList)
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Vector qualified as Vector
import Data.Vector.Storable qualified as Storable
import Test.Tasty (TestTree, defaultMain, testGroup)
import Test.Tasty.HUnit (assertBool, assertFailure, testCase, (@?=))

import KB.Text.Layout.Analysis (BreakKind (..))
import KB.Text.Layout.Analysis qualified as Analysis
import KB.Text.Layout.Break (Cursor (..), LineEnd (..), LineStretch (..))
import KB.Text.Layout.Break qualified as Break
import KB.Text.Layout.Html qualified as Html
import KB.Text.Layout.Measure (Face (..), Grapheme (..), LayoutContext, MeasuredSegment (..), PreparedText (..), Span (..), Style)
import KB.Text.Layout.Measure qualified as Measure
import KB.Text.Layout.Segmentation qualified as Segmentation
import KB.Text.Shape qualified as TextShape
import KB.Text.Shape.FFI.Enums.FeatureTag qualified as FeatureTag

main :: IO ()
main =
  TextShape.withContext \shape -> do
    font <- TextShape.pushFontFromFile shape "demos/assets/NotoSans-Regular.ttf" 0
    variable <- TextShape.pushFontFromFile shape "demos/assets/Ubuntu-R.ttf" 0
    ctx <- Measure.createLayoutContext shape
    body <- Measure.newStyle ctx font 1.0
    accent <- Measure.newStyle ctx font 1.5
    defaultMain $
      testGroup
        "kb-text-layout"
        [ layoutTests
        , prepareTests ctx body accent
        , faceTests ctx body variable
        ]

layoutTests :: TestTree
layoutTests =
  testGroup
    "layout"
    [ testCase "single line" do
        (Break.layoutStats (Break.layoutGreedy (fromSegments prepared) 200)).lineCount @?= 1
    , testCase "break at space" do
        (Break.layoutStats (Break.layoutGreedy (fromSegments prepared) 100)).lineCount @?= 2
    , testCase "emergency graphemes" do
        materialized prepared 30 @?= ["hel", "lo", "wor", "ld"]
    , testCase "soft hyphen" do
        materialized hyphenated 30 @?= ["hy-", "phen"]
    , testCase "soft hyphen too wide to use" do
        materialized wideHyphen 30 @?= ["hyphen"]
    , testCase "hard break" do
        materialized broken 1000 @?= ["one", "two"]
    , testCase "glue holds" do
        materialized glued 60 @?= ["aaa\xA0\&bb", "b"]
    , testCase "atomic breaks before" do
        materialized [word 50 "word", atom 40 "[pill]"] 60 @?= ["word", "[pill]"]
    , testCase "atomic breaks after" do
        materialized [atom 40 "[pill]", word 50 "word"] 60 @?= ["[pill]", "word"]
    , testCase "atomic never splits" do
        materialized [word 30 "abc", space, atom 100 "[wide-pill]"] 60
          @?= ["abc", "[wide-pill]"]
    , testCase "stepping matches walk" do
        stepped prepared (replicate 10 30) @?= materialized prepared 30
    , testCase "variable widths route lines" do
        stepped rhythm [70, 30] @?= ["aaa bbb", "ccc"]
    , testCase "narrower route reflows" do
        stepped rhythm [40, 40, 40] @?= ["aaa", "bbb", "ccc"]
    , testCase "slices merge same-style runs" do
        let prep = fromSegments styled
        map (\s -> (s.text, s.style)) (concatMap (Break.materializeSlices prep) (Break.layoutGreedy prep 200))
          @?= [("aaa bbb ", 1), ("ccc", 2)]
    , testCase "slices mark atoms with widths" do
        let prep = fromSegments [styledWord 1 30 "hi", styledSpace 1, atom 40 "[pill]"]
        map (\s -> (s.text, s.atom, s.width)) (concatMap (Break.materializeSlices prep) (Break.layoutGreedy prep 200))
          @?= [("hi ", False, 40), ("[pill]", True, 40)]
    , testCase "slices absorb the soft hyphen" do
        let prep = fromSegments hyphenated
        map (\s -> (s.text, s.width)) (concatMap (Break.materializeSlices prep) (take 1 (Break.layoutGreedy prep 30)))
          @?= [("hy-", 25)]
    , testCase "slices concat to materialized text" do
        for_ [prepared, hyphenated, broken, glued, rhythm, styled] \xs ->
          for_ [25, 30, 60, 200] \w -> do
            let prep = fromSegments xs
            for_ (Break.layoutGreedy prep w) \line ->
              Text.concat (map (\s -> s.text) (Break.materializeSlices prep line))
                @?= Break.materializeLineRange prep line
    , testCase "emergency breaks respect clusters" do
        let accented =
              [ MeasuredSegment
                  { text = "a\x301\&bc"
                  , kind = Word
                  , width = 30
                  , style = 0
                  , graphemes = [Grapheme 2 10, Grapheme 1 10, Grapheme 1 10]
                  }
              ]
        materialized accented 10 @?= ["a\x301", "b", "c"]
        materialized accented 20 @?= ["a\x301\&b", "c"]
    , testCase "kbts clusters marks, zwj sequences, and flags" do
        Segmentation.clusters "a\x301\&bc" @?= ["a\x301", "b", "c"]
        Segmentation.clusters "\128104\8205\128105\8205\128103 ok"
          @?= ["\128104\8205\128105\8205\128103", " ", "o", "k"]
        Segmentation.clusters "\127482\127462\127482\127462" @?= ["\127482\127462", "\127482\127462"]
    , testCase "kbts break positions arrive as Char offsets" do
        Segmentation.boundaries "" @?= []
        Segmentation.boundaries "\128512\&a" @?= [0, 1, 2]
        Segmentation.boundaries "a\128104\8205\128105\8205\128103b" @?= [0, 1, 6, 7]
        Segmentation.softBreaks "\26085\26412\35486 ok" @?= [1, 2, 4, 6]
        Segmentation.softBreaks "go \128105\127997\8205\128640 now" @?= [3, 8, 11]
        Segmentation.wordBreaks "one two" @?= [0, 3, 4, 7]
    , testCase "kbts soft breaks split words at line break opportunities" do
        map (\s -> (s.text, s.kind)) (Analysis.analyze "well-known")
          @?= [("well-", Word), ("", ZeroWidthBreak), ("known", Word)]
        map (\s -> s.text) (Analysis.analyze "\26085\26412\12290\35486")
          @?= ["\26085", "", "\26412\12290", "", "\35486"]
        map (\s -> s.text) (Analysis.analyze "ab\128104\8205\128105\8205\128103\&cd")
          @?= ["ab\128104\8205\128105\8205\128103", "", "cd"]
        map (\s -> (s.text, s.kind)) (Analysis.analyze "plain words")
          @?= [("plain", Word), (" ", Space), ("words", Word)]
    , testCase "tailored breaks split url queries and hold dash ranges" do
        map (\s -> s.text) (Analysis.analyze "see example.com/a?b=1&c#d now")
          @?= ["see", " ", "example.com/", "", "a?", "", "b", "", "=1", "", "&c", "", "#d", " ", "now"]
        map (\s -> s.text) (Analysis.analyze "pages 3\8211\&5 and 2+2=4 at AT&T")
          @?= ["pages", " ", "3\8211\&5", " ", "and", " ", "2+2=4", " ", "at", " ", "AT&T"]
        map (\s -> s.text) (Analysis.analyze "10:30-11:00") @?= ["10:30-11:00"]
    , testCase "line ends carry their reason" do
        let ends xs w = map (\l -> l.ended) (Break.layoutGreedy (fromSegments xs) w)
        ends prepared 30 @?= [Overflowed, Wrapped, Overflowed, Finished]
        ends hyphenated 30 @?= [Hyphenated, Finished]
        ends broken 1000 @?= [HardBroken, Finished]
        ends rhythm 70 @?= [Wrapped, Finished]
    , testCase "kept words overflow whole" do
        kept prepared 30 @?= ["hello", "world"]
        map (\l -> l.ended) (Break.layoutGreedyWith Break.KeepWords (fromSegments prepared) 30) @?= [Wrapped, Finished]
    , testCase "a wide atom ends its line like any other piece" do
        let
          prep = fromSegments [atom 50 "[A]", seg HardBreak 0 "\n", word 10 "x", space, atom 50 "[B]"]
          lns = Break.layoutGreedy prep 30
        map (\l -> (Break.materializeLineRange prep l, l.ended, l.overfull)) lns
          @?= [("[A]", HardBroken, True), ("x", Wrapped, False), ("[B]", Finished, True)]
    , testCase "kept words at zero width put one word on each line" do
        kept rhythm 0 @?= ["aaa", "bbb", "ccc"]
    , testCase "kept glue holds its whole run" do
        kept glued 60 @?= ["aaa\xA0\&bbb"]
    , testCase "kept words still break at soft hyphens" do
        kept hyphenated 30 @?= ["hy-", "phen"]
        kept hyphenated 0 @?= ["hy-", "phen"]
    , testCase "kept words agree with split words when everything fits" do
        for_ [70, 100, 200] \w -> kept rhythm w @?= materialized rhythm w
    , testCase "optimal layout keeps overlong words whole" do
        let prep = fromSegments prepared
        map (Break.materializeLineRange prep) (Break.layoutOptimalWith Break.ragged Break.KeepWords prep 30)
          @?= ["hello", "world"]
    , testCase "optimal kept words agree with greedy around overlong words" do
        let optimalKept xs w = map (Break.materializeLineRange (fromSegments xs)) (Break.layoutOptimalWith Break.justified Break.KeepWords (fromSegments xs) w)
        for_ [wideHyphen <> [space, word 10 "x"], indented, [seg Tab 20 "\t", space, word 80 "longword", space, word 30 "next"]] \xs ->
          optimalKept xs 90 @?= kept xs 90
        optimalKept (wideHyphen <> [space, word 10 "x"]) 30 @?= ["hyphen", "x"]
    , testCase "an overlong word leaves the other paragraphs optimal" do
        let
          paragraphs = [word 60 "aaaaaa", space, word 10 "b", space, word 10 "c", space, word 60 "dddddd", seg HardBreak 0 "\n", word 100 "longlonglong"]
          prep = fromSegments paragraphs
          layout overflow = Break.layoutOptimalWith Break.justified overflow prep 77
        map (Break.materializeLineRange prep) (layout Break.KeepWords) @?= ["aaaaaa b", "c dddddd", "longlonglong"]
        map (\l -> l.overfull) (layout Break.KeepWords) @?= [False, False, True]
        map (Break.materializeLineRange prep) (layout Break.SplitWords) @?= ["aaaaaa b", "c dddddd", "longlongl", "ong"]
    , testCase "every layout keeps every word and atom" do
        let
          strip = Text.filter (`notElem` [' ', '\t', '\xA0', '-'])
          content xs = strip (Text.concat [s.text | s <- xs, s.kind `elem` [Word, Glue, Atomic]])
          layouts =
            [Break.layoutGreedyWith overflow | overflow <- [Break.SplitWords, Break.KeepWords]]
              <> [Break.layoutOptimalWith spacing overflow | spacing <- [Break.justified, Break.ragged], overflow <- [Break.SplitWords, Break.KeepWords]]
          atoms = [[atom 40 "[pill]", word 50 "word"], [word 30 "abc", space, atom 100 "[wide]"], [word 30 "abc", atom 40 "[pill]", word 30 "def"]]
        for_ ([prepared, hyphenated, wideHyphen, broken, glued, rhythm, indented] <> atoms) \xs ->
          for_ layouts \layout ->
            for_ [0, 25, 30, 60, 90, 200] \w -> do
              let prep = fromSegments xs
              strip (Text.concat (map (Break.materializeLineRange prep) (layout prep w))) @?= content xs
    , testCase "optimal split words overflow only where greedy does" do
        let atoms = [[atom 40 "[pill]", word 50 "word"], [word 30 "abc", space, atom 100 "[wide]"], [word 30 "abc", atom 40 "[pill]", word 30 "def"]]
        for_ ([prepared, hyphenated, wideHyphen, broken, glued, rhythm, indented] <> atoms) \xs ->
          for_ [Break.justified, Break.ragged] \spacing ->
            for_ [0, 10, 25, 30, 60, 90, 200] \w -> do
              let
                prep = fromSegments xs
                overflowing lns = any (\l -> l.overfull) lns
              assertBool ("overflow at " <> show w) $
                not (overflowing (Break.layoutOptimalWith spacing Break.SplitWords prep w)) || overflowing (Break.layoutGreedy prep w)
    , testCase "optimal split words split a hyphenated word that doesn't fit" do
        let optimal xs w = map (Break.materializeLineRange (fromSegments xs)) (Break.layoutOptimal (fromSegments xs) w)
        optimal wideHyphen 25 @?= ["hyph", "en"]
        optimal [word 25 "abc", seg SoftHyphen 10 "\xAD", word 6 "def"] 30 @?= ["abcde", "f"]
    , testCase "greedy lines flag overflow past the target" do
        map (\l -> l.overfull) (Break.layoutGreedyWith Break.KeepWords (fromSegments prepared) 30) @?= [True, True]
        map (\l -> l.overfull) (Break.layoutGreedy (fromSegments prepared) 30) @?= [False, False, False, False]
        map (\l -> l.overfull) (Break.layoutGreedyWith Break.KeepWords (fromSegments hyphenated) 0) @?= [True, True]
    , testCase "stepping kept words matches the walk" do
        let
          prep = fromSegments prepared
          go Nothing = []
          go (Just cursor) =
            case Break.layoutNextLineRangeWith Break.KeepWords prep 30 cursor of
              Nothing -> []
              Just (line, next) -> Break.materializeLineRange prep line : go next
        go (Just (Cursor 0 0)) @?= kept prepared 30
    , testCase "kept words prefer a fitting break before an atom to an overflowing hyphen" do
        kept [word 20 "hy", seg SoftHyphen 15 "\xAD", word 9 "phen", atom 40 "[pill]"] 30 @?= ["hyphen", "[pill]"]
        kept [word 50 "hello", space, atom 10 "[x]"] 30 @?= ["hello", "[x]"]
    , testCase "a fitting word before an atom keeps its soft hyphen unused" do
        materialized [word 20 "hy", seg SoftHyphen 5 "\xAD", word 3 "p", atom 40 "[pill]"] 30 @?= ["hyp", "[pill]"]
    , testCase "line stretch counts interior spaces only" do
        let
          prep = fromSegments rhythm
          stretches w = map (Break.lineStretch prep) (Break.layoutGreedy prep w)
        stretches 200 @?= [LineStretch{spaces = 2, width = 20}]
        stretches 70 @?= [LineStretch{spaces = 1, width = 10}, LineStretch{spaces = 0, width = 0}]
        map (Break.lineStretch (fromSegments indented)) (Break.layoutGreedy (fromSegments indented) 200)
          @?= [LineStretch{spaces = 0, width = 0}, LineStretch{spaces = 0, width = 0}, LineStretch{spaces = 0, width = 0}]
    , testCase "leading spaces survive hard breaks" do
        let
          prep = fromSegments indented
          lns = Break.layoutGreedy prep 200
        map (Break.materializeLineRange prep) lns @?= ["main", "do", "  putStrLn"]
        map (\l -> (l.from, l.width)) lns @?= [(Cursor 0 0, 40), (Cursor 2 0, 20), (Cursor 4 0, 100)]
        map (Break.materializeLineRange prep) (Break.layoutOptimal prep 200) @?= ["main", "do", "  putStrLn"]
        materialized [space, word 40 "word"] 200 @?= [" word"]
        materialized [indent, word 80 "putStrLn"] 90 @?= ["  putStrL", "n"]
        stepped indented [200, 200, 200] @?= ["main", "do", "  putStrLn"]
    , testCase "leading spaces still fold after a wrap" do
        let prep = fromSegments [word 30 "aaa", indent, word 30 "bbb"]
        materialized [word 30 "aaa", indent, word 30 "bbb"] 40 @?= ["aaa", "bbb"]
        map (Break.materializeLineRange prep) (Break.layoutOptimal prep 40) @?= ["aaa", "bbb"]
    , testCase "shrinkwrap tightens without adding lines" do
        Break.shrinkwrap (fromSegments rhythm) 80 @?= 70
        Break.shrinkwrap (fromSegments rhythm) 200 @?= 110
        Break.shrinkwrap (fromSegments prepared) 45 @?= 30
    , testCase "shrinkwrap with kept words widens to the widest word" do
        Break.shrinkwrapWith Break.KeepWords (fromSegments prepared) 45 @?= 50
        Break.shrinkwrapWith Break.KeepWords (fromSegments rhythm) 80 @?= 70
    , testCase "optimal layout beats greedy on loose middle lines" do
        let
          squeeze = [word 60 "aaaaaa", space, word 10 "b", space, word 10 "c", space, word 60 "dddddd"]
          prep = fromSegments squeeze
        materialized squeeze 77 @?= ["aaaaaa", "b c", "dddddd"]
        map (Break.materializeLineRange prep) (Break.layoutOptimal prep 77) @?= ["aaaaaa b", "c dddddd"]
    , testCase "ragged glue never shrinks a line past the target" do
        let
          squeeze = [word 60 "aaaaaa", space, word 10 "b", space, word 10 "c", space, word 60 "dddddd"]
          prep = fromSegments squeeze
          lines' = Break.layoutOptimalWith Break.ragged Break.SplitWords prep 77
        map (Break.materializeLineRange prep) lines' @?= ["aaaaaa", "b c", "dddddd"]
        assertBool "every line fits" $ all (\line -> line.width <= 77) lines'
    , testCase "justified glue is the default optimal layout" do
        let
          squeeze = [word 60 "aaaaaa", space, word 10 "b", space, word 10 "c", space, word 60 "dddddd"]
          prep = fromSegments squeeze
        map (Break.materializeLineRange prep) (Break.layoutOptimalWith Break.justified Break.SplitWords prep 77)
          @?= ["aaaaaa b", "c dddddd"]
    , testCase "ragged glue takes an exact fit despite float noise" do
        let
          noisy = [word 20 "aa", space, word 20 "bb", space, word 10.00005 "c", space, word 30 "ddd", space, word 20 "ee"]
          prep = fromSegments noisy
        map (Break.materializeLineRange prep) (Break.layoutOptimalWith Break.ragged Break.SplitWords prep 70)
          @?= ["aa bb c", "ddd ee"]
    , testCase "rigid glue does not scatter words across lines" do
        let
          prep = fromSegments [word 30 "aaa", space, word 10 "b", space, word 40 "cccc", space, word 20 "dd", space, word 30 "eee", space, word 10 "f", space, word 50 "ggggg", space, word 20 "hh"]
          rigid = Break.Spacing{stretch = 0, shrink = 0}
        length (Break.layoutOptimalWith rigid Break.SplitWords prep 70) @?= length (Break.layoutGreedy prep 70)
    , testCase "optimal layout keeps atoms" do
        let optimal xs w = map (Break.materializeLineRange (fromSegments xs)) (Break.layoutOptimal (fromSegments xs) w)
        optimal [atom 40 "[pill]", word 50 "word"] 60 @?= ["[pill]", "word"]
        optimal [word 50 "word", atom 40 "[pill]"] 60 @?= ["word", "[pill]"]
        optimal [word 30 "abc", atom 40 "[pill]", word 30 "def"] 200 @?= ["abc[pill]def"]
        optimal [word 30 "abc", atom 40 "[pill]", word 30 "def"] 75 @?= ["abc[pill]", "def"]
    , testCase "optimal layout leaves the space before an atom off the line" do
        let prep = fromSegments [word 30 "abc", space, atom 100 "[wide]"]
        map (\l -> (Break.materializeLineRange prep l, l.width)) (Break.layoutOptimal prep 60) @?= [("abc", 30), ("[wide]", 100)]
    , testCase "optimal layout keeps indentation rigid" do
        let prep = fromSegments [word 40 "main", seg HardBreak 0 "\n", indent, word 30 "aaa", space, word 35 "bbb", space, word 30 "ccc"]
        map (Break.materializeLineRange prep) (Break.layoutOptimal prep 90) @?= ["main", "  aaa", "bbb ccc"]
    , testCase "optimal layout agrees with greedy where greedy is fine" do
        let optimal xs w = map (Break.materializeLineRange (fromSegments xs)) (Break.layoutOptimal (fromSegments xs) w)
        optimal rhythm 70 @?= ["aaa bbb", "ccc"]
        optimal hyphenated 30 @?= ["hy-", "phen"]
        optimal broken 1000 @?= ["one", "two"]
    , testCase "optimal layout splits an overlong glued run like greedy" do
        map (Break.materializeLineRange (fromSegments glued)) (Break.layoutOptimal (fromSegments glued) 60)
          @?= materialized glued 60
    , testCase "html renderer escapes and anchors baselines" do
        let
          prep = fromSegments [styledWord 1 30 "a<b", styledSpace 1, styledWord 1 30 "cd"]
          opts = Html.Options{width = 40, lineHeight = 20, unit = 1, baseCap = 1, justify = False, baseCss = "font:16px serif", styleCss = const "font:16px serif"}
          html = Html.render opts prep (Break.layoutGreedy prep 40)
        Text.isInfixOf "a&lt;b" html @?= True
        Text.isInfixOf "top:0.00px" html @?= True
        Text.isInfixOf "top:20.00px" html @?= True
        Text.isInfixOf "height:21.00px" html @?= True
        Text.isInfixOf "text-box-trim:trim-both" html @?= True
    , testCase "html renderer scales layout units" do
        let
          prep = fromSegments [styledWord 1 30 "ab", styledSpace 1, styledWord 1 30 "cd"]
          opts = Html.Options{width = 40, lineHeight = 20, unit = 2, baseCap = 1, justify = False, baseCss = "", styleCss = const ""}
          html = Html.render opts prep (Break.layoutGreedy prep 40)
        Text.isInfixOf "width:80.00px" html @?= True
        Text.isInfixOf "top:40.00px" html @?= True
        Text.isInfixOf "height:42.00px" html @?= True
    , testCase "html renderer justifies wrapped lines only" do
        let
          prep = fromSegments rhythm
          opts = Html.Options{width = 80, lineHeight = 20, unit = 1, baseCap = 1, justify = True, baseCss = "", styleCss = const ""}
          html = Html.render opts prep (Break.layoutGreedy prep 80)
        Text.count "word-spacing" html @?= 1
        Text.isInfixOf "word-spacing:10.00px" html @?= True
    , testCase "html renderer shrinks tight last lines" do
        let
          prep = fromSegments rhythm
          opts = Html.Options{width = 105, lineHeight = 20, unit = 1, baseCap = 1, justify = True, baseCss = "", styleCss = const ""}
          html = Html.render opts prep (Break.layoutOptimal prep 105)
        Text.isInfixOf "word-spacing:-2.50px" html @?= True
    , testCase "html renderer stretches interior spaces only" do
        let
          prep = fromSegments [word 40 "main", seg HardBreak 0 "\n", indent, word 30 "aaa", space, word 30 "bbb", seg Glue 10 "\xA0", word 30 "ccc", space, word 30 "ddd"]
          opts = Html.Options{width = 150, lineHeight = 20, unit = 1, baseCap = 1, justify = True, baseCss = "", styleCss = const ""}
          html = Html.render opts prep (Break.layoutGreedy prep 150)
        Text.isInfixOf "word-spacing:20.00px" html @?= True
        Text.isInfixOf "<span style=\"word-spacing:normal\">  </span>aaa bbb" html @?= True
        Text.isInfixOf "bbb<span style=\"word-spacing:normal\">\xA0</span>ccc" html @?= True
    , testCase "html renderer leaves overflowing lines unjustified" do
        let
          overflowing = [seg Tab 20 "\t", space, word 80 "longword", space, word 30 "next"]
          prep = fromSegments overflowing
          opts = Html.Options{width = 90, lineHeight = 20, unit = 1, baseCap = 1, justify = True, baseCss = "", styleCss = const ""}
          html = Html.render opts prep (Break.layoutGreedyWith Break.KeepWords prep 90)
        Text.isInfixOf "word-spacing" html @?= False
    ]

faceTests :: LayoutContext -> Style -> TextShape.Font -> TestTree
faceTests ctx body variable =
  testGroup
    "faces"
    [ testCase "styles shape in their own font, not the one on top of the stack" do
        let t = "Hamburgefonstiv"
        alone <- TextShape.withContext \shape -> do
          font <- TextShape.pushFontFromFile shape "demos/assets/NotoSans-Regular.ttf" 0
          aloneCtx <- Measure.createLayoutContext shape
          aloneStyle <- Measure.newStyle aloneCtx font 1.0
          Measure.charAdvances aloneCtx aloneStyle t
        stacked <- Measure.charAdvances ctx body t
        stacked @?= alone
    , testCase "feature overrides reach the shaper and stay in their style" do
        proportional <- Measure.newStyleWith ctx (Measure.face body.face.font){features = [(FeatureTag.FEATURE_TAG_pnum, 1)]} 1.0
        before <- Measure.charAdvances ctx body "1111"
        narrowed <- Measure.charAdvances ctx proportional "1111"
        after <- Measure.charAdvances ctx body "1111"
        assertBool "pnum narrows every digit" (Storable.and (Storable.zipWith (<) narrowed before))
        after @?= before
    , testCase "variation axes reach the shaper and stay in their style" do
        regular <- Measure.newStyle ctx variable 1.0
        bold <- Measure.newStyleWith ctx (Measure.face variable){variations = [TextShape.Variation (TextShape.fourcc 'w' 'g' 'h' 't') 800]} 1.0
        before <- Measure.charAdvances ctx regular "Abc"
        widened <- Measure.charAdvances ctx bold "Abc"
        after <- Measure.charAdvances ctx regular "Abc"
        assertBool "wght 800 widens every letter" (Storable.and (Storable.zipWith (<) before widened))
        after @?= before
        assertClose bold.scale regular.scale
    , testCase "a repeated tag keeps its last value and spares the other tags" do
        let
          pnum = FeatureTag.FEATURE_TAG_pnum
          kern = FeatureTag.FEATURE_TAG_kern
          wght = TextShape.Variation (TextShape.fourcc 'w' 'g' 'h' 't')
          advancesWith typeface t = Measure.newStyleWith ctx typeface 1.0 >>= \style -> Measure.charAdvances ctx style t
          withFeatures font features = (Measure.face font){features}
          withVariations font variations = (Measure.face font){variations}
        proportional <- advancesWith (withFeatures body.face.font [(pnum, 1)]) "1111"
        tabular <- advancesWith (withFeatures body.face.font []) "1111"
        repeatedKern <- advancesWith (withFeatures body.face.font [(kern, 1), (kern, 1), (pnum, 1)]) "1111"
        overridden <- advancesWith (withFeatures body.face.font [(pnum, 1), (pnum, 0)]) "1111"
        repeatedKern @?= proportional
        overridden @?= tabular
        bold <- advancesWith (withVariations variable [wght 800]) "Abc"
        reweighted <- advancesWith (withVariations variable [wght 400, wght 800]) "Abc"
        reweighted @?= bold
        normalized <- Measure.newStyleWith ctx (withFeatures body.face.font [(pnum, 1), (kern, 0), (pnum, 0)]) 1.0
        normalized.face.features @?= [(kern, 0), (pnum, 0)]
    ]

prepareTests :: LayoutContext -> Style -> Style -> TestTree
prepareTests ctx body accent =
  testGroup
    "prepare"
    [ testCase "segments follow analysis and carry the style key" do
        prep <- Measure.prepare ctx body "hello world"
        map (\s -> (s.text, s.kind, s.style)) (toList prep.segments)
          @?= [("hello", Word, body.key), (" ", Space, body.key), ("world", Word, body.key)]
    , testCase "empty text prepares to nothing" do
        prep <- Measure.prepare ctx body ""
        prep.segments @?= Vector.empty
        advances <- Measure.charAdvances ctx body ""
        advances @?= Storable.empty
    , testCase "widths add up to the shaped advances" do
        let t = "The quick brown fox"
        prep <- Measure.prepare ctx body t
        advances <- Measure.charAdvances ctx body t
        Storable.length advances @?= Text.length t
        assertBool "advances are positive" (Storable.all (> 0) advances)
        assertClose (Storable.sum advances) (sum (map (\s -> s.width) (toList prep.segments)))
        for_ prep.segments \s -> do
          sum (map (.codepoints) s.graphemes) @?= Text.length s.text
          assertClose s.width (sum (map (.width) s.graphemes))
    , testCase "prepareWithAdvances agrees with prepare" do
        let t = "shaped \xAD\&once,\tthen reused"
        prep <- Measure.prepare ctx body t
        reused <- Measure.prepareWithAdvances ctx body t =<< Measure.charAdvances ctx body t
        reused @?= prep
    , testCase "prepareWithAdvances rejects a codepoint/advance count mismatch" do
        attempt <- try (Measure.prepareWithAdvances ctx body "abc" (Storable.fromList [1, 2]))
        case attempt of
          Left (_ :: ErrorCall) -> pure ()
          Right prep -> assertFailure ("accepted " <> show prep)
    , testCase "custom advances flow into grapheme widths" do
        prep <- Measure.prepareWithAdvances ctx body "a\x301\&b c" (Storable.fromList [1, 0.5, 2, 4, 8])
        map (\s -> (s.text, s.width, s.graphemes)) (toList prep.segments)
          @?= [("a\x301\&b", 3.5, [Grapheme 2 1.5, Grapheme 1 2]), (" ", 4, [Grapheme 1 4]), ("c", 8, [Grapheme 1 8])]
    , testCase "zero-advance codepoints fold into the preceding cluster" do
        prep <- Measure.prepareWithAdvances ctx body "<=>ff \x301\&ab" (Storable.fromList [5, 0, 0, 3, 0, 4, 0, 1, 0])
        map (\s -> (s.text, s.graphemes)) (toList prep.segments)
          @?= [("<=>ff", [Grapheme 3 5, Grapheme 2 3]), (" ", [Grapheme 1 4]), ("\x301\&ab", [Grapheme 1 0, Grapheme 2 1])]
    , testCase "emergency breaks never split a shaped cluster" do
        prep <- Measure.prepareWithAdvances ctx body "<=>xy" (Storable.fromList [5, 0, 0, 2, 2])
        let lines' maxWidth = map (Break.materializeLineRange prep) (Break.layoutGreedy prep maxWidth)
        lines' 3 @?= ["<=>", "x", "y"]
        lines' 6 @?= ["<=>", "xy"]
        for_ (Break.layoutGreedy prep 3) \line ->
          for_ [line.from, line.to] \cursor ->
            assertBool ("cursor inside a cluster: " <> show cursor) (cursor.grapheme `elem` [0, 3, 4, 5])
    , testCase "font ligatures shape as one cluster and overflow whole" do
        advances <- Measure.charAdvances ctx body "ffi"
        Storable.toList (Storable.drop 1 advances) @?= [0, 0]
        prep <- Measure.prepare ctx body "ffi"
        map (.graphemes) (toList prep.segments) @?= [[Grapheme 3 (Storable.head advances)]]
        let narrow = Break.layoutGreedy prep (Storable.head advances / 2)
        map (Break.materializeLineRange prep) narrow @?= ["ffi"]
        map (\l -> (l.ended, l.overfull)) narrow @?= [(Finished, True)]
    , testCase "soft hyphens and tabs take their widths from the style" do
        spaceWidth <- Measure.measure ctx body " "
        hyphenWidth <- Measure.measure ctx body "-"
        body.spaceWidth @?= spaceWidth
        body.hyphenWidth @?= hyphenWidth
        assertBool "space is not empty" (spaceWidth > 0)
        prep <- Measure.prepareWithAdvances ctx body "a\xAD\&b\t\tc\n" (Storable.replicate 7 1)
        map (\s -> (s.text, s.kind, s.width)) (toList prep.segments)
          @?= [ ("a", Word, 1)
              , ("\xAD", SoftHyphen, hyphenWidth)
              , ("b", Word, 1)
              , ("\t\t", Tab, 2 * spaceWidth)
              , ("c", Word, 1)
              , ("\n", HardBreak, 0)
              ]
    , testCase "styles scale their metrics" do
        assertClose accent.spaceWidth (1.5 * body.spaceWidth)
        assertClose accent.hyphenWidth (1.5 * body.hyphenWidth)
        assertBool "styles get distinct keys" (accent.key /= body.key)
    , testCase "prepareStyled mixes text, shaped, and atom spans" do
        prep <-
          Measure.prepareStyled
            ctx
            [ TextSpan body "ab "
            , AtomSpan accent "[pill]" 7
            , ShapedSpan accent " cd" (Storable.fromList [1, 2, 3])
            ]
        map (\s -> (s.text, s.kind, s.style)) (toList prep.segments)
          @?= [ ("ab", Word, body.key)
              , (" ", Space, body.key)
              , ("[pill]", Atomic, accent.key)
              , (" ", Space, accent.key)
              , ("cd", Word, accent.key)
              ]
        map (\s -> (s.width, s.graphemes)) (drop 2 (toList prep.segments))
          @?= [(7, [Grapheme 6 7]), (1, [Grapheme 1 1]), (5, [Grapheme 1 2, Grapheme 1 3])]
    ]

assertClose :: Float -> Float -> IO ()
assertClose expected actual =
  assertBool (show actual <> " is not close to " <> show expected) (abs (expected - actual) < 1e-3)

materialized :: [MeasuredSegment] -> Float -> [Text]
materialized xs maxWidth =
  map (Break.materializeLineRange prep) (Break.layoutGreedy prep maxWidth)
  where
    prep = fromSegments xs

kept :: [MeasuredSegment] -> Float -> [Text]
kept xs maxWidth =
  map (Break.materializeLineRange prep) (Break.layoutGreedyWith Break.KeepWords prep maxWidth)
  where
    prep = fromSegments xs

stepped :: [MeasuredSegment] -> [Float] -> [Text]
stepped xs = go (Just (Cursor 0 0))
  where
    prep = fromSegments xs
    go _ [] = []
    go Nothing _ = []
    go (Just cursor) (w : rest) =
      case Break.layoutNextLineRange prep w cursor of
        Nothing -> []
        Just (line, next) -> Break.materializeLineRange prep line : go next rest

prepared, hyphenated, wideHyphen, broken, glued, rhythm, styled, indented :: [MeasuredSegment]
prepared = [word 50 "hello", space, word 50 "world"]
indented = [word 40 "main", seg HardBreak 0 "\n", word 20 "do", seg HardBreak 0 "\n", indent, word 80 "putStrLn"]
hyphenated = [word 20 "hy", seg SoftHyphen 5 "\xAD", word 30 "phen"]
wideHyphen = [word 20 "hy", seg SoftHyphen 15 "\xAD", word 9 "phen"]
broken = [word 30 "one", seg HardBreak 0 "\n", word 30 "two"]
glued = [word 30 "aaa", seg Glue 10 "\xA0", word 30 "bbb"]
rhythm = [word 30 "aaa", space, word 30 "bbb", space, word 30 "ccc"]
styled = [styledWord 1 30 "aaa", styledSpace 1, styledWord 1 30 "bbb", styledSpace 1, styledWord 2 30 "ccc"]

word :: Float -> Text -> MeasuredSegment
word = styledWord 0

styledWord :: Int -> Float -> Text -> MeasuredSegment
styledWord sk w t = MeasuredSegment{text = t, kind = Word, width = w, style = sk, graphemes}
  where
    n = Text.length t
    graphemes = replicate n (Grapheme 1 (w / fromIntegral n))

styledSpace :: Int -> MeasuredSegment
styledSpace sk = MeasuredSegment{text = " ", kind = Space, width = 10, style = sk, graphemes = [Grapheme 1 10]}

seg :: BreakKind -> Float -> Text -> MeasuredSegment
seg kind w t = MeasuredSegment{text = t, kind, width = w, style = 0, graphemes = [Grapheme (Text.length t) w]}

atom :: Float -> Text -> MeasuredSegment
atom w label = MeasuredSegment{text = label, kind = Atomic, width = w, style = 0, graphemes = [Grapheme (Text.length label) w]}

space, indent :: MeasuredSegment
space = seg Space 10 " "
indent = seg Space 20 "  "

fromSegments :: [MeasuredSegment] -> PreparedText
fromSegments xs = PreparedText{segments = Vector.fromList xs}