typographic-unit 0.1.1 → 0.2.0
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/.gitignore +7 -0
- data/Gemfile +4 -0
- data/HISTORY.md +12 -0
- data/{License.txt → LICENSE.txt} +4 -2
- data/README.md +87 -0
- data/Rakefile +16 -4
- data/lib/typographic-unit.rb +26 -284
- data/lib/typographic-unit/american-point.rb +7 -0
- data/lib/typographic-unit/big-point.rb +7 -0
- data/lib/typographic-unit/centimeter.rb +7 -0
- data/lib/typographic-unit/cicero.rb +7 -0
- data/lib/typographic-unit/didot-point.rb +7 -0
- data/lib/typographic-unit/gou.rb +50 -0
- data/lib/typographic-unit/inch.rb +6 -0
- data/lib/typographic-unit/jis-point.rb +6 -0
- data/lib/typographic-unit/meter.rb +7 -0
- data/lib/typographic-unit/millimeter.rb +7 -0
- data/lib/typographic-unit/pica.rb +7 -0
- data/lib/typographic-unit/postscript-point.rb +7 -0
- data/lib/typographic-unit/q.rb +8 -0
- data/lib/typographic-unit/scaled-point.rb +7 -0
- data/lib/typographic-unit/tex-point.rb +6 -0
- data/lib/typographic-unit/unit.rb +261 -0
- data/lib/typographic-unit/version.rb +2 -7
- data/{spec/typographic-unit_spec.rb → test/spec_typographic-unit.rb} +30 -32
- data/typographic-unit.gemspec +21 -0
- metadata +76 -68
- data/History.txt +0 -4
- data/Manifest.txt +0 -26
- data/README.txt +0 -27
- data/config/hoe.rb +0 -83
- data/config/requirements.rb +0 -17
- data/log/debug.log +0 -0
- data/script/destroy +0 -14
- data/script/generate +0 -14
- data/script/txt2html +0 -74
- data/setup.rb +0 -1585
- data/spec/spec.opts +0 -1
- data/spec/spec_helper.rb +0 -10
- data/tasks/deployment.rake +0 -34
- data/tasks/environment.rake +0 -7
- data/tasks/rspec.rake +0 -21
- data/tasks/website.rake +0 -17
- data/website/index.html +0 -180
- data/website/index.txt +0 -103
- data/website/javascripts/rounded_corners_lite.inc.js +0 -285
- data/website/stylesheets/screen.css +0 -138
- data/website/template.rhtml +0 -48
@@ -0,0 +1,50 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module TypographicUnit
|
3
|
+
# Gou is a unit of Japanese Gou(号).
|
4
|
+
# See http://www.um.u-tokyo.ac.jp/publish_db/1996Moji/05/5901.html and
|
5
|
+
# http://www.asahi-net.or.jp/~ax2s-kmtn/ref/type_size.html for details.
|
6
|
+
# In this library, "初号" is Gou.new(:first) or 0.gou.
|
7
|
+
class Gou < Unit
|
8
|
+
unit :gou, AmericanPoint, nil
|
9
|
+
|
10
|
+
def initialize(value)
|
11
|
+
if value.kind_of?(Unit)
|
12
|
+
raise ArgumentError.new, value
|
13
|
+
end
|
14
|
+
unless (0..8).include?(value) or value == :first
|
15
|
+
raise ArgumentError.new, value
|
16
|
+
end
|
17
|
+
@value = value
|
18
|
+
end
|
19
|
+
|
20
|
+
# Return a size according to "初号".
|
21
|
+
def self.first
|
22
|
+
0.gou
|
23
|
+
end
|
24
|
+
|
25
|
+
# Convert the value into american point.
|
26
|
+
def scaled_point
|
27
|
+
val = case @value
|
28
|
+
when :first, 0
|
29
|
+
42.american_pt
|
30
|
+
when 1
|
31
|
+
27.5.american_pt
|
32
|
+
when 2
|
33
|
+
21.american_pt
|
34
|
+
when 3
|
35
|
+
15.75.american_pt
|
36
|
+
when 4
|
37
|
+
13.75.american_pt
|
38
|
+
when 5
|
39
|
+
10.5.american_pt
|
40
|
+
when 6
|
41
|
+
7.875.american_pt
|
42
|
+
when 7
|
43
|
+
5.25.american_pt
|
44
|
+
when 8
|
45
|
+
3.9375.american_pt
|
46
|
+
end
|
47
|
+
val.kind_of?(ScaledPoint) ? val : val.scaled_point
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
module TypographicUnit
|
2
|
+
# This is a base class for all unit classes.
|
3
|
+
class Unit < ::Numeric
|
4
|
+
class << self
|
5
|
+
# @api private
|
6
|
+
def register(short, unit)
|
7
|
+
TypographicUnit::TABLE[short] = unit
|
8
|
+
end
|
9
|
+
private :register
|
10
|
+
|
11
|
+
# Define a unit. This method is used in concrete unit classes.
|
12
|
+
#
|
13
|
+
# @param short [Symbol]
|
14
|
+
# short name for the unit
|
15
|
+
# @param base [Unit]
|
16
|
+
# base unit class
|
17
|
+
# @param size [Rational]
|
18
|
+
# unit size
|
19
|
+
# @return [void]
|
20
|
+
def unit(short, base, size)
|
21
|
+
@short = short
|
22
|
+
@base = base
|
23
|
+
@size = size
|
24
|
+
register(short, self)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Return short name of the unit.
|
28
|
+
#
|
29
|
+
# @return [Symbol]
|
30
|
+
# short name of the unit
|
31
|
+
def short
|
32
|
+
@short
|
33
|
+
end
|
34
|
+
|
35
|
+
# Return base unit class.
|
36
|
+
#
|
37
|
+
# @return [Class]
|
38
|
+
# base unit class
|
39
|
+
def base
|
40
|
+
@base
|
41
|
+
end
|
42
|
+
|
43
|
+
# Return size of the unit.
|
44
|
+
#
|
45
|
+
# @return [Rational]
|
46
|
+
# size of the unit
|
47
|
+
def size
|
48
|
+
@size
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
attr_reader :value
|
53
|
+
|
54
|
+
def initialize(value)
|
55
|
+
unless value.kind_of?(Numeric) and not(value.kind_of?(Unit))
|
56
|
+
raise ArgumentError.new, value
|
57
|
+
end
|
58
|
+
@value = value
|
59
|
+
end
|
60
|
+
|
61
|
+
# Convert the value into scaled point.
|
62
|
+
#
|
63
|
+
# @return [ScaledPoint]
|
64
|
+
# scaled point representation of the length
|
65
|
+
def scaled_point
|
66
|
+
val = self.class.base.new(@value * self.class.size)
|
67
|
+
val.kind_of?(ScaledPoint) ? val : val.scaled_point
|
68
|
+
end
|
69
|
+
|
70
|
+
# @api private
|
71
|
+
def <=>(other)
|
72
|
+
raise ArgumentError, other unless other.kind_of?(Unit)
|
73
|
+
self.scaled_point.value.to_i <=> other.scaled_point.value.to_i
|
74
|
+
end
|
75
|
+
|
76
|
+
# Convert the length into other unit.
|
77
|
+
#
|
78
|
+
# @param other [Unit]
|
79
|
+
# target unit
|
80
|
+
def >>(other)
|
81
|
+
oclass = other.kind_of?(Symbol) ? TABLE[other] : other
|
82
|
+
u = oclass.new(1)
|
83
|
+
oclass.new((self.scaled_point.value / u.scaled_point.value).to_f)
|
84
|
+
end
|
85
|
+
|
86
|
+
alias :convert :>>
|
87
|
+
|
88
|
+
# Same as +Number#%+.
|
89
|
+
def %(other)
|
90
|
+
case other
|
91
|
+
when Unit
|
92
|
+
self.class.new(@value % (other >> self.class).value)
|
93
|
+
when Integer, Float
|
94
|
+
self.class.new(@value % other)
|
95
|
+
else
|
96
|
+
raise ArgumentError, other
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Same as +Number#div+.
|
101
|
+
def div(other)
|
102
|
+
case other
|
103
|
+
when Unit
|
104
|
+
@value.div((other >> self.class).value)
|
105
|
+
when Integer, Float
|
106
|
+
@value.div(other)
|
107
|
+
else
|
108
|
+
raise ArgumentError, other
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Same as +Number#quo+.
|
113
|
+
def quo(other)
|
114
|
+
case other
|
115
|
+
when Unit
|
116
|
+
@value.quo((other >> self.class).value)
|
117
|
+
when Integer, Float
|
118
|
+
@value.quo(other)
|
119
|
+
else
|
120
|
+
raise ArgumentError, other
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Same as Number#+@.
|
125
|
+
def +@
|
126
|
+
self.class.new(+@value)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Same as +Number#-@+.
|
130
|
+
def -@
|
131
|
+
self.class.new(-@value)
|
132
|
+
end
|
133
|
+
|
134
|
+
# Same as +Number#abs+.
|
135
|
+
def abs
|
136
|
+
self.class.new(@value.abs)
|
137
|
+
end
|
138
|
+
|
139
|
+
# Perform addition.
|
140
|
+
#
|
141
|
+
# @param other [Unit]
|
142
|
+
# additional value
|
143
|
+
# @return [Unit]
|
144
|
+
# new value of the unit
|
145
|
+
# @example
|
146
|
+
# 1.cm + 1.mm #=> 1.1.cm
|
147
|
+
def +(other)
|
148
|
+
raise ArgumentError, other unless other.kind_of?(Unit)
|
149
|
+
self.class.new((@value + (other >> self.class).value).to_f)
|
150
|
+
end
|
151
|
+
|
152
|
+
# Perform subtraction.
|
153
|
+
#
|
154
|
+
# @param other [Unit]
|
155
|
+
# subtractional value
|
156
|
+
# @return [Unit]
|
157
|
+
# new value of the unit
|
158
|
+
# @example
|
159
|
+
# 1.cm - 1.mm #=> 0.9.cm
|
160
|
+
def -(other)
|
161
|
+
raise ArgumentError, other unless other.kind_of?(Unit)
|
162
|
+
self.class.new((@value - (other >> self.class).value).to_f)
|
163
|
+
end
|
164
|
+
|
165
|
+
# Same as +Number#*+.
|
166
|
+
def *(other)
|
167
|
+
raise ArgumentError, other unless other.kind_of?(Integer) or other.kind_of?(Float)
|
168
|
+
self.class.new(@value * other)
|
169
|
+
end
|
170
|
+
|
171
|
+
# Same as +Number#ceil+.
|
172
|
+
def ceil
|
173
|
+
self.class.new(@value.ceil)
|
174
|
+
end
|
175
|
+
|
176
|
+
# Same as +Number#floor+.
|
177
|
+
def floor
|
178
|
+
self.class.new(@value.floor)
|
179
|
+
end
|
180
|
+
|
181
|
+
# Same as +Number#round+.
|
182
|
+
def round
|
183
|
+
self.class.new(@value.round)
|
184
|
+
end
|
185
|
+
|
186
|
+
# Same as +Number#truncate+.
|
187
|
+
def truncate
|
188
|
+
self.class.new(@value.truncate)
|
189
|
+
end
|
190
|
+
|
191
|
+
# Same as +Number#integer?+.
|
192
|
+
def integer?
|
193
|
+
@value.integer?
|
194
|
+
end
|
195
|
+
|
196
|
+
# Same as +Number#nonzero?+.
|
197
|
+
def nonzero?
|
198
|
+
@value.nonzero?
|
199
|
+
end
|
200
|
+
|
201
|
+
# Convert to float representation.
|
202
|
+
#
|
203
|
+
# @return [Unit]
|
204
|
+
# same unit but the value is float
|
205
|
+
# @example
|
206
|
+
# 1.cm.to_f #=> 1.0.cm
|
207
|
+
def to_f
|
208
|
+
self.class.new(@value.to_f)
|
209
|
+
end
|
210
|
+
|
211
|
+
# Convert to int representaion.
|
212
|
+
#
|
213
|
+
# @return [Unit]
|
214
|
+
# same unit but the value is int
|
215
|
+
# @example
|
216
|
+
# 1.0.cm.to_i #=> 1.cm
|
217
|
+
def to_i
|
218
|
+
self.class.new(@value.to_i)
|
219
|
+
end
|
220
|
+
|
221
|
+
# Convert to int representation.
|
222
|
+
#
|
223
|
+
# @return [Unit]
|
224
|
+
# same unit but the value is float
|
225
|
+
# @example
|
226
|
+
# 1.cm.to_f #=> 1.0.cm
|
227
|
+
def to_int
|
228
|
+
@value.to_i
|
229
|
+
end
|
230
|
+
|
231
|
+
# Same as +Number#zero?+.
|
232
|
+
def zero?
|
233
|
+
@value.zero?
|
234
|
+
end
|
235
|
+
|
236
|
+
# Same as +Number#step+ but you can specify +step+ as unit length.
|
237
|
+
#
|
238
|
+
# @param limit [Unit]
|
239
|
+
# limit length
|
240
|
+
# @param step [Unit]
|
241
|
+
# step
|
242
|
+
def step(limit, step=1)
|
243
|
+
step = step.kind_of?(Unit) ? step : self.class.new(step)
|
244
|
+
@value.step(limit.value, (step >> self.class).value) do |n|
|
245
|
+
yield(self.class.new(n)) if block_given?
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
# Return string format.
|
250
|
+
def to_s
|
251
|
+
@value.to_s + self.class.short.to_s
|
252
|
+
end
|
253
|
+
|
254
|
+
# @api private
|
255
|
+
def inspect
|
256
|
+
"#<#{@value.to_s}#{self.class.short.to_s}>"
|
257
|
+
end
|
258
|
+
|
259
|
+
alias :eql? :"=="
|
260
|
+
end
|
261
|
+
end
|