-
Notifications
You must be signed in to change notification settings - Fork 16
LispKit Draw
Library (lispkit draw)
defines an API for creating drawings. A drawing is defined in terms of a sequence of instructions for drawing shapes and images. Drawings can be composed and saved as a PDF. It is also possible to draw a drawing into a bitmap and save it in formats like PNG, JPG, or TIFF. A bitmap is a special image that is not based on vector graphics.
Both drawings and shapes are based on a coordinate system whose zero point is in the upper left corner of a plane. The x and y axis extend to the right and down. Coordinates and dimensions are always expressed in terms of floating-point numbers.
Drawings are mutable objects created via make-drawing
. The functions listed in this section change the state of a drawing object and they persist drawing instructions defining the drawing. For most functions, the drawing is an optional argument. If it is not provided, the function applies to the drawing provided by the current-drawing
parammeter object.
current-drawing [parameter object]
Defines the current drawing, which is used as a default by all functions for which the drawing argument is optional. If there is no current drawing, this parameter is set to #f
.
(drawing? obj) [procedure]
Returns #t
if obj is a drawing. Otherwise, it returns #f
.
(make-drawing) [procedure]
Returns a new, empty drawing. A drawing consists of a sequence of drawing instructions and drawing state consisting of the following components:
- Stroke color (set via
set-color
) - Fill color (set via
fill-color
) - Shadow (set via
set-shadow
andremove-shadow
) - Transformation (add transformation via
enable-transformation
and remove viadisable-transformation
)
(copy-drawing drawing) [procedure]
Returns a copy of the given drawing.
(set-color color) [procedure]
(set-color color drawing)
Sets the stroke color for the given drawing, or current-drawing
if the drawing argument is not provided.
(set-fill-color color) [procedure]
(set-fill-color color drawing)
Sets the fill color for the given drawing, or current-drawing
if the drawing argument is not provided.
(set-shadow color size blur-radius) [procedure]
(set-shadow color size blur-radius drawing)
Defines a shadow for the given drawing, or current-drawing
if the drawing argument is not provided. color is the color of the shade, blur-radius defines the radius for bluring the shadow.
(remove-shadow) [procedure]
(remove-shadow drawing)
Removes shadow for the subsequent drawing instructions of the given drawing, or current-drawing
if the drawing argument is missing.
(enable-transformation tf) [procedure]
(enable-transformation tf drawing)
Enables the transformation tf for subsequent drawing instructions of the given drawing, or current-drawing
if the drawing argument is missing. Each drawing maintains an active affine transformation for shifting, rotating, and scaling the coordinate systems of subsequent drawing instructions.
(disable-transformation tf) [procedure]
(disable-transformation tf drawing)
Disables the transformation tf for subsequent drawing instructions of the given drawing, or current-drawing
if the drawing argument is missing.
(draw shape) [procedure]
(draw shape width)
(draw shape width drawing)
Draws shape with a given stroke width into the drawing specified via drawing or parameter object current-drawing
if drawing is not provided. 1.0
is the default for width in case it is not provided. The stroke is drawn in the current stroke color of the drawing.
(draw-dashed shape lengths phase) [procedure]
(draw-dashed shape lengths phase width)
(draw-dashed shape lengths phase width drawing)
Draws shape with a dashed stroke of width width into the drawing specified via drawing or parameter object current-drawing
if drawing is not provided. 1.0
is the default for width in case it is not provided. lengths specifies an alternating list of dash/space lengths. phase determines the start of the dash/space pattern. The dashed stroke is drawn in the current stroke color of the drawing.
(fill shape) [procedure]
(fill shape drawing)
Fills shape with the current fill color in the drawing specified via drawing or parameter object current-drawing
if drawing is not provided.
(fill-gradient shape colors) [procedure]
(fill-gradient shape colors spec)
(fill-gradient shape colors spec drawing)
Fills shape with a gradient in the drawing specified via argument drawing or parameter object current-drawing
if drawing is not provided. The gradient is specified in terms of a list of colors and argument spec. spec can either be a number or a point. If spec is a number, this number determines an angle for a linear gradient. If spec is a point, it is the center of a radial gradient.
(draw-text str location font) [procedure]
(draw-text str location font color)
(draw-text str location font color drawing)
Draws string str at location in the given font and color in the drawing specified by argument drawing or parameter object current-drawing
if drawing is not provided. location is either the left, top-most point at which the string is drawn, or it is a rect specifying a bounding box. color specifies the text color. If it is not provided, the text is drawn in black.
(draw-image image location) [procedure]
(draw-image image location opacity)
(draw-image image location opacity composition)
(draw-image image location opacity composition drawing)
Draws image image at location with the given opacity and composition method. The image is drawn in the drawing specified by argument drawing or parameter object current-drawing
if drawing is not provided. location is either the left, top-most point at which the image is drawn, or it is a rect specifying a bounding box for the image. composition is a floating-point number between 0.0 (= transparent) and 1.0 (= completely not transparent) with 1.0 being the default. composition refers to a symbol specifying a composition method. The following methods are supported (the source is the image, the destination is the drawing):
-
clear
: Transparency everywhere. -
copy
: The source image (default). -
multiply
: The source color is multiplied by the destination color. -
overlay
: Source colors overlay the destination. -
source-over
: The source image wherever it is opaque, and the destination elsewhere. -
source-in
: The source image wherever both images are opaque, and transparent elsewhere. -
source-out
: The source image wherever it is opaque and the destination is transparent, and transparent elsewhere. -
source-atop
: The source image wherever both source and destination are opaque, the destination wherever it is opaque but the source image is transparent, and transparent elsewhere. -
destination-over
: The destination wherever it is opaque, and the source image elsewhere. -
destination-in
: The destination wherever both images are opaque, and transparent elsewhere. -
destination-out
: The destination wherever it is opaque and the source image is transparent, and transparent elsewhere. -
destination-atop
: The destination wherever both image and destination are opaque, the source image wherever it is opaque and the destination is transparent, and transparent elsehwere.
(draw-drawing other) [procedure]
(draw-drawing other drawing)
Draws drawing other into the drawing specified by argument drawing or parameter object current-drawing
if drawing is not provided. This function can be used
to compose drawings.
(clip-drawing other clippingshape) [procedure]
(clip-drawing other clippingshape drawing)
Draws drawing other into the drawing specified by argument drawing or parameter object current-drawing
if drawing is not provided. This function clips the drawing using shape clippingshape; i.e. only parts within clippingshape are drawn.
(inline-drawing other) [procedure]
(inline-drawing other drawing)
Draws drawing other into the drawing specified by argument drawing or parameter object current-drawing
if drawing is not provided. This function can be used to compose drawings in a way such that the drawing instructions from other are inlined into drawing.
(save-drawing drawing path size) [procedure]
(save-drawing drawing path size title)
(save-drawing drawing path size title author)
Saves drawing into a PDF file at the given path. size is a size specifying the width and height of the PDF page containing the drawing in points; i.e. the media box of the page is (rect zero-point
size)
.
(drawing body ...) [syntax]
Creates a new empty drawing, binds parameter object current-drawing
to it and executes the body statements in the dynamic scope of this binding. This special form returns the new drawing.
(with-drawing drawing body ...) [syntax]
Binds parameter object current-drawing
to drawing and executes the body statements in the dynamic scope of this binding. This special form returns the result of the last statement in the body.
(transform tf body ...) [syntax]
This form is used in the context of drawing into current-drawing
. It enables the transformation tf, executes the statements in the body and disables the transformation again.
Shapes are mutable objects created via a number of constructors, including make-shape
, copy-shape
, line
, polygon
, rectangular
, circle
, oval
, arc
, and glyphs
. Besides the constructors, functions like move-to
, line-to
and curve-to
are used to extend a shape. For those functions, the affected shape is an optional argument. If it is not provided, the function applies to the shape defined by the current-shape
parammeter object.
current-shape [parameter object]
Defines the current shape, which is used as a default by all functions for which the shape argument is optional. If there is no current shape, this parameter is set to #f
.
(shape? obj) [procedure]
Returns #t
if obj is a shape. Otherwise, it returns #f
.
(make-shape) [procedure]
(make-shape prototype)
Returns a new shape object. If argument prototype is provided, the new shape object will inherit from shape prototype; i.e. the new shape's definition will extend the definition of shape prototype.
(copy-shape shape) [procedure]
Returns a copy of shape.
(line start end) [procedure]
Retuns a new line shape. start and end are the start and end points of the line.
(polygon point ...) [procedure]
Returns a new polygon shape. The polygon is defined in terms of a sequence of points.
(rectangle point size) [procedure]
(rectangle point size radius)
(rectangle point size xradius yradius)
Returns a new rectangular shape. The rectangle is defined in terms of the left, topmost point and a size defining both width and height of the rectangle. The optional radius, xradius and yradius arguments are used to create a rounded rectangular whose rounded edges are defined in terms of an x and y-radius. If only one radius is provided, it defines both x and y-radius.
(circle point radius) [procedure]
Returns a new circle shape. The circle is defined in terms of a center point and a radius.
(oval point size) [procedure]
Returns a new oval shape. The oval is defined in terms of a rectangle whose left, topmost point is provided as argument point, and whose width and height are given via argument size.
(arc point radius start end) [procedure]
(arc point radius start end clockwise)
Returns a new arc shape. The arc is defined via the arguments point, radius, start , end and optionally clockwise. point is the starting point of the arc, radius defines the radius of the arc, start is a starting angle in radians, and end is the end angle in radians. clockwise is a boolean argument defining whether the arc is drawn clockwise or counter-clockwise. The default is #t
.
(glyphs str point size font) [procedure]
Returns a new "glyphs" shape. This is a shape defined by a string str rendered in the given size and font at a given point. font is a font object, size is the font size in points, and point are the start coordinates where the glyphs are drawn.
(transform-shape shape tf) [procedure]
Returns a new shape derived from shape by applying transformation tf.
(flip-shape shape) [procedure]
(flip-shape shape box)
(flip-shape shape box orientation)
Returns a new shape by flipping/mirroring shape within box. box is a rect. If it is not provided, the bounding box of shape is used as a default. Argument orientation is a symbol defining along which axis the shape is flipped. Supported are horizontal
, vertical
, and mirror
. Default is vertical
.
(interpolate points) [procedure]
(interpolate points closed)
(interpolate points closed alpha)
(interpolate points closed alpha method)
Returns a shape interpolating a list of points. closed is an optional boolean argument specifying whether the shape is closed. The default for closed is #f
. alpha is an interpolation parameter in the range [0.0,1.0]; default is 0.33. method specifies the interpolation method via a symbol. The following two methods are supported: hermite
and catmull-rom
; default is hermite
.
(move-to point) [procedure]
(move-to point shape)
Sets the "current point" to point for the shape specified by argument shape or parameter object current-shape
if shape is not provided.
(line-to point ...) [procedure]
(line-to point ... shape)
Creates a line from the "current point" to point for the shape specified by argument shape or parameter object current-shape
if shape is not provided. point becomes the new "current point".
(curve-to point cntrl1 cntrl2) [procedure]
(curve-to point cntrl1 cntrl2 shape)
Creates a curve from the "current point" to point for the shape specified by argument shape or parameter object current-shape
if shape is not provided. cntrl1 and cntrl2 are control points defining tangets shaping the curve at the start and end points.
(relative-move-to point) [procedure]
(relative-move-to point shape)
This function is equivalent to move-to
with the exception that point is relative to the "current point".
(relative-line-to point ...) [procedure]
(relative-line-to point ... shape)
This function is equivalent to line-to
with the exception that point is relative to the "current point".
(relative-curve-to point cntrl1 cntrl2) [procedure]
(relative-curve-to point cntrl1 cntrl2 shape)
This function is equivalent to curve-to
with the exception that point, cntrl1 and cntrl2 are relative to the "current point".
(add-shape other) [procedure]
(add-shape other shape)
Adds shape other to the shape specified by argument shape or parameter object current-shape
if shape is not provided. This function is typically used to compose shapes.
(shape-bounds shape) [procedure]
Returns the bounding box for the given shape as a rect.
(shape body ...) [syntax]
Creates a new empty shape, binds parameter object current-shape
to it and executes the body statements in the dynamic scope of this binding. This special form returns the new drawing.
(with-shape shape body ...) [syntax]
Binds parameter object current-shape
to shape and executes the body statements in the dynamic scope of this binding. This special form returns the result of the last statement in the body.
Images are immutable objects representing pictures of a fixed size. Images are either loaded from image files or they are created from drawings. Images are either vector-based or bitmap-based. The current image API only allows loading vector-based images from PDF files. Bitmap-based images, on the other hand, can be loaded from PNG, JPG, GIF, etc. image files or they are created by drawing a drawing object into an empty bitmap.
(image? obj) [procedure]
Returns #t
if obj is an image. Otherwise, it returns #f
.
(load-image path) [procedure]
Loads an image from the file at path and returns the corresponding image object.
(image-size image) [procedure]
Returns the size of the given image object.
(bitmap? obj) [procedure]
Returns #t
if obj is a bitmap-based image. Otherwise, it returns #f
.
(make-bitmap drawing size scale) [procedure]
Creates a new bitmap-based image by drawing the object drawing into an empty bitmap of size size. scale is a factor determining the pixel density. For a scale factor of 1.0 (default), size corresponds to the number of horizontal and vertical pixels. For 2.0, the number of pixels is the double of the horizontal and vertical size, etc.
(save-bitmap bitmap path format) [procedure]
Saves a given bitmap-based image in a file at path. format is a symbol specifying the image file format. Supported are: png
, jpg
, gif
, bmp
, and tiff
.
A transformation is an immutable object defining an affine transformation. Transformations can be used to:
- shift,
- scale, and
- rotate coordinate systems.
Transformations are typically used in drawings to transform drawing instructions. They can also be used to transform shapes.
(transformation? obj) [procedure]
Returns #t
if obj is a transformation. Otherwise, it returns #f
.
(transformation tf ...) [procedure]
Creates a new transformation by composing the given transformations tf.
(invert tf) [procedure]
Inverts transformation tf and returns a new transformation object for it.
(translate dx dy) [procedure]
(translate dx dy tf)
Returns a transformation for shifting the coordinate system by dx and dy. If transformation tf is provided, the translation transformation extends tf.
(scale dx dy) [procedure]
(scale dx dy tf)
Returns a transformation for scaling the coordinate system by dx and dy. If transformation tf is provided, the scaling transformation extends tf.
(rotate angle) [procedure]
(rotate angle tf)
Returns a transformation for rotating the coordinate system by angle (in radians). If transformation tf is provided, the rotation transformation extends tf.
Colors are immutable objects defining colors in terms of four components: red, green, blue and alpha. Library (lispkit draw)
currently only supports RGB color spaces.
(color? obj) [procedure]
Returns #t
if obj is a color. Otherwise, it returns #f
.
(color r g b) [procedure]
(color r g b alpha)
Returns a new color object for the given color components r, g, b, and alpha. alpha determines the transparency of the color (0.0 = fully transparent, 1.0 = no transparency). The default value for alpha is 1.0.
(color-red color) [procedure]
Returns the red color component of color.
(color-green color) [procedure]
Returns the green color component of color.
(color-blue color) [procedure]
Returns the blue color component of color.
(color-alpha color) [procedure]
Returns the alpha color component of color.
black [object]
gray
white
red
green
blue
yellow
Predefined color objects.
Fonts are immutable objects defining fonts in terms of a font name and a font size (in points).
(font? obj) [procedure]
Returns #t
if obj is a font. Otherwise, it returns #f
.
(font name size) [procedure]
Returns a new font object for a font with the given name and font size (in points).
(font-name font) [procedure]
Returns the name of font.
(font-size font) [procedure]
Returns the font size of font in points.
A point describes a location on a two-dimensional plane consisting of a x and y coordinate. Points are represented as pairs of floating-point numbers where the car representes the x-coordinate and the cdr represents the y-coordinate. Even though an expression like '(3.5 . -2.0)
does represent a point, it is recommended to always construct points via function point
; e.g. (point 3.5 -2.0)
.
(point? obj) [procedure]
Returns #t
if obj is a valid point. Otherwise, it returns #f
.
(point x y) [procedure]
Returns a point for coordinates x and y.
(point-x point) [procedure]
Returns the x-coordinate for point.
(point-y point) [procedure]
Returns the y-coordinate for point.
zero-point [object]
The zero point, i.e (point 0.0 0.0)
.
A size describes the dimensions of a rectangle consisting of width and height values. Sizes are represented as pairs of floating-point numbers where the car representes the width and the cdr represents the height. Even though an expression like '(5.0 . 3.0)
does represent a size, it is recommended to always construct sizes via function size
; e.g. (size 5.0 3.0)
.
(size? obj) [procedure]
Returns #t
if obj is a valid size. Otherwise, it returns #f
.
(size w h) [procedure]
Returns a size for the given width w and height h.
(size-width size) [procedure]
Returns the width for size.
(size-height size) [procedure]
Returns the height for size.
A rect describes a rectangle in terms of an upper left point and a size. Rects are represented as pairs whose car is a point and whose cdr is a size. Even though an expression like '((1.0 . 2.0) . (3.0 4.0))
does represent a rect, it is recommended to always construct rects via function rect
; e.g. (rect (point 1.0 2.0) (size 3.0 4.0))
.
(rect? obj) [procedure]
Returns #t
if obj is a valid rect. Otherwise, it returns #f
.
(rect point size) [procedure]
(rect x y width height)
Returns a rect either from the given point and size, or from x-coordinate x, y-coordinate y, width w, and height h.
(rect-point rect) [procedure]
Returns the upper left corner point of rect.
(rect-size rect) [procedure]
Returns the size of the rect.
(rect-x rect) [procedure]
Returns the x-coordinate of the upper left corner point of rect.
(rect-y rect) [procedure]
Returns the y-coordinate of the upper left corner point of rect.
(rect-width rect) [procedure]
Returns the width of rect.
(rect-height rect) [procedure]
Returns the height of rect.