10. StdPicture

While drawing we need the current state called Picture state. The functions that are used for drawing need a Picture state as an argument and return the new updated state. The Draw monad is defined so that it would be easier for the user to write the functions. The Draw monad is in fact an IO monad, but it also takes care of the management of the Picture state. The definition of Draw is abstract

newtype Draw

instance Monad Draw
instance Functor Draw
instance IOMonad Draw

See Section 4, “StdIOCommon” for definition of IOMonad class.

There are some basic objects related to the drawing:

10.1. Pen

data    PenAttribute                                    -- Default:
        = PenSize       Int                             -- 1
        | PenPos        Point2                          -- zero
        | PenColour     Colour                          -- Black
        | PenBack       Colour                          -- White
        | PenFont       Font                            -- defaultFont

setPenAttributes :: [PenAttribute] -> Draw ()
getPenAttributes :: Draw [PenAttribute]

setPenPos :: Point2 -> Draw ()
getPenPos :: Draw Point2

class MovePen f where
        movePenPos :: f -> Draw ()

instance MovePen Vector2
instance MovePen Curve

setPenSize :: Int -> Draw ()
getPenSize :: Draw Int
setDefaultPenSize :: Draw ()

setPenColour :: Colour -> Draw ()
getPenColour :: Draw Colour
setDefaultPenColour :: Draw ()

setPenBack :: Colour -> Draw ()
getPenBack :: Draw Colour
setDefaultPenBack :: Draw ()

setPenFont :: Font -> Draw ()
getPenFont :: Draw Font
setDefaultPenFont :: Draw ()
setPenPos

corresponds to the PenPos attribute. The function moves the pen to the given position.

getPenPos

corresponds to the PenPos attribute. The function returns the current pen position.

class MovePen

With this class the pen position can be moved in the direction of any linear graphical object. There is instances:

instance MovePen Vector2
instance MovePen Oval

where Oval is defined in Section 10.3, “Drawables,Fillables and Hilites”.

setPenSize

corresponds to the PenSize attribute. The function sets the new pen size.

getPenSize

corresponds to the PenSize attribute. The function returns the current pen size.

setDefaultPenSize

corresponds to the PenSize attribute. The function sets the pen size to 1.

setPenColour

corresponds to the PenColour attribute. The function sets the new pen colour.

getPenColour

corresponds to the PenColour attribute. The function returns the current pen colour.

setDefaultPenColour

corresponds to the PenColour attribute. The function sets the pen colour to Black.

setPenBack

corresponds to the PenBack attribute. The function sets the new pen background colour.

getPenBack

corresponds to the PenBack attribute. The function returns the current pen background colour.

setDefaultPenBack

corresponds to the PenBack attribute. The function sets the pen background colour to White.

setPenFont

corresponds to the PenFont attribute. The function sets the new pen font.

getPenFont

corresponds to the PenFont attribute. The function returns the current pen font.

setDefaultPenFont

corresponds to the PenFont attribute. The function sets the pen font to the system dependent default font.

setPenAttributes

setups the pen attributes by a list of values.

getPenAttributes

returns the current pen attributes as a list of values.

10.2. Font

data Font -- abstract

data    FontDef
        = FontDef
                { fName           :: !FontName          -- Name of the font
                , fStyles         :: ![FontStyle]       -- Stylistic variations
                , fSize           :: !FontSize          -- Size in points
                }

type    FontName  = String
type    FontStyle = String
type    FontSize  = Int

data    FontMetrics
        = FontMetrics
                { fAscent         :: !Int               -- Distance between top    and base line
                , fDescent        :: !Int               -- Distance between bottom and base line
                , fLeading        :: !Int               -- Distance between two text lines
                , fMaxWidth       :: !Int               -- Max character width including spacing
                }

openFont :: FontDef -> Draw (Maybe Font)
openDefaultFont :: Draw Font
openDialogFont :: Draw Font

getFontNames :: Draw [FontName]

getFontStyles :: FontName -> Draw [FontStyle]

