Class: Squib::Graphics::SavePDF

Inherits:
Object
  • Object
show all
Defined in:
lib/squib/graphics/save_pdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(deck) ⇒ SavePDF

Returns a new instance of SavePDF


7
8
9
# File 'lib/squib/graphics/save_pdf.rb', line 7

def initialize(deck)
  @deck = deck
end

Instance Method Details

#render_pdf(range, sheet) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

:nodoc:


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
# File 'lib/squib/graphics/save_pdf.rb', line 13

def render_pdf(range, sheet)
  cc = init_cc(sheet)
  cc.scale(POINTS_PER_IN / @deck.dpi, POINTS_PER_IN / @deck.dpi) # for bug #62
  card_width   = @deck.width  - 2 * sheet.trim
  card_height  = @deck.height - 2 * sheet.trim
  start_x_pos = sheet.rtl ? sheet.width - sheet.margin - card_width - 2 * sheet.trim : sheet.margin
  x_increment = (card_width + sheet.gap) * (sheet.rtl ? -1 : 1)
  y = sheet.margin
  x = start_x_pos
  track_progress(range, sheet) do |bar|
    range.each do |i|
      card = @deck.cards[i]
      cc.translate(x, y)
      cc.rectangle(sheet.trim, sheet.trim, card_width, card_height)
      cc.clip
      case card.backend.downcase.to_sym
      when :memory
        cc.set_source(card.cairo_surface, 0, 0)
        cc.paint
      when :svg
        card.cairo_surface.finish
        cc.save
        cc.scale(0.8, 0.8) # I really don't know why I needed to do this at all. But 0.8 is the magic number to get this to scale right
        cc.render_rsvg_handle(Rsvg::Handle.new_from_file(card.svgfile))
        cc.restore
      else
        abort "No such back end supported for save_pdf: #{backend}"
      end
      bar.increment
      cc.reset_clip
      cc.translate(-x, -y)

      draw_crop_marks(cc, x, y, sheet)
      x += x_increment
      if (x > (sheet.width - card_width - sheet.margin)) or (x < sheet.margin)
        x = start_x_pos
        y += card.height + sheet.gap - 2 * sheet.trim
        if y > (sheet.height - card_height - sheet.margin)
          cc.show_page # next page
          y = sheet.margin
          x = start_x_pos
        end
      end
    end
  end
  cc.target.finish
end