Class: Barby::PrawnOutputter

Inherits:
Outputter show all
Defined in:
lib/barby/outputter/prawn_outputter.rb

Instance Attribute Summary collapse

Attributes inherited from Outputter

#barcode

Instance Method Summary collapse

Methods inherited from Outputter

#boolean_groups, #booleans, #encoding, #initialize, register, #two_dimensional?

Constructor Details

This class inherits a constructor from Barby::Outputter

Instance Attribute Details

#colorObject



115
116
117
# File 'lib/barby/outputter/prawn_outputter.rb', line 115

def color
  @color
end

#heightObject



73
74
75
# File 'lib/barby/outputter/prawn_outputter.rb', line 73

def height
  two_dimensional? ? (ydim * encoding.length) : (@height || 50)
end

#marginObject

Margin is used for x and y if not given explicitly, effectively placing the barcode <margin> points from the [left,bottom] of the page. If you define x and y, there will be no margin. And if you don’t define margin, it’s 0.



88
89
90
# File 'lib/barby/outputter/prawn_outputter.rb', line 88

def margin
  @margin || 0
end

#unbleedObject

Defines an amount to reduce black bars/squares by to account for “ink bleed” If xdim = 3, unbleed = 0.2, a single/width black bar will be 2.6 wide For 2D, both x and y dimensions are reduced.



111
112
113
# File 'lib/barby/outputter/prawn_outputter.rb', line 111

def unbleed
  @unbleed || 0
end

#xObject



92
93
94
# File 'lib/barby/outputter/prawn_outputter.rb', line 92

def x
  @x || margin
end

#xdimObject



100
101
102
# File 'lib/barby/outputter/prawn_outputter.rb', line 100

def xdim
  @xdim || 1
end

#yObject



96
97
98
# File 'lib/barby/outputter/prawn_outputter.rb', line 96

def y
  @y || margin
end

#ydimObject



104
105
106
# File 'lib/barby/outputter/prawn_outputter.rb', line 104

def ydim
  @ydim || xdim
end

Instance Method Details

#annotate_pdf(pdf, opts = {}) ⇒ Object



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
60
61
62
# File 'lib/barby/outputter/prawn_outputter.rb', line 19

def annotate_pdf(pdf, opts={})
  with_options opts do
    xpos, ypos = x, y
    orig_xpos = xpos
    orig_color = pdf.fill_color

    pdf.fill_color = color if color

    if barcode.two_dimensional?
      boolean_groups.reverse_each do |groups|
        groups.each do |bar,amount|
          if bar
            pdf.move_to(xpos+unbleed, ypos+unbleed)
            pdf.line_to(xpos+unbleed, ypos+ydim-unbleed)
            pdf.line_to(xpos+(xdim*amount)-unbleed, ypos+ydim-unbleed)
            pdf.line_to(xpos+(xdim*amount)-unbleed, ypos+unbleed)
            pdf.line_to(xpos+unbleed, ypos+unbleed)
            pdf.fill
          end
          xpos += (xdim*amount)
        end
        xpos = orig_xpos
        ypos += ydim
      end
    else
      boolean_groups.each do |bar,amount|
        if bar
          pdf.move_to(xpos+unbleed, ypos)
          pdf.line_to(xpos+unbleed, ypos+height)
          pdf.line_to(xpos+(xdim*amount)-unbleed, ypos+height)
          pdf.line_to(xpos+(xdim*amount)-unbleed, ypos)
          pdf.line_to(xpos+unbleed, ypos)
          pdf.fill
        end
        xpos += (xdim*amount)
      end
    end#if

    pdf.fill_color = orig_color

  end#with_options

  pdf
end

#full_heightObject



81
82
83
# File 'lib/barby/outputter/prawn_outputter.rb', line 81

def full_height
  height + (margin * 2)
end

#full_widthObject



77
78
79
# File 'lib/barby/outputter/prawn_outputter.rb', line 77

def full_width
  width + (margin * 2)
end

#lengthObject



65
66
67
# File 'lib/barby/outputter/prawn_outputter.rb', line 65

def length
  two_dimensional? ? encoding.first.length : encoding.length
end

#to_pdf(opts = {}) ⇒ Object



12
13
14
15
16
# File 'lib/barby/outputter/prawn_outputter.rb', line 12

def to_pdf(opts={})
  doc_opts = opts.delete(:document) || {}
  doc_opts[:page_size] ||= 'A4'
  annotate_pdf(Prawn::Document.new(doc_opts), opts).render
end

#widthObject



69
70
71
# File 'lib/barby/outputter/prawn_outputter.rb', line 69

def width
  length * xdim
end