Current section
Files
Jump to
Current section
Files
src/escpos@protocol.erl
-module(escpos@protocol).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/escpos/protocol.gleam").
-export([line_feed/1, justify/1, bold/1, underline/1, double_strike/1, upside_down/1, flip/1, font/1, cut/1, reverse/1, smooth/1, character_size/2, image_to_graphics_buffer/7, print_graphics_buffer/0]).
-export_type([justify/0, cut/0, font/0, image_tone/0, image_scale/0, print_color/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Low-level ESC/POS command encoding.\n"
"\n"
" Each function returns a `BitArray` containing the raw bytes for a single\n"
" ESC/POS command. These are used internally by the `escpos` module to\n"
" build command buffers.\n"
).
-type justify() :: left | center | right.
-type cut() :: partial | full.
-type font() :: font_a |
font_b |
font_c |
font_d |
font_e |
special_font_a |
special_font_b.
-type image_tone() :: monochrome | multiple_tone.
-type image_scale() :: scale1x | scale2x.
-type print_color() :: color1 | color2 | color3 | color4.
-file("src/escpos/protocol.gleam", 74).
?DOC(" Feeds the given number of lines, clamped to 1–255 (`ESC d`).\n").
-spec line_feed(integer()) -> bitstring().
line_feed(Lines) ->
case Lines of
L when L < 2 ->
<<27, "d"/utf8, 1>>;
L@1 when L@1 > 254 ->
<<27, "d"/utf8, 255>>;
_ ->
<<27, "d"/utf8, Lines>>
end.
-file("src/escpos/protocol.gleam", 84).
?DOC(
" Sets text justification (`ESC a`). Must be at the start of a line\n"
" to take effect.\n"
).
-spec justify(justify()) -> bitstring().
justify(Justify) ->
case Justify of
left ->
<<27, "a"/utf8, 0>>;
center ->
<<27, "a"/utf8, 1>>;
right ->
<<27, "a"/utf8, 2>>
end.
-file("src/escpos/protocol.gleam", 93).
?DOC(" Enables or disables bold text (`ESC E`).\n").
-spec bold(boolean()) -> bitstring().
bold(On) ->
case On of
true ->
<<27, "E"/utf8, 1>>;
false ->
<<27, "E"/utf8, 0>>
end.
-file("src/escpos/protocol.gleam", 101).
?DOC(" Enables or disables underlined text (`ESC -`).\n").
-spec underline(boolean()) -> bitstring().
underline(On) ->
case On of
true ->
<<27, "-"/utf8, 1>>;
false ->
<<27, "-"/utf8, 0>>
end.
-file("src/escpos/protocol.gleam", 109).
?DOC(" Enables or disables double-strike text (`ESC G`).\n").
-spec double_strike(boolean()) -> bitstring().
double_strike(On) ->
case On of
true ->
<<27, "G"/utf8, 1>>;
false ->
<<27, "G"/utf8, 0>>
end.
-file("src/escpos/protocol.gleam", 125).
?DOC(" Enables or disables upside-down printing (`ESC {`).\n").
-spec upside_down(boolean()) -> bitstring().
upside_down(On) ->
case On of
true ->
<<27, "{"/utf8, 1>>;
false ->
<<27, "{"/utf8, 0>>
end.
-file("src/escpos/protocol.gleam", 141).
?DOC(" Enables or disables 180-degree rotation (`ESC V`).\n").
-spec flip(boolean()) -> bitstring().
flip(On) ->
case On of
true ->
<<27, "V"/utf8, 1>>;
false ->
<<27, "V"/utf8, 0>>
end.
-file("src/escpos/protocol.gleam", 149).
?DOC(" Selects a built-in printer font (`ESC M`).\n").
-spec font(font()) -> bitstring().
font(Font) ->
case Font of
font_a ->
<<27, "M"/utf8, 0>>;
font_b ->
<<27, "M"/utf8, 1>>;
font_c ->
<<27, "M"/utf8, 2>>;
font_d ->
<<27, "M"/utf8, 3>>;
font_e ->
<<27, "M"/utf8, 4>>;
special_font_a ->
<<27, "M"/utf8, 97>>;
special_font_b ->
<<27, "M"/utf8, 98>>
end.
-file("src/escpos/protocol.gleam", 66).
?DOC(" Paper cut command (`GS V`).\n").
-spec cut(cut()) -> bitstring().
cut(Cut) ->
case Cut of
full ->
<<29, "V"/utf8, 0>>;
partial ->
<<29, "V"/utf8, 1>>
end.
-file("src/escpos/protocol.gleam", 117).
?DOC(" Enables or disables reverse (white on black) printing (`GS B`).\n").
-spec reverse(boolean()) -> bitstring().
reverse(On) ->
case On of
true ->
<<29, "B"/utf8, 1>>;
false ->
<<29, "B"/utf8, 0>>
end.
-file("src/escpos/protocol.gleam", 133).
?DOC(" Enables or disables character smoothing (`GS b`).\n").
-spec smooth(boolean()) -> bitstring().
smooth(On) ->
case On of
true ->
<<29, "b"/utf8, 1>>;
false ->
<<29, "b"/utf8, 0>>
end.
-file("src/escpos/protocol.gleam", 162).
?DOC(" Sets character width and height 1–8 (`GS !`).\n").
-spec character_size(integer(), integer()) -> bitstring().
character_size(Width, Height) ->
W = begin
_pipe = gleam@int:clamp(Width, 1, 8),
gleam@int:subtract(_pipe, 1)
end,
H = begin
_pipe@1 = gleam@int:clamp(Height, 1, 8),
gleam@int:subtract(_pipe@1, 1)
end,
<<29, "!"/utf8, 0:1, W:3, 0:1, H:3>>.
-file("src/escpos/protocol.gleam", 170).
?DOC(
" Stores raster image data into the printer's graphics buffer\n"
" (`GS ( L`, fn=112).\n"
).
-spec image_to_graphics_buffer(
bitstring(),
integer(),
integer(),
image_tone(),
image_scale(),
image_scale(),
print_color()
) -> bitstring().
image_to_graphics_buffer(Data, Width, Height, Tone, Scale_x, Scale_y, Color) ->
Data_size = erlang:byte_size(Data),
Data_length = 10 + Data_size,
Pl = Data_length rem 256,
Ph = Data_length div 256,
Xl = Width rem 256,
Xh = Width div 256,
Yl = Height rem 256,
Yh = Height div 256,
A = case Tone of
monochrome ->
48;
multiple_tone ->
52
end,
Bx = case Scale_x of
scale1x ->
1;
scale2x ->
2
end,
By = case Scale_y of
scale1x ->
1;
scale2x ->
2
end,
C = case Color of
color1 ->
49;
color2 ->
50;
color3 ->
51;
color4 ->
52
end,
<<29,
"("/utf8,
"L"/utf8,
Pl,
Ph,
48,
112,
A,
Bx,
By,
C,
Xl,
Xh,
Yl,
Yh,
Data/bitstring>>.
-file("src/escpos/protocol.gleam", 239).
?DOC(" Prints the contents of the graphics buffer (`GS ( L`, fn=50).\n").
-spec print_graphics_buffer() -> bitstring().
print_graphics_buffer() ->
<<29, "("/utf8, "L"/utf8, 2, 0, 48, 50>>.