Class: CodeSlide::PDFFormatter
- Inherits:
-
Object
- Object
- CodeSlide::PDFFormatter
- Defined in:
- lib/code_slide/pdf_formatter.rb
Instance Method Summary collapse
- #_box_position(pdf, width, height, gravity) ⇒ Object
- #_prepare_background(pdf, bgcolor) ⇒ Object
- #_prepare_font(pdf) ⇒ Object
- #_prepare_options(pdf, gravity) ⇒ Object
- #build_pdf(gravity: :center, background_color: nil, font_size: 16, page_width: 792, page_height: 612) ⇒ Object
-
#initialize(analyzer) ⇒ PDFFormatter
constructor
A new instance of PDFFormatter.
- #use_font(path, bold: path, italic: path, bold_italic: path) ⇒ Object
Constructor Details
#initialize(analyzer) ⇒ PDFFormatter
Returns a new instance of PDFFormatter.
5 6 7 |
# File 'lib/code_slide/pdf_formatter.rb', line 5 def initialize(analyzer) @analyzer = analyzer end |
Instance Method Details
#_box_position(pdf, width, height, gravity) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/code_slide/pdf_formatter.rb', line 71 def _box_position(pdf, width, height, gravity) left = 0 hcenter = (pdf.bounds.width - width) / 2 right = pdf.bounds.width - width top = pdf.bounds.height vcenter = pdf.bounds.height - (pdf.bounds.height - height) / 2 bottom = height case gravity when :northwest then [left, top ] when :north then [hcenter, top ] when :northeast then [right, top ] when :west then [left, vcenter] when :center then [hcenter, vcenter] when :east then [right, vcenter] when :southwest then [left, bottom ] when :south then [hcenter, bottom ] when :southeast then [right, bottom ] else raise ArguentError, "unsupported gravity #{gravity.inspect}" end end |
#_prepare_background(pdf, bgcolor) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/code_slide/pdf_formatter.rb', line 33 def _prepare_background(pdf, bgcolor) bgcolor ||= @analyzer.styles[:background] && @analyzer.styles[:background][:color] return unless bgcolor pdf.canvas do pdf.fill_color bgcolor pdf.fill_rectangle [pdf.bounds.left, pdf.bounds.top], pdf.bounds.right, pdf.bounds.top end end |
#_prepare_font(pdf) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/code_slide/pdf_formatter.rb', line 45 def _prepare_font(pdf) if @font.nil? 'Courier' else pdf.font_families.update( 'Custom' => { normal: @font, bold: @bold, italic: @italic, bold_italic: @bold_italic }) 'Custom' end end |
#_prepare_options(pdf, gravity) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/code_slide/pdf_formatter.rb', line 58 def (pdf, gravity) width = pdf.width_of('M') * @analyzer.width height = pdf.height_of('M') * @analyzer.height width += @analyzer.gutter_width { document: pdf, overflow: :overflow, at: _box_position(pdf, width, height, gravity) } end |
#build_pdf(gravity: :center, background_color: nil, font_size: 16, page_width: 792, page_height: 612) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/code_slide/pdf_formatter.rb', line 18 def build_pdf(gravity: :center, background_color: nil, font_size: 16, page_width: 792, page_height: 612) Prawn::Document.new(page_size: [page_width, page_height]).tap do |pdf| Prawn::Font::AFM.hide_m17n_warning = true _prepare_background(pdf, background_color) pdf.font _prepare_font(pdf), size: font_size = (pdf, gravity) box = Prawn::Text::Formatted::Box.new(@analyzer.elements, ) box.render end end |
#use_font(path, bold: path, italic: path, bold_italic: path) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/code_slide/pdf_formatter.rb', line 9 def use_font(path, bold: path, italic: path, bold_italic: path) @font = path @bold = bold @italic = italic @bold_italic = bold_italic self end |