Skip to content

Commit

Permalink
Merge pull request #120 from ksylvest/ksylvest/swap-semacode-for-dmtx
Browse files Browse the repository at this point in the history
Swap `semacode` for `dmtx`
  • Loading branch information
toretore authored Oct 26, 2024
2 parents 683f08e + 0927440 commit 1b41686
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* 0.7.0

* Swap semaocde for dmtx [ksylvest]

* 0.6.7

* RMagick outputter should use line command for lines [joshuaflanagan]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ require 'barby/barcode/<filename>'
| EAN-8 | `ean_8` ||
| UPC/EAN supplemental, 2 & 5 digits | `upc_supplemental` ||
| QR Code | `qr_code` | `rqrcode` |
| DataMatrix (Semacode) | `data_matrix` | `semacode` or `semacode-ruby19` |
| DataMatrix (dmtx) | `data_matrix` | `dmtx` |
| PDF417 | `pdf_417` | JRuby |


Expand Down
2 changes: 1 addition & 1 deletion barby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Gem::Specification.new do |s|
s.add_development_dependency "minitest", "~> 5.11"
s.add_development_dependency "bundler", "~> 1.16"
s.add_development_dependency "rake", "~> 10.0"
s.add_development_dependency "semacode-ruby19", "~> 0.7"
s.add_development_dependency "rqrcode", "~> 0.10"
s.add_development_dependency "prawn", "~> 2.2"
s.add_development_dependency "cairo", "~> 1.15"
s.add_development_dependency "dmtx", "~> 0.2"
end
49 changes: 42 additions & 7 deletions lib/barby/barcode/data_matrix.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'semacode' #Ruby 1.8: gem install semacode - Ruby 1.9: gem install semacode-ruby19
require 'dmtx' # gem install dmtx
require 'barby/barcode'

module Barby


#Uses the semacode library (gem install semacode) to encode DataMatrix barcodes
#Uses the dmtx library (gem install dmtx) to encode DataMatrix barcodes
class DataMatrix < Barcode2D

attr_reader :data
Expand All @@ -21,21 +21,56 @@ def data=(data)
end

def encoder
@encoder ||= ::DataMatrix::Encoder.new(data)
@encoder ||= ::Dmtx::DataMatrix.new(data)
end


# Converts the barcode to an array of lines where 0 is white and 1 is black.
#
# @example
# code = Barby::DataMatrix.new('humbaba')
# code.encoding
# # => [
# # "10101010101010",
# # "10111010000001",
# # "11100101101100",
# # "11101001110001",
# # "11010101111110",
# # "11100101100001",
# # "11011001011110",
# # "10011011010011",
# # "11011010000100",
# # "10101100101001",
# # "11011100001100",
# # "10101110110111",
# # "11000001010100",
# # "11111111111111",
# # ]
#
# @return [String]
def encoding
encoder.data.map{|a| a.map{|b| b ? '1' : '0' }.join }
width = encoder.width
height = encoder.height

height.times.map { |y| width.times.map { |x| bit?(x, y) ? '1' : '0' }.join }
end


def semacode?
#TODO: Not sure if this is right
data =~ /^http:\/\//
# NOTE: this method is not exposed via the gem so using send ahead of opening a PR to hopefully support:
#
# https://github.com/mtgrosser/dmtx/blob/master/lib/dmtx/data_matrix.rb#L133-L135
#
# @param x [Integer] x-coordinate
# @param y [Integer] y-coordinate
# @return [Boolean]
def bit?(x, y)
encoder.send(:bit?, x, y)
end


# The data being encoded.
#
# @return [String]
def to_s
data
end
Expand Down
4 changes: 2 additions & 2 deletions lib/barby/version.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Barby #:nodoc:
module VERSION #:nodoc:
MAJOR = 0
MINOR = 6
TINY = 8
MINOR = 7
TINY = 0

STRING = [MAJOR, MINOR, TINY].join('.').freeze
end
Expand Down
22 changes: 16 additions & 6 deletions test/data_matrix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ class DataMatrixTest < Barby::TestCase
end

it "should have the expected encoding" do
@code.encoding.must_equal ["1010101010101010", "1011111000011111", "1110111000010100",
"1110100100000111", "1101111010101000", "1101111011110011",
"1111111100000100", "1100101111110001", "1001000010001010",
"1101010110111011", "1000000100011110", "1001010010000011",
"1101100111011110", "1110111010000101", "1110010110001010",
"1111111111111111"]
@code.encoding.must_equal [
"10101010101010",
"10111010000001",
"11100101101100",
"11101001110001",
"11010101111110",
"11100101100001",
"11011001011110",
"10011011010011",
"11011010000100",
"10101100101001",
"11011100001100",
"10101110110111",
"11000001010100",
"11111111111111",
]
end

it "should return data on to_s" do
Expand Down

0 comments on commit 1b41686

Please sign in to comment.