From 4a0c5e78b6e693573e267bf1e639354528238c4e Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Mon, 13 Apr 2015 22:59:34 -0400 Subject: [PATCH] Fix PDF scaling issue Properly compute the size of the PDF based on DPI and PDF-specific resolution (i.e. 72dpi). Closes #62 --- lib/squib/graphics/save_doc.rb | 3 ++- samples/foo.rb | 8 ++++++++ spec/data/samples/custom_config.rb.txt | 1 + spec/data/samples/saves.rb.txt | 2 ++ spec/graphics/graphics_save_doc_spec.rb | 2 ++ 5 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 samples/foo.rb diff --git a/lib/squib/graphics/save_doc.rb b/lib/squib/graphics/save_doc.rb index 8f0f147..8cbddac 100644 --- a/lib/squib/graphics/save_doc.rb +++ b/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] diff --git a/samples/foo.rb b/samples/foo.rb new file mode 100644 index 0000000..30c3afc --- /dev/null +++ b/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 \ No newline at end of file diff --git a/spec/data/samples/custom_config.rb.txt b/spec/data/samples/custom_config.rb.txt index 4a2f45f..f3d6909 100644 --- a/spec/data/samples/custom_config.rb.txt +++ b/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([]) diff --git a/spec/data/samples/saves.rb.txt b/spec/data/samples/saves.rb.txt index 49b43ea..2f57334 100644 --- a/spec/data/samples/saves.rb.txt +++ b/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([]) diff --git a/spec/graphics/graphics_save_doc_spec.rb b/spec/graphics/graphics_save_doc_spec.rb index 5249d58..2bb79cf 100644 --- a/spec/graphics/graphics_save_doc_spec.rb +++ b/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)