Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to call create_makefile("pdf417/pdf417") on Ruby 2.7.7 / Bundler 2.4.2 #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
AllCops:
NewCops: enable

Metrics:
Enabled: false
13 changes: 10 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# frozen_string_literal: true

# A sample Gemfile
source "https://rubygems.org"
source 'https://rubygems.org'

group :test do
gem 'shoulda', :require => false
gem 'shoulda', require: false
end

gemspec

gemspec
# Dev dependencies
gem 'bundler', '~> 2.5'
gem 'minitest', '~> 5.0'
gem 'ostruct', '~> 0.6.0'
gem 'rake', '~> 13'
21 changes: 11 additions & 10 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
require "bundler/gem_tasks"
require "rake/testtask"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.libs << 'test'
t.libs << 'lib'
t.test_files = FileList['test/**/*_test.rb']
end

task :default => :test
task default: :test

desc 'rebuilds the pdf417 library'
task :build_extension do
pwd = `pwd`
system "cd ext/pdf417 && make clean"
system "cd ext/pdf417 && ruby extconf.rb && make"
`pwd`
system 'cd ext/pdf417 && make clean'
system 'cd ext/pdf417 && ruby extconf.rb && make'
end


task :test => [:build_extension]
task test: [:build_extension]
4 changes: 3 additions & 1 deletion ext/pdf417/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true

require 'mkmf'
extension_name = 'pdf417'
dir_config(extension_name)
create_makefile(extension_name)
create_makefile("#{extension_name}/#{extension_name}")
195 changes: 95 additions & 100 deletions lib/pdf417.rb
Original file line number Diff line number Diff line change
@@ -1,118 +1,112 @@
# frozen_string_literal: true

require 'pdf417/pdf417'
require 'pdf417/lib'

# A wrapper for the pdf417lib C Library,
class PDF417

class GenerationError < StandardError; end

class << self
def encode_text(text)
PDF417::Lib.encode_text(text)
end
end
end

attr_reader :bit_columns, :bit_rows, :bit_length

def initialize(*attrs)
# TODO: Test these defaults, make sure that they can get set on init, check to see that the output changes accordingly
self.text = ""
# TODO: Test these defaults, make sure that they can get set on init, and the output changes accordingly
self.text = ''
@y_height = 3
@aspect_ratio = 0.5
@rows = nil
@cols = nil

if attrs.first.is_a?(String)
self.text = attrs.first
elsif attrs.first.is_a?(Hash)
attrs.first.each do |k,v|
if self.respond_to?("#{k}=".to_sym)
self.send("#{k}=".to_sym, v)
end
elsif attrs.first.is_a?(Hash)
attrs.first.each do |k, v|
send(:"#{k}=", v) if respond_to?(:"#{k}=")
end
end

@blob = nil
end

def inspect
"#<#{self.class.name}:#{self.object_id} >"
"#<#{self.class.name}:#{object_id} >"
end



def text=(val)
@codewords = @blob = nil
@text = val
end

def text
if @raw_codewords.nil?
@text
else
""
''
end
end

def codewords
return @raw_codewords if !@raw_codewords.nil?
return @raw_codewords unless @raw_codewords.nil?

@codewords ||= self.class.encode_text(text)
end

# Y Height
def y_height
@y_height
end
attr_reader :y_height

def y_height=(val)
@blob = nil
@y_height = val
end

# Aspect ratio
def aspect_ratio
@aspect_ratio
end
attr_reader :aspect_ratio

def aspect_ratio=(val)
@blob = nil
@rows = nil
@cols = nil
@aspect_ratio = val
end

# For setting the codewords directly
def raw_codewords
@raw_codewords
end
attr_reader :raw_codewords

def raw_codewords=(val)
@blob = nil
@raw_codewords = val
end

# Make the barcode at least this number of rows
def rows
@rows
end
attr_reader :rows

def rows=(val)
@blob = nil
@rows = val
end

# Make the barcode at least this number of columns
def cols
@cols
end
attr_reader :cols

def cols=(val)
@blob = nil
@cols = val
end

# Request the specified error level must be between 0 and 8
def error_level
@error_level
end
attr_reader :error_level

def error_level=(val)
@blob = nil
@error_level = val
end

def generate!
lib = Lib.new(text)
options = []
Expand All @@ -123,106 +117,109 @@ def generate!
lib.generation_options |= Lib::PDF417_USE_RAW_CODEWORDS
options << 'raw codewords'
end
if self.rows.to_i > 0 && self.cols.to_i > 0
lib.code_rows = self.rows.to_i
lib.code_cols = self.cols.to_i

if rows.to_i.positive? && cols.to_i.positive?
lib.code_rows = rows.to_i
lib.code_cols = cols.to_i
lib.generation_options |= Lib::PDF417_FIXED_RECTANGLE
options << "#{rows}x#{cols}"
elsif self.rows.to_i > 0
lib.code_rows = self.rows.to_i
elsif rows.to_i.positive?
lib.code_rows = rows.to_i
lib.generation_options |= Lib::PDF417_FIXED_ROWS
options << "#{rows} rows"
elsif self.cols.to_i > 0
lib.code_cols = self.cols.to_i
elsif cols.to_i.positive?
lib.code_cols = cols.to_i
lib.generation_options |= Lib::PDF417_FIXED_COLUMNS
options << "#{cols} cols"
end
if self.error_level.to_i >= 0 && self.error_level.to_i <= 8
lib.error_level = self.error_level.to_i

