packages feed

nano-ui-svg-0.2.0.0: lib/NanoUI/Draw/FrameSvg.hs

-- |
-- Module      : NanoUI.Draw.FrameSvg
-- Description : Export whole painted nano-ui frames to SVG
-- Copyright   : (c) 2026 Zachary Churchill
-- License     : MIT
-- Maintainer  : zacharyachurchill@gmail.com
--
-- Render a whole painted frame of any view to an SVG document.
--
-- Paint lowers every widget straight to the vertex buffer. With
-- 'setDrawRecordOps' on, the shape emitters also record each solid shape they
-- draw, and each image, as the 'DrawOp' that draws it, under its layer and
-- clip. Drawings lower to the same emitters, so their shapes are recorded
-- with the widgets'. This module renders the recorded ops through
-- "NanoUI.Draw.Svg", with the text from 'collectRasterSpans'.
--
-- Call 'enableSvgExport' on the context before running frames. It records
-- the shapes, moves all text (including text inside drawings) onto the
-- raster spans consumed here, and draws rounded shapes round. Images, in
-- widgets and drawings alike, resolve their id through 'fsImageUri'.
--
-- > enableSvgExport ctx
-- > (_, dd, _) <- runFrame ctx inp view
-- > svg <- frameToSvg defaultFrameSvgConfig ctx inp dd
--
-- Text is fitted to the widths the context measured. A context from
-- 'NanoUI.Testing.newPixelContext' measures in a 16 px monospace grid, so
-- for text that reads like the app's, run the frames inside a backend
-- session (@withSdl@) that measures with the real font.
module NanoUI.Draw.FrameSvg
  ( FrameSvgConfig (..)
  , defaultFrameSvgConfig
  , enableSvgExport
  , frameToSvg
  )
where

import Control.Monad (forM, unless)
import Data.Bits (xor)
import Data.IORef (readIORef)
import Data.IntMap.Strict qualified as IM
import Data.List (find, partition, sortOn)
import Data.Map.Strict qualified as M
import Data.Primitive.PrimArray (PrimArray)
import Data.Primitive.SmallArray (smallArrayFromList)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Lazy qualified as TL
import Data.Text.Lazy.Builder qualified as TB
import Data.Word (Word32, Word64)
import Numeric (showHex)
import NanoUI
  ( Color (..)
  , FontVariant (..)
  , Input (..)
  , Rect (..)
  , Size (..)
  , colorA
  , rectIntersect
  )
import NanoUI.Widgets.Custom (DrawOp (..))
import NanoUI.Backend
  ( DrawData (..)
  , FontMetrics (..)
  , Layer (..)
  , collectRasterSpans
  , textNodeFontStyle
  , textNodeFontVariant
  , textNodeFontWeight
  , textNodeTextDecoration
  )
import NanoUI.Draw.Svg (SvgConfig (..), defaultSvgConfig, renderDrawOpsSvg)
import NanoUI.Draw.Svg.Attr
  ( attr
  , escapeXml
  , fillAttrs
  , fmtNum
  , fontStyleAttrs
  , numAttr
  , rectEl
  )
import NanoUI.Internal.Context
  ( Context (..)
  , RecordedOp (..)
  , activeEffectNodes
  , drawRecordedOps
  , getDrawRecordOps
  , lookupShaderEffectAt
  , setDrawExternalText
  , setDrawRecordOps
  , setDrawSquareGeometry
  )
import NanoUI.Internal.Context.Types (SpanCacheEntry (..))
import NanoUI.Internal.Layout.Arena
  ( NodeType (..)
  , arenaCount
  , getNodeRect
  , getNodeType
  , getStyleIdx
  , hasCenteredLabel
  , nodeFontBits
  )
import NanoUI.Internal.Path (ringBox)
import NanoUI.Internal.Shader (ShaderEffectEntry (..))

