packages feed

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

module Main (main) where

import Control.Monad
import Data.Char (chr, ord)
import Data.IORef
import Foreign hiding (void)
import Foreign.C (CInt, withCString)
import Prelude hiding (id)

import Control.Exception (ErrorCall(..), bracket, evaluate, throwIO, try)
import Data.ByteString (ByteString)
import Data.Either (isLeft)
import Data.ByteString qualified as ByteString
import Data.ByteString.Unsafe qualified as ByteString
import Data.List (sort)
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.Foreign qualified as Text
import System.Mem (performMajorGC)
import Test.Tasty
import Test.Tasty.HUnit

import KB.Text.Shape.FFI.API.Context qualified as ShapeContext
import KB.Text.Shape.FFI.API.Direct qualified as ShapeDirect
import KB.Text.Shape.FFI.API.Other qualified as Other
import KB.Text.Shape.FFI.API.Segmentation qualified as Segmentation
import KB.Text.Shape.FFI.Allocator qualified as Allocator
import KB.Text.Shape.FFI.Enums qualified as Enums
import KB.Text.Shape.FFI.Flags qualified as Flags
import KB.Text.Shape.FFI.Handles qualified as Handles
import KB.Text.Shape.FFI.Iterators qualified as Iterators
import KB.Text.Shape.FFI.Structs qualified as Structs

import KB.Text.Shape qualified as TextShape
import KB.Text.Shape.Font qualified as Font
import KB.Text.Shape.Segmentation qualified as TextSegmentation

testFontTtf :: FilePath
testFontTtf = "test/Ubuntu-R.ttf"

-- | Written on suite startup; also serves as the bench fixture.
testFontKbts :: FilePath
testFontKbts = "test/Ubuntu-R.kbts"

-- | Latin, Ethiopic, Hebrew and Devanagari runs. Ubuntu-R only has glyphs for the Latin parts.
testText :: Text
testText = "Hello, ሰላם።, שלמלך, नमस्ते world!"

longTestText :: Text
longTestText = Text.replicate 64 testText

main :: IO ()
main = do
  ttfData <- ByteString.readFile testFontTtf
  blobData <- Font.extractBlob ttfData 0
  ByteString.writeFile testFontKbts blobData
  defaultMain $ testGroup "kb-text-shape"
    [ fontTests ttfData blobData
    , contextTests ttfData blobData
    , directTests ttfData
    , segmentationTests
    , scriptTests
    , coverageTests ttfData
    , allocatorTests ttfData
    ]

-- * Fonts

fontTests :: ByteString -> ByteString -> TestTree
fontTests ttfData blobData = testGroup "fonts"
  [ testCase "kbts_FontCount sees one font in the TTF" $
      ByteString.unsafeUseAsCStringLen ttfData \(ptr, len) -> do
        count <- evaluate $ ShapeDirect.kbts_FontCount (castPtr ptr) (fromIntegral len)
        count @?= 1

  , testCase "createFont produces a valid font" $
      withFont ttfData \font -> do
        valid <- evaluate $ ShapeDirect.kbts_FontIsValid font
        assertBool "kbts_FontIsValid" (valid /= 0)

  , testCase "extractBlob output loads without another blob pass" $ do
      assertBool "blob is not empty" . not $ ByteString.null blobData
      Font.withLoader \font statePtr ->
        Font.loadFont blobData 0 font statePtr >>= \case
          Right Font.LoadFontReady -> pure ()
          Right Font.LoadFontNeedsBlob{} -> assertFailure "blob wants another blob pass"
          Left err -> assertFailure $ "blob failed to load: " <> show err

  , testCase "garbage font data is not counted as a font" $
      -- NB: this is as far as input validation goes, fonts are trusted input!
      ByteString.unsafeUseAsCStringLen (ByteString.replicate 1024 0x2A) \(ptr, len) -> do
        count <- evaluate $ ShapeDirect.kbts_FontCount (castPtr ptr) (fromIntegral len)
        count @?= 0

  , testCase "getFontInfo reports style and metrics" $
      withFont ttfData \font -> do
        info <- Font.getFontInfo font
        lookup Enums.FONT_INFO_STRING_ID_FAMILY info.strings @?= Just "Ubuntu"
        info.styleFlags @?= Flags.FONT_STYLE_FLAG_REGULAR
        info.weight @?= Enums.FONT_WEIGHT_NORMAL
        info.width @?= Enums.FONT_WIDTH_NORMAL
        info.unitsPerEm @?= 1000
        info.capitalHeight @?= 693
        info.ascent @?= 932
        info.descent @?= (-189)
        info.lineGap @?= 0
        (info.xMin, info.yMin, info.xMax, info.yMax) @?= (-211, -195, 1266, 958)

  , testCase "getFontInfo agrees between TTF and blob" $
      withFont ttfData \ttf ->
        withFont blobData \blob -> do
          ttfInfo <- Font.getFontInfo ttf
          blobInfo <- Font.getFontInfo blob
          blobInfo @?= ttfInfo

  , testCase "lookupFontString reads Windows-language name records" $
      withFont ttfData \font -> do
        family <- Font.lookupFontString font Enums.FONT_INFO_STRING_ID_FAMILY Enums.WINDOWS_LANGUAGE_ID_ENGLISH_UNITED_STATES
        family @?= Just "Ubuntu"
        missing <- Font.lookupFontString font Enums.FONT_INFO_STRING_ID_FAMILY Enums.WINDOWS_LANGUAGE_ID_JAPANESE_JAPAN
        missing @?= Nothing

  , testCase "kbts_WindowsIdsForLanguage lists en-US for English" $ do
      count <- ShapeDirect.kbts_WindowsIdsForLanguage Enums.LANGUAGE_ENGLISH nullPtr 0
      assertBool "English has Windows IDs" (count > 0)
      ids <- allocaArray (fromIntegral count) \idsPtr -> do
        matching <- ShapeDirect.kbts_WindowsIdsForLanguage Enums.LANGUAGE_ENGLISH idsPtr count
        matching @?= count
        peekArray (fromIntegral count) idsPtr
      assertBool "en-US is listed" (Enums.WINDOWS_LANGUAGE_ID_ENGLISH_UNITED_STATES `elem` ids)

  , testCase "Ubuntu-R exposes its variation axes" $
      withFont ttfData \font -> do
        (axisCount, instanceCount) <- variationInfo font
        (axisCount, instanceCount) @?= (2, 12)
        axes <- forM [0 .. axisCount - 1] \axisIndex ->
          alloca \infoPtr -> do
            found <- ShapeDirect.kbts_GetVariationAxisInfo font axisIndex infoPtr
            assertBool "axis index in bounds" (found /= 0)
            peek infoPtr
        [ (axis.tag, axis.minimumValue, axis.defaultValue, axis.maximumValue, axis.flags) | axis <- axes ]
          @?= [ (tagWord "wdth", 75, 100, 100, Flags.VARIATION_AXIS_FLAG_NONE)
              , (tagWord "wght", 100, 400, 800, Flags.VARIATION_AXIS_FLAG_NONE)
              ]
        names <- forM axes \axis -> englishName font axis.nameStringId
        names @?= [Just "Width", Just "Weight"]
        outOfBounds <- alloca $ ShapeDirect.kbts_GetVariationAxisInfo font axisCount
        outOfBounds @?= 0

  , testCase "variationAxes lists the axes in font order" $
      withFont ttfData \font -> do
        axes <- Font.variationAxes font
        [ (axis.tag, axis.minimumValue, axis.defaultValue, axis.maximumValue) | axis <- axes ]
          @?= [ (tagWord "wdth", 75, 100, 100)
              , (tagWord "wght", 100, 400, 800)
              ]

  , testCase "named instances carry coordinates and subfamily names" $
      withFont ttfData \font ->
        alloca \infoPtr ->
          allocaArray 2 \vectorPtr -> do
            found <- ShapeDirect.kbts_GetVariationInstance font 4 infoPtr vectorPtr
            assertBool "instance index in bounds" (found /= 0)
            info <- peek infoPtr
            info.flags @?= Flags.VARIATION_INSTANCE_FLAG_NONE
            englishName font info.subfamilyNameStringId >>= (@?= Just "Bold")
            peekArray 2 vectorPtr >>= (@?= [100, 700])

  , testCase "NormalizeVariationVector maps axis extremes to F2Dot14" $
      withFont ttfData \font -> do
        normalizeVariations font [100, 400] >>= (@?= [0, 0])
        normalizeVariations font [100, 800] >>= (@?= [0, 0x4000])
        normalizeVariations font [75, 100] >>= (@?= [-0x4000, -0x4000])

  , testCase "kbts_GetFontInfo (v1) agrees with the extended query" $
      withFont ttfData \font ->
        alloca \infoPtr -> do
          ShapeDirect.kbts_GetFontInfo font infoPtr
          Structs.FontInfo{styleFlags, weight, width} <- peek infoPtr
          info2 <- Font.getFontInfo font
          (styleFlags, weight, width) @?= (info2.styleFlags, info2.weight, info2.width)
  ]

