class Jan::Symbol

Public Class Methods

new(code) click to toggle source

@param code [String]

# File lib/jan/symbol.rb, line 9
def initialize(code)
  @code = code
end

Public Instance Methods

band_patterns() click to toggle source

@return [Array<Jan::Symbol::BandPattern>]

# File lib/jan/symbol.rb, line 14
def band_patterns
  [
    BandPattern::LeftQuietZone.new,
    BandPattern::NormalGuardPattern.new
  ] + left_symbol_characters + [
    BandPattern::CenterGuardPattern.new,
  ] + right_symbol_characters + [
    BandPattern::NormalGuardPattern.new,
    BandPattern::RightQuietZone.new
  ]
end
codepoints() click to toggle source

@return [Array<String>]

# File lib/jan/symbol.rb, line 27
def codepoints
  case @code.length
  when 13
    additional_digit, *digits = @code.each_char.to_a
    variable_parity_encodation_sequence(additional_digit).zip(digits).map(&:join)
  when 8
    digits = @code.each_char.to_a
    parity_encodation_sequence = %w[A A A A C C C C]
    parity_encodation_sequence.zip(digits).map(&:join)
  else
    raise 'Invalid code length (code must be 13-digit or 8 digit)'
  end
end
svg() click to toggle source

EXPERIMENTAL

@return [String]

# File lib/jan/symbol.rb, line 44
def svg
  x = 0
  width = svg_width(@code)
  height = 60

  builder = Builder::XmlMarkup.new(indent: 2)
  builder.instruct!(:xml, version: '1.0', encoding: 'UTF-8')
  builder.svg(xmlns: 'http://www.w3.org/2000/svg', width: width, height: height) do |svg|
    svg.rect(x: 0, y: 0, width: width, height: 60, fill: 'white')
    band_patterns.each do |band_pattern|
      svg.g(class: band_pattern.class.name.split('::').last) do |group|
        band_pattern.bands.each do |band|
          group.rect(x: x,  y: 0, width: band.width, height: height, fill: band.color)
          x += band.width
        end
      end
    end
  end
end

Private Instance Methods

left_symbol_characters() click to toggle source

@return [Array<Jan::Symbol::BandPattern::SymbolCharacter>]

# File lib/jan/symbol.rb, line 67
def left_symbol_characters
  case @code.length
  when 13
    codepoints[0..5].map { |codepoint| BandPattern::SymbolCharacter.new(codepoint) }
  when 8
    codepoints[0..3].map { |codepoint| BandPattern::SymbolCharacter.new(codepoint) }
  else
    raise
  end
end
right_symbol_characters() click to toggle source

@return [Array<Jan::Symbol::BandPattern::SymbolCharacter>]

# File lib/jan/symbol.rb, line 79
def right_symbol_characters
  case @code.length
  when 13
    codepoints[6..11].map { |codepoint| BandPattern::SymbolCharacter.new(codepoint) }
  when 8
    codepoints[4..7].map { |codepoint| BandPattern::SymbolCharacter.new(codepoint) }
  else
    raise
  end
end
svg_width(code) click to toggle source

@param code [String] @return [Integer]

# File lib/jan/symbol.rb, line 119
def svg_width(code)
  case code.length
  when 13
    113
  when 8
    85
  else
    raise
  end
end
variable_parity_encodation_sequence(digit) click to toggle source

@param digit [String] @return [Array<String>]

# File lib/jan/symbol.rb, line 92
def variable_parity_encodation_sequence(digit)
  case digit
  when '0'
    %w[A A A A A A C C C C C C]
  when '1'
    %w[A A B A B B C C C C C C]
  when '2'
    %w[A A B B A B C C C C C C]
  when '3'
    %w[A A B B B A C C C C C C]
  when '4'
    %w[A B A A B B C C C C C C]
  when '5'
    %w[A B B A A B C C C C C C]
  when '6'
    %w[A B B B A A C C C C C C]
  when '7'
    %w[A B A B A B C C C C C C]
  when '8'
    %w[A B A B B A C C C C C C]
  when '9'
    %w[A B B A B A C C C C C C]
  end
end