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
# encoding: utf-8
# 
# This example is a demonstration of how Prawn does its text positioning,
# meant to assist those that need to do advanced positioning calculations.
# Run the example for a clearer picture of how things work
#
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
require 'prawn'

Prawn::Document.generate('font_calculations.pdf') do
  
  def demonstration(pos)
    move_down 10

    stroke_horizontal_rule

    text "When using flowing text, Prawn will position text\n" +
         "starting font.#{pos} below the baseline, and leave\n" +
         "the y-cursor at the baseline of the next line of text"

    stroke_horizontal_rule

    move_down 20

    bl = y - bounds.absolute_bottom

    stroke_horizontal_rule
    text "When using text positioned with :at, the baseline is used", :at => [0, bl]

    text "(and the Y-cursor is not moved)", :at => [350, bl]

    colors = { :ascender    => "ff0000", 
               :descender   => "00ff00",
               :line_gap    => "0000ff",
               :font_height => "005500" }


    pad(20) do                    
      text "Calculations Demo", :size => 16
    end

    fill_color colors[:ascender]
    text "ASCENDER"

    fill_color colors[:descender]
    text "DESCENDER"

    fill_color colors[:line_gap]
    text "LINEGAP" 

    fill_color colors[:font_height]
    text "FONT_HEIGHT"

    fill_color "000000"
    font.size = 20

    move_down 40

    bl = y - bounds.absolute_bottom
    text "The quick brown fox jumps over the lazy dog.", :at => [0, bl]

    stroke_color colors[:ascender]
    stroke_line [0, bl], [0, bl + font.ascender]
    stroke_line [0, bl + font.ascender], [bounds.width, bl + font.ascender]

    stroke_color colors[:descender]
    stroke_line [0,bl], [0, bl + font.descender]
    stroke_line [0, bl + font.descender], [bounds.width, bl + font.descender]

    stroke_color colors[:line_gap]
    stroke_line [0, bl + font.descender], [0,bl + font.descender - font.line_gap]
    stroke_line [0, bl + font.descender - font.line_gap], 
                [bounds.width,bl + font.descender - font.line_gap]

    stroke_color colors[:font_height]
    stroke_line [bounds.width, bl + font.ascender],
                [bounds.width, bl + font.descender - font.line_gap]
                
    stroke_color "000000"
    fill_color "000000"
  end
  
  text "Using AFM", :size => 20
  demonstration("height")
  
  move_down 75
  font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
  text "Using TTF", :size => 20
  demonstration("ascender")
  
end