rmagick 2.15.3 → 2.15.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rmagick might be problematic. Click here for more details.

Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +14 -0
  3. data/.rubocop.yml +27 -3
  4. data/.travis.yml +9 -6
  5. data/CHANGELOG.md +4 -0
  6. data/CONTRIBUTING.md +10 -0
  7. data/README.textile +4 -0
  8. data/before_install_linux.sh +4 -4
  9. data/doc/ex/gravity.rb +2 -1
  10. data/ext/RMagick/extconf.rb +60 -17
  11. data/lib/rmagick/version.rb +1 -1
  12. data/lib/rvg/clippath.rb +37 -36
  13. data/lib/rvg/container.rb +106 -107
  14. data/lib/rvg/deep_equal.rb +46 -48
  15. data/lib/rvg/describable.rb +35 -36
  16. data/lib/rvg/embellishable.rb +384 -380
  17. data/lib/rvg/misc.rb +705 -690
  18. data/lib/rvg/paint.rb +39 -40
  19. data/lib/rvg/pathdata.rb +120 -121
  20. data/lib/rvg/rvg.rb +212 -209
  21. data/lib/rvg/stretchable.rb +159 -158
  22. data/lib/rvg/stylable.rb +99 -100
  23. data/lib/rvg/text.rb +0 -1
  24. data/lib/rvg/transformable.rb +110 -110
  25. data/lib/rvg/units.rb +58 -58
  26. data/rmagick.gemspec +1 -1
  27. data/spec/rmagick/image/blue_shift_spec.rb +16 -0
  28. data/spec/rmagick/image/composite_spec.rb +140 -0
  29. data/spec/rmagick/image/constitute_spec.rb +15 -0
  30. data/spec/rmagick/image/dispatch_spec.rb +18 -0
  31. data/spec/rmagick/image/from_blob_spec.rb +14 -0
  32. data/spec/rmagick/image/ping_spec.rb +14 -0
  33. data/spec/rmagick/image/properties_spec.rb +29 -0
  34. data/spec/spec_helper.rb +3 -0
  35. data/test/Image1.rb +524 -718
  36. data/test/Image2.rb +1262 -1262
  37. data/test/Image3.rb +973 -973
  38. data/test/ImageList2.rb +341 -341
  39. data/test/Image_attributes.rb +678 -678
  40. data/test/Info.rb +336 -336
  41. data/test/Magick.rb +245 -242
  42. data/test/Pixel.rb +105 -105
  43. data/test/Preview.rb +42 -42
  44. metadata +21 -6
@@ -1,54 +1,52 @@
1
1
  module Magick
2
- class RVG
3
- [PathData, Styles, Transforms].each do |c|
4
- c.class_eval do
5
- def deep_equal(other)
6
- if self != other
7
- puts "#{c.inspect} not equal.\nself:#{self} != other:#{other}"
8
- return false
9
- end
10
- true
11
- end
12
- end
2
+ class RVG
3
+ [PathData, Styles, Transforms].each do |c|
4
+ c.class_eval do
5
+ def deep_equal(other)
6
+ if self != other
7
+ puts "#{c.inspect} not equal.\nself:#{self} != other:#{other}"
8
+ return false
9
+ end
10
+ true
13
11
  end
12
+ end
13
+ end
14
14
 
15
- [Shape, TextBase, Image, Group, Content, Use, ClipPath, Pattern, self].each do |c|
16
- c.class_eval do
17
- def deep_equal(other)
18
- ivs = instance_variables
19
-
20
- ivs.each do |iv|
21
- itv = instance_variable_get(iv)
22
- otv = other.instance_variable_get(iv)
23
- if itv.respond_to?(:deep_equal)
24
- if itv.equal?(otv)
25
- puts "#{iv} has deep_equal but self.#{iv} and other.#{iv} are the same object."
26
- return false
27
- end
28
- unless itv.deep_equal(otv)
29
- puts "Not equal.\nself.#{iv}=#{itv.inspect}\nother.#{iv}=#{otv.inspect}"
30
- return false
31
- end
32
- else
33
- case itv
34
- when Float, Symbol, TrueClass, FalseClass, Fixnum, NilClass
35
- return false if itv != otv
36
- else
37
- if itv.equal?(otv)
38
- puts "#{iv} is dup-able but self.#{iv} and other.#{iv} are the same object."
39
- return false
40
- end
41
- if itv != otv
42
- puts "Not equal.\nself.#{iv}=#{itv.inspect}\nother.#{iv}=#{otv.inspect}"
43
- return false
44
- end
45
- end
46
- end
47
- end
48
-
49
- true
50
- end
15
+ [Shape, TextBase, Image, Group, Content, Use, ClipPath, Pattern, self].each do |c|
16
+ c.class_eval do
17
+ def deep_equal(other)
18
+ ivs = instance_variables
19
+ ivs.each do |iv|
20
+ itv = instance_variable_get(iv)
21
+ otv = other.instance_variable_get(iv)
22
+ if itv.respond_to?(:deep_equal)
23
+ if itv.equal?(otv)
24
+ puts "#{iv} has deep_equal but self.#{iv} and other.#{iv} are the same object."
25
+ return false
26
+ end
27
+ unless itv.deep_equal(otv)
28
+ puts "Not equal.\nself.#{iv}=#{itv.inspect}\nother.#{iv}=#{otv.inspect}"
29
+ return false
30
+ end
31
+ else
32
+ case itv
33
+ when Float, Symbol, TrueClass, FalseClass, Fixnum, NilClass
34
+ return false if itv != otv
35
+ else
36
+ if itv.equal?(otv)
37
+ puts "#{iv} is dup-able but self.#{iv} and other.#{iv} are the same object."
38
+ return false
39
+ end
40
+ if itv != otv
41
+ puts "Not equal.\nself.#{iv}=#{itv.inspect}\nother.#{iv}=#{otv.inspect}"
42
+ return false
43
+ end
44
+ end
51
45
  end
