dyi 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +7 -1
- data/lib/dyi.rb +3 -1
- data/lib/dyi/animation.rb +5 -4
- data/lib/dyi/canvas.rb +5 -8
- data/lib/dyi/chart.rb +1 -1
- data/lib/dyi/chart/array_reader.rb +104 -10
- data/lib/dyi/chart/axis_util.rb +31 -17
- data/lib/dyi/chart/base.rb +104 -7
- data/lib/dyi/chart/csv_reader.rb +56 -8
- data/lib/dyi/chart/excel_reader.rb +27 -4
- data/lib/dyi/chart/legend.rb +10 -8
- data/lib/dyi/chart/line_chart.rb +29 -25
- data/lib/dyi/chart/pie_chart.rb +192 -29
- data/lib/dyi/chart/table.rb +12 -10
- data/lib/dyi/color.rb +9 -7
- data/lib/dyi/coordinate.rb +177 -61
- data/lib/dyi/drawing.rb +1 -1
- data/lib/dyi/drawing/clipping.rb +9 -3
- data/lib/dyi/drawing/color_effect.rb +7 -4
- data/lib/dyi/drawing/filter.rb +10 -7
- data/lib/dyi/drawing/pen.rb +421 -11
- data/lib/dyi/drawing/pen_3d.rb +12 -7
- data/lib/dyi/element.rb +5 -4
- data/lib/dyi/event.rb +3 -3
- data/lib/dyi/font.rb +6 -4
- data/lib/dyi/formatter.rb +1 -1
- data/lib/dyi/formatter/base.rb +24 -15
- data/lib/dyi/formatter/emf_formatter.rb +6 -5
- data/lib/dyi/formatter/eps_formatter.rb +15 -14
- data/lib/dyi/formatter/svg_formatter.rb +16 -14
- data/lib/dyi/formatter/svg_reader.rb +4 -3
- data/lib/dyi/formatter/xaml_formatter.rb +9 -7
- data/lib/dyi/length.rb +213 -114
- data/lib/dyi/matrix.rb +4 -2
- data/lib/dyi/painting.rb +161 -87
- data/lib/dyi/script.rb +1 -1
- data/lib/dyi/script/ecmascript.rb +18 -29
- data/lib/dyi/script/simple_script.rb +4 -8
- data/lib/dyi/shape.rb +1 -1
- data/lib/dyi/shape/base.rb +8 -17
- data/lib/dyi/shape/path.rb +102 -19
- data/lib/dyi/stylesheet.rb +4 -3
- data/lib/dyi/svg_element.rb +9 -7
- data/lib/dyi/type.rb +5 -2
- data/lib/dyi/util.rb +36 -1
- data/lib/ironruby.rb +1 -1
- data/lib/util.rb +53 -5
- metadata +4 -19
data/lib/dyi/chart/table.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# -*- encoding: UTF-8 -*-
|
2
2
|
|
3
|
-
# Copyright (c) 2009-
|
3
|
+
# Copyright (c) 2009-2012 Sound-F Co., Ltd. All rights reserved.
|
4
4
|
#
|
5
5
|
# Author:: Mamoru Yuo
|
6
6
|
#
|
@@ -19,9 +19,11 @@
|
|
19
19
|
# You should have received a copy of the GNU General Public License
|
20
20
|
# along with DYI. If not, see <http://www.gnu.org/licenses/>.
|
21
21
|
|
22
|
-
|
23
|
-
|
22
|
+
#
|
23
|
+
module DYI
|
24
|
+
module Chart
|
24
25
|
|
26
|
+
# @since 0.0.0
|
25
27
|
class Table < Base
|
26
28
|
attr_reader :frame_canvas, :data_canvas
|
27
29
|
|
@@ -130,19 +132,19 @@ module DYI #:nodoc:
|
|
130
132
|
=end
|
131
133
|
private
|
132
134
|
|
133
|
-
def options
|
135
|
+
def options
|
134
136
|
@options
|
135
137
|
end
|
136
138
|
|
137
|
-
def default_csv_format
|
139
|
+
def default_csv_format
|
138
140
|
[0, 1]
|
139
141
|
end
|
140
142
|
|
141
|
-
def convert_data(value)
|
143
|
+
def convert_data(value)
|
142
144
|
value.strip
|
143
145
|
end
|
144
146
|
|
145
|
-
def create_vector_image
|
147
|
+
def create_vector_image
|
146
148
|
pen = Drawing::Pen.new
|
147
149
|
@frame_canvas = Shape::ShapeGroup.draw_on(@canvas)
|
148
150
|
@data_canvas = Shape::ShapeGroup.draw_on(@canvas)
|
@@ -150,7 +152,7 @@ module DYI #:nodoc:
|
|
150
152
|
draw_data(pen)
|
151
153
|
end
|
152
154
|
|
153
|
-
def draw_frame(pen)
|
155
|
+
def draw_frame(pen)
|
154
156
|
w = table_width
|
155
157
|
h = table_height
|
156
158
|
|
@@ -166,7 +168,7 @@ module DYI #:nodoc:
|
|
166
168
|
end
|
167
169
|
end
|
168
170
|
|
169
|
-
def draw_column_colors
|
171
|
+
def draw_column_colors
|
170
172
|
if column_colors
|
171
173
|
brush = Drawing::Brush.new
|
172
174
|
h = table_height
|
@@ -181,7 +183,7 @@ module DYI #:nodoc:
|
|
181
183
|
end
|
182
184
|
end
|
183
185
|
|
184
|
-
def draw_data(pen)
|
186
|
+
def draw_data(pen)
|
185
187
|
cell_margin = (row_height - (font && font.draw_size || Font::DEFAULT_SIZE)) / 2
|
186
188
|
series.inject(cell_margin) do |x, column_index|
|
187
189
|
y = row_height - cell_margin
|
data/lib/dyi/color.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# -*- encoding: UTF-8 -*-
|
2
2
|
|
3
|
-
# Copyright (c) 2009-
|
3
|
+
# Copyright (c) 2009-2012 Sound-F Co., Ltd. All rights reserved.
|
4
4
|
#
|
5
5
|
# Author:: Mamoru Yuo
|
6
6
|
#
|
@@ -19,8 +19,10 @@
|
|
19
19
|
# You should have received a copy of the GNU General Public License
|
20
20
|
# along with DYI. If not, see <http://www.gnu.org/licenses/>.
|
21
21
|
|
22
|
-
|
22
|
+
#
|
23
|
+
module DYI
|
23
24
|
|
25
|
+
# @since 0.0.0
|
24
26
|
class Color
|
25
27
|
@@named_colors = {'aliceblue'=>[240,248,255],'antiquewhite'=>[250,235,215],'aqua'=>[0,255,255],'aquamarine'=>[127,255,212],'azure'=>[240,255,255],'beige'=>[245,245,220],'bisque'=>[255,228,196],'black'=>[0,0,0],'blanchedalmond'=>[255,235,205],'blue'=>[0,0,255],'blueviolet'=>[138,43,226],'brown'=>[165,42,42],'burlywood'=>[222,184,135],'cadetblue'=>[95,158,160],'chartreuse'=>[127,255,0],'chocolate'=>[210,105,30],'coral'=>[255,127,80],'cornflowerblue'=>[100,149,237],'cornsilk'=>[255,248,220],'crimson'=>[220,20,60],'cyan'=>[0,255,255],'darkblue'=>[0,0,139],'darkcyan'=>[0,139,139],'darkgoldenrod'=>[184,134,11],'darkgray'=>[169,169,169],'darkgreen'=>[0,100,0],'darkgrey'=>[169,169,169],'darkkhaki'=>[189,183,107],'darkmagenta'=>[139,0,139],'darkolivegreen'=>[85,107,47],'darkorange'=>[255,140,0],'darkorchid'=>[153,50,204],'darkred'=>[139,0,0],'darksalmon'=>[233,150,122],'darkseagreen'=>[143,188,143],'darkslateblue'=>[72,61,139],'darkslategray'=>[47,79,79],'darkslategrey'=>[47,79,79],'darkturquoise'=>[0,206,209],'darkviolet'=>[148,0,211],'deeppink'=>[255,20,147],'deepskyblue'=>[0,191,255],'dimgray'=>[105,105,105],'dimgrey'=>[105,105,105],'dodgerblue'=>[30,144,255],'firebrick'=>[178,34,34],'floralwhite'=>[255,250,240],'forestgreen'=>[34,139,34],'fuchsia'=>[255,0,255],'gainsboro'=>[220,220,220],'ghostwhite'=>[248,248,255],'gold'=>[255,215,0],'goldenrod'=>[218,165,32],'gray'=>[128,128,128],'grey'=>[128,128,128],'green'=>[0,128,0],'greenyellow'=>[173,255,47],'honeydew'=>[240,255,240],'hotpink'=>[255,105,180],'indianred'=>[205,92,92],'indigo'=>[75,0,130],'ivory'=>[255,255,240],'khaki'=>[240,230,140],'lavender'=>[230,230,250],'lavenderblush'=>[255,240,245],'lawngreen'=>[124,252,0],'lemonchiffon'=>[255,250,205],'lightblue'=>[173,216,230],'lightcoral'=>[240,128,128],'lightcyan'=>[224,255,255],'lightgoldenrodyellow'=>[250,250,210],'lightgray'=>[211,211,211],'lightgreen'=>[144,238,144],'lightgrey'=>[211,211,211],'lightpink'=>[255,182,193],'lightsalmon'=>[255,160,122],'lightseagreen'=>[32,178,170],'lightskyblue'=>[135,206,250],'lightslategray'=>[119,136,153],'lightslategrey'=>[119,136,153],'lightsteelblue'=>[176,196,222],'lightyellow'=>[255,255,224],'lime'=>[0,255,0],'limegreen'=>[50,205,50],'linen'=>[250,240,230],'magenta'=>[255,0,255],'maroon'=>[128,0,0],'mediumaquamarine'=>[102,205,170],'mediumblue'=>[0,0,205],'mediumorchid'=>[186,85,211],'mediumpurple'=>[147,112,219],'mediumseagreen'=>[60,179,113],'mediumslateblue'=>[123,104,238],'mediumspringgreen'=>[0,250,154],'mediumturquoise'=>[72,209,204],'mediumvioletred'=>[199,21,133],'midnightblue'=>[25,25,112],'mintcream'=>[245,255,250],'mistyrose'=>[255,228,225],'moccasin'=>[255,228,181],'navajowhite'=>[255,222,173],'navy'=>[0,0,128],'oldlace'=>[253,245,230],'olive'=>[128,128,0],'olivedrab'=>[107,142,35],'orange'=>[255,165,0],'orangered'=>[255,69,0],'orchid'=>[218,112,214],'palegoldenrod'=>[238,232,170],'palegreen'=>[152,251,152],'paleturquoise'=>[175,238,238],'palevioletred'=>[219,112,147],'papayawhip'=>[255,239,213],'peachpuff'=>[255,218,185],'peru'=>[205,133,63],'pink'=>[255,192,203],'plum'=>[221,160,221],'powderblue'=>[176,224,230],'purple'=>[128,0,128],'red'=>[255,0,0],'rosybrown'=>[188,143,143],'royalblue'=>[65,105,225],'saddlebrown'=>[139,69,19],'salmon'=>[250,128,114],'sandybrown'=>[244,164,96],'seagreen'=>[46,139,87],'seashell'=>[255,245,238],'sienna'=>[160,82,45],'silver'=>[192,192,192],'skyblue'=>[135,206,235],'slateblue'=>[106,90,205],'slategray'=>[112,128,144],'slategrey'=>[112,128,144],'snow'=>[255,250,250],'springgreen'=>[0,255,127],'steelblue'=>[70,130,180],'tan'=>[210,180,140],'teal'=>[0,128,128],'thistle'=>[216,191,216],'tomato'=>[255,99,71],'turquoise'=>[64,224,208],'violet'=>[238,130,238],'wheat'=>[245,222,179],'white'=>[255,255,255],'whitesmoke'=>[245,245,245],'yellow'=>[255,255,0],'yellowgreen'=>[154,205,50]}
|
26
28
|
@@default_format = ['#%02X%02X%02X', false]
|
@@ -87,7 +89,7 @@ module DYI #:nodoc:
|
|
87
89
|
self == other
|
88
90
|
end
|
89
91
|
|
90
|
-
def hash
|
92
|
+
def hash
|
91
93
|
(@r << 16) + (@g << 8) + @b
|
92
94
|
end
|
93
95
|
|
@@ -149,7 +151,7 @@ module DYI #:nodoc:
|
|
149
151
|
|
150
152
|
public
|
151
153
|
|
152
|
-
def new(*args)
|
154
|
+
def new(*args)
|
153
155
|
if args.size == 1
|
154
156
|
case args.first
|
155
157
|
when self
|
@@ -167,7 +169,7 @@ module DYI #:nodoc:
|
|
167
169
|
(args.size == 1 && args.first.nil?) ? nil : new(*args)
|
168
170
|
end
|
169
171
|
|
170
|
-
def method_missing(name, *args)
|
172
|
+
def method_missing(name, *args)
|
171
173
|
if args.size == 0 && color = named_color(name)
|
172
174
|
instance_eval %{
|
173
175
|
def self.#{name}
|
@@ -192,7 +194,7 @@ module DYI #:nodoc:
|
|
192
194
|
@@default_format
|
193
195
|
end
|
194
196
|
|
195
|
-
def check_format(fmt)
|
197
|
+
def check_format(fmt)
|
196
198
|
begin
|
197
199
|
(fmt = fmt.to_s) % [0.0, 0.0, 0.0]
|
198
200
|
fmt
|
@@ -203,7 +205,7 @@ module DYI #:nodoc:
|
|
203
205
|
|
204
206
|
private
|
205
207
|
|
206
|
-
def named_color(name)
|
208
|
+
def named_color(name)
|
207
209
|
name = name.to_s.downcase
|
208
210
|
color = instance_variable_get('@' + name) rescue (return nil)
|
209
211
|
return color if color
|
data/lib/dyi/coordinate.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
# -*- encoding: UTF-8 -*-
|
2
2
|
|
3
|
-
# Copyright (c) 2009-
|
3
|
+
# Copyright (c) 2009-2012 Sound-F Co., Ltd. All rights reserved.
|
4
4
|
#
|
5
5
|
# Author:: Mamoru Yuo
|
6
|
-
# Documentation:: Mamoru Yuo
|
7
6
|
#
|
8
7
|
# This file is part of DYI.
|
9
8
|
#
|
@@ -19,52 +18,51 @@
|
|
19
18
|
#
|
20
19
|
# You should have received a copy of the GNU General Public License
|
21
20
|
# along with DYI. If not, see <http://www.gnu.org/licenses/>.
|
22
|
-
#
|
23
|
-
# == Overview
|
24
|
-
#
|
25
|
-
# This file provides the DYI::Coordinate class, which provides
|
26
|
-
# coordinate supports for DYI scripts. The coordinate represents a
|
27
|
-
# length in the user coordinate system that is the given distance from the
|
28
|
-
# origin of the user coordinate system along the relevant axis (the x-axis for
|
29
|
-
# X coordinates, the y-axis for Y coordinates).
|
30
|
-
#
|
31
|
-
# See the documentation to the DYI::Coordinate class for more details
|
32
|
-
# and examples of usage.
|
33
|
-
#
|
34
21
|
|
35
|
-
|
22
|
+
#
|
23
|
+
module DYI
|
36
24
|
|
37
|
-
# Class representing a coordinate.
|
38
|
-
#
|
39
|
-
#
|
40
|
-
# == Introduction
|
41
|
-
#
|
42
|
-
# This class works with two length that mean orthogonal coordinates. The
|
43
|
-
# initial coordinate system has the origin at the top/left with the x-axis
|
44
|
-
# pointing to the right and the y-axis pointing down.
|
25
|
+
# Class representing a coordinate. This class works with two length that mean
|
26
|
+
# orthogonal coordinates. The initial coordinate system has the origin at the
|
27
|
+
# top/left with the x-axis pointing to the right and the y-axis pointing down.
|
45
28
|
#
|
46
|
-
# The equality operator '
|
29
|
+
# The equality operator '{#== ==}' does not test equality instance but test
|
47
30
|
# equality value of x-coordinate and y-coordinate.
|
48
31
|
#
|
49
|
-
# == Ways of
|
32
|
+
# == Ways of Calculating
|
50
33
|
#
|
51
|
-
# This class suports following arithmetic operators and methods:
|
52
|
-
#
|
53
|
-
#
|
54
|
-
# then calculate.
|
34
|
+
# This class suports following arithmetic operators and methods: {#+ +},
|
35
|
+
# {#- -}, {#* *}, {#/ /}, {#** **}, {#quo}. The operators '{#+ +}', '{#- -}'
|
36
|
+
# coerces a right hand operand into Coordinate, and then calculates.
|
55
37
|
#
|
56
|
-
#
|
38
|
+
# @since 0.0.0
|
57
39
|
class Coordinate
|
58
|
-
@@default_format = '(x,y)'
|
59
40
|
|
60
|
-
|
41
|
+
# @private
|
42
|
+
@@default_format = '(x,y)'
|
61
43
|
|
62
|
-
#
|
63
|
-
#
|
64
|
-
|
65
|
-
|
66
|
-
#
|
67
|
-
#
|
44
|
+
# Returns an x-coordinate
|
45
|
+
# @return [Length] an x-coordinate
|
46
|
+
attr_reader :x
|
47
|
+
|
48
|
+
# Returns a y-coordinate
|
49
|
+
# @return [Length] a y-coordinate
|
50
|
+
attr_reader :y
|
51
|
+
|
52
|
+
# @overload initialize(coordinate)
|
53
|
+
# Returns the argument itself.
|
54
|
+
# @param [Coordinate] coordinate the source coordinate
|
55
|
+
# @overload initialize(array)
|
56
|
+
# Return a new instance of Coordinate. First element of _array_ is used
|
57
|
+
# for x-coordinate, second element of _array_ is used y-coordinate.
|
58
|
+
# @param [Array<Length, Number, String>] array an array converted into
|
59
|
+
# Coordinate
|
60
|
+
# @raise [ArgumentError] size of _array_ does not equal to 2
|
61
|
+
# @overload initialize(x, y)
|
62
|
+
# @param [Length, Number, String] x an x-cooridnate
|
63
|
+
# @param [Length, Number, String] y a y-cooridnate
|
64
|
+
# @raise [TypeError] the argument can not be coerced into +Coordinate+
|
65
|
+
# @see .new
|
68
66
|
def initialize(*args)
|
69
67
|
case args.size
|
70
68
|
when 1
|
@@ -87,70 +85,123 @@ module DYI #:nodoc:
|
|
87
85
|
end
|
88
86
|
end
|
89
87
|
|
88
|
+
# The origin point.
|
90
89
|
ZERO = new(0,0)
|
91
90
|
|
91
|
+
# Unary Plus -- Returns the receiver's value.
|
92
|
+
# @return [Length] receiver itself
|
92
93
|
def +@
|
93
94
|
self
|
94
95
|
end
|
95
96
|
|
97
|
+
# Unary Minus -- Returns a coordinate whose x-coordinate and y-coordinate
|
98
|
+
# negated.
|
99
|
+
# @return [Length] the negated receiver's value
|
96
100
|
def -@
|
97
101
|
self.class.new(-@x, -@y)
|
98
102
|
end
|
99
103
|
|
104
|
+
# Returns a new coordinate which is the sum of the receiver and _other_.
|
105
|
+
# First, _other_ is converted into +Coordinate+.
|
106
|
+
# @param [Coordinate, Array<Length, Number, String>] other the value that
|
107
|
+
# can be converted into +Coordinate+
|
108
|
+
# @return [Length] a new length which is the sum of the receiver and _other_
|
100
109
|
def +(other)
|
101
110
|
other = self.class.new(other)
|
102
111
|
self.class.new(@x + other.x, @y + other.y)
|
103
112
|
end
|
104
113
|
|
114
|
+
# Returns a new length which is the difference of the receiver and _other_.
|
115
|
+
# First _other_ is converted into +Coordinate+.
|
116
|
+
# @param [Length, Numeric, String] other the value that can be converted
|
117
|
+
# into +Coordinate+
|
118
|
+
# @return [Length] a new length which is the difference of the receiver and
|
119
|
+
# _other_
|
105
120
|
def -(other)
|
106
121
|
other = self.class.new(other)
|
107
122
|
self.class.new(@x - other.x, @y - other.y)
|
108
123
|
end
|
109
124
|
|
125
|
+
# Returns a new muliplicative coordinate of the receiver by _number_.
|
126
|
+
# @param [Numeric] number the operand value
|
127
|
+
# @return [Length] a new muliplicative length
|
110
128
|
def *(number)
|
111
129
|
self.class.new(@x * number, @y * number)
|
112
130
|
end
|
113
131
|
|
132
|
+
# Raises a coordinate the _number_ power.
|
133
|
+
# @param [Numeric] number the operand value
|
134
|
+
# @return [Length] a coordinate the _number_ power
|
114
135
|
def **(number)
|
115
136
|
self.class.new(@x ** number, @y ** number)
|
116
137
|
end
|
117
138
|
|
118
|
-
|
139
|
+
# Returns a new divisional length of the receiver by _number_.
|
140
|
+
# @param [Numeric] other the operand value
|
141
|
+
# @return [Length] a new divisional length
|
142
|
+
# @raise [TypeError] _other_ can't be coerced into Numeric
|
143
|
+
def /(number)
|
119
144
|
raise TypeError, "#{number.class} can't be coerced into Numeric" unless number.kind_of?(Numeric)
|
120
145
|
self.class.new(@x.quo(number.to_f), @y.quo(number.to_f))
|
121
146
|
end
|
122
147
|
|
123
|
-
alias /
|
148
|
+
alias quo /
|
124
149
|
|
150
|
+
# Returns whether the receiver is the origin point.
|
151
|
+
# @return [Boolean] true if the receiver is the origin point, false
|
152
|
+
# otherwise
|
125
153
|
def zero?
|
126
154
|
@x.zero? && @y.zero?
|
127
155
|
end
|
128
156
|
|
157
|
+
# Returns whether the receiver is not the origin point.
|
158
|
+
# @return [Coordinate, nil] self if the receiver is not the origin point,
|
159
|
+
# nil otherwise
|
129
160
|
def nonzero?
|
130
161
|
zero? ? nil : self
|
131
162
|
end
|
132
163
|
|
164
|
+
# Returns whether the receiver equals to _other_.
|
165
|
+
# @param [Object] other an object
|
166
|
+
# @return [Boolean] true if _other_ is an instance of +Coordinate+ and
|
167
|
+
# each coordinate of receiver equals to a coordinate of _other_, false
|
168
|
+
# otherwise
|
133
169
|
def ==(other)
|
134
170
|
return false unless other.kind_of?(self.class)
|
135
171
|
@x == other.x && @y == other.y
|
136
172
|
end
|
137
173
|
|
174
|
+
# Returns a distance between receiver and the origin point.
|
175
|
+
# @return [Length] a distance between receiver and the origin point
|
138
176
|
def abs
|
139
177
|
(@x ** 2 + @y ** 2) ** 0.5
|
140
178
|
end
|
141
179
|
|
180
|
+
# Returns a distance between receiver and _origin_.
|
181
|
+
# @return [Length] a distance between receiver and _origin_
|
142
182
|
def distance(other)
|
143
183
|
(self - other).abs
|
144
184
|
end
|
145
185
|
|
186
|
+
# Returns a coordinate that converted into the user unit.
|
187
|
+
# @return [Coordinate] a coordinate that converted into the user unit
|
146
188
|
def to_user_unit
|
147
189
|
self.class.new(@x.to_user_unit, @y.to_user_unit)
|
148
190
|
end
|
149
191
|
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
#
|
192
|
+
# Returns a string to represent the receiver.
|
193
|
+
#
|
194
|
+
# Format string can be specified for the argument. If no argument is given,
|
195
|
+
# {.default_format} is used as format string. About format string, see the
|
196
|
+
# documentation of {.default_format} method.
|
197
|
+
# @param [String] format a format string
|
198
|
+
# @return [Length] a string to represent the receiver
|
199
|
+
# @example
|
200
|
+
# point = DYI::Coordinate.new(10, 20)
|
201
|
+
# point.to_s('<x, y>') # => "<10, 20>"
|
202
|
+
# point.to_s('\\x:x, \\y:y') # => "x:10, y:20"
|
203
|
+
# @see .default_format=
|
204
|
+
# @see .set_default_format
|
154
205
|
def to_s(format=nil)
|
155
206
|
fmts = (format || @@default_format).split('\\\\')
|
156
207
|
fmts = fmts.map do |fmt|
|
@@ -159,7 +210,8 @@ module DYI #:nodoc:
|
|
159
210
|
fmts.join('\\')
|
160
211
|
end
|
161
212
|
|
162
|
-
|
213
|
+
# @private
|
214
|
+
def inspect
|
163
215
|
"(#{@x.inspect}, #{@y.inspect})"
|
164
216
|
end
|
165
217
|
|
@@ -167,26 +219,84 @@ module DYI #:nodoc:
|
|
167
219
|
|
168
220
|
public
|
169
221
|
|
170
|
-
|
222
|
+
# Creates and returns a new instance of Coordinate provided the argument
|
223
|
+
# is not an instace of Coordinate. If the argument is an instace of
|
224
|
+
# Coordinate, returns the argument itself.
|
225
|
+
# @overload new(coordinate)
|
226
|
+
# Returns the argument itself.
|
227
|
+
# @param [Coordinate] coordinate the source coordinate
|
228
|
+
# @overload new(array)
|
229
|
+
# Return a new instance of Coordinate. First element of _array_ is used
|
230
|
+
# for x-coordinate, second element of _array_ is used y-coordinate.
|
231
|
+
# @param [Array<Length, Number, String>] array an array converted into
|
232
|
+
# Coordinate
|
233
|
+
# @raise [ArgumentError] size of _array_ does not equal to 2
|
234
|
+
# @overload new(x, y)
|
235
|
+
# @param [Length, Number, String] x an x-cooridnate
|
236
|
+
# @param [Length, Number, String] y a y-cooridnate
|
237
|
+
# @raise (see #initialize)
|
238
|
+
# @example
|
239
|
+
# x = DYI::Length(10)
|
240
|
+
# y = DYI::Length(20)
|
241
|
+
# point1 = DYI::Coordinate.new(x, y) # this point is (10, 20)
|
242
|
+
# point2 = DYI::Coordinate.new(10, 20) # it is (10, 20) too
|
243
|
+
# point3 = DYI::Coordinate.new([x, y]) # it is (10, 20) too
|
244
|
+
# point4 = DYI::Coordinate.new([10, 20]) # it is (10, 20) too
|
245
|
+
# point5 = DYI::Coordinate.new(['10px', '20px']) # it is (10, 20) too
|
246
|
+
def new(*args)
|
171
247
|
return args.first if args.size == 1 && args.first.instance_of?(self)
|
172
248
|
super
|
173
249
|
end
|
174
250
|
|
175
|
-
|
176
|
-
#
|
177
|
-
#
|
251
|
+
# Returns a new instace of Coordinate if the argments is not +nil+ (calls
|
252
|
+
# +Coordinate.new+ method), but returns +nil+ if the argument is +nil+.
|
253
|
+
# @return [Coordinate, nil] a new instace of Length if the argments is not
|
254
|
+
# nil, nil otherwise
|
255
|
+
# @see .new
|
178
256
|
def new_or_nil(*args)
|
179
257
|
(args.size == 1 && args.first.nil?) ? nil : new(*args)
|
180
258
|
end
|
181
259
|
|
260
|
+
# Creates a new instance of Coordinate using the cartesian coordinates,
|
261
|
+
# and returns it.
|
262
|
+
# @param [Length, Number, String] x an x-cooridnate
|
263
|
+
# @param [Length, Number, String] y a y-cooridnate
|
182
264
|
def orthogonal_coordinates(x, y)
|
183
265
|
new(x, y)
|
184
266
|
end
|
185
267
|
|
268
|
+
# Creates a new instance of Coordinate using the polar coordinates,
|
269
|
+
# and returns it.
|
270
|
+
# @param [Length, Number, String] radius distance from the origin point
|
271
|
+
# @param [Numeric] theta the angle from x-direction in degrees
|
186
272
|
def polar_coordinates(radius, theta)
|
187
273
|
new(radius * DYI::Util.cos(theta), radius * DYI::Util.sin(theta))
|
188
274
|
end
|
189
275
|
|
276
|
+
# Invokes block with given format string as default format.
|
277
|
+
# @overload set_default_format(format)
|
278
|
+
# Invokes block with given _format_ as default format. After invokes the
|
279
|
+
# block, the original format is used.
|
280
|
+
# @param [String] format a format string
|
281
|
+
# @yield a block which the format string is used in
|
282
|
+
# @return [Length] the receiver itself
|
283
|
+
# @overload set_default_format(format)
|
284
|
+
# Sets default format setring as {.default_format=} method.
|
285
|
+
# @param [String] format a format string
|
286
|
+
# @return [String] the given argument
|
287
|
+
# @example
|
288
|
+
# # an initial format string is "(x,y)"
|
289
|
+
# point = DYI::Coordinate.new(10, 20)
|
290
|
+
# point.to_s # => "(10,20)"
|
291
|
+
# DYI::Coordinate.set_default_format('<x, y>') {
|
292
|
+
# point.to_s # => "<10, 20>"
|
293
|
+
# DYI::Length.set_default_format('0.0u') {
|
294
|
+
# point.to_s # => "<10.0pt, 20.0pt>"
|
295
|
+
# }
|
296
|
+
# }
|
297
|
+
# point.to_s # => "(10,20)"
|
298
|
+
# @see Length.set_default_format
|
299
|
+
# @see .default_format=
|
190
300
|
def set_default_format(format)
|
191
301
|
if block_given?
|
192
302
|
org_format = default_format
|
@@ -199,23 +309,29 @@ module DYI #:nodoc:
|
|
199
309
|
end
|
200
310
|
end
|
201
311
|
|
312
|
+
# Returns a format that is used when called {#to_s} without an argument.
|
313
|
+
# @return [String] a format string
|
314
|
+
# @see .default_format=
|
202
315
|
def default_format
|
203
316
|
@@default_format
|
204
317
|
end
|
205
318
|
|
206
|
-
# Sets format that is used when called to_s
|
207
|
-
#
|
208
|
-
#
|
209
|
-
#
|
319
|
+
# Sets a format string that is used when called {#to_s} without an
|
320
|
+
# argument. The format string that is set at this method is used
|
321
|
+
# permanently. Use {.set_default_format} with a block when you want to use
|
322
|
+
# a format string temporarily.
|
210
323
|
#
|
211
|
-
#
|
212
|
-
#
|
213
|
-
#
|
214
|
-
#
|
215
|
-
#
|
216
|
-
#
|
217
|
-
#
|
218
|
-
#
|
324
|
+
# Uses the following characters as coordinate format strings.
|
325
|
+
# [<tt>"x"</tt> (x-coordinate placeholder)] Placeholder '+x+' is replaced
|
326
|
+
# as x-coordinate.
|
327
|
+
# [<tt>"y"</tt> (y-coordinate placeholder)] Placeholder '+y+' is replaced
|
328
|
+
# as y-coordinate.
|
329
|
+
# [<tt>"\\"</tt> (Escape Character)] Causes the next character to be
|
330
|
+
# interpreted as a literal.
|
331
|
+
# @see #to_s
|
332
|
+
# @see .set_default_format
|
333
|
+
# @see Length.default_format=
|
334
|
+
# @see Numeric#strfnum
|
219
335
|
def default_format=(fromat)
|
220
336
|
@@default_format = fromat.clone
|
221
337
|
end
|