Magick::Draw.new [ { optional parameters } ] -> draw
Creates a new Draw object.
If you plan to use the annotate
method with
this object you can initialize the attributes in the associated block.
Draw
object.
gc = Draw.new
draw.annotate(img, width, height, x, y, text) [ { additional parameters } ] -> draw
Annotates a image with text. The text is positioned
according to the gravity
attribute around the
rectangle described by the x, y, width, and height arguments.
If either of the width or height arguments are omitted, uses
the image width - x and the image height - y to compute the
rectangle width and height. The attributes described in
annotate attributes, below, influence
the appearance and position of the text. These attributes may
be set in the Draw object before calling annotate
,
or within annotate
's optional additional parameters block.
Note: all of the annotate
attributes
are set-only.
self
This example is an excerpt of colors.rb. Many other
examples also use annotate
.
title.annotate(montage, 0,0,0,40, 'Named Colors') { self.font_family = 'Helvetica' self.fill = 'white' self.stroke = 'transparent' self.pointsize = 32 self.font_weight = BoldWeight self.gravity = NorthGravity }
Escape a blank, quote, or apostrophe by preceding it with a backslash ("\"). To include a backslash in the text, use two consecutive backslashes. To include a '%' in the text, use '%%'. You can include information about the image by including one or more of the special character sequences shown in this table.
Format | Replaced by |
---|---|
%b | file size in bytes |
%c | "comment" property string |
%d | directory in which the file resides |
%e | filename extension |
%f | original filename |
%g | group number |
%h | height |
%i | current filename |
%k | number of unique colors |
%l | "label" property string |
%m | file format |
%n | number of images in the sequence (for RMagick, this is always 1) |
%o | output filename |
%p | page number (for RMagick, this is always 1) |
%q | depth |
%r | A string of the form "ClasstypeColorspace," for example
"DirectClassRGB". If the image's matte attribute
is true , appends the word "Matte" to the end
of the string. |
%s | scene number (always 1) |
%t | filename without extension |
%u | unique temporary filename |
%w | width |
%x | x resolution |
%y | y resolution |
%z | depth |
This method is also defined in the Image class.
AnnotateImage
draw.clone -> aDraw
Same as dup except the frozen state of the original is propogated to the copy.
A new Draw object.
draw.draw(img) -> draw
Draws the list of graphic primitives on the specified image.
Calling draw
does not affect the list of
primitives. Each time you call draw
the primitives
will be re-executed from the beginning.
Either an imagelist or a image. If an imagelist, draws on the current image.
self
See the other examples on this page.
DrawImage
draw.dup -> aDraw
Duplicate a Draw
object.
An exact copy of the receiver, including the list of drawing primitives.
draw.get_multiline_type_metrics([anImage,] aString) -> aTypeMetric
draw.get_type_metrics([anImage,] aString) -> aTypeMetric
Returns information for a specific string if rendered on a
image. The get_multiline_type_metrics
method is
the same as get_type_metrics
, except the former
returns the maximum height and width for multiple lines of
text. (Text lines are separated by "\n" characters.)
The Magick++ documentation for its TypeMetric class provides this useful additional information. (I've changed it a bit to use RMagick names.)
The TypeMetric class provides the means to pass data from the Draw class's get_type_metric method to the user. It provides information regarding font metrics such as ascent, descent, text width, text height, and maximum horizontal advance. The units of these font metrics are in pixels...(T)he metrics are dependent on the current Image font (default Ghostscript's "Helvetica"), pointsize (default 12 points), and x/y resolution (default 72 DPI) settings.
The pixel units may be converted to points (the standard resolution-independent measure used by the typesetting industry) via the following equation:
size_points = (size_pixels * 72)/resolution
where resolution is in dots-per-inch (DPI). This means that at the default image resolution, there is one pixel per point.
Note that a font's pointsize is only a first-order approximation of the font height (ascender + descender) in points. The relationship between the specified pointsize and the rendered font height is determined by the font designer.
See FreeType Glyph Conventions for a detailed description of font metrics related issues.
get_type_metrics
substitutes a dummy
image with default attributes. The string argument may not contain any of the
special characters shown in this table.A Magick::TypeMetric struct. This structure has the following attributes. (The descriptions are taken from the Magick++ documentation and source code.)
Accessor | Description |
---|---|
ascent | Distance in pixels from the text baseline to the highest/upper grid coordinate used to place an outline point. Always a positive value. |
descent | Distance in pixels from the baseline to the lowest grid coordinate used to place an outline point. Always a negative value. |
width | Text width in pixels |
height | Text height in pixels |
max_advance | Maximum horizontal advance (advance from the beginning of a character to the beginning of the next character) in pixels. |
This example shows the details of a TypeMetric struct.
This example uses the width
and
height
values returned by
get_multiline_type_metrics
to draw a border around
the text lines.
GetTypeMetrics, GetMultilineTypeMetrics (available in ImageMagick 6.1.5)
draw.inspect -> draw
Displays the list of primitives that have been added to the
draw
object. This is very handy for debugging.
draw.inspect » "stroke red fill transparent rectangle 20,20 380,180 line 200,20 200,180 line 20,100 380,100 stroke transparent fill black"
draw.affine(sx, rx, ry, sy, tx, ty) -> draw
Transforms the coordinate system by a 3x3 transformation matrix. See Coordinate system transformations in the Scalable Vector Graphics (SVG) 1.1 Specification.
self
This example changes the default coordinate system to the standard Cartesian coordinate system: the x-axis points rightward and the y-axis points up.
gc.affine(1, 0, 0, -1, 0, max_y)
draw.arc(start_x, start_y, end_x, end_y, start_degrees, end_degrees) -> draw
Draws an arc within a rectangle.
self
gc.arc(40, 50, 250, 180, 0, 270)
The arc
method is broken in all releases of
ImageMagick prior to 6.0.0, and all releases of GraphicsMagick
prior to 1.1. If you see a diagonal line here instead of an
arc, it's broken. Either upgrade to the newest version or use
one of the other methods listed above.
draw.bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2...) -> draw
Draw a cubic Bezier curve.
The arguments are pairs of points. At least 4 pairs must be specified. Each point xn, yn on the curve is associated with a control point cxn, cyn. The first point, x1, y1, is the starting point. The last point, xn, yn, is the ending point. Other point/control point pairs specify intermediate points on a polybezier curve.
self
The following examples are taken from the Paths section of the Scalable Vector Graphics (SVG) 1.1 Specification.
gc.bezier(20,120, 20,20, 320,20, 320,120)
gc.bezier(25,125, 100,25, 400,25, 325,125)
gc.bezier(100,150, 25,50, 475,50, 400,150)
gc.bezier(20,180, 20,30, 320,330, 320,180)
gc.bezier(20,120, 95,20, 245,20, 320,120)
gc.bezier(50,150, 50,50, 200,50, 200,150, 200,250,
350,250, 350,150)
draw.circle(origin_x, origin_y, perim_x, perim_y) -> draw
self
Draw a circle.
self
gc.circle(125,125, 25,125)
draw.clip_path(pathname) -> draw
Draws the clip path on the image mask. The clip path defines a clipping area, where only the contained area will be drawn upon. Areas outside of the clipping area are masked.
Before using a clip-path, you must create it using the define_clip_path method.
The name of the clip path.
self
In this example, the picture of the girl is drawn onto the canvas using a star-shaped clipping path.
draw.clip_rule("evenodd"
or
"nonzero"
) -> draw
Specify how to determine if a point on the image is inside
clipping region. See
the 'fill-rule' property
in the
Scalable Vector Graphics (SVG) 1.1 Specification
for a complete description and examples.
Either "evenodd" or "nonzero".
self
draw.clip_units("userSpace"
or "userSpaceOnUse"
or
"objectSpace"
) -> draw
Defines the coordinate space within the clipping region. See Establishing a New Clipping Path in the Scalable Vector Graphics (SVG) 1.1 Specification for a complete description and examples.
Either "userSpace", "userSpaceOnUse", or "objectSpace".
self
draw.color(x, y, aPaintMethod) -> draw
Set color in image according to the specified PaintMethod constant.
A PaintMethod constant.
self
draw.color(x, y, Magick::ReplaceMethod)
draw.composite(x, y, width, height,
composite_image, op=Magick::OverCompositeOp
) ->
draw
Composite composite_image with the receiver image.
self
draw.decorate(aDecorationType) -> draw
Specify text decoration.
A DecorationType constant.
self
draw.decorate(Magick::LineThroughDecoration)
draw.define_clip_path(aString) { block } -> draw
Define a clip-path. Within block,
call other drawing primitive methods (rectangle
,
polygon
, text
, etc.) to define the
clip-path. The union of all the primitives (excluding the
effects of rendering methods such as stroke_width
,
etc.) is the clip-path. After the clip-path is invoked by the
clip-path method, anything drawn on
the image inside the clip-path will appear. Anything drawn
outside the clip-path will be hidden. (See clip_rule for a definition of how
×Magick decides what is "inside" and what is "outside" of
the clip-path.)
The name of the clip-path. This is the name that is
specified in the clip_path
method.
self
draw.ellipse(origin_x, origin_y, width, height, arc_start, arc_end) -> draw
Draw an ellipse.
self
draw.ellipse(180,125, 150,75, 0, 270)
draw.encoding(aString) -> draw
Specify the font encoding.
See the ×Magick documentation for the
-encoding
option to the mogrify
command
self
draw.fill(aString) -> draw
Specify the color or pattern with which graphical elements are filled.
A color name or pattern name.
self
draw.fill('red')
fill_color
, fill_pattern
draw.fill_opacity(aFloat or aString) -> draw
Specify the fill opacity.
A number between 0 and 1, inclusive, or a percentage represented as a string, i.e. '30%'. The argument 0.3 is the same as '30%'.
self
See the example for opacity.
draw.fill_rule("evenodd"
or
"nonzero"
) -> draw
Specify how to determine if a point on the image is inside a
shape. See
the 'fill-rule' property
in the
Scalable Vector Graphics (SVG) 1.1 Specification
for a complete description and examples.
Either "evenodd" or "nonzero".
self
draw.font(aString) -> draw
Specify the font to draw text with.
The font name or filename. You can tag a font to specify whether it is a Postscript, Truetype, or OPTION1 font. For example, Arial.ttf is a Truetype font, ps:helvetica is Postscript, and x:fixed is OPTION1.
The font name can be a complete filename such as
"/mnt/windows/windows/fonts/Arial.ttf"
. The font
name can also be a fully qualified X font name such as
"-urw-times-medium-i-normal--0-0-0-0-p-0-iso8859-13"
.
self
draw.font_family(aString) -> draw
Specify the font family.
A font family name such as "arial" or "helvetica".
self
See the example for text.
draw.font_stretch(aStretchType) -> draw
Specify the spacing between text characters.
A StretchType constant.
self
draw.font_style(aStyleType) -> draw
Specify the font style, i.e. italic, oblique, or normal.
A StyleType constant.
self
draw.font_weight(aWeightType) -> draw
Specify the font weight. For example, "bold" or "normal".
A WeightType constant, or one of the numbers 100, 200, 300, 400, 500, 600, 700, 800, or 900.
self
draw.gravity(aGravityType) -> draw
Specify how the text is positioned. The default is
Magick::NorthWestGravity
.
A GravityType constant.
self
draw.line(here_x, here_y, there_x, there_y) -> draw
Draw a line from here to there.
self
gc.line(50,50, 50,200)
gc.line(50,200, 200,200)
gc.line(200,200, 50,50)
draw.matte(x,y, aPaintMethod) -> draw
Make the image transparent according to the specified PaintMethod constant.
self
draw.opacity(opacity) -> draw
Specify the fill and stroke opacities.
Either a number between 0 and 1, or a percentage represented as a string, i.e. "25%". The string argument "25%" is the same as the numeric argument 0.25. Both the fill and stroke opacities are set to the specified value.
self
This example demonstrates 4 levels of opacity.
draw.path(aString) -> draw
Draw using SVG-compatible path drawing commands. See "Paths" in the Scalable Vector Graphics (SVG) 1.1 Specification.
A string containing SVG moveto, line, curve, arc, or closepath instructions. The string is equivalent to the d attribute in an SVG 'path' element.
self
These examples are all taken from the SVG path examples.
gc.path('M110,100 h-75 a75,75 0 1,0 75,-75 z')
gc.path('M97.5,87.5 v-75 a75,75 0 0,0 -75,75 z')
gc.path("M20,150 Q120,25 220,150 T420,150")
gc.path("M20,120 C20,20 170,20 170,120 S320,220
320,120")
draw.pattern(name, x, y, width, height) { pattern primitives } -> draw
Define a pattern that can be used as the fill or stroke pattern.
self
draw.point(x, y) -> draw
draw.pointsize(anInteger) -> draw
Set the font size in points. The default is 12.
See the example for text.
self
font_size
draw.polygon(x1, y1,...,xN, yN) -> draw
Draw a polygon.
The arguments are a sequence of 2 or more points. If the last point is not the same as the first, the polygon is closed by drawing a line from the last point to the first.
self
This example is taken from the The 'polygon' element in the Scalable Vector Graphics (SVG) 1.1 Specification
draw.polyline(x1, y1,...,xN, yN) -> draw
Draw a polyline. Unlike a polygon, a polyline is not automatically closed.
A sequence of 2 or more points.
self
This example is taken from the The 'polyline' element in the Scalable Vector Graphics (SVG) 1.1 Specification
draw.pop -> draw
Restore the graphics context to the state it was in when push was called last.
self
draw.push -> draw
Save the current state of the graphics context, including the attribute settings and the current set of primitives. Use the pop primitive to restore the state.
Note: The push
and pop
methods are probably not as useful in RMagick as they are in C
language ×Magick programs, since it is easy to create
separate draw objects, each with its own set of properties and
primitives.
self
draw.rectangle(x1, y1, x2, y2) -> draw
Draw a rectangle.
self
gc.rectangle(20,20, 280,180)
draw.rotate(degrees) -> draw
Specify a rotation transformation to the coordinate space. The default is 0.
The amount of rotation, in degrees. The angle of rotation is clockwise, so 90° is South.
self
gc.rotate(45)
draw.roundrectangle(x1, y1, x2, y2, cw, ch) -> draw
Draw a rectangle with rounded corners.
self
gc.roundrectangle(20,20, 280,180, 8, 8)
draw.scale(sx, sy) -> draw
Define a scale transformation to the coordinate space. The default scale is (1.0, 1.0).
self
draw.skewx(aFloat) -> draw
Define a skew tranformation along the x-axis.
The amount of skew, in degrees.
self
gc.skewx(30)
draw.skewy(aFloat) -> draw
Define a skew tranformation along the y-axis.
The amount of skew, in degrees.
self
gc.skewy(30)
draw.stroke(aString) -> draw
Specify the stroke color or pattern.
A color name or pattern name.
self
stroke_color
,
stroke_pattern
draw.stroke_antialias(true
or
false
) -> draw
Specify if the stroke should be antialiased. The default is
true
.
draw.stroke_dasharray(x1,...,xn) -> draw
Describe a pattern of dashes to be used when stroking paths. The arguments are a list of pixel widths of alternating dashes and gaps.
The first argument is the width of the first dash. The second is the width of the gap following the first dash. The third argument is another dash width, followed by another gap width, etc. If you specify an odd number of arguments, the arguments are repeated to produce an even number. All arguments must be > 0.
To produce a solid stroke, specify no arguments, i.e.
stroke_dasharray()
self
draw.stroke_dashoffset(anInteger) -> draw
Specify the initial distance into the dash pattern.
The initial distance into the dash pattern. The units are pixels.
self
draw.stroke_linecap(aString) -> draw
Specify how the line ends should be drawn.
One of "butt", "round", or "square".
self
The following example is from the Stroke Properties section of the Scalable Vector Graphics (SVG) 1.1 Specification.
draw.stroke_linejoin(aString) -> draw
Specify how corners are drawn.
One of "miter", "round", or "bevel".
self
The following example is from the Stroke Properties section of the Scalable Vector Graphics (SVG) 1.0 Specification.
draw.stroke_miterlimit(aFloat) -> draw
Specify a constraint on the length of the "miter" formed by two lines meeting at an angle. If the angle if very sharp, the miter could be very long relative to the line thickness. The miter limit is a limit on the ratio of the miter length to the line width.
A number >= 1. The limit on the ratio of the miter length to the line width.
self
draw.stroke_opacity(aFloat or aString) -> draw
Specify the stroke opacity.
A number between 0 and 1, inclusive, or a percentage represented as a string, i.e. '30%'. The argument 0.3 is the same as '30%'.
self
draw.stroke_opacity(0.4) draw.stroke_opacity('40%')
draw.stroke_width(anInteger) -> draw
draw.text(x,y, text) -> draw
Draw text at the location specified by (x,y). Use gravity to position text relative to (x, y). Specify the font appearance with the font, font_family, font_stretch, font_style, and font_weight methods. Specify the text attributes with the text_align, text_anchor, text_antialias, and text_undercolor methods.
You can embed information about the image by including special character sequences in the text string.
If the text string contains an embedded blank, quote, or apostrophe, surround the entire text string using a character that is not in the text. ×Magick uses these characters to delimit the string but will strip them off before drawing the text. You can use quotes, apostrophes, or braces ("{}"). You can also escape a blank, quote, or apostrophe by preceding it with a backslash ("\"). To include a backslash in the text, use two consecutive backslashes. To include a '%' in the text, use '%%'. See the examples below.
self
The text to the right of » is the text that will be drawn.
gc.text(10,10, '"Hello there!"') » Hello there! gc.text(10,10, "'What\'s up?'") » What's up? gc.text(10,10, %q/"What's up?"/) » What's up? gc.text(10,10, %q/{"What's up?"}/) » "What's up?"
draw.text_align(anAlignType) -> draw
Align text relative to the starting point.
An AlignType constant.
self
draw.text_anchor(anAnchorType) -> draw
Align text relative to the starting point. This is the SVG 1.1 equivalent to text_align.
One of the constants StartAnchor
,
MiddleAnchor
, or EndAnchor
.
self
draw.text_antialias(true
or
false
) -> draw
Specify if the text is to be antialiased.
Either true
or false
. The default
is true
.
self
The character on the left is not antialiased. The character on the right is antialiased.
draw.undercolor(aString) -> draw
The color to draw underneath text. The default is transparent.
A color name.
self
draw.translate(tx, ty) -> draw
Specify a translation operation on the coordinate space.
self
gc.translate(125, 125)
draw..affine = anAffineMatrix
The transformation matrix. The default is the null transformation.
An AffineMatrix.
self
draw..align = anAlignType
The text alignment. The default is
LeftAlign
.
A AlignType constant.
self
draw.decorate = aDecorationType
The text decoration. The default is
NoDecorationType
.
A DecorationType constant.
self
draw.density = aString
The text density in the x and y directions. The default is "72x72".
self
draw.encoding = aString
The text encoding.
See the ×Magick documentation for the
-encoding
option to the mogrify
command.
self
draw.fill = aString or aPixel
May be a color name or a Pixel object.
self
draw.font = aString
The font name. The default is "Helvetica". See font for more information about font names.
self
draw.font_family = aString
The font family name. For example, "arial" or "helvetica".
self
draw.font_stretch = aStretchType
draw.font_style = aStyleType
draw.font_weight = aWeightType
The font weight.
A WeightType constant or one of the numbers 100, 200, 300, 400, 500, 600, 700, 800, or 900.
self
draw.gravity = aGravityType
Specifies how to orient text with respect to the text's origin.
A GravityType constant.
self
draw.pointsize = anNumeric
The font size in points. The default is 12.
self
draw.rotation = aNumeric
Apply rotation to text. The default is no rotation.
The amount of rotation in degrees.
self
draw.stroke = aString or aPixel
The stroke color. This is the color used to outline the text. The default is "black".
self
draw.stroke = anInteger
The stroke width in pixels. The default is 1.
self
draw.text_antialias =
true
or false
Whether the text is antialiased or not. The default is
true
.
self
draw.undercolor = aString or aPixel
If set, causes the text to be drawn over a box of the specified color.
self