-- * Context API

contextTests :: ByteString -> ByteString -> TestTree
contextTests ttfData blobData = testGroup "context API"
  [ testCase "font stack refcounting" $
      TextShape.withContext \ctx -> do
        font <- TextShape.pushFontFromMemory ctx ttfData 0
        uses <- TextShape.pushFont ctx font
        uses @?= 2
        pop1 <- TextShape.popFont ctx
        pop1 @?= (1, font)
        pop0 <- TextShape.popFont ctx
        pop0 @?= (0, font)

  , testCase "pushFontFromFile loads from disk" $
      TextShape.withContext \ctx -> do
        _font <- TextShape.pushFontFromFile ctx testFontTtf 0
        glyphs <- shapeText ctx "Abc"
        map (.codepoint) glyphs @?= "Abc"

  , testCase "pushFontFromFile loads a pre-distilled blob" $ do
      ttfGlyphs <- TextShape.withContext \ctx -> do
        _font <- TextShape.pushFontFromFile ctx testFontTtf 0
        shapeText ctx "Abc"
      blobGlyphs <- TextShape.withContext \ctx -> do
        _font <- TextShape.pushFontFromFile ctx testFontKbts 0
        shapeText ctx "Abc"
      blobGlyphs @?= ttfGlyphs

  , testCase "simple latin text is one LTR run" $
      withFontContext ttfData \ctx _font -> do
        results <- TextShape.run ctx $ TextShape.text_ "Hello!"
        (r, glyphs) <- expectOneRun results
        r.script @?= Enums.SCRIPT_LATIN
        r.direction @?= Enums.DIRECTION_LTR
        r.paragraphDirection @?= Enums.DIRECTION_LTR
        map (.codepoint) glyphs @?= "Hello!"
        map (.codepointIndex) glyphs @?= [0 .. 5]
        assertBool "all glyphs mapped" $ all (\g -> g.id /= 0) glyphs
        assertBool "positive advances" $ all (\g -> g.gpos.advanceX > 0) glyphs
        shapeError <- ShapeContext.kbts_ShapeError ctx.handle
        shapeError @?= Enums.SHAPE_ERROR_NONE

  , testCase "char_ feeds single codepoints" $
      withFontContext ttfData \ctx _font -> do
        results <- TextShape.run ctx do
          TextShape.char_ 'A'
          TextShape.char_ 'b'
        concatMap (map (.codepoint) . snd) results @?= "Ab"

  , testCase "multi-script text segments into runs" $
      withFontContext ttfData \ctx _font -> do
        results <- TextShape.run ctx $ TextShape.text_ testText
        let runs = map fst results
        assertBool "several runs" (length runs >= 4)
        forM_ [Enums.SCRIPT_LATIN, Enums.SCRIPT_ETHIOPIC, Enums.SCRIPT_HEBREW, Enums.SCRIPT_DEVANAGARI] \script ->
          assertBool ("has a run of " <> show script) $
            script `elem` map (.script) runs
        forM_ runs \r ->
          r.paragraphDirection @?= Enums.DIRECTION_LTR
        -- all input made it through, in logical order per run
        let allIndices = concatMap (map (.codepointIndex) . snd) results
        sort allIndices @?= [0 .. Text.length testText - 1]

  , testCase "RTL runs are flipped to visual order" $
      withFontContext ttfData \ctx _font -> do
        results <- TextShape.run ctx $ TextShape.text_ testText
        let rtl = [ x | x@(r, _) <- results, r.direction == Enums.DIRECTION_RTL ]
        assertBool "has RTL runs" . not $ null rtl
        forM_ rtl \(r, glyphs) -> do
          r.script @?= Enums.SCRIPT_HEBREW
          let indices = map (.codepointIndex) glyphs
          indices @?= reverse (sort indices)

  , testCase "TTF and blob shape identically" $ do
      ttfResults <- withFontContext ttfData \ctx _font ->
        TextShape.run ctx $ TextShape.text_ testText
      blobResults <- withFontContext blobData \ctx _font ->
        TextShape.run ctx $ TextShape.text_ testText
      -- font handles differ between contexts, compare everything else
      let strip (r, glyphs) = ((r.script, r.direction, r.paragraphDirection, r.flags), glyphs)
      map strip blobResults @?= map strip ttfResults

  , testCase "withFont_ splits the text into font runs" $
      TextShape.withContext \ctx -> do
        fontA <- TextShape.pushFontFromMemory ctx ttfData 0
        fontB <- TextShape.pushFontFromMemory ctx ttfData 0
        assertBool "two loads give distinct handles" (fontA /= fontB)
        results <- TextShape.run ctx do
          TextShape.text_ "abc"
          TextShape.withFont_ fontA $ TextShape.text_ "def"
          TextShape.text_ "ghi"
        concatMap (map (.codepoint) . snd) results @?= "abcdefghi"
        -- XXX: characterizes an upstream quirk: the font is sampled when
        -- pending breaks flush, not when codepoints are fed, so mid-input
        -- font switches land about one segment too early.
        -- Expected [(B, "abc"), (A, "def"), (B, "ghi")], but 2.32 gives:
        let named font
              | font == fontA = 'A'
              | font == fontB = 'B'
              | otherwise = '?'
        [ (named r.font, map (.codepoint) glyphs) | (r, glyphs) <- results ]
          @?= [('A', "abc"), ('B', "defghi")]

  , testCase "bottom-to-top font priority flag reverses the stack" $
      bracket (TextShape.createContextWith Flags.SHAPE_CONTEXT_FLAG_FONT_PRIORITY_BOTTOM_TO_TOP) TextShape.destroyContext \ctx -> do
        fontA <- TextShape.pushFontFromMemory ctx ttfData 0
        fontB <- TextShape.pushFontFromMemory ctx ttfData 0
        assertBool "two loads give distinct handles" (fontA /= fontB)
        results <- TextShape.run ctx $ TextShape.text_ "abc"
        map (\(r, _) -> r.font) results @?= [fontA]

  , testCase "context works in caller-provided fixed memory" $
      withFont ttfData \font ->
        withFixedMemoryContext (4 * 1024 * 1024) \ctx -> do
          _uses <- TextShape.pushFont ctx font
          glyphs <- shapeText ctx "Fixed"
          map (.codepoint) glyphs @?= "Fixed"
          -- fixed-memory contexts own no external allocations, nothing to destroy

  , testCase "a context out of memory fails the push and the run" $
      withFont ttfData \font ->
        withFixedMemoryContext 1024 \ctx -> do
          expectContextError Enums.SHAPE_ERROR_OUT_OF_MEMORY $ TextShape.pushFont ctx font
          expectContextError Enums.SHAPE_ERROR_OUT_OF_MEMORY $ TextShape.run ctx (TextShape.text_ "Abc")

  , testCase "pushFont rejects a font without a blob" $
      withFontContext ttfData \ctx font -> do
        expectFontError $ TextShape.pushFont ctx (Handles.Font nullPtr)
        popped <- TextShape.popFont ctx
        popped @?= (0, font)

  , testCase "withFont_ with a rejected font leaves the stack intact" $
      withFontContext ttfData \ctx font -> do
        expectFontError $ TextShape.run ctx do
          TextShape.text_ "abc"
          TextShape.withFont_ (Handles.Font nullPtr) $ TextShape.text_ "def"
        results <- TextShape.run ctx $ TextShape.text_ "Abc"
        map (\(r, _) -> r.font) results @?= [font]

  , testCase "pushFontFromMemory pops a font that fails validation" $
      withFontContext ttfData \ctx font -> do
        try @TextShape.ShapeException (TextShape.pushFontFromMemory ctx (withoutCapitalHeight ttfData) 0) >>= \case
          Left (TextShape.FontError _origin reason) -> reason @?= "font has no capital height (OS/2 sCapHeight)"
          other -> assertFailure $ "expected the capital height rejection, got " <> show other
        results <- TextShape.run ctx $ TextShape.text_ "Abc"
        map (\(r, _) -> r.font) results @?= [font]

  , testCase "pushFontFromFile reports a missing file" $
      TextShape.withContext \ctx ->
        expectFontError $ TextShape.pushFontFromFile ctx "test/does-not-exist.ttf" 0

  , testCase "feature overrides toggle ligatures" $
      withFontContext ttfData \ctx _font -> do
        ligated <- shapeText ctx "fi"
        length ligated @?= 1
        assertBool "ligature flag set" $
          all (\g -> g.flags .&. Flags.GLYPH_FLAG_LIGATURE /= mempty) ligated
        plain <- concatMap snd <$> TextShape.run ctx do
          TextShape.withFeature_ Enums.FEATURE_TAG_liga 0 $
            TextShape.text_ "fi"
        map (.codepoint) plain @?= "fi"

  , testCase "feature stack pop reports the removal" $
      withFontContext ttfData \ctx _font -> do
        popped <- newIORef (-1)
        _results <- TextShape.run ctx do
          TextShape.pushFeature_ Enums.FEATURE_TAG_liga 1
          TextShape.text_ "fi"
          TextShape.popFeature_ Enums.FEATURE_TAG_liga >>= writeIORef popped
        readIORef popped >>= (@?= 1)

  , testCase "withFeature_ drops its override when the section throws" $
      withFontContext ttfData \ctx _font -> do
        thrown <- try @ErrorCall $ TextShape.run ctx do
          TextShape.withFeature_ Enums.FEATURE_TAG_liga 0 do
            TextShape.text_ "fi"
            throwIO $ ErrorCall "section failed"
        assertBool "the section threw" (isLeft thrown)
        ligated <- shapeText ctx "fi"
        length ligated @?= 1

  , testCase "repeated feature tags keep the latest override" $
      withFontContext ttfData \ctx _font -> do
        onThenOff <- concatMap snd <$> TextShape.run ctx do
          TextShape.withFeature_ Enums.FEATURE_TAG_liga 1 $
            TextShape.withFeature_ Enums.FEATURE_TAG_liga 0 $
              TextShape.text_ "fi"
        map (.codepoint) onThenOff @?= "fi"
        afterRepeats <- concatMap snd <$> TextShape.run ctx do
          TextShape.withFeature_ Enums.FEATURE_TAG_kern 1 $
            TextShape.withFeature_ Enums.FEATURE_TAG_kern 1 $
              TextShape.withFeature_ Enums.FEATURE_TAG_liga 0 $
                TextShape.text_ "fi"
        map (.codepoint) afterRepeats @?= "fi"

  , testCase "shaped codepoints can be retrieved by index" $
      withFontContext ttfData \ctx _font -> do
        _results <- TextShape.run ctx $ TextShape.text_ "Hi"
        alloca \cpPtr -> do
          found <- ShapeContext.kbts_ShapeGetShapeCodepoint ctx.handle 1 cpPtr
          assertBool "codepoint 1 found" (found /= 0)
          cp <- peek cpPtr
          cp.codepoint @?= ord 'i'
          cp.userId @?= 1
          cp.featureOverrideCount @?= 0

  , testCase "codepoint iterator walks the input" $
      withFontContext ttfData \ctx _font -> do
        collected <- newIORef []
        _results <- TextShape.run ctx do
          TextShape.text_ "abc"
          alloca \itPtr -> alloca \cpPtr -> alloca \ixPtr -> do
            ShapeContext.hs_ShapeCurrentCodepointsIterator ctx.handle itPtr
            valid <- ShapeContext.kbts_ShapeCodepointIteratorIsValid itPtr
            assertBool "iterator is valid" (valid /= 0)
            let go acc = do
                  more <- ShapeContext.kbts_ShapeCodepointIteratorNext itPtr cpPtr ixPtr
                  if more /= 0
                    then do
                      cp <- peek cpPtr
                      ix <- peek ixPtr
                      go $ (fromIntegral ix, chr cp.codepoint) : acc
                    else pure $ reverse acc
            go [] >>= writeIORef collected
        readIORef collected >>= (@?= [(0 :: Int, 'a'), (1, 'b'), (2, 'c')])

  , testCase "codepoint iterator walks the input (v2)" $
      withFontContext ttfData \ctx _font -> do
        collected <- newIORef []
        _results <- TextShape.run ctx do
          TextShape.text_ "abc"
          alloca \itPtr -> alloca \cpPtr -> alloca \ixPtr -> do
            ShapeContext.hs_ShapeCurrentCodepointsIterator ctx.handle itPtr
            Structs.prepareCodepoint2 cpPtr
            let go acc = do
                  more <- ShapeContext.kbts_ShapeCodepointIteratorNext2 itPtr cpPtr ixPtr
                  if more /= 0
                    then do
                      cp <- peek cpPtr
                      ix <- peek ixPtr
                      go $ (fromIntegral ix, chr cp.codepoint) : acc
                    else pure $ reverse acc
            go [] >>= writeIORef collected
        readIORef collected >>= (@?= [(0 :: Int, 'a'), (1, 'b'), (2, 'c')])

  , testCase "Codepoint reports the paragraph direction" $
      withFontContext ttfData \ctx _font -> do
        feedText ctx Enums.DIRECTION_RTL "abc"
        _runs <- shapeRuns2 ctx
        alloca \cpPtr -> do
          found <- ShapeContext.kbts_ShapeGetShapeCodepoint ctx.handle 0 cpPtr
          assertBool "codepoint 0 found" (found /= 0)
          cp <- peek cpPtr
          cp.codepoint @?= ord 'a'
          cp.direction @?= Enums.DIRECTION_LTR
          cp.paragraphDirection @?= Enums.DIRECTION_RTL

  , testCase "Codepoint2 reports the paragraph direction" $
      withFontContext ttfData \ctx _font -> do
        feedText ctx Enums.DIRECTION_RTL "abc"
        _runs <- shapeRuns2 ctx
        alloca \cpPtr -> do
          Structs.prepareCodepoint2 cpPtr
          found <- ShapeContext.kbts_ShapeGetShapeCodepoint2 ctx.handle 0 cpPtr
          assertBool "codepoint 0 found" (found /= 0)
          cp <- peek cpPtr
          cp.codepoint @?= ord 'a'
          cp.direction @?= Enums.DIRECTION_LTR
          cp.paragraphDirection @?= Enums.DIRECTION_RTL

  , testCase "ShapeRun2 yields the same runs as ShapeRun" $ do
      viaRun <- withFontContext ttfData \ctx _font ->
        TextShape.run ctx $ TextShape.text_ testText
      viaRun2 <- withFontContext ttfData \ctx _font -> do
        feedText ctx Enums.DIRECTION_DONT_KNOW testText
        shapeRuns2 ctx
      let strip (r, glyphs) = ((r.script, r.direction, r.paragraphDirection, r.flags), glyphs)
      map strip viaRun2 @?= map strip viaRun

  , testCase "font variations change the shaped advances" $
      withFont ttfData \font ->
        TextShape.withContext \ctx -> do
          (_, regular) <- pushFontWith ctx font []
          regularAdvances <- advancesOf ctx "Abc"
          _ <- ShapeContext.kbts_ShapePopFont ctx.handle
          (handle, pushed) <- pushFontWith ctx font [Structs.Variation (tagWord "wght") 800]
          pushed @?= regular
          assertBool "font got a stack slot" (handle /= Handles.CONTEXT_FONT_HANDLE_INVALID)
          boldAdvances <- advancesOf ctx "Abc"
          assertBool "Extra Bold is wider" (sum boldAdvances > sum regularAdvances)
          applied <- ShapeContext.kbts_ShapeSetFontVariation ctx.handle handle (tagWord "wght") 400
          applied @?= 1
          advancesOf ctx "Abc" >>= (@?= regularAdvances)
          vectorSet <- withArray [100, 800] $ ShapeContext.kbts_ShapeSetFontVariationVector ctx.handle handle
          vectorSet @?= 1
          advancesOf ctx "Abc" >>= (@?= boldAdvances)
          setMany <- withArrayLen [Structs.Variation (tagWord "wght") 400, Structs.Variation (tagWord "slnt") 10] \count variationsPtr ->
            ShapeContext.kbts_ShapeSetFontVariations ctx.handle handle variationsPtr (fromIntegral count)
          setMany @?= 1
          advancesOf ctx "Abc" >>= (@?= regularAdvances)
          unsupported <- ShapeContext.kbts_ShapeSetFontVariation ctx.handle handle (tagWord "slnt") 10
          unsupported @?= 0

  , testCase "withFontVariations shapes the whole run with the axis values" $
      withFontContext ttfData \ctx font -> do
        extraBold <- TextShape.withContext \boldCtx -> do
          _ <- pushFontWith boldCtx font [wght 800]
          advancesOf boldCtx "Abc"
        regular <- advancesOf ctx "Abc"
        assertBool "Extra Bold is wider" (sum extraBold > sum regular)
        TextShape.withFontVariations ctx font [wght 800] (advancesOf ctx "Abc") >>= (@?= extraBold)
        advancesOf ctx "Abc" >>= (@?= regular)
        TextShape.popFont ctx >>= (@?= (0, font))

  , testCase "withFontVariations takes the last value of a repeated axis" $
      withFontContext ttfData \ctx font -> do
        extraBold <- TextShape.withFontVariations ctx font [wght 800] (advancesOf ctx "Abc")
        TextShape.withFontVariations ctx font [wght 300, wght 800] (advancesOf ctx "Abc") >>= (@?= extraBold)

  , testCase "withFontVariations keeps the body's context error" $
      withFont ttfData \font ->
        withFixedMemoryContext (16 * 1024) \ctx -> do
          thrown <- try $ TextShape.withFontVariations ctx font [] $
            TextShape.run ctx (TextShape.text_ longTestText)
          case thrown of
            Left (TextShape.ContextError origin err) -> do
              origin @?= "kbts_ShapeRun"
              err @?= Enums.SHAPE_ERROR_OUT_OF_MEMORY
            other -> assertFailure $ "expected the run's ContextError, got " <> show (void other)
          kept <- readIORef ctx.fonts
          assertBool "the pushed font was released" (null kept)

  , testCase "a font section at the end of the input shapes in the font below (upstream font_push_lags_input)" $
      withFontContext ttfData \ctx font -> do
        regular <- advancesOf ctx "Abc"
        section <- TextShape.run ctx $
          TextShape.withFontVariations_ font [wght 800] $
            TextShape.text_ "Abc"
        map (.advanceX) (concatMap snd section) @?= regular

  , testCase "withFontVariations_ without variations matches withFont_" $
      withFontContext ttfData \ctx font -> do
        plain <- TextShape.run ctx $ TextShape.withFont_ font $ TextShape.text_ "Abc"
        varied <- TextShape.run ctx $ TextShape.withFontVariations_ font [] $ TextShape.text_ "Abc"
        varied @?= plain

  , testCase "withFontVariations_ with a rejected font leaves the stack intact" $
      withFontContext ttfData \ctx font -> do
        expectFontError $ TextShape.run ctx do
          TextShape.withFontVariations_ (Handles.Font nullPtr) [wght 800] $ TextShape.text_ "Abc"
        results <- TextShape.run ctx $ TextShape.text_ "Abc"
        map (\(r, _) -> r.font) results @?= [font]

  , testCase "blob-loaded fonts keep their variations" $
      withFont blobData \blobFont ->
        withFont ttfData \ttfFont -> do
          fromBlob <- TextShape.withContext \ctx -> do
            _ <- pushFontWith ctx blobFont [wght 800]
            advancesOf ctx "Abc"
          fromTtf <- TextShape.withContext \ctx -> do
            _ <- pushFontWith ctx ttfFont [wght 800]
            advancesOf ctx "Abc"
          fromBlob @?= fromTtf
          axes <- Font.variationAxes blobFont
          map (.tag) axes @?= [tagWord "wdth", tagWord "wght"]

  , testCase "fonts pushed from file and memory take variations" $ do
      regular <- withFontContext ttfData \ctx _font -> advancesOf ctx "Abc"
      let bold = [Structs.Variation (tagWord "wght") 800]
      fromFile <- TextShape.withContext \ctx -> do
        withCString testFontTtf \pathPtr ->
          withArrayLen bold \count variationsPtr ->
            expectWrittenHandle ctx $
              ShapeContext.kbts_ShapePushFontFromFile2 ctx.handle pathPtr 0 variationsPtr (fromIntegral count)
        advancesOf ctx "Abc"
      fromMemory <- TextShape.withContext \ctx ->
        ByteString.unsafeUseAsCStringLen ttfData \(memoryPtr, memorySize) -> do
          withArrayLen bold \count variationsPtr ->
            expectWrittenHandle ctx $
              ShapeContext.kbts_ShapePushFontFromMemory2 ctx.handle (castPtr memoryPtr) (fromIntegral memorySize) 0 variationsPtr (fromIntegral count)
          advancesOf ctx "Abc"
      fromMemory @?= fromFile
      assertBool "Extra Bold is wider" (sum fromFile > sum regular)

  , testCase "failed pushes report the invalid context font handle" $
      TextShape.withContext \ctx -> do
        fromFile <- withCString "test/does-not-exist.ttf" \pathPtr ->
          failedPushHandle $ ShapeContext.kbts_ShapePushFontFromFile2 ctx.handle pathPtr 0 nullPtr 0
        fromMemory <- withArrayLen [1 .. 8 :: Word8] \size garbagePtr ->
          failedPushHandle $ ShapeContext.kbts_ShapePushFontFromMemory2 ctx.handle (castPtr garbagePtr) (fromIntegral size) 0 nullPtr 0
        fromFile @?= Handles.CONTEXT_FONT_HANDLE_INVALID
        fromMemory @?= Handles.CONTEXT_FONT_HANDLE_INVALID

  , testCase "ShapeRun2 reports the run's variation vector" $
      withFont ttfData \font ->
        TextShape.withContext \ctx -> do
          _ <- pushFontWith ctx font [Structs.Variation (tagWord "wght") 700]
          feedText ctx Enums.DIRECTION_DONT_KNOW "Abc"
          runs <- shapeRuns2 ctx
          assertBool "has runs" . not $ null runs
          forM_ runs \(run, _glyphs) -> do
            assertBool "vector is set" (run.variationVector /= nullPtr)
            peekArray 2 run.variationVector >>= (@?= [100, 700])

  , testCase "global variation pops keep the rest of the stack" $
      withFontContext ttfData \ctx _font -> do
        let weight = tagWord "wght"
            width = tagWord "wdth"
        pushes <- sequence
          [ ShapeContext.kbts_ShapePushGlobalVariation ctx.handle weight 300
          , ShapeContext.kbts_ShapePushGlobalVariation ctx.handle width 80
          ]
        pushes @?= [1, 1]
        pops <- mapM (ShapeContext.kbts_ShapePopGlobalVariation ctx.handle) [width, weight, weight]
        pops @?= [1, 1, 0]
        glyphs <- shapeText ctx "Abc"
        map (.codepoint) glyphs @?= "Abc"
  ]