46
+ end
47
+ true
52
48
  end
53
- end # class RVG
49
+ end
50
+ end
51
+ end # class RVG
54
52
  end # module Magick
@@ -2,47 +2,46 @@
2
2
  # $Id: describable.rb,v 1.5 2009/02/28 23:52:13 rmagick Exp $
3
3
  # Copyright (C) 2009 Timothy P. Hunter
4
4
  #++
5
-
6
5
  module Magick
7
- class RVG
8
- #--
9
- # Corresponds to SVG's Description.class
10
- #++
11
- # This module defines a number of metadata attributes.
12
- module Describable
13
- private
6
+ class RVG
7
+ #--
8
+ # Corresponds to SVG's Description.class
9
+ #++
10
+ # This module defines a number of metadata attributes.
11
+ module Describable
12
+ private
14
13
 
15
- def initialize(*args, &block) #:nodoc:
16
- super
17
- @title, @desc, @metadata = nil
18
- end
14
+ def initialize(*args, &block) #:nodoc:
15
+ super
16
+ @title, @desc, @metadata = nil
17
+ end
19
18
 
20
- public
19
+ public
21
20
 
22
- # Sets the object description
23
- attr_writer :desc
24
- # Sets the object title
25
- attr_writer :title
26
- # Sets the object metadata
27
- attr_writer :metadata
21
+ # Sets the object description
22
+ attr_writer :desc
23
+ # Sets the object title
24
+ attr_writer :title
25
+ # Sets the object metadata
26
+ attr_writer :metadata
28
27
 
29
- # Returns the title of this object. The RVG object title is stored as
30
- # the 'title' property on the image
31
- def title
32
- @title.to_s
33
- end
28
+ # Returns the title of this object. The RVG object title is stored as
29
+ # the 'title' property on the image
30
+ def title
31
+ @title.to_s
32
+ end
34
33
 
35
- # Returns the description of this object. The RVG object description is
36
- # stored as the 'desc' property on the image
37
- def desc
38
- @desc.to_s
39
- end
34
+ # Returns the description of this object. The RVG object description is
35
+ # stored as the 'desc' property on the image
36
+ def desc
37
+ @desc.to_s
38
+ end
40
39
 
41
- # Returns additional metadata of this object. The RVG object metadata
42
- # are stored as the 'metadata' property on the image
43
- def metadata
44
- @metadata.to_s
45
- end
46
- end # module Describable
47
- end # class RVG
40
+ # Returns additional metadata of this object. The RVG object metadata
41
+ # are stored as the 'metadata' property on the image
42
+ def metadata
43
+ @metadata.to_s
44
+ end
45
+ end # module Describable
46
+ end # class RVG
48
47
  end # module Magick
@@ -2,386 +2,390 @@
2
2
  # $Id: embellishable.rb,v 1.9 2009/02/28 23:52:13 rmagick Exp $
3
3
  # Copyright (C) 2009 Timothy P. Hunter
4
4
  #++
5
-
6
5
  module Magick