-- | Options for 'frameToSvg'.
data FrameSvgConfig = FrameSvgConfig
  { fsPrecision :: !Int
  -- ^ Maximum decimal places for coordinates and lengths (clamped to
  -- @0..6@). One or two digits suits a whole window.
  , fsFontFamily :: !Text
  -- ^ The @font-family@ spans render in. Text is fitted to its measured
  -- width with @textLength@, so any family that lays out similarly to the
  -- context's font reads correctly.
  , fsImageUri :: !(Int -> Maybe Text)
  -- ^ As 'svgImageUri': resolves an image's id ('NanoUI.ImageId') to an
  -- @href@ or data URI. Images without a resolution are omitted.
  , fsIdPrefix :: !Text
  -- ^ Prepended to every clip and gradient id in the document, so several
  -- documents inlined in one page keep their ids apart. Empty, the default,
  -- takes a prefix from a hash of the document, which differs between
  -- documents that differ.
  }

-- | One decimal place, a sans-serif stack, no image resolver, and an id
-- prefix from the document's hash.
defaultFrameSvgConfig :: FrameSvgConfig
defaultFrameSvgConfig =
  FrameSvgConfig
    { fsPrecision = 1
    , fsFontFamily = "Inter, 'Segoe UI', system-ui, sans-serif"
    , fsImageUri = const Nothing
    , fsIdPrefix = T.empty
    }

-- | Render the frame 'NanoUI.Backend.runFrame' returned for @inp@ into a standalone SVG
-- document sized to the input's window. Page text draws over the page's
-- shapes, and overlay shapes and text over both, in paint order for each.
--
-- Fails if the context was not recording shapes ('enableSvgExport'), which
-- would export the text alone.
frameToSvg :: FrameSvgConfig -> Context -> Input -> DrawData -> IO Text
frameToSvg cfg0 ctx inp dd = do
  recording <- getDrawRecordOps ctx
  unless recording $
    ioError (userError "frameToSvg: the context does not record shapes; call enableSvgExport before running the frame")
  let
    autoPrefix = T.null (fsIdPrefix cfg0)
    -- An automatic prefix is filled in once the document is known. The
    -- placeholder holds a character that text and attribute values always
    -- carry escaped, so it can only be one of the ids.
    cfg = if autoPrefix then cfg0 {fsIdPrefix = idPlaceholder} else cfg0
  (pageSpans, overlaySpans) <- collectRasterSpans ctx inp
  spanStyles <- collectSpanStyles ctx
  opacityZones <- collectSvgOpacityZones ctx
  recorded <- drawRecordedOps ctx
  let
    (pageShapes, overlayShapes) = frameShapes recorded
    Size vw vh = inputWindowSize inp
    p = max 0 (min 6 (fsPrecision cfg))
    fm = ctxFontMetrics ctx
    sizeRatio = ctxFontSize ctx / fmLineHeight fm
    vp = Rect 0 0 vw vh
    -- Each render numbers its ids from 1, so each has its own prefix.
    render scope runs =
      mconcat
        [ let subScope = if i == 0 then scope else scope <> T.pack (show i)
              inner = TB.fromText . renderDrawOpsSvg (fragCfg subScope) vp $ smallArrayFromList ops
           in if null ops
                then mempty
                else
                  if alpha < 0.9999
                    then "<g" <> attr "opacity" (fmtNum p alpha) <> ">" <> inner <> "</g>"
                    else inner
        | (i, (alpha, ops)) <- zip [0 :: Int ..] runs
        ]
    fragCfg scope =
      defaultSvgConfig
        { svgStandalone = False
        , svgPrecision = p
        , svgImageUri = fsImageUri cfg
        , svgIdPrefix = fsIdPrefix cfg <> scope
        }
    tclip = TB.fromText (fsIdPrefix cfg) <> "tclip_"
    (pageTextDefs, pageTextBody, nextTClip) = spanTexts p (fsFontFamily cfg) fm sizeRatio spanStyles opacityZones tclip 1 pageSpans
    (overlayTextDefs, overlayTextBody, lastTClip) = spanTexts p (fsFontFamily cfg) fm sizeRatio spanStyles opacityZones tclip nextTClip overlaySpans
    allDefs
      | lastTClip == 1 = mempty
      | otherwise = "<defs>" <> pageTextDefs <> overlayTextDefs <> "</defs>"
    doc =
      "<svg xmlns=\"http://www.w3.org/2000/svg\""
        <> numAttr 0 "width" vw
        <> numAttr 0 "height" vh
        <> attr "viewBox" ("0 0 " <> fmtNum 0 vw <> " " <> fmtNum 0 vh)
        <> ">"
        <> allDefs
        <> solidRect p vp (drawClear dd)
        <> render "s" pageShapes
        <> pageTextBody
        <> render "o" overlayShapes
        <> overlayTextBody
        <> "</svg>"
    out = TL.toStrict (TB.toLazyText doc)
  pure (if autoPrefix then T.replace idPlaceholder (documentIdPrefix out) out else out)