-- * Direct API

directTests :: ByteString -> TestTree
directTests ttfData = testGroup "direct API"
  [ testCase "direct shaping matches the context pipeline" $
      withShapeSetup ttfData \font _config scratchpad -> do
        direct <- directShape font scratchpad nullGlyphConfig "Hello!"
        viaContext <- withFontContext ttfData \ctx _font -> shapeText ctx "Hello!"
        map (.id) direct @?= map (.id) viaContext
        map (.gpos) direct @?= map (.gpos) viaContext

  , testCase "a scratchpad can be reused across shaping calls" $
      withShapeSetup ttfData \font _config scratchpad -> do
        one <- directShape font scratchpad nullGlyphConfig "abc"
        two <- directShape font scratchpad nullGlyphConfig "abc"
        two @?= one

  , testCase "scratchpad in caller-provided fixed memory" $
      withFont ttfData \font ->
        withShapeConfig font \config -> do
          need <- evaluate $ ShapeDirect.kbts_SizeOfShapeScratchpad config
          assertBool "scratchpad size is known" (need > 0)
          let size = fromIntegral need + 4 * 1024 * 1024
          allocaBytes size \mem -> do
            scratchpad <- ShapeDirect.kbts_PlaceShapeScratchpadFixedMemory config (castPtr mem) (fromIntegral size)
            assertBool "scratchpad placed" (scratchpad /= Handles.ShapeScratchpad nullPtr)
            glyphs <- directShape font scratchpad nullGlyphConfig "Fixed"
            map (.codepoint) glyphs @?= "Fixed"

  , testCase "scratchpad placed at caller memory, allocating dynamically" $
      withFont ttfData \font ->
        withShapeConfig font \config -> do
          need <- evaluate $ ShapeDirect.kbts_SizeOfShapeScratchpad config
          allocaBytes (fromIntegral need) \mem -> do
            scratchpad <- ShapeDirect.kbts_PlaceShapeScratchpad config (castPtr mem) nullFunPtr nullPtr
            assertBool "scratchpad placed" (scratchpad /= Handles.ShapeScratchpad nullPtr)
            glyphs <- directShape font scratchpad nullGlyphConfig "Placed"
            map (.codepoint) glyphs @?= "Placed"
            ShapeDirect.kbts_DestroyShapeScratchpad scratchpad

  , testCase "shape config placed in caller memory" $
      withFont ttfData \font -> do
        size <- evaluate $ ShapeDirect.kbts_SizeOfShapeConfig font Enums.SCRIPT_LATIN Enums.LANGUAGE_DONT_KNOW
        assertBool "config size is known" (size > 0)
        allocaBytes (fromIntegral size) \mem -> do
          config <- ShapeDirect.kbts_PlaceShapeConfig font Enums.SCRIPT_LATIN Enums.LANGUAGE_DONT_KNOW (castPtr mem)
          assertBool "config placed" (config /= Handles.ShapeConfig nullPtr)
          withScratchpad config \scratchpad -> do
            glyphs <- directShape font scratchpad nullGlyphConfig "Placed"
            map (.codepoint) glyphs @?= "Placed"

  , testCase "ShapeDirect2 without variations matches ShapeDirect" $
      withShapeSetup ttfData \font _config scratchpad -> do
        viaShapeDirect <- directShape font scratchpad nullGlyphConfig "Hello!"
        viaShapeDirect2 <- directShape2 font scratchpad nullPtr "Hello!"
        viaShapeDirect2 @?= viaShapeDirect

  , testCase "ShapeDirect2 applies a normalized variation vector" $
      withShapeSetup ttfData \font _config scratchpad -> do
        regular <- directShape font scratchpad nullGlyphConfig "Abc"
        bold <- withArray [100, 800] \vectorPtr ->
          allocaArray 2 \normalizedPtr -> do
            ShapeDirect.kbts_NormalizeVariationVector font vectorPtr normalizedPtr
            directShape2 font scratchpad normalizedPtr "Abc"
        assertBool "Extra Bold is wider" (sum (map (.advanceX) bold) > sum (map (.advanceX) regular))

  , testCase "shape configs without variations share a UID across v1 and v2" $
      withFont ttfData \font -> do
        size <- ShapeDirect.kbts_SizeOfShapeConfig2 font Enums.SCRIPT_LATIN Enums.LANGUAGE_DONT_KNOW nullPtr
        sizeV1 <- evaluate $ ShapeDirect.kbts_SizeOfShapeConfig font Enums.SCRIPT_LATIN Enums.LANGUAGE_DONT_KNOW
        size @?= sizeV1
        allocaBytes (fromIntegral size) \mem -> alloca \placedUidPtr -> alloca \queriedUidPtr -> do
          config <- ShapeDirect.kbts_PlaceShapeConfig2 font Enums.SCRIPT_LATIN Enums.LANGUAGE_DONT_KNOW nullPtr (castPtr mem) placedUidPtr
          assertBool "config placed" (config /= Handles.ShapeConfig nullPtr)
          placedUid <- peek placedUidPtr
          assertBool "UID is set" (placedUid /= Structs.Uid 0 0 0 0)
          ShapeDirect.kbts_ShapeConfigUid config queriedUidPtr
          peek queriedUidPtr >>= (@?= placedUid)
          bracket
            (ShapeDirect.kbts_CreateShapeConfig font Enums.SCRIPT_LATIN Enums.LANGUAGE_DONT_KNOW nullFunPtr nullPtr)
            ShapeDirect.kbts_DestroyShapeConfig
            \configV1 -> do
              ShapeDirect.kbts_ShapeConfigUid configV1 queriedUidPtr
              peek queriedUidPtr >>= (@?= placedUid)

  , testCase "shape configs can be created for a variation" $
      withFont ttfData \font ->
        withArray [100, 800] \vectorPtr ->
          alloca \uidPtr ->
            bracket
              (ShapeDirect.kbts_CreateShapeConfig2 font Enums.SCRIPT_LATIN Enums.LANGUAGE_DONT_KNOW vectorPtr nullFunPtr nullPtr uidPtr)
              ShapeDirect.kbts_DestroyShapeConfig
              \config -> do
                assertBool "config created" (config /= Handles.ShapeConfig nullPtr)
                uid <- peek uidPtr
                assertBool "UID is set" (uid /= Structs.Uid 0 0 0 0)
                withScratchpad config \scratchpad -> do
                  glyphs <- allocaArray 2 \normalizedPtr -> do
                    ShapeDirect.kbts_NormalizeVariationVector font vectorPtr normalizedPtr
                    directShape2 font scratchpad normalizedPtr "Abc"
                  map (.codepoint) glyphs @?= "Abc"

  , testCase "glyph storage in caller-provided fixed memory" $
      withShapeSetup ttfData \font _config scratchpad -> do
        let size = 1024 * 1024
        allocaBytes size \mem ->
          alloca @Structs.GlyphStorage \storagePtr -> do
            ok <- ShapeDirect.kbts_InitializeGlyphStorageFixedMemory storagePtr (castPtr mem) (fromIntegral size)
            assertBool "storage initialized" (ok /= 0)
            forM_ (Text.unpack "abc") \c -> do
              glyphPtr <- ShapeDirect.kbts_PushGlyph storagePtr font (fromIntegral (ord c)) nullGlyphConfig 0
              assertBool "glyph pushed" (glyphPtr /= nullPtr)
            glyphs <- shapeStorage scratchpad storagePtr
            map (.codepoint) glyphs @?= "abc"

  , testCase "glyph configs accept feature overrides" $
      withShapeSetup ttfData \font config scratchpad -> do
        let overrides =
              [ Structs.FeatureOverride Enums.FEATURE_TAG_liga 0
              , Structs.FeatureOverride Enums.FEATURE_TAG_smcp 1
              , Structs.FeatureOverride Enums.FEATURE_TAG_salt 3 -- non-binary values are stored explicitly
              ]
        withArrayLen overrides \count overridesPtr -> do
          size <- evaluate $ ShapeDirect.kbts_SizeOfGlyphConfig config overridesPtr (fromIntegral count)
          assertBool "glyph config size is known" (size > 0)
          allocaBytes (fromIntegral size) \mem -> do
            placed <- ShapeDirect.kbts_PlaceGlyphConfig config overridesPtr (fromIntegral count) (castPtr mem)
            assertBool "glyph config placed" (placed /= nullGlyphConfig)
            glyphs <- directShape font scratchpad placed "fine"
            map (.codepoint) glyphs @?= "fine"
          bracket
            (ShapeDirect.kbts_CreateGlyphConfig config overridesPtr (fromIntegral count) nullFunPtr nullPtr)
            ShapeDirect.kbts_DestroyGlyphConfig
            \created -> do
              assertBool "glyph config created" (created /= nullGlyphConfig)
              glyphs <- directShape font scratchpad created "fine"
              map (.codepoint) glyphs @?= "fine"

  , testCase "codepoint to glyph mapping" $
      withFont ttfData \font -> do
        glyphIdA <- ShapeDirect.kbts_CodepointToGlyphId font (fromIntegral (ord 'A'))
        assertBool "'A' is mapped" (glyphIdA > 0)
        glyphIdHan <- ShapeDirect.kbts_CodepointToGlyphId font 0x6C49
        glyphIdHan @?= 0
        alloca @Structs.Glyph \glyphPtr -> do
          ShapeDirect.hs_CodepointToGlyph font (fromIntegral (ord 'A')) nullGlyphConfig 7 glyphPtr
          glyph <- peek glyphPtr
          glyph.codepoint @?= fromIntegral (ord 'A')
          fromIntegral glyph.id @?= glyphIdA
          glyph.userIdOrCodepointIndex @?= 7

  , testCase "clearing active glyphs empties the iterator" $
      withShapeSetup ttfData \font _config _scratchpad ->
        withGlyphStorage \storagePtr -> do
          forM_ (Text.unpack "abc") \c ->
            void $ ShapeDirect.kbts_PushGlyph storagePtr font (fromIntegral (ord c)) nullGlyphConfig 0
          alloca \itPtr -> do
            ShapeDirect.hs_ActiveGlyphIterator storagePtr itPtr
            valid <- evaluate $ Iterators.kbts_GlyphIteratorIsValid itPtr
            assertBool "iterator over pushed glyphs is valid" (valid /= 0)
          ShapeDirect.kbts_ClearActiveGlyphs storagePtr
          alloca \itPtr -> do
            ShapeDirect.hs_ActiveGlyphIterator storagePtr itPtr
            alloca \outPtr -> do
              more <- Iterators.kbts_GlyphIteratorNext itPtr outPtr
              more @?= 0
  ]

