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 Enumerable Geometry OptionalMethodArguments HatchFill Draw lib/RMagick.rb lib/rvg/misc.rb ObjectData Application 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 218
218:     def affine(sx, rx, ry, sy, tx, ty)
219:         primitive "affine " + sprintf("%g,%g,%g,%g,%g,%g", sx, rx, ry, sy, tx, ty)
220:     end

Draw an arc.

[Source]

     # File lib/RMagick.rb, line 223
223:     def arc(startX, startY, endX, endY, startDegrees, endDegrees)
224:         primitive "arc " + sprintf("%g,%g %g,%g %g,%g",
225:                     startX, startY, endX, endY, startDegrees, endDegrees)
226:     end

Draw a bezier curve.

[Source]

     # File lib/RMagick.rb, line 229
229:     def bezier(*points)
230:         if points.length == 0
231:             Kernel.raise ArgumentError, "no points specified"
232:         elsif points.length % 2 != 0
233:             Kernel.raise ArgumentError, "odd number of arguments specified"
234:         end
235:         primitive "bezier " + points.join(',')
236:     end

Draw a circle

[Source]

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

Invoke a clip-path defined by def_clip_path.

[Source]

     # File lib/RMagick.rb, line 244
244:     def clip_path(name)
245:         primitive "clip-path #{name}"
246:     end

Define the clipping rule.

[Source]

     # File lib/RMagick.rb, line 249
249:     def clip_rule(rule)
250:         if ( not ["evenodd", "nonzero"].include?(rule.downcase) )
251:             Kernel.raise ArgumentError, "Unknown clipping rule #{rule}"
252:         end
253:         primitive "clip-rule #{rule}"
254:     end

Define the clip units

[Source]

     # File lib/RMagick.rb, line 257
257:     def clip_units(unit)
258:         if ( not ["userspace", "userspaceonuse", "objectboundingbox"].include?(unit.downcase) )
259:             Kernel.raise ArgumentError, "Unknown clip unit #{unit}"
260:         end
261:         primitive "clip-units #{unit}"
262:     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 266
266:     def color(x, y, method)
267:         if ( not PAINT_METHOD_NAMES.has_key?(method.to_i) )
268:             Kernel.raise ArgumentError, "Unknown PaintMethod: #{method}"
269:         end
270:         primitive "color #{x},#{y},#{PAINT_METHOD_NAMES[method.to_i]}"
271:     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 275
275:     def decorate(decoration)
276:         if ( DECORATION_TYPE_NAMES.has_key?(decoration.to_i) )
277:             primitive "decorate #{DECORATION_TYPE_NAMES[decoration.to_i]}"
278:         else
279:             primitive "decorate #{enquote(decoration)}"
280:         end
281:     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 288
288:     def define_clip_path(name)
289:         begin
290:             push('defs')
291:             push('clip-path', name)
292:             push('graphic-context')
293:             yield
294:         ensure
295:             pop('graphic-context')
296:             pop('clip-path')
297:             pop('defs')
298:         end
299:     end

Draw an ellipse

[Source]

     # File lib/RMagick.rb, line 302
302:     def ellipse(originX, originY, width, height, arcStart, arcEnd)
303:         primitive "ellipse " + sprintf("%g,%g %g,%g %g,%g",
304:                         originX, originY, width, height, arcStart, arcEnd)
305:     end

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

[Source]

     # File lib/RMagick.rb, line 309
309:     def encoding(encoding)
310:         primitive "encoding #{encoding}"
311:     end

Specify object fill, a color name or pattern name

[Source]

     # File lib/RMagick.rb, line 314
314:     def fill(colorspec)
315:         primitive "fill #{enquote(colorspec)}"
316:     end
fill_color(colorspec)

Alias for fill

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

[Source]

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

Alias for fill

[Source]

     # File lib/RMagick.rb, line 325
325:     def fill_rule(rule)
326:         if ( not ["evenodd", "nonzero"].include?(rule.downcase) )
327:             Kernel.raise ArgumentError, "Unknown fill rule #{rule}"
328:         end
329:         primitive "fill-rule #{rule}"
330:     end

Specify text drawing font

[Source]

     # File lib/RMagick.rb, line 333
333:     def font(name)
334:         primitive "font #{name}"
335:     end

[Source]

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

Alias for pointsize

[Source]

     # File lib/RMagick.rb, line 341
341:     def font_stretch(stretch)
342:         if ( not STRETCH_TYPE_NAMES.has_key?(stretch.to_i) )
343:             Kernel.raise ArgumentError, "Unknown stretch type"
344:         end
345:         primitive "font-stretch #{STRETCH_TYPE_NAMES[stretch.to_i]}"
346:     end

[Source]

     # File lib/RMagick.rb, line 348
348:     def font_style(style)
349:         if ( not STYLE_TYPE_NAMES.has_key?(style.to_i) )
350:             Kernel.raise ArgumentError, "Unknown style type"
351:         end
352:         primitive "font-style #{STYLE_TYPE_NAMES[style.to_i]}"
353:     end

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

[Source]

     # File lib/RMagick.rb, line 357
