A formatting language to create a quick reference sheet. This is a multi-column page in landscape mode that generally has three or four columns. This format may also be used for brochures, but brochure creation requires a bit of management to create properly.
A three-column reference sheet is generally in the form of:
Page 1:
column 1 | column 2 | column 3
Page 2:
column 4 | column 5 | column 6
The formatting language provided in QuickRef is based around this text flow. The title of the quick reference sheet is in column 1. The two pages are intended to be printed on both sides of pieces of paper so that columns 1 and 6 are matched. This will use a Z-fold that places columns 5 and 6 face to face and columns 2 and 3 face to face. In the folded reference sheet, columns 1 and 4 will be facing out.
In contrast, brochures differ vastly in their design, although the common brochure is also three columns and either follows the same layout as a reference sheet or uses an overlapping fold.
When an overlapping fold is used, the title is typically on column 6 (assuming a left-to-right reading order). A short summary will appear on column 4. Contact information about the maker of the brochure is typically in column 5. Columns 1, 2, and 3 will contain the main body of the brochure. The brochure will be folded so that columns 2 and 3 are face to face. After this, column 1 will face column 4 (exposed by the first fold). In the folded brochure, columns 5 and 6 are facing out.
qr = PDF::QuickRef.new # 3-column LETTER qr.title "My QuickRef" qr.h1 "H1 Text" qr.lines "Text to put after the header." qr.save_as "MyQuickRef.pdf"
The name of the font that will be used for title text. The default font is Times-Roman.
Creates a QuickRef document and then calls instance_eval on the document. This allows for a more natural use of the QuickRef class as a DSL for creating these documents.
PDF::QuickRef.make do # 3-column LETTER title "My QuickRef" h1 "H1 Text" lines "Text to put after the header." save_as "MyQuickRef.pdf" end
# File lib/pdf/quickref.rb, line 328 def self.make(*args, &block) qr = PDF::QuickRef.new(*args) qr.__send__(:instance_eval, &block) end
Create the quick reference document. paper is passed unchanged to the PDF::Writer.new; the page is always created landscape. Margins are initialized to 18 points. After some additional initialization is performed, the quick reference document is yielded to an optional block for further configuration. All of this is done before the columns are started.
After the columns are started, lines will be drawn between column positions.
# File lib/pdf/quickref.rb, line 64 def initialize(paper = "LETTER", columns = 3, column_separators_visible = true) @pdf = PDF::Writer.new(:paper => paper, :orientation => :landscape) @pdf.margins_pt 18 @pdf.y = @pdf.absolute_top_margin @title_font = "Times-Roman" @heading_font = "Times-Roman" @body_font = "Times-Roman" @code_font = "Courier" @title_font_size = 14 @h1_font_size = 11 @h2_font_size = 9 @h3_font_size = 8 @h4_font_size = 7 @body_font_size = 6 @ptab = PDF::SimpleTable.new do |tab| tab.column_order.replace %(one two) tab.font_size = @body_font_size tab.show_lines = :none tab.show_headings = false tab.orientation = :center tab.position = :center end @ltab = PDF::SimpleTable.new do |tab| tab.column_order.replace %(line) tab.font_size = @body_font_size tab.show_lines = :none tab.show_headings = false tab.orientation = :center tab.position = :center end yield self if block_given? @pdf.start_columns columns @ptab.font_size = @body_font_size @ltab.font_size = @body_font_size @ptab.maximum_width = @pdf.column_width - 10 @ltab.maximum_width = @pdf.column_width - 10 if column_separators_visible # Put lines between the columns. all = @pdf.open_object @pdf.save_state @pdf.stroke_color! Color::RGB::Black @pdf.stroke_style PDF::Writer::StrokeStyle::DEFAULT (1 .. (columns - 1)).each do |ii| x = @pdf.left_margin + (@pdf.column_width * ii) x += (@pdf.column_gutter * (ii - 0.5)) @pdf.line(x, @pdf.page_height - @pdf.top_margin, x, @pdf.bottom_margin) @pdf.stroke end @pdf.restore_state @pdf.close_object @pdf.add_object(all, :all_pages) end end
Writes body text. Paragraphs will be reflowed for optimal placement of text. Text separated by two line separators (e.g., nn, although the separator is platform dependent). The text will be placed with full justification.
# File lib/pdf/quickref.rb, line 275 def body(text) # Transform the text a little. paras = text.split(%(#{$/}{2})) paras.map! { |para| para.split($/).join(" ").squeeze(" ") } text = paras.join("\n\n") @pdf.text "#{text}\n", :font_size => @body_font_size, :justification => :full end
Creates a one-column zebra-striped table using the code font. Each line of the text is a separate row.
# File lib/pdf/quickref.rb, line 202 def codelines(text) data = text.split($/).map { |line| { "line" => line } } @ltab.data.replace data use_code_font @ltab.render_on(@pdf) use_body_font @pdf.text "\n", :font_size => @body_font_size end
Creates a two-column zebra-striped table using the code font. Each line of the text is a separate row; the two columns are separated by tab characters.
# File lib/pdf/quickref.rb, line 181 def codepairs(text) data = text.split($/).map do |line| one, two = line.split(/\t/) { 'one' => one, 'two' => two } end @ptab.data.replace data use_code_font @ptab.render_on(@pdf) use_body_font @pdf.text "\n", :font_size => @body_font_size end
Writes the text with the heading_font and h1_font_size left justified in the column. The font is set to body_font after the heading is drawn.
# File lib/pdf/quickref.rb, line 242 def h1(text) use_heading_font @pdf.text text, :font_size => @h1_font_size use_body_font end
Writes the text with the heading_font and h2_font_size left justified in the column. The font is set to body_font after the heading is drawn.
# File lib/pdf/quickref.rb, line 250 def h2(text) use_heading_font @pdf.text "<i>#{text}</i>", :font_size => @h2_font_size use_body_font end
Writes the text with the heading_font and h3_font_size left justified in the column. The font is set to body_font after the heading is drawn.
# File lib/pdf/quickref.rb, line 258 def h3(text) use_heading_font @pdf.text "<i>#{text}</i>", :font_size => @h3_font_size use_body_font end
Writes the text with the heading_font and h4_font_size left justified in the column. The font is set to body_font after the heading is drawn.
# File lib/pdf/quickref.rb, line 266 def h4(text) use_heading_font @pdf.text "<b><i>#{text}</i></b>", :font_size => @h4_font_size use_body_font end
Draws a horizontal line with the specified style and colour.
# File lib/pdf/quickref.rb, line 291 def hline(style = PDF::Writer::StrokeStyle::DEFAULT, color = Color::RGB::Black) @pdf.y -= 2.5 @pdf.save_state @pdf.stroke_style style @pdf.stroke_color! color x0 = @pdf.left_margin x1 = @pdf.left_margin + pdf.column_width @pdf.line(x0, @pdf.y, x1, @pdf.y) @pdf.stroke @pdf.restore_state @pdf.y -= 2.5 end
Creates a one-column zebra-striped table using the body font. Each line of the text is a separate row.
# File lib/pdf/quickref.rb, line 194 def lines(text) data = text.split($/).map { |line| { "line" => line } } @ltab.data.replace data @ltab.render_on(@pdf) @pdf.text "\n", :font_size => @body_font_size end
Creates a two-column zebra-striped table using the body font. Each line of the text is a separate row; the two columns are separated by tab characters.
# File lib/pdf/quickref.rb, line 169 def pairs(text) data = text.split($/).map do |line| one, two = line.split(/\t/) { 'one' => one, 'two' => two } end @ptab.data.replace data @ptab.render_on(@pdf) @pdf.text "\n", :font_size => @body_font_size end
Writes code text. Newlines and spaces will be preserved.
# File lib/pdf/quickref.rb, line 284 def pre(text) use_code_font @pdf.text "#{text}\n", :font_size => @body_font_size use_body_font end
Generates the PDF document as a string.
# File lib/pdf/quickref.rb, line 311 def render @pdf.render end
Writes the Quick Reference to disk.
# File lib/pdf/quickref.rb, line 306 def save_as(filename) @pdf.save_as(filename) end
Writes the text with the title_font and title_font_size centered in the column. After the title has been written, an hline will be drawn under the title. The font is set to body_font after the title is drawn.
# File lib/pdf/quickref.rb, line 233 def title(text) use_title_font @pdf.text text, :font_size => @title_font_size, :justification => :center use_body_font hline end
Change the current font to the body font.
# File lib/pdf/quickref.rb, line 221 def use_body_font @pdf.select_font @body_font, @body_font_encoding end
Change the current font to the code font.
# File lib/pdf/quickref.rb, line 225 def use_code_font @pdf.select_font @code_font, @code_font_encoding end
Change the current font to the title font.
# File lib/pdf/quickref.rb, line 212 def use_title_font @pdf.select_font @title_font, @title_font_encoding end
Generated with the Darkfish Rdoc Generator 2.