Portability | portable |
---|---|
Stability | experimental |
Maintainer | byorgey@gmail.com |
Safe Haskell | None |
An embedded domain-specific language (EDSL) for creating simple diagrams, built on top of the Cairo rendering engine.
- data Diagram
- nil :: Diagram
- circle :: Double -> Diagram
- poly :: Int -> Double -> Diagram
- rect :: Double -> Double -> Diagram
- shape :: ShapeClass s => s -> Diagram
- hspace :: Double -> Diagram
- vspace :: Double -> Diagram
- empty :: Double -> Double -> Diagram
- (##) :: Diagram -> Diagram -> Diagram
- union :: [Diagram] -> Diagram
- unionA :: HAlignment -> VAlignment -> [Diagram] -> Diagram
- (<>) :: Diagram -> Diagram -> Diagram
- (//) :: Diagram -> Diagram -> Diagram
- hcat :: [Diagram] -> Diagram
- vcat :: [Diagram] -> Diagram
- hcatA :: VAlignment -> [Diagram] -> Diagram
- vcatA :: HAlignment -> [Diagram] -> Diagram
- hsep :: Double -> [Diagram] -> Diagram
- vsep :: Double -> [Diagram] -> Diagram
- hsepA :: Double -> VAlignment -> [Diagram] -> Diagram
- vsepA :: Double -> HAlignment -> [Diagram] -> Diagram
- hdistrib :: Double -> HAlignment -> [Diagram] -> Diagram
- vdistrib :: Double -> VAlignment -> [Diagram] -> Diagram
- hdistribA :: Double -> HAlignment -> VAlignment -> [Diagram] -> Diagram
- vdistribA :: Double -> VAlignment -> HAlignment -> [Diagram] -> Diagram
- type VAlignment = Alignment
- top :: VAlignment
- vcenter :: VAlignment
- bottom :: VAlignment
- type HAlignment = Alignment
- left :: HAlignment
- hcenter :: HAlignment
- right :: HAlignment
- stretch :: Double -> Double -> Diagram -> Diagram
- scale :: Double -> Diagram -> Diagram
- scaleX :: Double -> Diagram -> Diagram
- scaleY :: Double -> Diagram -> Diagram
- translate :: Double -> Double -> Diagram -> Diagram
- translateX :: Double -> Diagram -> Diagram
- translateY :: Double -> Diagram -> Diagram
- rotate :: Double -> Diagram -> Diagram
- rotateR :: Double -> Diagram -> Diagram
- view :: Point -> Point -> Diagram -> Diagram
- fillColor :: Color -> Diagram -> Diagram
- fc :: Color -> Diagram -> Diagram
- fillTransparency :: Int -> Diagram -> Diagram
- ft :: Int -> Diagram -> Diagram
- lineColor :: Color -> Diagram -> Diagram
- lc :: Color -> Diagram -> Diagram
- lineWidth :: Double -> Diagram -> Diagram
- lw :: Double -> Diagram -> Diagram
- data Color
- rgb :: Double -> Double -> Double -> Color
- rgba :: Double -> Double -> Double -> Double -> Color
- module Graphics.Rendering.Diagrams.Colors
- renderToPng :: String -> Int -> Int -> Diagram -> IO ()
- compose :: Double -> Double -> Diagram -> Render ()
Introduction
Graphics.Rendering.Diagrams is an embedded domain-specific language (EDSL) for creating simple diagrams. It is compositional; starting with some basic shapes, you can build up complex diagrams by combining simpler diagrams in various ways.
A few fundamental concepts to keep in mind:
- When constructing diagrams, there is no concept of an absolute coordinate system; everything is relative. You cannot say "this diagram should go at (2,1)"; you can only say "this diagram should go to the right of this other one", or "this diagram should go two units to the right and one unit above where it would otherwise be".
- Every diagram has an associated rectangular bounding box, which determines its positioning and alignment relative to other diagrams. Usually this makes no difference but there are times when it's nice to be aware of it. For example, translating a diagram works by moving the diagram relative to its bounding box; positioning the bounding box where it would have gone means the diagram itself ends up elsewhere. Also, some shapes have non-obvious bounding boxes; for example, regular polygons have a bounding box that fits their circumcircle.
- The positive y-axis points downwards.
For some simple examples, see http://byorgey.wordpress.com/2008/04/30/new-haskell-diagrams-library/.
Enjoy! Please send comments, suggestions, bug reports, or patches to byorgey at gmail dot com.
Primitives
Shapes
poly :: Int -> Double -> DiagramSource
poly n r
is a regular n-gon, with a circumcircle of radius r
.
One vertex is oriented along the positive x-axis. Note that the
bounding box is the square circumscribed around the circumcircle.
shape :: ShapeClass s => s -> DiagramSource
Create a Diagram
out of any instance of ShapeClass
.
Spacers
hspace :: Double -> DiagramSource
hspace w
is a Diagram
which produces no output but takes up
w
amount of space horizontally. Useful for manually creating
horizontal separation between two diagrams. A negative value
of w
can also be used to move two diagrams closer to one
another. hspace w
is equivalent to empty w 0
.
vspace :: Double -> DiagramSource
vspace h
is a Diagram
which produces no output but takes up
h
amount of space vertically. Useful for manually creating
vertical separation between two diagrams. A negative value of
h
can also be used to move two diagrams closer to one
another. vspace h
is equivalent to empty 0 h
.
empty :: Double -> Double -> DiagramSource
empty w h
is an empty diagram which produces no output, but
takes up an amount of space equal to a w
by h
rectangle.
Combinators
Union
unionA :: HAlignment -> VAlignment -> [Diagram] -> DiagramSource
Create a Diagram
as a union of subdiagrams superimposed on one
another, aligned vertically and/or horizontally.
Lists
(<>) :: Diagram -> Diagram -> DiagramSource
d1 d2
is a Diagram
with d1
to the left of d2
, aligned
along their top edges.
(//) :: Diagram -> Diagram -> DiagramSource
d1 d2
is a Diagram
with d1
above d2
, aligned
along their left edges.
hcat :: [Diagram] -> DiagramSource
Lay out a list of Diagram
s horizontally from left to right,
aligned along their top edges.
vcat :: [Diagram] -> DiagramSource
Lay out a list of Diagram
s vertically from top to bottom,
aligned along their left edges.
hcatA :: VAlignment -> [Diagram] -> DiagramSource
vcatA :: HAlignment -> [Diagram] -> DiagramSource
Lay out a list of Diagram
s horizontally, aligned along their
top edges, with a given amount of separation in between each pair.
Lay out a list of Diagram
s vertically, aligned along their
left edges, with a given amount of separation in between each pair.
:: Double | amount of separation between each pair of diagrams |
-> VAlignment | |
-> [Diagram] | |
-> Diagram |
:: Double | amount of separation between each pair of diagrams |
-> HAlignment | |
-> [Diagram] | |
-> Diagram |
:: Double | How far from one diagram to the next? |
-> HAlignment | Distribute according to which parts of
the diagrams ( |
-> [Diagram] | |
-> Diagram |
Distribute a list of Diagram
s horizontally according to a
regular spacing, aligned along their top edges.
:: Double | How far from one diagram to the next? |
-> VAlignment | Distribute according to which parts of
the diagrams ( |
-> [Diagram] | |
-> Diagram |
Distribute a list of Diagram
s vertically according to a regular
spacing, aligned along their left edges.
:: Double | How far from one diagram to the next? |
-> HAlignment | Distribute according to which parts of
the diagrams ( |
-> VAlignment | |
-> [Diagram] | |
-> Diagram |
Distribute a list of Diagram
s horizontally according to a
regular spacing, with the given alignment.
:: Double | How far from one diagram to the next? |
-> VAlignment | Distribute according to which parts of
the diagrams ( |
-> HAlignment | |
-> [Diagram] | |
-> Diagram |
Distribute a list of Diagram
s vertically according to a
regular spacing, with the given alignment.
type VAlignment = AlignmentSource
Vertical alignment.
type HAlignment = AlignmentSource
Horizontal alignment.
Transformations
Various ways to modify and transform Diagram
s.
stretch :: Double -> Double -> Diagram -> DiagramSource
Stretch a diagram by a separate scaling factor for each axis.
stretch w h
scales by a factor of w
in the x direction and
a factor of h
in the y direction.
scale :: Double -> Diagram -> DiagramSource
Scale by the same scaling factor in both dimensions, so the diagram retains its aspect ratio.
scaleX :: Double -> Diagram -> DiagramSource
Scale a diagram along the x-axis only. scaleX s
is equivalent
to stretch s 1
.
scaleY :: Double -> Diagram -> DiagramSource
Scale a diagram along the y-axis only. scaleY s
is equivalent
to stretch 1 s
.
translate :: Double -> Double -> Diagram -> DiagramSource
Translate a diagram by the given relative offsets in the x and y directions. Note that the positive x-axis is to the right, while the positive y-axis points downwards.
translateX :: Double -> Diagram -> DiagramSource
Translate a diagram along the x-axis only. translateX x
is
equivalent to translate x 0
.
translateY :: Double -> Diagram -> DiagramSource
Translate a diagram along the y-axis only. translateY y
is
equivalent to translate 0 y
.
rotate :: Double -> Diagram -> DiagramSource
rotate f
rotates a diagram clockwise by fraction f
of a
complete revolution. rotate f
is equivalent to rotateR
(2*pi*f)
.
Attributes
Attributes which affect the way in which a Diagram
is rendered.
For a large list of predefined Color
values, see
Graphics.Rendering.Diagrams.Colors.
fillColor :: Color -> Diagram -> DiagramSource
Draw a diagram using the given fill color. Note that the new color only applies to parts of the diagram which are not otherwise colored; subdiagrams which already have an explicit fill color will not be affected. The default fill color is completely transparent.
fillTransparency :: Int -> Diagram -> DiagramSource
Set the transparency used for the fill color, from 0 (completely
transparent) to 255 (completely opaque). Note that you can set
the alpha channel of the fill color using fillColor
combined
with rgba
, but fillTransparency
allows you to set the alpha
channel independently of the color channels. This means you can
use it together with the colors defined in
Graphics.Rendering.Diagrams.Colors.
ft :: Int -> Diagram -> DiagramSource
ft
is provided as a convenient short synonym for fillTransparency
.
lineColor :: Color -> Diagram -> DiagramSource
Draw a diagram using the given color for lines. Note that the new color only applies to parts of the diagram which are not otherwise colored; subdiagrams which already have an explicit line color will not be affected. The default line color is black.
lineWidth :: Double -> Diagram -> DiagramSource
Draw shape outlines and lines with the given width. Note that the line width is invariant under uniform scaling, although under non-uniform scaling (scaling by different amounts in the x and y axes) lines can become distorted. The default line width is 1.
The Color
type represents colors in red-green-blue-alpha
format, with each channel in the range 0-1. For a large list of
predefined colors, see Graphics.Rendering.Diagrams.Colors.
Construct an opaque (alpha = 1) color from RGB values specified as Doubles in the range 0-1.
:: Double | red channel |
-> Double | green channel |
-> Double | blue channel |
-> Double | alpha (transparency) channel |
-> Color |
Construct a color from RGBA values, specified as Doubles in the range 0-1.
Rendering
:: String | The name of the file to create. |
-> Int | The desired width of the image, in pixels. |
-> Int | The desired height of the image. |
-> Diagram | The diagram to render. |
-> IO () |
Render a diagram to a file in PNG format.
Given a target width and height and a user-constructed Diagram
,
render it using the Cairo rendering library. Note that compose
takes care of all the rendering details, including preprocessing
of the Diagram
, and scaling/translating the final output so
that it fits within the given width and height.
The background of the output diagram will be opaque white.
In order to produce a physical output, the output of compose
must be given as input to an output adapter such as writePng
.