-- * Segmentation

segmentationTests :: TestTree
segmentationTests = testGroup "segmentation"
  [ testCase "guess text properties (UTF-8)" $ do
      guessUtf8 "hello there" >>= (@?= (Enums.DIRECTION_LTR, Enums.SCRIPT_LATIN))
      guessUtf8 "שלום" >>= (@?= (Enums.DIRECTION_RTL, Enums.SCRIPT_HEBREW))

  , testCase "guess text properties (UTF-32)" $ do
      guessUtf32 "hello" >>= (@?= (Enums.DIRECTION_LTR, Enums.SCRIPT_LATIN))
      guessUtf32 "مرحبا" >>= (@?= (Enums.DIRECTION_RTL, Enums.SCRIPT_ARABIC))

  , testCase "guess text properties (generic entry point)" $ do
      let text = "hello" :: Text
      Text.withCStringLen text \(ptr, len) ->
        alloca \directionPtr -> alloca \scriptPtr -> do
          poke directionPtr Enums.DIRECTION_DONT_KNOW
          poke scriptPtr Enums.SCRIPT_DONT_KNOW
          Segmentation.kbts_GuessTextProperties (castPtr ptr) (fromIntegral len) Enums.TEXT_FORMAT_UTF8 directionPtr scriptPtr
          direction <- peek directionPtr
          script <- peek scriptPtr
          (direction, script) @?= (Enums.DIRECTION_LTR, Enums.SCRIPT_LATIN)

  , testCase "BreakEntireStringUtf32 finds words and hard line breaks" $ do
      breaks <- breakEntireUtf32 "one two\nthree"
      assertBool "found breaks" . not $ null breaks
      assertBool "found a word break" $
        any (\b -> hasBreakFlag b.flags Flags.BREAK_FLAG_WORD) breaks
      let hard = [ b.position | b <- breaks, hasBreakFlag b.flags Flags.BREAK_FLAG_LINE_HARD ]
      hard @?= [8]

  , testCase "incremental break state agrees with the entire-string API" $ do
      let text = "one two\nthree four"
      whole <- breakEntireUtf32 text
      incremental <- breakIncremental text
      -- The entire-string API merges the flags of same-position breaks, but
      -- its output needs another merge pass: a late-arriving break can get
      -- merged into a zeroed hole (see 'withBreakBuffers') instead of the
      -- real record, splitting the position across two records.
      mergeBreaks incremental @?= mergeBreaks whole

  , testCase "Segmentation.breaks reports UTF-8 byte positions" $ do
      let text = "øne two\nthree fõur"
      whole <- breakEntireUtf8 text
      mergeBreaks (TextSegmentation.breaks text) @?= mergeBreaks whole

  , testCase "BreakEntireStringUtf8 agrees with UTF-32 on ASCII" $ do
      let text = "one two\nthree four"
      utf32 <- breakEntireUtf32 text
      utf8 <- breakEntireUtf8 text
      let key b = (b.position, b.flags)
      map key utf8 @?= map key utf32
  ]