-- | Turn on what 'frameToSvg' reads from a context: shape recording
-- ('setDrawRecordOps'), text as spans ('setDrawExternalText'), and rounded
-- geometry ('setDrawSquareGeometry' off). Call it before running the frames
-- to export.
enableSvgExport :: Context -> IO ()
enableSvgExport ctx = do
  setDrawRecordOps ctx True
  setDrawExternalText ctx True
  setDrawSquareGeometry ctx False

-- | Stands for the automatic id prefix while the document is built.
idPlaceholder :: Text
idPlaceholder = "<id>"

-- | An id prefix from an FNV-1a hash of a document, as eight hex digits.
documentIdPrefix :: Text -> Text
documentIdPrefix t =
  let h = T.foldl' (\acc c -> (acc `xor` fromIntegral (fromEnum c)) * 16777619) (2166136261 :: Word32) t
      hex = showHex h ""
   in "n" <> T.pack (replicate (8 - length hex) '0' <> hex) <> "-"

--------------------------------------------------------------------------------
-- Shapes the paint recorded
--------------------------------------------------------------------------------

-- | The recorded shapes in paint order (layer by layer, as the commands
-- run), split into page shapes ('LayerBackground', 'LayerContent') and
-- overlay shapes ('LayerOverlay', 'LayerChrome'), grouped into consecutive
-- opacity runs for SVG @\<g opacity\>@ export.
frameShapes :: [RecordedOp] -> ([(Float, [DrawOp])], [(Float, [DrawOp])])
frameShapes recorded = (opacityRuns page, opacityRuns overlay)
 where
  (page, overlay) =
    partition (\(RecordedOp layer _ _ _) -> layer < LayerOverlay) $
      sortOn (\(RecordedOp layer _ _ _) -> layer) recorded
  opacityRuns [] = []
  opacityRuns (r@(RecordedOp _ _ a _) : rs) =
    let (same, rest) = span (\(RecordedOp _ _ a' _) -> abs (a' - a) < 1.0e-4) rs
     in (a, clipRuns (r : same)) : opacityRuns rest

