(lispkit draw)
Last updated
Last updated
Library (lispkit draw)
provides 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
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)
Returns #t
if obj is a drawing; otherwise, it returns #f
.
(make-drawing)
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
and remove-shadow
)
Transformation (add transformation via enable-transformation
and remove via disable-transformation
)
(copy-drawing drawing)
Returns a copy of the given drawing.
Sets the stroke color for the given drawing, or current-drawing
if the drawing argument is not provided.
Sets the fill color for the given drawing, or current-drawing
if the drawing argument is not provided.
Sets the default stroke width for the given drawing, or current-drawing
if the drawing argument is not provided.
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.
Removes shadow for the subsequent drawing instructions of the given drawing, or current-drawing
if the drawing argument is missing.
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.
Disables the transformation tf for subsequent drawing instructions of the given drawing, or current-drawing
if the drawing argument is missing.
Draws shape with a given stroke width into the drawing specified via drawing or parameter object current-drawing
if drawing is not provided. The default for width, in case it is not provided, is set via set-line-width. The stroke is drawn in the current stroke color of the 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.
Fills shape with the current fill color in the drawing specified via drawing or parameter object current-drawing
if drawing is not provided.
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.
Draws a line between point start and point end in the drawing specified via argument drawing or parameter object current-drawing
, if drawing is not provided. The line is drawn in the default stroke width and the current stroke color.
Draws a rectangular given by rect in the drawing specified via argument drawing or parameter object current-drawing
, if drawing is not provided. The rectangular is drawn in the default stroke width and the current stroke color.
Fills a rectangular given by rect with the current fill color in the drawing specified via argument drawing or parameter object current-drawing
, if drawing is not provided.
Draws an ellipse into the rectangular rect in the drawing specified via argument drawing or parameter object current-drawing
, if drawing is not provided. The ellipse is drawn in the default stroke width and the current stroke color.
Fills an ellipse given by rectangular rect with the current fill color in the drawing specified via argument drawing or parameter object current-drawing
, if drawing is not provided.
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.
Returns a size object describing the width and height needed to draw string str using font in a space constrained by dimensions. dimensions is either a size object specifying the maximum width and height, or it is a number constraining the width only, assuming infinite hight. If dimensions is omitted, the maximum width and height is infinity.
Draws styled text txt at location 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 styled text is drawn, or it is a rect specifying a bounding box.
Returns a size object describing the width and height needed to draw styled text txt in a space constrained by dimensions. dimensions is either a size object specifying the maximum width and height, or it is a number constraining the width only, assuming infinite hight. If dimensions is omitted, the maximum width and height is infinity.
Draws a string html containing HTML source code at location 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 HTML is drawn, or it is a rect specifying a bounding box.
Returns a size object describing the width and height needed to render the HTML in string html in a space constrained by dimensions. dimensions is either a size object specifying the maximum width and height, or it is a number constraining the width only, assuming infinite hight. If dimensions is omitted, the maximum width and height is infinity.
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.
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.
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.
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.
Saves drawing into a PDF file at the given filepath 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)
. title and author are optional strings defining the title and author metadata for the generated PDF file.
Saves a list of pages into a PDF file at the given filepath path. A page is defined in terms of a list of two elements (drawing size), where drawing is a drawing for that page and size is a media box for the page. title and author are optional strings defining the title and author metadata for the generated PDF file.
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.
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.
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.
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
.
Returns #t
if obj is a shape. Otherwise, it returns #f
.
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.
Returns a copy of shape.
Retuns a new line shape. start and end are the start and end points of the line.
Returns a new polygon shape. The polygon is defined in terms of a sequence of points.
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.
Returns a new circle shape. The circle is defined in terms of a center point and a radius.
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.
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
.
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.
Returns a new shape derived from shape by applying transformation tf.
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
.
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
.
Sets the "current point" to point for the shape specified by argument shape or parameter object current-shape
if shape is not provided.
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".
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.
This function is equivalent to move-to
with the exception that point is relative to the "current point".
This function is equivalent to line-to
with the exception that point is relative to the "current point".
This function is equivalent to curve-to
with the exception that point, cntrl1 and cntrl2 are relative to the "current point".
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.
Returns the bounding box for the given shape as a rect.
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.
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 objects representing immutable pictures of mutable size and metadata. 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. Bitmap-based images optionally have mutable EXIF data.
Returns #t
if obj is an image. Otherwise, it returns #f
.
Loads an image from the file at path and returns the corresponding image object.
Loads an image from the file at the given relative file path and returns the corresponding image object. type refers to the default suffix of the file to load (e.g. "png"
for PNG images).
load-image-asset
constructs a relative file path in the following way (assuming path does not have a suffix already):
dir/path.type
where dir is "Images"
if it is not provided as a parameter. It then searches the asset paths in their given order for a file matching this relative file path. Once the first matching file is found, the file is loaded as an image file and the image gets returned by load-image-asset
. It is an error if no matching image was found.
Loads an image from the binary data provided by bytevector bvec between positions start and end and returns the corresponding image object.
Returns the size of the given image object in points.
Sets the size of image to size, a size in points.
Returns #t
if obj is a bitmap-based image. Otherwise, it returns #f
.
Returns the size of the bitmap bmap in points. If bmap is not a bitmap object, bitmap-size
returns #f
.
Returns the number of horizontal and vertical pixels of the bitmap bmap as a size. If bmap is not a bitmap object, bitmap-size
returns #f
.
Returns the EXIF metadata associated with bitmap bmap. EXIF metadata is represented as an association list in which symbols are used as keys.
Sets the EXIF metadata for the given bitmap bmap to exif. exif is an association list defining all the EXIF attributes with symbols being used as keys.
Creates a new bitmap-based image by drawing the object drawing into an empty bitmap of size size in points. ppi determines the number of pixels per inch. By default, ppi is set to 72. In this case, the number of pixels of the bitmap corresponds to the number of points (since 1 pixel corresponds to 1/72 of an inch). For a ppi value of 144, the horizontal and vertial number of pixels is doubled, etc.
Crops a rectangle from the given bitmap and returns the result in a new bitmap. rect is a rectangle in pixels. Its intersection with the dimensions of bitmap (in pixels) are used for cropping.
Blurs the given bitmap with the given blur radius and returns the result in a new bitmap of the same size.
Saves a given bitmap-based image bitmap in a file at filepath path. format is a symbol specifying the image file format. Supported are: png
, jpg
, gif
, bmp
, and tiff
.
Returns a bytevector with an encoding of bitmap in the given format. format is a symbol specifying the image 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.
Returns #t
if obj is a transformation. Otherwise, it returns #f
.
Creates a new transformation by composing the given transformations tf.
Inverts transformation tf and returns a new transformation object for it.
Returns a transformation for shifting the coordinate system by dx and dy. If transformation tf is provided, the translation transformation extends tf.
Returns a transformation for scaling the coordinate system by dx and dy. If transformation tf is provided, the scaling transformation extends 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.
(lispkit draw)
supports the concept of color lists on macOS. A color list is provided as a .plist
file and stored in the "ColorLists" asset directory of LispKit. It maps color names expressed as symbols to color values. Color lists need to be loaded explicitly via procedure load-color-list
.
Returns #t
if obj is a color. Otherwise, it returns #f
.
This procedure returns new color objects. If spec is provided, it either is a string containing a color description in hex format, or it is a symbol referring to the name of a color in the default color list (White Yellow Red Purple Orange Magenta Green Cyan Brown Blue Black)
. If a different color list should be used, its name can be specified via string clist. Procedure (available-color-lists)
returns a list of all available color lists. If the color is specified via a hex string, the following formats can be used: "ccc"
, "#ccc"
, "rrggbb"
, and "#rrggbb"
.
The color can also be specified using 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.
Returns the red color component of color.
Returns the green color component of color.
Returns the blue color component of color.
Returns the alpha color component of color.
Returns a representation of the given color in hex form as a string.
Predefined color objects.
Returns a list of available color lists. The LispKit installation guarantees that there is at least color list "HTML" containing all named colors from the HTML 5 specification.
Loads a new color list stored as a .plist
file in the assets directory of LispKit at the given file path (which can also refer to color lists outside of the assets directory via absolute file paths). name is a string which specifies the name of the color list. It is added to the list of available colors if loading of the color list was successful. load-color-list
returns #t
if the color list could be successfully loaded, #f
otherwise.
Returns a list of color identifiers supported by the given color list. clist is a string specifying the name of the color list.
Fonts are immutable objects defining fonts in terms of a font name and a font size (in points).
Returns #t
if obj is a font. Otherwise, it returns #f
.
If only two arguments, fontname and size, are provided, font
will return a new font object for a font with the given font name and font size (in points). If more than two arguments are provided, font
will return a new font object for a font with the given font family name, font size (in points), font weight, as well as a number of font traits.
The weight of a font is specified as an integer on a scale from 0 to 15. Library (lispkit draw)
exports the following weight constants:
ultralight
(1)
thin
(2)
light
(3)
book
(4)
normal
(5)
medium
(6)
demi
(7)
semi
(8)
bold
(9)
extra
(10)
heavy
(11)
super
(12)
ultra
(13)
extrablack
(14)
Font traits are specified as integer masks. The following trait constants are exported from library (lispkit draw)
:
italic
boldface
unitalic
unboldface
narrow
expanded
condensed
small-caps
poster
compressed
monospace
Returns the font name of font.
Returns the font family name of font.
Returns the font size of font in points.
Returns the font weight of font. See documentation of function font
for details.
Returns the font traits of font as an integer bitmask. See documentation of function font
for details.
Returns #t
if font font has all the given traits.
Returns all the available fonts that have matching font traits.
Returns all the available font families, i.e. all font families for which there is at least one font installed.
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)
.
Returns #t
if obj is a valid point. Otherwise, it returns #f
.
Returns a point for coordinates x and y.
Moves point by dx and dy and returns the result as a point.
Returns the x-coordinate for point.
Returns the y-coordinate for point.
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)
.
Returns #t
if obj is a valid size. Otherwise, it returns #f
.
Returns a size for the given width w and height h.
Returns the width for size.
Returns the height for size.
Returns a new size object whose width is increased by dx and whose height is increased by dy.
Returns a new size object whose width and height is multiplied by factor.
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))
.
Returns #t
if obj is a valid rect. Otherwise, it returns #f
.
Returns a rect either from the given point and size, or from x-coordinate x, y-coordinate y, width w, and height h.
Moves rect by dx and dy and returns the result.
Returns the upper left corner point of rect.
Returns the size of the rect.
Returns the x-coordinate of the upper left corner point of rect.
Returns the y-coordinate of the upper left corner point of rect.
Returns the size of rect as a size, i.e. as a pair of floating-point numbers where the car representes the width and the cdr represents the height of rect.
Returns the width of rect.
Returns the height of rect.
(set-color color) (set-color color drawing)
(set-fill-color color) (set-fill-color color drawing)
(set-line-width width) (set-line-width width drawing)
(set-shadow color size blur-radius) (set-shadow color size blur-radius drawing)
(remove-shadow) (remove-shadow drawing)
(enable-transformation tf) (enable-transformation tf drawing)
(disable-transformation tf) (disable-transformation tf drawing)
(draw shape) (draw shape width) (draw shape width drawing)
(draw-dashed shape lengths phase) (draw-dashed shape lengths phase width) (draw-dashed shape lengths phase width drawing)
(fill shape) (fill shape drawing)
(fill-gradient shape colors) (fill-gradient shape colors spec) (fill-gradient shape colors spec drawing)
(draw-line start end) (draw-line start end drawing)
(draw-rect rect) (draw-rect rect drawing)
(fill-rect rect) (fill-rect rect drawing)
(draw-ellipse rect) (draw-ellipse rect drawing)
(fill-ellipse rect) (fill-ellipse rect drawing)
(draw-text str location font) (draw-text str location font color) (draw-text str location font color drawing)
(text-size str) (text-size str font) (text-size str font dimensions)
(draw-styled-text txt location) (draw-styled-text txt location drawing)
(styled-text-size txt) (styled-text-size txt dimensions)
(draw-html html location) (draw-html html location drawing)
(html-size html) (html-size html dimensions)
(draw-image image location) (draw-image image location opacity) (draw-image image location opacity composition) (draw-image image location opacity composition drawing)
(draw-drawing other) (draw-drawing other drawing)
(clip-drawing other clippingshape) (clip-drawing other clippingshape drawing)
(inline-drawing other) (inline-drawing other drawing)
(save-drawing path drawing size) (save-drawing path drawing size title) (save-drawing path drawing size title author)
(save-drawings path pages) (save-drawings path pages title) (save-drawings path pages title author)
(drawing body ...)
(with-drawing drawing body ...)
(transform tf body ...)
current-shape
(shape? obj)
(make-shape) (make-shape prototype)
(copy-shape shape)
(line start end)
(polygon point ...)
(rectangle point size) (rectangle point size radius) (rectangle point size xradius yradius)
(circle point radius)
(oval point size)
(arc point radius start end) (arc point radius start end clockwise)
(glyphs str point size font)
(transform-shape shape tf)
(flip-shape shape) (flip-shape shape box) (flip-shape shape box orientation)
(interpolate points) (interpolate points closed) (interpolate points closed alpha) (interpolate points closed alpha method)
(move-to point) (move-to point shape)
(line-to point ...) (line-to point ... shape)
(curve-to point cntrl1 cntrl2) (curve-to point cntrl1 cntrl2 shape)
(relative-move-to point) (relative-move-to point shape)
(relative-line-to point ...) (relative-line-to point ... shape)
(relative-curve-to point cntrl1 cntrl2) (relative-curve-to point cntrl1 cntrl2 shape)
(add-shape other) (add-shape other shape)
(shape-bounds shape)
(shape body ...)
(with-shape shape body ...)
(image? obj)
(load-image path)
(load-image-asset path type) (load-image-asset path type dir)
(bytevector->image bvec) (bytevector->image bvec start) (bytevector->image bvec start end)
(image-size image)
(set-image-size! image size)
(bitmap? obj)
(bitmap-size bmap)
(bitmap-pixels bmap)
(bitmap-exif-data bmap)
(set-bitmap-exif-data! bmap exif)
(make-bitmap drawing size) (make-bitmap drawing size ppi)
(bitmap-crop bitmap rect)
(bitmap-blur bitmap radius)
(save-bitmap path bitmap format)
(bitmap->bytevector bitmap format)
(transformation? obj)
(transformation tf ...)
(invert tf)
(translate dx dy) (translate dx dy tf)
(scale dx dy) (scale dx dy tf)
(rotate angle) (rotate angle tf)
(color? obj)
(color spec) (color name clist) (color r g b) (color r g b alpha)
(color-red color)
(color-green color)
(color-blue color)
(color-alpha color)
(color->hex color)
black gray white red green blue yellow
(available-color-lists)
(load-color-list name path)
(available-colors clist)
(font? obj)
(font fontname size) (font familyname size weight trait ...)
(font-name font)
(font-family-name font)
(font-size font)
(font-weight font)
(font-traits font)
(font-has-traits font trait ...)
(available-fonts) (available-fonts trait ...)
(available-font-families)
(point? obj)
(point x y)
(move-point point dx fy)
(point-x point)
(point-y point)
zero-point
(size? obj)
(size w h)
(size-width size)
(size-height size)
(increase-size size dx dy)
(scale-size size factor)
(rect? obj)
(rect point size) (rect x y width height)
(move-rect rect dx dy)
(rect-point rect)
(rect-size rect)
(rect-x rect)
(rect-y rect)
(rect-size rect)
(rect-width rect)
(rect-height rect)