pdf_margins 0.2.3 → 0.3.1

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: 627c8a5b2633419fa30dc4d9633ca4b5825f822b
4
- data.tar.gz: 0489558643554e50cdbeaf5c18c78c0a62013e8e
3
+ metadata.gz: 1f4e0cb5d988f4165e55240cce916d486f9316c3
4
+ data.tar.gz: 25460409951b86d6f51f5b91d61fd88edd3e1b82
5
5
  SHA512:
6
- metadata.gz: 950b461d6e613d583da0e7ae629bc6c04d792fa440ef77e0469e50c2925a2ed061c1927e3852e995f07514f6166582dc443155ccdbcb2624005e45ccbc241141
7
- data.tar.gz: 646cb27222b39c19529abdf79915afff2a8f875c73fbba2efcfac0b242e0889279813ec99c2152554f2954ea792f000a61ea5bc47ec94a63443600969bd2258a
6
+ metadata.gz: d286a7d196b91486767fa831413a0704e65d40043e606c57f9609e1fe1b69817e5a6812cb4b8dd073ad4090517d5b359511972b5393762faafb0eaa48db8cb6d
7
+ data.tar.gz: b7ea7a4386a7ae7a58b1ee8651ebe80bde3e0e708d6d6308539383b58351e815b4ba287a10bad6d269b5e4d9b202958311491370fca4371fe4d745c9b5a5df21
@@ -3,6 +3,7 @@ require 'pdf/margins/errors'
3
3
  require 'chunky_png'
4
4
  require 'oily_png'
5
5
  require 'tmpdir'
6
+ require 'logger'
6
7
 
7
8
  module PDF
8
9
  module Margins
@@ -15,21 +16,29 @@ module PDF
15
16
  SCALE_MULTIPLIER = 1
16
17
  RESOLUTION = DEFAULT_RESOLUTION * SCALE_MULTIPLIER
17
18
 
18
- attr_reader :file_path, :top_margin, :right_margin, :bottom_margin, :left_margin, :spreads
19
+ attr_reader :file_path, :logger
20
+ attr_reader :top_margin, :right_margin, :bottom_margin, :left_margin, :spreads
19
21
 
20
22
  # Dimensions are in mm, to be converted to PDF points later. Pass spreads
21
23
  # as true to check left and right margins of spreads, not pages.
22
- def initialize(file_path, top_margin, right_margin, bottom_margin, left_margin, spreads=false)
24
+ def initialize(file_path, options = {})
23
25
  @file_path = file_path
24
- @top_margin = top_margin
25
- @right_margin = right_margin
26
- @bottom_margin = bottom_margin
27
- @left_margin = left_margin
28
- @spreads = spreads
26
+ @keep_pngs = options.delete(:keep_pngs) || false
27
+
28
+ @top_margin = options.delete(:top_margin) || 0
29
+ @right_margin = options.delete(:right_margin) || 0
30
+ @bottom_margin = options.delete(:bottom_margin) || 0
31
+ @left_margin = options.delete(:left_margin) || 0
32
+ @spreads = options.delete(:spreads) || false
33
+
34
+ @logger = options.delete(:logger) || Logger.new(STDOUT).tap do |l|
35
+ l.level = options.delete(:log_level) || Logger::WARN
36
+ end
29
37
  end
30
38
 
31
39
  def issues
32
40
  temp_dir_path = Dir.mktmpdir("pdf_margins")
41
+ logger.debug("Rendering PNGs into #{temp_dir_path}")
33
42
 
34
43
  begin
35
44
  # This produces greyscale PNGs - we throw the colour away because we
@@ -39,12 +48,17 @@ module PDF
39
48
  issues = []
40
49
 
41
50
  files = Dir.glob("#{temp_dir_path}/*.png")
51
+ file_count = files.length
42
52
  # ensure the files are sorted naturally
43
53
  files = files.sort_by{ |f| f.split('/').last.to_i }
44
54
 
55
+ logger.debug("Rendered #{file_count} files")
56
+
45
57
  files.each_with_index do |png_path, index|
