prawn 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'rake/testtask'
4
4
  require "rake/rdoctask"
5
5
  require "rake/gempackagetask"
6
6
 
7
- PRAWN_VERSION = "0.2.0"
7
+ PRAWN_VERSION = "0.2.1"
8
8
 
9
9
  task :default => [:test]
10
10
 
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require "prawn"
5
+
6
+ Prawn::Document.generate("span.pdf") do
7
+
8
+ # Spans will typically be used outside of bounding boxes as a way to build
9
+ # single columns of flowing text that span across pages.
10
+ #
11
+ span(350, :position => :center) do
12
+ text "Here's some centered text in a 350 point column. " * 100
13
+ end
14
+
15
+ # Spans are not really compatible with bounding boxes because they break
16
+ # the nesting chain and also may position text outside of the bounding box
17
+ # boundaries, but sometimes you may wish to use them anyway for convenience
18
+ # Here's an example of how to do that dynamically.
19
+ #
20
+ bounding_box([50,300], :width => 400) do
21
+ text "Here's some default bounding box text. " * 10
22
+ span(bounds.width,
23
+ :position => bounds.absolute_left - margin_box.absolute_left) do
24
+ text "The rain in spain falls mainly on the plains. " * 300
25
+ end
26
+ end
27
+ end
@@ -20,7 +20,7 @@ module Prawn
20
20
  # The base source directory for Prawn as installed on the system
21
21
  BASEDIR = File.expand_path(File.join(dir, '..'))
22
22
 
23
- VERSION = "0.2.0"
23
+ VERSION = "0.2.1"
24
24
 
25
25
  extend self
26
26
 
@@ -12,6 +12,7 @@ require "prawn/document/bounding_box"
12
12
  require "prawn/document/text"
13
13
  require "prawn/document/table"
14
14
  require "prawn/document/internals"
15
+ require "prawn/document/span"
15
16
 
16
17
  module Prawn
17
18
  class Document
@@ -78,7 +79,7 @@ module Prawn
78
79
  def initialize(options={},&block)
79
80
  Prawn.verify_options [:page_size, :page_layout, :on_page_start,
80
81
  :on_page_stop, :left_margin, :right_margin, :top_margin,
81
- :bottom_margin, :skip_page_creation, :compress ], options
82
+ :bottom_margin, :skip_page_creation, :compress, :skip_encoding, :text_options ], options
82
83
 
83
84
  @objects = []
84
85
  @info = ref(:Creator => "Prawn", :Producer => "Prawn")
@@ -0,0 +1,31 @@
1
+ module Prawn
2
+ class Document
3
+ def span(width, options={})
4
+ Prawn.verify_options [:position], options
5
+ original_position = self.y
6
+
7
+ # FIXME: How many effing times do I want to write this same code?
8
+ left_boundary = case(options[:position] || :left)
9
+ when :left
10
+ margin_box.absolute_left
11
+ when :center
12
+ margin_box.absolute_left + margin_box.width / 2.0 - width /2.0
13
+ when :right
14
+ margin_box.absolute_right - width
15
+ when Numeric
16
+ margin_box.absolute_left + options[:position]
17
+ else
18
+ raise ArgumentError, "Invalid option for :position"
19
+ end
20
+
21
+ # we need to bust out of whatever nested bounding boxes we're in.
22
+ canvas do
23
+ bounding_box([left_boundary,
24
+ margin_box.absolute_top], :width => width) do
25
+ self.y = original_position
26
+ yield
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -39,7 +39,7 @@ module Prawn
39
39
 
40
40
  # Hash of Font objects keyed by names
41
41
  #
42
- def font_registry
42
+ def font_registry #:nodoc:
43
43
  @font_registry ||= {}
44
44
  end
45
45
 
@@ -94,14 +94,28 @@ module Prawn
94
94
 
95
95
  DEFAULT_SIZE = 12
96
96
 
97
- def self.register(name,options={})
97
+ def self.register(name,options={}) #:nodoc:
98
98
  options[:for].font_registry[name] = Font.new(name,options)
99
99
  end
