Class | Magick::Geometry |
In: |
lib/RMagick.rb
|
Parent: | Object |
FLAGS | = | ['', '%', '!', '<', '>', '@', '^'] | ||
RFLAGS | = | { '%' => PercentGeometry, '!' => AspectGeometry, '<' => LessGeometry, '>' => GreaterGeometry, '@' => AreaGeometry, '^' => MinimumGeometry } | ||
W | = | /(\d+\.\d+%?)|(\d*%?)/ | Construct an object from a geometry string | |
H | = | W | ||
X | = | /(?:([-+]\d+))?/ | ||
Y | = | X | ||
RE | = | /\A#{W}x?#{H}#{X}#{Y}([!<>@\^]?)\Z/ |
flag | [RW] | |
height | [RW] | |
width | [RW] | |
x | [RW] | |
y | [RW] |
# File lib/RMagick.rb, line 89 89: def Geometry.from_s(str) 90: 91: m = RE.match(str) 92: if m 93: width = (m[1] || m[2]).to_f 94: height = (m[3] || m[4]).to_f 95: x = m[5].to_i 96: y = m[6].to_i 97: flag = RFLAGS[m[7]] 98: else 99: Kernel.raise ArgumentError, "invalid geometry format" 100: end 101: if str['%'] 102: flag = PercentGeometry 103: end 104: Geometry.new(width, height, x, y, flag) 105: end
# File lib/RMagick.rb, line 53 53: def initialize(width=nil, height=nil, x=nil, y=nil, flag=nil) 54: raise(ArgumentError, "width set to #{width.to_s}") if width.is_a? GeometryValue 55: raise(ArgumentError, "height set to #{height.to_s}") if height.is_a? GeometryValue 56: raise(ArgumentError, "x set to #{x.to_s}") if x.is_a? GeometryValue 57: raise(ArgumentError, "y set to #{y.to_s}") if y.is_a? GeometryValue 58: 59: # Support floating-point width and height arguments so Geometry 60: # objects can be used to specify Image#density= arguments. 61: if width == nil 62: @width = 0 63: elsif width.to_f >= 0.0 64: @width = width.to_f 65: else 66: Kernel.raise ArgumentError, "width must be >= 0: #{width}" 67: end 68: if height == nil 69: @height = 0 70: elsif height.to_f >= 0.0 71: @height = height.to_f 72: else 73: Kernel.raise ArgumentError, "height must be >= 0: #{height}" 74: end 75: 76: @x = x.to_i 77: @y = y.to_i 78: @flag = flag 79: 80: end
Convert object to a geometry string
# File lib/RMagick.rb, line 108 108: def to_s 109: str = '' 110: if @width > 0 111: fmt = @width.truncate == @width ? "%d" : "%.2f" 112: str << sprintf(fmt, @width) 113: str << '%' if @flag == PercentGeometry 114: end 115: 116: if (@width > 0 && @flag != PercentGeometry) || (@height > 0) 117: str << 'x' 118: end 119: 120: if @height > 0 121: fmt = @height.truncate == @height ? "%d" : "%.2f" 122: str << sprintf(fmt, @height) 123: str << '%' if @flag == PercentGeometry 124: end 125: str << sprintf("%+d%+d", @x, @y) if (@x != 0 || @y != 0) 126: if @flag != PercentGeometry 127: str << FLAGS[@flag.to_i] 128: end 129: str 130: end