nano-ui-0.2.0.0: lib/NanoUI/Internal/Draw.hs
{-# LANGUAGE StrictData #-}
-- | Draw layer facade: data types, arena and geometry, plus the text emitters
-- (plain and synthetic-styled) and the 'DrawOp' interpreter.
module NanoUI.Internal.Draw
( Layer (..)
, DrawCmd (..)
, ShaderCall (..)
, EffectSpan (..)
, emptyShaderCalls
, emptyEffectSpans
, emptyUniformBytes
, DrawData (..)
, DrawFrameExtra (..)
, DrawArena (..)
, RecordedOp (..)
, DrawOp (..)
, LineCap (..)
, LineJoin (..)
, Shade (..)
, TextFont (..)
, defaultTextFont
, DrawingBuild
, shiftDrawOp
, newDrawArena
, resetDrawArena
, setDrawSnapScale
, getDrawSnapScale
, setDrawPixelScale
, setDrawSquareGeometry
, setDrawClear
, getDrawClear
, setDrawExternalText
, setDrawRecordOps
, getDrawRecordOps
, withRecordOpacity
, recordedOps
, beginLayer
, currentLayer
, currentClip
, setClip
, setClipPieces
, getClipPieces
, withClip
, pushShaderQuad
, EffectSpanToken
, beginEffectSpan
, endEffectSpan
, finishDraw
, drawCmdCount
, drawnVertexCount
, drawCmdNull
, forDrawCmdsInLayer_
, drawCmdElems
, cmdEffectiveClip
, vertexSize
, indexSize
, backdropDimTextureId
, glyphAtlasTextureId
, glyphAtlasPages
, glyphPageTextureId
, textureGlyphPage
, pushRect
, pushQuadGradient
, pushImage
, pushRoundedRect
, pushRoundedRectRaw
, pushRoundedStroke
, pushLine
, pushFilledTriangle
, pushPolylineAA
, points3
, drawTextBox
, glyphSlackLines
, pushText
, pushPreparedTextStyled
, emitDrawOps
, pushImageOp
, pushShapeOp
, checkboxOps
) where
import Control.Monad (unless, when)
import Data.Char (isSpace)
import Data.IORef (modifyIORef', readIORef)
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import qualified Data.Text.Foreign as TF
import qualified Data.Text.Unsafe as TU
import Data.Primitive.SmallArray (SmallArray, indexSmallArray, sizeofSmallArray)
import Data.Primitive.PrimArray (PrimArray, indexPrimArray, readPrimArray, sizeofPrimArray)
import Data.Word (Word32, Word8)
import Foreign.Ptr (Ptr)
import NanoUI.Internal.Draw.Arena
import NanoUI.Internal.Draw.Shapes
import NanoUI.Internal.Draw.Types
import NanoUI.Internal.Font
import NanoUI.Internal.SIMD (pokeQuadCornersSIMD, pokeQuadSIMD)
import NanoUI.Internal.Style (FontStyle (..), FontWeight (..), TextDecoration (..), Theme (..), resolveFontSize, styleBg)
import NanoUI.Internal.Types (Color (..), Rect (..), onGrid, rectInflate, rectIntersect, spanEffectiveClip)
-- | The line box, in logical pixels, that a 'DrawText' of the text anchored
-- at @(x, y)@ by @ax@ and @ay@ occupies, measured with @fm@'s advances.
drawTextBox :: FontMetrics -> Float -> Float -> Float -> Float -> T.Text -> Rect
drawTextBox fm x y ax ay t =
let tw = lineWidth fm t
px = x - tw * max 0 ax
in Rect px (textBoxTop fm y ay) tw (fmLineHeight fm)
-- | Top of a 'drawTextBox' line box anchored at @y@ by @ay@.
textBoxTop :: FontMetrics -> Float -> Float -> Float
textBoxTop fm y ay
| ay < 0 = y - fmAscent fm
| otherwise = y - fmLineHeight fm * (1 - ay)
-- | Intersect a command's clip rectangle with an optional damage rectangle.
-- Returns 'Nothing' when the command is culled, @Just Nothing@ when both the
-- command clip and damage are open, and @Just (Just clip)@ for a concrete clip.
{-# INLINE cmdEffectiveClip #-}
cmdEffectiveClip :: Maybe Rect -> DrawCmd -> Maybe (Maybe Rect)
cmdEffectiveClip mDamage cmd =
spanEffectiveClip mDamage (Rect (cmdClipX cmd) (cmdClipY cmd) (cmdClipW cmd) (cmdClipH cmd))
{-# INLINE pushText #-}
pushText :: DrawArena -> FontMetrics -> Float -> Float -> T.Text -> Color -> IO ()
pushText _da _fm _x _y txt _col | T.null txt = pure ()
pushText da fm x y txt col = do
prepared <- prepareFontMetrics fm txt
external <- readIORef (daExternalText da)
unless external $ pushPreparedTextQuads da prepared x y txt col
-- | Styled text with metrics already prepared for @txt@.
pushPreparedTextStyled ::
DrawArena ->
FontMetrics ->
FontWeight ->
FontStyle ->
TextDecoration ->
Float ->
Float ->
T.Text ->
Color ->
IO ()
pushPreparedTextStyled da prepared weight fstyle deco x y txt col = do
external <- readIORef (daExternalText da)
unless external $ pushPreparedTextStyledQuads da prepared weight fstyle deco x y txt col
-- Snapping the pen to the device pixel grid keeps every glyph quad on a whole
-- pixel. Advances, bearings, and ink sizes are all integer pixel counts divided
-- by the snap scale, so snapping the origin alone aligns the whole line:
-- otherwise fractional layout positions leave glyphs straddling pixel
-- boundaries, which makes nearest-sampled atlas text blurry and jitter as
-- scroll position changes.
pushPreparedTextQuads :: DrawArena -> FontMetrics -> Float -> Float -> T.Text -> Color -> IO ()
pushPreparedTextQuads da fm x y txt col = do
let !px = onGrid (fmSnapScale fm) x
!py = onGrid (fmSnapScale fm) y
-- The host's shaped glyphs when it shapes, otherwise glyphs by character.
drawShaped fm txt >>= \case
Just glyphs -> pushShapedQuads da fm 0 px py glyphs col
Nothing -> pushGlyphQuads da fm 0 px py txt col
-- | How far a glyph's x may fall behind a glyph before it in a line, and how
-- wide a glyph may be, in line heights. Pens only move right in visual order:
-- a glyph lands left of an earlier one only by a negative bearing or as a
-- mark over the glyph before it, and no glyph is wider than a few ems. The
-- clip skips in 'pushShapedQuads' and 'pushGlyphQuads' rely on it.
glyphSlackLines :: Float
glyphSlackLines = 4
-- | Horizontal clip bounds expanded by synthetic-oblique shear lean, plus the
-- conservative glyph slack in logical pixels.
{-# INLINE glyphClipBounds #-}
glyphClipBounds :: DrawArena -> FontMetrics -> Float -> IO (Float, Float, Float)
glyphClipBounds da fm slant = do
cx <- readPrimArray (daCurrentClip da) 0
cw <- readPrimArray (daCurrentClip da) 2
let !lean = abs slant * (fmLineHeight fm + abs (fmAscent fm))
pure (cx - lean, cx + cw + lean, glyphSlackLines * fmLineHeight fm)
-- | Binary search for the first glyph in @[lo0, hi0)@ whose x reaches @thr@.
firstGlyphReaching :: PrimArray Float -> Float -> Float -> Int -> Int -> Int
firstGlyphReaching quads !px !thr !lo0 !hi0 = go lo0 hi0
where
go !lo !hi
| lo >= hi = lo
| otherwise =
let !mid = (lo + hi) `quot` 2
in if px + indexPrimArray quads (mid * 8) < thr
then go (mid + 1) hi
else go lo mid
-- | Emit shaped glyph quads on atlas @page@ from @q0@ up to @end@, recursing
-- only when a glyph lies on another atlas page. Keeping the inner loop strictly
-- tail-recursive lets GHC compile it as a zero-allocation join point.
drawShapedPages ::
DrawArena ->
PrimArray Float ->
Float -> Float -> Float -> Float -> Float -> Float -> Float -> Float -> Float -> Float ->
Int -> Int -> Int ->
IO ()
drawShapedPages da quads !slant !px !py !left !right !baselineY !r !g !b !a !end !q0 !page = do
setTexture da (glyphPageTextureId page)
let !shown = end - q0
!pageU = fromIntegral page
(vp, ip, base, baseIdx) <- reserveRaw da (shown * 4) (shown * 6)
let go !q !m
| q >= end = do
commitRaw da base baseIdx (m * 4) (m * 6)
pure end
| otherwise = do
let !o = q * 8
!gx = px + indexPrimArray quads o
!gw = indexPrimArray quads (o + 2)
!u0 = indexPrimArray quads (o + 4)
if gx + gw < left || gx > right
then go (q + 1) m
else if u0 < pageU || u0 >= pageU + 1
then
-- A glyph whose u is on no page is skipped.
if u0 < 0 || u0 >= fromIntegral glyphAtlasPages
then go (q + 1) m
else do
commitRaw da base baseIdx (m * 4) (m * 6)
pure q
else do
let !gy = py + indexPrimArray quads (o + 1)
!gh = indexPrimArray quads (o + 3)
!v0 = indexPrimArray quads (o + 5)
!u1 = indexPrimArray quads (o + 6)
!v1 = indexPrimArray quads (o + 7)
pokeGlyphQuad vp ip base baseIdx slant baselineY r g b a m gx gy gw gh (u0 - pageU) v0 (u1 - pageU) v1
go (q + 1) (m + 1)
!nextQ <- go q0 0
when (nextQ < end) $
drawShapedPages da quads slant px py left right baselineY r g b a end nextQ (truncate (indexPrimArray quads (nextQ * 8 + 4)))
-- | A shaped line's glyph quads from pen @(px, py)@, sheared by @slant@
-- around the baseline like 'pushGlyphQuads'. Glyphs wholly left or right of
-- the current clip emit nothing. Glyph x offsets rise in visual order, to
-- within 'glyphSlackLines', so binary searches find the first glyph that
-- can reach the clip and the first that starts well past it: a long line in
-- a narrow view costs, and reserves buffer room for, the glyphs it shows
-- plus two searches. Glyphs on another atlas page than the one before them
-- start a new draw command on that page's texture.
pushShapedQuads :: DrawArena -> FontMetrics -> Float -> Float -> Float -> ShapedGlyphs -> Color -> IO ()
pushShapedQuads da fm !slant !px !py (ShapedGlyphs quads) !col = do
let !count = sizeofPrimArray quads `quot` 8
when (count > 0) $ do
(!left, !right, !slack) <- glyphClipBounds da fm slant
let -- Every glyph before @start@ ends left of the clip, and every glyph
-- from @end@ on starts right of it.
!start = firstGlyphReaching quads px (left - slack) 0 count
!end = firstGlyphReaching quads px (right + slack) start count
when (start < end) $ do
let !(r, g, b, a) = unpackColorF col
!baselineY = py + fmAscent fm
!uFirst = indexPrimArray quads (start * 8 + 4)
!startPage =
if uFirst >= 0 && uFirst < fromIntegral glyphAtlasPages
then truncate uFirst
else 0
drawShapedPages da quads slant px py left right baselineY r g b a end start startPage
-- | Glyph quad @q@ of a text reservation whose vertices start at @base@ and
-- indices at @baseIdx@. A non-zero @slant@ shears the quad around
-- @baselineY@. INLINE: it runs per glyph and takes more arguments than GHC
-- unboxes for a call.
{-# INLINE pokeGlyphQuad #-}
pokeGlyphQuad ::
Ptr Word8 ->
Ptr Word8 ->
Int ->
Int ->
Float ->
Float ->
Float ->
Float ->
Float ->
Float ->
Int ->
Float ->
Float ->
Float ->
Float ->
Float ->
Float ->
Float ->
Float ->
IO ()
pokeGlyphQuad vp ip base baseIdx slant baselineY r g b a q gx gy gw gh u0 v0 u1 v1 = do
let !vb = (base + q * 4) * vertexSize
!ib = (baseIdx + q * 6) * indexSize
!i0 = fromIntegral (base + q * 4) :: Word32
if slant == 0
then pokeQuadSIMD vp vb ip ib gx gy gw gh u0 v0 u1 v1 r g b a i0
else do
let !gy1 = gy + gh
!topDx = slant * (baselineY - gy)
!botDx = slant * (baselineY - gy1)
pokeQuadCornersSIMD vp vb ip ib (gx + topDx) gy (gx + gw + topDx) gy (gx + gw + botDx) gy1 (gx + botDx) gy1 u0 v0 u1 v1 r g b a i0
-- | Glyph quads for one line from pen @(px, py)@, used as given: synthetic bold
-- relies on its sub-pixel pass offsets. Every quad shares one arena
-- reservation. A non-zero @slant@ shears glyphs around the shared baseline for
-- synthetic oblique, so stems stay parallel and descenders lean left. A glyph
-- the font lacks, other than whitespace, draws an upright advance box on the
-- device grid. As in 'pushShapedQuads', glyphs wholly outside the clip emit
-- nothing, and the walk stops once the pen is well past it.
pushGlyphQuads :: DrawArena -> FontMetrics -> Float -> Float -> Float -> T.Text -> Color -> IO ()
pushGlyphQuads da fm slant px py txt col = do
let !byteLen = TF.lengthWord8 txt
when (byteLen > 0) $ do
let !cap = T.length txt
scale <- readIORef (daSnapScale da)
cx <- readPrimArray (daCurrentClip da) 0
cw <- readPrimArray (daCurrentClip da) 2
let !lh = fmLineHeight fm
!lean = abs slant * (lh + abs (fmAscent fm))
!left = cx - lean
!right = cx + cw + lean
!slack = glyphSlackLines * lh
setTexture da glyphAtlasTextureId
withVertsReserve da (cap * 4) (cap * 6) $ \vp ip base baseIdx commit -> do
let !(r, g, b, a) = unpackColorF col
!baselineY = py + fmAscent fm
!stop = right + slack
-- A pen this far left of the clip draws nothing, whatever its glyph.
!before = left - slack
-- Walk UTF-8 byte offsets with 'TU.iter' to avoid per-character
-- 'T.uncons' Text slicing and Maybe/tuple allocation.
walk !q !ox !first !prev !off
| off >= byteLen || ox > stop = pure q
| otherwise = do
let !(TU.Iter c step) = TU.iter txt off
!gx = if first then ox else ox + fmKerning fm prev c
!adv = fmAdvance fm c
!ox' = gx + adv
!off' = off + step
if gx < before
then walk q ox' False c off'
else case fmGlyph fm c of
Nothing
| adv > 0 && not (isSpace c) && not (gx + adv < left || gx > right) -> do
pokeGlyphQuad vp ip base baseIdx 0 baselineY r g b a q (onGrid scale gx) (onGrid scale py) adv lh whitePixel whitePixel whitePixel whitePixel
walk (q + 1) ox' False c off'
| otherwise -> walk q ox' False c off'
Just gq ->
let !qx = gx + gqX gq
!qw = gqW gq
in if qx + qw < left || qx > right
then walk q ox' False c off'
else do
pokeGlyphQuad vp ip base baseIdx slant baselineY r g b a q qx (py + gqY gq) qw (gqH gq) (gqU0 gq) (gqV0 gq) (gqU1 gq) (gqV1 gq)
walk (q + 1) ox' False c off'
!k <- walk 0 px True ' ' 0
commit (k * 4) (k * 6)
-- | Run @emitAt@ at each horizontal pen offset needed to synthesize @weight@
-- with step @bOff@. Inlined so GHC fuses @emitAt@ with zero list allocation.
{-# INLINE emitWeightPasses #-}
emitWeightPasses :: FontWeight -> Float -> (Float -> IO ()) -> IO ()
emitWeightPasses weight !bOff emitAt = case weight of
WeightNormal -> emitAt 0
WeightLight -> emitAt 0
WeightMedium -> emitAt 0 >> emitAt (0.5 * bOff)
WeightSemiBold -> emitAt 0 >> emitAt (0.75 * bOff)
WeightBold -> emitAt 0 >> emitAt bOff
WeightExtraBold -> emitAt 0 >> emitAt bOff >> emitAt (1.5 * bOff)
WeightBlack -> emitAt 0 >> emitAt bOff >> emitAt (1.5 * bOff) >> emitAt (2 * bOff)
-- | Emit pre-shaped glyph quads with synthetic weight and slant (no decoration).
{-# INLINE pushShapedStyledQuads #-}
pushShapedStyledQuads :: DrawArena -> FontMetrics -> FontWeight -> FontStyle -> Float -> Float -> ShapedGlyphs -> Color -> IO ()
pushShapedStyledQuads da fm weight fstyle x y glyphs col =
let !px = onGrid (fmSnapScale fm) x
!py = onGrid (fmSnapScale fm) y
!bOff = max 1.0 (0.05 * fmLineHeight fm)
!slant = if fstyle == FontStyleNormal then 0 else 0.18
in emitWeightPasses weight bOff (\dx -> pushShapedQuads da fm slant (px + dx) py glyphs col)
-- | Synthetic weight, slant and decoration over the plain text path. Upright
-- normal weight keeps the run path; every other pass walks glyphs.
pushPreparedTextStyledQuads :: DrawArena -> FontMetrics -> FontWeight -> FontStyle -> TextDecoration -> Float -> Float -> T.Text -> Color -> IO ()
pushPreparedTextStyledQuads da fm weight fstyle deco x y txt col
| weight == WeightNormal && fstyle == FontStyleNormal && deco == DecorationNone =
pushPreparedTextQuads da fm x y txt col
| otherwise = do
let !px = onGrid (fmSnapScale fm) x
!py = onGrid (fmSnapScale fm) y
!lh = fmLineHeight fm
!bOff = max 1.0 (0.05 * lh)
!slant = if fstyle == FontStyleNormal then 0 else 0.18
drawShaped fm txt >>= \case
Just glyphs -> emitWeightPasses weight bOff (\dx -> pushShapedQuads da fm slant (px + dx) py glyphs col)
Nothing -> emitWeightPasses weight bOff (\dx -> pushGlyphQuads da fm slant (px + dx) py txt col)
when (deco /= DecorationNone) $ do
let !textW = lineWidth fm txt
!(thick, underOff, strikeOff) = textDecorationMetrics lh (fmAscent fm)
underline = pushRect da (Rect px (py + underOff) textW thick) col
strike = pushRect da (Rect px (py + strikeOff) textW thick) col
case deco of
DecorationUnderline -> underline
DecorationStrikethrough -> strike
DecorationUnderlineStrike -> underline >> strike
DecorationNone -> pure ()
-- | Whether two 'TextFont' values resolve to the same font metrics (ignoring
-- 'textFontDecoration', which is painted as strokes rather than resolved).
{-# INLINE sameResolvedFont #-}
sameResolvedFont :: TextFont -> TextFont -> Bool
sameResolvedFont a b =
textFontSize a == textFontSize b
&& textFontVariant a == textFontVariant b
&& textFontWeight a == textFontWeight b
&& textFontStyle a == textFontStyle b
&& textFontTabular a == textFontTabular b
-- | Emit ops with @fm@ as the default font, at @size@. @resolve@ gives the
-- font for styled text and whether it renders weight and slant natively.
-- @imageUv@ maps an image op's id to an atlas texture and UV bounds, or
-- 'Nothing' when the id is itself a texture. A 'PushClip' intersects the
-- current clip until its 'PopClip'; an unclosed one ends with the ops.
-- Under external text, text ops push no quads: each line is recorded as a
-- span ('pushTextSpan') with the current clip, filed by layer.
emitDrawOps ::
DrawArena
-> FontMetrics
-> Float
-> (TextFont -> IO (FontMetrics, Bool))
-> (Int -> IO (Maybe (Int, (Float, Float, Float, Float))))
-> SmallArray DrawOp
-> IO ()
emitDrawOps da fm size resolve imageUv ops = do
!external <- readIORef (daExternalText da)
let !n = sizeofSmallArray ops
-- Vertical clip test reading clip Y/H directly without boxing a Rect or
-- allocating an action closure.
{-# INLINE inVerticalClip #-}
inVerticalClip lineFm !top = do
!cy <- readPrimArray (daCurrentClip da) 1
!ch <- readPrimArray (daCurrentClip da) 3
let !lh = fmLineHeight lineFm
!slack = glyphSlackLines * lh
pure $! ch > 0 && not (top + lh + slack < cy || top - slack > cy + ch)
{-# INLINE emitStyled #-}
emitStyled styledFm native font !x !y t c = do
let !deco = textFontDecoration font
!weight = if native then WeightNormal else textFontWeight font
!fstyle = if native then FontStyleNormal else textFontStyle font
if not external && deco == DecorationNone
then
drawShaped styledFm t >>= \case
Just glyphs -> pushShapedStyledQuads da styledFm weight fstyle x y glyphs c
Nothing -> do
prepared <- prepareFontMetrics styledFm t
pushPreparedTextStyledQuads da prepared weight fstyle DecorationNone x y t c
else do
prepared <- prepareFontMetrics styledFm t
if external
then pushTextSpan da (Rect x y (lineWidth prepared t) (fmLineHeight prepared)) t c
else pushPreparedTextStyledQuads da prepared weight fstyle deco x y t c
{-# INLINE emitAligned #-}
emitAligned styledFm native font !x !y !ax !ay t c = do
let !top = textBoxTop styledFm y ay
vis <- inVerticalClip styledFm top
when vis $
if not external && ax <= 0
then emitStyled styledFm native font x top t c
else do
prepared <- prepareFontMetrics styledFm t
let !box@(Rect px py _ _) = drawTextBox prepared x y ax ay t
if external
then pushTextSpan da box t c
else
let !weight = if native then WeightNormal else textFontWeight font
!fstyle = if native then FontStyleNormal else textFontStyle font
in pushPreparedTextStyledQuads da prepared weight fstyle (textFontDecoration font) px py t c
-- 2-slot MRU cache of resolved TextFonts (covers normal/bold alternation
-- in syntax-highlighted text with zero per-op resolver allocation).
-- Hitting slot 2 promotes it to slot 1 so a third font evicts the older
-- entry rather than the active one.
{-# INLINE withResolved #-}
withResolved font !has1 !f1 !fm1 !nat1 !has2 !f2 !fm2 !nat2 k
| has1 && sameResolvedFont font f1 =
k fm1 nat1 has1 f1 fm1 nat1 has2 f2 fm2 nat2
| has2 && sameResolvedFont font f2 =
k fm2 nat2 True f2 fm2 nat2 has1 f1 fm1 nat1
| otherwise = do
(!styledFm, !native) <- resolve font
k styledFm native True font styledFm native has1 f1 fm1 nat1
-- Clips saved by open 'PushClip's are kept innermost first.
go !i saved !has1 !f1 !fm1 !nat1 !has2 !f2 !fm2 !nat2
| i >= n = unless (null saved) (setClip da (last saved))
| otherwise = case indexSmallArray ops i of
DrawTextStyled x y font t c
| T.null t -> go (i + 1) saved has1 f1 fm1 nat1 has2 f2 fm2 nat2
| otherwise ->
withResolved font has1 f1 fm1 nat1 has2 f2 fm2 nat2 $
\styledFm native h1 k1 m1 n1 h2 k2 m2 n2 -> do
vis <- inVerticalClip styledFm y
when vis $ emitStyled styledFm native font x y t c
go (i + 1) saved h1 k1 m1 n1 h2 k2 m2 n2
PushClip r -> do
prev <- currentClip da
setClip da (fromMaybe (Rect 0 0 0 0) (rectIntersect prev r))
go (i + 1) (prev : saved) has1 f1 fm1 nat1 has2 f2 fm2 nat2
PopClip -> case saved of
prev : rest -> setClip da prev >> go (i + 1) rest has1 f1 fm1 nat1 has2 f2 fm2 nat2
[] -> go (i + 1) [] has1 f1 fm1 nat1 has2 f2 fm2 nat2
DrawText x y ax ay t c -> do
unless (T.null t) $
emitAligned fm False defaultTextFont x y ax ay t c
go (i + 1) saved has1 f1 fm1 nat1 has2 f2 fm2 nat2
DrawTextAligned x y ax ay k font t c
| T.null t -> go (i + 1) saved has1 f1 fm1 nat1 has2 f2 fm2 nat2
| otherwise ->
let font'
| k == 1 = font
| otherwise =
let resolved = resolveFontSize size (textFontSize font)
baseSz = if resolved > 0 then resolved else size
in font {textFontSize = k * baseSz}
in withResolved font' has1 f1 fm1 nat1 has2 f2 fm2 nat2 $
\styledFm native h1 k1 m1 n1 h2 k2 m2 n2 -> do
emitAligned styledFm native font' x y ax ay t c
go (i + 1) saved h1 k1 m1 n1 h2 k2 m2 n2
op@DrawImage {} -> do
pushImageOp da imageUv op
go (i + 1) saved has1 f1 fm1 nat1 has2 f2 fm2 nat2
op -> do
pushShapeOp da op
go (i + 1) saved has1 f1 fm1 nat1 has2 f2 fm2 nat2
go 0 [] False defaultTextFont fm False False defaultTextFont fm False
-- | Record drawing text as a host span with its line box: under external
-- text a drawing's text has no quads and no node span, so the host draws it
-- from this one ('dfeTextSpans'), in the current clip and filed by layer. An
-- empty clip records nothing, as it gets no command. Top level and out of
-- line so the local closures of 'emitDrawOps' capture nothing more for it.
{-# NOINLINE pushTextSpan #-}
pushTextSpan :: DrawArena -> Rect -> T.Text -> Color -> IO ()
pushTextSpan da !box t c = unless (T.null t) $ do
clip@(Rect _ _ cw ch) <- currentClip da
when (cw > 0 && ch > 0) $ do
layer <- currentLayer da
let entry = (box, t, c, Color 0, clip)
modifyIORef' (daExtra da) $ \ex ->
let (base, overlay) = dfeTextSpans ex
in ex {dfeTextSpans = if layer >= LayerOverlay then (base, entry : overlay) else (entry : base, overlay)}
-- | Paint a 'DrawImage' op; other ops paint nothing. @imageUv@ is as for
-- 'emitDrawOps', and the op's UVs span 0 to 1 over the image. Unrotated
-- images use the snapped quad; only rotated ones compute their corners.
-- An image is recorded as the op, under its own id; a solid one (no
-- texture) records the shape it paints.
{-# INLINE pushImageOp #-}
pushImageOp :: DrawArena -> (Int -> IO (Maybe (Int, (Float, Float, Float, Float)))) -> DrawOp -> IO ()
pushImageOp da imageUv = \case
op@(DrawImage r angle tex u0 v0 u1 v1 c) -> do
let draw !t !a0 !b0 !a1 !b1
| angle == 0 = pushImage da r t a0 b0 a1 b1 c
| otherwise = pushImageRotated da r angle t a0 b0 a1 b1 c
when (tex > 0) (recordOp da op)
imageUv tex >>= \case
Just (atlas, (a0, b0, a1, b1)) ->
draw atlas (a0 + u0 * (a1 - a0)) (b0 + v0 * (b1 - b0)) (a0 + u1 * (a1 - a0)) (b0 + v1 * (b1 - b0))
Nothing -> draw tex u0 v0 u1 v1
_ -> pure ()
-- | Paint a fill, stroke, line or gradient op. Text, image and clip ops
-- paint nothing here ('emitDrawOps' handles them). Inlined so that an op
-- built only to be painted is optimized away.
{-# INLINE pushShapeOp #-}
pushShapeOp :: DrawArena -> DrawOp -> IO ()
pushShapeOp da = \case
FillRect r c -> pushRect da r c
FillRoundedRect r radius c -> pushRoundedRect da r radius c
FillTriangle x0 y0 x1 y1 x2 y2 c -> pushFilledTriangle da x0 y0 x1 y1 x2 y2 c
FillCircle cx cy radius c -> pushCircle da cx cy radius c
Stroke x0 y0 x1 y1 t c -> pushStroke da x0 y0 x1 y1 t c
StrokeRoundedRect r radius bw c -> pushRoundedStroke da r radius bw c
StrokeCircle cx cy radius bw c -> pushCircleStroke da cx cy radius bw c
StrokeLineAA x0 y0 x1 y1 bw c -> pushStrokeAA da x0 y0 x1 y1 bw c
FillPolygon pts rings tris sh -> pushPolygonAA da pts rings tris sh
StrokePolyline pts w closed cap join limit sh -> pushPolylineAA da pts w closed cap join limit sh
FillQuadGradient r c0 c1 c2 c3 -> pushQuadGradient da r c0 c1 c2 c3
_ -> pure ()
-- | The checkbox widget's box, @box@ wide with its top-left corner at
-- @(x, y)@, passed op by op to @op@: the theme accent with a check mark when
-- @checked@, else an input well outlined in @border@. The widget paints the
-- ops directly ('pushShapeOp'); a canvas collects them
-- ('NanoUI.Widgets.Custom.drawCheckbox').
{-# INLINE checkboxOps #-}
checkboxOps :: Applicative f => (DrawOp -> f ()) -> Theme -> Color -> Float -> Float -> Float -> Bool -> f ()
checkboxOps op theme border x y box checked
| checked =
op (FillRoundedRect outer r accent)
*> op (StrokeRoundedRect outer r bw accent)
*> stroke x0 y0 x1 y1
*> stroke x1 y1 x2 y2
*> cap x0 y0
*> cap x1 y1
*> cap x2 y2
| otherwise =
op (FillRoundedRect (rectInflate (-bw) outer) (max 0 (r - bw)) (styleBg (themeInput theme)))
*> op (StrokeRoundedRect outer r bw border)
where
outer = Rect x y box box
r = min 6 (box / 3.5)
bw = 1.5
accent = themeAccent theme
mark = themeOnAccent theme
t = max 1.6 (box * 0.11)
x0 = x + box * 0.22
y0 = y + box * 0.52
x1 = x + box * 0.42
y1 = y + box * 0.72
x2 = x + box * 0.78
y2 = y + box * 0.28
stroke ax ay bx by = op (StrokeLineAA ax ay bx by t mark)
-- Caps snap their centres, like the stroke ends. Snapping a cap's corner
-- instead can land it a pixel off the stroke at fractional scales.
cap cx cy = op (FillCircle cx cy (t / 2) mark)