typographic-unit 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.gitignore +7 -0
  2. data/Gemfile +4 -0
  3. data/HISTORY.md +12 -0
  4. data/{License.txt → LICENSE.txt} +4 -2
  5. data/README.md +87 -0
  6. data/Rakefile +16 -4
  7. data/lib/typographic-unit.rb +26 -284
  8. data/lib/typographic-unit/american-point.rb +7 -0
  9. data/lib/typographic-unit/big-point.rb +7 -0
  10. data/lib/typographic-unit/centimeter.rb +7 -0
  11. data/lib/typographic-unit/cicero.rb +7 -0
  12. data/lib/typographic-unit/didot-point.rb +7 -0
  13. data/lib/typographic-unit/gou.rb +50 -0
  14. data/lib/typographic-unit/inch.rb +6 -0
  15. data/lib/typographic-unit/jis-point.rb +6 -0
  16. data/lib/typographic-unit/meter.rb +7 -0
  17. data/lib/typographic-unit/millimeter.rb +7 -0
  18. data/lib/typographic-unit/pica.rb +7 -0
  19. data/lib/typographic-unit/postscript-point.rb +7 -0
  20. data/lib/typographic-unit/q.rb +8 -0
  21. data/lib/typographic-unit/scaled-point.rb +7 -0
  22. data/lib/typographic-unit/tex-point.rb +6 -0
  23. data/lib/typographic-unit/unit.rb +261 -0
  24. data/lib/typographic-unit/version.rb +2 -7
  25. data/{spec/typographic-unit_spec.rb → test/spec_typographic-unit.rb} +30 -32
  26. data/typographic-unit.gemspec +21 -0
  27. metadata +76 -68
  28. data/History.txt +0 -4
  29. data/Manifest.txt +0 -26
  30. data/README.txt +0 -27
  31. data/config/hoe.rb +0 -83
  32. data/config/requirements.rb +0 -17
  33. data/log/debug.log +0 -0
  34. data/script/destroy +0 -14
  35. data/script/generate +0 -14
  36. data/script/txt2html +0 -74
  37. data/setup.rb +0 -1585
  38. data/spec/spec.opts +0 -1
  39. data/spec/spec_helper.rb +0 -10
  40. data/tasks/deployment.rake +0 -34
  41. data/tasks/environment.rake +0 -7
  42. data/tasks/rspec.rake +0 -21
  43. data/tasks/website.rake +0 -17
  44. data/website/index.html +0 -180
  45. data/website/index.txt +0 -103
  46. data/website/javascripts/rounded_corners_lite.inc.js +0 -285
  47. data/website/stylesheets/screen.css +0 -138
  48. data/website/template.rhtml +0 -48
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ html
2
+ *~
3
+ Gemfile.lock
4
+ gems
5
+ .bundle
6
+ .yardoc
7
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # -*- ruby -*-
2
+
3
+ source 'https://rubygems.org'
4
+ gemspec
data/HISTORY.md ADDED
@@ -0,0 +1,12 @@
1
+ ## 0.2.0 2013-03-10
2
+
3
+ * Changed to use bundler
4
+ * Refactoring
5
+
6
+ ## 0.1.1 2007-12-30
7
+
8
+ * add AmericanPoint and Gou
9
+
10
+ ## 0.1.0 2007-12-19
11
+
12
+ * Initial release
@@ -1,4 +1,6 @@
1
- Copyright (c) 2007 Keita Yamaguchi
1
+ Copyright (c) 2007-2013 Keita Yamaguchi
2
+
3
+ MIT License
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
@@ -17,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # typographic-unit
2
+
3
+ typographic-unit is a Ruby library for converting between typographic units
4
+ according to TeX's way. This library can handle the following units:
5
+
6
+ * TeX Scaled Point(sp)
7
+ * TeX Point(pt)
8
+ * Pica(pt)
9
+ * Inch(in)
10
+ * TeX Big Point(bp)
11
+ * PostScript Point(ps_pt)
12
+ * Meter(m)
13
+ * Centimeter(cm)
14
+ * Milimeter(mm)
15
+ * Didot Point(dd)
16
+ * Cicero(cc)
17
+ * Japanese Q(q)
18
+ * American Point(american_pt)
19
+ * JIS Point(jis_pt)
20
+ * Japanese Gou(gou)
21
+
22
+ ## Installing
23
+
24
+ gem install typographic-unit
25
+
26
+ ## How to use
27
+
28
+ ### Basics
29
+
30
+ ```ruby
31
+ require "typographic-unit"
32
+ 1.cm # => #<1cm>
33
+ 1.cm - 1.mm # => #<0.9cm>
34
+ 1.cm == 10.mm # => true
35
+ 1.in >> :cm # => #<2.54cm>
36
+ 2.54.cm >> :in # => #<1.0in>
37
+ 1.in - 1.cm # => #<0.606299212598425in>
38
+ (1.in - 1.cm) >> :cm # => #<1.54cm>
39
+ ```
40
+
41
+ ### Convert
42
+
43
+ ```ruby
44
+ 1.pt >> :mm # => #<0.351459803514598mm>
45
+ 7227.pt >> :cm # => #<254.0cm>
46
+ 1.in >> :bp # => #<72.0bp>
47
+ 1.pt >> :sp # => #<65536.0sp>
48
+ 1157.pt >> :pt # => #<1238.0pt>
49
+ 1.cc >> :dd # => #<12.0dd>
50
+ 10.q >> :mm # => #<2.5mm>
51
+ 1.jis_pt >> :mm # => #<0.3514mm>
52
+ ```
53
+
54
+ ### Calculate
55
+
56
+ ```ruby
57
+ 1.cm + 1.mm # => #<1.1cm>
58
+ 1.cm + 1.in # => #<3.54cm>
59
+ 1.pt - 1.bp # => #<-0.00131797426317971mm>
60
+ 100.ps_pt - 100.jis_pt >> :mm # => #<0.137777777777779mm>
61
+ 1.cm * 10 # => #<10cm>
62
+ ```
63
+
64
+ ### Step
65
+
66
+ ```ruby
67
+ list = []
68
+ 1.cm.step(3.cm, 0.5.cm) do |i|
69
+ list << i
70
+ end
71
+ list # => [1.cm, 1.5.cm, 2.cm, 2.5.cm, 3.cm]
72
+ ```
73
+
74
+ ## License
75
+
76
+ This code is free to use under the terms of the MIT license.
77
+
78
+ ## Reference
79
+
80
+ * "Wikipedia:活字":http://ja.wikipedia.org/wiki/%E6%B4%BB%E5%AD%97
81
+ * "歴史の文字 記載・活字・活版 第三部 活版の世界":http://www.um.u-tokyo.ac.jp/publish_db/1996Moji/05/5901.html
82
+ * "CyberLibrarian:文字サイズ":http://www.asahi-net.or.jp/~ax2s-kmtn/ref/type_size.html
83
+
84
+ ## Links
85
+
86
+ * [Project Home](http://typographic-unit.github.com/)
87
+ * [Repository](http://rubyforge.org/projects/typographicunit/)
data/Rakefile CHANGED
@@ -1,4 +1,16 @@
1
- require 'config/requirements'
2
- require 'config/hoe' # setup Hoe + all gem configuration
3
-
4
- Dir['tasks/**/*.rake'].each { |rake| load rake }
1
+ require "bundler/gem_tasks"
2
+
3
+ desc 'Test specs'
4
+ task 'test' do
5
+ sh "bacon -a"
6
+ end
7
+
8
+ desc 'Generate API document'
9
+ task 'html' do
10
+ sh "yard doc -o html --hide-void-return --no-api"
11
+ end
12
+
13
+ desc 'Show undocumented function list'
14
+ task 'html:undoc' do
15
+ sh "yard stats --list-undoc --no-api"
16
+ end
@@ -1,10 +1,11 @@
1
- require "rational"
2
-
1
+ # TypographicUnit is a namespece for unit classes.
3
2
  module TypographicUnit
4
- Table = Hash.new
3
+ # @api private
4
+ TABLE = Hash.new
5
5
 
6
- def self.open #:nodoc:
7
- Table.values.each do |unit|
6
+ # Initialize typographic-unit. This makes unit methods for Numeric.
7
+ def self.init
8
+ TABLE.values.each do |unit|
8
9
  ::Numeric.instance_eval do
9
10
  define_method(unit.short) do
10
11
  unit.new(self)
@@ -12,285 +13,26 @@ module TypographicUnit
12
13
  end
13
14
  end
14
15
  end
15
-
16
- class Unit < ::Numeric
17
- def self.register(short, unit)
18
- TypographicUnit::Table[short] = unit
19
- end
20
-
21
- def self.unit(short, base, size)
22
- @short = short
23
- @base = base
24
- @size = size
25
- register(short, self)
26
- end
27
-
28
- def self.short; @short; end
29
- def self.base; @base; end
30
- def self.size; @size; end
31
-
32
- attr_reader :value
33
-
34
- def initialize(value)
35
- unless value.kind_of?(Numeric) and not(value.kind_of?(Unit))
36
- raise ArgumentError.new, value
37
- end
38
- @value = value
39
- end
40
-
41
- # Convert the value into scaled point.
42
- def scaled_point
43
- val = self.class.base.new(@value * self.class.size)
44
- val.kind_of?(ScaledPoint) ? val : val.scaled_point
45
- end
46
-
47
- def <=>(other)
48
- raise ArgumentError, other unless other.kind_of?(Unit)
49
- self.scaled_point.value.to_i <=> other.scaled_point.value.to_i
50
- end
51
-
52
- # Convert the length into other unit
53
- # other:: unit
54
- def >>(other)
55
- oclass = other.kind_of?(Symbol) ? Table[other] : other
56
- u = oclass.new(1)
57
- oclass.new((self.scaled_point.value / u.scaled_point.value).to_f)
58
- end
59
-
60
- def %(other)
61
- case other
62
- when Unit
63
- self.class.new(@value % (other >> self.class).value)
64
- when Integer, Float
65
- self.class.new(@value % other)
66
- else
67
- raise ArgumentError, other
68
- end
69
- end
70
-
71
- def div(other)
72
- case other
73
- when Unit
74
- @value.div((other >> self.class).value)
75
- when Integer, Float
76
- @value.div(other)
77
- else
78
- raise ArgumentError, other
79
- end
80
- end
81
-
82
- def quo(other)
83
- case other
84
- when Unit
85
- @value.quo((other >> self.class).value)
86
- when Integer, Float
87
- @value.quo(other)
88
- else
89
- raise ArgumentError, other
90
- end
91
- end
92
-
93
- def +@
94
- self.class.new(+@value)
95
- end
96
-
97
- def -@
98
- self.class.new(-@value)
99
- end
100
-
101
- def abs
102
- self.class.new(@value.abs)
103
- end
104
-
105
- def +(other)
106
- raise ArgumentError, other unless other.kind_of?(Unit)
107
- self.class.new((@value + (other >> self.class).value).to_f)
108
- end
109
-
110
- def -(other)
111
- raise ArgumentError, other unless other.kind_of?(Unit)
112
- self.class.new((@value - (other >> self.class).value).to_f)
113
- end
114
-
115
- def *(other)
116
- raise ArgumentError, other unless other.kind_of?(Integer) or other.kind_of?(Float)
117
- self.class.new(@value * other)
118
- end
119
-
120
- def ceil
121
- self.class.new(@value.ceil)
122
- end
123
-
124
- def floor
125
- self.class.new(@value.floor)
126
- end
127
-
128
- def round
129
- self.class.new(@value.round)
130
- end
131
-
132
- def truncate
133
- self.class.new(@value.truncate)
134
- end
135
-
136
- def integer?
137
- @value.integer?
138
- end
139
-
140
- def nonzero?
141
- @value.nonzero?
142
- end
143
-
144
- def to_i
145
- self.class.new(@value.to_i)
146
- end
147
-
148
- def to_f
149
- self.class.new(@value.to_f)
150
- end
151
-
152
- def to_int
153
- @value.to_i
154
- end
155
-
156
- def to_float
157
- @value.to_f
158
- end
159
-
160
- def zero?
161
- @value.zero?
162
- end
163
-
164
- def step(limit, _step=1)
165
- step = _step.kind_of?(Unit) ? _step : self.class.new(_step)
166
- @value.step(limit.value, (step >> self.class).value) do |n|
167
- yield(self.class.new(n)) if block_given?
168
- end
169
- end
170
-
171
- def to_s
172
- @value.to_s + self.class.short.to_s
173
- end
174
-
175
- def inspect
176
- "#<#{@value.to_s}#{self.class.short.to_s}>"
177
- end
178
-
179
- alias :eql? :==
180
- alias :convert :>>
181
- end
182
-
183
- class ScaledPoint < Unit
184
- unit :sp, self, 1
185
- end
186
-
187
- # TeXPoint is a unit of TeX point. 1 TeX point equals 65536 scaled point.
188
- class TeXPoint < Unit
189
- unit :pt, ScaledPoint, 65536
190
- end
191
-
192
- class Pica < Unit
193
- unit :pc, TeXPoint, 12
194
- end
195
-
196
- # Inch is a unit of inch. 7227 TeX point equals 100 inch.
197
- class Inch < Unit
198
- unit :in, TeXPoint, Rational(7227, 100)
199
- end
200
-
201
- class BigPoint < Unit
202
- unit :bp, Inch, Rational(1, 72)
203
- end
204
-
205
- class PostScriptPoint < Unit
206
- unit :ps_pt, BigPoint, 1
207
- end
208
-
209
- class Millimeter < Unit
210
- unit :mm, Inch, Rational(10, 254)
211
- end
212
-
213
- class Centimeter < Unit
214
- unit :cm, Millimeter, 10
215
- end
216
-
217
- class Meter < Unit
218
- unit :m, Centimeter, 100
219
- end
220
-
221
- class DidotPoint < Unit
222
- unit :dd, TeXPoint, Rational(1238, 1157)
223
- end
224
-
225
- class Cicero < Unit
226
- unit :cc, DidotPoint, 12
227
- end
228
-
229
- # Q is a unit of Japanese Q(級).
230
- class Q < Unit
231
- unit :q, Millimeter, Rational(25, 100)
232
- end
233
-
234
- # AmericanPoint is a unit of American point.
235
- # See http://www.ops.dti.ne.jp/~robundo/TMTaroY01.html for details.
236
- class AmericanPoint < Unit
237
- unit :american_pt, Millimeter, Rational(3514, 10000)
238
- end
239
-
240
- # JISPoint is a unit of JIS point. JIS point is just same as American point.
241
- class JISPoint < AmericanPoint
242
- unit :jis_pt, AmericanPoint, 1
243
- end
244
-
245
- # Gou is a unit of Japanese Gou(号).
246
- # See http://www.um.u-tokyo.ac.jp/publish_db/1996Moji/05/5901.html and
247
- # http://www.asahi-net.or.jp/~ax2s-kmtn/ref/type_size.html for details.
248
- # In this library, "初号" is Gou.new(:first) or 0.gou.
249
- class Gou < Unit
250
- unit :gou, AmericanPoint, nil
251
-
252
- def initialize(value)
253
- if value.kind_of?(Unit)
254
- raise ArgumentError.new, value
255
- end
256
- unless (0..8).include?(value) or value == :first
257
- raise ArgumentError.new, value
258
- end
259
- @value = value
260
- end
261
-
262
- # Return a size according to "初号".
263
- def self.first
264
- 0.gou
265
- end
266
-
267
- # Convert the value into american point.
268
- def scaled_point
269
- val = case @value
270
- when :first, 0
271
- 42.american_pt
272
- when 1
273
- 27.5.american_pt
274
- when 2
275
- 21.american_pt
276
- when 3
277
- 15.75.american_pt
278
- when 4
279
- 13.75.american_pt
280
- when 5
281
- 10.5.american_pt
282
- when 6
283
- 7.875.american_pt
284
- when 7
285
- 5.25.american_pt
286
- when 8
287
- 3.9375.american_pt
288
- end
289
- val.kind_of?(ScaledPoint) ? val : val.scaled_point
290
- end
291
- end
292
16
  end
293
17
 
294
- require "typographic-unit/version"
18
+ require "rational"
295
19
 
296
- TypographicUnit.open
20
+ require 'typographic-unit/version'
21
+ require 'typographic-unit/unit'
22
+ require 'typographic-unit/scaled-point'
23
+ require 'typographic-unit/tex-point'
24
+ require 'typographic-unit/pica'
25
+ require 'typographic-unit/inch'
26
+ require 'typographic-unit/big-point'
27
+ require 'typographic-unit/postscript-point'
28
+ require 'typographic-unit/millimeter'
29
+ require 'typographic-unit/centimeter'
30
+ require 'typographic-unit/meter'
31
+ require 'typographic-unit/didot-point'
32
+ require 'typographic-unit/cicero'
33
+ require 'typographic-unit/q'
34
+ require 'typographic-unit/american-point'
35
+ require 'typographic-unit/jis-point'
36
+ require 'typographic-unit/gou'
37
+
38
+ TypographicUnit.init