46
- image = ChunkyPNG::Image.from_file(png_path)
47
58
  page_number = index + 1
59
+ logger.debug("Rendering page #{page_number} from #{png_path}")
60
+
61
+ image = ChunkyPNG::Image.from_file(png_path)
48
62
 
49
63
  if dirty_top_margin?(image, top_margin)
50
64
  issues << Issue.new(page_number, :top)
@@ -64,7 +78,8 @@ module PDF
64
78
  end
65
79
 
66
80
  ensure
67
- FileUtils.remove_entry(temp_dir_path)
81
+ FileUtils.remove_entry(temp_dir_path) unless @keep_pngs
82
+ logger.info("PNG files kept at: #{temp_dir_path}") if @keep_pngs
68
83
  end
69
84
 
70
85
  return issues
@@ -108,8 +123,11 @@ module PDF
108
123
  height.times do |y_offset|
109
124
  x_position = x + x_offset
110
125
  y_position = y + y_offset
126
+ pixel = image[x_position, y_position]
111
127
 
112
- if image[x_position, y_position] != white
128
+ if pixel != white
129
+ hex = ChunkyPNG::Color.to_hex(pixel)
130
+ logger.debug("Found #{hex} pixel at #{x_position},#{y_position}")
113
131
  found_dirty_pixel = true
114
132
  break
115
133
  end
@@ -1,5 +1,5 @@
1
1
  module PDF
2
2
  module Margins
3
- VERSION = "0.2.3"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
@@ -2,18 +2,27 @@ require 'test_helper'
2
2
 
3
3
  class CheckerTest < Test::Unit::TestCase
4
4
 
5
+ def default_options(margin_width = 15)
6
+ {
7
+ top_margin: margin_width,
8
+ right_margin: margin_width,
9
+ bottom_margin: margin_width,
10
+ left_margin: margin_width
11
+ }
12
+ end
13
+
5
14
  test "PDF as pages with clear margins tested as pages" do
6
- checker = PDF::Margins::Checker.new(pdf_path('clear-margins-pages.pdf'), 15, 15, 15, 15)
15
+ checker = PDF::Margins::Checker.new(pdf_path('clear-margins-pages.pdf'), default_options)
7
16
  assert_equal [], checker.issues
8
17
  end
9
18
 
10
19
  test "PDF as spreads with clear margins tested as spreads" do
11
- checker = PDF::Margins::Checker.new(pdf_path('clear-margins-spreads.pdf'), 15, 15, 15, 15, true)
20
+ checker = PDF::Margins::Checker.new(pdf_path('clear-margins-spreads.pdf'), default_options.merge(spreads: true))
12
21
  assert_equal [], checker.issues
13
22
  end
14
23
 
15
24
  test "PDF as spreads with clear margins tested as pages" do
