forked from prawnpdf/prawn
/
inline_format.rb
104 lines (87 loc) · 4.18 KB
/
inline_format.rb
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
# encoding: utf-8
#
# This example shows how to use inline formatting
#
require File.expand_path(File.join(File.dirname(__FILE__),
%w[.. example_helper]))
Prawn::Document.generate("inline_format.pdf") do |pdf|
pdf.text("hello <strikethrough><b>world\nhow <i>are</i></b> you?</strikethrough> world, <u>how are you</u> now?",
:inline_format => true)
pdf.text("<font size='14'>left: </font>" + "hello <b>world <font name='Times-Roman' size='28'>how</font> <i>are</i></b> you? <font size='14'><b>Goodbye.</b></font> " * 8,
:inline_format => true)
pdf.text("right: " + "hello <b>world how <i>are</i></b> you? " * 2,
:inline_format => true,
:align => :right)
pdf.text("center: " + "hello <b>world how <i>are</i></b> you? " * 2,
:inline_format => true,
:align => :center)
pdf.text("\njustify: " + "hello <b>world <i>goodbye</i></b> " * 12 + "the end ",
:inline_format => true,
:align => :justify)
pdf.text("\njustify: " + "hello world goodbye " * 12 + "the end ",
:inline_format => true,
:align => :justify)
pdf.text("\njustify: " + "hello world goodbye " * 12 + "the end ",
:align => :justify)
pdf.text("<color rgb='00ff00'>link: <font size='24' character_spacing='7.5'>please make</font> <color rgb='#0000ff'><u><link href='http://wiki.github.com/sandal/prawn/'>this</link></u></color> clickable.</color> Here we have A<color rgb='#0000ff'><sup><link href='http://wiki.github.com/sandal/prawn/'>superscript</link></sup></color> link and A<color rgb='#0000ff'><sub><link href='http://wiki.github.com/sandal/prawn/'> subscript</link></sub></color> link.",
:inline_format => true)
pdf.text("<color c='100' m='0' y='0' k='0'><font size='24'>CMYK</font></color>",
:inline_format => true)
file = "#{Prawn::BASEDIR}/data/fonts/Action Man.dfont"
pdf.font_families["Action Man"] = {
:normal => { :file => file, :font => "ActionMan" },
:italic => { :file => file, :font => "ActionMan-Italic" },
:bold => { :file => file, :font => "ActionMan-Bold" },
:bold_italic => { :file => file, :font => "ActionMan-BoldItalic" }
}
pdf.font("Action Man")
pdf.text("\nhello <b>world\nhow <i>are</i></b> you?",
:inline_format => true)
pdf.font("Helvetica")
class Highlight
def initialize(options)
@color = options[:color]
@document = options[:document]
end
def render_behind(fragment)
original_color = @document.fill_color
@document.fill_color = @color
@document.fill_rectangle(fragment.top_left,
fragment.width,
fragment.height)
@document.fill_color = original_color
end
end
class FragmentBorder
def initialize(options)
@radius = options[:radius]
@connect_corners = options[:connect_corners]
@document = options[:document]
end
def render_in_front(fragment)
box = fragment.bounding_box
if @connect_corners
@document.stroke_polygon(fragment.top_left, fragment.top_right,
fragment.bottom_right, fragment.bottom_left)
end
@document.stroke_circle_at(fragment.top_left, :radius => @radius)
@document.stroke_circle_at(fragment.top_right, :radius => @radius)
@document.stroke_circle_at(fragment.bottom_right, :radius => @radius)
@document.stroke_circle_at(fragment.bottom_left, :radius => @radius)
end
end
highlight_callback = Highlight.new(:color => 'ffff00', :document => pdf)
border_callback = FragmentBorder.new(:radius => 2.5,
:connect_corners => true,
:document => pdf)
pdf.formatted_text([
{ :text => "\n" },
{ :text => "hello ",
:callback => highlight_callback },
{ :text => "world",
:size => 24,
:character_spacing => 0,
:callback => [highlight_callback, border_callback] },
{ :text => " hello" }
], :indent_paragraphs => 40, :character_spacing => -2)
end