7
- class RVG
8
- # Parent class of Circle, Ellipse, Text, etc.
9
- class Shape #:nodoc:
10
- include Stylable
11
- include Transformable
12
- include Duplicatable
13
-
14
- # Each shape can have its own set of transforms and styles.
15
- def add_primitives(gc)
16
- gc.push
17
- add_transform_primitives(gc)
18
- add_style_primitives(gc)
19
- gc.__send__(@primitive, *@args)
20
- gc.pop
21
- end
22
- end # class Shape
23
-
24
- class Circle < Shape
25
- # Define a circle with radius +r+ and centered at [<tt>cx</tt>, <tt>cy</tt>].
26
- # Use the RVG::ShapeConstructors#circle method to create Circle objects in a container.
27
- def initialize(r, cx=0, cy=0)
28
- super()
29
- r, cx, cy = Magick::RVG.convert_to_float(r, cx, cy)
30
- if r < 0
31
- fail ArgumentError, "radius must be >= 0 (#{r} given)"
32
- end
33
- @primitive = :circle
34
- @args = [cx, cy, cx+r, cy]
35
- self
36
- end
37
- end # class Circle
38
-
39
- class Ellipse < Shape
40
- # Define an ellipse with a center at [<tt>cx</tt>, <tt>cy</tt>], a horizontal radius +rx+
41
- # and a vertical radius +ry+.
42
- # Use the RVG::ShapeConstructors#ellipse method to create Ellipse objects in a container.
43
- def initialize(rx, ry, cx=0, cy=0)
44
- super()
45
- rx, ry, cx, cy = Magick::RVG.convert_to_float(rx, ry, cx, cy)
46
- if rx < 0 || ry < 0
47
- fail ArgumentError, "radii must be >= 0 (#{rx}, #{ry} given)"
48
- end
49
- @primitive = :ellipse
50
- # Ellipses are always complete.
51
- @args = [cx, cy, rx, ry, 0, 360]
52
- end
53
- end # class Ellipse
54
-
55
- class Line < Shape
56
- # Define a line from [<tt>x1</tt>, <tt>y1</tt>] to [<tt>x2</tt>, <tt>y2</tt>].
57
- # Use the RVG::ShapeConstructors#line method to create Line objects in a container.
58
- def initialize(x1=0, y1=0, x2=0, y2=0)
59
- super()
60
- @primitive = :line
61
- @args = [x1, y1, x2, y2]
62
- end
63
- end # class Line
64
-
65
- class Path < Shape
66
- # Define an SVG path. The argument can be either a path string
67
- # or a PathData object.
68
- # Use the RVG::ShapeConstructors#path method to create Path objects in a container.
69
- def initialize(path)
70
- super()
71
- @primitive = :path
72
- @args = [path.to_s]
73
- end
74
- end # class Path
75
-
76
- class Rect < Shape
77
- # Define a width x height rectangle. The upper-left corner is at [<tt>x</tt>, <tt>y</tt>].
78
- # If either <tt>width</tt> or <tt>height</tt> is 0, the rectangle is not rendered.
79
- # Use the RVG::ShapeConstructors#rect method to create Rect objects in a container.
80
- def initialize(width, height, x=0, y=0)
81
- super()
82
- width, height, x, y = Magick::RVG.convert_to_float(width, height, x, y)
83
- if width < 0 || height < 0
84
- fail ArgumentError, "width, height must be >= 0 (#{width}, #{height} given)"
85
- end
86
- @args = [x, y, x+width, y+height]
87
- @primitive = :rectangle
88
- end
89
-
90
- # Specify optional rounded corners for a rectangle. The arguments
91
- # are the x- and y-axis radii. If y is omitted it defaults to x.
92
- def round(rx, ry=nil)
93
- rx, ry = Magick::RVG.convert_to_float(rx, ry || rx)
94
- if rx < 0 || ry < 0
95
- fail ArgumentError, "rx, ry must be >= 0 (#{rx}, #{ry} given)"
96
- end
97
- @args << rx << ry
98
- @primitive = :roundrectangle
99
- self
100
- end
101
- end # class Rect
102
-
103
- class PolyShape < Shape
104
- def polypoints(points)
105
- case points.length
106
- when 1
107
- points = Array(points[0])
108
- when 2
109
- x_coords = Array(points[0])
110
- y_coords = Array(points[1])
111
- unless x_coords.length > 0 && y_coords.length > 0
112
- fail ArgumentError, 'array arguments must contain at least one point'
113
- end
114
- n = x_coords.length - y_coords.length
115
- short = n > 0 ? y_coords : x_coords
116
- olen = short.length
117
- n.abs.times {|x| short << short[x % olen]}
118
- points = x_coords.zip(y_coords).flatten
119
- end
120
- n = points.length
121
- if n < 4 || n.odd?
122
- fail ArgumentError, "insufficient/odd number of points specified: #{n}"
123
- end
124
- Magick::RVG.convert_to_float(*points)
125
- end
126
- end # class PolyShape
127
-
128
- class Polygon < PolyShape
129
- # Draws a polygon. The arguments are [<tt>x</tt>, <tt>y</tt>] pairs that
130
- # define the points that make up the polygon. At least two
131
- # points must be specified. If the last point is not the
132
- # same as the first, adds an additional point to close
133
- # the polygon.
134
- # Use the RVG::ShapeConstructors#polygon method to create Polygon objects in a container.
135
- def initialize(*points)
136
- super()
137
- @primitive = :polygon
138
- @args = polypoints(points)
139
- end
140
- end # class Polygon
141
-
142
- class Polyline < PolyShape
143
- # Draws a polyline. The arguments are [<tt>x</tt>, <tt>y</tt>] pairs that
144
- # define the points that make up the polyline. At least two
145
- # points must be specified.
146
- # Use the RVG::ShapeConstructors#polyline method to create Polyline objects in a container.
147
- def initialize(*points)
148
- super()
149
- points = polypoints(points)
150
- @primitive = :polyline
151
- @args = Magick::RVG.convert_to_float(*points)
152
- end
153
- end # class Polyline
154
-
155
- class Image
156
- include Stylable
157
- include Transformable
158
- include Describable
159
- include PreserveAspectRatio
160
- include Duplicatable
161
-
162
- private
163
-
164
- def align_to_viewport(scale)
165
- tx = case @align
166
- when 'none', /\AxMin/
167
- 0
168
- when NilClass, /\AxMid/
169
- (@width - @image.columns*scale) / 2.0
170
- when /\AxMax/
171
- @width - @image.columns*scale
172
- end
173
-
174
- ty = case @align
175
- when 'none', /YMin\z/
176
- 0
177
- when NilClass, /YMid\z/
178
- (@height - @image.rows*scale) / 2.0
179
- when /YMax\z/
180
- @height - @image.rows*scale
181
- end
182
- [tx, ty]
183
- end
184
-
185
- def add_composite_primitive(gc)
186
- if @align == 'none'
187
- # Let RMagick do the scaling
188
- scale = 1.0
189
- width, height = @width, @height
190
- elsif @meet_or_slice == 'meet'
191
- scale = [@width/@image.columns, @height/@image.rows].min
192
- width, height = @image.columns, @image.rows
193
- else
194
- # Establish clipping path around the current viewport
195
- name = __id__.to_s
196
- gc.define_clip_path(name) do
197
- gc.path("M#{@x},#{@y} l#{@width},0 l0,#{@height} l-#{@width},0 l0,-#{@height}z")
198
- end
199
-
200
- gc.clip_path(name)
201
- scale = [@width/@image.columns, @height/@image.rows].max
202
- width, height = @image.columns, @image.rows
203
- end
204
- tx, ty = align_to_viewport(scale)
205
- gc.composite(@x+tx, @y+ty, width*scale, height*scale, @image)
206
- end
207
-
208
- def init_viewbox
209
- @align = nil
210
- @vbx_width, @vbx_height = @image.columns, @image.rows
211
- @meet_or_slice = 'meet'
212
- end
213
-
214
- public
215
-
216
- # Composite a raster image in the viewport defined by [x,y] and
217
- # +width+ and +height+.
218
- # Use the RVG::ImageConstructors#image method to create Text objects in a container.
219
- def initialize(image, width=nil, height=nil, x=0, y=0)
220
- super() # run module initializers
221
- @image = image.copy # use a copy of the image in case app. re-uses the argument
222
- @x, @y, @width, @height = Magick::RVG.convert_to_float(x, y, width || @image.columns, height || @image.rows)
223
- if @width < 0 || @height < 0
224
- fail ArgumentError, 'width, height must be >= 0'
225
- end
226
- init_viewbox
227
- end
228
-
229
- def add_primitives(gc) #:nodoc:
230
- # Do not render if width or height is 0
231
- return if @width == 0 || @height == 0
232
- gc.push
233
- add_transform_primitives(gc)
234
- add_style_primitives(gc)
235
- add_composite_primitive(gc)
236
- gc.pop
237
- end
238
- end # class Image
239
-
240
- # Methods that construct basic shapes within a container
241
- module ShapeConstructors
242
- # Draws a circle whose center is [<tt>cx</tt>, <tt>cy</tt>] and radius is +r+.
243
- def circle(r, cx=0, cy=0)
244
- circle = Circle.new(r, cx, cy)
245
- @content << circle
246
- circle
247
- end
248
-
249
- # Draws an ellipse whose center is [<tt>cx</tt>, <tt>cy</tt>] and having
250
- # a horizontal radius +rx+ and vertical radius +ry+.
251
- def ellipse(rx, ry, cx=0, cy=0)
252
- ellipse = Ellipse.new(rx, ry, cx, cy)
253
- @content << ellipse
254
- ellipse
255
- end
256
-
257
- # Draws a line from [<tt>x1</tt>, <tt>y1</tt>] to [<tt>x2</tt>, <tt>y2</tt>].
258
- def line(x1=0, y1=0, x2=0, y2=0)
259
- line = Line.new(x1, y1, x2, y2)
260
- @content << line
261
- line
262
- end
263
-
264
- # Draws a path defined by an SVG path string or a PathData
265
- # object.
266
- def path(path)
267
- path = Path.new(path)
268
- @content << path
269
- path
270
- end
271
-
272
- # Draws a rectangle whose upper-left corner is [<tt>x</tt>, <tt>y</tt>] and
273
- # with the specified +width+ and +height+. Unless otherwise
274
- # specified the rectangle has square corners. Returns a
275
- # Rectangle object.
276
- #
277
- # Draw a rectangle with rounded corners by calling the #round
278
- # method on the Rectangle object. <tt>rx</tt> and <tt>ry</tt> are
279
- # the corner radii in the x- and y-directions. For example:
280
- # canvas.rect(width, height, x, y).round(8, 6)
281
- # If <tt>ry</tt> is omitted it defaults to <tt>rx</tt>.
282
- def rect(width, height, x=0, y=0)
283
- rect = Rect.new(width, height, x, y)
284
- @content << rect
285
- rect
286
- end
287
-
288
- # Draws a polygon. The arguments are [<tt>x</tt>, <tt>y</tt>] pairs that
289
- # define the points that make up the polygon. At least two
290
- # points must be specified. If the last point is not the
291
- # same as the first, adds an additional point to close
292
- # the polygon.
293
- def polygon(*points)
294
- polygon = Polygon.new(*points)
295
- @content << polygon
296
- polygon
297
- end
298
-
299
- # Draws a polyline. The arguments are [<tt>x</tt>, <tt>y</tt>] pairs that
300
- # define the points that make up the polyline. At least two
301
- # points must be specified.
302
- def polyline(*points)
303
- polyline = Polyline.new(*points)
304
- @content << polyline
305
- polyline
306
- end
307
- end # module ShapeContent
308
-
309
- # Methods that reference ("use") other drawable objects within a container
310
- module UseConstructors
311
- # Reference an object to be inserted into the container's
312
- # content. [<tt>x</tt>,<tt>y</tt>] is the offset from the upper-left
313
- # corner. If the argument is an RVG or Image object and +width+ and +height+
314
- # are specified, these values will override the +width+ and +height+
315
- # attributes on the argument.
316
- def use(obj, x=0, y=0, width=nil, height=nil)
317
- use = Use.new(obj, x, y, width, height)
318
- @content << use
319
- use
320
- end
321
- end # module UseConstructors
322
-
323
- # Methods that construct container objects within a container
324
- module StructureConstructors
325
- # Establishes a new viewport. [<tt>x</tt>, <tt>y</tt>] is the coordinate of the
326
- # upper-left corner within the containing viewport. This is a
327
- # _container_ method. Styles and
328
- # transforms specified on this object will be used by objects
329
- # contained within, unless overridden by an inner container or
330
- # the contained object itself.
331
- def rvg(cols, rows, x=0, y=0, &block)
332
- rvg = Magick::RVG.new(cols, rows, &block)
333
- begin
334
- x, y = Float(x), Float(y)
335
- rescue ArgumentError
336
- args = [cols, rows, x, y]
337
- raise ArgumentError, "at least one argument is not convertable to Float (got #{args.collect {|a| a.class}.join(', ')})"
338
- end
339
- rvg.corner(x, y)
340
- @content << rvg
341
- rvg
342
- end
343
-
344
- # Defines a group.
345
- #
346
- # This method constructs a new
347
- # Group _container_ object. The styles and
348
- # transforms specified on this object will be used by objects
349
- # contained within, unless overridden by an inner container or
350
- # the contained object itself.
351
- # Define grouped elements by calling RVG::Embellishable
352
- # methods within the associated block.
353
- def g(&block)
354
- group = Group.new(&block)
355
- @content << group
356
- group
357
- end
358
- end # module StructureConstructors
359
-
360
- # Methods that construct raster image objects within a container
361
- module ImageConstructors
362
- # Composite a raster image at [<tt>x</tt>,<tt>y</tt>]
363
- # in a viewport of the specified <tt>width</tt> and <tt>height</tt>.
364
- # If not specified, the width and height are the width and height
365
- # of the image. Use the RVG::PreserveAspectRatio#preserve_aspect_ratio method to
366
- # control the placement and scaling of the image within the
367
- # viewport. By default, the image is scaled to fit inside the
368
- # viewport and centered within the viewport.
369
- def image(image, width=nil, height=nil, x=0, y=0)
370
- img = Image.new(image, width, height, x, y)
371
- @content << img
372
- img
6
+ class RVG
7
+ # Parent class of Circle, Ellipse, Text, etc.
8
+ class Shape #:nodoc:
9
+ include Stylable
10
+ include Transformable
11
+ include Duplicatable
12
+
13
+ # Each shape can have its own set of transforms and styles.
14
+ def add_primitives(gc)
15
+ gc.push
16
+ add_transform_primitives(gc)
17
+ add_style_primitives(gc)
18
+ gc.__send__(@primitive, *@args)
19
+ gc.pop
20
+ end
21
+ end # class Shape
22
+
23
+ class Circle < Shape
24
+ # Define a circle with radius +r+ and centered at [<tt>cx</tt>, <tt>cy</tt>].
25
+ # Use the RVG::ShapeConstructors#circle method to create Circle objects in a container.
26
+ def initialize(r, cx = 0, cy = 0)
27
+ super()
28
+ r, cx, cy = Magick::RVG.convert_to_float(r, cx, cy)
29
+ if r < 0
30
+ fail ArgumentError, "radius must be >= 0 (#{r} given)"
31
+ end
32
+ @primitive = :circle
33
+ @args = [cx, cy, cx+r, cy]
34
+ self
35
+ end
36
+ end # class Circle
37
+
38
+ class Ellipse < Shape
39
+ # Define an ellipse with a center at [<tt>cx</tt>, <tt>cy</tt>], a horizontal radius +rx+
40
+ # and a vertical radius +ry+.
41
+ # Use the RVG::ShapeConstructors#ellipse method to create Ellipse objects in a container.
42
+ def initialize(rx, ry, cx = 0, cy = 0)
43
+ super()
44
+ rx, ry, cx, cy = Magick::RVG.convert_to_float(rx, ry, cx, cy)
45
+ if rx < 0 || ry < 0
46
+ fail ArgumentError, "radii must be >= 0 (#{rx}, #{ry} given)"
47
+ end
48
+ @primitive = :ellipse
49
+ # Ellipses are always complete.
50
+ @args = [cx, cy, rx, ry, 0, 360]
51
+ end
52
+ end # class Ellipse
53
+
54
+ class Line < Shape
55
+ # Define a line from [<tt>x1</tt>, <tt>y1</tt>] to [<tt>x2</tt>, <tt>y2</tt>].
56
+ # Use the RVG::ShapeConstructors#line method to create Line objects in a container.
57
+ def initialize(x1 = 0, y1 = 0, x2 = 0, y2 = 0)
58
+ super()
59
+ @primitive = :line
60
+ @args = [x1, y1, x2, y2]
61
+ end
62
+ end # class Line
63
+
64
+ class Path < Shape
65
+ # Define an SVG path. The argument can be either a path string
66
+ # or a PathData object.
67
+ # Use the RVG::ShapeConstructors#path method to create Path objects in a container.
68
+ def initialize(path)
69
+ super()
70
+ @primitive = :path
71
+ @args = [path.to_s]
72
+ end
73
+ end # class Path
74
+
75
+ class Rect < Shape
76
+ # Define a width x height rectangle. The upper-left corner is at [<tt>x</tt>, <tt>y</tt>].
77
+ # If either <tt>width</tt> or <tt>height</tt> is 0, the rectangle is not rendered.
78
+ # Use the RVG::ShapeConstructors#rect method to create Rect objects in a container.
79
+ def initialize(width, height, x = 0, y = 0)
80
+ super()
81
+ width, height, x, y = Magick::RVG.convert_to_float(width, height, x, y)
82
+ if width < 0 || height < 0
83
+ fail ArgumentError, "width, height must be >= 0 (#{width}, #{height} given)"
84
+ end
85
+ @args = [x, y, x+width, y+height]
86
+ @primitive = :rectangle
87
+ end
88
+
89
+ # Specify optional rounded corners for a rectangle. The arguments
90
+ # are the x- and y-axis radii. If y is omitted it defaults to x.
91
+ def round(rx, ry = nil)
92
+ rx, ry = Magick::RVG.convert_to_float(rx, ry || rx)
93
+ if rx < 0 || ry < 0
94
+ fail ArgumentError, "rx, ry must be >= 0 (#{rx}, #{ry} given)"
95
+ end
96
+ @args << rx << ry
97
+ @primitive = :roundrectangle
98
+ self
99
+ end
100
+ end # class Rect
101
+
102
+ class PolyShape < Shape
103
+ def polypoints(points)
104
+ case points.length
105
+ when 1
106
+ points = Array(points[0])
107
+ when 2
108
+ x_coords = Array(points[0])
109
+ y_coords = Array(points[1])
110
+ unless x_coords.length > 0 && y_coords.length > 0
111
+ fail ArgumentError, 'array arguments must contain at least one point'
373
112
  end