getFontSizes :: Int -> Int -> FontName -> Draw [FontSize]

getFontDef :: Font -> FontDef

getFontCharWidth :: Font -> Char -> Draw Int
getFontCharWidths :: Font -> [Char] -> Draw [Int]
getFontStringWidth :: Font -> String -> Draw Int
getFontStringWidths :: Font -> [String] -> Draw [Int]
getFontMetrics :: Font -> Draw FontMetrics

getPenFontCharWidth :: Char -> Draw Int
getPenFontCharWidths :: [Char] -> Draw [Int]
getPenFontStringWidth :: String -> Draw Int
getPenFontStringWidths :: [String] -> Draw [Int]
getPenFontMetrics :: Draw FontMetrics
openFont

creates a font with name, size and styles defined in the FontDef parameter.

openDefaultFont

creates the system dependent default font.

openDialogFont

creates a font with the same parameters that are used for the system font for dialogs.

getFontNames

returns a list of names for all existing fonts.

getFontStyles

returns a list of all existing styles for a given font.

getFontSizes sizeBound1 sizeBound2

returns a list of all existing sizes between sizeBound1 and sizeBound2 for a given font.

getFontDef

extracts the font definition from a given font.

getFontCharWidth, getFontCharWidths

calculates the single char width(s) for a given font

getFontStringWidth, getFontStringWidths

calculates the string width(s) for a given font

getFontMetrics

returns FontMetrics data for a given font

getPenFontCharWidth, getPenFontCharWidths

calculates the single char width(s) for the active font

getPenFontStringWidth, getPenFontStringWidths

calculates the string width(s) for the active font

getPenFontMetrics

returns FontMetrics data for the active font

10.3. Drawables,Fillables and Hilites

Basic graphical objects such as line,circle and point are defined in the system. The user can define his/her own shape types. There are three classes which allow user to define the graphical view of his/her shapes.

class Drawables figure where
        draw    :: figure -> Draw ()
        drawAt  :: Point2 -> figure -> Draw ()
        undraw  :: figure -> Draw ()
        undrawAt:: Point2 -> figure -> Draw ()

class Fillables figure where
        fill    :: figure -> Draw ()
        fillAt  :: Point2 -> figure -> Draw ()
        unfill  :: figure -> Draw ()
        unfillAt:: Point2 -> figure -> Draw ()

class Hilites figure where
        hilite  :: figure -> Draw ()
        hiliteAt:: Point2 -> figure -> Draw ()

This is a list of all built-in shapes.

data    Line2                                           -- A line connects two points
        = Line2
                { line_end1       :: !Point2            -- The first  point
                , line_end2       :: !Point2            -- The second point
                }
data    Oval                                            -- An oval is a stretched unit circle
        = Oval
                { oval_rx         :: !Int               -- The horizontal radius (stretch)
                , oval_ry         :: !Int               -- The vertical   radius (stretch)
                }
data    Curve                                           -- A curve is a slice of an oval
        = Curve
                { curve_oval      :: !Oval              -- The source oval
                , curve_from      :: !Float             -- Starting angle (in radians)
                , curve_to        :: !Float             -- Ending   angle (in radians)
                , curve_clockwise :: !Bool              -- Direction: True iff clockwise
                }
data    Box                                             -- A box is a rectangle
        = Box
                { box_w           :: !Int               -- The width  of the box
                , box_h           :: !Int               -- The height of the box
                }
data    Polygon                                         -- A polygon is an outline shape
        = Polygon
                { polygon_shape   :: ![Vector2]         -- The shape of the polygon
                }

Instances for Drawables:

instance Drawables Char
instance Drawables String
instance Drawables Vector2
instance Drawables Rectangle
instance Drawables Line2
instance Drawables Oval
instance Drawables Curve
instance Drawables Box
instance Drawables Polygon

Instances for Fillables:

instance Fillables Rectangle
instance Fillables Oval
instance Fillables Curve
instance Fillables Box
instance Fillables Polygon

Instances for Hilites:

instance Hilites Box where
instance Hilites Rectangle where