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 HatchFill Draw lib/RMagick.rb lib/rvg/rvg.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 211
211:     def affine(sx, rx, ry, sy, tx, ty)
212:         primitive "affine " + sprintf("%g,%g,%g,%g,%g,%g", sx, rx, ry, sy, tx, ty)
213:     end

Draw an arc.

[Source]

     # File lib/RMagick.rb, line 216
216:     def arc(startX, startY, endX, endY, startDegrees, endDegrees)
217:         primitive "arc " + sprintf("%g,%g %g,%g %g,%g",
218:                     startX, startY, endX, endY, startDegrees, endDegrees)
219:     end

Draw a bezier curve.

[Source]

     # File lib/RMagick.rb, line 222
222:     def bezier(*points)
223:         if points.length == 0
224:             Kernel.raise ArgumentError, "no points specified"
225:         elsif points.length % 2 != 0
226:             Kernel.raise ArgumentError, "odd number of arguments specified"
227:         end
228:         primitive "bezier " + points.join(',')
229:     end

Draw a circle

[Source]

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

Invoke a clip-path defined by def_clip_path.

[Source]

     # File lib/RMagick.rb, line 237
237:     def clip_path(name)
238:         primitive "clip-path #{name}"
239:     end

Define the clipping rule.

[Source]

     # File lib/RMagick.rb, line 242
242:     def clip_rule(rule)
243:         if ( not ["evenodd", "nonzero"].include?(rule.downcase) )
244:             Kernel.raise ArgumentError, "Unknown clipping rule #{rule}"
245:         end
246:         primitive "clip-rule #{rule}"
247:     end

Define the clip units

[Source]

     # File lib/RMagick.rb, line 250
250:     def clip_units(unit)
251:         if ( not ["userspace", "userspaceonuse", "objectboundingbox"].include?(unit.downcase) )
252:             Kernel.raise ArgumentError, "Unknown clip unit #{unit}"
253:         end
254:         primitive "clip-units #{unit}"
255:     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 259
259:     def color(x, y, method)
260:         if ( not PAINT_METHOD_NAMES.has_key?(method.to_i) )
261:             Kernel.raise ArgumentError, "Unknown PaintMethod: #{method}"
262:         end
263:         primitive "color #{x},#{y},#{PAINT_METHOD_NAMES[method.to_i]}"
264:     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 268
268:     def decorate(decoration)
269:         if ( DECORATION_TYPE_NAMES.has_key?(decoration.to_i) )
270:             primitive "decorate #{DECORATION_TYPE_NAMES[decoration.to_i]}"
271:         else
272:             primitive "decorate #{enquote(decoration)}"
273:         end
274:     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 281
281:     def define_clip_path(name)
282:         begin
283:             push('defs')
284:             push('clip-path', name)
285:             push('graphic-context')
286:             yield
287:         ensure
288:             pop('graphic-context')
289:             pop('clip-path')
290:             pop('defs')
291:         end
292:     end

Draw an ellipse

[Source]

     # File lib/RMagick.rb, line 295
295:     def ellipse(originX, originY, width, height, arcStart, arcEnd)
296:         primitive "ellipse " + sprintf("%g,%g %g,%g %g,%g",
297:                         originX, originY, width, height, arcStart, arcEnd)
298:     end

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

[Source]

     # File lib/RMagick.rb, line 302
302:     def encoding(encoding)
303:         primitive "encoding #{encoding}"
304:     end

Specify object fill, a color name or pattern name

[Source]

     # File lib/RMagick.rb, line 307
307:     def fill(colorspec)
308:         primitive "fill #{enquote(colorspec)}"
309:     end
fill_color(colorspec)

Alias for fill

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

[Source]

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

Alias for fill

[Source]

     # File lib/RMagick.rb, line 318
318:     def fill_rule(rule)
319:         if ( not ["evenodd", "nonzero"].include?(rule.downcase) )
320:             Kernel.raise ArgumentError, "Unknown fill rule #{rule}"
321:         end
322:         primitive "fill-rule #{rule}"
323:     end

Specify text drawing font

[Source]

     # File lib/RMagick.rb, line 326
326:     def font(name)
327:         primitive "font #{name}"
328:     end

[Source]

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

Alias for pointsize

[Source]

     # File lib/RMagick.rb, line 334
334:     def font_stretch(stretch)
335:         if ( not STRETCH_TYPE_NAMES.has_key?(stretch.to_i) )
336:             Kernel.raise ArgumentError, "Unknown stretch type"
337:         end
338:         primitive "font-stretch #{STRETCH_TYPE_NAMES[stretch.to_i]}"
339:     end

[Source]

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

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

