brother_escp 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.rubocop.yml +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +79 -0
- data/Rakefile +7 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/brother_escp.gemspec +40 -0
- data/examples/simple.rb +18 -0
- data/examples/small.png +0 -0
- data/examples/small2.png +0 -0
- data/lib/brother_escp.rb +17 -0
- data/lib/brother_escp/document.rb +71 -0
- data/lib/brother_escp/image.rb +56 -0
- data/lib/brother_escp/image_converter.rb +98 -0
- data/lib/brother_escp/sequence.rb +57 -0
- data/lib/brother_escp/version.rb +6 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 61a11c65fb3b3a8b52c80e597cdca3e5a0d422fd
|
4
|
+
data.tar.gz: 2aee66767228566915faf782710bce5673a6e39d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8dcff64a46a6e61586b3229a9bd11370884854c8daaa51f8993036a5f223c53e9bc457f691eb15f88457914b8907c5e543bf67a416907632ddbedd009a6b0892
|
7
|
+
data.tar.gz: 0612d490fbcdedc6b05441af62838423018c7d4e003d8337d73cdc6441e60baa5b8e7749814c15bfb20ff8e41165b630a634b0cef6e5c57a61d49e03da0e0d49
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
brother_escp
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.1
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at tal.bourguignon@laposte.net. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 David Bourguignon
|
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,79 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/butterware/brother_escp.svg?branch=master)](https://travis-ci.org/butterware/brother_escp)
|
2
|
+
|
3
|
+
# BrotherEscp
|
4
|
+
|
5
|
+
*WORK IN PROGRESS*
|
6
|
+
|
7
|
+
Library to allow printing on Brother label printer, tested with TD-4000 model.
|
8
|
+
|
9
|
+
Allow printing of text and images.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'brother_escp'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install brother_escp
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
require 'brother_escp'
|
32
|
+
|
33
|
+
# Create a new document to send to the printer
|
34
|
+
doc = BrotherEscp::Document.new
|
35
|
+
|
36
|
+
# Use predefined sequences
|
37
|
+
doc.sequence :LANDSCAPE_OFF
|
38
|
+
|
39
|
+
# Use helper to setup some printing settings
|
40
|
+
doc.page_length = 50
|
41
|
+
|
42
|
+
# Print some text
|
43
|
+
doc.write "Hello, world"
|
44
|
+
|
45
|
+
# Add an image
|
46
|
+
doc.image(file_name: 'examples/small.png', density: :high_density)
|
47
|
+
|
48
|
+
# Add a form feed
|
49
|
+
doc.sequence(:CTL_FF)
|
50
|
+
|
51
|
+
# Raw data to be sent to the printer
|
52
|
+
doc.to_escp
|
53
|
+
|
54
|
+
# Dump the data in readable hexadecimal form
|
55
|
+
doc.inspect
|
56
|
+
|
57
|
+
# Raw data encoded in Base64
|
58
|
+
doc.to_base64
|
59
|
+
|
60
|
+
```
|
61
|
+
|
62
|
+
## Documentation
|
63
|
+
|
64
|
+
Generate the documentation with yard
|
65
|
+
|
66
|
+
## Development
|
67
|
+
|
68
|
+
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.
|
69
|
+
|
70
|
+
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).
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/brother_escp. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
75
|
+
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'brother_escp'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'brother_escp/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'brother_escp'
|
9
|
+
spec.version = BrotherEscp::VERSION
|
10
|
+
spec.authors = ['David Bourguignon']
|
11
|
+
spec.email = ['david@butterware.co.uk']
|
12
|
+
|
13
|
+
spec.summary = 'Print text and images to a Brother label printer with ESC/P.'
|
14
|
+
spec.description = 'Print text and images to a Brother label printer with ESC/P.'
|
15
|
+
spec.homepage = 'https://github.com/butterware/brother_escp'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
# if spec.respond_to?(:metadata)
|
21
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
22
|
+
# else
|
23
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
24
|
+
# "public gem pushes."
|
25
|
+
# end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(test|spec|features)/})
|
29
|
+
end
|
30
|
+
spec.bindir = 'exe'
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
|
34
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
35
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
36
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
37
|
+
# spec.add_development_dependency 'simplecov', '~> 3.0'
|
38
|
+
|
39
|
+
spec.add_dependency 'chunky_png'
|
40
|
+
end
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'brother_escp'
|
3
|
+
|
4
|
+
doc = BrotherEscp::Document.new
|
5
|
+
|
6
|
+
doc.sequence(:LANDSCAPE_OFF)
|
7
|
+
|
8
|
+
doc.image(file_name: 'examples/small.png')
|
9
|
+
doc.image(file_name: 'examples/small.png', density: :high_density)
|
10
|
+
doc.image(file_name: 'examples/small.png', density: :higher_density)
|
11
|
+
|
12
|
+
doc.sequence(:CTL_FF)
|
13
|
+
|
14
|
+
# puts doc.to_escp
|
15
|
+
puts
|
16
|
+
puts doc.inspect
|
17
|
+
puts
|
18
|
+
puts doc.to_base64
|
data/examples/small.png
ADDED
Binary file
|
data/examples/small2.png
ADDED
Binary file
|
data/lib/brother_escp.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'base64'
|
3
|
+
require 'logger'
|
4
|
+
require 'brother_escp/version'
|
5
|
+
require 'brother_escp/document'
|
6
|
+
require 'brother_escp/image'
|
7
|
+
require 'brother_escp/sequence'
|
8
|
+
|
9
|
+
# Main module
|
10
|
+
module BrotherEscp
|
11
|
+
module_function
|
12
|
+
|
13
|
+
# Helper method to access logger
|
14
|
+
def logger
|
15
|
+
@logger ||= Logger.new(STDERR)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'base64'
|
3
|
+
require 'brother_escp/version'
|
4
|
+
require 'brother_escp/image'
|
5
|
+
require 'brother_escp/sequence'
|
6
|
+
|
7
|
+
module BrotherEscp
|
8
|
+
# Main class to manage document
|
9
|
+
# Instanciate one for each printing
|
10
|
+
class Document
|
11
|
+
# @param log_level set log level, default to info
|
12
|
+
def initialize(log_level: Logger::INFO)
|
13
|
+
@data = String.new('')
|
14
|
+
sequence(:HW_SET_ESCP_MODE)
|
15
|
+
sequence(:HW_INIT)
|
16
|
+
|
17
|
+
BrotherEscp.logger.level = log_level
|
18
|
+
end
|
19
|
+
|
20
|
+
# Write data to the document
|
21
|
+
# @param data [String] raw data to send to the printer
|
22
|
+
def write(data)
|
23
|
+
@data << data
|
24
|
+
end
|
25
|
+
|
26
|
+
# Add an image to the document
|
27
|
+
# This will only work for PNG files, the alpha layer will be ignored
|
28
|
+
# @param file_name [String] file name of the image file
|
29
|
+
# @param density [Symbol] set the image density, default to single_density, possible values are: :single_density, :high_density, :higher_density
|
30
|
+
def image(file_name:, density: :single_density)
|
31
|
+
img = BrotherEscp::Image.new(file_name: file_name, converter: density)
|
32
|
+
write(img.convert.to_escp)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Add a pre-defined sequence to the document
|
36
|
+
# @see BrotherEscp::Sequence#sequence
|
37
|
+
def sequence(value)
|
38
|
+
write(BrotherEscp::Sequence.sequence(value))
|
39
|
+
end
|
40
|
+
|
41
|
+
# Helper method to set the line feed size
|
42
|
+
# @param n [Integer] line feed size in dots
|
43
|
+
def line_feed_size=(n)
|
44
|
+
write(BrotherEscp::Sequence.line_feed_size(n))
|
45
|
+
end
|
46
|
+
|
47
|
+
# Helper method to set the page length
|
48
|
+
# @param n [Integer] page length size in dots
|
49
|
+
def page_length=(n)
|
50
|
+
write(BrotherEscp::Sequence.page_length(n))
|
51
|
+
end
|
52
|
+
|
53
|
+
# Inspect the data
|
54
|
+
# @return [String] the hexadecimal string representation of the data
|
55
|
+
def inspect
|
56
|
+
to_escp.chars.map { |c| "0x#{c.unpack('H*').first}" }.join(' ')
|
57
|
+
end
|
58
|
+
|
59
|
+
# Return the raw data
|
60
|
+
# @return [String] the raw data
|
61
|
+
def to_escp
|
62
|
+
@data
|
63
|
+
end
|
64
|
+
|
65
|
+
# Return base64 encoded data
|
66
|
+
# @return [String] the base64 encoded data
|
67
|
+
def to_base64
|
68
|
+
Base64.strict_encode64 to_escp
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'chunky_png'
|
3
|
+
require 'logger'
|
4
|
+
require 'brother_escp/image_converter'
|
5
|
+
|
6
|
+
module BrotherEscp
|
7
|
+
# Main classe to manipulate images
|
8
|
+
class Image
|
9
|
+
attr_reader :image
|
10
|
+
attr_reader :converter
|
11
|
+
|
12
|
+
# @param file_name file name of the image to load
|
13
|
+
# @param converter [Symbol] set the image density, default to single_density, possible values are: :single_density, :high_density, :higher_density
|
14
|
+
def initialize(file_name:, converter: :single_density)
|
15
|
+
load(file_name: file_name)
|
16
|
+
|
17
|
+
load_converter(converter)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Print some meta data to current image
|
21
|
+
def inspect
|
22
|
+
puts "height : #{@image.height}"
|
23
|
+
puts "width : #{@image.width}"
|
24
|
+
puts "metadata : #{@image.metadata}"
|
25
|
+
@lines&.each_with_index do |e, i|
|
26
|
+
puts "#{i}: #{e.inspect}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Run the convert process. Create internally an array of lines
|
31
|
+
# @return self
|
32
|
+
def convert
|
33
|
+
@lines = converter.convert(image: image)
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
# Give the image data converted to the printer format
|
38
|
+
def to_escp
|
39
|
+
@lines.map { |line| convert_line_to_escp(line) }.join
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def load_converter(converter)
|
45
|
+
@converter = BrotherEscp::ImageConverter.converter(density: converter)
|
46
|
+
end
|
47
|
+
|
48
|
+
def convert_line_to_escp(line)
|
49
|
+
converter.convert_line_to_escp(line: line) + BrotherEscp::Sequence.crlf
|
50
|
+
end
|
51
|
+
|
52
|
+
def load(file_name:)
|
53
|
+
@image = ChunkyPNG::Image.from_file(file_name)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module BrotherEscp
|
3
|
+
# Image conversion logic
|
4
|
+
class ImageConverter
|
5
|
+
# printer parameters for each supported density
|
6
|
+
CONVERTER_PARAMS = {
|
7
|
+
single_density: { density_code: 0, line_height_in_bytes: 1 },
|
8
|
+
high_density: { density_code: 39, line_height_in_bytes: 3 },
|
9
|
+
higher_density: { density_code: 72, line_height_in_bytes: 6 }
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
# Create a new instance of an image
|
13
|
+
# @param density [Symbol] set the image density, default to single_density, possible values are: :single_density, :high_density, :higher_density
|
14
|
+
# @return [BrotherEscp::ImageConverter] instance
|
15
|
+
def self.converter(density: :single_density)
|
16
|
+
params = CONVERTER_PARAMS[density]
|
17
|
+
raise "Unknown converter (#{density})" unless params
|
18
|
+
new(density_code: params[:density_code], line_height_in_bytes: params[:line_height_in_bytes])
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :density_code
|
22
|
+
attr_reader :line_height_in_bytes
|
23
|
+
attr_reader :line_height_in_pixels
|
24
|
+
|
25
|
+
# @param density_code [Integer] density code
|
26
|
+
# @param line_height_in_bytes [Integer] number of bytes by line
|
27
|
+
def initialize(density_code: 0, line_height_in_bytes: 1)
|
28
|
+
@density_code = density_code
|
29
|
+
@line_height_in_bytes = line_height_in_bytes
|
30
|
+
@line_height_in_pixels = line_height_in_bytes * 8
|
31
|
+
end
|
32
|
+
|
33
|
+
# Main conversion method from image data to printer bitmap format
|
34
|
+
# @param image [ChunkyPNG::Image] Image to convert
|
35
|
+
# @return [Array] return an array of array, one array for each line, with one element for each byte
|
36
|
+
def convert(image:)
|
37
|
+
check_image(image: image)
|
38
|
+
lines = []
|
39
|
+
line_count = (image.height / line_height_in_pixels)
|
40
|
+
# BrotherEscp.logger.debug "convert, line_count = #{line_count}"
|
41
|
+
0.upto(line_count - 1) do |line_index|
|
42
|
+
lines << create_line(image: image, line_index: line_index)
|
43
|
+
end
|
44
|
+
lines
|
45
|
+
end
|
46
|
+
|
47
|
+
# Convert a line array to a raw data
|
48
|
+
# @param line [Array] Array of bytes representing one line
|
49
|
+
def convert_line_to_escp(line:)
|
50
|
+
n1 = line.length / line_height_in_bytes % 256
|
51
|
+
n2 = line.length / line_height_in_bytes / 256
|
52
|
+
BrotherEscp.logger.debug "convert_line_to_escp, length = #{line.length}, n1 = #{n1}, n2 = #{n2}"
|
53
|
+
data = [0x1b, 0x2a, density_code, n1, n2]
|
54
|
+
data += line
|
55
|
+
data.pack('C*')
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def check_image(image:)
|
61
|
+
remain = image.height % line_height_in_pixels
|
62
|
+
BrotherEscp.logger.warn { "the height (#{image.height}) is not a multiple if #{line_height_in_pixels}, the last #{remain} lines will be ignored." } if remain.positive?
|
63
|
+
end
|
64
|
+
|
65
|
+
def create_line(image:, line_index:)
|
66
|
+
line = []
|
67
|
+
0.upto(image.width - 1) do |x|
|
68
|
+
0.upto(line_height_in_bytes - 1) do |byte_offset|
|
69
|
+
line << create_bits(image: image, line_index: line_index, x: x, byte_offset: byte_offset)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
line
|
73
|
+
end
|
74
|
+
|
75
|
+
def create_bits(image:, line_index:, x:, byte_offset:)
|
76
|
+
bits = String.new('')
|
77
|
+
0.upto(7) do |y_offset|
|
78
|
+
y = (line_index * line_height_in_pixels) + (byte_offset * 8) + y_offset
|
79
|
+
bit = convert_pixel_to_bw(image[x, y])
|
80
|
+
# BrotherEscp.logger.debug "convert, line = #{line_index}, byte_offset = #{byte_offset}, x = #{x}, y = #{y}, bit: #{bit}"
|
81
|
+
bits << bit
|
82
|
+
end
|
83
|
+
bits.to_i(2)
|
84
|
+
end
|
85
|
+
|
86
|
+
def convert_pixel_to_bw(pixel)
|
87
|
+
r = ChunkyPNG::Color.r(pixel)
|
88
|
+
g = ChunkyPNG::Color.g(pixel)
|
89
|
+
b = ChunkyPNG::Color.b(pixel)
|
90
|
+
a = ChunkyPNG::Color.a(pixel)
|
91
|
+
px = (r + g + b) / 3
|
92
|
+
# BrotherEscp.logger.debug "convert_pixel_to_bw: #{[r, g, b, a]}, px = #{px}"
|
93
|
+
BrotherEscp.logger.warn 'PNG images with alpha are not supported.' unless a == 255
|
94
|
+
|
95
|
+
px > 128 ? '0' : '1'
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module BrotherEscp
|
3
|
+
# This module take care of the commands and various esc sequences to send meta data to the printer
|
4
|
+
module Sequence
|
5
|
+
module_function
|
6
|
+
|
7
|
+
# predefined sequence of commands for the printer
|
8
|
+
SEQUENCES = {
|
9
|
+
|
10
|
+
# Printer hardware
|
11
|
+
HW_INIT: [0x1b, 0x40], # Clear data in buffer and reset modes
|
12
|
+
HW_SET_ESCP_MODE: [0x1B, 0x69, 0x61, 0x00], # Sets the command mode to ESC/P
|
13
|
+
|
14
|
+
# Select landscape orientation
|
15
|
+
LANDSCAPE_ON: [0x1b, 0x69, 0x4c, 1],
|
16
|
+
LANDSCAPE_OFF: [0x1b, 0x69, 0x4c, 0],
|
17
|
+
|
18
|
+
# Line feed commands
|
19
|
+
LINE_FEED: [0x1b, 0x33],
|
20
|
+
|
21
|
+
# Specify page length
|
22
|
+
PAGE_LENGTH: [0x1b, 0x28, 0x43, 2, 0],
|
23
|
+
|
24
|
+
# Feed control sequences
|
25
|
+
CTL_LF: [0x0a], # Print and line feed
|
26
|
+
CTL_FF: [0x0c], # Form feed
|
27
|
+
CTL_CR: [0x0d], # Carriage return
|
28
|
+
CTL_HT: [0x09], # Horizontal tab
|
29
|
+
CTL_VT: [0x0b], # Vertical tab
|
30
|
+
}.freeze
|
31
|
+
|
32
|
+
# Format a sequence to the raw data needed by the printer
|
33
|
+
# @param value [Symbol, Integer, Array] Symbol will take the references from the {SEQUENCES}, Integer or Array will be packed as a simple ASCII string
|
34
|
+
def sequence(value)
|
35
|
+
val = value.is_a?(Symbol) ? SEQUENCES[value] : value
|
36
|
+
|
37
|
+
Array(val).pack('C*')
|
38
|
+
end
|
39
|
+
|
40
|
+
# helper method to issue a carriage return / line feed sequence (go to the next line)
|
41
|
+
def crlf
|
42
|
+
sequence [0x0d, 0x0a]
|
43
|
+
end
|
44
|
+
|
45
|
+
# Helper method to set the line feed size
|
46
|
+
# @param n [Integer] line feed size in dots
|
47
|
+
def line_feed_size(n)
|
48
|
+
sequence(:LINE_FEED) + sequence(n)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Helper method to set the page length
|
52
|
+
# @param n [Integer] page length size in dots
|
53
|
+
def page_length(n)
|
54
|
+
sequence(:PAGE_LENGTH) + sequence([n % 256, n / 256])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: brother_escp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Bourguignon
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-25 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: chunky_png
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Print text and images to a Brother label printer with ESC/P.
|
70
|
+
email:
|
71
|
+
- david@butterware.co.uk
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".ruby-gemset"
|
80
|
+
- ".ruby-version"
|
81
|
+
- ".travis.yml"
|
82
|
+
- CODE_OF_CONDUCT.md
|
83
|
+
- Gemfile
|
84
|
+
- LICENSE.txt
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- bin/console
|
88
|
+
- bin/setup
|
89
|
+
- brother_escp.gemspec
|
90
|
+
- examples/simple.rb
|
91
|
+
- examples/small.png
|
92
|
+
- examples/small2.png
|
93
|
+
- lib/brother_escp.rb
|
94
|
+
- lib/brother_escp/document.rb
|
95
|
+
- lib/brother_escp/image.rb
|
96
|
+
- lib/brother_escp/image_converter.rb
|
97
|
+
- lib/brother_escp/sequence.rb
|
98
|
+
- lib/brother_escp/version.rb
|
99
|
+
homepage: https://github.com/butterware/brother_escp
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
metadata: {}
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.5.1
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: Print text and images to a Brother label printer with ESC/P.
|
123
|
+
test_files: []
|