public
Description: Fast, Nimble PDF Writer for Ruby
Homepage: http://prawn.majesticseacreature.com
Clone URL: git://github.com/sandal/prawn.git
prawn / spec / text_spec.rb
100644 143 lines (110 sloc) 3.817 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
 
class TextObserver
  
  attr_accessor :font_settings, :size, :string
            
  def initialize
    @font_settings = []
    @fonts = {}
  end
  
  def resource_font(*params)
    @fonts[params[0]] = params[1].basefont
  end
 
  def set_text_font_and_size(*params)
    @font_settings << { :name => @fonts[params[0]], :size => params[1] }
  end
  
  def show_text(*params)
    @string = params[0]
  end
end
 
class FontObserver
 
  attr_accessor :page_fonts
 
  def initialize
    @page_fonts = []
  end
 
  def resource_font(*params)
    @page_fonts.last << params[1].basefont
  end
 
  def begin_page(*params)
    @page_fonts << []
  end
end
 
describe "when drawing text" do
   
   before(:each) { create_pdf }
 
   it "should advance down the document based on font_height" do
     position = @pdf.y
     @pdf.text "Foo"
 
     @pdf.y.should be_close(position - @pdf.font_metrics.font_height(12),
                            0.0001)
 
     position = @pdf.y
     @pdf.text "Foo\nBar\nBaz"
     @pdf.y.should be_close(position - 3*@pdf.font_metrics.font_height(12),
                            0.0001)
   end
   
   it "should default to 12 point helvetica" do
      @pdf.text "Blah", :at => [100,100]
      text = observer(TextObserver)
      text.font_settings[0][:name].should == :Helvetica
      text.font_settings[0][:size].should == 12
      text.string.should == "Blah"
   end
   
   it "should allow setting font size" do
     @pdf.text "Blah", :at => [100,100], :size => 16
     text = observer(TextObserver)
     text.font_settings[0][:size].should == 16
   end
   
   it "should allow setting a default font size" do
     @pdf.font_size! 16
     @pdf.text "Blah"
     text = observer(TextObserver)
     text.font_settings[0][:size].should == 16
   end
   
   it "should allow overriding default font for a single instance" do
     @pdf.font_size! 16
 
     @pdf.text "Blah", :size => 11
     @pdf.text "Blaz"
     text = observer(TextObserver)
     text.font_settings[0][:size].should == 11
     text.font_settings[1][:size].should == 16
   end
   
   
   it "should allow setting a font size transaction with a block" do
     @pdf.font_size 16 do
       @pdf.text 'Blah'
     end
 
     @pdf.text 'blah'
 
     text = observer(TextObserver)
     text.font_settings[0][:size].should == 16
     text.font_settings[1][:size].should == 12
   end
   
   it "should allow manual setting the font size " +
       "when in a font size block" do
     @pdf.font_size 16 do
        @pdf.text 'Foo'
        @pdf.text 'Blah', :size => 11
        @pdf.text 'Blaz'
      end
      text = observer(TextObserver)
      text.font_settings[0][:size].should == 16
      text.font_settings[1][:size].should == 11
      text.font_settings[2][:size].should == 16
   end
      
   it "should allow registering of built-in font_settings on the fly" do
     @pdf.font "Courier"
     @pdf.text "Blah", :at => [100,100]
     @pdf.font "Times-Roman"
     @pdf.text "Blaz", :at => [150,150]
     text = observer(TextObserver)
            
     text.font_settings[0][:name].should == :Courier
     text.font_settings[1][:name].should == :"Times-Roman"
   end
 
   it "should utilise the same default font across multiple pages" do
     @pdf.text "Blah", :at => [100,100]
     @pdf.start_new_page
     @pdf.text "Blaz", :at => [150,150]
     text = observer(FontObserver)
 
     text.page_fonts.size.should eql(2)
     text.page_fonts[0][0].should eql(:Helvetica)
     text.page_fonts[1][0].should eql(:Helvetica)
   end
   
   it "should raise an exception when an unknown font is used" do
     lambda { @pdf.font "Pao bu" }.should raise_error(Prawn::Errors::UnknownFont)
   end
 
end