class Magick::Geometry

Constants

FLAGS
H
RE
RFLAGS
W

Construct an object from a geometry string

X
Y

Attributes

flag[RW]
height[RW]
width[RW]
x[RW]
y[RW]

Public Class Methods

from_s(str) click to toggle source
# File lib/rmagick_internal.rb, line 159
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
new(width = nil, height = nil, x = nil, y = nil, flag = nil) click to toggle source
# File lib/rmagick_internal.rb, line 124
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

Public Instance Methods

to_s() click to toggle source

Convert object to a geometry string

# File lib/rmagick_internal.rb, line 175
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