Skip to content

Commit

Permalink
simple codes
Browse files Browse the repository at this point in the history
  • Loading branch information
qinmingyuan committed Nov 7, 2024
1 parent 4619dc2 commit 74b31ba
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 172 deletions.
168 changes: 167 additions & 1 deletion app/prints/base_esc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def write(data)
escpos_data = data.respond_to?(:to_escpos) ? data.to_escpos : data
@data << escpos_data.force_encoding("ASCII-8BIT")
end
alias :<< :write

def partial_cut!
@data << EscHelper.sequence(Escpos::PAPER_PARTIAL_CUT)
Expand All @@ -29,4 +28,171 @@ def to_escpos
@data
end

# Encodes UTF-8 string to encoding acceptable for the printer
# The printer must be set to that encoding
# Available encodings can be listed in console using Encoding.constants
def encode(data, opts = {})
data.encode(opts.fetch(:encoding), 'UTF-8', {
invalid: opts.fetch(:invalid, :replace),
undef: opts.fetch(:undef, :replace),
replace: opts.fetch(:replace, '?')
})
end

# Set printer encoding
# example: encoding(Escpos::CP_ISO8859_2)
def encoding(data)
[
sequence(Escpos::CP_SET),
sequence(data)
].join
end

def text(data)
[
sequence(Escpos::TXT_NORMAL),
data,
sequence(Escpos::TXT_NORMAL),
].join
end

def double_height(data)
[
sequence(Escpos::TXT_2HEIGHT),
data,
sequence(Escpos::TXT_NORMAL),
].join
end

def quad_text(data)
[
sequence(Escpos::TXT_4SQUARE),
data,
sequence(Escpos::TXT_NORMAL),
].join
end

def double_width(data)
[
sequence(Escpos::TXT_2WIDTH),
data,
sequence(Escpos::TXT_NORMAL),
].join
end

def underline(data)
[
sequence(Escpos::TXT_UNDERL_ON),
data,
sequence(Escpos::TXT_UNDERL_OFF),
].join
end

def underline2(data)
[
sequence(Escpos::TXT_UNDERL2_ON),
data,
sequence(Escpos::TXT_UNDERL_OFF),
].join
end

def bold(data)
[
sequence(Escpos::TXT_BOLD_ON),
data,
sequence(Escpos::TXT_BOLD_OFF),
].join
end

def left(data = '')
[
sequence(Escpos::TXT_ALIGN_LT),
data,
sequence(Escpos::TXT_ALIGN_LT),
].join
end

def right(data = '')
[
sequence(Escpos::TXT_ALIGN_RT),
data,
sequence(Escpos::TXT_ALIGN_LT),
].join
end

def center(data = '')
[
sequence(Escpos::TXT_ALIGN_CT),
data,
sequence(Escpos::TXT_ALIGN_LT),
].join
end

def inverted(data)
[
sequence(Escpos::TXT_INVERT_ON),
data,
sequence(Escpos::TXT_INVERT_OFF),
].join
end

def black
[
sequence(Escpos::TXT_COLOR_BLACK),
data,
sequence(Escpos::TXT_COLOR_BLACK),
].join
end

def red
[
sequence(Escpos::TXT_COLOR_BLACK),
data,
sequence(Escpos::TXT_COLOR_RED),
].join
end

def barcode(data, opts = {})
text_position = opts.fetch(:text_position, Escpos::BARCODE_TXT_OFF)
possible_text_positions = [
Escpos::BARCODE_TXT_OFF,
Escpos::BARCODE_TXT_ABV,
Escpos::BARCODE_TXT_BLW,
Escpos::BARCODE_TXT_BTH
]
unless possible_text_positions.include?(text_position)
raise ArgumentError("Wrong text position.")
end
height = opts.fetch(:height, 50)
if height && (height < 1 || height > 255)
raise ArgumentError("Height must be in range from 1 to 255.")
end
width = opts.fetch(:width, 3)
if width && (width < 2 || width > 6)
raise ArgumentError("Width must be in range from 2 to 6.")
end
[
sequence(text_position),
sequence(Escpos::BARCODE_WIDTH),
sequence([width]),
sequence(Escpos::BARCODE_HEIGHT),
sequence([height]),
sequence(opts.fetch(:format, Escpos::BARCODE_EAN13)),
data
].join
end

def partial_cut
sequence(Escpos::PAPER_PARTIAL_CUT)
end

def cut
sequence(Escpos::PAPER_FULL_CUT)
end

# Transforms an array of codes into a string
def sequence(*arr_sequence)
arr_sequence.flatten.pack('U*')
end

end
171 changes: 0 additions & 171 deletions app/prints/concerns/esc_helper.rb

This file was deleted.

0 comments on commit 74b31ba

Please sign in to comment.