Browse Source

Fix PDF scaling issue

Properly compute the size of the PDF based on DPI and PDF-specific resolution (i.e. 72dpi).

Closes #62
dev
Andy Meneely 11 years ago
parent
commit
4a0c5e78b6
  1. 3
      lib/squib/graphics/save_doc.rb
  2. 8
      samples/foo.rb
  3. 1
      spec/data/samples/custom_config.rb.txt
  4. 2
      spec/data/samples/saves.rb.txt
  5. 2
      spec/graphics/graphics_save_doc_spec.rb

3
lib/squib/graphics/save_doc.rb

@ -22,7 +22,8 @@ module Squib
paper_width = p[:width]
paper_height = p[:height]
file = "#{p[:dir]}/#{p[:file]}"
cc = Cairo::Context.new(Cairo::PDFSurface.new(file, paper_width, paper_height))
cc = Cairo::Context.new(Cairo::PDFSurface.new(file, paper_width * 72.0 / @dpi, paper_height * 72.0 / @dpi))
cc.scale(72.0 / @dpi, 72.0 / @dpi) # for bug #62
x, y = p[:margin], p[:margin]
card_width = @width - 2 * p[:trim]
card_height = @height - 2 * p[:trim]

8
samples/foo.rb

@ -0,0 +1,8 @@
require 'squib'
puts "Using Squib #{Squib::VERSION}"
Squib::Deck.new(cards: 3, dpi: 600) do
background color: :gray
text str: %w(Hello fine world!)
save_pdf file: 'foo.pdf', gap: 5, width: '11in', height: '8.5in'
end

1
spec/data/samples/custom_config.rb.txt

@ -35,6 +35,7 @@ cairo: scale([1.0, 1.0])
cairo: render_rsvg_handle([RSVG::Handle, nil])
cairo: restore([])
surface: write_to_png(["_output/custom-config_00.png"])
cairo: scale([0.24, 0.24])
cairo: translate([75, 75])
cairo: rectangle([0, 0, 825, 1125])
cairo: clip([])

2
spec/data/samples/saves.rb.txt

@ -494,6 +494,7 @@ cairo: move_to([0, 0])
cairo: update_pango_layout([MockDouble])
cairo: show_pango_layout([MockDouble])
cairo: restore([])
cairo: scale([0.24, 0.24])
cairo: translate([75, 75])
cairo: rectangle([37, 37, 751, 1051])
cairo: clip([])
@ -608,6 +609,7 @@ cairo: paint([])
cairo: reset_clip([])
cairo: translate([-2343, -1131])
cairo: show_page([])
cairo: scale([0.24, 0.24])
cairo: translate([75, 75])
cairo: rectangle([0, 0, 825, 1125])
cairo: clip([])

2
spec/graphics/graphics_save_doc_spec.rb

@ -28,6 +28,7 @@ describe Squib::Deck, '#save_pdf' do
deck = Squib::Deck.new(cards: 9, width: 825, height: 1125)
expect(deck).to receive(:dirify) { |arg| arg } #don't create the dir
expect(Squib.logger).to receive(:debug).at_least(:once)
expect(cxt).to receive(:scale).with(0.24, 0.24)
expect_card_place(75, 75)
expect_card_place(831, 75)
@ -50,6 +51,7 @@ describe Squib::Deck, '#save_pdf' do
deck = Squib::Deck.new(cards: num_cards, width: 825, height: 1125)
expect(Squib.logger).to receive(:debug).at_least(:once)
expect(deck).to receive(:dirify) { |arg| arg } #don't create the dir
expect(cxt).to receive(:scale).with(0.24, 0.24)
expect_card_place(75, 75)
expect_card_place(831, 75)

Loading…
Cancel
Save