zpl-scaler 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +70 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/zpl-scaler.rb +196 -0
- data/lib/zpl-scaler/version.rb +3 -0
- data/zpl-scaler.gemspec +26 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b41f6026b7cfa2103d123dd1f16739881deac3d5
|
4
|
+
data.tar.gz: 2c7439b046e13cc4f89d12b3e83efea758213c60
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 47719d5d2f94260116f25a99bdb1e5b5a4273584c6356bc356441d9293306e2dfb16652cb4116bef8cebe255c6894d7110586e1404af6e7622fcc41798b46558
|
7
|
+
data.tar.gz: 87d66700d1ee74c88a5f2a73a61343499eea43a6adcd09a4405358fc3cbc77fa6001303c74cdf0ad7721b709b94e060f9413ee2df0a2a0c58333a856e4188617
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Benoit de Chezelles
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# ZplScaler
|
2
|
+
|
3
|
+
TODO: why?!
|
4
|
+
|
5
|
+
|
6
|
+
TODO: list handled commands (only the commands that manipulate coordinates and that need to be re-calculated, all other commands are ignored and left as is)
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'zpl-scaler'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install zpl-scaler
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
**Simple example**:
|
27
|
+
```rb
|
28
|
+
require 'zpl-scaler'
|
29
|
+
|
30
|
+
# Scale by dpi:
|
31
|
+
ZplScaler.dpi_scale '^XA^GB12,30,2,B^XZ', from_dpi: 203, to_dpi: 300
|
32
|
+
# => "^XA^GB17,44,2,B^XZ"
|
33
|
+
|
34
|
+
# Scale by ratio:
|
35
|
+
ZplScaler.ratio_scale '^XA^GB12,30,2,B^XZ', 1.2
|
36
|
+
# => "^XA^GB14,36,2,B^XZ"
|
37
|
+
```
|
38
|
+
|
39
|
+
**Complete example**:
|
40
|
+
```rb
|
41
|
+
require 'zpl-scaler'
|
42
|
+
|
43
|
+
zpl_content = File.read("label_at_203dpi.zpl")
|
44
|
+
|
45
|
+
puts '-- Unique used commands ----'
|
46
|
+
p ZplScaler::ZplReader.uniq_commands zpl_content
|
47
|
+
|
48
|
+
scaled_zpl = ZplScaler.dpi_scale zpl_content, from_dpi: 203, to_dpi: 300
|
49
|
+
|
50
|
+
puts '-- Scaled ZPL --------------'
|
51
|
+
puts scaled_zpl
|
52
|
+
|
53
|
+
File.open("label_at_300dpi.zpl", "w") do |file|
|
54
|
+
file.write(scaled_zpl)
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
## Development
|
59
|
+
|
60
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
61
|
+
|
62
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bew/zpl-scaler.
|
67
|
+
|
68
|
+
## License
|
69
|
+
|
70
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zpl/scaler"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/zpl-scaler.rb
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
require 'strscan'
|
2
|
+
require 'set'
|
3
|
+
require 'zpl-scaler/version'
|
4
|
+
|
5
|
+
module ZplScaler
|
6
|
+
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
# Data class that holds the name & params of a ZPL command.
|
10
|
+
class ZplCommand
|
11
|
+
attr_accessor :name
|
12
|
+
attr_accessor :params
|
13
|
+
|
14
|
+
def initialize(name, params)
|
15
|
+
@name = name
|
16
|
+
@params = params
|
17
|
+
end
|
18
|
+
|
19
|
+
# Converts the command to a ZPL string.
|
20
|
+
def to_zpl_string
|
21
|
+
"^#{ @name }#{ @params.join(',') }"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# NOTE: doesn't handle ZPL that changes the control char (default: '^')
|
26
|
+
class ZplReader
|
27
|
+
# Example format: ^XXparam1,param2,,param4
|
28
|
+
# ZplCommand name: XX (the command is read as 2 chars, no more no less)
|
29
|
+
# 4 (5) params (param 3 & 5 are not given)
|
30
|
+
RX_ZPL_COMMAND = /\^([A-Z0-9]{2})([^\^]*)/
|
31
|
+
|
32
|
+
# Returns the list of unique commands used in the given ZPL.
|
33
|
+
def self.uniq_commands(zpl_content)
|
34
|
+
uniq_cmds = Set.new
|
35
|
+
new(zpl_content).each_command do |cmd|
|
36
|
+
uniq_cmds << cmd.name
|
37
|
+
end
|
38
|
+
uniq_cmds.to_a
|
39
|
+
end
|
40
|
+
|
41
|
+
# Creates a new reader that will read ZPL commands from *content* string.
|
42
|
+
def initialize(content)
|
43
|
+
@scanner = StringScanner.new content
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns the next zpl command if any, or nil.
|
47
|
+
def next_command
|
48
|
+
return if @scanner.eos?
|
49
|
+
|
50
|
+
@scanner.scan(RX_ZPL_COMMAND)
|
51
|
+
|
52
|
+
cmd_name = @scanner[1]
|
53
|
+
raw_params = @scanner[2]
|
54
|
+
|
55
|
+
ZplCommand.new(cmd_name, raw_params&.split(',') || [])
|
56
|
+
end
|
57
|
+
|
58
|
+
# Yields each ZPL command to the block. Stops when there are no more commands to read.
|
59
|
+
def each_command
|
60
|
+
while cmd = next_command
|
61
|
+
yield cmd
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# TODO: doc
|
67
|
+
# It works by parsing ZPL commands, then edit the parameters of specific commands
|
68
|
+
# to scale the coordinates to the new dpi
|
69
|
+
#
|
70
|
+
# NOTE: Cannot work for embedded images
|
71
|
+
class Scaler
|
72
|
+
COMMANDS_PARAM_INDEXES_TO_SCALE = {
|
73
|
+
# ^MN - Media Tracking
|
74
|
+
#
|
75
|
+
# Param 0: media being used
|
76
|
+
# Param 1: black mark offset in dots (optional)
|
77
|
+
"MN" => [1],
|
78
|
+
|
79
|
+
# ^BY - Bar Code Field Default
|
80
|
+
#
|
81
|
+
# Param 0: module width in dots
|
82
|
+
# Param 1: wide bar to narrow bar width ratio (float)
|
83
|
+
# Param 2: bar code height in dots
|
84
|
+
"BY" => [0, 2],
|
85
|
+
|
86
|
+
# ^FO - Field Origin
|
87
|
+
#
|
88
|
+
# Param 0: x-axis location in dots
|
89
|
+
# Param 1: y-axis location in dots
|
90
|
+
# Param 2: justification (enum)
|
91
|
+
"FO" => [0, 1],
|
92
|
+
|
93
|
+
# ^B2 - Interleaved 2 of 5 Bar Code
|
94
|
+
#
|
95
|
+
# Param 0: orientation (enum)
|
96
|
+
# Param 1: bar code height in dots
|
97
|
+
# Param 2: print interpretation line above code (bool)
|
98
|
+
"B2" => [1],
|
99
|
+
|
100
|
+
# ^GB - Graphic Box
|
101
|
+
#
|
102
|
+
# Param 0: box width in dots
|
103
|
+
# Param 1: box height in dots
|
104
|
+
# Param 2: border thickness
|
105
|
+
# Param 3: line color (enum)
|
106
|
+
# Param 4: degree of corner rounding (enum)
|
107
|
+
"GB" => [0, 1, 2],
|
108
|
+
|
109
|
+
# ^BC - Code 128 Bar Code (Subsets A, B, and C)
|
110
|
+
#
|
111
|
+
# Param 0: orientation (enum)
|
112
|
+
# Param 1: bar code height in dots
|
113
|
+
# Param 2: print interpretation line (bool)
|
114
|
+
# Param 3: print interpretation line above code (bool)
|
115
|
+
# Param 4: UCC check digit (bool)
|
116
|
+
# Param 5: mode (enum)
|
117
|
+
"BC" => [1],
|
118
|
+
}
|
119
|
+
|
120
|
+
# ^A - Scalable/Bitmapped Font
|
121
|
+
#
|
122
|
+
# Param -1: font name (value: [A-Z0-9])
|
123
|
+
# Param 0: field orientation (enum)
|
124
|
+
# Param 1: character height in dots
|
125
|
+
# Param 2: width in dots
|
126
|
+
#
|
127
|
+
# (Param -1 is part of the command name, it will not appear in ZplCommand's params)
|
128
|
+
# ---
|
129
|
+
# This must be done externally because the command is 1 char long, with the second
|
130
|
+
# char being the font name, this means there are 36 2-char commands with the same
|
131
|
+
# fonctionnality.
|
132
|
+
#
|
133
|
+
# So instead of:
|
134
|
+
# AA: [....]
|
135
|
+
# AB: [....]
|
136
|
+
# AC: [....]
|
137
|
+
# etc..
|
138
|
+
# We simply generate it. Simple.
|
139
|
+
(('A'..'Z').to_a.concat ('0'..'9').to_a).each do |font_name|
|
140
|
+
COMMANDS_PARAM_INDEXES_TO_SCALE["A" + font_name] = [1, 2]
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.ratio_scale(zpl_content, scale_ratio)
|
144
|
+
reader = ZplReader.new zpl_content
|
145
|
+
scaled_zpl = StringIO.new
|
146
|
+
|
147
|
+
reader.each_command do |cmd|
|
148
|
+
scale_cmd!(cmd, scale_ratio)
|
149
|
+
scaled_zpl << cmd.to_zpl_string
|
150
|
+
end
|
151
|
+
|
152
|
+
scaled_zpl.string
|
153
|
+
end
|
154
|
+
|
155
|
+
protected
|
156
|
+
|
157
|
+
def self.cmd_need_scale?(cmd)
|
158
|
+
!!COMMANDS_PARAM_INDEXES_TO_SCALE[cmd.name]
|
159
|
+
end
|
160
|
+
|
161
|
+
def self.scale_cmd!(cmd, scale_ratio)
|
162
|
+
return unless cmd_need_scale? cmd
|
163
|
+
|
164
|
+
cmd_params = cmd.params
|
165
|
+
|
166
|
+
param_indexes_to_scale = COMMANDS_PARAM_INDEXES_TO_SCALE[cmd.name]
|
167
|
+
param_indexes_to_scale.each do |param_index|
|
168
|
+
|
169
|
+
if (param_s = cmd_params[param_index]) && (param_i = param_to_i?(param_s))
|
170
|
+
cmd_params[param_index] = (param_i * scale_ratio).to_i
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
# Returns an integer converted from the string *param*, or nil if it cannot be
|
176
|
+
# converted.
|
177
|
+
def self.param_to_i?(param)
|
178
|
+
begin
|
179
|
+
Integer(param)
|
180
|
+
rescue ArgumentError # raised when *param* string cannot be converted to Integer
|
181
|
+
nil
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def self.dpi_scale(zpl_content, from_dpi:, to_dpi:)
|
187
|
+
scale_ratio = to_dpi.to_f / from_dpi.to_f
|
188
|
+
|
189
|
+
Scaler.ratio_scale(zpl_content, scale_ratio)
|
190
|
+
end
|
191
|
+
|
192
|
+
def self.ratio_scale(zpl_content, scale_ratio)
|
193
|
+
Scaler.ratio_scale(zpl_content, scale_ratio)
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
data/zpl-scaler.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "zpl-scaler/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zpl-scaler"
|
8
|
+
spec.version = ZplScaler::VERSION
|
9
|
+
spec.authors = ["Benoit de Chezelles"]
|
10
|
+
spec.email = ["benoit.dechezelles@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple gem to scale ZPL label}
|
13
|
+
spec.homepage = "https://github.com/bew/zpl-scaler.rb"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zpl-scaler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Benoit de Chezelles
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- benoit.dechezelles@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/zpl-scaler.rb
|
72
|
+
- lib/zpl-scaler/version.rb
|
73
|
+
- zpl-scaler.gemspec
|
74
|
+
homepage: https://github.com/bew/zpl-scaler.rb
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.5.1
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Simple gem to scale ZPL label
|
98
|
+
test_files: []
|