357:     def font_weight(weight)
358:         if ( FONT_WEIGHT_NAMES.has_key?(weight.to_i) )
359:             primitive "font-weight #{FONT_WEIGHT_NAMES[weight.to_i]}"
360:         else
361:             primitive "font-weight #{weight}"
362:         end
363:     end

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

[Source]

     # File lib/RMagick.rb, line 367
367:     def gravity(grav)
368:         if ( not GRAVITY_NAMES.has_key?(grav.to_i) )
369:             Kernel.raise ArgumentError, "Unknown text positioning gravity"
370:         end
371:         primitive "gravity #{GRAVITY_NAMES[grav.to_i]}"
372:     end

IM 6.4.8-3 and later

[Source]

     # File lib/RMagick.rb, line 375
375:     def interword_spacing(space)
376:         begin
377:             Float(space)
378:         rescue ArgumentError
379:             Kernel.raise ArgumentError, "invalid value for interword_spacing"
380:         rescue TypeError
381:             Kernel.raise TypeError, "can't convert #{space.class} into Float"
382:         end
383:         primitive "interword-spacing #{space}"
384:     end

IM 6.4.8-3 and later

[Source]

     # File lib/RMagick.rb, line 387
387:     def kerning(space)
388:         begin
389:             Float(space)
390:         rescue ArgumentError
391:             Kernel.raise ArgumentError, "invalid value for kerning"
392:         rescue TypeError
393:             Kernel.raise TypeError, "can't convert #{space.class} into Float"
394:         end
395:         primitive "kerning #{space}"
396:     end

Draw a line

[Source]

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

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

[Source]

     # File lib/RMagick.rb, line 405
405:     def matte(x, y, method)
406:         if ( not PAINT_METHOD_NAMES.has_key?(method.to_i) )
407:             Kernel.raise ArgumentError, "Unknown paint method"
408:         end
409:         primitive "matte #{x},#{y} #{PAINT_METHOD_NAMES[method.to_i]}"
410:     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 414
414:     def opacity(opacity)
415:         if (Numeric === opacity)
416:             if (opacity < 0 || opacity > 1.0)
417:                 Kernel.raise ArgumentError, "opacity must be >= 0 and <= 1.0"
418:             end
419:         end
420:         primitive "opacity #{opacity}"
421:     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 426
426:     def path(cmds)
427:         primitive "path '" + cmds + "'"
428:     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 433
433:     def pattern(name, x, y, width, height)
434:         begin
435:             push('defs')
436:             push("pattern #{name} #{x} #{y} #{width} #{height}")
437:             push('graphic-context')
438:             yield
439:         ensure
440:             pop('graphic-context')
441:             pop('pattern')
442:             pop('defs')
443:         end
444:     end

Set point to fill color.

[Source]

     # File lib/RMagick.rb, line 447
447:     def point(x, y)
448:         primitive "point #{x},#{y}"
449:     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 453
453:     def pointsize(points)
454:         primitive "font-size #{points}"
455:     end

Draw a polygon

[Source]

     # File lib/RMagick.rb, line 459
459:     def polygon(*points)
460:         if points.length == 0
461:             Kernel.raise ArgumentError, "no points specified"
462:         elsif points.length % 2 != 0
463:             Kernel.raise ArgumentError, "odd number of points specified"
464:         end
465:         primitive "polygon " + points.join(',')
466:     end

Draw a polyline

[Source]

     # File lib/RMagick.rb, line 469
469:     def polyline(*points)
470:         if points.length == 0
471:             Kernel.raise ArgumentError, "no points specified"
472:         elsif points.length % 2 != 0
473:             Kernel.raise ArgumentError, "odd number of points specified"
474:         end
475:         primitive "polyline " + points.join(',')
476:     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 484
484:     def pop(*what)
485:         if what.length == 0
486:             primitive "pop graphic-context"
487:         else
488:             # to_s allows a Symbol to be used instead of a String
489:             primitive "pop " + what.map {|w| w.to_s}.join(' ')
490:         end
491:     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 498
498:     def push(*what)
499:         if what.length == 0
500:             primitive "push graphic-context"
501:         else
502:             # to_s allows a Symbol to be used instead of a String
503:             primitive "push " + what.map {|w| w.to_s}.join(' ')
504:         end
505:     end

Draw a rectangle

[Source]

     # File lib/RMagick.rb, line 508
508:     def rectangle(upper_left_x, upper_left_y, lower_right_x, lower_right_y)
509:         primitive "rectangle " + sprintf("%g,%g %g,%g",
510:                 upper_left_x, upper_left_y, lower_right_x, lower_right_y)
511:     end

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

[Source]

     # File lib/RMagick.rb, line 514
514:     def rotate(angle)
515:         primitive "rotate #{angle}"
516:     end

Draw a rectangle with rounded corners

[Source]

     # File lib/RMagick.rb, line 519
519:     def roundrectangle(center_x, center_y, width, height, corner_width, corner_height)
520:         primitive "roundrectangle " + sprintf("%g,%g,%g,%g,%g,%g",
521:             center_x, center_y, width, height, corner_width, corner_height)
522:     end

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

