Module: RFPDF
- Defined in:
- vendor/plugins/rfpdf/lib/rfpdf/view.rb,
vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb,
vendor/plugins/rfpdf/lib/rfpdf/errors.rb
Overview
Copyright © 2006 4ssoM LLC <www.4ssoM.com>
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Thanks go out to Bruce Williams of codefluency who created RTex. This template handler is modification of his work.
Example Registration
ActionView::Base::register_template_handler 'rfpdf', RFpdfView
Defined Under Namespace
Classes: GenerationError, View
Constant Summary
- COLOR_PALETTE =
{ :black => [0x00, 0x00, 0x00], :white => [0xff, 0xff, 0xff], }.freeze
Instance Method Summary
-
- (Object) close_tag(tag, options = {})
:nodoc:.
-
- (Object) draw_box(x, y, w, h, options = {})
Draw a box at (x, y), w wide and h high.
-
- (Object) draw_line(x1, y1, x2, y2, options = {})
Draw a line from (x1, y1) to (x2, y2).
-
- (Object) draw_text(x, y, text, options = {})
Draw a string of text at (x, y).
-
- (Object) draw_text_block(x, y, text, left_margin, right_margin, options = {})
Draw a block of text at (x, y) bounded by left_margin and right_margin.
-
- (Object) draw_text_box(x, y, w, h, text, options = {})
Draw a string of text at (x, y) in a box w wide and h high.
-
- (Object) draw_title(x, y, title, options = {})
Draw a string of text at (x, y) as a title.
-
- (Object) open_tag(tag, options = {})
:nodoc:.
-
- (Object) put_link(url, txt)
:nodoc:.
-
- (Object) set_draw_color(color = COLOR_PALETTE[:black])
Set the draw color.
-
- (Object) set_fill_color(color = COLOR_PALETTE[:white])
Set the fill color.
-
- (Object) set_style(tag, enable = true)
:nodoc:.
-
- (Object) set_text_color(color = COLOR_PALETTE[:black])
Set the text color.
-
- (Object) write_html(html, options = {})
Write a string containing html characters.
Instance Method Details
- (Object) close_tag(tag, options = {})
:nodoc:
271 272 273 274 275 276 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 271 def close_tag(tag, = {}) #:nodoc: #Closing tag tag = tag.to_s.upcase set_style(tag, false) if tag == 'B' or tag == 'I' or tag == 'U' @href = '' if $tag == 'A' end |
- (Object) draw_box(x, y, w, h, options = {})
Draw a box at (x, y), w wide and h high.
Options are:
- :border - Draw a border, 0 = no, 1 = yes? Default value is 1.
- :border_color - Default value is COLOR_PALETTE[:black].
- :border_width - Default value is 0.5.
- :fill - Fill the box, 0 = no, 1 = yes? Default value is 1.
- :fill_color - Default value is nothing or COLOR_PALETTE[:white].
Example:
draw_box(x, y - 1, 38, 22)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 87 def draw_box(x, y, w, h, = {}) [:border] ||= 1 [:border_color] ||= COLOR_PALETTE[:black] [:border_width] ||= 0.5 [:fill] ||= 1 [:fill_color] ||= COLOR_PALETTE[:white] SetLineWidth([:border_width]) set_draw_color([:border_color]) set_fill_color([:fill_color]) fd = "" fd = "D" if [:border] == 1 fd += "F" if [:fill] == 1 Rect(x, y, w, h, fd) end |
- (Object) draw_line(x1, y1, x2, y2, options = {})
Draw a line from (x1, y1) to (x2, y2).
Options are:
- :line_color - Default value is COLOR_PALETTE[:black].
- :line_width - Default value is 0.5.
Example:
draw_line(x1, y1, x1, y1+h, :line_color => ReportHelper::COLOR_PALETTE[:dark_blue], :line_width => 1)
17 18 19 20 21 22 23 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 17 def draw_line(x1, y1, x2, y2, = {}) [:line_color] ||= COLOR_PALETTE[:black] [:line_width] ||= 0.5 set_draw_color([:line_color]) SetLineWidth([:line_width]) Line(x1, y1, x2, y2) end |
- (Object) draw_text(x, y, text, options = {})
Draw a string of text at (x, y).
Options are:
- :font_color - Default value is COLOR_PALETTE[:black].
- :font_size - Default value is 10.
- :font_style - Default value is nothing or ’’.
Example:
draw_text(x, y, header_left, :font_size => 10)
36 37 38 39 40 41 42 43 44 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 36 def draw_text(x, y, text, = {}) [:font_color] ||= COLOR_PALETTE[:black] [:font_size] ||= 10 [:font_style] ||= '' set_text_color([:font_color]) SetFont('Arial', [:font_style], [:font_size]) SetXY(x, y) Write([:font_size] + 4, text) end |
- (Object) draw_text_block(x, y, text, left_margin, right_margin, options = {})
Draw a block of text at (x, y) bounded by left_margin and right_margin. Both margins are measured from their corresponding edge.
Options are:
- :font_color - Default value is COLOR_PALETTE[:black].
- :font_size - Default value is 10.
- :font_style - Default value is nothing or ’’.
Example:
draw_text_block(left_margin, 85, "question", left_margin, 280, :font_color => ReportHelper::COLOR_PALETTE[:dark_blue], :font_size => 12, :font_style => 'I')
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 61 def draw_text_block(x, y, text, left_margin, right_margin, = {}) [:font_color] ||= COLOR_PALETTE[:black] [:font_size] ||= 10 [:font_style] ||= '' set_text_color([:font_color]) SetFont('Arial', [:font_style], [:font_size]) SetXY(x, y) SetLeftMargin(left_margin) SetRightMargin(right_margin) Write([:font_size] + 4, text) SetMargins(0,0,0) end |
- (Object) draw_text_box(x, y, w, h, text, options = {})
Draw a string of text at (x, y) in a box w wide and h high.
Options are:
- :align - Vertical alignment ‘C’ = center, ‘L’ = left, ‘R’ = right. Default value is ‘C’.
- :border - Draw a border, 0 = no, 1 = yes? Default value is 0.
- :border_color - Default value is COLOR_PALETTE[:black].
- :border_width - Default value is 0.5.
- :fill - Fill the box, 0 = no, 1 = yes? Default value is 1.
- :fill_color - Default value is nothing or COLOR_PALETTE[:white].
- :font_color - Default value is COLOR_PALETTE[:black].
- :font_size - Default value is nothing or 8.
- :font_style - ‘B’ = bold, ‘I’ = italic, ‘U’ = underline. Default value is nothing ’’.
- :padding - Default value is nothing or 2.
- :valign - ‘M’ = middle, ‘T’ = top, ‘B’ = bottom. Default value is nothing or ‘M’.
Example:
draw_text_box(x, y - 1, 38, 22, "your_score_title", :fill => 0, :font_color => ReportHelper::COLOR_PALETTE[:blue], :font_line_spacing => 0, :font_style => "B", :valign => "M")
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 127 def draw_text_box(x, y, w, h, text, = {}) [:align] ||= 'C' [:border] ||= 0 [:border_color] ||= COLOR_PALETTE[:black] [:border_width] ||= 0.5 [:fill] ||= 1 [:fill_color] ||= COLOR_PALETTE[:white] [:font_color] ||= COLOR_PALETTE[:black] [:font_size] ||= 8 [:font_line_spacing] ||= [:font_size] * 0.3 [:font_style] ||= '' [:padding] ||= 2 [:valign] ||= "M" if [:fill] == 1 or [:border] == 1 draw_box(x, y, w, h, ) end SetMargins(0,0,0) set_text_color([:font_color]) font_size = [:font_size] SetFont('Arial', [:font_style], font_size) font_size += [:font_line_spacing] case [:valign] when "B" y -= [:padding] text = "\n" + text if text["\n"].nil? when "T" y += [:padding] end SetXY(x, y) if GetStringWidth(text) > w or not text["\n"].nil? or [:valign] == "T" font_size += [:font_size] * 0.1 #TODO 2006-07-21 Level=1 - this is assuming a 2 line text SetXY(x, y + ((h - (font_size * 2)) / 2)) if [:valign] == "M" MultiCell(w, font_size, text, 0, [:align]) else Cell(w, h, text, 0, 0, [:align]) end end |
- (Object) draw_title(x, y, title, options = {})
Draw a string of text at (x, y) as a title.
Options are:
- :font_color - Default value is COLOR_PALETTE[:black].
- :font_size - Default value is 18.
- :font_style - Default value is nothing or ’’.
Example:
draw_title(left_margin, 60, "title:", :font_color => ReportHelper::COLOR_PALETTE[:dark_blue])
179 180 181 182 183 184 185 186 187 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 179 def draw_title(x, y, title, = {}) [:font_color] ||= COLOR_PALETTE[:black] [:font_size] ||= 18 [:font_style] ||= '' set_text_color([:font_color]) SetFont('Arial', [:font_style], [:font_size]) SetXY(x, y) Write([:font_size] + 2, title) end |
- (Object) open_tag(tag, options = {})
:nodoc:
263 264 265 266 267 268 269 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 263 def open_tag(tag, = {}) #:nodoc: #Opening tag tag = tag.to_s.upcase set_style(tag, true) if tag == 'B' or tag == 'I' or tag == 'U' @href = ['HREF'] if tag == 'A' Ln([:height]) if tag == 'BR' end |
- (Object) put_link(url, txt)
:nodoc:
288 289 290 291 292 293 294 295 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 288 def put_link(url, txt) #:nodoc: #Put a hyperlink SetTextColor(0,0,255) set_style('U',true) Write(5, txt, url) set_style('U',false) SetTextColor(0) end |
- (Object) set_draw_color(color = COLOR_PALETTE[:black])
Set the draw color. Default value is COLOR_PALETTE[:black].
Example:
set_draw_color(ReportHelper::COLOR_PALETTE[:dark_blue])
195 196 197 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 195 def set_draw_color(color = COLOR_PALETTE[:black]) SetDrawColor(color[0], color[1], color[2]) end |
- (Object) set_fill_color(color = COLOR_PALETTE[:white])
Set the fill color. Default value is COLOR_PALETTE[:white].
Example:
set_fill_color(ReportHelper::COLOR_PALETTE[:dark_blue])
205 206 207 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 205 def set_fill_color(color = COLOR_PALETTE[:white]) SetFillColor(color[0], color[1], color[2]) end |
- (Object) set_style(tag, enable = true)
:nodoc:
278 279 280 281 282 283 284 285 286 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 278 def set_style(tag, enable = true) #:nodoc: #Modify style and select corresponding font style = "" @style[tag] = enable ['B','I','U'].each do |s| style += s if not @style[s].nil? and @style[s] end SetFont('', style) end |
- (Object) set_text_color(color = COLOR_PALETTE[:black])
Set the text color. Default value is COLOR_PALETTE[:white].
Example:
set_text_color(ReportHelper::COLOR_PALETTE[:dark_blue])
215 216 217 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 215 def set_text_color(color = COLOR_PALETTE[:black]) SetTextColor(color[0], color[1], color[2]) end |
- (Object) write_html(html, options = {})
Write a string containing html characters. Default value is COLOR_PALETTE[:white].
Options are:
- :height - Line height. Default value is 20.
Example:
write_html(html, :height => 12)
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'vendor/plugins/rfpdf/lib/rfpdf/rfpdf.rb', line 228 def write_html(html, = {}) [:height] ||= 20 #HTML parser @href = nil @style = {} html.gsub!("\n",' ') re = %r{ ( <!--.*?--> | < (?: [^<>"] + | " (?: \\. | [^\\"]+ ) * " ) * > ) }xm html.split(re).each do |value| if "<" == value[0,1] #Tag if (value[1, 1] == '/') close_tag(value[2..-2], ) else tag = value[1..-2] open_tag(tag, ) end else #Text if @href put_link(@href,value) else Write([:height], value) end end end end |