-- * Scripts

scriptTests :: TestTree
scriptTests = testGroup "scripts"
  [ testCase "kbts_ScriptDirection" $ do
      Other.kbts_ScriptDirection Enums.SCRIPT_LATIN @?= Enums.DIRECTION_LTR
      Other.kbts_ScriptDirection Enums.SCRIPT_HEBREW @?= Enums.DIRECTION_RTL
      Other.kbts_ScriptDirection Enums.SCRIPT_ARABIC @?= Enums.DIRECTION_RTL

  , testCase "kbts_ScriptIsComplex" $ do
      Other.kbts_ScriptIsComplex Enums.SCRIPT_LATIN @?= 0
      assertBool "arabic is complex" $ Other.kbts_ScriptIsComplex Enums.SCRIPT_ARABIC /= 0
      assertBool "devanagari is complex" $ Other.kbts_ScriptIsComplex Enums.SCRIPT_DEVANAGARI /= 0

  , testCase "kbts_ScriptTagToScript decodes FOURCC tags" $ do
      Other.kbts_ScriptTagToScript (scriptTag "latn") @?= Enums.SCRIPT_LATIN
      Other.kbts_ScriptTagToScript (scriptTag "hebr") @?= Enums.SCRIPT_HEBREW
      Other.kbts_ScriptTagToScript (scriptTag "dev2") @?= Enums.SCRIPT_DEVANAGARI -- kbts uses the v2 Indic tags
  ]

