dmtx 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a114ad5037384a5dd7a9944663ff50f2ef82b5ccefdcf9f415cf1e364dbd6bb5
4
- data.tar.gz: 4b3346d6d27a62311a04df3c6ea459ee1524920811d7d5b34104fbf61185923b
3
+ metadata.gz: 9aedbfc3b9bdc27d443dd1e2d65783af65da3ffb05c5a55060a04747a0ffab6f
4
+ data.tar.gz: 34069b336ce07010638aabccb0b5523ccc448ff31b4a5902ad061d1bb2d9d18b
5
5
  SHA512:
6
- metadata.gz: '01866bc5320e4c56a495eed7c2671871b64dc7d84af57e4d3aa15078617d69aca943da63386b62fcd2e4efb1ee73d9c30b58ff9443651e2ba304237b1a2135a6'
7
- data.tar.gz: 6b8e0d0d64248af532a77c4407dc884fea29cba0428115733bf3a046fde586c57eea2b3fab5b963c4f81c3c233f5c0f2cf626bb2d3069b208cb38a73f6c64746
6
+ metadata.gz: 7f126e2e555695137c8720f304cd9f87caea35fc38d6c6714420b6d1e62ee7ae172cf2ae39c13ee7190b0143f3e2d372a80a228e8deb47a09091025353830295
7
+ data.tar.gz: a40eb8a839564b6877665d92c0f8c5f7117ad9ff379594648586170547d3d1108e9dd3ea68bd1af81760e5aa336a3e34dd54e0a6c1d4d31c9d1567d1d04ee7cf
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [0.2.0] - 2024-02-11
2
+
3
+ - Support GS1 DataMatrix
4
+
1
5
  ## [0.1.1] - 2021-09-02
2
6
 
