barcode 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/barcode/code39.rb +124 -0
- metadata +37 -0
@@ -0,0 +1,124 @@
|
|
1
|
+
# barcode/code39.rb
|
2
|
+
#
|
3
|
+
# Author:: Clinton R. Nixon <crnixon@gmail.com>
|
4
|
+
# Copyright:: Copyright (c) 2005 Demand Publishing
|
5
|
+
# License:: Distributes under the same terms as Ruby
|
6
|
+
# Version:: 0.2
|
7
|
+
|
8
|
+
require 'RMagick'
|
9
|
+
include Magick
|
10
|
+
|
11
|
+
# This class takes a string and generates a Code 39-based barcode
|
12
|
+
# from that string.
|
13
|
+
class Code39
|
14
|
+
|
15
|
+
# quick reference as to what's up with @@encoding:
|
16
|
+
# - B => large solid bar
|
17
|
+
# - b => small solid bar
|
18
|
+
# - W => large space
|
19
|
+
# - w => small space
|
20
|
+
@@encoding = {
|
21
|
+
'0' => "bwbWBwBwb",
|
22
|
+
'1' => "BwbWbwbwB",
|
23
|
+
'2' => "bwBWbwbwB",
|
24
|
+
'3' => "BwBWbwbwb",
|
25
|
+
'4' => "bwbWBwbwB",
|
26
|
+
'5' => "BwbWBwbwb",
|
27
|
+
'6' => "bwBWBwbwb",
|
28
|
+
'7' => "bwbWbwBwB",
|
29
|
+
'8' => "BwbWbwBwb",
|
30
|
+
'9' => "bwBWbwBwb",
|
31
|
+
'A' => "BwbwbWbwB",
|
32
|
+
'B' => "bwBwbWbwB",
|
33
|
+
'C' => "BwBwbWbwb",
|
34
|
+
'D' => "bwbwBWbwB",
|
35
|
+
'E' => "BwbwBWbwb",
|
36
|
+
'F' => "bwBwBWbwb",
|
37
|
+
'G' => "bwbwbWBwB",
|
38
|
+
'H' => "BwbwbWBwb",
|
39
|
+
'I' => "bwBwbWBwb",
|
40
|
+
'J' => "bwbwBWBwb",
|
41
|
+
'K' => "BwbwbwbWB",
|
42
|
+
'L' => "bwBwbwbWB",
|
43
|
+
'M' => "BwBwbwbWb",
|
44
|
+
'N' => "bwbwBwbWB",
|
45
|
+
'O' => "BwbwBwbWb",
|
46
|
+
'P' => "bwBwBwbWb",
|
47
|
+
'Q' => "bwbwbwBWB",
|
48
|
+
'R' => "BwbwbwBWb",
|
49
|
+
'S' => "bwBwbwBWb",
|
50
|
+
'T' => "bwbwBwBWb",
|
51
|
+
'U' => "BWbwbwbwB",
|
52
|
+
'V' => "bWBwbwbwB",
|
53
|
+
'W' => "BWBwbwbwb",
|
54
|
+
'X' => "bWbwBwbwB",
|
55
|
+
'Y' => "BWbwBwbwb",
|
56
|
+
'Z' => "bWBwBwbwb",
|
57
|
+
'-' => "bWbwbwBwB",
|
58
|
+
'.' => "BWbwbwBwb",
|
59
|
+
' ' => "bWBwbwBwb",
|
60
|
+
'$' => "bWbWbWbwb",
|
61
|
+
'/' => "bWbWbwbWb",
|
62
|
+
'+' => "bWbwbWbWb",
|
63
|
+
'%' => "bwbWbWbWb",
|
64
|
+
'*' => "bWbwBwBwb"
|
65
|
+
}
|
66
|
+
|
67
|
+
# Takes a string, uppercases it, and wraps it in asterisks.
|
68
|
+
# If any character is unavailable in Code 39 encoding,
|
69
|
+
# throws a vicious error.
|
70
|
+
def initialize(text)
|
71
|
+
text = text.upcase
|
72
|
+
text.split(//).each do |char|
|
73
|
+
if not @@encoding.has_key?(char)
|
74
|
+
raise "Unencodable string."
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
@text = "*#{text}*"
|
79
|
+
end
|
80
|
+
|
81
|
+
# Outputs a string that represents the encoding.
|
82
|
+
# Not necessarily useful externally, but
|
83
|
+
# who knows?
|
84
|
+
def to_s
|
85
|
+
output = String.new()
|
86
|
+
@text.split(//).each do |char|
|
87
|
+
output += @@encoding[char] + " "
|
88
|
+
end
|
89
|
+
return output
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
# The overly complicated image generation code.
|
94
|
+
# Your barcode image width and height should be in pixels.
|
95
|
+
# The actual barcode will not be that width, instead
|
96
|
+
# it will be a multiple of your text string (plus two
|
97
|
+
# asterisks - @text) length * 14. White space will center
|
98
|
+
# the rest.
|
99
|
+
def to_img(filename, width, height)
|
100
|
+
bar_code_width = width - (width % ( @text.length * 14 ) )
|
101
|
+
letter_width = bar_code_width / @text.length
|
102
|
+
small_bar_width = letter_width / 14
|
103
|
+
large_bar_width = ((7.0 / 3.0) * small_bar_width.to_f).to_i
|
104
|
+
|
105
|
+
canvas = Image.new(width, height)
|
106
|
+
gc = Draw.new
|
107
|
+
gc.fill('black')
|
108
|
+
gc.stroke('black')
|
109
|
+
gc.stroke_width(-1)
|
110
|
+
# Necessary to prevent massive blurry image nonsense.
|
111
|
+
gc.stroke_antialias(false)
|
112
|
+
|
113
|
+
cur_x = (width - bar_code_width) / 2
|
114
|
+
|
115
|
+
self.to_s.split(//).each do |char|
|
116
|
+
if char.upcase == "B"
|
117
|
+
gc.rectangle(cur_x, 0, cur_x + (char == "B" ? large_bar_width : small_bar_width), height)
|
118
|
+
end
|
119
|
+
cur_x += char.match(/[BW]/) ? large_bar_width : small_bar_width
|
120
|
+
end
|
121
|
+
gc.draw(canvas)
|
122
|
+
canvas.write(filename)
|
123
|
+
end
|
124
|
+
end
|
metadata
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.10
|
3
|
+
specification_version: 1
|
4
|
+
name: barcode
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: "0.2"
|
7
|
+
date: 2005-07-13
|
8
|
+
summary: Barcode classes.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: crnixon@gmail.com
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description: Simple classes for creating barcodes.
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
-
|
22
|
+
- ">"
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.0
|
25
|
+
version:
|
26
|
+
platform: ruby
|
27
|
+
authors:
|
28
|
+
- Clinton R. Nixon
|
29
|
+
files:
|
30
|
+
- lib/barcode/code39.rb
|
31
|
+
test_files: []
|
32
|
+
rdoc_options: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
requirements: []
|
37
|
+
dependencies: []
|