-- | Ops cut to their clips: a rect (an upright or level stroke among them)
-- is cut to it, a shape inside it stands alone, one outside it is dropped,
-- and a run of shapes overhanging one clip share a 'PushClip' group.
clipRuns :: [RecordedOp] -> [DrawOp]
clipRuns = go . concatMap cut
 where
  cut (RecordedOp _ clip _ op) = case asRect op of
    FillRect r col -> [(Nothing, FillRect r' col) | Just r' <- [rectIntersect clip r]]
    _ -> case opBounds op of
      Just b
        | rectIntersect clip b == Nothing -> []
        | fullyInside b clip -> [(Nothing, op)]
      _ -> [(Just clip, op)]
  go [] = []
  go ((Nothing, op) : rest) = op : go rest
  go ((Just c, op) : rest) =
    let (same, rest') = span ((== Just c) . fst) rest
     in PushClip c : op : map snd same ++ PopClip : go rest'

-- | A straight stroke along an axis as the rect it fills; any other op as
-- it is.
asRect :: DrawOp -> DrawOp
asRect op = case op of
  Stroke x0 y0 x1 y1 bw col -> segment x0 y0 x1 y1 bw col
  StrokeLineAA x0 y0 x1 y1 bw col -> segment x0 y0 x1 y1 bw col
  _ -> op
 where
  segment x0 y0 x1 y1 bw col
    | x0 == x1 = FillRect (Rect (x0 - bw / 2) (min y0 y1) bw (abs (y1 - y0))) col
    | y0 == y1 = FillRect (Rect (min x0 x1) (y0 - bw / 2) (abs (x1 - x0)) bw) col
    | otherwise = op

-- | The box an op draws within, for the ops the emitters record; a rotated
-- image has none, and goes in its clip's group.
opBounds :: DrawOp -> Maybe Rect
opBounds = \case
  FillRect r _ -> Just r
  FillRoundedRect r _ _ -> Just r
  StrokeRoundedRect r _ _ _ -> Just r
  FillQuadGradient r _ _ _ _ -> Just r
  DrawImage r 0 _ _ _ _ _ _ -> Just r
  Stroke x0 y0 x1 y1 bw _ -> Just (segmentBox x0 y0 x1 y1 bw)
  StrokeLineAA x0 y0 x1 y1 bw _ -> Just (segmentBox x0 y0 x1 y1 bw)
  FillPolygon pts _ _ _ -> pointsBox 0 pts
  StrokePolyline pts w _ _ _ _ _ -> pointsBox (w / 2) pts
  _ -> Nothing
 where
  segmentBox x0 y0 x1 y1 bw =
    let hw = bw / 2 in Rect (min x0 x1 - hw) (min y0 y1 - hw) (abs (x1 - x0) + bw) (abs (y1 - y0) + bw)

-- | The box around x/y pairs, grown by @pad@ on every side.
pointsBox :: Float -> PrimArray Float -> Maybe Rect
pointsBox pad pts
  | x0 > x1 = Nothing
  | otherwise = Just (Rect (x0 - pad) (y0 - pad) (x1 - x0 + 2 * pad) (y1 - y0 + 2 * pad))
 where
  (x0, y0, x1, y1) = ringBox pts

--------------------------------------------------------------------------------
-- Text from the raster spans
--------------------------------------------------------------------------------

-- | Packed font-style codes for 'NodeText' spans, keyed by their exact
-- rects, and for text fields, buttons and selects, whose spans lie inside
-- them ('nodeFontBits').
collectSpanStyles :: Context -> IO (M.Map RectKey Word64, [(Rect, Word64)])
collectSpanStyles ctx = do
  sc <- readIORef (ctxSpanCache ctx)
  let
    textStyles =
      M.fromList
        [ (rectKey r, sceStyle e)
        | e <- IM.elems sc
        , sceStyle e /= 0
        , (r, _, _, _) <- sceSpans e
        ]
    na = ctxNodeArena ctx
  n <- arenaCount na
  inputStyles <-
    forM [0 .. n - 1] $ \i -> do
      nt <- getNodeType na i
      if nt == NodeTextInput || hasCenteredLabel nt
        then do
          si <- nodeFontBits nt <$> getStyleIdx na i
          if si /= 0
            then Just . (,si) <$> getNodeRect na i
            else pure Nothing
        else pure Nothing
  pure (textStyles, [st | Just st <- inputStyles])


-- | Collect active Tier 2 'seeSvgOpacity' containers as @(bounds, opacity)@
-- pairs so text spans inside @withShader groupOpacity@ fade with their group.
collectSvgOpacityZones :: Context -> IO [(Rect, Float)]
collectSvgOpacityZones ctx = do
  effNodes <- activeEffectNodes ctx
  if null effNodes
    then pure []
    else do
      let na = ctxNodeArena ctx
      zones <-
        forM effNodes $ \idx -> do
          mEntry <- lookupShaderEffectAt ctx idx
          case mEntry >>= seeSvgOpacity of
            Just op | op < 0.9999 -> Just . (,max 0 (min 1 op)) <$> getNodeRect na idx
            _ -> pure Nothing
      pure [z | Just z <- zones]

-- | Every span as clipped group and fitted text. Spans whose line box is
-- not fully inside their clip share one @clipPath@ per clip.
spanTexts ::
  Int -> Text -> FontMetrics -> Float -> (M.Map RectKey Word64, [(Rect, Word64)]) -> [(Rect, Float)] -> TB.Builder -> Int -> [(Rect, Text, Color, Color, Rect)] -> (TB.Builder, TB.Builder, Int)
spanTexts p fontFam fm ratio (textStyles, inputStyles) opacityZones clipPrefix startId spans = (defs, body, nextId)
 where
  baseLineH = fmLineHeight fm
  spaceAdv = fmAdvance fm ' '
  lookupStyle r0 = case M.lookup (rectKey r0) textStyles of
    Just si -> si
    Nothing -> maybe 0 snd (find (fullyInside r0 . fst) inputStyles)
  opacityFor (Rect x y w h) =
    let cx = x + w / 2
        cy = y + h / 2
        inZone (Rect zx zy zw zh) = cx >= zx && cx <= zx + zw && cy >= zy && cy <= zy + zh
     in foldl' (\acc (zr, op) -> if inZone zr then acc * op else acc) 1 opacityZones
  fontAttrs si =
    attr "font-family" (TB.fromText fam)
      <> fontStyleAttrs (textNodeFontWeight si) (textNodeFontStyle si) (textNodeTextDecoration si)
   where
    fam = case textNodeFontVariant si of
      FontMono -> "'Cascadia Mono', 'JetBrains Mono', monospace"
      _ -> fontFam
  trimSpan (Rect x y w h) t0 =
    let
      lead = T.length (T.takeWhile (== ' ') t0)
      trail = T.length (T.takeWhileEnd (== ' ') t0)
      t = T.strip t0
      spW = if baseLineH > 0 then spaceAdv * (h / baseLineH) else spaceAdv
      x' = x + fromIntegral lead * spW
      w' = max 1 (w - fromIntegral (lead + trail) * spW)
    in
      (Rect x' y w' h, t)
  kept =
    [ (r', t', fg, c, lookupStyle r, opacityFor r)
    | (r, t, fg, _, c) <- spans
    , let (r', t') = trimSpan r t
    , not (T.null t')
    , rectW r > 0.5
    , rectH r > 0.5
    , rectIntersect r c /= Nothing
    ]
  (defs, wrapClip, nextId) = clipDefs p clipPrefix startId [c | (r, _, _, c, _, _) <- kept, not (fullyInside r c)]
  body = mconcat (map el kept)
  el (r@(Rect x y w h), t, fg, c, si, op)
    | fullyInside r c = textEl
    | otherwise = wrapClip c textEl
   where
    textEl =
      "<text"
        <> numAttr p "x" x
        <> numAttr p "y" (y + h / 2)
        <> attr "dominant-baseline" "central"
        <> numAttr p "font-size" (h * ratio)
        <> fontAttrs si
        <> attr "xml:space" "preserve"
        <> fillAttrs p fg
        <> (if op < 0.9999 then attr "opacity" (fmtNum p op) else mempty)
        <> numAttr p "textLength" (max 1 w)
        <> attr "lengthAdjust" "spacing"
        <> ">"
        <> escapeXml t
        <> "</text>"

--------------------------------------------------------------------------------
-- SVG fragments
--------------------------------------------------------------------------------

-- | A rect's fields, as an ordered key.
type RectKey = (Float, Float, Float, Float)

rectKey :: Rect -> RectKey
rectKey (Rect x y w h) = (x, y, w, h)

-- | One @clipPath@ per distinct clip, numbered from @startId@ under @prefix@
-- in order of first use; a wrapper putting a fragment in its clip's group;
-- and the next free number.
clipDefs :: Int -> TB.Builder -> Int -> [Rect] -> (TB.Builder, Rect -> TB.Builder -> TB.Builder, Int)
clipDefs p prefix startId clips = (defs, wrap, startId + M.size ids)
 where
  (ids, ordered) = foldl' assign (M.empty, []) clips
  assign acc@(m, os) c
    | M.member (rectKey c) m = acc
    | otherwise = let i = startId + M.size m in (M.insert (rectKey c) i m, (c, i) : os)
  clipRef i = prefix <> TB.fromString (show i)
  defs =
    mconcat
      [ "<clipPath id=\"" <> clipRef i <> "\"><rect"
          <> numAttr p "x" (rectX c)
          <> numAttr p "y" (rectY c)
          <> numAttr p "width" (rectW c)
          <> numAttr p "height" (rectH c)
          <> "/></clipPath>"
      | (c, i) <- reverse ordered
      ]
  wrap c b =
    "<g clip-path=\"url(#" <> clipRef (M.findWithDefault 0 (rectKey c) ids) <> ")\">" <> b <> "</g>"

-- | A filled rect, or nothing for a transparent or empty one.
solidRect :: Int -> Rect -> Color -> TB.Builder
solidRect p r@(Rect _ _ w h) col
  | colorA col == 0 || w <= 0.01 || h <= 0.01 = mempty
  | otherwise = rectEl p r col

fullyInside :: Rect -> Rect -> Bool
fullyInside (Rect x y w h) (Rect sx sy sw sh) =
  x >= sx && y >= sy && x + w <= sx + sw && y + h <= sy + sh