pdf-wrapper 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ require 'spec/rake/spectask'
8
8
  require 'roodi'
9
9
  require 'roodi_task'
10
10
 
11
- PKG_VERSION = "0.3.0"
11
+ PKG_VERSION = "0.3.1"
12
12
  PKG_NAME = "pdf-wrapper"
13
13
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
14
14
 
data/lib/pdf/core.rb CHANGED
@@ -1,19 +1,41 @@
1
1
  # coding: utf-8
2
2
 
3
- class Hash
4
- # raise an error if this hash has any keys that aren't in the supplied list
5
- # - borrowed from activesupport
6
- def assert_valid_keys(*valid_keys)
7
- unknown_keys = keys - [valid_keys].flatten
8
- raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
3
+
4
+ unless {}.respond_to?(:assert_valid_keys)
5
+ class Hash
6
+ # raise an error if this hash has any keys that aren't in the supplied list
7
+ # - borrowed from activesupport
8
+ def assert_valid_keys(*valid_keys)
9
+ unknown_keys = keys - [valid_keys].flatten
10
+ raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
11
+ end
12
+ end
13
+ end
14
+
15
+ unless {}.respond_to?(:except)
16
+
17
+ class Hash
18
+ def except(*keys)
19
+ keys.flatten!
20
+ self.dup.reject { |k,v|
21
+ keys.include? k.to_sym
22
+ }
23
+ end
9
24
  end
10
25
 
11
- def only(*keys)
12
- keys.flatten!
13
- self.dup.reject { |k,v|
14
- !keys.include? k.to_sym
15
- }
26
+ end
27
+
28
+ unless {}.respond_to?(:only)
29
+
30
+ class Hash
31
+ def only(*keys)
32
+ keys.flatten!
33
+ self.dup.reject { |k,v|
34
+ !keys.include? k.to_sym
35
+ }
36
+ end
16
37
  end
38
+
17
39
  end
18
40
 
19
41
  unless [].respond_to?(:sum)
data/lib/pdf/wrapper.rb CHANGED
@@ -103,7 +103,8 @@ module PDF
103
103
  # <tt>output</tt>:: Where to render the PDF to. Can be a string containing a filename,
104
104
  # or an IO object (File, StringIO, etc)
105
105
  # Options:
106
- # <tt>:paper</tt>:: The paper size to use (default :A4)
106
+ # <tt>:paper</tt>:: The paper size to use (default :A4). Can be a predefined paper size,
107
+ # or an array of [width, height]
107
108
  # <tt>:orientation</tt>:: :portrait (default) or :landscape
108
109
  # <tt>:background_color</tt>:: The background colour to use (default :white)
109
110
  # <tt>:margin_top</tt>:: The size of the default top margin (default 5% of page)
@@ -154,7 +155,7 @@ module PDF
154
155
  options.merge!(opts)
155
156
 
156
157
  # test for invalid options
