sandal / prawn

Fast, Nimble PDF Writer for Ruby

This URL has Read+Write access

prawn / lib / prawn / document / text / box.rb
a123f498 » sandal 2008-11-13 Experimental text box idea 1 module Prawn
2 class Document
3
4 def text_box(text,options)
5 Text::Box.new(text, options.merge(:for => self)).render
6 end
7
8 module Text
9 class Box
10 def initialize(text,options={})
11 @document = options[:for]
12 @text = text
13 @at = options[:at] || [0, @document.y - @document.bounds.absolute_bottom]
14 @width = options[:width] || @document.bounds.width
15 @height = options[:height]
16 @overflow = options[:overflow] || :truncate
17 end
18
19 def render
20 x,y = @at
21
22 unless @overflow == :expand
23 fit_text_to_box
24 end
25
26 @document.y = @document.bounds.absolute_bottom + y
27 @document.span(@width, :position => x) do
28 @document.text @text
29 end
30
31 end
32
33 private
34
35 def fit_text_to_box
36 text = @document.font.metrics.naive_wrap(@text,
37 @width, @document.font.size)
38
39 max_lines = (@height / @document.font.height).floor
40
41 unless text.lines.size < max_lines
42 @text = text.lines[0...max_lines].join
43 case(@overflow)
44 when :ellipses
45 @text[-3..-1] = "..."
46 end
47 end
48 end
49
50 end
51 end
52 end
53 end