[Source]

     # File lib/RMagick.rb, line 350
350:     def font_weight(weight)
351:         if ( FONT_WEIGHT_NAMES.has_key?(weight.to_i) )
352:             primitive "font-weight #{FONT_WEIGHT_NAMES[weight.to_i]}"
353:         else
354:             primitive "font-weight #{weight}"
355:         end
356:     end

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

[Source]

     # File lib/RMagick.rb, line 360
360:     def gravity(grav)
361:         if ( not GRAVITY_NAMES.has_key?(grav.to_i) )
362:             Kernel.raise ArgumentError, "Unknown text positioning gravity"
363:         end
364:         primitive "gravity #{GRAVITY_NAMES[grav.to_i]}"
365:     end

Draw a line

[Source]

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

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

[Source]

     # File lib/RMagick.rb, line 374
374:     def matte(x, y, method)
375:         if ( not PAINT_METHOD_NAMES.has_key?(method.to_i) )
376:             Kernel.raise ArgumentError, "Unknown paint method"
377:         end
378:         primitive "matte #{x},#{y} #{PAINT_METHOD_NAMES[method.to_i]}"
379:     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 383
383:     def opacity(opacity)
384:         if (Numeric === opacity)
385:             if (opacity < 0 || opacity > 1.0)
386:                 Kernel.raise ArgumentError, "opacity must be >= 0 and <= 1.0"
387:             end
388:         end
389:         primitive "opacity #{opacity}"
390:     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 395
395:     def path(cmds)
396:         primitive "path '" + cmds + "'"
397:     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 402
402:     def pattern(name, x, y, width, height)
403:         begin
404:             push('defs')
405:             push("pattern #{name} #{x} #{y} #{width} #{height}")
406:             push('graphic-context')
407:             yield
408:         ensure
409:             pop('graphic-context')
410:             pop('pattern')
411:             pop('defs')
412:         end
413:     end

Set point to fill color.

[Source]

     # File lib/RMagick.rb, line 416
416:     def point(x, y)
417:         primitive "point #{x},#{y}"
418:     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 422
422:     def pointsize(points)
423:         primitive "font-size #{points}"
424:     end

Draw a polygon

[Source]

     # File lib/RMagick.rb, line 428
428:     def polygon(*points)
429:         if points.length == 0
430:             Kernel.raise ArgumentError, "no points specified"
431:         elsif points.length % 2 != 0
432:             Kernel.raise ArgumentError, "odd number of points specified"
433:         end
434:         primitive "polygon " + points.join(',')
435:     end

Draw a polyline

[Source]

     # File lib/RMagick.rb, line 438
438:     def polyline(*points)
439:         if points.length == 0
440:             Kernel.raise ArgumentError, "no points specified"
441:         elsif points.length % 2 != 0
442:             Kernel.raise ArgumentError, "odd number of points specified"
443:         end
444:         primitive "polyline " + points.join(',')
445:     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 453
453:     def pop(*what)
454:         if what.length == 0
455:             primitive "pop graphic-context"
456:         else
457:             # to_s allows a Symbol to be used instead of a String
458:             primitive "pop " + what.map {|w| w.to_s}.join(' ')
459:         end
460:     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 467
467:     def push(*what)
468:         if what.length == 0
469:             primitive "push graphic-context"
470:         else
471:             # to_s allows a Symbol to be used instead of a String
472:             primitive "push " + what.map {|w| w.to_s}.join(' ')
473:         end
474:     end

Draw a rectangle

[Source]

     # File lib/RMagick.rb, line 477
477:     def rectangle(upper_left_x, upper_left_y, lower_right_x, lower_right_y)
478:         primitive "rectangle " + sprintf("%g,%g %g,%g",
479:                 upper_left_x, upper_left_y, lower_right_x, lower_right_y)
480:     end

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

[Source]

     # File lib/RMagick.rb, line 483
483:     def rotate(angle)
484:         primitive "rotate #{angle}"
485:     end

Draw a rectangle with rounded corners

[Source]

     # File lib/RMagick.rb, line 488
488:     def roundrectangle(center_x, center_y, width, height, corner_width, corner_height)
489:         primitive "roundrectangle " + sprintf("%g,%g,%g,%g,%g,%g",
490:             center_x, center_y, width, height, corner_width, corner_height)
491:     end

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

[Source]

     # File lib/RMagick.rb, line 494
494:     def scale(x, y)
495:         primitive "scale #{x},#{y}"
496:     end

[Source]

     # File lib/RMagick.rb, line 498
498:     def skewx(angle)
499:         primitive "skewX #{angle}"
500:     end

[Source]

     # File lib/RMagick.rb, line 502