100
-
101
- attr_reader :metrics, :identifier, :reference, :name, :family
100
+
101
+ # The font metrics object
102
+ attr_reader :metrics
103
+
104
+ # The current font name
105
+ attr_reader :name
106
+
107
+ # The current font family
108
+ attr_reader :family
109
+
110
+ attr_reader :identifier, :reference #:nodoc:
111
+
112
+ # Sets the size of the current font:
113
+ #
114
+ # font.size = 16
115
+ #
102
116
  attr_writer :size
103
117
 
104
- def initialize(name,options={})
118
+ def initialize(name,options={}) #:nodoc:
105
119
  @name = name
106
120
  @family = options[:family]
107
121
 
@@ -113,7 +127,7 @@ module Prawn
113
127
  @identifier = :"F#{@document.font_registry.size + 1}"
114
128
 
115
129
  case(name)
116
- when /\.ttf$/
130
+ when /\.ttf$/i
117
131
  embed_ttf(name)
118
132
  else
119
133
  register_builtin(name)
@@ -170,11 +184,15 @@ module Prawn
170
184
  @metrics.font_height(@size)
171
185
  end
172
186
 
173
- def ascender # :nodoc:
187
+ # The height of the ascender at the current font size in PDF points
188
+ #
189
+ def ascender
174
190
  @metrics.ascender / 1000.0 * @size
175
191
  end
176
192
 
177
- def descender # :nodoc:
193
+ # The height of the descender at the current font size in PDF points
194
+ #
195
+ def descender
178
196
  @metrics.descender / 1000.0 * @size
179
197
  end
180
198
 
@@ -7,7 +7,7 @@
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
 
9
9
  module Prawn
10
- class Font #:nodoc:
10
+ class Font
11
11
  class CMap #:nodoc:
12
12
 
13
13
  def initialize
@@ -10,13 +10,13 @@
10
10
  # This is free software. Please see the LICENSE and COPYING files for details.
11
11
 
12
12
  module Prawn
13
- class Font #:nodoc:
13
+ class Font
14
14
  class Metrics #:nodoc:
15
15
 
16
16
  include Prawn::Font::Wrapping
17
17
 
18
18
  def self.[](font)
19
- data[font] ||= (font.match(/\.ttf$/) ? TTF : Adobe).new(font)
19
+ data[font] ||= (font.match(/\.ttf$/i) ? TTF : Adobe).new(font)
20
20
  end
21
21
 
22
22
  def self.data
@@ -6,17 +6,17 @@
6
6
  #
7
7
  # This is free software. Please see the LICENSE and COPYING files for details.
8
8
  module Prawn
9
- class Font #:nodoc:
9
+ class Font
10
10
  module Wrapping #:nodoc:
11
11
  ruby_18 { $KCODE="U" }
12
12
 
13
13
  # TODO: Replace with TeX optimal algorithm
14
14
  def naive_wrap(string, line_width, font_size, options = {})
15
- accumulated_width = 0
16
15
  scan_pattern = options[:mode] == :character ? /./ : /\S+|\s+/
17
16
 
18
17
  output = ""
19
- string.lines.each do |line|
18
+ string.lines.each do |line|
19
+ accumulated_width = 0
20
20
  segments = line.scan(scan_pattern)
21
21
 
22
22
  segments.each do |segment|
@@ -24,7 +24,7 @@ module Prawn
24
24
  :kerning => options[:kerning])
25
25
 
26
26
  if (accumulated_width + segment_width).round > line_width.round
27
- output = "#{output.rstrip}\n"
27
+ output = "#{output.sub(/[ \t]*\n?(\n*)\z/, "\n\\1")}"
28
28
 
29
29
  if segment =~ /\s/
30
30
  accumulated_width = 0
@@ -36,11 +36,15 @@ module Prawn
36
36
  # proportionally. When both are provided, the image will be stretched to
37
37
  # fit the dimensions without maintaining the aspect ratio.
38
38
  #
39
- def image(filename, options={})
39
+ def image(file, options={})
40
40
  Prawn.verify_options [:at,:position, :height, :width, :scale], options
41
- raise ArgumentError, "#{filename} not found" unless File.file?(filename)
42
41
 
