pdf-wrapper 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +13 -2
- data/Rakefile +6 -2
- data/lib/pdf/wrapper/images.rb +2 -2
- data/lib/pdf/wrapper/text.rb +27 -1
- data/lib/pdf/wrapper.rb +9 -11
- data/specs/text_spec.rb +9 -2
- metadata +28 -11
data/CHANGELOG
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
v0.3.2 (6th September 2010)
|
2
|
+
- fix importing of PDF files as images
|
3
|
+
- always imports the first page, currently no support for importing the
|
4
|
+
second or subsequent pages
|
5
|
+
- PDF::Wrapper#cell() - allow font size to be specified as a range. The
|
6
|
+
largest size that fits all the cell text will be used
|
7
|
+
|
8
|
+
v0.3.1 (24th November 2009)
|
9
|
+
- avoid errors being printed to stderr when rendering tables
|
10
|
+
- allow manual page sizes to be specified
|
11
|
+
|
1
12
|
v0.3.0 (15th October 2009)
|
2
13
|
- remove some deprecated functions from Table
|
3
14
|
- added support for table cells with text and images
|
@@ -50,11 +61,11 @@ v0.1.1 (Unreleased)
|
|
50
61
|
|
51
62
|
v0.1.0 (28th May 2008)
|
52
63
|
- added PDF::Wrapper#translate and PDF::Wrapper#scale
|
53
|
-
- fixed a bug that caused some text to be rendered off the page when wrapping onto 3rd and
|
64
|
+
- fixed a bug that caused some text to be rendered off the page when wrapping onto 3rd and
|
54
65
|
subsequent pages
|
55
66
|
- Improved table support using a new class PDF::Wrapper::Table
|
56
67
|
- Added a :center option to Wrapper#image
|
57
|
-
- Added a :markup option to text related functions. Pango markup can be used to tweak
|
68
|
+
- Added a :markup option to text related functions. Pango markup can be used to tweak
|
58
69
|
text style (bold, italics, super script, etc)
|
59
70
|
- Replaced PDF::Wrapper#rounded_rectangle with the :radius option to PDF::Wrapper#rectangle
|
60
71
|
- Allow cells to have a border with rounded corners
|
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.
|
11
|
+
PKG_VERSION = "0.3.2"
|
12
12
|
PKG_NAME = "pdf-wrapper"
|
13
13
|
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
14
14
|
|
@@ -72,12 +72,16 @@ spec = Gem::Specification.new do |spec|
|
|
72
72
|
spec.has_rdoc = true
|
73
73
|
spec.extra_rdoc_files = %w{README.rdoc CHANGELOG TODO}
|
74
74
|
spec.rdoc_options << '--title' << 'PDF::Wrapper Documentation' << '--main' << 'README.rdoc' << '-q'
|
75
|
-
spec.add_dependency 'cairo', '>=1.5.0'
|
76
75
|
spec.author = "James Healy"
|
77
76
|
spec.homepage = "http://pdf-wrapper.rubyforge.org/"
|
78
77
|
spec.email = "jimmy@deefa.com"
|
79
78
|
spec.rubyforge_project = "pdf-wrapper"
|
80
79
|
spec.description = "A unicode aware PDF writing library that uses the ruby bindings to various c libraries ( like cairo, pango, poppler and rsvg ) to do the heavy lifting."
|
80
|
+
spec.add_dependency("cairo", "~>1.8")
|
81
|
+
#spec.add_dependency("pango")
|
82
|
+
#spec.add_dependency("poppler")
|
83
|
+
#spec.add_dependency("gdkpixbuf")
|
84
|
+
# TODO - add rsvg2
|
81
85
|
end
|
82
86
|
|
83
87
|
# package the library into a gem
|
data/lib/pdf/wrapper/images.rb
CHANGED
@@ -117,7 +117,7 @@ module PDF
|
|
117
117
|
# based on a similar function in rabbit. Thanks Kou.
|
118
118
|
load_libpoppler
|
119
119
|
x, y = current_point
|
120
|
-
page = Poppler::Document.new(filename).get_page(
|
120
|
+
page = Poppler::Document.new(filename).get_page(0)
|
121
121
|
w, h = page.size
|
122
122
|
width, height = calc_image_dimensions(opts[:width], opts[:height], w, h, opts[:proportional])
|
123
123
|
x, y = calc_image_coords(opts[:left] || x, opts[:top] || y, opts[:width] || w, opts[:height] || h, width, height, opts[:center])
|
@@ -191,7 +191,7 @@ module PDF
|
|
191
191
|
case detect_image_type(filename)
|
192
192
|
when :pdf then
|
193
193
|
load_libpoppler
|
194
|
-
page = Poppler::Document.new(filename).get_page(
|
194
|
+
page = Poppler::Document.new(filename).get_page(0)
|
195
195
|
return page.size
|
196
196
|
when :png then
|
197
197
|
img_surface = Cairo::ImageSurface.from_png(filename)
|
data/lib/pdf/wrapper/text.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
module PDF
|
2
2
|
class Wrapper
|
3
3
|
|
4
|
+
DEFAULT_CELL_PADDING = 3
|
5
|
+
|
4
6
|
# Change the default font size
|
5
7
|
#
|
6
8
|
# If no block is provided, the change is permanent. If a block
|
@@ -52,16 +54,24 @@ module PDF
|
|
52
54
|
# <tt>:border_color</tt>:: What color should the border be?
|
53
55
|
# <tt>:fill_color</tt>:: A background color for the cell. Defaults to none.
|
54
56
|
# <tt>:radius</tt>:: Give the border around the cell rounded corners. Implies :border => "tblr"
|
57
|
+
#
|
58
|
+
# Unlike with the text() method, the :font_size argument can be a range. The
|
59
|
+
# largest size in the range that will fit all the text in the cell will be used.
|
60
|
+
#
|
55
61
|
def cell(str, x, y, w, h, opts={})
|
56
62
|
# TODO: add a wrap option so wrapping can be disabled
|
57
63
|
# TODO: add an option for vertical alignment
|
58
64
|
# TODO: allow cell contents to be defined as a block, like link_to in EDGE rails
|
59
65
|
|
60
66
|
options = default_text_options
|
61
|
-
options.merge!({:border => "tblr", :border_width => @default_line_width, :border_color => :black, :fill_color => nil, :padding =>
|
67
|
+
options.merge!({:border => "tblr", :border_width => @default_line_width, :border_color => :black, :fill_color => nil, :padding => DEFAULT_CELL_PADDING, :radius => nil})
|
62
68
|
options.merge!(opts)
|
63
69
|
options.assert_valid_keys(default_text_options.keys + [:width, :border, :border_width, :border_color, :fill_color, :padding, :radius])
|
64
70
|
|
71
|
+
if options[:font_size] && options[:font_size].is_a?(Range)
|
72
|
+
options[:font_size] = best_font_size(str, w, h, options[:font_size], options)
|
73
|
+
end
|
74
|
+
|
65
75
|
# apply padding
|
66
76
|
textw = w - (options[:padding] * 2)
|
67
77
|
texth = h - (options[:padding] * 2)
|
@@ -214,6 +224,22 @@ module PDF
|
|
214
224
|
|
215
225
|
private
|
216
226
|
|
227
|
+
# calculate the largest font size within range that will fit str in
|
228
|
+
# a box with dimensions of height by width.
|
229
|
+
#
|
230
|
+
# best_font_size("Hello There", 50, 50, 5..9)
|
231
|
+
# => 9
|
232
|
+
#
|
233
|
+
def best_font_size(str, width, height, sizes, options = {})
|
234
|
+
cellpadding = DEFAULT_CELL_PADDING * 2
|
235
|
+
sizes.map { |size|
|
236
|
+
opts = options.only(:markup).merge(:font_size => size)
|
237
|
+
h = text_height( str, width, opts )
|
238
|
+
h <= height - cellpadding ? size : nil
|
239
|
+
}.compact[-1] || sizes.first
|
240
|
+
end
|
241
|
+
|
242
|
+
|
217
243
|
# takes a string and a range of options and creates a pango layout for us. Pango
|
218
244
|
# does all the hard work of calculating text layout, wrapping, fonts, sizes,
|
219
245
|
# direction and more. Thank $diety.
|
data/lib/pdf/wrapper.rb
CHANGED
@@ -6,17 +6,15 @@ require 'pdf/errors'
|
|
6
6
|
require 'tempfile'
|
7
7
|
require 'fileutils'
|
8
8
|
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
12
|
-
require
|
13
|
-
require
|
14
|
-
require
|
15
|
-
require
|
16
|
-
require
|
17
|
-
|
18
|
-
require 'rubygems'
|
19
|
-
gem 'cairo', '>=1.5'
|
9
|
+
require "pdf/wrapper/graphics"
|
10
|
+
require "pdf/wrapper/images"
|
11
|
+
require "pdf/wrapper/loading"
|
12
|
+
require "pdf/wrapper/text_cell"
|
13
|
+
require "pdf/wrapper/text_image_cell"
|
14
|
+
require "pdf/wrapper/table"
|
15
|
+
require "pdf/wrapper/text"
|
16
|
+
require "pdf/wrapper/page"
|
17
|
+
|
20
18
|
require 'cairo'
|
21
19
|
|
22
20
|
module PDF
|
data/specs/text_spec.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require File.dirname(__FILE__) + '/spec_helper'
|
4
4
|
|
5
|
-
|
5
|
+
describe PDF::Wrapper do
|
6
6
|
|
7
7
|
before(:each) { create_pdf }
|
8
8
|
|
@@ -168,7 +168,8 @@ 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
|
171
|
+
(@pdf.text_width(str2, opts) >= 1106).should be_true
|
172
|
+
(@pdf.text_width(str2, opts) <= 1107).should be_true
|
172
173
|
end
|
173
174
|
|
174
175
|
specify "should raise an exception if build_pango_layout is passed anything other than a string" do
|
@@ -246,4 +247,10 @@ context "The PDF::Wrapper class" do
|
|
246
247
|
lambda { @pdf.text msg, :wrap => :ponies }.should raise_error(ArgumentError)
|
247
248
|
end
|
248
249
|
|
250
|
+
specify "should determine the largest font size possible that will fit some text in a cell" do
|
251
|
+
@pdf.__send__(:best_font_size, "Hello There", 34, 50, 5..9).should eql(9)
|
252
|
+
@pdf.__send__(:best_font_size, "<b>Hello There</b>", 34, 50, 5..9, :markup => :pango).should eql(8)
|
253
|
+
@pdf.__send__(:best_font_size, "Hello There", 5, 50, 5..9).should eql(5)
|
254
|
+
end
|
255
|
+
|
249
256
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pdf-wrapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
- 2
|
10
|
+
version: 0.3.2
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- James Healy
|
@@ -9,19 +15,24 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-09-06 00:00:00 +10:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: cairo
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
20
26
|
requirements:
|
21
|
-
- -
|
27
|
+
- - ~>
|
22
28
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
29
|
+
hash: 31
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 8
|
33
|
+
version: "1.8"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
25
36
|
description: A unicode aware PDF writing library that uses the ruby bindings to various c libraries ( like cairo, pango, poppler and rsvg ) to do the heavy lifting.
|
26
37
|
email: jimmy@deefa.com
|
27
38
|
executables: []
|
@@ -94,21 +105,27 @@ rdoc_options:
|
|
94
105
|
require_paths:
|
95
106
|
- lib
|
96
107
|
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
97
109
|
requirements:
|
98
110
|
- - ">="
|
99
111
|
- !ruby/object:Gem::Version
|
112
|
+
hash: 3
|
113
|
+
segments:
|
114
|
+
- 0
|
100
115
|
version: "0"
|
101
|
-
version:
|
102
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
103
118
|
requirements:
|
104
119
|
- - ">="
|
105
120
|
- !ruby/object:Gem::Version
|
121
|
+
hash: 3
|
122
|
+
segments:
|
123
|
+
- 0
|
106
124
|
version: "0"
|
107
|
-
version:
|
108
125
|
requirements: []
|
109
126
|
|
110
127
|
rubyforge_project: pdf-wrapper
|
111
|
-
rubygems_version: 1.3.
|
128
|
+
rubygems_version: 1.3.7
|
112
129
|
signing_key:
|
113
130
|
specification_version: 3
|
114
131
|
summary: A PDF generating library built on top of cairo
|