Class: Magick::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/RMagick.rb

Constant Summary collapse

FLAGS =
['', '%', '!', '<', '>', '@']
RFLAGS =
{ '%' => PercentGeometry,
'!' => AspectGeometry,
'<' => LessGeometry,
'>' => GreaterGeometry,
'@' => AreaGeometry }
RE =

Construct an object from a geometry string

/\A(\d*)(?:x(\d+))?([-+]\d+)?([-+]\d+)?([%!<>@]?)\Z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width = nil, height = nil, x = nil, y = nil, flag = nil) ⇒ Geometry

Returns a new instance of Geometry.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/RMagick.rb', line 48

def initialize(width=nil, height=nil, x=nil, y=nil, flag=nil)

    # 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
        raise ArgumentError, "width must be >= 0: #{width}"
    end
    if height == nil
        @height = 0
    elsif height.to_f >= 0.0
        @height = height.to_f
    else
        raise ArgumentError, "height must be >= 0: #{height}"
    end

    @x    = x.to_i
    @y    = y.to_i
    @flag = flag
end

Instance Attribute Details

#flagObject

Returns the value of attribute flag.



46
47
48
# File 'lib/RMagick.rb', line 46

def flag
  @flag
end

#heightObject

Returns the value of attribute height.



46
47
48
# File 'lib/RMagick.rb', line 46

def height
  @height
end

#widthObject

Returns the value of attribute width.



46
47
48
# File 'lib/RMagick.rb', line 46

def width
  @width
end

#xObject

Returns the value of attribute x.



46
47
48
# File 'lib/RMagick.rb', line 46

def x
  @x
end

#yObject

Returns the value of attribute y.



46
47
48
# File 'lib/RMagick.rb', line 46

def y
  @y
end

Class Method Details

.from_s(str) ⇒ Object

Raises:

  • (ArgumentError)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/RMagick.rb', line 75

def Geometry.from_s(str)
    raise(ArgumentError, "no geometry string specified") unless str

    m = RE.match(str)
    if m
        width  = m[1].to_i
        height = m[2].to_i
        x      = m[3].to_i
        y      = m[4].to_i
        flag   = RFLAGS[m[5]]
    else
        raise ArgumentError, "invalid geometry format"
    end
    Geometry.new(width, height, x, y, flag)
end

Instance Method Details

#to_sObject

Convert object to a geometry string



92
93
94
95
96
97
98
99
# File 'lib/RMagick.rb', line 92

def to_s
    str = ''
    str << sprintf("%g", @width) if @width > 0
    str << 'x' if (@width > 0 || @height > 0)
    str << sprintf("%g", @height) if @height > 0
    str << sprintf("%+d%+d", @x, @y) if (@x != 0 || @y != 0)
    str << FLAGS[@flag.to_i]
end