Class | Magick::Draw |
In: |
lib/RMagick.rb
|
Parent: | Object |
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' |
Apply coordinate transformations to support scaling (s), rotation (r), and translation (t). Angles are specified in radians.
# 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
# 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
Define the clipping rule.
# 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
# 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
# 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)
# 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".
# 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
Let anything through, but the only defined argument is "UTF-8". All others are apparently ignored.
# File lib/RMagick.rb, line 302 302: def encoding(encoding) 303: primitive "encoding #{encoding}" 304: end
# 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
# File lib/RMagick.rb, line 330 330: def font_family(name) 331: primitive "font-family \'#{name}\'" 332: end
# 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
# 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]
# 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
# 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
IM 6.4.8-3 and later
# File lib/RMagick.rb, line 368 368: def interword_spacing(space) 369: begin 370: Float(space) 371: rescue ArgumentError 372: Kernel.raise ArgumentError, "invalid value for interword_spacing" 373: rescue TypeError 374: Kernel.raise TypeError, "can't convert #{space.class} into Float" 375: end 376: primitive "interword-spacing #{space}" 377: end
IM 6.4.8-3 and later
# File lib/RMagick.rb, line 380 380: def kerning(space) 381: begin 382: Float(space) 383: rescue ArgumentError 384: Kernel.raise ArgumentError, "invalid value for kerning" 385: rescue TypeError 386: Kernel.raise TypeError, "can't convert #{space.class} into Float" 387: end 388: primitive "kerning #{space}" 389: end
Set matte (make transparent) in image according to the specified colorization rule
# File lib/RMagick.rb, line 398 398: def matte(x, y, method) 399: if ( not PAINT_METHOD_NAMES.has_key?(method.to_i) ) 400: Kernel.raise ArgumentError, "Unknown paint method" 401: end 402: primitive "matte #{x},#{y} #{PAINT_METHOD_NAMES[method.to_i]}" 403: end
Specify drawing fill and stroke opacities. If the value is a string ending with a %, the number will be multiplied by 0.01.
# File lib/RMagick.rb, line 407 407: def opacity(opacity) 408: if (Numeric === opacity) 409: if (opacity < 0 || opacity > 1.0) 410: Kernel.raise ArgumentError, "opacity must be >= 0 and <= 1.0" 411: end 412: end 413: primitive "opacity #{opacity}" 414: 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
# File lib/RMagick.rb, line 426 426: def pattern(name, x, y, width, height) 427: begin 428: push('defs') 429: push("pattern #{name} #{x} #{y} #{width} #{height}") 430: push('graphic-context') 431: yield 432: ensure 433: pop('graphic-context') 434: pop('pattern') 435: pop('defs') 436: end 437: end
# File lib/RMagick.rb, line 452 452: def polygon(*points) 453: if points.length == 0 454: Kernel.raise ArgumentError, "no points specified" 455: elsif points.length % 2 != 0 456: Kernel.raise ArgumentError, "odd number of points specified" 457: end 458: primitive "polygon " + points.join(',') 459: end
# File lib/RMagick.rb, line 462 462: def polyline(*points) 463: if points.length == 0 464: Kernel.raise ArgumentError, "no points specified" 465: elsif points.length % 2 != 0 466: Kernel.raise ArgumentError, "odd number of points specified" 467: end 468: primitive "polyline " + points.join(',') 469: end
Return to the previously-saved set of whatever pop(‘graphic-context’) (the default if no arguments) pop(‘defs’) pop(‘gradient’) pop(‘pattern’)
# File lib/RMagick.rb, line 477 477: def pop(*what) 478: if what.length == 0 479: primitive "pop graphic-context" 480: else 481: # to_s allows a Symbol to be used instead of a String 482: primitive "pop " + what.map {|w| w.to_s}.join(' ') 483: end 484: 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’)
# File lib/RMagick.rb, line 491 491: def push(*what) 492: if what.length == 0 493: primitive "push graphic-context" 494: else 495: # to_s allows a Symbol to be used instead of a String 496: primitive "push " + what.map {|w| w.to_s}.join(' ') 497: end 498: end
Specify coordinate space rotation. "angle" is measured in degrees
# File lib/RMagick.rb, line 507 507: def rotate(angle) 508: primitive "rotate #{angle}" 509: end
Specify scaling to be applied to coordinate space on subsequent drawing commands.
# File lib/RMagick.rb, line 518 518: def scale(x, y) 519: primitive "scale #{x},#{y}" 520: end
# File lib/RMagick.rb, line 544 544: def stroke_dasharray(*list) 545: if list.length == 0 546: primitive "stroke-dasharray none" 547: else 548: list.each { |x| 549: if x <= 0 then 550: Kernel.raise ArgumentError, "dash array elements must be > 0 (#{x} given)" 551: end 552: } 553: primitive "stroke-dasharray #{list.join(',')}" 554: end 555: end
# File lib/RMagick.rb, line 562 562: def stroke_linecap(value) 563: if ( not ["butt", "round", "square"].include?(value.downcase) ) 564: Kernel.raise ArgumentError, "Unknown linecap type: #{value}" 565: end 566: primitive "stroke-linecap #{value}" 567: end
# File lib/RMagick.rb, line 569 569: def stroke_linejoin(value) 570: if ( not ["round", "miter", "bevel"].include?(value.downcase) ) 571: Kernel.raise ArgumentError, "Unknown linejoin type: #{value}" 572: end 573: primitive "stroke-linejoin #{value}" 574: end
# File lib/RMagick.rb, line 576 576: def stroke_miterlimit(value) 577: if (value < 1) 578: Kernel.raise ArgumentError, "miterlimit must be >= 1" 579: end 580: primitive "stroke-miterlimit #{value}" 581: end
Draw text at position x,y. Add quotes to text that is not already quoted.
# File lib/RMagick.rb, line 595 595: def text(x, y, text) 596: if text.to_s.empty? 597: Kernel.raise ArgumentError, "missing text argument" 598: end 599: if text.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(text) 600: ; # text already quoted 601: elsif !text['\''] 602: text = '\''+text+'\'' 603: elsif !text['"'] 604: text = '"'+text+'"' 605: elsif !(text['{'] || text['}']) 606: text = '{'+text+'}' 607: else 608: # escape existing braces, surround with braces 609: text = '{' + text.gsub(/[}]/) { |b| '\\' + b } + '}' 610: end 611: primitive "text #{x},#{y} #{text}" 612: end
Specify text alignment relative to a given point
# File lib/RMagick.rb, line 615 615: def text_align(alignment) 616: if ( not ALIGN_TYPE_NAMES.has_key?(alignment.to_i) ) 617: Kernel.raise ArgumentError, "Unknown alignment constant: #{alignment}" 618: end 619: primitive "text-align #{ALIGN_TYPE_NAMES[alignment.to_i]}" 620: end
SVG-compatible version of text_align
# File lib/RMagick.rb, line 623 623: def text_anchor(anchor) 624: if ( not ANCHOR_TYPE_NAMES.has_key?(anchor.to_i) ) 625: Kernel.raise ArgumentError, "Unknown anchor constant: #{anchor}" 626: end 627: primitive "text-anchor #{ANCHOR_TYPE_NAMES[anchor.to_i]}" 628: end
Specify center of coordinate space to use for subsequent drawing commands.
# File lib/RMagick.rb, line 643 643: def translate(x, y) 644: primitive "translate #{x},#{y}" 645: end