Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Cairo drawing backend.
Synopsis
- render :: FilePath -> Int -> Int -> Render () -> IO ()
- data CoordinateSystem
- coordinateSystem :: CoordinateSystem -> Render ()
- haddockRender :: FilePath -> Int -> Int -> (Vec2 -> Render ()) -> IO ()
- moveToVec :: Vec2 -> Render ()
- lineToVec :: Vec2 -> Render ()
- class Sketch a where
- sketch :: a -> Render ()
- data Arrow = Arrow !Line !ArrowSpec
- data ArrowSpec = ArrowSpec {}
- data Circle = Circle {
- _circleCenter :: !Vec2
- _circleRadius :: !Double
- data Cross = Cross {
- _crossCenter :: !Vec2
- _crossRadius :: !Double
- newtype PolyBezier f = PolyBezier (f Bezier)
- arcSketch :: Vec2 -> Double -> Angle -> Angle -> Render ()
- arcSketchNegative :: Vec2 -> Double -> Angle -> Angle -> Render ()
- data Colour a
- type Color a = Colour a
- data AlphaColour a
- type AlphaColor a = AlphaColour a
- class CairoColor color where
- module Draw.Color
- module Draw.Color.Schemes.Discrete
- module Draw.Color.Schemes.Continuous
- withOperator :: Operator -> Render a -> Render a
- cairoScope :: Render a -> Render a
- grouped :: Render after -> Render a -> Render a
- cartesianCoordinateSystem :: CartesianParams -> Render ()
- data CartesianParams = CartesianParams {
- _cartesianMinX :: !Int
- _cartesianMaxX :: !Int
- _cartesianMinY :: !Int
- _cartesianMaxY :: !Int
- _cartesianAlpha :: !Double
- _renderAxisLabels :: !Bool
- _renderTens :: !Bool
- _renderHundreds :: !Bool
- radialCoordinateSystem :: PolarParams -> Render ()
- data PolarParams = PolarParams {
- _polarCenter :: !Vec2
- _polarMaxRadius :: !Double
- _polarAlpha :: !Double
- fromCairoMatrix :: Matrix -> Transformation
- toCairoMatrix :: Transformation -> Matrix
- module Draw.Text
- for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f ()
- module Data.Foldable
SVG and PNG file handling
Renders the drawing as PNG or SVG, depending on the file extension.
data CoordinateSystem Source #
Argument to coordinateSystem
to pick zero location and axis direction.
CairoStandard_ZeroTopLeft_XRight_YDown | Left-handed coordinate system. Standard Cairo/computer graphics coordinates. Zero is on the top left, and the Y axis points downwards. Even though this is common in computer graphics, working geometrically with a left-handed coordinate system can be a bit awkward and surprising. (image code)
|
MathStandard_ZeroBottomLeft_XRight_YUp Double | Right-handed coordinate system. Standard math coordinates, with zero on the bottom left. Needs the image’s height as arguments for technical reasons. (image code)
|
MathStandard_ZeroCenter_XRight_YUp Double Double | Right-handed coordinate system. Standard math coordinates, with zero in the center. Needs the image’s width and height as arguments for technical reasons. (image code)
|
Instances
Show CoordinateSystem Source # | |
Defined in Draw showsPrec :: Int -> CoordinateSystem -> ShowS # show :: CoordinateSystem -> String # showList :: [CoordinateSystem] -> ShowS # | |
Default CoordinateSystem Source # | |
Defined in Draw | |
Eq CoordinateSystem Source # | |
Defined in Draw (==) :: CoordinateSystem -> CoordinateSystem -> Bool # (/=) :: CoordinateSystem -> CoordinateSystem -> Bool # | |
Ord CoordinateSystem Source # | |
Defined in Draw compare :: CoordinateSystem -> CoordinateSystem -> Ordering # (<) :: CoordinateSystem -> CoordinateSystem -> Bool # (<=) :: CoordinateSystem -> CoordinateSystem -> Bool # (>) :: CoordinateSystem -> CoordinateSystem -> Bool # (>=) :: CoordinateSystem -> CoordinateSystem -> Bool # max :: CoordinateSystem -> CoordinateSystem -> CoordinateSystem # min :: CoordinateSystem -> CoordinateSystem -> CoordinateSystem # |
coordinateSystem :: CoordinateSystem -> Render () Source #
Choose a coordinate system.
:: FilePath | |
-> Int | Image width (px) |
-> Int | Image height (px) |
-> (Vec2 -> Render ()) | The width/height of the image is passed as |
-> IO () |
Render pictures for Haddock with doctests. Nomenclature: the FilePath
for
Foo.Bar.Baz is Foo/Bar/Baz/pic_name.svg.
Prints status information about the generated file so that doctests fail when the file contents change. Inspect the new output and update the output if the result is OK.
Drawing presets
Sketch a shape that can then be made visible by drawing functions such as stroke
or fill
.
Instances
Sketch Arrow Source # | (image code)
|
Sketch Cross Source # | (image code)
|
Sketch Ray Source # | |
Sketch VoronoiPolygon Source # | |
Defined in Geometry.Algorithms.Delaunay.Internal.Delaunator.Api sketch :: VoronoiPolygon -> Render () Source # | |
Sketch Bezier Source # | (image code)
|
Sketch BoundingBox Source # | Sketches a rectangle with a diagonal cross through it. Useful for debugging. (image code)
|
Defined in Draw sketch :: BoundingBox -> Render () Source # | |
Sketch Circle Source # | (image code)
|
Sketch Ellipse Source # | (image code)
|
Sketch Line Source # | (image code)
|
Sketch Polygon Source # | (image code)
|
Sketch Polyline Source # | Polyline, i.e. a sequence of lines given by their joints. (image code)
|
Sketch Transformation Source # | Draw a \(100\times 100\) square with its corner at (image code)
|
Defined in Draw sketch :: Transformation -> Render () Source # | |
Sequential f => Sketch (PolyBezier f) Source # | Sketch a continuous curve consisting of multiple Bezier segments. The end of each segment is assumed to be the start of the next one. |
Defined in Draw sketch :: PolyBezier f -> Render () Source # | |
Sketch a => Sketch (Maybe a) Source # | |
Sketch a => Sketch [a] Source # | |
(Sketch a, Sketch b) => Sketch (Either a b) Source # | |
(Sketch a, Sketch b) => Sketch (a, b) Source # | |
(Sketch a, Sketch b, Sketch c) => Sketch (a, b, c) Source # | |
(Sketch a, Sketch b, Sketch c, Sketch d) => Sketch (a, b, c, d) Source # | |
(Sketch a, Sketch b, Sketch c, Sketch d, Sketch e) => Sketch (a, b, c, d, e) Source # | |
For sketch
ing arrows.
ArrowSpec | |
|
Circles are not an instance of Transform
, because e.g. shear
ing a circle
yields an Ellipse
. To transform circles, convert them to an ellipse first with
toEllipse
.
Circle | |
|
Instances
Show Circle Source # | |
Default Circle Source # | Unit circle |
Defined in Geometry.Core | |
NFData Circle Source # | |
Defined in Geometry.Core | |
Sketch Circle Source # | (image code)
|
Plotting Circle Source # | |
HasBoundingBox Circle Source # | (image code)
|
Defined in Geometry.Core boundingBox :: Circle -> BoundingBox Source # | |
Eq Circle Source # | |
Ord Circle Source # | |
sketch
a cross like ×. Sometimes useful to decorate a line with for e.g.
strikethrough effects, or to contrast the o in tic tac toe.
When drawn with the same radius, it combines to ⨂ with a Circle
.
Cross | |
|
Instances
Show Cross Source # | |
Sketch Cross Source # | (image code)
|
Eq Cross Source # | |
Ord Cross Source # | |
newtype PolyBezier f Source #
PolyBezier (f Bezier) |
Instances
Sequential f => Sketch (PolyBezier f) Source # | Sketch a continuous curve consisting of multiple Bezier segments. The end of each segment is assumed to be the start of the next one. |
Defined in Draw sketch :: PolyBezier f -> Render () Source # |
:: Vec2 | Center |
-> Double | Radius |
-> Angle | Starting angle (absolute) |
-> Angle | Ending angle (absolute) |
-> Render () |
Sketch part of a circle.
:: Vec2 | Center |
-> Double | Radius |
-> Angle | Starting angle (absolute) |
-> Angle | Ending angle (absolute) |
-> Render () |
Sketch part of a circle.
Colors
data AlphaColour a #
Instances
type AlphaColor a = AlphaColour a Source #
American English type synonym
class CairoColor color where Source #
Anything we can instruct Cairo to set its color to.
Nothing
setColor :: color -> Render () Source #
>>>
:{
haddockRender "Draw/Color/set_color.svg" 140 40 $ \_ -> do for_ (zip [0..] [30, 40 .. 150-30]) $ \(i, x) -> do setColor (mma i) sketch (Circle (Vec2 x 20) 10) C.fill :} Generated file: size 4KB, crc32: 0xe0e16234
Instances
(Real a, Floating a) => CairoColor (AlphaColour a) Source # | |
Defined in Draw.Color setColor :: AlphaColour a -> Render () Source # setColour :: AlphaColour a -> Render () Source # | |
Real a => CairoColor (Colour a) Source # | |
module Draw.Color
Discrete color schemes
Discrete color schemes, taken from:
- Mathematica: https://www.wolfram.com/mathematica/
- Color Brewer 2: https://colorbrewer2.org/
Name | Domain | |
---|---|---|
mma | [0..∞) | |
accent | [0..7] | |
dark2 | [0..7] | |
paired | [0..11] | |
pastel1 | [0..8] | |
pastel2 | [0..7] | |
set1 | [0..8] | |
set2 | [0..7] | |
set3 | [0..11] |
module Draw.Color.Schemes.Discrete
Continuous color schemes
Continuous color schemes, taken from:
- Color Brewer 2: https://colorbrewer2.org/
- Matplotlib: https://matplotlib.org/
- Seaborn: https://seaborn.pydata.org/
Name | Domain | Type | |
---|---|---|---|
haskell | [0..1] | Monotone | |
magma | [0..1] | Monotone | |
inferno | [0..1] | Monotone | |
plasma | [0..1] | Monotone | |
viridis | [0..1] | Monotone | |
cividis | [0..1] | Monotone | |
turbo | [0..1] | Monotone | |
twilight | [0..1] | Cyclic | |
rocket | [0..1] | Monotone | |
mako | [0..1] | Monotone | |
flare | [0..1] | Monotone | |
crest | [0..1] | Monotone | |
vlag | [0..1] | Divisive | |
icefire | [0..1] | Divisive | |
orRd | [0..1] | Monotone | |
puBu | [0..1] | Monotone | |
buPu | [0..1] | Monotone | |
oranges | [0..1] | Monotone | |
buGn | [0..1] | Monotone | |
ylOrBr | [0..1] | Monotone | |
ylGn | [0..1] | Monotone | |
reds | [0..1] | Monotone | |
rdPu | [0..1] | Monotone | |
greens | [0..1] | Monotone | |
ylGnBu | [0..1] | Monotone | |
purples | [0..1] | Monotone | |
gnBu | [0..1] | Monotone | |
greys | [0..1] | Monotone | |
ylOrRd | [0..1] | Monotone | |
puRd | [0..1] | Monotone | |
blues | [0..1] | Monotone | |
puBuGn | [0..1] | Monotone | |
spectral | [0..1] | Divisive | |
rdYlGn | [0..1] | Divisive | |
rdBu | [0..1] | Divisive | |
piYG | [0..1] | Divisive | |
pRGn | [0..1] | Divisive | |
rdYlBu | [0..1] | Divisive | |
brBG | [0..1] | Divisive | |
rdGy | [0..1] | Divisive | |
puOr | [0..1] | Divisive |
Temporary Cairo modifications
withOperator :: Operator -> Render a -> Render a Source #
Temporarily draw using a different composition operator, such as
OperatorClear
to delete part of an image.
cairoScope :: Render a -> Render a Source #
Open a new Cairo scope to allow local parameter changes. When the scope closes, the parameters are reset. Cairo documentation hides what actually is in the parameter state remarkably well; the state thus includes (source):
- Drawing operator (
withOperator
) - Tolerance (
setTolerance
) - Antialiasing (
setAntialias
) - Line style (
setLineWidth
,setLineCap
,setLineJoin
,setMiterLimit
,setDash
) - Fill rule (
setFillRule
) - Font face, scaling, options
- Clipping (
clip
) - Pattern (includes colors/
setColor
, gradients/withLinearPattern
etc.) - Tranformation matrix (
translate
etc.)
For example, we can paint the first block with a wide style, the second one dashed, and afterwards fall back to the implicit defaults:
>>>
:{
haddockRender "Draw/cairoScope.svg" 200 40 $ \_ -> do let line = Line (Vec2 10 0) (Vec2 190 0) cairoScope $ do C.translate 0 30 setLineWidth 3 setColor (mma 1) sketch line stroke cairoScope $ do C.translate 0 20 setDash [5,3] 0 setColor (mma 2) sketch line stroke C.translate 0 10 sketch line stroke :} Generated file: size 2KB, crc32: 0x2d7bee90
grouped :: Render after -> Render a -> Render a Source #
Render something as a group, as in encapsulate it in pushGroup
and
popGroupToSource
. This function semantically includes a call cairoScope
.
grouped
is commonly used to avoid a less transparent area when overlapping two
transparent areas.
The naive way has the intersection of the two circles darker,
dosetSourceRGBA
0 0 0 0.5sketch
(Circle
(Vec2
0 0) 10)fill
sketch
(Circle
(Vec2
7 0) 10)fill
On the other hand this will have the combination of the entire combined shape drawn with 0.5 alpha:
grouped
(paintWithAlpha
0.5) $ dosetSourceRGBA
0 0 0 1sketch
(Circle
(Vec2
0 0) 10)fill
sketch
(Circle
(Vec2
7 0) 10)fill
Orientation helpers
cartesianCoordinateSystem :: CartesianParams -> Render () Source #
Draw a caresian coordinate system in range (x,x') (y,y'). Very useful for prototyping.
(image code)
>>> :{ haddockRender "Draw/cartesianCoordinateSystem.svg" 320 220 $ _ -> cartesianCoordinateSystem def :} Generated file: size 21KB, crc32: 0xf43aac0c
data CartesianParams Source #
CartesianParams | |
|
Instances
Show CartesianParams Source # | |
Defined in Draw showsPrec :: Int -> CartesianParams -> ShowS # show :: CartesianParams -> String # showList :: [CartesianParams] -> ShowS # | |
Default CartesianParams Source # | |
Defined in Draw | |
Eq CartesianParams Source # | |
Defined in Draw (==) :: CartesianParams -> CartesianParams -> Bool # (/=) :: CartesianParams -> CartesianParams -> Bool # | |
Ord CartesianParams Source # | |
Defined in Draw compare :: CartesianParams -> CartesianParams -> Ordering # (<) :: CartesianParams -> CartesianParams -> Bool # (<=) :: CartesianParams -> CartesianParams -> Bool # (>) :: CartesianParams -> CartesianParams -> Bool # (>=) :: CartesianParams -> CartesianParams -> Bool # max :: CartesianParams -> CartesianParams -> CartesianParams # min :: CartesianParams -> CartesianParams -> CartesianParams # |
radialCoordinateSystem :: PolarParams -> Render () Source #
Like cartesianCoordinateSystem
, but with polar coordinates.
(image code)
>>>
:{
haddockRender "Draw/radialCoordinateSystem.svg" 250 250 $ \_ -> do C.translate 50 50 radialCoordinateSystem def :} Generated file: size 26KB, crc32: 0x9b68b36
data PolarParams Source #
PolarParams | |
|
Instances
Show PolarParams Source # | |
Defined in Draw showsPrec :: Int -> PolarParams -> ShowS # show :: PolarParams -> String # showList :: [PolarParams] -> ShowS # | |
Default PolarParams Source # | |
Defined in Draw def :: PolarParams | |
Eq PolarParams Source # | |
Defined in Draw (==) :: PolarParams -> PolarParams -> Bool # (/=) :: PolarParams -> PolarParams -> Bool # | |
Ord PolarParams Source # | |
Defined in Draw compare :: PolarParams -> PolarParams -> Ordering # (<) :: PolarParams -> PolarParams -> Bool # (<=) :: PolarParams -> PolarParams -> Bool # (>) :: PolarParams -> PolarParams -> Bool # (>=) :: PolarParams -> PolarParams -> Bool # max :: PolarParams -> PolarParams -> PolarParams # min :: PolarParams -> PolarParams -> PolarParams # |
Transformations
fromCairoMatrix :: Matrix -> Transformation Source #
Translate between Cairo and our matrix representation for transformations.
Cairo does its transformation in inverse: we transform the geometry, Cairo
transforms the canvas. fromCairoMatrix
and toCairoMatrix
translate between
the worlds, so that conceptually both of these yield the same output:
trafo ::Transformation
sketch
(Transform
trafo geometry) --transform
(fromCairoMatrix
trafo)>>
sketch
geometry
Note that Cairo’s transform
does more than just moving around lines: it
also scales other properties such as line width, so the pictures described above
might have some differences.
Useful Cairo functions for working with this are
transform
::Matrix
->Render
()setMatrix
::Matrix
->Render
()getMatrix
::Render
Matrix
toCairoMatrix :: Transformation -> Matrix Source #
See fromCairoMatrix
’ documentation, of which toCairoMatrix
is the inverse.
Text
module Draw.Text
Convenience
for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f () #
for_
is traverse_
with its arguments flipped. For a version
that doesn't ignore the results see for
. This
is forM_
generalised to Applicative
actions.
for_
is just like forM_
, but generalised to Applicative
actions.
Examples
Basic usage:
>>>
for_ [1..4] print
1 2 3 4
module Data.Foldable