43
- image_content = File.read_binary(filename)
42
+ if file.kind_of?(IO)
43
+ image_content = file.read
44
+ else
45
+ raise ArgumentError, "#{file} not found" unless File.file?(file)
46
+ image_content = File.read_binary(file)
47
+ end
44
48
 
45
49
  image_sha1 = Digest::SHA1.hexdigest(image_content)
46
50
 
@@ -83,6 +87,8 @@ module Prawn
83
87
  # add the image to the current page
84
88
  instruct = "\nq\n%.3f 0 0 %.3f %.3f %.3f cm\n/%s Do\nQ"
85
89
  add_content instruct % [ w, h, x, y - h, label ]
90
+
91
+ return info
86
92
  end
87
93
 
88
94
  private
@@ -17,7 +17,29 @@ describe "Font Metrics" do
17
17
  comicsans = "#{Prawn::BASEDIR}/data/fonts/comicsans.ttf"
18
18
  @pdf.font(comicsans)
19
19
  @pdf.font.metrics.should == Prawn::Font::Metrics[comicsans]
20
- end
20
+ end
21
+
22
+ it "should wrap text" do
23
+ @pdf = Prawn::Document.new
24
+ @pdf.font "Courier"
25
+ @pdf.font.metrics.naive_wrap("Please wrap this text about HERE. More text that should be wrapped", 220, @pdf.font.size).should ==
26
+ "Please wrap this text about\nHERE. More text that should be\nwrapped"
27
+ end
28
+
29
+ it "should respect end of line when wrapping text" do
30
+ @pdf = Prawn::Document.new
31
+ @pdf.font "Courier"
32
+ text = "Please wrap only before\nTHIS word. Don't wrap this"
33
+ @pdf.font.metrics.naive_wrap(text, 220, @pdf.font.size).should == text
34
+ end
35
+
36
+ it "should respect end of line when wrapping text and mode is set to 'character'" do
37
+ @pdf = Prawn::Document.new
38
+ @pdf.font "Courier"
39
+ opts = {:mode => :character}
40
+ @pdf.font.metrics.naive_wrap("You can wrap this text HERE", 180, @pdf.font.size, opts).should ==
41
+ "You can wrap this text HE\nRE"
42
+ end
21
43
 
22
44
  end
23
45
 
@@ -5,7 +5,7 @@ require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
5
5
  describe "the image() function" do
6
6
 
7
7
  before(:each) do
8
- @filename = "#{Prawn::BASEDIR}/data/images/pigs.jpg"
8
+ @filename = "#{Prawn::BASEDIR}/data/images/pigs.jpg"
9
9
  create_pdf
10
10
  end
11
11
 
@@ -21,5 +21,20 @@ describe "the image() function" do
21
21
  # but only 1 image xobject
22
22
  output.scan(/\/Type \/XObject/).size.should == 1
23
23
  end
24
+
25
+ it "should return the image info object" do
26
+ info = @pdf.image(@filename)
27
+
28
+ assert info.kind_of?(Prawn::Images::JPG)
29
+
30
+ info.height.should == 453
31
+ end
32
+
33
+ it "should accept IO objects" do
34
+ file = File.open(@filename, "rb")
35
+ info = @pdf.image(file)
36
+
37
+ info.height.should == 453
38
+ end
24
39
  end
25
40
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregory Brown
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-12 00:00:00 -04:00
12
+ date: 2008-10-17 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -53,6 +53,7 @@ files:
53
53
  - examples/simple_text.rb
54
54
  - examples/simple_text_ttf.rb
55
55
  - examples/sjis.rb
56
+ - examples/span.rb
56
57
  - examples/table.rb
57
58
  - examples/text_flow.rb
58
59
  - examples/utf8.rb
@@ -62,6 +63,7 @@ files:
62
63
  - lib/prawn/document/bounding_box.rb
63
64
  - lib/prawn/document/internals.rb
64
65
  - lib/prawn/document/page_geometry.rb
66
+ - lib/prawn/document/span.rb
65
67
  - lib/prawn/document/table.rb
66
68
  - lib/prawn/document/text.rb
67
69
  - lib/prawn/document.rb