Skip to content

Commit

Permalink
Let PDF::Core handle decimal rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
practicingruby committed Oct 17, 2014
1 parent 6848965 commit 0249316
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
19 changes: 11 additions & 8 deletions lib/prawn/graphics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ module Graphics
# pdf.move_to(100,50)
#
def move_to(*point)
x,y = map_to_absolute(point)
renderer.add_content("%.3f %.3f m" % [ x, y ])
xy = PDF::Core.real_params(map_to_absolute(point))
renderer.add_content("#{xy} m")
end

# Draws a line from the current drawing position to the specified point.
Expand All @@ -57,8 +57,8 @@ def move_to(*point)
# pdf.line_to(50,50)
#
def line_to(*point)
x,y = map_to_absolute(point)
renderer.add_content("%.3f %.3f l" % [ x, y ])
xy = PDF::Core.real_params(map_to_absolute(point))
renderer.add_content("#{xy} l")
end

# Draws a Bezier curve from the current drawing position to the
Expand All @@ -71,9 +71,10 @@ def curve_to(dest,options={})
"Bounding points for bezier curve must be specified "+
"as :bounds => [[x1,y1],[x2,y2]]"

curve_points = (options[:bounds] << dest).map { |e| map_to_absolute(e) }
renderer.add_content("%.3f %.3f %.3f %.3f %.3f %.3f c" %
curve_points.flatten )
curve_points = PDF::Core.real_params(
(options[:bounds] << dest).flat_map { |e| map_to_absolute(e) })

renderer.add_content("#{curve_points} c")
end

# Draws a rectangle given <tt>point</tt>, <tt>width</tt> and
Expand All @@ -83,7 +84,9 @@ def curve_to(dest,options={})
#
def rectangle(point,width,height)
x,y = map_to_absolute(point)
renderer.add_content("%.3f %.3f %.3f %.3f re" % [ x, y - height, width, height ])
box = PDF::Core.real_params([x, y - height, width, height])

renderer.add_content("#{box} re")
end

# Draws a rounded rectangle given <tt>point</tt>, <tt>width</tt> and
Expand Down
5 changes: 2 additions & 3 deletions lib/prawn/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ def embed_image(pdf_obj, info, options)
label = "I#{next_image_id}"
state.page.xobjects.merge!(label => pdf_obj)

# add the image to the current page
instruct = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
renderer.add_content instruct % [ w, h, x, y - h, label ]
cm_params = PDF::Core.real_params([ w, 0, 0, h, x, y - h])
renderer.add_content("\nq\n#{cm_params} cm\n/#{label} Do\nQ")
end

private
Expand Down
2 changes: 1 addition & 1 deletion prawn.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
spec.licenses = ['RUBY', 'GPL-2', 'GPL-3']

spec.add_dependency('ttfunk', '~> 1.4.0')
spec.add_dependency('pdf-core', "~> 0.4.0")
spec.add_dependency('pdf-core', "~> 0.5.0")

spec.add_development_dependency('pdf-inspector', '~> 1.1.0')
spec.add_development_dependency('yard')
Expand Down
32 changes: 19 additions & 13 deletions spec/graphics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@
end

it "should draw a rectangle by connecting lines with rounded bezier curves" do
@all_coords.should == [[60.0, 550.0],[90.0, 550.0], [95.523, 550.0], [100.0, 545.523], [100.0, 540.0],
[100.0, 460.0], [100.0, 454.477], [95.523, 450.0], [90.0, 450.0],
[60.0, 450.0], [54.477, 450.0], [50.0, 454.477], [50.0, 460.0],
[50.0, 540.0], [50.0, 545.523], [54.477, 550.0], [60.0, 550.0]]
@all_coords.should == [[60.0, 550.0],[90.0, 550.0], [95.5228, 550.0],
[100.0, 545.5228], [100.0, 540.0], [100.0, 460.0],
[100.0, 454.4772], [95.5228, 450.0], [90.0, 450.0],
[60.0, 450.0], [54.4772, 450.0], [50.0, 454.4772],
[50.0, 460.0], [50.0, 540.0], [50.0, 545.5228],
[54.4772, 550.0], [60.0, 550.0]]
end

it "should start and end with the same point" do
Expand All @@ -167,20 +169,20 @@
@curve.coords.should ==
[125.0, 100.0,

125.0, 127.614,
113.807, 150,
125.0, 127.6142,
113.8071, 150,
100.0, 150.0,

86.193, 150.0,
75.0, 127.614,
86.1929, 150.0,
75.0, 127.6142,
75.0, 100.0,

75.0, 72.386,
86.193, 50.0,
75.0, 72.3858,
86.1929, 50.0,
100.0, 50.0,

113.807, 50.0,
125.0, 72.386,
113.8071, 50.0,
125.0, 72.3858,
125.0, 100.0,

100.0, 100.0]
Expand Down Expand Up @@ -427,14 +429,18 @@
@pdf.graphic_state.dash[:dash].should == 5
end

it "should round dash values to four decimal places" do
@pdf.dash 5.12345
@pdf.graphic_state.dash_setting.should == "[5.1235 5.1235] 0.0 d"
end

it "should raise an error when dash is called w. a zero length or space" do
expect { @pdf.dash(0) }.to raise_error(ArgumentError)
expect { @pdf.dash([0]) }.to raise_error(ArgumentError)
expect { @pdf.dash([0,0]) }.to raise_error(ArgumentError)
expect { @pdf.dash([0,0,0,1]) }.to raise_error(ArgumentError)
end


it "the current graphic state should keep track of previous unchanged settings" do
@pdf.stroke_color '000000'
@pdf.save_graphics_state
Expand Down
2 changes: 1 addition & 1 deletion spec/soft_mask_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def make_soft_mask
end

extgstate = PDF::Inspector::ExtGState.analyze(@pdf.render).extgstates.first
extgstate[:soft_mask][:G].data.should == "q\n/DeviceRGB cs\n0.000 0.000 0.000 scn\n/DeviceRGB CS\n0.000 0.000 0.000 SCN\n1 w\n0 J\n0 j\n[ ] 0 d\n/DeviceRGB cs\n0.502 0.502 0.502 scn\n100.000 -100.000 200.000 200.000 re\nf\nQ\n"
extgstate[:soft_mask][:G].data.should == "q\n/DeviceRGB cs\n0.000 0.000 0.000 scn\n/DeviceRGB CS\n0.000 0.000 0.000 SCN\n1 w\n0 J\n0 j\n[] 0 d\n/DeviceRGB cs\n0.502 0.502 0.502 scn\n100.0 -100.0 200.0 200.0 re\nf\nQ\n"
end

it "should not create duplicate extended graphics states" do
Expand Down
4 changes: 2 additions & 2 deletions spec/text_spacing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@pdf.text("hello world")
end
contents = PDF::Inspector::Text.analyze(@pdf.render)
contents.character_spacing.first.should == 10.556
contents.character_spacing.first.should == 10.5556
end
it "should not draw the character spacing to the document" +
" when the new character spacing matches the old" do
Expand Down Expand Up @@ -63,7 +63,7 @@
@pdf.text("hello world")
end
contents = PDF::Inspector::Text.analyze(@pdf.render)
contents.word_spacing.first.should == 10.556
contents.word_spacing.first.should == 10.5556
end
it "should draw the word spacing to the document" +
" when the new word spacing matches the old" do
Expand Down

0 comments on commit 0249316

Please sign in to comment.