[Source]

     # File lib/RMagick.rb, line 525
525:     def scale(x, y)
526:         primitive "scale #{x},#{y}"
527:     end

[Source]

     # File lib/RMagick.rb, line 529
529:     def skewx(angle)
530:         primitive "skewX #{angle}"
531:     end

[Source]

     # File lib/RMagick.rb, line 533
533:     def skewy(angle)
534:         primitive "skewY #{angle}"
535:     end

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

[Source]

     # File lib/RMagick.rb, line 538
538:     def stroke(colorspec)
539:         primitive "stroke #{enquote(colorspec)}"
540:     end

Specify if stroke should be antialiased or not

[Source]

     # File lib/RMagick.rb, line 545
545:     def stroke_antialias(bool)
546:         bool = bool ? '1' : '0'
547:         primitive "stroke-antialias #{bool}"
548:     end
stroke_color(colorspec)

Alias for stroke

Specify a stroke dash pattern

[Source]

     # File lib/RMagick.rb, line 551
551:     def stroke_dasharray(*list)
552:         if list.length == 0
553:             primitive "stroke-dasharray none"
554:         else
555:             list.each { |x|
556:                 if x <= 0 then
557:                     Kernel.raise ArgumentError, "dash array elements must be > 0 (#{x} given)"
558:                 end
559:             }
560:             primitive "stroke-dasharray #{list.join(',')}"
561:         end
562:     end

Specify the initial offset in the dash pattern

[Source]

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

[Source]

     # File lib/RMagick.rb, line 569
569:     def stroke_linecap(value)
570:         if ( not ["butt", "round", "square"].include?(value.downcase) )
571:             Kernel.raise ArgumentError, "Unknown linecap type: #{value}"
572:         end
573:         primitive "stroke-linecap #{value}"
574:     end

[Source]

     # File lib/RMagick.rb, line 576
576:     def stroke_linejoin(value)
577:         if ( not ["round", "miter", "bevel"].include?(value.downcase) )
578:             Kernel.raise ArgumentError, "Unknown linejoin type: #{value}"
579:         end
580:         primitive "stroke-linejoin #{value}"
581:     end

[Source]

     # File lib/RMagick.rb, line 583
583:     def stroke_miterlimit(value)
584:         if (value < 1)
585:             Kernel.raise ArgumentError, "miterlimit must be >= 1"
586:         end
587:         primitive "stroke-miterlimit #{value}"
588:     end

Specify opacity of stroke drawing color

 (use "xx%" to indicate percentage)

[Source]

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

Alias for stroke

Specify stroke (outline) width in pixels.

[Source]

     # File lib/RMagick.rb, line 597
597:     def stroke_width(pixels)
598:         primitive "stroke-width #{pixels}"
599:     end

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

[Source]

     # File lib/RMagick.rb, line 602
602:     def text(x, y, text)
603:         if text.to_s.empty?
604:             Kernel.raise ArgumentError, "missing text argument"
605:         end
606:         if text.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(text)
607:             ; # text already quoted
608:         elsif !text['\'']
609:             text = '\''+text+'\''
610:         elsif !text['"']
611:             text = '"'+text+'"'
612:         elsif !(text['{'] || text['}'])
613:             text = '{'+text+'}'
614:         else
615:             # escape existing braces, surround with braces
616:             text = '{' +  text.gsub(/[}]/) { |b| '\\' + b } + '}'
617:         end
618:         primitive "text #{x},#{y} #{text}"
619:     end

Specify text alignment relative to a given point

[Source]

     # File lib/RMagick.rb, line 622
622:     def text_align(alignment)
623:         if ( not ALIGN_TYPE_NAMES.has_key?(alignment.to_i) )
624:             Kernel.raise ArgumentError, "Unknown alignment constant: #{alignment}"
625:         end
626:         primitive "text-align #{ALIGN_TYPE_NAMES[alignment.to_i]}"
627:     end

SVG-compatible version of text_align

[Source]

     # File lib/RMagick.rb, line 630
630:     def text_anchor(anchor)
631:         if ( not ANCHOR_TYPE_NAMES.has_key?(anchor.to_i) )
632:             Kernel.raise ArgumentError, "Unknown anchor constant: #{anchor}"
633:         end
634:         primitive "text-anchor #{ANCHOR_TYPE_NAMES[anchor.to_i]}"
635:     end

Specify if rendered text is to be antialiased.

[Source]

     # File lib/RMagick.rb, line 638
638:     def text_antialias(boolean)
639:         boolean = boolean ? '1' : '0'
640:         primitive "text-antialias #{boolean}"
641:     end

Specify color underneath text

[Source]

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

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

[Source]

     # File lib/RMagick.rb, line 650
650:     def translate(x, y)
651:         primitive "translate #{x},#{y}"
652:     end

Private Instance methods

[Source]

     # File lib/RMagick.rb, line 206
206:     def enquote(str)
207:         if str.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(str)
208:             return str
209:         else
210:             return '"' + str + '"'
211:         end
212:     end

[Validate]