$Id: units.rb,v 1.2 2005/12/31 14:41:04 rmagick Exp $ Copyright (C) 2006 Timothy P. Hunter
WORD_SEP | = | / / |
background_fill | [R] | The background fill color specified by background_fill= |
background_fill_opacity | [R] | The background fill color opacity specified by background_fill_opacity= |
background_image | [R] | The background image specified by background_image= |
background_position | [R] | The background image layout specified by background_position= |
canvas | [R] | The image after drawing has completed |
dpi | [R] | |
height | [R] | |
width | [R] | |
x | [R] | For embedded RVG objects, the x-axis coordinate of the upper-left corner |
y | [R] | For embedded RVG objects, the x-axis coordinate of the upper-left corner |
# File lib/rvg/misc.rb, line 59 59: def self.convert_one_to_float(arg) 60: begin 61: farg = Float(arg) 62: rescue ArgumentError, TypeError 63: raise ArgumentError, "argument cannot be converted to Float (got #{arg.class})" 64: end 65: return farg 66: end
# File lib/rvg/misc.rb, line 45 45: def self.convert_to_float(*args) 46: allow_nil = false 47: if args.last == :allow_nil 48: allow_nil = true 49: args.pop 50: end 51: begin 52: fargs = args.collect { |a| (allow_nil && a.nil?) ? a : Float(a) } 53: rescue ArgumentError, TypeError 54: raise ArgumentError, self.fmsg(*args) 55: end 56: return fargs 57: end
# File lib/rvg/units.rb, line 8 8: def dpi=(n) 9: if !defined?(@dpi) 10: [Float, Fixnum].each do |c| 11: c.class_eval do 12: # the default measurement - 1px is 1 pixel 13: def px 14: self 15: end 16: # inches 17: def in 18: self * ::Magick::RVG.dpi 19: end 20: # millimeters 21: def mm 22: self * ::Magick::RVG.dpi / 25.4 23: end 24: # centimeters 25: def cm 26: self * ::Magick::RVG.dpi / 2.54 27: end 28: # points 29: def pt 30: self * ::Magick::RVG.dpi / 72.0 31: end 32: # picas 33: def pc 34: self * ::Magick::RVG.dpi / 6.0 35: end 36: # percentage of the argument 37: def pct(of) 38: self * Float(of) / 100.0 39: end 40: # the default is deg 41: def deg 42: self 43: end 44: # radians -> degrees 45: def rad 46: self * 180.0 / Math::PI 47: end 48: # grads -> degrees 49: def grad 50: self * 9.0 / 10.0 51: end 52: end 53: end 54: end 55: 56: @dpi = Float(n) 57: return @dpi 58: rescue ArgumentError 59: raise TypeError, "Can't convert `#{n}' to Float" 60: end
Convert an array of method arguments to Float objects. If any cannot be converted, raise ArgumentError and issue a message.
# File lib/rvg/misc.rb, line 41 41: def self.fmsg(*args) 42: "at least one argument cannot be converted to Float (got #{args.collect {|a| a.class}.join(', ')})" 43: end
Draw a width x height image. The image is specified by calling one or more drawing methods on the RVG object. You can group the drawing method calls in the optional associated block. The x and y arguments have no meaning for the outermost RVG object. On nested RVG objects [x, y] is the coordinate of the upper-left corner in the containing canvas on which the nested RVG object is placed.
Drawing occurs on a canvas created by the draw method. By default the canvas is transparent. You can specify a different canvas with the background_fill= or background_image= methods.
RVG objects are containers. That is, styles and transforms defined on the object are used by contained objects such as shapes, text, and groups unless overridden by an inner container or the object itself.
# File lib/rvg/rvg.rb, line 215 215: def initialize(width=nil, height=nil) 216: super 217: @width, @height = width, height 218: @content = Content.new 219: @canvas = nil 220: @background_fill = nil 221: @background_fill_opacity = 1.0 # applies only if background_fill= is used 222: @background_position = :scaled 223: @background_pattern, @background_image, @desc, @title, @metadata = nil 224: @x, @y = 0.0 225: @nested = false 226: yield(self) if block_given? 227: end
percentage of the argument
# File lib/rvg/units.rb, line 37 37: def pct(of) 38: self * Float(of) / 100.0 39: end
the default measurement - 1px is 1 pixel
# File lib/rvg/units.rb, line 13 13: def px 14: self 15: end
Sets the canvas background color. Either a Magick::Pixel or a color name. The default fill is "none", that is, transparent black.
# File lib/rvg/rvg.rb, line 173 173: def background_fill=(color) 174: warn "background_fill= has no effect in nested RVG objects" if @nested 175: if ! color.kind_of?(Magick::Pixel) 176: begin 177: @background_fill = Magick::Pixel.from_color(color) 178: rescue Magick::ImageMagickError 179: raise ArgumentError, "unknown color `#{color}'" 180: rescue TypeError 181: raise TypeError, "cannot convert #{color.class} into Pixel" 182: rescue 183: raise ArgumentError, "argument must be a color name or a Pixel (got #{color.class})" 184: end 185: else 186: @background_fill = color 187: end 188: end
Opacity of the background fill color, a number between 0.0 (transparent) and 1.0 (opaque). The default is 1.0 when the background_fill= attribute has been set.
# File lib/rvg/rvg.rb, line 192 192: def background_fill_opacity=(opacity) 193: warn "background_fill_opacity= has no effect in nested RVG objects" if @nested 194: begin 195: @background_fill_opacity = Float(opacity) 196: rescue ArgumentError 197: raise ArgumentError, "background_fill_opacity must be a number between 0 and 1 (#{opacity} given)" 198: end 199: end
Sets an image to use as the canvas background. See background_position= for layout options.
# File lib/rvg/rvg.rb, line 140 140: def background_image=(bg_image) 141: warn "background_image= has no effect in nested RVG objects" if @nested 142: if bg_image && ! bg_image.kind_of?(Magick::Image) 143: raise ArgumentError, "background image must be an Image (got #{bg_image.class})" 144: end 145: @background_image = bg_image 146: end
Sets an object to use to fill the canvas background. The object must have a fill method. See the Fill Classes section in the RMagick doc for more information.
# File lib/rvg/rvg.rb, line 151 151: def background_pattern=(filler) 152: warn "background_pattern= has no effect in nested RVG objects" if @nested 153: @background_pattern = filler 154: end
How to position the background image on the canvas. One of the following symbols:
# File lib/rvg/rvg.rb, line 162 162: def background_position=(pos) 163: warn "background_position= has no effect in nested RVG objects" if @nested 164: bg_pos = pos.to_s.downcase 165: if ! ['scaled', 'tiled', 'fit'].include?(bg_pos) 166: raise ArgumentError, "background position must be `scaled', `tiled', or `fit' (#{pos} given)" 167: end 168: @background_position = bg_pos.to_sym 169: end
# File lib/rvg/deep_equal.rb, line 5 5: def deep_equal(other) 6: if self != other 7: puts "#{c.inspect} not equal.\nself:#{self} != other:#{other}" 8: return false 9: end 10: return true 11: end
# File lib/rvg/deep_equal.rb, line 17 17: def deep_equal(other) 18: ivs = self.instance_variables 19: 20: ivs.each do |iv| 21: itv = self.instance_variable_get(iv) 22: otv = other.instance_variable_get(iv) 23: if itv.respond_to?(:deep_equal) 24: if itv.equal?(otv) 25: puts "#{iv} has deep_equal but self.#{iv} and other.#{iv} are the same object." 26: return false 27: end 28: if !itv.deep_equal(otv) 29: puts "Not equal.\nself.#{iv}=#{itv.inspect}\nother.#{iv}=#{otv.inspect}" 30: return false 31: end 32: else 33: case itv 34: when Float, Symbol, TrueClass, FalseClass, Fixnum, NilClass 35: return false if itv != otv 36: else 37: if itv.equal?(otv) 38: puts "#{iv} is dup-able but self.#{iv} and other.#{iv} are the same object." 39: return false 40: end 41: if itv != otv 42: puts "Not equal.\nself.#{iv}=#{itv.inspect}\nother.#{iv}=#{otv.inspect}" 43: return false 44: end 45: end 46: end 47: end 48: 49: return true 50: end
Construct a canvas or reuse an existing canvas. Execute drawing commands. Return the canvas.
# File lib/rvg/rvg.rb, line 231 231: def draw 232: raise StandardError, "draw not permitted in nested RVG objects" if @nested 233: @canvas ||= new_canvas # allow drawing over existing canvas 234: gc = Utility::GraphicContext.new 235: add_outermost_primitives(gc) 236: pp(self) if ENV['debug_rvg'] 237: print_gc(gc) if ENV['debug_prim'] 238: gc.draw(@canvas) 239: return @canvas 240: end
background_fill defaults to ‘none’. If background_fill has been set to something else, combine it with the background_fill_opacity.
# File lib/rvg/rvg.rb, line 62 62: def bgfill() 63: if @background_fill.nil? 64: color = Magick::Pixel.new(0,0,0,Magick::TransparentOpacity) 65: else 66: color = @background_fill 67: color.opacity = (1.0 - @background_fill_opacity) * Magick::TransparentOpacity 68: end 69: return color 70: end
# File lib/rvg/rvg.rb, line 72 72: def new_canvas 73: if @background_pattern 74: canvas = Magick::Image.new(@width, @height, @background_pattern) 75: elsif @background_image 76: if @width != @background_image.columns || @height != @background_image.rows 77: canvas = case @background_position 78: when :scaled 79: @background_image.resize(@width, @height) 80: when :tiled 81: Magick::Image.new(@width, @height, Magick::TextureFill.new(@background_image)) 82: when :fit 83: width, height = @width, @height 84: bgcolor = bgfill() 85: @background_image.change_geometry(Magick::Geometry.new(width, height)) do |new_cols, new_rows| 86: bg_image = @background_image.resize(new_cols, new_rows) 87: if bg_image.columns != width || bg_image.rows != height 88: bg = Magick::Image.new(width, height) { self.background_color = bgcolor } 89: bg_image = bg.composite!(bg_image, Magick::CenterGravity, Magick::OverCompositeOp) 90: end 91: bg_image 92: end 93: end 94: else 95: canvas = @background_image.copy 96: end 97: else 98: bgcolor = bgfill() 99: canvas = Magick::Image.new(Integer(@width), Integer(@height)) { self.background_color = bgcolor } 100: end 101: canvas[:desc] = @desc if @desc 102: canvas[:title] = @title if @title 103: canvas[:metadata] = @metadata if @metadata 104: return canvas 105: end