3
7
  - Fix PNG bgcolor
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Gem Version](https://badge.fury.io/rb/dmtx.svg)](https://badge.fury.io/rb/dmtx)
2
+ [![build](https://github.com/mtgrosser/dmtx/actions/workflows/build.yml/badge.svg)](https://github.com/mtgrosser/dmtx/actions/workflows/build.yml)
3
+
1
4
  # dmtx
2
5
  Pure Ruby Data Matrix generator
3
6
 
@@ -39,6 +42,13 @@ dmtx.width
39
42
  dmtx.height
40
43
  => 16
41
44
 
45
+ dmtx.encoding
46
+ => :ascii
47
+
48
+ dmtx.encoded_message
49
+ => [68, 105, 118, 111, 108, 122, 33, 67, 98, 100, 112, 111,
50
+ 62, 103, 49, 93, 99, 53, 117, 202, 250, 186, 232, 14]
51
+
42
52
  # Generate SVG
43
53
  dmtx.to_svg
44
54
  => "<svg xmlns= ..."
@@ -63,4 +73,25 @@ dmtx.to_i
63
73
 
64
74
  # Binary representation
65
75
  dmtx.to_i.to_s(2)
66
- => "1010101010101010101110101000001111010111111101101001101011101001100110010100111010111011111100111001011001001000100010000111010110110001101111001011001001101011110000011111110010101101101010111001101001000000111100101000110110101110011100101111111111111111"
76
+ => "1010101010101010
77
+ 1011101010000011
78
+ 1101011111110110
79
+ 1001101011101001
80
+ 1001100101001110
81
+ 1011101111110011
82
+ 1001011001001000
83
+ 1000100001110101
84
+ 1011000110111100
85
+ 1011001001101011
86
+ 1100000111111100
87
+ 1010110110101011
88
+ 1001101001000000
89
+ 1111001010001101
90
+ 1010111001110010
91
+ 1111111111111111"
92
+
93
+ # Choose encoding
94
+ Dmtx::DataMatrix.new('Chunky Bacon', encoding: :txt)
95
+
96
+ # GS1 DataMatrix
97
+ Dmtx::DataMatrix.new("\x1d01095011010209171719050810ABCD1234\x1d2110", encoding: :gs1)
@@ -6,7 +6,7 @@ require 'chunky_png'
6
6
 
7
7
  module Dmtx
8
8
  class DataMatrix
9
- attr_reader :width, :height
9
+ attr_reader :width, :height, :encoded_message, :encoding
10
10
 
11
11
  C40 = [230,
12
12
  31, 0, 0,
@@ -43,16 +43,26 @@ module Dmtx
43
43
  64, 8, 0,
44
44
  90, 9, 51,
45
45
  255, 8, 0]
46
-
47
- def initialize(msg, rect: false)
46
+
47
+ # See https://www.gs1.org/standards/gs1-datamatrix-guideline/25
48
+ FNC1 = 29
49
+ FNC1_CODEWORD = 232
50
+
51
+ DEFAULT_ENCODINGS = %i[ascii c40 txt x12 edifact base gs1].freeze
52
+ ENCODINGS = (%i[gs1] + DEFAULT_ENCODINGS).freeze
53
+
54
+ def initialize(msg, rect: false, encoding: nil)
48
55
  @m = []
49
56
  @width = 0
50
57
  @height = 0
51
- encode(msg, rect)
58
+ raise ArgumentError, "illegal encoding #{encoding.inspect}" if encoding && !ENCODINGS.include?(encoding)
59
+ @encoding, @encoded_message = encode_message(msg, encoding)
60
+ raise EncodingError, "illegal payload" unless @encoded_message
61
+ encode(@encoded_message, rect)
52
62
  end
53
63
 
54
64
  def inspect
55
- "#<#{self.class.name}:0x#{object_id.to_s(16)} #{width}x#{height}>"
65
+ "#<#{self.class.name}:0x#{object_id.to_s(16)} #{width}x#{height}@#{encoding}>"
56
66
  end
57
67
 
58
68
  def to_i
@@ -76,7 +86,6 @@ module Dmtx
76
86
  y = height
77
87
  while y > 0
78
88
  y -= 1
79
- d = 0
80
89
  x = width
81
90
  while x > 0
82
91
  x -= 1
@@ -139,6 +148,23 @@ module Dmtx
139
148
  end
140
149
  result
141
150
  end
151
+
152
+ def gs1_encode(t)
153
+ bytes, result = t.bytes, []
154
+ while c = bytes.shift
155
+ if !bytes.empty? && c > 47 && c < 58 && bytes.first > 47 && bytes.first < 58
156
+ result << (c - 48) * 10 + bytes.shift + 82
157
+ elsif c > 127
158
+ result << 235
159
+ result << ((c - 127) & 255)
160
+ elsif c == FNC1
161
+ result << FNC1_CODEWORD
162
+ else
163
+ result << c + 1
164
+ end
165
+ end
166
+ result
167
+ end
142
168
 
143
169
  def base_encode(t)
144
170
  bytes, result = t.bytes, [231]
@@ -204,18 +230,27 @@ module Dmtx
204
230
  result.concat ascii_encode(bytes[(i - cc)..-1].to_a.pack('C*')) if cc > 0 || i < l
205
231
  result
206
232
  end
207
-
208
- def encodings(msg)
209
- { ascii: ascii_encode(msg),
210
- c40: text_encode(msg, C40),
211
- txt: text_encode(msg, TEXT),
212
- x12: text_encode(msg, X12),
213
- edifact: edifact_encode(msg),
214
- base: base_encode(msg) }
233
+
234
+ def c40_encode(t)
235
+ text_encode(t, C40)
236
+ end
237
+
238
+ def txt_encode(t)
239
+ text_encode(t, TEXT)
240
+ end
241
+
242
+ def x12_encode(t)
243
+ text_encode(t, X12)
244
+ end
245
+
246
+ def encode_message(msg, encoding)
247
+ (encoding ? [encoding] : DEFAULT_ENCODINGS)
248
+ .map { |name| [name, send("#{name}_encode", msg)] }
249
+ .reject { |_, encoded| encoded.empty? }
250
+ .min_by { |_, encoded| encoded.size }
215
251
  end
216
252
 
217
- def encode(text, rct)
218
- enc = encodings(text).values.reject(&:empty?).min_by(&:size)
253
+ def encode(enc, rct)
219
254
  el = enc.size
220
255
  nc = nr = 1
221
256
  j = -1
data/lib/dmtx/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dmtx
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/dmtx.rb CHANGED
@@ -3,4 +3,5 @@ require_relative 'dmtx/data_matrix'
3
3
 
4
4
  module Dmtx
5
5
  class Error < StandardError; end
6
+ class EncodingError < Error; end
6
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dmtx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Grosser
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-02 00:00:00.000000000 Z
11
+ date: 2024-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: builder
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description:
41
+ description:
42
42
  email:
43
43
  - mtgrosser@gmx.net
44
44
  executables: []
@@ -56,7 +56,7 @@ homepage: https://github.com/mtgrosser/dmtx
56
56
  licenses:
57
57
  - MIT
58
58
  metadata: {}
59
- post_install_message:
59
+ post_install_message:
60
60
  rdoc_options: []
61
61
  require_paths:
62
62
  - lib
@@ -71,8 +71,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  - !ruby/object:Gem::Version
72
72
  version: '0'
73
73
  requirements: []
74
- rubygems_version: 3.0.3
75
- signing_key:
74
+ rubygems_version: 3.1.4
75
+ signing_key:
76
76
  specification_version: 4
77
77
  summary: Pure Ruby Datamatrix Generator
78
78
  test_files: []