Class: Jan::Symbol

Inherits:
Object
  • Object
show all
Defined in:
lib/jan/symbol.rb,
lib/jan/symbol/band.rb,
lib/jan/symbol/band/bar.rb,
lib/jan/symbol/band/space.rb,
lib/jan/symbol/band_pattern.rb,
lib/jan/symbol/band_pattern/left_quiet_zone.rb,
lib/jan/symbol/band_pattern/right_quiet_zone.rb,
lib/jan/symbol/band_pattern/symbol_character.rb,
lib/jan/symbol/band_pattern/center_guard_pattern.rb,
lib/jan/symbol/band_pattern/normal_guard_pattern.rb

Defined Under Namespace

Classes: Band, BandPattern

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Symbol

Returns a new instance of Symbol.

Parameters:

  • code (String)


9
10
11
# File 'lib/jan/symbol.rb', line 9

def initialize(code)
  @code = code
end

Instance Method Details

#band_patternsArray<Jan::Symbol::BandPattern>

Returns:



14
15
16
17
18
19
20
21
22
23
24
# 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

#codepointsArray<String>

Returns:

  • (Array<String>)


27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

#svgString

EXPERIMENTAL

Returns:

  • (String)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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