| 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 96
96: def Geometry.from_s(str)
97:
98: m = RE.match(str)
99: if m
100: width = (m[1] || m[2]).to_f
101: height = (m[3] || m[4]).to_f
102: x = m[5].to_i
103: y = m[6].to_i
104: flag = RFLAGS[m[7]]
105: else
106: Kernel.raise ArgumentError, "invalid geometry format"
107: end
108: if str['%']
109: flag = PercentGeometry
110: end
111: Geometry.new(width, height, x, y, flag)
112: end
# File lib/RMagick.rb, line 60
60: def initialize(width=nil, height=nil, x=nil, y=nil, flag=nil)
61: raise(ArgumentError, "width set to #{width.to_s}") if width.is_a? GeometryValue
62: raise(ArgumentError, "height set to #{height.to_s}") if height.is_a? GeometryValue
63: raise(ArgumentError, "x set to #{x.to_s}") if x.is_a? GeometryValue
64: raise(ArgumentError, "y set to #{y.to_s}") if y.is_a? GeometryValue
65:
66: # Support floating-point width and height arguments so Geometry
67: # objects can be used to specify Image#density= arguments.
68: if width == nil
69: @width = 0
70: elsif width.to_f >= 0.0
71: @width = width.to_f
72: else
73: Kernel.raise ArgumentError, "width must be >= 0: #{width}"
74: end
75: if height == nil
76: @height = 0
77: elsif height.to_f >= 0.0
78: @height = height.to_f
79: else
80: Kernel.raise ArgumentError, "height must be >= 0: #{height}"
81: end
82:
83: @x = x.to_i
84: @y = y.to_i
85: @flag = flag
86:
87: end
Convert object to a geometry string
# File lib/RMagick.rb, line 115
115: def to_s
116: str = ''
117: if @width > 0
118: fmt = @width.truncate == @width ? "%d" : "%.2f"
119: str << sprintf(fmt, @width)
120: str << '%' if @flag == PercentGeometry
121: end
122:
123: if (@width > 0 && @flag != PercentGeometry) || (@height > 0)
124: str << 'x'
125: end
126:
127: if @height > 0
128: fmt = @height.truncate == @height ? "%d" : "%.2f"
129: str << sprintf(fmt, @height)
130: str << '%' if @flag == PercentGeometry
131: end
132: str << sprintf("%+d%+d", @x, @y) if (@x != 0 || @y != 0)
133: if @flag != PercentGeometry
134: str << FLAGS[@flag.to_i]
135: end
136: str
137: end