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

Orientation module #4

Merged
merged 10 commits into from
Jun 29, 2016
1 change: 1 addition & 0 deletions lib/zebra/zpl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
require "zebra/zpl/barcode_type"
require "zebra/print_job"
require "zebra/zpl/qrcode"
require "zebra/zpl/justification"
2 changes: 1 addition & 1 deletion lib/zebra/zpl/barcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def to_zpl
check_attributes
human_readable = print_human_readable_code ? "Y" : "N"
# ["B#{x}", y, rotation, type, narrow_bar_width, wide_bar_width, height, human_readable, "\"#{data}\""].join(",")
"^FO#{x},#{y}^FB600,4,0,C,0^B#{type}#{rotation},#{height},#{human_readable},N,N^FD#{data}"
"^FO#{x},#{y}^FB800,4,0,C,0^B#{type}#{rotation},#{height},#{human_readable},N,N^FD#{data}"
end

private
Expand Down
23 changes: 22 additions & 1 deletion lib/zebra/zpl/font.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,33 @@ class InvalidFontSizeError < StandardError; end
SIZE_9 = 133 # 48pt

def self.valid_font_size?(font_size)
[17, 22, 28, 33, 44, 67, 100, 111, 133].include?(font_size.to_i) || ('A'..'Z').include?(font_size)
[17, 22, 28, 33, 44, 67, 100, 111, 133].include?(font_size.to_i)
end

def self.validate_font_size(font_size)
raise InvalidFontSizeError unless valid_font_size?(font_size)
end
end

module FontType
class InvalidFontTypeError < StandardError; end

TYPE_0 = "0" # 6pt
TYPE_CD = "CD" # 6pt
TYPE_A = "A" # 6pt
TYPE_B = "B" # 6pt
TYPE_E = "E" # 6pt
TYPE_F = "F" # 6pt
TYPE_G = "G" # 6pt
TYPE_H = "H" # 6pt

def self.valid_font_type?(font_type)
["0", "CD", "A", "B", "E", "F", "G", "H"].include?(font_type)
end

def self.validate_font_type(font_type)
raise InvalidFontTypeError unless valid_font_type?(font_type)
end
end
end
end
21 changes: 21 additions & 0 deletions lib/zebra/zpl/justification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Zebra
module Zpl
module Justification
class InvalidJustificationError < StandardError; end

# ZPL-supported values ("L" is default)
LEFT = 'L'
RIGHT = 'R'
CENTER = 'C'
JUSTIFIED = 'J'

def self.valid_justification?(justification)
[LEFT, RIGHT, CENTER, JUSTIFIED].include? justification
end

def self.validate_justification(justification)
raise InvalidJustificationError unless valid_justification?(justification)
end
end
end
end
9 changes: 9 additions & 0 deletions lib/zebra/zpl/printable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ def position=(coords)
@position, @x, @y = coords, coords[0], coords[1]
end

def justification=(just)
Justification.validate_justification just
@justification = just
end

def justification
@justification || Justification::LEFT
end

def rotation=(rot)
Rotation.validate_rotation rot
@rotation = rot
Expand Down
13 changes: 11 additions & 2 deletions lib/zebra/zpl/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@ module Zpl
class Text
include Printable

attr_reader :font_size
attr_reader :font_size, :font_type

def font_size=(f)
FontSize.validate_font_size f
@font_size = f
end

def font_type=(type)
FontType.validate_font_type type
@font_type = type
end

def font_type
@font_type || FontType::TYPE_0
end

def print_mode=(mode)
PrintMode.validate_mode mode
@print_mode = mode
Expand Down Expand Up @@ -48,7 +57,7 @@ def to_zpl
# ["A#{x}", y, rotation, font_size, h_multiplier, v_multiplier, print_mode, "\"#{data}\""].join(",")
# "^FO25,25^FB600,100,0,C,0^FDFoo^FS"

"^CF0,#{font_size},#{font_size}^FO#{x},#{y}^FB600,4,0,L,0^FD#{data}^FS"
"^CF#{font_type},#{font_size}^FO#{x},#{y}^FB800,4,0,#{justification},0^FD#{data}^FS"
end

private
Expand Down