Class: Asciidoctor::Diagram::BarcodeConverter

Inherits:
Object
  • Object
show all
Includes:
DiagramConverter
Defined in:
lib/asciidoctor-diagram/barcode/converter.rb

Constant Summary collapse

BARCODE_TYPES =
[
  :bookland,
  :codabar,
  :code25,
  :code25iata,
  :code25interleaved,
  :code39,
  :code93,
  :code128,
  :code128a,
  :code128b,
  :code128c,
  :ean8,
  :ean13,
  :gs1_128,
  :qrcode,
  :upca,
]

Instance Method Summary collapse

Methods included from DiagramConverter

#native_scaling?, #wrap_source

Instance Method Details

#collect_options(source) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/asciidoctor-diagram/barcode/converter.rb', line 33

def collect_options(source)
  options = {}

  options[:xdim] = source.attr('xdim')
  options[:ydim] = source.attr('ydim')
  options[:margin] = source.attr('margin')
  options[:height] = source.attr('height')
  options[:foreground] = source.attr('foreground')
  options[:background] = source.attr('background')

  [:xdim, :ydim, :margin, :height].each { |o| options[o] = options[o].to_i if options[o] }

  options
end

#convert(source, format, options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
# File 'lib/asciidoctor-diagram/barcode/converter.rb', line 48

def convert(source, format, options)
  BarcodeDependencies::BARCODE_DEPENDENCIES.each_pair { |n, v| source.ensure_gem(n, v) }
  require 'barby'

  code = source.code
  type = source.config[:type]

  begin
    case type
      when :bookland
        require 'barby/barcode/bookland'
        barcode = Barby::Bookland.new(code)
      when :codabar
        require 'barby/barcode/codabar'
        barcode = Barby::Codabar.new(code)
      when :code25
        require 'barby/barcode/code_25'
        barcode = Barby::Code25.new(code)
      when :code25iata
        require 'barby/barcode/code_25_iata'
        barcode = Barby::Code25IATA.new(code)
      when :code25interleaved
        require 'barby/barcode/code_25_interleaved'
        barcode = Barby::Code25Interleaved.new(code)
      when :code39
        require 'barby/barcode/code_39'
        barcode = Barby::Code39.new(code)
      when :code93
        require 'barby/barcode/code_93'
        barcode = Barby::Code93.new(code)
      when :code128
        require 'barby/barcode/code_128'
        barcode = Barby::Code128.new(code)
      when :code128a
        require 'barby/barcode/code_128'
        barcode = Barby::Code128A.new(code)
      when :code128b
        require 'barby/barcode/code_128'
        barcode = Barby::Code128B.new(code)
      when :code128c
        require 'barby/barcode/code_128'
        barcode = Barby::Code128C.new(code)
      when :ean8
        require 'barby/barcode/ean_8'
        barcode = Barby::EAN8.new(code)
      when :ean13
        require 'barby/barcode/ean_13'
        barcode = Barby::EAN13.new(code)
      when :gs1_128
        require 'barby/barcode/code_128'
        gs1_code = code.gsub /\([^)]+\)/ do |control|
          case control.upcase
            when '(FNC1)'
              Barby::Code128::FNC1
            when '(FNC2)'
              Barby::Code128::FNC2
            when '(FNC3)'
              Barby::Code128::FNC3
            when '(FNC4)'
              Barby::Code128::FNC4
            when '(CODEA)'
              Barby::Code128::CODEA
            when '(CODEB)'
              Barby::Code128::CODEB
            when '(CODEC)'
              Barby::Code128::CODEC
            when '(SHIFT)'
              Barby::Code128::SHIFT
            when '(SP)'
              ' '
            else
              control
          end
        end
        gs1_code = gs1_code.gsub(/\s+/, '')
        gs1_code = gs1_code.prepend(Barby::Code128::FNC1) unless gs1_code[0] == Barby::Code128::FNC1
        barcode = Barby::Code128.new(gs1_code)
      when :qrcode
        BarcodeDependencies::QRCODE_DEPENDENCIES.each_pair { |n, v| source.ensure_gem(n, v) }
        require 'barby/barcode/qr_code'
        barcode = Barby::QrCode.new(code)
      when :upca
        require 'barby/barcode/ean_13'
        barcode = Barby::UPCA.new(code)
      else
        raise "Unsupported barcode type: #{type}"
    end
  rescue ArgumentError
    raise "Invalid #{type} data"
  end

  case format
    when :png
      BarcodeDependencies::PNG_DEPENDENCIES.each_pair { |n, v| source.ensure_gem(n, v) }
      require 'barby/outputter/png_outputter'
      require 'chunky_png/color'
      options[:foreground] = ChunkyPNG::Color(options[:foreground]) if options[:foreground]
      options[:background] = ChunkyPNG::Color(options[:background]) if options[:background]
      barcode.to_png(options)
    when :svg
      require_relative 'svg_outputter'
      options[:title] = code
      options[:foreground] = "##{options[:foreground]}" if options[:foreground] =~ /^[0-9a-f]+$/i
      options[:background] = "##{options[:background]}" if options[:background] =~ /^[0-9a-f]+$/i
      barcode.to_svg(options)
    when :txt
      require 'barby/outputter/ascii_outputter'
      barcode.to_ascii
    else
      raise "Unsupported format: #{format}"
  end
end

#supported_formatsObject



29
30
31
# File 'lib/asciidoctor-diagram/barcode/converter.rb', line 29

def supported_formats
  [:svg, :png, :txt]
end