Class: Dmtx::DataMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/dmtx/data_matrix.rb

Constant Summary collapse

C40 =
[230,
31, 0, 0,
32, 9, 29,
47, 1, 33,
57, 9, 44,
64, 1, 43,
90, 9, 51,
95, 1, 69,
127, 2, 96,
255, 1,  0]
TEXT =
[239,
31, 0,  0,
32, 9, 29,
47, 1, 33,
57, 9, 44,
64, 1, 43,
90, 2, 64,
95, 1, 69,
122, 9, 83,
127, 2, 96,
255, 1,  0]
X12 =
[238,
12, 8,  0,
13, 9, 13,
31, 8,  0,
32, 9, 29,
41, 8,  0,
42, 9, 41,
47, 8,  0,
57, 9, 44,
64, 8,  0,
90, 9, 51,
255, 8,  0]
FNC1 =
29
FNC1_CODEWORD =
232
DEFAULT_ENCODINGS =
i[ascii c40 txt x12 edifact base gs1].freeze
ENCODINGS =
(i[gs1] + DEFAULT_ENCODINGS).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, rect: false, encoding: nil) ⇒ DataMatrix

Returns a new instance of DataMatrix.

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
# File 'lib/dmtx/data_matrix.rb', line 54

def initialize(msg, rect: false, encoding: nil)
  @m = []
  @width = 0
  @height = 0
  raise ArgumentError, "illegal encoding #{encoding.inspect}" if encoding && !ENCODINGS.include?(encoding)
  @encoding, @encoded_message = encode_message(msg, encoding)
  raise EncodingError, "illegal payload" unless @encoded_message
  encode(@encoded_message, rect)
end

Instance Attribute Details

#encoded_messageObject (readonly)

Returns the value of attribute encoded_message.



9
10
11
# File 'lib/dmtx/data_matrix.rb', line 9

def encoded_message
  @encoded_message
end

#encodingObject (readonly)

Returns the value of attribute encoding.



9
10
11
# File 'lib/dmtx/data_matrix.rb', line 9

def encoding
  @encoding
end

#heightObject (readonly)

Returns the value of attribute height.



9
10
11
# File 'lib/dmtx/data_matrix.rb', line 9

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



9
10
11
# File 'lib/dmtx/data_matrix.rb', line 9

def width
  @width
end

Instance Method Details

#inspectObject



64
65
66
# File 'lib/dmtx/data_matrix.rb', line 64

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} #{width}x#{height}@#{encoding}>"
end

#to_iObject



68
69
70
# File 'lib/dmtx/data_matrix.rb', line 68

def to_i
  (0..(height - 1)).inject(0) { |i, y| (0..(width - 1)).inject(i) { |j, x| (j << 1) | (bit?(x,y) ? 1 : 0) } }
end

#to_png(mod: 8, pad: 2, bgcolor: nil, color: '#000') ⇒ Object

Raises:

  • (ArgumentError)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dmtx/data_matrix.rb', line 109

def to_png(mod: 8, pad: 2, bgcolor: nil, color: '#000')
  raise ArgumentError, 'module size too small' unless mod > 0
  raise ArgumentError, 'padding too small' unless pad >= 0
  color = color ? ChunkyPNG::Color(color) : ChunkyPNG::Color('black')
  bgcolor = bgcolor ? ChunkyPNG::Color(bgcolor) : ChunkyPNG::Color::TRANSPARENT
  width_px = mod * (width + pad * 2)
  height_px = mod * (height + pad * 2)
  png = ChunkyPNG::Image.new(width_px, height_px, bgcolor)
  sx = sy = mod * pad
  (0..(height - 1)).each do |y|
    (0..(width - 1)).each do |x|
      png.rect(sx + x * mod, sy + y * mod, sx + (x + 1) * mod, sy + (y + 1) * mod, color, color) if bit?(x,y)
    end
  end
  png
end

#to_s(pad: 2) ⇒ Object



72
73
74
75
76
# File 'lib/dmtx/data_matrix.rb', line 72

def to_s(pad: 2)
  (0..(height - 1)).inject('') do |s, y|
    (0..(width - 1)).inject(s) { |t, x| t << (bit?(x,y) ? '██' : '  ') } << "\n"
  end
end

#to_svg(dim: 256, pad: 2, bgcolor: nil, color: '#000') ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/dmtx/data_matrix.rb', line 78

def to_svg(dim: 256, pad: 2, bgcolor: nil, color: '#000')
  raise ArgumentError, 'illegal dimension' unless dim > 0
  raise ArgumentError, 'illegal padding' unless pad >= 0
  color ||= '#000'
  path = ''
  sx = width + pad * 2
  sy = height + pad * 2
  mx = [1, 0, 0, 1, pad, pad]
  y = height
  while y > 0
    y -= 1
    x = width
    while x > 0
      x -= 1
      path << "M#{x},#{y}h1v1h-1v-1z" if bit?(x,y)
    end
  end
  builder = Builder::XmlMarkup.new
  builder.tag! 'svg', xmlns: 'http://www.w3.org/2000/svg',
    viewBox: [0, 0, sx, sy].join(' '),
    width: dim * sx / sy,
    height: dim,
    fill: color,
    'shape-rendering' => 'crispEdges',
    version: '1.1' do |svg|
    svg.path fill: bgcolor, d: "M0,0v#{sy}h#{sx}V0H0Z" if bgcolor
    svg.path transform: "matrix(#{mx.map(&:to_s).join(',')})", d: path
  end
  builder.target!
end