pdf-core 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7f20557e9a614c10244d01e2b1ed78d4434cef6c
4
- data.tar.gz: 8668979b6592e45d81d8274e8acd387642e5a3fb
3
+ metadata.gz: 50239bf1d5c19e31512ccee0e9d69a73734d022e
4
+ data.tar.gz: a3ca5e4b7607e24dc7bb6d5a9b9a9e8e80a0ef8d
5
5
  SHA512:
6
- metadata.gz: 68d6e43d2579ff909d9cabc4a9d641648e9830685a2d2b76d24fe85046e48ea22fb7bbb4bf005d3442d4595b44b18dddd29dad37f99ecd32a3b9fa515490503e
7
- data.tar.gz: f6f49f285719b0d72d3113bc465db2e588d52c2b1c096c7285cb56f43744a31122d4a0c8338ab88353b29555bb352d4c2cc662eac2c73164948dda3ca5c72933
6
+ metadata.gz: 6098bf18e958d0d6aa0a496a0ddaadef7925e2b94016511f99d62d94b5b6ac395fcc82c1d924a9b8e14ebd56bff8cc5c91805732c5cb1a9b5b1620a674df9036
7
+ data.tar.gz: 8c77d27a7da4cf581d7311030f5dc28b41447137dcfff66e1d3989a05d1354f9b237fcc6e46cead04ad9476f6cb984c505cefbe2d896769dd7a39d59a9a00145
@@ -63,11 +63,17 @@ module PDF
63
63
  end
64
64
 
65
65
  def dash_setting
66
+ return "[] 0 d" unless @dash[:dash]
67
+
66
68
  if @dash[:dash].kind_of?(Array)
67
- "[#{@dash[:dash].join(' ')}] #{@dash[:phase]} d"
69
+ array = @dash[:dash]
68
70
  else
69
- "[#{@dash[:dash]} #{@dash[:space]}] #{@dash[:phase]} d"
71
+ array = [@dash[:dash], @dash[:space]]
70
72
  end
73
+
74
+
75
+ "[#{PDF::Core.real_params(array)}] "+
76
+ "#{PDF::Core.real(@dash[:phase])} d"
71
77
  end
72
78
 
73
79
  private
@@ -12,6 +12,14 @@ module PDF
12
12
  module Core
13
13
  module_function
14
14
 
15
+ def real(num)
16
+ num.to_f.round(4)
17
+ end
18
+
19
+ def real_params(array)
20
+ array.map { |e| real(e) }.join(" ")
21
+ end
22
+
15
23
  def utf8_to_utf16(str)
16
24
  "\xFE\xFF".force_encoding(::Encoding::UTF_16BE) + str.encode(::Encoding::UTF_16BE)
17
25
  end
@@ -43,12 +51,10 @@ module PDF
43
51
  when TrueClass then "true"
44
52
  when FalseClass then "false"
45
53
  when Numeric
46
- if (str = String(obj)) =~ /e/i
47
- # scientific notation is not supported in PDF
48
- sprintf("%.16f", obj).gsub(/\.?0+\z/, "")
49
- else
50
- str
51
- end
54
+ obj = real(obj) unless obj.kind_of?(Integer)
55
+
56
+ String(obj) # NOTE: this can fail on huge floating point numbers,
57
+ # but it seems unlikely to ever happen in practice.
52
58
  when Array
53
59
  "[" << obj.map { |e| PdfObject(e, in_content_stream) }.join(' ') << "]"
54
60
  when PDF::Core::LiteralString
@@ -47,8 +47,9 @@ module PDF
47
47
  #
48
48
  # # Raw line drawing example:
49
49
  # x1,y1,x2,y2 = 100,500,300,550
50
- # pdf.add_content("%.3f %.3f m" % [ x1, y1 ]) # move
51
- # pdf.add_content("%.3f %.3f l" % [ x2, y2 ]) # draw path
50
+ #
51
+ # pdf.add_content("#{PDF::Core.real_params([x1, y1])} m") # move
52
+ # pdf.add_content("#{PDF::Core.real_params([ x2, y2 ])} l") # draw path
52
53
  # pdf.add_content("S") # stroke
53
54
  #
54
55
  def add_content(str)
@@ -204,9 +204,9 @@ module PDF
204
204
  yield
205
205
  else
206
206
  @character_spacing = amount
207
- add_content "\n%.3f Tc" % amount
207
+ add_content "\n#{PDF::Core.real(amount)} Tc"
208
208
  yield
209
- add_content "\n%.3f Tc" % original_character_spacing
209
+ add_content "\n#{PDF::Core.real(original_character_spacing)} Tc"
210
210
  @character_spacing = original_character_spacing
211
211
  end
212
212
  end
@@ -222,9 +222,10 @@ module PDF
222
222
  yield
223
223
  else
224
224
  @word_spacing = amount
225
- add_content "\n%.3f Tw" % amount
225
+ add_content "\n#{PDF::Core.real(amount)} Tw"
226
226
  yield
227
- add_content "\n%.3f Tw" % original_word_spacing
227
+ add_content "\n#{PDF::Core.real(original_word_spacing)} Tw"
228
+
228
229
  @word_spacing = original_word_spacing
229
230
  end
230
231
  end
@@ -237,7 +238,7 @@ module PDF
237
238
  if options[:rotate]
238
239
  rad = options[:rotate].to_f * Math::PI / 180
239
240
  arr = [ Math.cos(rad), Math.sin(rad), -Math.sin(rad), Math.cos(rad), x, y ]
240
- add_content "%.3f %.3f %.3f %.3f %.3f %.3f Tm" % arr
241
+ add_content "#{PDF::Core.real_params(array)} Tm"
241
242
  else
242
243
  add_content "#{x} #{y} Td"
243
244
  end
@@ -0,0 +1,12 @@
1
+ # encoding: binary
2
+ require_relative "spec_helper"
3
+
4
+ describe "Decimal rounding" do
5
+ it "should round floating point numbers to four decimal places" do
6
+ PDF::Core.real(1.23456789).should == 1.2346
7
+ end
8
+
9
+ it "should be able to create a PDF parameter list of rounded decimals" do
10
+ PDF::Core.real_params([1,2.34567,Math::PI]).should == "1.0 2.3457 3.1416"
11
+ end
12
+ end
@@ -14,10 +14,10 @@ describe "PDF Object Serialization" do
14
14
  end
15
15
 
16
16
  it "should convert a Ruby number to PDF number" do
17
- PDF::Core::PdfObject(1).should == "1"
18
- PDF::Core::PdfObject(1.214112421).should == "1.214112421"
19
- # scientific notation is not valid in PDF
20
- PDF::Core::PdfObject(0.000005).should == "0.000005"
17
+ PDF::Core::PdfObject(42).should == "42"
18
+
19
+ # numbers are rounded to four decimal places
20
+ PDF::Core::PdfObject(1.214112421).should == "1.2141"
21
21
  end
22
22
 
23
23
  it "should convert a Ruby time object to a PDF timestamp" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregory Brown
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2014-09-16 00:00:00.000000000 Z
15
+ date: 2014-10-17 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: pdf-reader
@@ -122,6 +122,7 @@ files:
122
122
  - lib/pdf/core/stream.rb
123
123
  - lib/pdf/core/text.rb
124
124
  - pdf-core.gemspec
125
+ - spec/decimal_rounding_spec.rb
125
126
  - spec/filters_spec.rb
126
127
  - spec/name_tree_spec.rb
127
128
  - spec/object_store_spec.rb