Class Magick::Draw
In: lib/RMagick.rb
Parent: Object
Enum GeometryValue Stylable RVG\n[lib/rvg/clippath.rb\nlib/rvg/container.rb\nlib/rvg/deep_equal.rb\nlib/rvg/describable.rb\nlib/rvg/embellishable.rb\nlib/rvg/misc.rb\nlib/rvg/paint.rb\nlib/rvg/pathdata.rb\nlib/rvg/rvg.rb\nlib/rvg/stretchable.rb\nlib/rvg/stylable.rb\nlib/rvg/text.rb\nlib/rvg/transformable.rb\nlib/rvg/units.rb] Transformable Stretchable Embellishable Describable Duplicatable Comparable Image ImageList Array Geometry HatchFill Draw lib/RMagick.rb lib/rvg/misc.rb Application ObjectData Pre_ObjectData_Descriptor Envelope Post_ObjectData_Descriptor IPTC Magick dot/m_14_0.png

Methods

Constants

ALIGN_TYPE_NAMES = { LeftAlign.to_i => 'left', RightAlign.to_i => 'right', CenterAlign.to_i => 'center'   Thse hashes are used to map Magick constant values to the strings used in the primitives.
ANCHOR_TYPE_NAMES = { StartAnchor.to_i => 'start', MiddleAnchor.to_i => 'middle', EndAnchor.to_i => 'end'
DECORATION_TYPE_NAMES = { NoDecoration.to_i => 'none', UnderlineDecoration.to_i => 'underline', OverlineDecoration.to_i => 'overline', LineThroughDecoration.to_i => 'line-through'
FONT_WEIGHT_NAMES = { AnyWeight.to_i => 'all', NormalWeight.to_i => 'normal', BoldWeight.to_i => 'bold', BolderWeight.to_i => 'bolder', LighterWeight.to_i => 'lighter', }.freeze
GRAVITY_NAMES = { NorthWestGravity.to_i => 'northwest', NorthGravity.to_i => 'north', NorthEastGravity.to_i => 'northeast', WestGravity.to_i => 'west', CenterGravity.to_i => 'center', EastGravity.to_i => 'east', SouthWestGravity.to_i => 'southwest', SouthGravity.to_i => 'south', SouthEastGravity.to_i => 'southeast'
PAINT_METHOD_NAMES = { PointMethod.to_i => 'point', ReplaceMethod.to_i => 'replace', FloodfillMethod.to_i => 'floodfill', FillToBorderMethod.to_i => 'filltoborder', ResetMethod.to_i => 'reset'
STRETCH_TYPE_NAMES = { NormalStretch.to_i => 'normal', UltraCondensedStretch.to_i => 'ultra-condensed', ExtraCondensedStretch.to_i => 'extra-condensed', CondensedStretch.to_i => 'condensed', SemiCondensedStretch.to_i => 'semi-condensed', SemiExpandedStretch.to_i => 'semi-expanded', ExpandedStretch.to_i => 'expanded', ExtraExpandedStretch.to_i => 'extra-expanded', UltraExpandedStretch.to_i => 'ultra-expanded', AnyStretch.to_i => 'all'
STYLE_TYPE_NAMES = { NormalStyle.to_i => 'normal', ItalicStyle.to_i => 'italic', ObliqueStyle.to_i => 'oblique', AnyStyle.to_i => 'all'

Public Instance methods

Apply coordinate transformations to support scaling (s), rotation (r), and translation (t). Angles are specified in radians.

[Source]

     # File lib/RMagick.rb, line 180
180:     def affine(sx, rx, ry, sy, tx, ty)
181:         primitive "affine " + sprintf("%g,%g,%g,%g,%g,%g", sx, rx, ry, sy, tx, ty)
182:     end

Draw an arc.

[Source]

     # File lib/RMagick.rb, line 185
185:     def arc(startX, startY, endX, endY, startDegrees, endDegrees)
186:         primitive "arc " + sprintf("%g,%g %g,%g %g,%g",
187:                     startX, startY, endX, endY, startDegrees, endDegrees)
188:     end

Draw a bezier curve.

[Source]

     # File lib/RMagick.rb, line 191
191:     def bezier(*points)
192:         if points.length == 0
193:             Kernel.raise ArgumentError, "no points specified"
194:         elsif points.length % 2 != 0
195:             Kernel.raise ArgumentError, "odd number of arguments specified"
196:         end
197:         primitive "bezier " + points.join(',')
198:     end

Draw a circle

[Source]

     # File lib/RMagick.rb, line 201
201:     def circle(originX, originY, perimX, perimY)
202:         primitive "circle " + sprintf("%g,%g %g,%g", originX, originY, perimX, perimY)
203:     end

Invoke a clip-path defined by def_clip_path.

[Source]

     # File lib/RMagick.rb, line 206
206:     def clip_path(name)
207:         primitive "clip-path #{name}"
208:     end

Define the clipping rule.

[Source]

     # File lib/RMagick.rb, line 211
211:     def clip_rule(rule)
212:         if ( not ["evenodd", "nonzero"].include?(rule.downcase) )
213:             Kernel.raise ArgumentError, "Unknown clipping rule #{rule}"
214:         end
215:         primitive "clip-rule #{rule}"
216:     end

Define the clip units

[Source]

     # File lib/RMagick.rb, line 219
219:     def clip_units(unit)
220:         if ( not ["userspace", "userspaceonuse", "objectboundingbox"].include?(unit.downcase) )
221:             Kernel.raise ArgumentError, "Unknown clip unit #{unit}"
222:         end
223:         primitive "clip-units #{unit}"
224:     end

Set color in image according to specified colorization rule. Rule is one of point, replace, floodfill, filltoborder,reset

[Source]

     # File lib/RMagick.rb, line 228
228:     def color(x, y, method)
229:         if ( not PAINT_METHOD_NAMES.has_key?(method.to_i) )
230:             Kernel.raise ArgumentError, "Unknown PaintMethod: #{method}"
231:         end
232:         primitive "color #{x},#{y},#{PAINT_METHOD_NAMES[method.to_i]}"
233:     end

Specify EITHER the text decoration (none, underline, overline, line-through) OR the text solid background color (any color name or spec)

[Source]

     # File lib/RMagick.rb, line 237
237:     def decorate(decoration)
238:         if ( DECORATION_TYPE_NAMES.has_key?(decoration.to_i) )
239:             primitive "decorate #{DECORATION_TYPE_NAMES[decoration.to_i]}"
240:         else
241:             primitive "decorate #{enquote(decoration)}"
242:         end
243:     end

Define a clip-path. A clip-path is a sequence of primitives bracketed by the "push clip-path <name>" and "pop clip-path" primitives. Upon advice from the IM guys, we also bracket the clip-path primitives with "push(pop) defs" and "push (pop) graphic-context".

[Source]

     # File lib/RMagick.rb, line 250
250:     def define_clip_path(name)
251:         begin
252:             push('defs')
253:             push('clip-path ', name)
254:             push('graphic-context')
255:             yield
256:         ensure
257:             pop('graphic-context')
258:             pop('clip-path')
259:             pop('defs')
260:         end
261:     end

Draw an ellipse

[Source]

     # File lib/RMagick.rb, line 264
264:     def ellipse(originX, originY, width, height, arcStart, arcEnd)
265:         primitive "ellipse " + sprintf("%g,%g %g,%g %g,%g",
266:                         originX, originY, width, height, arcStart, arcEnd)
267:     end

Let anything through, but the only defined argument is "UTF-8". All others are apparently ignored.

[Source]

     # File lib/RMagick.rb, line 271
271:     def encoding(encoding)
272:         primitive "encoding #{encoding}"
273:     end

Specify object fill, a color name or pattern name

[Source]

     # File lib/RMagick.rb, line 276
276:     def fill(colorspec)
277:         primitive "fill #{enquote(colorspec)}"
278:     end
fill_color(colorspec)

Alias for fill

Specify fill opacity (use "xx%" to indicate percentage)

[Source]

     # File lib/RMagick.rb, line 283
283:     def fill_opacity(opacity)
284:         primitive "fill-opacity #{opacity}"
285:     end
fill_pattern(colorspec)

Alias for fill

[Source]

     # File lib/RMagick.rb, line 287
287:     def fill_rule(rule)
288:         if ( not ["evenodd", "nonzero"].include?(rule.downcase) )
289:             Kernel.raise ArgumentError, "Unknown fill rule #{rule}"
290:         end
291:         primitive "fill-rule #{rule}"
292:     end

Specify text drawing font

[Source]

     # File lib/RMagick.rb, line 295
295:     def font(name)
296:         primitive "font #{name}"
297:     end

[Source]

     # File lib/RMagick.rb, line 299
299:     def font_family(name)
300:         primitive "font-family \'#{name}\'"
301:     end
font_size(points)

Alias for pointsize

[Source]

     # File lib/RMagick.rb, line 303
303:     def font_stretch(stretch)
304:         if ( not STRETCH_TYPE_NAMES.has_key?(stretch.to_i) )
305:             Kernel.raise ArgumentError, "Unknown stretch type"
306:         end
307:         primitive "font-stretch #{STRETCH_TYPE_NAMES[stretch.to_i]}"
308:     end

[Source]

     # File lib/RMagick.rb, line 310
310:     def font_style(style)
311:         if ( not STYLE_TYPE_NAMES.has_key?(style.to_i) )
312:             Kernel.raise ArgumentError, "Unknown style type"
313:         end
314:         primitive "font-style #{STYLE_TYPE_NAMES[style.to_i]}"
315:     end

The font weight argument can be either a font weight constant or [100,200,…,900]

[Source]

     # File lib/RMagick.rb, line 319
319:     def font_weight(weight)
320:         if ( FONT_WEIGHT_NAMES.has_key?(weight.to_i) )
321:             primitive "font-weight #{FONT_WEIGHT_NAMES[weight.to_i]}"
322:         else
323:             primitive "font-weight #{weight}"
324:         end
325:     end

Specify the text positioning gravity, one of: NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast

[Source]

     # File lib/RMagick.rb, line 329
329:     def gravity(grav)
330:         if ( not GRAVITY_NAMES.has_key?(grav.to_i) )
331:             Kernel.raise ArgumentError, "Unknown text positioning gravity"
332:         end
333:         primitive "gravity #{GRAVITY_NAMES[grav.to_i]}"
334:     end

Draw a line

[Source]

     # File lib/RMagick.rb, line 337
337:     def line(startX, startY, endX, endY)
338:         primitive "line " + sprintf("%g,%g %g,%g", startX, startY, endX, endY)
339:     end

Set matte (make transparent) in image according to the specified colorization rule

[Source]

     # File lib/RMagick.rb, line 343
343:     def matte(x, y, method)
344:         if ( not PAINT_METHOD_NAMES.has_key?(method.to_i) )
345:             Kernel.raise ArgumentError, "Unknown paint method"
346:         end
347:         primitive "matte #{x},#{y} #{PAINT_METHOD_NAMES[method.to_i]}"
348:     end

Specify drawing fill and stroke opacities. If the value is a string ending with a %, the number will be multiplied by 0.01.

[Source]

     # File lib/RMagick.rb, line 352
352:     def opacity(opacity)
353:         if (Numeric === opacity)
354:             if (opacity < 0 || opacity > 1.0)
355:                 Kernel.raise ArgumentError, "opacity must be >= 0 and <= 1.0"
356:             end
357:         end
358:         primitive "opacity #{opacity}"
359:     end

Draw using SVG-compatible path drawing commands. Note that the primitive requires that the commands be surrounded by quotes or apostrophes. Here we simply use apostrophes.

[Source]

     # File lib/RMagick.rb, line 364
364:     def path(cmds)
365:         primitive "path '" + cmds + "'"
366:     end

Define a pattern. In the block, call primitive methods to draw the pattern. Reference the pattern by using its name as the argument to the ‘fill’ or ‘stroke’ methods

[Source]

     # File lib/RMagick.rb, line 371
371:     def pattern(name, x, y, width, height)
372:         begin
373:             push('defs')
374:             push("pattern #{name} #{x} #{y} #{width} #{height}")
375:             push('graphic-context')
376:             yield
377:         ensure
378:             pop('graphic-context')
379:             pop('pattern')
380:             pop('defs')
381:         end
382:     end

Set point to fill color.

[Source]

     # File lib/RMagick.rb, line 385
385:     def point(x, y)
386:         primitive "point #{x},#{y}"
387:     end

Specify the font size in points. Yes, the primitive is "font-size" but in other places this value is called the "pointsize". Give it both names.

[Source]

     # File lib/RMagick.rb, line 391
391:     def pointsize(points)
392:         primitive "font-size #{points}"
393:     end

Draw a polygon

[Source]

     # File lib/RMagick.rb, line 397
397:     def polygon(*points)
398:         if points.length == 0
399:             Kernel.raise ArgumentError, "no points specified"
400:         elsif points.length % 2 != 0
401:             Kernel.raise ArgumentError, "odd number of points specified"
402:         end
403:         primitive "polygon " + points.join(',')
404:     end

Draw a polyline

[Source]

     # File lib/RMagick.rb, line 407
407:     def polyline(*points)
408:         if points.length == 0
409:             Kernel.raise ArgumentError, "no points specified"
410:         elsif points.length % 2 != 0
411:             Kernel.raise ArgumentError, "odd number of points specified"
412:         end
413:         primitive "polyline " + points.join(',')
414:     end

Return to the previously-saved set of whatever pop(‘graphic-context’) (the default if no arguments) pop(‘defs’) pop(‘gradient’) pop(‘pattern’)

[Source]

     # File lib/RMagick.rb, line 422
422:     def pop(*what)
423:         if what.length == 0
424:             primitive "pop graphic-context"
425:         else
426:             # to_s allows a Symbol to be used instead of a String
427:             primitive "pop " + what.to_s
428:         end
429:     end

Push the current set of drawing options. Also you can use push(‘graphic-context’) (the default if no arguments) push(‘defs’) push(‘gradient’) push(‘pattern’)

[Source]

     # File lib/RMagick.rb, line 436
436:     def push(*what)
437:         if what.length == 0
438:             primitive "push graphic-context"
439:         else
440:             # to_s allows a Symbol to be used instead of a String
441:             primitive "push " + what.to_s
442:         end
443:     end

Draw a rectangle

[Source]

     # File lib/RMagick.rb, line 446
446:     def rectangle(upper_left_x, upper_left_y, lower_right_x, lower_right_y)
447:         primitive "rectangle " + sprintf("%g,%g %g,%g",
448:                 upper_left_x, upper_left_y, lower_right_x, lower_right_y)
449:     end

Specify coordinate space rotation. "angle" is measured in degrees

[Source]

     # File lib/RMagick.rb, line 452
452:     def rotate(angle)
453:         primitive "rotate #{angle}"
454:     end

Draw a rectangle with rounded corners

[Source]

     # File lib/RMagick.rb, line 457
457:     def roundrectangle(center_x, center_y, width, height, corner_width, corner_height)
458:         primitive "roundrectangle " + sprintf("%g,%g,%g,%g,%g,%g",
459:             center_x, center_y, width, height, corner_width, corner_height)
460:     end

Specify scaling to be applied to coordinate space on subsequent drawing commands.

[Source]

     # File lib/RMagick.rb, line 463
463:     def scale(x, y)
464:         primitive "scale #{x},#{y}"
465:     end

[Source]

     # File lib/RMagick.rb, line 467
467:     def skewx(angle)
468:         primitive "skewX #{angle}"
469:     end

[Source]

     # File lib/RMagick.rb, line 471
471:     def skewy(angle)
472:         primitive "skewY #{angle}"
473:     end

Specify the object stroke, a color name or pattern name.

[Source]

     # File lib/RMagick.rb, line 476
476:     def stroke(colorspec)
477:         primitive "stroke #{enquote(colorspec)}"
478:     end

Specify if stroke should be antialiased or not

[Source]

     # File lib/RMagick.rb, line 483
483:     def stroke_antialias(bool)
484:         bool = bool ? '1' : '0'
485:         primitive "stroke-antialias #{bool}"
486:     end
stroke_color(colorspec)

Alias for stroke

Specify a stroke dash pattern

[Source]

     # File lib/RMagick.rb, line 489
489:     def stroke_dasharray(*list)
490:         if list.length == 0
491:             primitive "stroke-dasharray none"
492:         else
493:             list.each { |x|
494:                 if x <= 0 then
495:                     Kernel.raise ArgumentError, "dash array elements must be > 0 (#{x} given)"
496:                 end
497:             }
498:             primitive "stroke-dasharray #{list.join(',')}"
499:         end
500:     end

Specify the initial offset in the dash pattern

[Source]

     # File lib/RMagick.rb, line 503
503:     def stroke_dashoffset(value=0)
504:         primitive "stroke-dashoffset #{value}"
505:     end

[Source]

     # File lib/RMagick.rb, line 507
507:     def stroke_linecap(value)
508:         if ( not ["butt", "round", "square"].include?(value.downcase) )
509:             Kernel.raise ArgumentError, "Unknown linecap type: #{value}"
510:         end
511:         primitive "stroke-linecap #{value}"
512:     end

[Source]

     # File lib/RMagick.rb, line 514
514:     def stroke_linejoin(value)
515:         if ( not ["round", "miter", "bevel"].include?(value.downcase) )
516:             Kernel.raise ArgumentError, "Unknown linejoin type: #{value}"
517:         end
518:         primitive "stroke-linejoin #{value}"
519:     end

[Source]

     # File lib/RMagick.rb, line 521
521:     def stroke_miterlimit(value)
522:         if (value < 1)
523:             Kernel.raise ArgumentError, "miterlimit must be >= 1"
524:         end
525:         primitive "stroke-miterlimit #{value}"
526:     end

Specify opacity of stroke drawing color

 (use "xx%" to indicate percentage)

[Source]

     # File lib/RMagick.rb, line 530
530:     def stroke_opacity(value)
531:         primitive "stroke-opacity #{value}"
532:     end
stroke_pattern(colorspec)

Alias for stroke

Specify stroke (outline) width in pixels.

[Source]

     # File lib/RMagick.rb, line 535
535:     def stroke_width(pixels)
536:         primitive "stroke-width #{pixels}"
537:     end

Draw text at position x,y. Add quotes to text that is not already quoted.

[Source]

     # File lib/RMagick.rb, line 540
540:     def text(x, y, text)
541:         if text.to_s.empty?
542:             Kernel.raise ArgumentError, "missing text argument"
543:         end
544:         if text.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(text)
545:             ; # text already quoted
546:         elsif !text['\'']
547:             text = '\''+text+'\''
548:         elsif !text['"']
549:             text = '"'+text+'"'
550:         elsif !(text['{'] || text['}'])
551:             text = '{'+text+'}'
552:         else
553:             # escape existing braces, surround with braces
554:             text = '{' +  text.gsub(/[}]/) { |b| '\\' + b } + '}'
555:         end
556:         primitive "text #{x},#{y} #{text}"
557:     end

Specify text alignment relative to a given point

[Source]

     # File lib/RMagick.rb, line 560
560:     def text_align(alignment)
561:         if ( not ALIGN_TYPE_NAMES.has_key?(alignment.to_i) )
562:             Kernel.raise ArgumentError, "Unknown alignment constant: #{alignment}"
563:         end
564:         primitive "text-align #{ALIGN_TYPE_NAMES[alignment.to_i]}"
565:     end

SVG-compatible version of text_align

[Source]

     # File lib/RMagick.rb, line 568
568:     def text_anchor(anchor)
569:         if ( not ANCHOR_TYPE_NAMES.has_key?(anchor.to_i) )
570:             Kernel.raise ArgumentError, "Unknown anchor constant: #{anchor}"
571:         end
572:         primitive "text-anchor #{ANCHOR_TYPE_NAMES[anchor.to_i]}"
573:     end

Specify if rendered text is to be antialiased.

[Source]

     # File lib/RMagick.rb, line 576
576:     def text_antialias(boolean)
577:         boolean = boolean ? '1' : '0'
578:         primitive "text-antialias #{boolean}"
579:     end

Specify color underneath text

[Source]

     # File lib/RMagick.rb, line 582
582:     def text_undercolor(color)
583:         primitive "text-undercolor #{enquote(color)}"
584:     end

Specify center of coordinate space to use for subsequent drawing commands.

[Source]

     # File lib/RMagick.rb, line 588
588:     def translate(x, y)
589:         primitive "translate #{x},#{y}"
590:     end

Private Instance methods

[Source]

     # File lib/RMagick.rb, line 168
168:     def enquote(str)
169:         if str.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(str)
170:             return str
171:         else
172:             return '"' + str + '"'
173:         end
174:     end

[Validate]