16
- checker = PDF::Margins::Checker.new(pdf_path('clear-margins-spreads.pdf'), 15, 15, 15, 15)
25
+ checker = PDF::Margins::Checker.new(pdf_path('clear-margins-spreads.pdf'), default_options)
17
26
  assert_equal [
18
27
  PDF::Margins::Issue.new(1, :left),
19
28
  PDF::Margins::Issue.new(2, :right),
@@ -23,21 +32,21 @@ class CheckerTest < Test::Unit::TestCase
23
32
  end
24
33
 
25
34
  test "PDF as pages with dirty left margin" do
26
- checker = PDF::Margins::Checker.new(pdf_path('left-margin-only.pdf'), 15, 15, 15, 15)
35
+ checker = PDF::Margins::Checker.new(pdf_path('left-margin-only.pdf'), default_options)
27
36
  assert_equal [
28
37
  PDF::Margins::Issue.new(1, :left)
29
38
  ], checker.issues
30
39
  end
31
40
 
32
41
  test "PDF as pages with dirty right margin" do
33
- checker = PDF::Margins::Checker.new(pdf_path('right-margin-only.pdf'), 15, 15, 15, 15)
42
+ checker = PDF::Margins::Checker.new(pdf_path('right-margin-only.pdf'), default_options)
34
43
  assert_equal [
35
44
  PDF::Margins::Issue.new(1, :right)
36
45
  ], checker.issues
37
46
  end
38
47
 
39
48
  test "PDF with dirty top margin as spreads" do
40
- checker = PDF::Margins::Checker.new(pdf_path('top-margin-only.pdf'), 15, 15, 15, 15, true)
49
+ checker = PDF::Margins::Checker.new(pdf_path('top-margin-only.pdf'), default_options.merge(spreads: true))
41
50
  assert_equal [
42
51
  PDF::Margins::Issue.new(1, :top),
43
52
  PDF::Margins::Issue.new(2, :top),
@@ -47,7 +56,7 @@ class CheckerTest < Test::Unit::TestCase
47
56
  end
48
57
 
49
58
  test "PDF with dirty bottom margin as spreads" do
50
- checker = PDF::Margins::Checker.new(pdf_path('bottom-margin-only.pdf'), 15, 15, 15, 15, true)
59
+ checker = PDF::Margins::Checker.new(pdf_path('bottom-margin-only.pdf'), default_options.merge(spreads: true))
51
60
  assert_equal [
52
61
  PDF::Margins::Issue.new(1, :bottom),
53
62
  PDF::Margins::Issue.new(2, :bottom),
@@ -57,7 +66,7 @@ class CheckerTest < Test::Unit::TestCase
57
66
  end
58
67
 
59
68
  test "PDF with dirty top margin as pages" do
60
- checker = PDF::Margins::Checker.new(pdf_path('top-margin-only.pdf'), 15, 15, 15, 15)
69
+ checker = PDF::Margins::Checker.new(pdf_path('top-margin-only.pdf'), default_options)
61
70
  assert_equal [
62
71
  PDF::Margins::Issue.new(1, :top),
63
72
  PDF::Margins::Issue.new(2, :top),
@@ -67,7 +76,7 @@ class CheckerTest < Test::Unit::TestCase
67
76
  end
68
77
 
69
78
  test "PDF with dirty bottom margin as pages" do
70
- checker = PDF::Margins::Checker.new(pdf_path('bottom-margin-only.pdf'), 15, 15, 15, 15)
79
+ checker = PDF::Margins::Checker.new(pdf_path('bottom-margin-only.pdf'), default_options)
71
80
  assert_equal [
72
81
  PDF::Margins::Issue.new(1, :bottom),
73
82
  PDF::Margins::Issue.new(2, :bottom),
@@ -77,17 +86,17 @@ class CheckerTest < Test::Unit::TestCase
77
86
  end
78
87
 
79
88
  test "PDF with all dirty margins being checked as pages with a margin width of 0" do
80
- checker = PDF::Margins::Checker.new(pdf_path('16-page-mini.pdf'), 0, 0, 0, 0, false)
89
+ checker = PDF::Margins::Checker.new(pdf_path('16-page-mini.pdf'), default_options(0))
81
90
  assert_equal [], checker.issues
82
91
  end
83
92
 
84
93
  test "PDF with all dirty margins being checked as spreads with a margin width of 0" do
85
- checker = PDF::Margins::Checker.new(pdf_path('16-page-mini.pdf'), 0, 0, 0, 0, true)
94
+ checker = PDF::Margins::Checker.new(pdf_path('16-page-mini.pdf'), default_options(0).merge(spreads: true))
86
95
  assert_equal [], checker.issues
87
96
  end
88
97
 
89
98
  test "shelling out to mudraw fails" do
90
- checker = PDF::Margins::Checker.new(pdf_path('bottom-margin-only.pdf'), 15, 15, 15, 15)
99
+ checker = PDF::Margins::Checker.new(pdf_path('bottom-margin-only.pdf'), default_options)
91
100
  checker.expects(:system).returns(false)
92
101
  assert_raises(PDF::Margins::MuDrawCommandError) { checker.issues }
93
102
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf_margins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Taylor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-16 00:00:00.000000000 Z
11
+ date: 2013-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png