| Class | Magick::Geometry |
| In: |
lib/RMagick.rb
|
| Parent: | Object |
| 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/ |
| flag | [RW] | |
| height | [RW] | |
| width | [RW] | |
| x | [RW] | |
| y | [RW] |
# File lib/RMagick.rb, line 98
98: def Geometry.from_s(str)
99:
100: m = RE.match(str)
101: if m
102: width = (m[1] || m[2]).to_f
103: height = (m[3] || m[4]).to_f
104: x = m[5].to_i
105: y = m[6].to_i
106: flag = RFLAGS[m[7]]
107: else
108: Kernel.raise ArgumentError, "invalid geometry format"
109: end
110: if str['%']
111: flag = PercentGeometry
112: end
113: Geometry.new(width, height, x, y, flag)
114: end
# File lib/RMagick.rb, line 62
62: def initialize(width=nil, height=nil, x=nil, y=nil, flag=nil)
63: raise(ArgumentError, "width set to #{width.to_s}") if width.is_a? GeometryValue
64: raise(ArgumentError, "height set to #{height.to_s}") if height.is_a? GeometryValue
65: raise(ArgumentError, "x set to #{x.to_s}") if x.is_a? GeometryValue
66: raise(ArgumentError, "y set to #{y.to_s}") if y.is_a? GeometryValue
67:
68: # Support floating-point width and height arguments so Geometry
69: # objects can be used to specify Image#density= arguments.
70: if width == nil
71: @width = 0
72: elsif width.to_f >= 0.0
73: @width = width.to_f
74: else
75: Kernel.raise ArgumentError, "width must be >= 0: #{width}"
76: end
77: if height == nil
78: @height = 0
79: elsif height.to_f >= 0.0
80: @height = height.to_f
81: else
82: Kernel.raise ArgumentError, "height must be >= 0: #{height}"
83: end
84:
85: @x = x.to_i
86: @y = y.to_i
87: @flag = flag
88:
89: end
Convert object to a geometry string
# File lib/RMagick.rb, line 117
117: def to_s
118: str = ''
119: if @width > 0
120: fmt = @width.truncate == @width ? "%d" : "%.2f"
121: str << sprintf(fmt, @width)
122: str << '%' if @flag == PercentGeometry
123: end
124:
125: if (@width > 0 && @flag != PercentGeometry) || (@height > 0)
126: str << 'x'
127: end
128:
129: if @height > 0
130: fmt = @height.truncate == @height ? "%d" : "%.2f"
131: str << sprintf(fmt, @height)
132: str << '%' if @flag == PercentGeometry
133: end
134: str << sprintf("%+d%+d", @x, @y) if (@x != 0 || @y != 0)
135: if @flag != PercentGeometry
136: str << FLAGS[@flag.to_i]
137: end
138: str
139: end