Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round to four decimal places anywhere real numbers are used #10

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/lines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
pdf = PDF::Core::Renderer.new(PDF::Core::DocumentState.new({}))

pdf.start_new_page
pdf.add_content("%.3f %.3f m" % [100, 500])
pdf.add_content("%.3f %.3f l" % [300, 550])
pdf.add_content("#{PDF::Core.real_params([100,500])} m")
pdf.add_content("#{PDF::Core.real_params([300,550])} l")
pdf.add_content("S")

pdf.render_file("x.pdf")
10 changes: 8 additions & 2 deletions lib/pdf/core/graphics_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,17 @@ def initialize(previous_state = nil)
end

def dash_setting
return "[] 0 d" unless @dash[:dash]

if @dash[:dash].kind_of?(Array)
"[#{@dash[:dash].join(' ')}] #{@dash[:phase]} d"
array = @dash[:dash]
else
"[#{@dash[:dash]} #{@dash[:space]}] #{@dash[:phase]} d"
array = [@dash[:dash], @dash[:space]]
end


"[#{PDF::Core.real_params(array)}] "+
"#{PDF::Core.real(@dash[:phase])} d"
end

private
Expand Down
18 changes: 12 additions & 6 deletions lib/pdf/core/pdf_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ module PDF
module Core
module_function

def real(num)
num.to_f.round(4)
end

def real_params(array)
array.map { |e| real(e) }.join(" ")
end

def utf8_to_utf16(str)
"\xFE\xFF".force_encoding(::Encoding::UTF_16BE) + str.encode(::Encoding::UTF_16BE)
end
Expand Down Expand Up @@ -43,12 +51,10 @@ def PdfObject(obj, in_content_stream = false)
when TrueClass then "true"
when FalseClass then "false"
when Numeric
if (str = String(obj)) =~ /e/i
# scientific notation is not supported in PDF
sprintf("%.16f", obj).gsub(/\.?0+\z/, "")
else
str
end
obj = real(obj) unless obj.kind_of?(Integer)

String(obj) # NOTE: this can fail on huge floating point numbers,
# but it seems unlikely to ever happen in practice.
when Array
"[" << obj.map { |e| PdfObject(e, in_content_stream) }.join(' ') << "]"
when PDF::Core::LiteralString
Expand Down
5 changes: 3 additions & 2 deletions lib/pdf/core/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ def deref(obj)
#
# # Raw line drawing example:
# x1,y1,x2,y2 = 100,500,300,550
# pdf.add_content("%.3f %.3f m" % [ x1, y1 ]) # move
# pdf.add_content("%.3f %.3f l" % [ x2, y2 ]) # draw path
#
# pdf.add_content("#{PDF::Core.real_params([x1, y1])} m") # move
# pdf.add_content("#{PDF::Core.real_params([ x2, y2 ])} l") # draw path
# pdf.add_content("S") # stroke
#
def add_content(str)
Expand Down
11 changes: 6 additions & 5 deletions lib/pdf/core/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ def character_spacing(amount=nil)
yield
else
@character_spacing = amount
add_content "\n%.3f Tc" % amount
add_content "\n#{PDF::Core.real(amount)} Tc"
yield
add_content "\n%.3f Tc" % original_character_spacing
add_content "\n#{PDF::Core.real(original_character_spacing)} Tc"
@character_spacing = original_character_spacing
end
end
Expand All @@ -222,9 +222,10 @@ def word_spacing(amount=nil)
yield
else
@word_spacing = amount
add_content "\n%.3f Tw" % amount
add_content "\n#{PDF::Core.real(amount)} Tw"
yield
add_content "\n%.3f Tw" % original_word_spacing
add_content "\n#{PDF::Core.real(original_word_spacing)} Tw"

@word_spacing = original_word_spacing
end
end
Expand All @@ -237,7 +238,7 @@ def add_text_content(text, x, y, options)
if options[:rotate]
rad = options[:rotate].to_f * Math::PI / 180
arr = [ Math.cos(rad), Math.sin(rad), -Math.sin(rad), Math.cos(rad), x, y ]
add_content "%.3f %.3f %.3f %.3f %.3f %.3f Tm" % arr
add_content "#{PDF::Core.real_params(array)} Tm"
else
add_content "#{x} #{y} Td"
end
Expand Down
12 changes: 12 additions & 0 deletions spec/decimal_rounding_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# encoding: binary
require_relative "spec_helper"

describe "Decimal rounding" do
it "should round floating point numbers to four decimal places" do
PDF::Core.real(1.23456789).should == 1.2346
end

it "should be able to create a PDF parameter list of rounded decimals" do
PDF::Core.real_params([1,2.34567,Math::PI]).should == "1.0 2.3457 3.1416"
end
end
8 changes: 4 additions & 4 deletions spec/pdf_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
end

it "should convert a Ruby number to PDF number" do
PDF::Core::PdfObject(1).should == "1"
PDF::Core::PdfObject(1.214112421).should == "1.214112421"
# scientific notation is not valid in PDF
PDF::Core::PdfObject(0.000005).should == "0.000005"
PDF::Core::PdfObject(42).should == "42"

# numbers are rounded to four decimal places
PDF::Core::PdfObject(1.214112421).should == "1.2141"
end

it "should convert a Ruby time object to a PDF timestamp" do
Expand Down