if error_level.to_i >= 0 && error_level.to_i <= 8
lib.error_level = error_level.to_i
lib.generation_options |= Lib::PDF417_USE_ERROR_LEVEL
options << "requested #{error_level.to_i} error level"
end
lib.aspect_ratio = self.aspect_ratio.to_f
lib.y_height = self.y_height.to_f
(@blob = lib.to_blob)

lib.aspect_ratio = aspect_ratio.to_f
lib.y_height = y_height.to_f

(@blob = lib.to_blob)
if @blob.nil? || @blob.empty?
if lib.generation_error == Lib::PDF417_ERROR_TEXT_TOO_BIG
raise GenerationError, "Text is too big"
raise GenerationError, 'Text is too big'
elsif lib.generation_error == Lib::PDF417_ERROR_INVALID_PARAMS
msg = "Invalid parameters: #{options.join(', ')}"
if lib.generation_options & Lib::PDF417_USE_RAW_CODEWORDS && lib.raw_codewords.length != lib.raw_codewords.first
msg +=". The first element of the raw codwords must be the length of the array. Currently it is #{lib.raw_codewords.first}, perhaps it should be #{lib.raw_codewords.length}?"
if (lib.generation_options &
Lib::PDF417_USE_RAW_CODEWORDS) && lib.raw_codewords.length != lib.raw_codewords.first
msg += '. The first element of the raw codwords must be the length of the array. ' \
"Currently it is #{lib.raw_codewords.first}, perhaps it should be #{lib.raw_codewords.length}?"
end
raise GenerationError, msg
else
raise GenerationError, "Could not generate bitmap error: #{options.join(', ')}"
end
else
if lib.raw_codewords.is_a?(Array)
@codewords = lib.raw_codewords
else
@codewords = lib.codewords
end
@codewords = if lib.raw_codewords.is_a?(Array)
lib.raw_codewords
else
lib.codewords
end
@bit_columns = lib.bit_columns
@bit_rows = ((lib.bit_columns - 1) / 8) + 1
@bit_length = lib.bit_length
@rows = lib.code_rows
@rows = lib.code_rows
@cols = lib.code_cols
@error_level = lib.error_level
@aspect_ratio = lib.aspect_ratio
@y_height = lib.y_height
return true
true
end
end



def to_blob
self.generate! if @blob.nil?
@blob
end
alias_method :blob, :to_blob
generate! if @blob.nil?
@blob
end
alias blob to_blob

def encoding
self.generate! if @blob.nil?
generate! if @blob.nil?

# This matches the output from the pdf417 lib sample output.
enc = self.blob.bytes.to_a.each_slice(self.bit_rows).to_a[0..(self.rows-1)] # sometimes we get more rows than expected, truncate

# The length returned here is too long and we have extra data that gets padded w/ zeroes, meaning it doesn't all match.
enc = blob.bytes.to_a.each_slice(bit_rows).to_a[0..(rows - 1)] # sometimes we get more rows than expected, truncate

# The length returned here is too long and we have extra data that gets padded w/ zeroes,
# meaning it doesn't all match.
# Eg, instead of ending with "111111101000101001" it ends with "1111111010001010010000000".
return enc.collect do |row_of_bytes|
row_of_bytes.collect{|x| sprintf("%08b", x)}.join[0..self.bit_columns-1]
enc.collect do |row_of_bytes|
row_of_bytes.collect { |x| format('%08b', x) }.join[0..bit_columns - 1]
end
end

def encoding_to_s
self.encoding.each{|x| puts x.gsub("0"," ")}
encoding.each { |x| puts x.gsub('0', ' ') }
end

def to_chunky_png(opts = {})
require 'chunky_png' unless defined?(ChunkyPNG)
self.generate! if @blob.nil?

generate! if @blob.nil?
opts[:x_scale] ||= 1
opts[:y_scale] ||= 3
opts[:margin] ||= 10
full_width = (self.bit_columns * opts[:x_scale]) + (opts[:margin] * 2)
full_height = (self.rows * opts[:y_scale]) + (opts[:margin] * 2)
full_width = (bit_columns * opts[:x_scale]) + (opts[:margin] * 2)
full_height = (rows * opts[:y_scale]) + (opts[:margin] * 2)

canvas = ChunkyPNG::Image.new(full_width, full_height, ChunkyPNG::Color::WHITE)

x, y = opts[:margin], opts[:margin]
booleans = encoding.map{|l| l.split(//).map{|c| c == '1' } }
x = opts[:margin]
y = opts[:margin]
booleans = encoding.map { |l| l.chars.map { |c| c == '1' } }
booleans.each do |line|
line.each do |bar|
if bar
x.upto(x+(opts[:x_scale]-1)) do |xx|
y.upto y+(opts[:y_scale]-1) do |yy|
canvas[xx,yy] = ChunkyPNG::Color::BLACK
x.upto(x + (opts[:x_scale] - 1)) do |xx|
y.upto y + (opts[:y_scale] - 1) do |yy|
canvas[xx, yy] = ChunkyPNG::Color::BLACK
end
end
end
Expand All @@ -237,6 +234,4 @@ def to_chunky_png(opts = {})
def to_png(opts = {})
to_chunky_png(opts).to_datastream.to_s
end


end
Loading