-- * Font coverage

coverageTests :: ByteString -> TestTree
coverageTests ttfData = testGroup "font coverage"
  [ testCase "latin text is covered" $
      coverage "Hello" >>= (@?= True)
  , testCase "CJK text is not covered" $
      coverage "汉字" >>= (@?= False)
  ]
  where
    coverage :: Text -> IO Bool
    coverage text =
      withFont ttfData \font ->
        alloca @Structs.FontCoverageTest \testPtr -> do
          fillBytes testPtr 0 $ sizeOf (undefined :: Structs.FontCoverageTest)
          Other.kbts_FontCoverageTestBegin testPtr font
          forM_ (Text.unpack text) \c ->
            Other.kbts_FontCoverageTestCodepoint testPtr (fromIntegral (ord c))
          covered <- Other.kbts_FontCoverageTestEnd testPtr
          pure $ covered /= 0

-- * Allocator callback

allocatorTests :: ByteString -> TestTree
allocatorTests ttfData = testGroup "allocator"
  [ testCase "haskell allocator drives a scratchpad lifecycle" $
      withFont ttfData \font ->
        withShapeConfig font \config -> do
          allocCount <- newIORef (0 :: Int)
          liveSet <- newIORef (Set.empty :: Set (Ptr ()))
          Allocator.withAllocator (countingAllocator allocCount liveSet) \allocFn -> do
            scratchpad <- ShapeDirect.kbts_CreateShapeScratchpad config allocFn nullPtr
            assertBool "scratchpad created" (scratchpad /= Handles.ShapeScratchpad nullPtr)
            glyphs <- directShape font scratchpad nullGlyphConfig "Hello, world"
            map (.codepoint) glyphs @?= "Hello, world"
            ShapeDirect.kbts_DestroyShapeScratchpad scratchpad
          allocs <- readIORef allocCount
          assertBool "allocator was exercised" (allocs > 0)
          live <- readIORef liveSet
          live @?= Set.empty

  , testCase "haskell allocator drives a context lifecycle" $
      withFont ttfData \font -> do
        allocCount <- newIORef (0 :: Int)
        liveSet <- newIORef (Set.empty :: Set (Ptr ()))
        Allocator.withAllocator (collectingAllocator allocCount liveSet) \allocFn -> do
          handle <- ShapeContext.kbts_CreateShapeContext2 allocFn nullPtr Flags.SHAPE_CONTEXT_FLAG_NONE
          assertBool "context created" (handle /= Handles.ShapeContext nullPtr)
          fonts <- newIORef mempty
          let ctx = TextShape.Context{handle, fonts}
          _uses <- TextShape.pushFont ctx font
          let styled section = TextShape.withFeature_ Enums.FEATURE_TAG_liga 0 $ TextShape.withFont_ font section
          viaText <- TextShape.run ctx $ styled $ TextShape.text_ longTestText
          viaChars <- TextShape.run ctx $ styled $ mapM_ TextShape.char_ (Text.unpack longTestText)
          concatMap (map (.codepoint) . snd) viaChars @?= concatMap (map (.codepoint) . snd) viaText
          _popped <- TextShape.popFont ctx
          TextShape.destroyContext ctx
        allocs <- readIORef allocCount
        assertBool "allocator was exercised" (allocs > 0)
        live <- readIORef liveSet
        live @?= Set.empty
  ]

countingAllocator :: IORef Int -> IORef (Set (Ptr ())) -> Allocator.Allocator
countingAllocator allocCount liveSet _data opPtr = do
  op <- peek opPtr
  case op.kind of
    Allocator.OP_KIND_ALLOCATE -> do
      ptr <- mallocBytes $ fromIntegral op.size
      modifyIORef' allocCount (+ 1)
      modifyIORef' liveSet $ Set.insert ptr
      poke opPtr op{Allocator.pointer = ptr}
    Allocator.OP_KIND_FREE -> do
      modifyIORef' liveSet $ Set.delete op.pointer
      free op.pointer
    other ->
      error $ "unexpected allocator op: " <> show other