157
- options.assert_valid_keys(:paper, :orientation, :background_color, :margin_left, :margin_right,
158
+ options.assert_valid_keys(:paper, :orientation, :background_color, :margin_left, :margin_right,
158
159
  :margin_top, :margin_bottom, :io, :template)
159
160
 
160
161
  set_dimensions(options[:orientation], options[:paper])
@@ -385,7 +386,7 @@ module PDF
385
386
  when StringIO then
386
387
  File.open(filename, "w") do |of|
387
388
  of.write(@output.string)
388
- end
389
+ end
389
390
  when File then return FileUtils.cp(@output.path, filename)
390
391
  else
391
392
  return FileUtils.cp(@output, filename)
@@ -607,6 +608,25 @@ module PDF
607
608
  private
608
609
 
609
610
  def set_dimensions(orientation, paper)
611
+ return if orientation.nil? || paper.nil?
612
+
613
+ if paper.is_a?(Array)
614
+ set_manual_dimensions(*paper)
615
+ else
616
+ set_predefined_dimensions(orientation, paper)
617
+ end
618
+ end
619
+
620
+ def set_manual_dimensions(*args)
621
+ @page_width, @page_height = *args
622
+ if @page_width > @page_height
623
+ @orientation = :landscape
624
+ else
625
+ @orientation = :portrait
626
+ end
627
+ end
628
+
629
+ def set_predefined_dimensions(orientation, paper)
610
630
  # use the defaults if none were provided
611
631
  orientation ||= @orientation
612
632
  paper ||= @paper
@@ -28,8 +28,8 @@ module PDF
28
28
  else
29
29
  str = self.data.dup
30
30
  end
31
- @min_width = wrapper.text_width(str.gsub(/\b|\B/,"\n"), text_options) + (padding * 4)
32
- @natural_width = wrapper.text_width(str, text_options) + (padding * 4)
31
+ @min_width = wrapper.text_width(str.gsub(/\b|\B/,"\n"), text_options_without_markup) + (padding * 4)
32
+ @natural_width = wrapper.text_width(str, text_options_without_markup) + (padding * 4)
33
33
  end
34
34
 
35
35
  def calculate_height(wrapper)
@@ -48,6 +48,10 @@ module PDF
48
48
  def text_options
49
49
  self.options.only(wrapper.default_text_options.keys)
50
50
  end
51
+
52
+ def text_options_without_markup
53
+ self.options.only(wrapper.default_text_options.keys).except(:markup)
54
+ end
51
55
  end
52
56
  end
53
57
  end
data/specs/tables_spec.rb CHANGED
@@ -144,6 +144,22 @@ context PDF::Wrapper do
144
144
  x.to_i.should eql(100)
145
145
  end
146
146
 
147
+ specify "should be able to draw a table with escaped content markup on the canvas" do
148
+ table = PDF::Wrapper::Table.new(:markup => :pango) do |t|
149
+ t.data = [%w{data1 data2}, %w{data3 data4&amp;5}]
150
+ end
151
+ @pdf.table(table)
152
+ @pdf.finish
153
+
154
+ receiver = PageTextReceiver.new
155
+ reader = PDF::Reader.string(@output.string, receiver)
156
+
157
+ receiver.content.first.include?("data1").should be_true
158
+ receiver.content.first.include?("data2").should be_true
159
+ receiver.content.first.include?("data3").should be_true
160
+ receiver.content.first.include?("data4&5").should be_true
161
+ end
162
+
147
163
  end
148
164
 
149
165
  context PDF::Wrapper, "data= method" do
data/specs/text_spec.rb CHANGED
@@ -168,7 +168,7 @@ context "The PDF::Wrapper class" do
168
168
  opts = {:font_size => 16, :font => "Sans Serif"}
169
169
  @pdf.text(str, opts)
170
170
  @pdf.text_width(str, opts).should eql(131)
171
- @pdf.text_width(str2, opts).should eql(1107)
171
+ @pdf.text_width(str2, opts).should eql(1106)
172
172
  end
173
173
 
174
174
  specify "should raise an exception if build_pango_layout is passed anything other than a string" do
@@ -18,6 +18,13 @@ context "The PDF::Wrapper class" do
18
18
  pdf.page_height.should eql(PDF::Wrapper::PAGE_SIZES[:A0].last)
19
19
  end
20
20
 
21
+ specify "should initilize with the correct manual paper size" do
22
+ output = StringIO.new
23
+ pdf = PDF::Wrapper.new(output, :paper => [100, 1000])
24
+ pdf.page_width.to_i.should eql(100)
25
+ pdf.page_height.to_i.should eql(1000)
26
+ end
27
+
21
28
  specify "should initilize with the correct custom orientation" do
22
29
  output = StringIO.new
23
30
  pdf = PDF::Wrapper.new(output, :paper => :A4, :orientation => :landscape)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf-wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Healy
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-15 00:00:00 +11:00
12
+ date: 2009-11-24 00:00:00 +11:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -33,49 +33,49 @@ extra_rdoc_files:
33
33
  - CHANGELOG
34
34
  - TODO
35
35
  files:
36
- - examples/cell.rb
37
- - examples/image.rb
38
- - examples/markup.rb
39
- - examples/repeating.rb
40
36
  - examples/utf8-long.rb
41
- - examples/shapes.rb
42
- - examples/table.rb
43
37
  - examples/scaled_image.rb
38
+ - examples/table_simple.rb
39
+ - examples/table.rb
40
+ - examples/image.rb
41
+ - examples/varied_page_size.rb
44
42
  - examples/translate.rb
45
43
  - examples/utf8.rb
46
- - examples/varied_page_size.rb
47
- - examples/table_fixed_col_width.rb
48
44
  - examples/table_images.rb
49
- - examples/table_simple.rb
50
- - lib/pdf/core.rb
51
- - lib/pdf/wrapper.rb
45
+ - examples/markup.rb
46
+ - examples/cell.rb
47
+ - examples/table_fixed_col_width.rb
48
+ - examples/repeating.rb
49
+ - examples/shapes.rb
52
50
  - lib/pdf/errors.rb
53
51
  - lib/pdf/wrapper/graphics.rb
54
- - lib/pdf/wrapper/images.rb
55
- - lib/pdf/wrapper/loading.rb
56
- - lib/pdf/wrapper/page.rb
57
52
  - lib/pdf/wrapper/table.rb
53
+ - lib/pdf/wrapper/loading.rb
54
+ - lib/pdf/wrapper/text_image_cell.rb
55
+ - lib/pdf/wrapper/images.rb
58
56
  - lib/pdf/wrapper/text.rb
57
+ - lib/pdf/wrapper/page.rb
59
58
  - lib/pdf/wrapper/text_cell.rb
60
- - lib/pdf/wrapper/text_image_cell.rb
59
+ - lib/pdf/core.rb
60
+ - lib/pdf/wrapper.rb
61
+ - specs/wrapper_spec.rb
62
+ - specs/image_spec.rb
63
+ - specs/graphics_spec.rb
61
64
  - specs/data/google.png
62
- - specs/data/iso-2022-jp.txt
63
- - specs/data/orc.svg
65
+ - specs/data/zits.gif
66
+ - specs/data/windmill.jpg
67
+ - specs/data/utf8-long.txt
64
68
  - specs/data/shift_jis.txt
69
+ - specs/data/orc.svg
65
70
  - specs/data/shipsail.jpg
66
71
  - specs/data/stef.jpg
67
- - specs/data/utf8-long.pdf
68
- - specs/data/utf8-long.txt
72
+ - specs/data/iso-2022-jp.txt
69
73
  - specs/data/utf8.txt
70
- - specs/data/windmill.jpg
71
- - specs/data/zits.gif
74
+ - specs/data/utf8-long.pdf
72
75
  - specs/spec_helper.rb
73
- - specs/image_spec.rb
74
- - specs/load_spec.rb
75
76
  - specs/tables_spec.rb
77
+ - specs/load_spec.rb
76
78
  - specs/text_spec.rb
77
- - specs/wrapper_spec.rb
78
- - specs/graphics_spec.rb
79
79
  - Rakefile
80
80
  - README.rdoc
81
81
  - CHANGELOG