lib/RMagick.rb in rmagick-1.8.3 vs lib/RMagick.rb in rmagick-1.9.0

- old
+ new

@@ -1,6 +1,6 @@ -# $Id: RMagick.rb,v 1.23 2005/03/05 16:19:10 rmagick Exp $ +# $Id: RMagick.rb,v 1.25 2005/07/12 23:34:04 rmagick Exp $ #============================================================================== # Copyright (C) 2005 by Timothy P. Hunter # Name: RMagick.rb # Author: Tim Hunter # Purpose: Extend Ruby to interface with ImageMagick. @@ -161,11 +161,22 @@ NormalStyle.to_i => 'normal', ItalicStyle.to_i => 'italic', ObliqueStyle.to_i => 'oblique', AnyStyle.to_i => 'all' } + + private + def enquote(str) + if str.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(str) + return str + else + return '"' + str + '"' + end + end + public + # Apply coordinate transformations to support scaling (s), rotation (r), # and translation (t). Angles are specified in radians. def affine(sx, rx, ry, sy, tx, ty) primitive "affine " + sprintf("%g,%g,%g,%g,%g,%g", sx, rx, ry, sy, tx, ty) end @@ -225,11 +236,11 @@ # line-through) OR the text solid background color (any color name or spec) def decorate(decoration) if ( DECORATION_TYPE_NAMES.has_key?(decoration.to_i) ) primitive "decorate #{DECORATION_TYPE_NAMES[decoration.to_i]}" else - primitive "decorate #{decoration}" + primitive "decorate #{enquote(decoration)}" end end # Define a clip-path. A clip-path is a sequence of primitives # bracketed by the "push clip-path <name>" and "pop clip-path" @@ -261,11 +272,11 @@ primitive "encoding #{encoding}" end # Specify object fill, a color name or pattern name def fill(colorspec) - primitive "fill #{colorspec}" + primitive "fill #{enquote(colorspec)}" end alias fill_color fill alias fill_pattern fill # Specify fill opacity (use "xx%" to indicate percentage) @@ -461,11 +472,11 @@ primitive "skewY #{angle}" end # Specify the object stroke, a color name or pattern name. def stroke(colorspec) - primitive "stroke #{colorspec}" + primitive "stroke #{enquote(colorspec)}" end alias stroke_color stroke alias stroke_pattern stroke # Specify if stroke should be antialiased or not @@ -523,17 +534,27 @@ # Specify stroke (outline) width in pixels. def stroke_width(pixels) primitive "stroke-width #{pixels}" end - # Draw text at position x,y. Generally it's best to surround the text with - # quotes. For example, - # gc.text(100, 100, "'embedded blanks'") + # Draw text at position x,y. Add quotes to text that is not already quoted. def text(x, y, text) if text.to_s.empty? raise ArgumentError, "missing text argument" - end + end + if text.length > 2 && /\A(?:\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})\z/.match(text) + ; # text already quoted + elsif !text['\''] + text = '\''+text+'\'' + elsif !text['"'] + text = '"'+text+'"' + elsif !(text['{'] || text['}']) + text = '{'+text+'}' + else + # escape existing braces, surround with braces + text = '{' + text.gsub(/[}]/) { |b| '\\' + b } + '}' + end primitive "text #{x},#{y} #{text}" end # Specify text alignment relative to a given point def text_align(alignment) @@ -557,10 +578,10 @@ primitive "text-antialias #{boolean}" end # Specify color underneath text def text_undercolor(color) - primitive "text-undercolor #{color}" + primitive "text-undercolor #{enquote(color)}" end # Specify center of coordinate space to use for subsequent drawing # commands. def translate(x, y)