{- | A 'countingAllocator' that runs a major GC on every call.

The GC has to stop every capability.
A capability stuck in an @unsafe@ call never stops.
A callback from such a call deadlocks the suite.
-}
collectingAllocator :: IORef Int -> IORef (Set (Ptr ())) -> Allocator.Allocator
collectingAllocator allocCount liveSet allocatorData opPtr = do
  performMajorGC
  countingAllocator allocCount liveSet allocatorData opPtr

-- * Fixtures

-- | A loaded standalone font.
withFont :: ByteString -> (Handles.Font -> IO r) -> IO r
withFont bytes action =
  bracket (Font.createFont bytes 0) Font.destroyFont \fontData ->
    Font.withFontData fontData action

-- | A shaping context with one font pushed.
withFontContext :: ByteString -> (TextShape.Context -> Handles.Font -> IO r) -> IO r
withFontContext bytes action =
  TextShape.withContext \ctx -> do
    font <- TextShape.pushFontFromMemory ctx bytes 0
    action ctx font

withFixedMemoryContext :: Int -> (TextShape.Context -> IO r) -> IO r
withFixedMemoryContext spareBytes action = do
  contextSize <- evaluate ShapeContext.kbts_SizeOfShapeContext
  assertBool "context size is known" (contextSize > 0)
  allocaBytes (size contextSize) \mem -> do
    handle <- ShapeContext.kbts_PlaceShapeContextFixedMemory2 (castPtr mem) (fromIntegral (size contextSize)) Flags.SHAPE_CONTEXT_FLAG_NONE
    assertBool "context placed" (handle /= Handles.ShapeContext nullPtr)
    fonts <- newIORef mempty
    action TextShape.Context{handle, fonts}
  where
    size contextSize = fromIntegral contextSize + spareBytes

withShapeConfig :: Handles.Font -> (Handles.ShapeConfig -> IO r) -> IO r
withShapeConfig font =
  bracket
    (ShapeDirect.kbts_CreateShapeConfig font Enums.SCRIPT_DONT_KNOW Enums.LANGUAGE_DONT_KNOW nullFunPtr nullPtr)
    ShapeDirect.kbts_DestroyShapeConfig

withScratchpad :: Handles.ShapeConfig -> (Handles.ShapeScratchpad -> IO r) -> IO r
withScratchpad config =
  bracket
    (ShapeDirect.kbts_CreateShapeScratchpad config nullFunPtr nullPtr)
    ShapeDirect.kbts_DestroyShapeScratchpad

-- | Font, shape config and scratchpad for direct shaping.
withShapeSetup :: ByteString -> (Handles.Font -> Handles.ShapeConfig -> Handles.ShapeScratchpad -> IO r) -> IO r
withShapeSetup bytes action =
  withFont bytes \font ->
    withShapeConfig font \config ->
      withScratchpad config \scratchpad ->
        action font config scratchpad

withGlyphStorage :: (Ptr Structs.GlyphStorage -> IO r) -> IO r
withGlyphStorage action =
  alloca @Structs.GlyphStorage \ptr -> do
    ok <- ShapeDirect.kbts_InitializeGlyphStorage ptr nullFunPtr nullPtr
    assertBool "glyph storage initialized" (ok /= 0)
    result <- action ptr
    ShapeDirect.kbts_FreeAllGlyphs ptr
    pure result

nullGlyphConfig :: Handles.GlyphConfig
nullGlyphConfig = Handles.GlyphConfig nullPtr

-- * Helpers

shapeText :: TextShape.Context -> Text -> IO [TextShape.Glyph]
shapeText ctx text = concatMap snd <$> TextShape.run ctx (TextShape.text_ text)

feedText :: TextShape.Context -> Enums.Direction -> Text -> IO ()
feedText ctx paragraphDirection text = do
  ShapeContext.kbts_ShapeBegin ctx.handle paragraphDirection Enums.LANGUAGE_DONT_KNOW
  Text.withCStringLen text \(ptr, len) ->
    ShapeContext.kbts_ShapeUtf8 ctx.handle ptr (fromIntegral len) Enums.USER_ID_GENERATION_MODE_CODEPOINT_INDEX
  ShapeContext.kbts_ShapeEnd ctx.handle

shapeRuns2 :: TextShape.Context -> IO [(Structs.Run2, [TextShape.Glyph])]
shapeRuns2 ctx =
  alloca \runPtr ->
    alloca \glyphOutPtr ->
      collect runPtr glyphOutPtr
  where
    collect runPtr glyphOutPtr = do
      more <- ShapeContext.kbts_ShapeRun2 ctx.handle runPtr
      if more == 0
        then pure []
        else do
          run <- peek runPtr
          glyphs <- TextShape.iterateGlyphs glyphOutPtr (Structs.run2GlyphIterator runPtr)
          ((run, glyphs) :) <$> collect runPtr glyphOutPtr

expectOneRun :: [(TextShape.Run, [TextShape.Glyph])] -> IO (TextShape.Run, [TextShape.Glyph])
expectOneRun = \case
  [x] -> pure x
  other -> assertFailure $ "expected one run, got " <> show (length other)

directShape :: Handles.Font -> Handles.ShapeScratchpad -> Handles.GlyphConfig -> Text -> IO [TextShape.Glyph]
directShape font scratchpad glyphConfig text =
  withGlyphStorage \storagePtr -> do
    forM_ (Text.unpack text) \c ->
      ShapeDirect.kbts_PushGlyph storagePtr font (fromIntegral (ord c)) glyphConfig 0
    shapeStorage scratchpad storagePtr

directShape2 :: Handles.Font -> Handles.ShapeScratchpad -> Ptr Int16 -> Text -> IO [TextShape.Glyph]
directShape2 font scratchpad normalizedVector text =
  withGlyphStorage \storagePtr -> do
    forM_ (Text.unpack text) \c ->
      ShapeDirect.kbts_PushGlyph storagePtr font (fromIntegral (ord c)) nullGlyphConfig 0
    alloca \itPtr -> do
      err <- ShapeDirect.kbts_ShapeDirect2 scratchpad storagePtr Enums.DIRECTION_DONT_KNOW normalizedVector itPtr
      err @?= Enums.SHAPE_ERROR_NONE
      alloca \outPtr -> TextShape.iterateGlyphs outPtr itPtr

variationInfo :: Handles.Font -> IO (CInt, CInt)
variationInfo font =
  alloca \axisCountPtr ->
    alloca \instanceCountPtr -> do
      ShapeDirect.kbts_GetVariationInfo font axisCountPtr instanceCountPtr
      (,) <$> peek axisCountPtr <*> peek instanceCountPtr

normalizeVariations :: Handles.Font -> [Float] -> IO [Int16]
normalizeVariations font vector =
  withArrayLen vector \count vectorPtr ->
    allocaArray count \normalizedPtr -> do
      ShapeDirect.kbts_NormalizeVariationVector font vectorPtr normalizedPtr
      peekArray count normalizedPtr

englishName :: Handles.Font -> Word16 -> IO (Maybe Text)
englishName font nameId =
  Font.lookupFontString font (Enums.FontInfoStringId (fromIntegral nameId)) Enums.WINDOWS_LANGUAGE_ID_ENGLISH_UNITED_STATES

pushFontWith :: TextShape.Context -> Handles.Font -> [Structs.Variation] -> IO (Handles.ContextFontHandle, Handles.Font)
pushFontWith ctx font variations =
  withArrayLen variations \count variationsPtr ->
    alloca \handlePtr -> do
      pushed <- ShapeContext.kbts_ShapePushFont2 ctx.handle font variationsPtr (fromIntegral count) handlePtr
      handle <- peek handlePtr
      pure (handle, pushed)

withoutCapitalHeight :: ByteString -> ByteString
withoutCapitalHeight ttf = before <> ByteString.pack [0, 0] <> ByteString.drop 2 rest
  where
    (before, rest) = ByteString.splitAt (os2Offset + 88) ttf
    os2Offset = case [readU32 (record + 8) | record <- tableRecords, ByteString.take 4 (ByteString.drop record ttf) == "OS/2"] of
      offset : _ -> offset
      [] -> error "withoutCapitalHeight: the font has no OS/2 table"
    tableRecords = [12 + 16 * index | index <- [0 .. readU16 4 - 1]]
    readU16 at = fromIntegral (ByteString.index ttf at) * 0x100 + fromIntegral (ByteString.index ttf (at + 1))
    readU32 at = readU16 at * 0x10000 + readU16 (at + 2)

