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 = Magick::Draw.new
draw.annotate(img, width, height, x, y, text) [ { additional parameters } ] -> self
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 0, 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') { |options| options.font_family = 'Helvetica' options.fill = 'white' options.stroke = 'transparent' options.pointsize = 32 options.font_weight = BoldWeight options.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.
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 -> other_draw
Same as dup except the frozen state of the original is propagated to the copy.
A new Draw object.
draw.composite(x, y, width, height, image, [operator]) -> self
This method draw the image using CompositeOperator.
self
draw.draw(img) -> self
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 -> other_draw
Duplicate a Draw
object.
An exact copy of the receiver, including the list of drawing primitives.
draw.get_multiline_type_metrics([image,] string) -> type_metrics
draw.get_type_metrics([image,] string) -> type_metrics
Returns information for a specific string if rendered on a image. Metrics are derived from either the
annotate attributes or from the image info attributes, but not from any drawing primitive methods.
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"), point size (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)/resolutionwhere 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 point size is only a first-order approximation of the font height (ascender + descender) in points. The relationship between the specified point size 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 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
draw.inspect -> string
Returns 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.primitive(primitive) -> self
Add a drawing primitive to the list of primitives in the Draw object.
self
draw.affine(sx, rx, ry, sy, tx, ty) -> self
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.alpha(x, y, method) -> self
Set alpha (make transparent) in image according to the specified colorization rule.
FillToBorderMethod
, assign the border color with the
Draw#border_color=
attribute before calling draw.
self
draw.alpha(x, y, FillToBorderMethod)
draw.arc(start_x, start_y, end_x, end_y, start_degrees, end_degrees) -> self
Draws an arc within a rectangle.
self
gc.arc(40, 50, 250, 180, 0, 270)
draw.bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2...) -> self
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) -> self
self
Draw a circle.
self
gc.circle(125,125, 25,125)
draw.clip_path(pathname) -> self
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. You must precede clip_path
with push and terminate the
primitives that draw within the clip path with pop. For example,
gc.push gc.clip_path('mypath') # drawing primitives within the clip path gc.pop gc.draw
Also see the example below.
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"
) -> self
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"
) -> self
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, paint_method) -> self
Set color in image according to the specified PaintMethod constant.
FillToBorderMethod
, assign the border color with the
Draw#border_color=
attribute before calling draw.
self
draw.border_color = 'black' draw.color(x, y, FillToBorderMethod)
draw.composite(x, y, width,
height, composite_image, op=OverCompositeOp
) ->
self
Composite composite_image with the receiver image.
self
draw.decorate(decoration) -> self
Specify text decoration.
A DecorationType constant.
self
draw.decorate(LineThroughDecoration)
draw.define_clip_path(string) { block } -> self
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 ImageMagick 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) -> self
Draw an ellipse.
self
draw.ellipse(180,125, 150,75, 0, 270)
draw.encoding(string) -> self
Specify the font encoding.
See the ImageMagick documentation for the -encoding
option to the mogrify
command
self
draw.fill(string) -> self
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(float or string) -> self
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"
) -> self
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(string) -> self
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(string) -> self
Specify the font family.
A font family name such as "arial" or "helvetica".
self
See the example for text.
draw.font_stretch(stretch) -> self
Specify the spacing between text characters.
A StretchType constant.
self
draw.font_style(style) -> self
Specify the font style, i.e. italic, oblique, or normal.
A StyleType constant.
self
draw.font_weight(weight) -> self
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(gravity) -> self
Specify how the text is positioned. The default is
NorthWestGravity
.
A GravityType constant.
self
draw.interline_spacing(space) -> self
Modify the spacing between lines when text has multiple lines.
A numeric value. If positive, inserts additional space between lines. If negative, removes space between lines. The amount of space inserted or removed depends on the font.
self
draw.interword_spacing(space) -> self
Modify the spacing between words in text.
A numeric value. If positive, inserts additional space between words. If negative, removes space between words. The amount of space inserted or removed depends on the font.
self
draw.kerning(space) -> self
Modify the spacing between letters in text.
A numeric value. If positive, inserts additional space between letters. If negative, removes space between letters. The amount of space inserted or removed depends on the font but is usually measured in pixels. That is, the following call adds about 5 pixels between each letter.
gc.kerning(5)
self
draw.line(here_x, here_y, there_x, there_y) -> self
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, paint_method) -> self
Make the image transparent according to the specified PaintMethod constant.
Draw#border_color=
attribute before calling draw.
self
draw.opacity(opacity) -> self
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(string) -> self
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 } -> self
Define a pattern that can be used as the fill or stroke pattern.
self
draw.point(x, y) -> self
draw.pointsize(integer) -> self
Set the font size in points. The default is 12.
See the example for text.
self
font_size
draw.polygon(x1, y1,...,xN, yN) -> self
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) -> self
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 -> self
Restore the graphics context to the state it was in when push was called last.
self
draw.push -> self
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 ImageMagick 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) -> self
Draw a rectangle.
self
gc.rectangle(20,20, 280,180)
draw.rotate(degrees) -> self
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) -> self
Draw a rectangle with rounded corners.
self
gc.roundrectangle(20,20, 280,180, 8, 8)
draw.scale(sx, sy) -> self
Define a scale transformation to the coordinate space. The default scale is (1.0, 1.0).
self
draw.skewx(float) -> self
Define a skew transformation along the x-axis.
The amount of skew, in degrees.
self
gc.skewx(30)
draw.skewy(float) -> self
Define a skew transformation along the y-axis.
The amount of skew, in degrees.
self
gc.skewy(30)
draw.stroke(string) -> self
Specify the stroke color or pattern.
A color name or pattern name.
self
stroke_color
,
stroke_pattern
draw.stroke_antialias(true
or false
) -> self
Specify if the stroke should be antialiased. The default is
true
.
draw.stroke_dasharray(x1,...,xn) -> self
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(integer) -> self
Specify the initial distance into the dash pattern.
The initial distance into the dash pattern. The units are pixels.
self
draw.stroke_linecap(string) -> self
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(string) -> self
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(float) -> self
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(float or string) -> self
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(integer) -> self
draw.text(x,y, text) -> self
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.
Generally it is a good idea to surround the text string with quotes (""), apostrophes (''), or braces ({}). If the text string starts with a digit or contains an embedded blank, quote, or apostrophe, you must do this. ImageMagick removes these characters before drawing the text. 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(alignment) -> self
Align text relative to the starting point.
An AlignType constant.
self
draw.text_anchor(anchor) -> self
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
) -> self
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.text_undercolor(string) -> self
The color to draw underneath text. The default is transparent.
A color name.
self
draw.translate(tx, ty) -> self
Specify a translation operation on the coordinate space.
self
gc.translate(125, 125)
draw.affine = matrix
The transformation matrix. The default is the null transformation.
An AffineMatrix.
draw.align = alignment
draw.decorate = decoration
The text decoration. The default is NoDecorationType
.
A DecorationType constant.
draw.density = string
The text density in the x and y directions. The default is "72x72".
draw.encoding = string
The text encoding.
See the ImageMagick documentation for the -encoding
option to the mogrify
command.
draw.fill = string or pixel
draw.fill_pattern = image
draw.font = string
The font name. The default is "Helvetica". See font for more information about font names.
draw.font_family = string
The font family name. For example, "arial" or "helvetica".
draw.font_stretch = stretch
draw.font_style = style
draw.font_weight = weight
The font weight.
A WeightType constant or one of the numbers 100, 200, 300, 400, 500, 600, 700, 800, or 900.
draw.gravity = gravity
Specifies how to orient text with respect to the text's origin.
A GravityType constant.
draw.interline_spacing = space
Modifies the space between lines.
A numeric value. May be positive or negative.
Available in ImageMagick 6.5.5-8.
draw.interword_spacing = space
Modifies the space between two words.
A numeric value. May be positive or negative.
Available in ImageMagick 6.4.8-0.
draw.kerning = space
Modifies the space between two letters.
A numeric value. May be positive or negative.
Available in ImageMagick 6.4.7-8.
draw.pointsize = integer
The font size in points. The default is 12.
draw.rotation = number
Apply rotation to text. The default is no rotation.
The amount of rotation in degrees.
draw.stroke = string or pixel
The stroke color. This is the color used to outline the text. The default is "black".
draw.stroke_pattern = image
draw.stroke = integer
The stroke width in pixels. The default is 1.
draw.text_antialias =
true
or false
Whether the text is antialiased or not. The default is
true
.
draw.tile = image
Tile image when filling a graphic primitive.
An image
draw.undercolor = string or pixel
If set, causes the text to be drawn over a box of the specified color.