Class: Strokes::Barcode

Inherits:
Object
  • Object
show all
Defined in:
lib/strokes/barcode.rb

Instance Method Summary collapse

Constructor Details

#initialize(symbol, content, options = '') ⇒ Barcode

Returns a new instance of Barcode.

Raises:



9
10
11
12
13
14
# File 'lib/strokes/barcode.rb', line 9

def initialize(symbol, content, options = '')
  raise IllegalSymbolError, symbol.to_s unless VALID_SYMBOLS.include?(symbol.to_sym)
  @symbol = symbol.to_sym
  @content = content
  @options = options
end

Instance Method Details

#save(filename, options = {}) ⇒ Object

filename should include the full path options:

- width: width of the resulting png

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/strokes/barcode.rb', line 19

def save(filename, options = {})
  ps = send(@symbol)
  gs_options = [
    "-dBATCH", "-dNOPAUSE", "-q", "-dTextAlphaBits=4", "-dSAFER",
    "-sOutputFile=#{filename}",
    "-sDEVICE=pnggray"
  ]
  barcode_ps = "#{File.expand_path(File.dirname(__FILE__))}/barcode.ps"
  sub = Subexec.run "gs #{gs_options.join(" ")} #{barcode_ps} -c '#{ps}'"
  raise(GhostScriptError, sub.output) unless sub.exitstatus == 0
  resize_method = (ps =~ /includetext/ ? "-scale" : "-sample")
  im_options = [
    "-trim",
    "-bordercolor white",
    "-border 10%",
    options[:width] ? "#{resize_method} #{options[:width]}" : nil
  ].compact
  sub = Subexec.run "mogrify #{im_options.join(' ')} #{filename}"
  raise(ImageMagickError, sub.output) unless sub.exitstatus == 0
  File.open(filename, 'r')
end