failedPushHandle :: (Ptr Handles.ContextFontHandle -> IO Handles.Font) -> IO Handles.ContextFontHandle
failedPushHandle push =
  alloca \handlePtr -> do
    poke handlePtr (Handles.ContextFontHandle 0x12345678)
    pushed <- push handlePtr
    pushed @?= Handles.Font nullPtr
    peek handlePtr

expectContextError :: Enums.ShapeError -> IO a -> Assertion
expectContextError expected action =
  try action >>= \case
    Left (TextShape.ContextError _origin err) -> err @?= expected
    Left other -> assertFailure $ "expected a ContextError, got " <> show other
    Right _ -> assertFailure "expected a ContextError, got no exception"

expectFontError :: IO a -> Assertion
expectFontError action =
  try action >>= \case
    Left (TextShape.FontError _origin _reason) -> pure ()
    Left other -> assertFailure $ "expected a FontError, got " <> show other
    Right _ -> assertFailure "expected a FontError, got no exception"

expectWrittenHandle :: TextShape.Context -> (Ptr Handles.ContextFontHandle -> IO Handles.Font) -> IO ()
expectWrittenHandle ctx push =
  alloca \handlePtr -> do
    poke handlePtr unwritten
    pushed <- push handlePtr
    assertBool "font pushed" (pushed /= Handles.Font nullPtr)
    handle <- peek handlePtr
    assertBool "context font handle written" (handle /= unwritten)
    applied <- ShapeContext.kbts_ShapeSetFontVariation ctx.handle handle (tagWord "wght") 800
    applied @?= 1
  where
    unwritten = Handles.ContextFontHandle 0x12345678

advancesOf :: TextShape.Context -> Text -> IO [Int]
advancesOf ctx text = map (.advanceX) <$> shapeText ctx text

shapeStorage :: Handles.ShapeScratchpad -> Ptr Structs.GlyphStorage -> IO [TextShape.Glyph]
shapeStorage scratchpad storagePtr =
  alloca \itPtr -> do
    err <- ShapeDirect.kbts_ShapeDirect scratchpad storagePtr Enums.DIRECTION_DONT_KNOW itPtr
    err @?= Enums.SHAPE_ERROR_NONE
    alloca \outPtr -> TextShape.iterateGlyphs outPtr itPtr

guessUtf8 :: Text -> IO (Enums.Direction, Enums.Script)
guessUtf8 text =
  Text.withCStringLen text \(ptr, len) ->
    alloca \directionPtr -> alloca \scriptPtr -> do
      poke directionPtr Enums.DIRECTION_DONT_KNOW
      poke scriptPtr Enums.SCRIPT_DONT_KNOW
      Segmentation.kbts_GuessTextPropertiesUtf8 ptr (fromIntegral len) directionPtr scriptPtr
      (,) <$> peek directionPtr <*> peek scriptPtr

guessUtf32 :: Text -> IO (Enums.Direction, Enums.Script)
guessUtf32 text =
  withArrayLen (map (fromIntegral . ord) (Text.unpack text) :: [CInt]) \len ptr ->
    alloca \directionPtr -> alloca \scriptPtr -> do
      poke directionPtr Enums.DIRECTION_DONT_KNOW
      poke scriptPtr Enums.SCRIPT_DONT_KNOW
      Segmentation.kbts_GuessTextPropertiesUtf32 ptr (fromIntegral len) directionPtr scriptPtr
      (,) <$> peek directionPtr <*> peek scriptPtr

hasBreakFlag :: Flags.BreakFlags -> Flags.BreakFlags -> Bool
hasBreakFlag flags flag = flags .&. flag == flag

-- | Collect breaks with 1-per-codepoint positions and a generous output capacity.
breakEntireUtf32 :: Text -> IO [Structs.Break]
breakEntireUtf32 text =
  withArrayLen (map (fromIntegral . ord) (Text.unpack text) :: [CInt]) \len codepointsPtr ->
    withBreakBuffers len \breaksPtr breakCountPtr flagsPtr flagCountPtr -> do
      Segmentation.kbts_BreakEntireStringUtf32
        Enums.DIRECTION_DONT_KNOW
        Enums.JAPANESE_LINE_BREAK_STYLE_NORMAL
        mempty
        (castPtr codepointsPtr)
        (fromIntegral len)
        breaksPtr
        (fromIntegral $ breakCapacity len)
        breakCountPtr
        flagsPtr
        (fromIntegral len)
        flagCountPtr

breakEntireUtf8 :: Text -> IO [Structs.Break]
breakEntireUtf8 text =
  Text.withCStringLen text \(textPtr, len) ->
    withBreakBuffers len \breaksPtr breakCountPtr flagsPtr flagCountPtr -> do
      Segmentation.kbts_BreakEntireStringUtf8
        Enums.DIRECTION_DONT_KNOW
        Enums.JAPANESE_LINE_BREAK_STYLE_NORMAL
        mempty
        textPtr
        (fromIntegral len)
        breaksPtr
        (fromIntegral $ breakCapacity len)
        breakCountPtr
        flagsPtr
        (fromIntegral len)
        flagCountPtr

breakCapacity :: Int -> Int
breakCapacity len = 4 * len + 16

{- | Prepare output buffers for a @BreakEntireString*@ call and read them back.

Upstream (still in 2.32) advances its write cursor and the final count even
when a break gets merged into a previously written position, leaving unwritten
holes in the output array. The vendored copy patches that. The buffer is still
pre-zeroed and empty-flag records dropped as insurance. A real break always
carries at least one flag.
-}
withBreakBuffers
  :: Int
  -> (Ptr Structs.Break -> Ptr CInt -> Ptr Flags.BreakFlags -> Ptr CInt -> IO ())
  -> IO [Structs.Break]
withBreakBuffers len fill =
  allocaArray capacity \breaksPtr ->
    alloca \breakCountPtr ->
      allocaArray len \flagsPtr ->
        alloca \flagCountPtr -> do
          fillBytes breaksPtr 0 $ capacity * sizeOf (undefined :: Structs.Break)
          fill breaksPtr breakCountPtr flagsPtr flagCountPtr
          count <- peek breakCountPtr
          breaks <- peekArray (min capacity (fromIntegral count)) breaksPtr
          pure $ filter (\b -> b.flags /= mempty) breaks
  where
    capacity = breakCapacity len

-- | Merge same-position breaks the way @kbts_BreakEntireString@ does.
mergeBreaks :: [Structs.Break] -> [(Int, Flags.BreakFlags)]
mergeBreaks = foldl insert []
  where
    insert acc b =
      case break (\(position, _flags) -> position == b.position) acc of
        (before, (position, flags) : next) -> before <> ((position, flags <> b.flags) : next)
        (_, []) -> acc <> [(b.position, b.flags)]

breakIncremental :: Text -> IO [Structs.Break]
breakIncremental text =
  allocaBytes Handles.sizeOfBreakState \raw -> do
    fillBytes raw 0 Handles.sizeOfBreakState
    let state = Handles.BreakState (castPtr raw)
    Segmentation.kbts_BreakBegin state Enums.DIRECTION_DONT_KNOW Enums.JAPANESE_LINE_BREAK_STYLE_NORMAL mempty
    alloca \breakPtr -> do
      let drain acc = do
            more <- Segmentation.kbts_Break state breakPtr
            if more /= 0
              then do
                b <- peek breakPtr
                drain $ b : acc
              else pure acc
          feed acc (c, isLast) = do
            Segmentation.kbts_BreakAddCodepoint state (fromIntegral (ord c)) 1 (if isLast then 1 else 0)
            drain acc
          codepoints = Text.unpack text
          annotated = zip codepoints $ map (== length codepoints) [1 ..]
      reverse <$> foldM feed [] annotated

scriptTag :: Text -> Other.ScriptTag
scriptTag = Other.ScriptTag . tagWord

tagWord :: Text -> Word32
tagWord tag = case Text.unpack tag of
  [a, b, c, d] -> Enums.fourcc a b c d
  other -> error $ "tagWord: expected 4 characters, got " <> show other

wght :: Float -> TextShape.Variation
wght = TextShape.Variation (TextShape.fourcc 'w' 'g' 'h' 't')