374
- end # module ImageConstructors
375
-
376
- # Methods that create shapes, text, and other drawable objects
377
- # within container objects such as ::Magick::RVG and
378
- # ::Magick::RVG::Group
379
- module Embellishable
380
- include StructureConstructors
381
- include ShapeConstructors
382
- include TextConstructors
383
- include UseConstructors
384
- include ImageConstructors
385
- end # module Embellishable
386
- end # class RVG
113
+ n = x_coords.length - y_coords.length
114
+ short = n > 0 ? y_coords : x_coords
115
+ olen = short.length
116
+ n.abs.times {|x| short << short[x % olen]}
117
+ points = x_coords.zip(y_coords).flatten
118
+ end
119
+ n = points.length
120
+ if n < 4 || n.odd?
121
+ fail ArgumentError, "insufficient/odd number of points specified: #{n}"
122
+ end
123
+ Magick::RVG.convert_to_float(*points)
124
+ end
125
+ end # class PolyShape
126
+
127
+ class Polygon < PolyShape
128
+ # Draws a polygon. The arguments are [<tt>x</tt>, <tt>y</tt>] pairs that
129
+ # define the points that make up the polygon. At least two
130
+ # points must be specified. If the last point is not the
131
+ # same as the first, adds an additional point to close
132
+ # the polygon.
133
+ # Use the RVG::ShapeConstructors#polygon method to create Polygon objects in a container.
134
+ def initialize(*points)
135
+ super()
136
+ @primitive = :polygon
137
+ @args = polypoints(points)
138
+ end
139
+ end # class Polygon
140
+
141
+ class Polyline < PolyShape
142
+ # Draws a polyline. The arguments are [<tt>x</tt>, <tt>y</tt>] pairs that
143
+ # define the points that make up the polyline. At least two
144
+ # points must be specified.
145
+ # Use the RVG::ShapeConstructors#polyline method to create Polyline objects in a container.
146
+ def initialize(*points)
147
+ super()
148
+ points = polypoints(points)
149
+ @primitive = :polyline
150
+ @args = Magick::RVG.convert_to_float(*points)
151
+ end
152
+ end # class Polyline
153
+
154
+ class Image
155
+ include Stylable
156
+ include Transformable
157
+ include Describable
158
+ include PreserveAspectRatio
159
+ include Duplicatable
160
+
161
+ private
162
+
163
+ def align_to_viewport(scale)
164
+ tx = case @align
165
+ when 'none', /\AxMin/
166
+ 0
167
+ when NilClass, /\AxMid/
168
+ (@width - @image.columns*scale) / 2.0
169
+ when /\AxMax/
170
+ @width - @image.columns*scale
171
+ end
172
+
173
+ ty = case @align
174
+ when 'none', /YMin\z/
175
+ 0
176
+ when NilClass, /YMid\z/
177
+ (@height - @image.rows*scale) / 2.0
178
+ when /YMax\z/
179
+ @height - @image.rows*scale
180
+ end
181
+ [tx, ty]
182
+ end
183
+
184
+ def add_composite_primitive(gc)
185
+ if @align == 'none'
186
+ # Let RMagick do the scaling
187
+ scale = 1.0
188
+ width = @width
189
+ height = @height
190
+ elsif @meet_or_slice == 'meet'
191
+ scale = [@width/@image.columns, @height/@image.rows].min
192
+ width = @image.columns
193
+ height = @image.rows
194
+ else
195
+ # Establish clipping path around the current viewport
196
+ name = __id__.to_s
197
+ gc.define_clip_path(name) do
198
+ gc.path("M#{@x},#{@y} l#{@width},0 l0,#{@height} l-#{@width},0 l0,-#{@height}z")
199
+ end
200
+
201
+ gc.clip_path(name)
202
+ scale = [@width/@image.columns, @height/@image.rows].max
203
+ width = @image.columns
204
+ height = @image.rows
205
+ end
206
+ tx, ty = align_to_viewport(scale)
207
+ gc.composite(@x+tx, @y+ty, width*scale, height*scale, @image)
208
+ end
209
+
210
+ def init_viewbox
211
+ @align = nil
212
+ @vbx_width = @image.columns
213
+ @vbx_height = @image.rows
214
+ @meet_or_slice = 'meet'
215
+ end
216
+
217
+ public
218
+
219
+ # Composite a raster image in the viewport defined by [x,y] and
220
+ # +width+ and +height+.
221
+ # Use the RVG::ImageConstructors#image method to create Text objects in a container.
222
+ def initialize(image, width = nil, height = nil, x = 0, y = 0)
223
+ super() # run module initializers
224
+ @image = image.copy # use a copy of the image in case app. re-uses the argument
225
+ @x, @y, @width, @height = Magick::RVG.convert_to_float(x, y, width || @image.columns, height || @image.rows)
226
+ if @width < 0 || @height < 0
227
+ fail ArgumentError, 'width, height must be >= 0'
228
+ end
229
+ init_viewbox
230
+ end
231
+
232
+ def add_primitives(gc) #:nodoc:
233
+ # Do not render if width or height is 0
234
+ return if @width == 0 || @height == 0
235
+ gc.push
236
+ add_transform_primitives(gc)
237
+ add_style_primitives(gc)
238
+ add_composite_primitive(gc)
239
+ gc.pop
240
+ end
241
+ end # class Image
242
+
243
+ # Methods that construct basic shapes within a container
244
+ module ShapeConstructors
245
+ # Draws a circle whose center is [<tt>cx</tt>, <tt>cy</tt>] and radius is +r+.
246
+ def circle(r, cx = 0, cy = 0)
247
+ circle = Circle.new(r, cx, cy)
248
+ @content << circle
249
+ circle
250
+ end
251
+
252
+ # Draws an ellipse whose center is [<tt>cx</tt>, <tt>cy</tt>] and having
253
+ # a horizontal radius +rx+ and vertical radius +ry+.
254
+ def ellipse(rx, ry, cx = 0, cy = 0)
255
+ ellipse = Ellipse.new(rx, ry, cx, cy)
256
+ @content << ellipse
257
+ ellipse
258
+ end
259
+
260
+ # Draws a line from [<tt>x1</tt>, <tt>y1</tt>] to [<tt>x2</tt>, <tt>y2</tt>].
261
+ def line(x1 = 0, y1 = 0, x2 = 0, y2 = 0)
262
+ line = Line.new(x1, y1, x2, y2)
263
+ @content << line
264
+ line
265
+ end
266
+
267
+ # Draws a path defined by an SVG path string or a PathData
268
+ # object.
269
+ def path(path)
270
+ path = Path.new(path)
271
+ @content << path
272
+ path
273
+ end
274
+
275
+ # Draws a rectangle whose upper-left corner is [<tt>x</tt>, <tt>y</tt>] and
276
+ # with the specified +width+ and +height+. Unless otherwise
277
+ # specified the rectangle has square corners. Returns a
278
+ # Rectangle object.
279
+ #
280
+ # Draw a rectangle with rounded corners by calling the #round
281
+ # method on the Rectangle object. <tt>rx</tt> and <tt>ry</tt> are
282
+ # the corner radii in the x- and y-directions. For example:
283
+ # canvas.rect(width, height, x, y).round(8, 6)
284
+ # If <tt>ry</tt> is omitted it defaults to <tt>rx</tt>.
285
+ def rect(width, height, x = 0, y = 0)
286
+ rect = Rect.new(width, height, x, y)
287
+ @content << rect
288
+ rect
289
+ end
290
+
291
+ # Draws a polygon. The arguments are [<tt>x</tt>, <tt>y</tt>] pairs that
292
+ # define the points that make up the polygon. At least two
293
+ # points must be specified. If the last point is not the
294
+ # same as the first, adds an additional point to close
295
+ # the polygon.
296
+ def polygon(*points)
297
+ polygon = Polygon.new(*points)
298
+ @content << polygon
299
+ polygon
300
+ end
301
+
302
+ # Draws a polyline. The arguments are [<tt>x</tt>, <tt>y</tt>] pairs that
303
+ # define the points that make up the polyline. At least two
304
+ # points must be specified.
305
+ def polyline(*points)
306
+ polyline = Polyline.new(*points)
307
+ @content << polyline
308
+ polyline
309
+ end
310
+ end # module ShapeContent
311
+
312
+ # Methods that reference ("use") other drawable objects within a container
313
+ module UseConstructors
314
+ # Reference an object to be inserted into the container's
315
+ # content. [<tt>x</tt>,<tt>y</tt>] is the offset from the upper-left
316
+ # corner. If the argument is an RVG or Image object and +width+ and +height+
317
+ # are specified, these values will override the +width+ and +height+
318
+ # attributes on the argument.
319
+ def use(obj, x = 0, y = 0, width = nil, height = nil)
320
+ use = Use.new(obj, x, y, width, height)
321
+ @content << use
322
+ use
323
+ end
324
+ end # module UseConstructors
325
+
326
+ # Methods that construct container objects within a container
327
+ module StructureConstructors
328
+ # Establishes a new viewport. [<tt>x</tt>, <tt>y</tt>] is the coordinate of the
329
+ # upper-left corner within the containing viewport. This is a
330
+ # _container_ method. Styles and
331
+ # transforms specified on this object will be used by objects
332
+ # contained within, unless overridden by an inner container or
333
+ # the contained object itself.
334
+ def rvg(cols, rows, x = 0, y = 0, &block)
335
+ rvg = Magick::RVG.new(cols, rows, &block)
336
+ begin
337
+ x = Float(x)
338
+ y = Float(y)
339
+ rescue ArgumentError
340
+ args = [cols, rows, x, y]
341
+ raise ArgumentError, "at least one argument is not convertable to Float (got #{args.collect {|a| a.class}.join(', ')})"
342
+ end
343
+ rvg.corner(x, y)
344
+ @content << rvg
345
+ rvg
346
+ end
347
+
348
+ # Defines a group.
349
+ #
350
+ # This method constructs a new
351
+ # Group _container_ object. The styles and
352
+ # transforms specified on this object will be used by objects
353
+ # contained within, unless overridden by an inner container or
354
+ # the contained object itself.
355
+ # Define grouped elements by calling RVG::Embellishable
356
+ # methods within the associated block.
357
+ def g(&block)
358
+ group = Group.new(&block)
359
+ @content << group
360
+ group
361
+ end
362
+ end # module StructureConstructors
363
+
364
+ # Methods that construct raster image objects within a container
365
+ module ImageConstructors
366
+ # Composite a raster image at [<tt>x</tt>,<tt>y</tt>]
367
+ # in a viewport of the specified <tt>width</tt> and <tt>height</tt>.
368
+ # If not specified, the width and height are the width and height
369
+ # of the image. Use the RVG::PreserveAspectRatio#preserve_aspect_ratio method to
370
+ # control the placement and scaling of the image within the
371
+ # viewport. By default, the image is scaled to fit inside the
372
+ # viewport and centered within the viewport.
373
+ def image(image, width = nil, height = nil, x = 0, y = 0)
374
+ img = Image.new(image, width, height, x, y)
375
+ @content << img
376
+ img
377
+ end
378
+ end # module ImageConstructors
379
+
380
+ # Methods that create shapes, text, and other drawable objects
381
+ # within container objects such as ::Magick::RVG and
382
+ # ::Magick::RVG::Group
383
+ module Embellishable
384
+ include StructureConstructors
385
+ include ShapeConstructors
386
+ include TextConstructors
387
+ include UseConstructors
388
+ include ImageConstructors
389
+ end # module Embellishable
390
+ end # class RVG
387
391
  end # module Magick