Class: Magick::Geometry
- Inherits:
-
Object
- Object
- Magick::Geometry
- Defined in:
- lib/rmagick_internal.rb
Constant Summary collapse
- FLAGS =
['', '%', '!', '<', '>', '@', '^']
- RFLAGS =
{ '%' => PercentGeometry, '!' => AspectGeometry, '<' => LessGeometry, '>' => GreaterGeometry, '@' => AreaGeometry, '^' => MinimumGeometry }
- W =
Construct an object from a geometry string
/(\d+\.\d+%?)|(\d*%?)/
- H =
W
- X =
/(?:([-+]\d+))?/
- Y =
X
- RE =
/\A#{W}x?#{H}#{X}#{Y}([!<>@\^]?)\Z/
Instance Attribute Summary collapse
-
#flag ⇒ Object
Returns the value of attribute flag.
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(width = nil, height = nil, x = nil, y = nil, flag = nil) ⇒ Geometry
constructor
A new instance of Geometry.
-
#to_s ⇒ Object
Convert object to a geometry string.
Constructor Details
#initialize(width = nil, height = nil, x = nil, y = nil, flag = nil) ⇒ Geometry
Returns a new instance of Geometry.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/rmagick_internal.rb', line 121 def initialize(width = nil, height = nil, x = nil, y = nil, flag = nil) raise(ArgumentError, "width set to #{width}") if width.is_a? GeometryValue raise(ArgumentError, "height set to #{height}") if height.is_a? GeometryValue raise(ArgumentError, "x set to #{x}") if x.is_a? GeometryValue raise(ArgumentError, "y set to #{y}") if y.is_a? GeometryValue # Support floating-point width and height arguments so Geometry # objects can be used to specify Image#density= arguments. if width.nil? @width = 0 elsif width.to_f >= 0.0 @width = width.to_f else Kernel.raise ArgumentError, "width must be >= 0: #{width}" end if height.nil? @height = 0 elsif height.to_f >= 0.0 @height = height.to_f else Kernel.raise ArgumentError, "height must be >= 0: #{height}" end @x = x.to_i @y = y.to_i @flag = flag end |
Instance Attribute Details
#flag ⇒ Object
Returns the value of attribute flag.
119 120 121 |
# File 'lib/rmagick_internal.rb', line 119 def flag @flag end |
#height ⇒ Object
Returns the value of attribute height.
119 120 121 |
# File 'lib/rmagick_internal.rb', line 119 def height @height end |
#width ⇒ Object
Returns the value of attribute width.
119 120 121 |
# File 'lib/rmagick_internal.rb', line 119 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
119 120 121 |
# File 'lib/rmagick_internal.rb', line 119 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
119 120 121 |
# File 'lib/rmagick_internal.rb', line 119 def y @y end |
Class Method Details
.from_s(str) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rmagick_internal.rb', line 156 def self.from_s(str) m = RE.match(str) if m width = (m[1] || m[2]).to_f height = (m[3] || m[4]).to_f x = m[5].to_i y = m[6].to_i flag = RFLAGS[m[7]] else Kernel.raise ArgumentError, 'invalid geometry format' end flag = PercentGeometry if str['%'] Geometry.new(width, height, x, y, flag) end |
Instance Method Details
#to_s ⇒ Object
Convert object to a geometry string
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/rmagick_internal.rb', line 172 def to_s str = '' if @width > 0 fmt = @width.truncate == @width ? '%d' : '%.2f' str << sprintf(fmt, @width) str << '%' if @flag == PercentGeometry end str << 'x' if (@width > 0 && @flag != PercentGeometry) || (@height > 0) if @height > 0 fmt = @height.truncate == @height ? '%d' : '%.2f' str << sprintf(fmt, @height) str << '%' if @flag == PercentGeometry end str << sprintf('%+d%+d', @x, @y) if @x != 0 || @y != 0 str << FLAGS[@flag.to_i] if @flag != PercentGeometry str end |