chunky_png 1.3.9 → 1.3.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +35 -0
- data/.standard.yml +16 -0
- data/.yardopts +1 -1
- data/CHANGELOG.rdoc +12 -4
- data/CONTRIBUTING.rdoc +17 -8
- data/Gemfile +15 -3
- data/LICENSE +1 -1
- data/README.md +15 -9
- data/Rakefile +5 -3
- data/benchmarks/decoding_benchmark.rb +17 -17
- data/benchmarks/encoding_benchmark.rb +22 -19
- data/benchmarks/filesize_benchmark.rb +6 -6
- data/bin/rake +29 -0
- data/bin/standardrb +29 -0
- data/chunky_png.gemspec +21 -15
- data/docs/.gitignore +3 -0
- data/docs/CNAME +1 -0
- data/docs/_config.yml +9 -0
- data/docs/_posts/2010-01-14-memory-efficiency-when-using-ruby.md +136 -0
- data/docs/_posts/2010-01-17-ode-to-array-pack-and-string-unpack.md +82 -0
- data/docs/_posts/2014-11-07-the-value-of-a-pure-ruby-library.md +61 -0
- data/docs/index.md +88 -0
- data/lib/chunky_png.rb +17 -30
- data/lib/chunky_png/canvas.rb +31 -28
- data/lib/chunky_png/canvas/adam7_interlacing.rb +16 -10
- data/lib/chunky_png/canvas/data_url_exporting.rb +3 -3
- data/lib/chunky_png/canvas/data_url_importing.rb +3 -3
- data/lib/chunky_png/canvas/drawing.rb +30 -43
- data/lib/chunky_png/canvas/masking.rb +14 -14
- data/lib/chunky_png/canvas/operations.rb +28 -24
- data/lib/chunky_png/canvas/png_decoding.rb +39 -33
- data/lib/chunky_png/canvas/png_encoding.rb +111 -103
- data/lib/chunky_png/canvas/resampling.rb +27 -32
- data/lib/chunky_png/canvas/stream_exporting.rb +8 -8
- data/lib/chunky_png/canvas/stream_importing.rb +8 -8
- data/lib/chunky_png/chunk.rb +101 -66
- data/lib/chunky_png/color.rb +218 -212
- data/lib/chunky_png/datastream.rb +24 -30
- data/lib/chunky_png/dimension.rb +18 -11
- data/lib/chunky_png/image.rb +11 -11
- data/lib/chunky_png/palette.rb +6 -9
- data/lib/chunky_png/point.rb +27 -26
- data/lib/chunky_png/rmagick.rb +10 -10
- data/lib/chunky_png/vector.rb +28 -29
- data/lib/chunky_png/version.rb +3 -1
- data/spec/chunky_png/canvas/adam7_interlacing_spec.rb +20 -21
- data/spec/chunky_png/canvas/data_url_exporting_spec.rb +8 -5
- data/spec/chunky_png/canvas/data_url_importing_spec.rb +5 -6
- data/spec/chunky_png/canvas/drawing_spec.rb +46 -38
- data/spec/chunky_png/canvas/masking_spec.rb +15 -16
- data/spec/chunky_png/canvas/operations_spec.rb +68 -67
- data/spec/chunky_png/canvas/png_decoding_spec.rb +37 -38
- data/spec/chunky_png/canvas/png_encoding_spec.rb +59 -50
- data/spec/chunky_png/canvas/resampling_spec.rb +19 -21
- data/spec/chunky_png/canvas/stream_exporting_spec.rb +47 -27
- data/spec/chunky_png/canvas/stream_importing_spec.rb +10 -11
- data/spec/chunky_png/canvas_spec.rb +63 -52
- data/spec/chunky_png/color_spec.rb +115 -114
- data/spec/chunky_png/datastream_spec.rb +55 -51
- data/spec/chunky_png/dimension_spec.rb +10 -10
- data/spec/chunky_png/image_spec.rb +11 -14
- data/spec/chunky_png/point_spec.rb +21 -23
- data/spec/chunky_png/rmagick_spec.rb +7 -8
- data/spec/chunky_png/vector_spec.rb +21 -17
- data/spec/chunky_png_spec.rb +2 -2
- data/spec/png_suite_spec.rb +35 -40
- data/spec/spec_helper.rb +6 -10
- data/tasks/benchmarks.rake +7 -8
- metadata +50 -12
- data/.travis.yml +0 -16
- data/lib/chunky_png/compatibility.rb +0 -15
@@ -1,5 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen-string-literal: true
|
2
2
|
|
3
|
+
module ChunkyPNG
|
3
4
|
# The Datastream class represents a PNG formatted datastream. It supports
|
4
5
|
# both reading from and writing to strings, streams and files.
|
5
6
|
#
|
@@ -9,9 +10,8 @@ module ChunkyPNG
|
|
9
10
|
#
|
10
11
|
# @see ChunkyPNG::Chunk
|
11
12
|
class Datastream
|
12
|
-
|
13
13
|
# The signature that each PNG file or stream should begin with.
|
14
|
-
SIGNATURE =
|
14
|
+
SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10].pack("C8").force_encoding(::Encoding::BINARY).freeze
|
15
15
|
|
16
16
|
# The header chunk of this datastream.
|
17
17
|
# @return [ChunkyPNG::Chunk::Header]
|
@@ -52,22 +52,21 @@ module ChunkyPNG
|
|
52
52
|
##############################################################################
|
53
53
|
|
54
54
|
class << self
|
55
|
-
|
56
55
|
# Reads a PNG datastream from a string.
|
57
56
|
# @param [String] str The PNG encoded string to load from.
|
58
57
|
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
|
59
58
|
def from_blob(str)
|
60
|
-
from_io(StringIO.new(str))
|
59
|
+
from_io(StringIO.new(str, "rb"))
|
61
60
|
end
|
62
61
|
|
63
|
-
alias
|
62
|
+
alias from_string from_blob
|
64
63
|
|
65
64
|
# Reads a PNG datastream from a file.
|
66
65
|
# @param [String] filename The path of the file to load from.
|
67
66
|
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
|
68
67
|
def from_file(filename)
|
69
68
|
ds = nil
|
70
|
-
File.open(filename,
|
69
|
+
File.open(filename, "rb") { |f| ds = from_io(f) }
|
71
70
|
ds
|
72
71
|
end
|
73
72
|
|
@@ -75,22 +74,23 @@ module ChunkyPNG
|
|
75
74
|
# @param [IO] io The stream to read from.
|
76
75
|
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
|
77
76
|
def from_io(io)
|
77
|
+
io.set_encoding(::Encoding::BINARY)
|
78
78
|
verify_signature!(io)
|
79
79
|
|
80
|
-
ds =
|
80
|
+
ds = new
|
81
81
|
while ds.end_chunk.nil?
|
82
82
|
chunk = ChunkyPNG::Chunk.read(io)
|
83
83
|
case chunk
|
84
|
-
when ChunkyPNG::Chunk::Header
|
85
|
-
when ChunkyPNG::Chunk::Palette
|
86
|
-
when ChunkyPNG::Chunk::Transparency
|
87
|
-
when ChunkyPNG::Chunk::ImageData
|
88
|
-
when ChunkyPNG::Chunk::Physical
|
89
|
-
when ChunkyPNG::Chunk::End
|
84
|
+
when ChunkyPNG::Chunk::Header then ds.header_chunk = chunk
|
85
|
+
when ChunkyPNG::Chunk::Palette then ds.palette_chunk = chunk
|
86
|
+
when ChunkyPNG::Chunk::Transparency then ds.transparency_chunk = chunk
|
87
|
+
when ChunkyPNG::Chunk::ImageData then ds.data_chunks << chunk
|
88
|
+
when ChunkyPNG::Chunk::Physical then ds.physical_chunk = chunk
|
89
|
+
when ChunkyPNG::Chunk::End then ds.end_chunk = chunk
|
90
90
|
else ds.other_chunks << chunk
|
91
91
|
end
|
92
92
|
end
|
93
|
-
|
93
|
+
ds
|
94
94
|
end
|
95
95
|
|
96
96
|
# Verifies that the current stream is a PNG datastream by checking its signature.
|
@@ -103,7 +103,7 @@ module ChunkyPNG
|
|
103
103
|
# the beginning of the stream.
|
104
104
|
def verify_signature!(io)
|
105
105
|
signature = io.read(ChunkyPNG::Datastream::SIGNATURE.length)
|
106
|
-
unless
|
106
|
+
unless signature == ChunkyPNG::Datastream::SIGNATURE
|
107
107
|
raise ChunkyPNG::SignatureMismatch, "PNG signature not found, found #{signature.inspect} instead of #{ChunkyPNG::Datastream::SIGNATURE.inspect}!"
|
108
108
|
end
|
109
109
|
end
|
@@ -127,7 +127,7 @@ module ChunkyPNG
|
|
127
127
|
yield(palette_chunk) if palette_chunk
|
128
128
|
yield(transparency_chunk) if transparency_chunk
|
129
129
|
yield(physical_chunk) if physical_chunk
|
130
|
-
data_chunks.each
|
130
|
+
data_chunks.each { |chunk| yield(chunk) }
|
131
131
|
yield(end_chunk)
|
132
132
|
end
|
133
133
|
|
@@ -142,8 +142,8 @@ module ChunkyPNG
|
|
142
142
|
# @return [Hash] A hash containing metadata fields and their values.
|
143
143
|
def metadata
|
144
144
|
metadata = {}
|
145
|
-
other_chunks.
|
146
|
-
metadata[chunk.keyword] = chunk.value if chunk.respond_to?(:keyword)
|
145
|
+
other_chunks.each do |chunk|
|
146
|
+
metadata[chunk.keyword] = chunk.value if chunk.respond_to?(:keyword) && chunk.respond_to?(:value)
|
147
147
|
end
|
148
148
|
metadata
|
149
149
|
end
|
@@ -158,12 +158,6 @@ module ChunkyPNG
|
|
158
158
|
# WRITING DATASTREAMS
|
159
159
|
##################################################################################
|
160
160
|
|
161
|
-
# Returns an empty stream using binary encoding that can be used as stream to encode to.
|
162
|
-
# @return [String] An empty, binary string.
|
163
|
-
def self.empty_bytearray
|
164
|
-
ChunkyPNG::EMPTY_BYTEARRAY.dup
|
165
|
-
end
|
166
|
-
|
167
161
|
# Writes the datastream to the given output stream.
|
168
162
|
# @param [IO] io The output stream to write to.
|
169
163
|
def write(io)
|
@@ -174,19 +168,19 @@ module ChunkyPNG
|
|
174
168
|
# Saves this datastream as a PNG file.
|
175
169
|
# @param [String] filename The filename to use.
|
176
170
|
def save(filename)
|
177
|
-
File.open(filename,
|
171
|
+
File.open(filename, "wb") { |f| write(f) }
|
178
172
|
end
|
179
173
|
|
180
174
|
# Encodes this datastream into a string.
|
181
175
|
# @return [String] The encoded PNG datastream.
|
182
176
|
def to_blob
|
183
177
|
str = StringIO.new
|
184
|
-
str.set_encoding(
|
178
|
+
str.set_encoding("ASCII-8BIT")
|
185
179
|
write(str)
|
186
|
-
|
180
|
+
str.string
|
187
181
|
end
|
188
182
|
|
189
|
-
alias
|
190
|
-
alias
|
183
|
+
alias to_string to_blob
|
184
|
+
alias to_s to_blob
|
191
185
|
end
|
192
186
|
end
|
data/lib/chunky_png/dimension.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen-string-literal: true
|
2
2
|
|
3
|
+
module ChunkyPNG
|
3
4
|
# Creates a {ChunkyPNG::Dimension} instance using arguments that can be interpreted
|
4
5
|
# as width and height.
|
5
6
|
#
|
@@ -29,8 +30,8 @@ module ChunkyPNG
|
|
29
30
|
# @see ChunkyPNG::Dimension
|
30
31
|
def self.Dimension(*args)
|
31
32
|
case args.length
|
32
|
-
when 2
|
33
|
-
when 1
|
33
|
+
when 2 then ChunkyPNG::Dimension.new(*args)
|
34
|
+
when 1 then build_dimension_from_object(args.first)
|
34
35
|
else raise ArgumentError,
|
35
36
|
"Don't know how to construct a dimension from #{args.inspect}"
|
36
37
|
end
|
@@ -45,8 +46,8 @@ module ChunkyPNG
|
|
45
46
|
when Array
|
46
47
|
ChunkyPNG::Dimension.new(source[0], source[1])
|
47
48
|
when Hash
|
48
|
-
width = source[:width] || source[
|
49
|
-
height = source[:height] || source[
|
49
|
+
width = source[:width] || source["width"]
|
50
|
+
height = source[:height] || source["height"]
|
50
51
|
ChunkyPNG::Dimension.new(width, height)
|
51
52
|
when ChunkyPNG::Dimension::DIMENSION_REGEXP
|
52
53
|
ChunkyPNG::Dimension.new($1, $2)
|
@@ -58,13 +59,13 @@ module ChunkyPNG
|
|
58
59
|
end
|
59
60
|
end
|
60
61
|
end
|
62
|
+
|
61
63
|
private_class_method :build_dimension_from_object
|
62
64
|
|
63
65
|
# Class that represents the dimension of something, e.g. a {ChunkyPNG::Canvas}.
|
64
66
|
#
|
65
67
|
# This class contains some methods to simplify performing dimension related checks.
|
66
68
|
class Dimension
|
67
|
-
|
68
69
|
# @return [Regexp] The regexp to parse dimensions from a string.
|
69
70
|
# @private
|
70
71
|
DIMENSION_REGEXP = /^[\(\[\{]?(\d+)\s*[x,]?\s*(\d+)[\)\]\}]?$/
|
@@ -89,7 +90,7 @@ module ChunkyPNG
|
|
89
90
|
end
|
90
91
|
|
91
92
|
# Checks whether a point is within bounds of this dimension.
|
92
|
-
# @param [ChunkyPNG::Point, ...] A point-like to bounds-check.
|
93
|
+
# @param [ChunkyPNG::Point, ...] point_like A point-like to bounds-check.
|
93
94
|
# @return [true, false] True iff the x and y coordinate fall in this dimension.
|
94
95
|
# @see ChunkyPNG.Point
|
95
96
|
def include?(*point_like)
|
@@ -98,17 +99,23 @@ module ChunkyPNG
|
|
98
99
|
end
|
99
100
|
|
100
101
|
# Checks whether 2 dimensions are identical.
|
101
|
-
# @param [ChunkyPNG::Dimension] The dimension to compare with.
|
102
|
+
# @param [ChunkyPNG::Dimension] other The dimension to compare with.
|
102
103
|
# @return [true, false] <tt>true</tt> iff width and height match.
|
103
104
|
def eql?(other)
|
104
105
|
return false unless other.respond_to?(:width) && other.respond_to?(:height)
|
105
106
|
other.width == width && other.height == height
|
106
107
|
end
|
107
108
|
|
108
|
-
|
109
|
+
alias == eql?
|
110
|
+
|
111
|
+
# Calculates a hash for the dimension object, based on width and height
|
112
|
+
# @return [Integer] A hashed value of the dimensions
|
113
|
+
def hash
|
114
|
+
[width, height].hash
|
115
|
+
end
|
109
116
|
|
110
117
|
# Compares the size of 2 dimensions.
|
111
|
-
# @param [ChunkyPNG::Dimension] The dimension to compare with.
|
118
|
+
# @param [ChunkyPNG::Dimension] other The dimension to compare with.
|
112
119
|
# @return [-1, 0, 1] -1 if the other dimension has a larger area, 1 of this
|
113
120
|
# dimension is larger, 0 if both are identical in size.
|
114
121
|
def <=>(other)
|
@@ -121,6 +128,6 @@ module ChunkyPNG
|
|
121
128
|
[width, height]
|
122
129
|
end
|
123
130
|
|
124
|
-
|
131
|
+
alias to_ary to_a
|
125
132
|
end
|
126
133
|
end
|
data/lib/chunky_png/image.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
|
1
|
+
# frozen-string-literal: true
|
2
2
|
|
3
|
+
module ChunkyPNG
|
3
4
|
# ChunkyPNG::Image is an extension of the {ChunkyPNG::Canvas} class, that
|
4
5
|
# also includes support for metadata.
|
5
6
|
#
|
6
7
|
# @see ChunkyPNG::Canvas
|
7
8
|
class Image < Canvas
|
8
|
-
|
9
9
|
# The minimum size of bytes the value of a metadata field should be before compression
|
10
10
|
# is enabled for the chunk.
|
11
11
|
METADATA_COMPRESSION_TRESHOLD = 300
|
12
|
-
|
12
|
+
|
13
13
|
# @return [Hash] The hash of metadata fields for this PNG image.
|
14
14
|
attr_accessor :metadata
|
15
|
-
|
15
|
+
|
16
16
|
# Initializes a new ChunkyPNG::Image instance.
|
17
17
|
# @param [Integer] width The width of the new image.
|
18
18
|
# @param [Integer] height The height of the new image.
|
@@ -23,7 +23,7 @@ module ChunkyPNG
|
|
23
23
|
super(width, height, bg_color)
|
24
24
|
@metadata = metadata
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
# Initializes a copy of another ChunkyPNG::Image instance.
|
28
28
|
#
|
29
29
|
# @param [ChunkyPNG::Image] other The other image to copy.
|
@@ -31,7 +31,7 @@ module ChunkyPNG
|
|
31
31
|
super(other)
|
32
32
|
@metadata = other.metadata
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
# Returns the metadata for this image as PNG chunks.
|
36
36
|
#
|
37
37
|
# Chunks will either be of the {ChunkyPNG::Chunk::Text} type for small
|
@@ -49,7 +49,7 @@ module ChunkyPNG
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
# Encodes the image to a PNG datastream for saving to disk or writing to an IO stream.
|
54
54
|
#
|
55
55
|
# Besides encoding the canvas, it will also encode the metadata fields to text chunks.
|
@@ -61,19 +61,19 @@ module ChunkyPNG
|
|
61
61
|
def to_datastream(constraints = {})
|
62
62
|
ds = super(constraints)
|
63
63
|
ds.other_chunks += metadata_chunks
|
64
|
-
|
64
|
+
ds
|
65
65
|
end
|
66
|
-
|
66
|
+
|
67
67
|
# Reads a ChunkyPNG::Image instance from a data stream.
|
68
68
|
#
|
69
69
|
# Besides decoding the canvas, this will also read the metadata fields
|
70
70
|
# from the datastream.
|
71
71
|
#
|
72
|
-
# @param [ChunkyPNG::Datastream] The datastream to read from.
|
72
|
+
# @param [ChunkyPNG::Datastream] ds The datastream to read from.
|
73
73
|
def self.from_datastream(ds)
|
74
74
|
image = super(ds)
|
75
75
|
image.metadata = ds.metadata
|
76
|
-
|
76
|
+
image
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
data/lib/chunky_png/palette.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
1
3
|
module ChunkyPNG
|
2
4
|
# A palette describes the set of colors that is being used for an image.
|
3
5
|
#
|
@@ -40,12 +42,8 @@ module ChunkyPNG
|
|
40
42
|
decoding_map = []
|
41
43
|
index = 0
|
42
44
|
|
43
|
-
palatte_bytes = palette_chunk.content.unpack(
|
44
|
-
|
45
|
-
alpha_channel = transparency_chunk.content.unpack('C*')
|
46
|
-
else
|
47
|
-
alpha_channel = []
|
48
|
-
end
|
45
|
+
palatte_bytes = palette_chunk.content.unpack("C*")
|
46
|
+
alpha_channel = transparency_chunk ? transparency_chunk.content.unpack("C*") : []
|
49
47
|
|
50
48
|
index = 0
|
51
49
|
palatte_bytes.each_slice(3) do |bytes|
|
@@ -165,7 +163,7 @@ module ChunkyPNG
|
|
165
163
|
#
|
166
164
|
# @return [ChunkyPNG::Chunk::Transparency] The tRNS chunk.
|
167
165
|
def to_trns_chunk
|
168
|
-
ChunkyPNG::Chunk::Transparency.new(
|
166
|
+
ChunkyPNG::Chunk::Transparency.new("tRNS", map { |c| ChunkyPNG::Color.a(c) }.pack("C*"))
|
169
167
|
end
|
170
168
|
|
171
169
|
# Creates a PLTE chunk that corresponds with this palette to store the r,
|
@@ -186,7 +184,7 @@ module ChunkyPNG
|
|
186
184
|
colors += ChunkyPNG::Color.to_truecolor_bytes(color)
|
187
185
|
end
|
188
186
|
|
189
|
-
ChunkyPNG::Chunk::Palette.new(
|
187
|
+
ChunkyPNG::Chunk::Palette.new("PLTE", colors.pack("C*"))
|
190
188
|
end
|
191
189
|
|
192
190
|
# Determines the most suitable colormode for this palette.
|
@@ -219,7 +217,6 @@ module ChunkyPNG
|
|
219
217
|
when 3..4 then 2
|
220
218
|
when 5..16 then 4
|
221
219
|
when 17..256 then 8
|
222
|
-
else nil
|
223
220
|
end
|
224
221
|
end
|
225
222
|
end
|
data/lib/chunky_png/point.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
1
3
|
module ChunkyPNG
|
2
|
-
|
3
4
|
# Factory method to create {ChunkyPNG::Point} instances.
|
4
|
-
#
|
5
|
-
# This method tries to be as flexible as possible with regards to the given input: besides
|
5
|
+
#
|
6
|
+
# This method tries to be as flexible as possible with regards to the given input: besides
|
6
7
|
# explicit coordinates, this method also accepts arrays, hashes, strings, {ChunkyPNG::Dimension}
|
7
8
|
# instances and anything that responds to <tt>:x</tt> and <tt>:y</tt>.
|
8
|
-
#
|
9
|
+
#
|
9
10
|
# @overload Point(x, y)
|
10
11
|
# @param [Integer, :to_i] x The x-coordinate
|
11
12
|
# @param [Integer, :to_i] y The y-coordinate
|
@@ -30,11 +31,11 @@ module ChunkyPNG
|
|
30
31
|
# @see ChunkyPNG::Point
|
31
32
|
def self.Point(*args)
|
32
33
|
case args.length
|
33
|
-
when 2
|
34
|
-
when 1
|
35
|
-
else raise ArgumentError,
|
34
|
+
when 2 then ChunkyPNG::Point.new(*args)
|
35
|
+
when 1 then build_point_from_object(args.first)
|
36
|
+
else raise ArgumentError,
|
36
37
|
"Don't know how to construct a point from #{args.inspect}!"
|
37
|
-
end
|
38
|
+
end
|
38
39
|
end
|
39
40
|
|
40
41
|
def self.build_point_from_object(source)
|
@@ -46,20 +47,21 @@ module ChunkyPNG
|
|
46
47
|
when Array
|
47
48
|
ChunkyPNG::Point.new(source[0], source[1])
|
48
49
|
when Hash
|
49
|
-
x = source[:x] || source[
|
50
|
-
y = source[:y] || source[
|
50
|
+
x = source[:x] || source["x"]
|
51
|
+
y = source[:y] || source["y"]
|
51
52
|
ChunkyPNG::Point.new(x, y)
|
52
53
|
when ChunkyPNG::Point::POINT_REGEXP
|
53
54
|
ChunkyPNG::Point.new($1.to_i, $2.to_i)
|
54
|
-
else
|
55
|
+
else
|
55
56
|
if source.respond_to?(:x) && source.respond_to?(:y)
|
56
57
|
ChunkyPNG::Point.new(source.x, source.y)
|
57
|
-
else
|
58
|
-
raise ArgumentError,
|
58
|
+
else
|
59
|
+
raise ArgumentError,
|
59
60
|
"Don't know how to construct a point from #{source.inspect}!"
|
60
61
|
end
|
61
62
|
end
|
62
63
|
end
|
64
|
+
|
63
65
|
private_class_method :build_point_from_object
|
64
66
|
|
65
67
|
# Simple class that represents a point on a canvas using an x and y coordinate.
|
@@ -69,7 +71,6 @@ module ChunkyPNG
|
|
69
71
|
#
|
70
72
|
# @see ChunkyPNG.Point
|
71
73
|
class Point
|
72
|
-
|
73
74
|
# @return [Regexp] The regexp to parse points from a string.
|
74
75
|
# @private
|
75
76
|
POINT_REGEXP = /^[\(\[\{]?(\d+)\s*[,]?\s*(\d+)[\)\]\}]?$/
|
@@ -79,22 +80,22 @@ module ChunkyPNG
|
|
79
80
|
|
80
81
|
# @return [Integer] The y-coordinate of the point.
|
81
82
|
attr_accessor :y
|
82
|
-
|
83
|
+
|
83
84
|
# Initializes a new point instance.
|
84
85
|
# @param [Integer, :to_i] x The x-coordinate.
|
85
86
|
# @param [Integer, :to_i] y The y-coordinate.
|
86
87
|
def initialize(x, y)
|
87
88
|
@x, @y = x.to_i, y.to_i
|
88
89
|
end
|
89
|
-
|
90
|
+
|
90
91
|
# Checks whether 2 points are identical.
|
91
92
|
# @return [true, false] <tt>true</tt> iff the x and y coordinates match
|
92
93
|
def eql?(other)
|
93
94
|
other.x == x && other.y == y
|
94
95
|
end
|
95
|
-
|
96
|
-
|
97
|
-
|
96
|
+
|
97
|
+
alias == eql?
|
98
|
+
|
98
99
|
# Compares 2 points.
|
99
100
|
#
|
100
101
|
# It will first compare the y coordinate, and it only takes the x-coordinate into
|
@@ -106,21 +107,21 @@ module ChunkyPNG
|
|
106
107
|
# @return [-1, 0, 1] <tt>-1</tt> If this point comes before the other one, <tt>1</tt>
|
107
108
|
# if after, and <tt>0</tt> if the points are identical.
|
108
109
|
def <=>(other)
|
109
|
-
(
|
110
|
+
(y <=> other.y) == 0 ? x <=> other.x : y <=> other.y
|
110
111
|
end
|
111
|
-
|
112
|
+
|
112
113
|
# Converts the point instance to an array.
|
113
114
|
# @return [Array] A 2-element array, i.e. <tt>[x, y]</tt>.
|
114
115
|
def to_a
|
115
116
|
[x, y]
|
116
117
|
end
|
117
|
-
|
118
|
-
|
119
|
-
|
118
|
+
|
119
|
+
alias to_ary to_a
|
120
|
+
|
120
121
|
# Checks whether the point falls into a dimension
|
121
|
-
# @param [ChunkyPNG::Dimension, ...] dimension_like The dimension of which the bounds
|
122
|
+
# @param [ChunkyPNG::Dimension, ...] dimension_like The dimension of which the bounds
|
122
123
|
# should be taken for the check.
|
123
|
-
# @return [true, false] <tt>true</tt> iff the x and y coordinate fall width the width
|
124
|
+
# @return [true, false] <tt>true</tt> iff the x and y coordinate fall width the width
|
124
125
|
# and height of the dimension.
|
125
126
|
def within_bounds?(*dimension_like)
|
126
127
|
ChunkyPNG::Dimension(*dimension_like).include?(self)
|