module Graphics.Rasterific
(
fill
, withTexture
, withClipping
, stroke
, dashedStroke
, printTextAt
, strokeDebug
, renderDrawing
, pathToPrimitives
, Texture
, Drawing
, Modulable
, V2( .. )
, Point
, Vector
, CubicBezier( .. )
, Line( .. )
, Bezier( .. )
, Primitive( .. )
, Path( .. )
, PathCommand( .. )
, Transformable( .. )
, line
, rectangle
, circle
, clip
, bezierFromPath
, lineFromPath
, cubicBezierFromPath
, Join( .. )
, Cap( .. )
, SamplerRepeat( .. )
, DashPattern
) where
import Control.Applicative( (<$>) )
import Control.Monad( forM_ )
import Control.Monad.Free( Free( .. ), liftF )
import Control.Monad.ST( ST, runST )
import Control.Monad.State( StateT, execStateT, get, lift )
import Codec.Picture.Types( Image( .. )
, Pixel( .. )
, MutableImage( .. )
, createMutableImage
, unsafeFreezeImage )
import qualified Data.Vector.Unboxed as VU
import Linear( V2( .. ), (^+^), (^*) )
import Graphics.Rasterific.Compositor
import Graphics.Rasterific.Rasterize
import Graphics.Rasterific.Texture
import Graphics.Rasterific.Types
import Graphics.Rasterific.Line
import Graphics.Rasterific.QuadraticBezier
import Graphics.Rasterific.CubicBezier
import Graphics.Rasterific.Stroke
import Graphics.Text.TrueType( Font, PointSize, getStringCurveAtPoint )
type DrawContext s px a =
StateT (MutableImage s px) (ST s) a
type Drawing px a = Free (DrawCommand px) a
data DrawCommand px next
= Fill [Primitive] next
| TextFill Font PointSize Point String next
| SetTexture (Texture px)
(Drawing px ()) next
| WithCliping (forall innerPixel. Drawing innerPixel ())
(Drawing px ()) next
instance Functor (DrawCommand px) where
fmap f (TextFill font size pos str next) =
TextFill font size pos str $ f next
fmap f (Fill prims next) = Fill prims $ f next
fmap f (SetTexture t sub next) = SetTexture t sub $ f next
fmap f (WithCliping sub com next) =
WithCliping sub com (f next)
withTexture :: Texture px -> Drawing px () -> Drawing px ()
withTexture texture subActions =
liftF $ SetTexture texture subActions ()
fill :: [Primitive] -> Drawing px ()
fill prims = liftF $ Fill prims ()
withClipping
:: (forall innerPixel. Drawing innerPixel ())
-> Drawing px ()
-> Drawing px ()
withClipping clipPath drawing =
liftF $ WithCliping clipPath drawing ()
stroke :: Float
-> Join
-> (Cap, Cap)
-> [Primitive]
-> Drawing px ()
stroke width join caping = fill . strokize width join caping
printTextAt :: Font
-> Int
-> Point
-> String
-> Drawing px ()
printTextAt font pointSize point string =
liftF $ TextFill font pointSize point string ()
renderDrawing
:: forall px
. ( Pixel px
, Pixel (PixelBaseComponent px)
, Modulable (PixelBaseComponent px)
, PixelBaseComponent (PixelBaseComponent px) ~ (PixelBaseComponent px)
)
=> Int
-> Int
-> px
-> Drawing px ()
-> Image px
renderDrawing width height background drawing = runST $
createMutableImage width height background
>>= execStateT (go Nothing stupidDefaultTexture drawing)
>>= unsafeFreezeImage
where
clipBackground = emptyValue :: PixelBaseComponent px
clipForeground = fullValue :: PixelBaseComponent px
stupidDefaultTexture =
uniformTexture $ colorMap (const clipBackground) background
clipRender =
renderDrawing width height clipBackground
. withTexture (uniformTexture clipForeground)
go :: Maybe (Texture (PixelBaseComponent px))
-> Texture px
-> Drawing px ()
-> DrawContext s px ()
go _ _ (Pure ()) = return ()
go Nothing texture (Free (Fill prims next)) =
fillWithTexture texture prims >> go Nothing texture next
go mo@(Just moduler) texture (Free (Fill prims next)) =
fillWithTextureAndMask texture moduler prims >> go mo texture next
go moduler texture (Free (SetTexture tx sub next)) =
go moduler tx sub >> go moduler texture next
go moduler texture (Free (TextFill font size (V2 x y) str next)) =
forM_ drawCalls (go moduler texture) >> go moduler texture next
where
drawCalls = beziersOfChar <$> getStringCurveAtPoint 90 (x, y) [(font, size, str)]
beziersOfChar curves = liftF $ Fill bezierCurves ()
where
bezierCurves = concat
[map BezierPrim . bezierFromPath . map (uncurry V2)
$ VU.toList c | c <- curves]
go moduler texture (Free (WithCliping clipPath path next)) =
go newModuler texture path >> go moduler texture next
where
modulationTexture :: Texture (PixelBaseComponent px)
modulationTexture = imageTexture $ clipRender clipPath
newModuler = Just $ subModuler moduler
subModuler Nothing = modulationTexture
subModuler (Just v) =
modulateTexture v modulationTexture
dashedStroke
:: DashPattern
-> Float
-> Join
-> (Cap, Cap)
-> [Primitive]
-> Drawing px ()
dashedStroke dashing width join caping =
mapM_ fill . dashedStrokize dashing width join caping
strokeDebug :: ( Pixel px, Modulable (PixelBaseComponent px))
=> Texture px -> Texture px
-> Float -> Join -> (Cap, Cap)
-> [Primitive] -> Drawing px ()
strokeDebug debugPair debugImpair width join caping elems = do
fill stroked
forM_ (zip debugColor stroked) subStroke
where stroked = strokize width join caping elems
debugColor = debugPair : debugImpair : debugColor
subStroke (color, el) =
withTexture color $ stroke 2 (JoinMiter 0)
(CapStraight 0, CapStraight 0) [el]
clip :: Point
-> Point
-> Primitive
-> [Primitive]
clip mini maxi (LinePrim l) = clipLine mini maxi l
clip mini maxi (BezierPrim b) = clipBezier mini maxi b
clip mini maxi (CubicBezierPrim c) = clipCubicBezier mini maxi c
fillWithTexture :: (Pixel px, Modulable (PixelBaseComponent px))
=> Texture px
-> [Primitive]
-> DrawContext s px ()
fillWithTexture texture els = do
img@(MutableImage width height _) <- get
let mini = V2 0 0
maxi = V2 (fromIntegral width) (fromIntegral height)
spans = rasterize $ els >>= clip mini maxi
lift $ mapM_ (composeCoverageSpan texture img) spans
fillWithTextureAndMask
:: ( Pixel px
, Pixel (PixelBaseComponent px)
, Modulable (PixelBaseComponent px))
=> Texture px
-> Texture (PixelBaseComponent px)
-> [Primitive]
-> DrawContext s px ()
fillWithTextureAndMask texture mask els = do
img@(MutableImage width height _) <- get
let mini = V2 0 0
maxi = V2 (fromIntegral width) (fromIntegral height)
spans = rasterize $ els >>= clip mini maxi
lift $ mapM_ (composeCoverageSpanWithMask texture mask img) spans
composeCoverageSpan :: forall s px .
( Pixel px, Modulable (PixelBaseComponent px) )
=> Texture px
-> MutableImage s px
-> CoverageSpan
-> ST s ()
composeCoverageSpan texture img coverage
| initialCov == 0 || initialX < 0 || y < 0 || imgWidth < initialX || imgHeight < y = return ()
| otherwise = go 0 initialX initIndex
where compCount = componentCount (undefined :: px)
maxi = _coverageLength coverage
imgData = mutableImageData img
y = floor $ _coverageY coverage
initialX = floor $ _coverageX coverage
imgWidth = mutableImageWidth img
imgHeight = mutableImageHeight img
initIndex = (initialX + y * imgWidth) * compCount
(initialCov, _) =
clampCoverage $ _coverageVal coverage
shader = texture SamplerPad
go count _ _ | count >= maxi = return ()
go count x idx = do
oldPixel <- unsafeReadPixel imgData idx
let px = shader (fromIntegral x) (fromIntegral y)
opacity = pixelOpacity px
(cov, icov) = coverageModulate initialCov opacity
unsafeWritePixel imgData idx
$ compositionAlpha cov icov oldPixel px
go (count + 1) (x + 1) $ idx + compCount
composeCoverageSpanWithMask
:: forall s px
. ( Pixel px
, Pixel (PixelBaseComponent px)
, Modulable (PixelBaseComponent px) )
=> Texture px
-> Texture (PixelBaseComponent px)
-> MutableImage s px
-> CoverageSpan
-> ST s ()
composeCoverageSpanWithMask texture mask img coverage
| initialCov == 0 || initialX < 0 || y < 0 || imgWidth < initialX || imgHeight < y = return ()
| otherwise = go 0 initialX initIndex
where compCount = componentCount (undefined :: px)
maxi = _coverageLength coverage
imgData = mutableImageData img
y = floor $ _coverageY coverage
initialX = floor $ _coverageX coverage
imgWidth = mutableImageWidth img
imgHeight = mutableImageHeight img
initIndex = (initialX + y * imgWidth) * compCount
(initialCov, _) =
clampCoverage $ _coverageVal coverage
maskShader = mask SamplerPad
shader = texture SamplerPad
go count _ _ | count >= maxi = return ()
go count x idx = do
oldPixel <- unsafeReadPixel imgData idx
let fx = fromIntegral x
fy = fromIntegral y
maskValue = maskShader fx fy
px = shader fx fy
(coeffMasked, _) = coverageModulate initialCov maskValue
(cov, icov) = coverageModulate coeffMasked $ pixelOpacity px
unsafeWritePixel imgData idx
$ compositionAlpha cov icov oldPixel px
go (count + 1) (x + 1) $ idx + compCount
circle :: Point
-> Float
-> [Primitive]
circle center radius = CubicBezierPrim . scaleMove <$> cubicBezierCircle
where
mv p = (p ^* radius) ^+^ center
scaleMove (CubicBezier p1 p2 p3 p4) =
CubicBezier (mv p1) (mv p2) (mv p3) (mv p4)
rectangle :: Point
-> Float
-> Float
-> [Primitive]
rectangle p@(V2 px py) w h =
LinePrim <$> lineFromPath
[ p, V2 (px + w) py, V2 (px + w) (py + h), V2 px (py + h), p ]
line :: Point -> Point -> [Primitive]
line p1 p2 = [LinePrim $ Line p1 p2]