502:     def skewy(angle)
503:         primitive "skewY #{angle}"
504:     end

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

[Source]

     # File lib/RMagick.rb, line 507
507:     def stroke(colorspec)
508:         primitive "stroke #{enquote(colorspec)}"
509:     end

Specify if stroke should be antialiased or not

[Source]

     # File lib/RMagick.rb, line 514
514:     def stroke_antialias(bool)
515:         bool = bool ? '1' : '0'
516:         primitive "stroke-antialias #{bool}"
517:     end
stroke_color(colorspec)

Alias for stroke

Specify a stroke dash pattern

[Source]

     # File lib/RMagick.rb, line 520
520:     def stroke_dasharray(*list)
521:         if list.length == 0
522:             primitive "stroke-dasharray none"
523:         else
524:             list.each { |x|
525:                 if x <= 0 then
526:                     Kernel.raise ArgumentError, "dash array elements must be > 0 (#{x} given)"
527:                 end
528:             }
529:             primitive "stroke-dasharray #{list.join(',')}"
530:         end
531:     end

Specify the initial offset in the dash pattern

[Source]

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

[Source]

     # File lib/RMagick.rb, line 538
538:     def stroke_linecap(value)
539:         if ( not ["butt", "round", "square"].include?(value.downcase) )
540:             Kernel.raise ArgumentError, "Unknown linecap type: #{value}"
541:         end
542:         primitive "stroke-linecap #{value}"
543:     end

[Source]

     # File lib/RMagick.rb, line 545
545:     def stroke_linejoin(value)
546:         if ( not ["round", "miter", "bevel"].include?(value.downcase) )
547:             Kernel.raise ArgumentError, "Unknown linejoin type: #{value}"
548:         end
549:         primitive "stroke-linejoin #{value}"
550:     end

[Source]

     # File lib/RMagick.rb, line 552
552:     def stroke_miterlimit(value)
553:         if (value < 1)
554:             Kernel.raise ArgumentError, "miterlimit must be >= 1"
555:         end
556:         primitive "stroke-miterlimit #{value}"
557:     end

Specify opacity of stroke drawing color

 (use "xx%" to indicate percentage)

[Source]

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

Alias for stroke

Specify stroke (outline) width in pixels.

[Source]

     # File lib/RMagick.rb, line 566
566:     def stroke_width(pixels)
567:         primitive "stroke-width #{pixels}"
568:     end

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

[Source]

     # File lib/RMagick.rb, line 571
571:     def text(x, y, text)
572:         if text.to_s.empty?
573:             Kernel.raise ArgumentError, "missing text argument"
574:         end
575:         if text.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(text)
576:             ; # text already quoted
577:         elsif !text['\'']
578:             text = '\''+text+'\''
579:         elsif !text['"']
580:             text = '"'+text+'"'
581:         elsif !(text['{'] || text['}'])
582:             text = '{'+text+'}'
583:         else
584:             # escape existing braces, surround with braces
585:             text = '{' +  text.gsub(/[}]/) { |b| '\\' + b } + '}'
586:         end
587:         primitive "text #{x},#{y} #{text}"
588:     end

Specify text alignment relative to a given point

[Source]

     # File lib/RMagick.rb, line 591
591:     def text_align(alignment)
592:         if ( not ALIGN_TYPE_NAMES.has_key?(alignment.to_i) )
593:             Kernel.raise ArgumentError, "Unknown alignment constant: #{alignment}"
594:         end
595:         primitive "text-align #{ALIGN_TYPE_NAMES[alignment.to_i]}"
596:     end

SVG-compatible version of text_align

[Source]

     # File lib/RMagick.rb, line 599
599:     def text_anchor(anchor)
600:         if ( not ANCHOR_TYPE_NAMES.has_key?(anchor.to_i) )
601:             Kernel.raise ArgumentError, "Unknown anchor constant: #{anchor}"
602:         end
603:         primitive "text-anchor #{ANCHOR_TYPE_NAMES[anchor.to_i]}"
604:     end

Specify if rendered text is to be antialiased.

[Source]

     # File lib/RMagick.rb, line 607
607:     def text_antialias(boolean)
608:         boolean = boolean ? '1' : '0'
609:         primitive "text-antialias #{boolean}"
610:     end

Specify color underneath text

[Source]

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

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

[Source]

     # File lib/RMagick.rb, line 619
619:     def translate(x, y)
620:         primitive "translate #{x},#{y}"
621:     end

Private Instance methods

[Source]

     # File lib/RMagick.rb, line 199
199:     def enquote(str)
200:         if str.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(str)
201:             return str
202:         else
203:             return '"' + str + '"'
204:         end
205:     end

[Validate]