prawn-format 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +37 -0
- data/Rakefile +31 -0
- data/examples/basic-formatting.rb +37 -0
- data/examples/christmas-carol.txt +717 -0
- data/examples/document.rb +61 -0
- data/examples/flowing.rb +24 -0
- data/examples/style-classes.rb +12 -0
- data/examples/syntax-highlighting.rb +31 -0
- data/examples/tags.rb +24 -0
- data/lib/prawn/format.rb +211 -0
- data/lib/prawn/format/effects/link.rb +30 -0
- data/lib/prawn/format/effects/underline.rb +32 -0
- data/lib/prawn/format/instructions/base.rb +62 -0
- data/lib/prawn/format/instructions/tag_close.rb +52 -0
- data/lib/prawn/format/instructions/tag_open.rb +95 -0
- data/lib/prawn/format/instructions/text.rb +89 -0
- data/lib/prawn/format/layout_builder.rb +113 -0
- data/lib/prawn/format/lexer.rb +222 -0
- data/lib/prawn/format/line.rb +99 -0
- data/lib/prawn/format/parser.rb +181 -0
- data/lib/prawn/format/state.rb +189 -0
- data/lib/prawn/format/text_object.rb +107 -0
- data/lib/prawn/format/version.rb +11 -0
- data/manual/html.rb +187 -0
- data/manual/include/basics.rb +6 -0
- data/manual/include/breaks.rb +13 -0
- data/manual/include/custom-tags.rb +10 -0
- data/manual/include/custom-tags2.rb +2 -0
- data/manual/include/indent.rb +4 -0
- data/manual/include/options.rb +15 -0
- data/manual/include/style-classes.rb +5 -0
- data/manual/manual.txt +101 -0
- data/manual/pdf.rb +204 -0
- data/prawn-format.gemspec +45 -0
- data/spec/layout_builder_spec.rb +27 -0
- data/spec/lexer_spec.rb +91 -0
- data/spec/parser_spec.rb +103 -0
- data/spec/spec_helper.rb +24 -0
- metadata +157 -0
@@ -0,0 +1,107 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'prawn/graphics/color'
|
4
|
+
|
5
|
+
module Prawn
|
6
|
+
module Format
|
7
|
+
class TextObject
|
8
|
+
include Prawn::Graphics::Color
|
9
|
+
|
10
|
+
RENDER_MODES = {
|
11
|
+
:fill => 0,
|
12
|
+
:stroke => 1,
|
13
|
+
:fill_stroke => 2,
|
14
|
+
:invisible => 3,
|
15
|
+
:fill_clip => 4,
|
16
|
+
:stroke_clip => 5,
|
17
|
+
:fill_stroke_clip => 6,
|
18
|
+
:clip => 7
|
19
|
+
}
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@content = nil
|
23
|
+
@last_x = @last_y = 0
|
24
|
+
end
|
25
|
+
|
26
|
+
def open
|
27
|
+
@content = "BT\n"
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def close
|
32
|
+
@content << "ET"
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def move_to(x, y)
|
37
|
+
move_by(x - @last_x, y - @last_y)
|
38
|
+
end
|
39
|
+
|
40
|
+
def move_by(dx,dy)
|
41
|
+
@last_x += dx
|
42
|
+
@last_y += dy
|
43
|
+
@content << "#{dx} #{dy} Td\n"
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def next_line(dy)
|
48
|
+
end
|
49
|
+
|
50
|
+
def show(argument)
|
51
|
+
instruction = argument.is_a?(Array) ? "TJ" : "Tj"
|
52
|
+
@content << "#{Prawn::PdfObject(argument, true)} #{instruction}\n"
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def character_space(dc)
|
57
|
+
@content << "#{dc} Tc\n"
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
def word_space(dw)
|
62
|
+
@content << "#{dw} Tw\n"
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
def leading(dl)
|
67
|
+
@content << "#{dl} TL\n"
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
def font(identifier, size)
|
72
|
+
@content << "/#{identifier} #{size} Tf\n"
|
73
|
+
self
|
74
|
+
end
|
75
|
+
|
76
|
+
def render(mode)
|
77
|
+
mode_value = RENDER_MODES[mode] || raise(ArgumentError, "unsupported render mode #{mode.inspect}, should be one of #{RENDER_MODES.keys.inspect}")
|
78
|
+
@content << "#{mode_value} Tr\n"
|
79
|
+
self
|
80
|
+
end
|
81
|
+
|
82
|
+
def rise(value)
|
83
|
+
@content << "#{value} Ts\n"
|
84
|
+
self
|
85
|
+
end
|
86
|
+
|
87
|
+
def rotate(x, y, theta)
|
88
|
+
radians = theta * Math::PI / 180
|
89
|
+
cos, sin = Math.cos(radians), Math.sin(radians)
|
90
|
+
arr = [cos, sin, -sin, cos, x, y]
|
91
|
+
add_content "%.3f %.3f %.3f %.3f %.3f %.3f Tm" % arr
|
92
|
+
end
|
93
|
+
|
94
|
+
def to_s
|
95
|
+
@content
|
96
|
+
end
|
97
|
+
|
98
|
+
def to_str
|
99
|
+
@content
|
100
|
+
end
|
101
|
+
|
102
|
+
def add_content(string)
|
103
|
+
@content << string << "\n"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/manual/html.rb
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
|
2
|
+
require 'prawn/format/version'
|
3
|
+
require 'coderay'
|
4
|
+
|
5
|
+
def process_style(document, style, content, line_number)
|
6
|
+
content = process_substitutions(content)
|
7
|
+
|
8
|
+
case style
|
9
|
+
when "h1" then h1(document, content)
|
10
|
+
when "h2" then h2(document, content)
|
11
|
+
when "p" then paragraph(document, content)
|
12
|
+
when "fp" then paragraph(document, content, false)
|
13
|
+
when "ul" then start_list(document)
|
14
|
+
when "li" then list_item(document, content)
|
15
|
+
when "/ul" then end_list(document)
|
16
|
+
when "page" then new_page(document)
|
17
|
+
when "highlight" then highlight(document, content)
|
18
|
+
when "hr" then horiz_rule(document)
|
19
|
+
when "center" then center(document, content)
|
20
|
+
else warn "unknown style #{style.inspect}"
|
21
|
+
end
|
22
|
+
|
23
|
+
rescue Exception => err
|
24
|
+
puts "[error occurred while processing line ##{line_number}]"
|
25
|
+
raise
|
26
|
+
end
|
27
|
+
|
28
|
+
def process_substitutions(content)
|
29
|
+
content.
|
30
|
+
gsub(/%FORMAT:VERSION%/, Prawn::Format::Version::STRING).
|
31
|
+
gsub(/%NOW%/, Time.now.utc.strftime("%e %B %Y at %H:%M UTC")).
|
32
|
+
gsub(/%PDF\{(.*?)\}HTML\{(.*?)\}END%/, '\\2')
|
33
|
+
end
|
34
|
+
|
35
|
+
def center(document, content)
|
36
|
+
document << "<center>#{content}</center>\n"
|
37
|
+
end
|
38
|
+
|
39
|
+
def horiz_rule(document)
|
40
|
+
document << "<hr />\n"
|
41
|
+
end
|
42
|
+
|
43
|
+
def h1(document, content)
|
44
|
+
document << "<h1>#{content}</h1>\n"
|
45
|
+
end
|
46
|
+
|
47
|
+
def h2(document, content)
|
48
|
+
document << "<h2>#{content}</h2>\n"
|
49
|
+
end
|
50
|
+
|
51
|
+
def paragraph(document, content, indent=true)
|
52
|
+
return unless content.strip.length > 0
|
53
|
+
document << "<p class='#{indent ? 'indent' : ''}'>#{content}</p>\n"
|
54
|
+
end
|
55
|
+
|
56
|
+
def start_list(document)
|
57
|
+
document << "<ul>\n"
|
58
|
+
end
|
59
|
+
|
60
|
+
def list_item(document, content)
|
61
|
+
document << "<li>#{content}</li>\n"
|
62
|
+
end
|
63
|
+
|
64
|
+
def end_list(document)
|
65
|
+
document << "</ul>\n"
|
66
|
+
end
|
67
|
+
|
68
|
+
def new_page(document)
|
69
|
+
document << "<hr />\n"
|
70
|
+
end
|
71
|
+
|
72
|
+
def highlight(document, content)
|
73
|
+
file, syntax = content.split(/,/)
|
74
|
+
analyzed = CodeRay.scan(File.read(File.join(File.dirname(__FILE__), file)), syntax.to_sym)
|
75
|
+
document << "<pre class='highlight'>#{analyzed.html}</pre>"
|
76
|
+
end
|
77
|
+
|
78
|
+
File.open("prawn-format.html", "w") do |html|
|
79
|
+
html << "<html>\n<head>\n<style type='text/css'>#{DATA.read}</style>\n</head>"
|
80
|
+
html << "<body>\n"
|
81
|
+
File.open("#{File.dirname(__FILE__)}/manual.txt") do |source|
|
82
|
+
number = 0
|
83
|
+
source.each_line do |line|
|
84
|
+
number += 1
|
85
|
+
line.chomp!
|
86
|
+
next if line.length == 0
|
87
|
+
|
88
|
+
style, content = line.match(/^(\S+)\.\s*(.*)/)[1,2]
|
89
|
+
process_style(html, style, content, number)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
html << "</body>\n</html>\n"
|
93
|
+
end
|
94
|
+
|
95
|
+
__END__
|
96
|
+
body {
|
97
|
+
margin-left: 15%;
|
98
|
+
margin-right: 15%;
|
99
|
+
margin-top: 1em;
|
100
|
+
}
|
101
|
+
|
102
|
+
pre.highlight {
|
103
|
+
background-color: #f8f8f8;
|
104
|
+
border: 1px solid silver;
|
105
|
+
font-family: Courier, monospace;
|
106
|
+
font-size: 90%;
|
107
|
+
color: #100;
|
108
|
+
line-height: 1.1em;
|
109
|
+
padding: 1em;
|
110
|
+
}
|
111
|
+
|
112
|
+
.highlight .af { color:#00C }
|
113
|
+
.highlight .an { color:#007 }
|
114
|
+
.highlight .av { color:#700 }
|
115
|
+
.highlight .aw { color:#C00 }
|
116
|
+
.highlight .bi { color:#509; font-weight:bold }
|
117
|
+
.highlight .c { color:#888 }
|
118
|
+
|
119
|
+
.highlight .ch { color:#04D }
|
120
|
+
.highlight .ch .k { color:#04D }
|
121
|
+
.highlight .ch .dl { color:#039 }
|
122
|
+
|
123
|
+
.highlight .cl { color:#B06; font-weight:bold }
|
124
|
+
.highlight .co { color:#036; font-weight:bold }
|
125
|
+
.highlight .cr { color:#0A0 }
|
126
|
+
.highlight .cv { color:#369 }
|
127
|
+
.highlight .df { color:#099; font-weight:bold }
|
128
|
+
.highlight .di { color:#088; font-weight:bold }
|
129
|
+
.highlight .dl { color:black }
|
130
|
+
.highlight .do { color:#970 }
|
131
|
+
.highlight .ds { color:#D42; font-weight:bold }
|
132
|
+
.highlight .e { color:#666; font-weight:bold }
|
133
|
+
.highlight .en { color:#800; font-weight:bold }
|
134
|
+
.highlight .er { color:#F00; background-color:#FAA }
|
135
|
+
.highlight .ex { color:#F00; font-weight:bold }
|
136
|
+
.highlight .fl { color:#60E; font-weight:bold }
|
137
|
+
.highlight .fu { color:#06B; font-weight:bold }
|
138
|
+
.highlight .gv { color:#d70; font-weight:bold }
|
139
|
+
.highlight .hx { color:#058; font-weight:bold }
|
140
|
+
.highlight .i { color:#00D; font-weight:bold }
|
141
|
+
.highlight .ic { color:#B44; font-weight:bold }
|
142
|
+
|
143
|
+
.highlight .il { background: #eee }
|
144
|
+
.highlight .il .il { background: #ddd }
|
145
|
+
.highlight .il .il .il { background: #ccc }
|
146
|
+
.highlight .il .dl { font-weight: bold ! important; color: #888 ! important }
|
147
|
+
|
148
|
+
.highlight .in { color:#B2B; font-weight:bold }
|
149
|
+
.highlight .iv { color:#33B }
|
150
|
+
.highlight .la { color:#970; font-weight:bold }
|
151
|
+
.highlight .lv { color:#963 }
|
152
|
+
.highlight .oc { color:#40E; font-weight:bold }
|
153
|
+
.highlight .on { color:#000; font-weight:bold }
|
154
|
+
.highlight .op { }
|
155
|
+
.highlight .pc { color:#038; font-weight:bold }
|
156
|
+
.highlight .pd { color:#369; font-weight:bold }
|
157
|
+
.highlight .pp { color:#579 }
|
158
|
+
.highlight .pt { color:#339; font-weight:bold }
|
159
|
+
.highlight .r { color:#080; font-weight:bold }
|
160
|
+
|
161
|
+
.highlight .rx { background-color:#fff0ff }
|
162
|
+
.highlight .rx .k { color:#808 }
|
163
|
+
.highlight .rx .dl { color:#404 }
|
164
|
+
.highlight .rx .mod { color:#C2C }
|
165
|
+
.highlight .rx .fu { color:#404; font-weight: bold }
|
166
|
+
|
167
|
+
.highlight .s { background-color:#fff0f0 }
|
168
|
+
.highlight .s .s { background-color:#ffe0e0 }
|
169
|
+
.highlight .s .s .s { background-color:#ffd0d0 }
|
170
|
+
.highlight .s .k { color:#D20 }
|
171
|
+
.highlight .s .dl { color:#710 }
|
172
|
+
|
173
|
+
.highlight .sh { background-color:#f0fff0 }
|
174
|
+
.highlight .sh .k { color:#2B2 }
|
175
|
+
.highlight .sh .dl { color:#161 }
|
176
|
+
|
177
|
+
.highlight .sy { color:#A60 }
|
178
|
+
.highlight .sy .k { color:#A60 }
|
179
|
+
.highlight .sy .dl { color:#630 }
|
180
|
+
|
181
|
+
.highlight .ta { color:#070 }
|
182
|
+
.highlight .tf { color:#070; font-weight:bold }
|
183
|
+
.highlight .ts { color:#D70; font-weight:bold }
|
184
|
+
.highlight .ty { color:#339; font-weight:bold }
|
185
|
+
.highlight .v { color:#036 }
|
186
|
+
.highlight .xt { color:#444 }
|
187
|
+
|
@@ -0,0 +1,10 @@
|
|
1
|
+
Prawn::Document.generate('basics.pdf') do
|
2
|
+
tags :h1 => { :font_size => "3em", :font_weight => :bold },
|
3
|
+
:h2 => { :font_size => "2em", :font_style => :italic },
|
4
|
+
:product => { :color => "red",
|
5
|
+
:text_decoration => :underline }
|
6
|
+
|
7
|
+
text "<h1>Manual</h1>"
|
8
|
+
text "<h2>Learning to do stuff</h2>"
|
9
|
+
text "<product>Prawn</product> is a PDF generator for Ruby"
|
10
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# force unformatted display
|
2
|
+
text "Show <b>this</b> verbatim", :plain => true
|
3
|
+
|
4
|
+
# force formatted display for full justification
|
5
|
+
text "I want this to be fully justified", :plain => false,
|
6
|
+
:align => :justify
|
7
|
+
|
8
|
+
# assume all text is blue
|
9
|
+
text "Blue text", :plain => false,
|
10
|
+
:default_style => { :color => "blue" }
|
11
|
+
|
12
|
+
# one-off tag and style definitions
|
13
|
+
text "<nifty class='orange'>Nifty!</nifty>",
|
14
|
+
:tags => { :nifty => { :font_weight => :bold } },
|
15
|
+
:styles => { :orange => { :color => "#fa0" } }
|
data/manual/manual.txt
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
h1. Prawn::Format %FORMAT:VERSION%
|
2
|
+
p. %PDF{}HTML{<em>A PDF version of this document may be downloaded <a href="http://jamisbuck.org/files/prawn-format-%FORMAT:VERSION%.pdf">here</a>.</em>}END%
|
3
|
+
fp. Prawn::Format is an extension library for the Prawn PDF generation library for Ruby, allowing you to easily embed formatting <em>tags</em> in your documents. It features:
|
4
|
+
ul.
|
5
|
+
li. Out-of-the-box support for many common HTML tags
|
6
|
+
li. Intra-document hyperlinks
|
7
|
+
li. Easily add your own tag definitions
|
8
|
+
li. Style classes
|
9
|
+
/ul.
|
10
|
+
p. Sound good? It's not all roses, though. Prawn::Format currently has the following known warts:
|
11
|
+
ul.
|
12
|
+
li. No support for block-level tags (paragraphs, divs, tables, etc.)
|
13
|
+
li. No support for inline images
|
14
|
+
/ul.
|
15
|
+
p. If that doesn't deter you, read on! This manual currently has information about the following topics:
|
16
|
+
ul.
|
17
|
+
li. <a href="#basic-usage">Basic usage</a>
|
18
|
+
li. <a href="#custom-tags">Custom tags</a>
|
19
|
+
li. <a href="#style-classes">Style classes</a>
|
20
|
+
li. <a href="#text-options"><code>text()</code> options</a>
|
21
|
+
/ul.
|
22
|
+
p. Prawn::Format was written by Jamis Buck (jamis@jamisbuck.org). Although there are not currently plans to extend the functionality much beyond the current incarnation, Jamis is definitely amenable to patches. What this means is that if you've got a pet feature you'd like to see in Prawn::Format, your best bet is to get to work and implement it!
|
23
|
+
p. Bug reports are welcome, and may be submitted at:
|
24
|
+
center. <b>http://prawn.lighthouseapp.com/projects/23476</b>
|
25
|
+
p. And if you're inclined to help out with patches and the like, the code is hosted at (where else?) GitHub:
|
26
|
+
center. <b>http://github.com/jamis/prawn-format</b>
|
27
|
+
p. Thanks!
|
28
|
+
hr.
|
29
|
+
fp. <about>This manual generated for Prawn::Format version %FORMAT:VERSION% on %NOW%.</about>
|
30
|
+
|
31
|
+
page.
|
32
|
+
h2. <a name="basic-usage"></a>Basic usage
|
33
|
+
fp. For most uses, adding text to your documents is exactly the same as it is in Prawn. The only difference is that you now add formatting tags (HTML-ish in nature) to the strings:
|
34
|
+
highlight. include/basics.rb,ruby
|
35
|
+
p. Prawn::Format does not support even the majority of HTML tags by default, but it does include support for the most common ones (and you can easily <a href="#custom-tags">add others</a> if you so desire). The supported tags are:
|
36
|
+
ul.
|
37
|
+
li. <code>a</code> — for hyperlinks (the <code>name</code> attribute is for hyperlink anchors, and the <code>href</code> attribute is for the actual links, just like in HTML)
|
38
|
+
li. <code>b</code> — bold-faced text (but only if the currently selected font family supports bold text)
|
39
|
+
li. <code>br</code> — line break
|
40
|
+
li. <code>code</code> — switch to monospaced font (Courier, by default)
|
41
|
+
li. <code>em</code> — italicized text (but only if the currently selected font family supports italicized text)
|
42
|
+
li. <code>font</code> — change font attributes, include <code>face</code> (to change font family), <code>color</code>, and <code>size</code>
|
43
|
+
li. <code>i</code> — italicized text (see <code>em</code>)
|
44
|
+
li. <code>pre</code> — "verbatim" text (preserving whitespace and newlines). Unlike the HTML <code>pre</code> tag, this is not a block tag
|
45
|
+
li. <code>span</code> — has no formatting, by default, but is recognized so that you can apply <a href="#style-classes">style classes</a> to the tag
|
46
|
+
li. <code>strong</code> — bold-faced text (see <code>b</code>)
|
47
|
+
li. <code>sub</code> — subscript text
|
48
|
+
li. <code>sup</code> — superscript text
|
49
|
+
li. <code>tt</code> — monospaced font (see <code>code</code>)
|
50
|
+
li. <code>u</code> — underlined text
|
51
|
+
/ul.
|
52
|
+
p. You've likely noticed, already, the conspicuous absence of such common HTML tags as <code>p</code>, <code>div</code>, and <code>table</code>. The reason these are missing is simple: Prawn::Format has no real box model. Turns out, implementing such a beast is a really, really complicated thing to do right. However, you can work around this limitation without too much effort. In fact, <em>%PDF{this entire manual is formatted using Prawn::Format!}HTML{the <a href="http://jamisbuck.org/files/prawn-format-%FORMAT:VERSION%.pdf">PDF version of this manual</a> was entirely formatted using Prawn::Format!}END%</em> (The source code for the manual is included in the Prawn::Format distribution; see <code>manual/pdf.rb</code> and <code>manual/html.rb</code>. Also, <code>examples/document.rb</code> has a simpler demonstration of wholesale document formatting.)
|
53
|
+
p. One particularly important thing to note about Prawn::Format tags: these are XML tags, and not HTML tags! The distinction is moot in most cases, but what this means in practical terms is that you <em>cannot</em> have unclosed tags in Prawn::Format. Even tags like <code><br></code> (which HTML allows to exist unclosed) must be closed. However, Prawn::Format allows you to use the abbreviated "open/close" XML syntax, e.g., <code><br/></code>.
|
54
|
+
p. Lastly, Prawn::Format will ignore whitespace, just like HTML. (Unless you're operating in verbatim mode, usually introduced via the <code><pre></code> tag.) This means that newlines in your text will <em>not</em> translate to newlines in your output; you need to use the line-break tag <code><br/></code> to put line-breaks in your output.
|
55
|
+
highlight. include/breaks.rb,ruby
|
56
|
+
p. Got all that? Great! Time to move on to more tricky stuff. Next up: <a href="#custom-tags">custom tags</a>!
|
57
|
+
|
58
|
+
page.
|
59
|
+
h2. <a name="custom-tags"></a>Custom tags
|
60
|
+
fp. Prawn::Format makes it very easy to define your own tags, either for clearer semantic markup or to add support for HTML tags that are not supported out-of-the-box.
|
61
|
+
highlight. include/custom-tags.rb,ruby
|
62
|
+
p. If you want to tweak the definition of an existing tag, you can reach right into the tags hash directly and add or modify existing styles:
|
63
|
+
highlight. include/custom-tags2.rb,ruby
|
64
|
+
p. The supported style options are:
|
65
|
+
ul.
|
66
|
+
li. <code>font_size</code>
|
67
|
+
li. <code>font_family</code> (this may be any font name that Prawn's <code>Document#font</code> method will support)
|
68
|
+
li. <code>font_style</code> — either <code>:normal</code> or <code>:italic</code>
|
69
|
+
li. <code>font_weight</code> — either <code>:normal</code> or <code>:bold</code>
|
70
|
+
li. <code>color</code> — may be any valid HTML color definition
|
71
|
+
li. <code>vertical_align</code> — either <code>:super</code> or <code>:sub</code>, or a number to indicate the vertical offset from the baseline
|
72
|
+
li. <code>text_decoration</code> — either <code>:none</code> or <code>:underline</code>
|
73
|
+
li. <code>white_space</code> — either <code>:normal</code>, or <code>:pre</code> (for verbatim text, where whitespace is preserved)
|
74
|
+
li. <code>width</code> — a number, used primarily to fake text indents at paragraph starts. Any tag with this attribute will have a width of that value (in addition to any contents of the tag)
|
75
|
+
li. <code>kerning</code> — either true or false, to indicate whether kerning should be done on the text
|
76
|
+
/ul.
|
77
|
+
p. As you can see, for the most part these follow the same naming and value schemes as CSS3, though of course the similarity is only superficial. Prawn::Format doesn't support cascading styles, or specifying <a href="#style-classes">style classes</a> by anything other than a bare class name. (Maybe someday this will change. If it does, it'll be thanks to some enterprising programmer who submits a patch.)
|
78
|
+
p. Still, even with the limitations, you can do quite a bit. For example, suppose you want indent the first line of a paragraph in your own document. Prawn::Format doesn't support this out of the box, but it's easy to do; just define an <code><indent></code> tag and give it a width the size of the text indent; then prepend that tag to every paragraph:
|
79
|
+
highlight. include/indent.rb,ruby
|
80
|
+
p. Oh, and did you catch that? Just like in CSS, you can specify most measurements in ems (e.g. <code>"2em"</code>), picas (<code>"3pc"</code>), inches (<code>"0.5in"</code>), or PDF "points" (<code>14</code>). There are 72 PDF points to an inch, and 6 picas to an inch. Also, like CSS, you can specify relative measurements, using percentages, in which case it is relative to some appropriate value. (Setting width to <code>"25%"</code> will set it to 25% of the width of the line. Setting the font size to <code>"80%"</code> will set it to 80% of the current font size.)
|
81
|
+
|
82
|
+
page.
|
83
|
+
h2. <a name="style-classes"></a>Style classes
|
84
|
+
fp. Adding a new tag for every different style in your document can get to be pretty cumbersome. For that reason, you can also define <em>style classes</em>. This is simply a style definition (just like you would use for <a href="#custom-tags">defining a new tag</a>). To apply the style to a tag, do just like you would in HTML: specify the style class in the <code>class</code> attribute of the tag:
|
85
|
+
highlight. include/style-classes.rb,ruby
|
86
|
+
p. As mentioned before, Prawn::Format doesn't support cascading styles (i.e. you can't specify the format of an element by the chain of elements and styles in it's ancestor elements). And sadly, in practice, this does limit the ease with which you can apply complex styles. However, with judicious use of both style classes and <a href="#custom-tags">custom tags</a> you can do quite a bit.
|
87
|
+
|
88
|
+
page.
|
89
|
+
h2. <a name="text-options"></a><code>text()</code> options
|
90
|
+
fp. Prawn::Format overloads the existing functionality of Prawn's <code>Document#text</code> method. In fact, unless formatting tags or XML entities are detected in the text, Prawn::Format's <code>text</code> method will simply fall back to the original. (Why? Because if you don't need the formatting, it's <em>much</em> faster to skip all the extra work that Prawn::Format does.)
|
91
|
+
p. What this means is that Prawn::Format tries very hard to support all of the options that Prawn's <code>text</code> method accepts.
|
92
|
+
p. Prawn::Format does <em>add</em> several new options to <code>text</code>, though. They are as follows:
|
93
|
+
ul.
|
94
|
+
li. <code>:plain</code> — a boolean indicating whether the text is "plain" or not. Plain text is processed by the original <code>text</code> method. By default, Prawn::Format analyzes the text to determine plainness, but this option lets you force it one way or the other.
|
95
|
+
li. <code>:align => :justify</code> — the original <code>text</code> method understood several text alignment methods, but not full-justification. Prawn::Format will understand all the original methods, and also <code>:justify</code>, for full justification.
|
96
|
+
li. <code>:tags</code> — a hash of tag definitions to use for this one call. Prawn::Format will always use the tags defined via <code>Document#tags</code>, but anything you specify via the <code>:tags</code> option takes precedent.
|
97
|
+
li. <code>:styles</code> — a hash of style class definitions to use for this one call. Prawn::Format will always use the style classes defined via <code>Document#styles</code>, but anything you specify via the <code>:styles</code> option takes precedent.
|
98
|
+
li. <code>:default_style</code> — a hash of style definitions that defines the default style to use for any otherwise-unstyled text. This defaults to the current font and color settings on the document, but you can set something else via this option.
|
99
|
+
/ul.
|
100
|
+
p. Here are a few examples using these options:
|
101
|
+
highlight. include/options.rb,ruby
|