Class Magick::Geometry
In: lib/RMagick.rb
Parent: Object
Enum GeometryValue Stylable RVG\n[lib/rvg/clippath.rb\nlib/rvg/container.rb\nlib/rvg/deep_equal.rb\nlib/rvg/describable.rb\nlib/rvg/embellishable.rb\nlib/rvg/misc.rb\nlib/rvg/paint.rb\nlib/rvg/pathdata.rb\nlib/rvg/rvg.rb\nlib/rvg/stretchable.rb\nlib/rvg/stylable.rb\nlib/rvg/text.rb\nlib/rvg/transformable.rb\nlib/rvg/units.rb] Transformable Stretchable Embellishable Describable Duplicatable Comparable Image ImageList Enumerable Geometry OptionalMethodArguments HatchFill Draw lib/RMagick.rb lib/rvg/misc.rb ObjectData Application Pre_ObjectData_Descriptor Envelope Post_ObjectData_Descriptor IPTC Magick dot/m_14_0.png

Methods

from_s   new   to_s  

Constants

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/

Attributes

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

Public Class methods

[Source]

     # 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

[Source]

    # 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

Public Instance methods

Convert object to a geometry string

[Source]

     # 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

[Validate]