Class: Magick::Geometry

Inherits:
Object
  • Object
show all
Defined in:
lib/RMagick.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

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.

Raises:

  • (ArgumentError)

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/RMagick.rb', line 53

def initialize(width=nil, height=nil, x=nil, y=nil, flag=nil)
    raise(ArgumentError, "width set to #{width.to_s}") if width.is_a? GeometryValue
    raise(ArgumentError, "height set to #{height.to_s}") if height.is_a? GeometryValue
    raise(ArgumentError, "x set to #{x.to_s}") if x.is_a? GeometryValue
    raise(ArgumentError, "y set to #{y.to_s}") 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

#flagObject

Returns the value of attribute flag


51
52
53
# File 'lib/RMagick.rb', line 51

def flag
  @flag
end

#heightObject

Returns the value of attribute height


51
52
53
# File 'lib/RMagick.rb', line 51

def height
  @height
end

#widthObject

Returns the value of attribute width


51
52
53
# File 'lib/RMagick.rb', line 51

def width
  @width
end

#xObject

Returns the value of attribute x


51
52
53
# File 'lib/RMagick.rb', line 51

def x
  @x
end

#yObject

Returns the value of attribute y


51
52
53
# File 'lib/RMagick.rb', line 51

def y
  @y
end

Class Method Details

.from_s(str) ⇒ Object


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/RMagick.rb', line 89

def Geometry.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
    if str['%']
      flag = PercentGeometry
    end
    Geometry.new(width, height, x, y, flag)
end

Instance Method Details

#to_sObject

Convert object to a geometry string


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/RMagick.rb', line 108

def to_s
    str = ''
    if @width > 0
      fmt = @width.truncate == @width ? "%d" : "%.2f"
      str << sprintf(fmt, @width)
      str << '%' if @flag == PercentGeometry
    end

    if (@width > 0 && @flag != PercentGeometry) || (@height > 0)
      str << 'x'
    end

    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)
    if @flag != PercentGeometry
      str << FLAGS[@flag.to_i]
    end
    str
end