-----=====ESC/P=====-----
=====Introduction=====
Epson created the ESC/P language for it's Bit-Image Printers,
and ESC/P became an industry standard. Many printers, Epson
and others, support ESC/P or variants. Not only that, but even
today many printers recognize ESC/P or the revised version of
the language, ESC/P2. Many Epson Laser Printers support ESC/P2
in emulation modes.
Many old IBM printers use ESC/P and Star Micronics Gemini-10 used
a superset of ESC/P. Unfortunately, many manufacturers added extensions
to the variant of ESC/P supported by their printers, and those extensions are
often incompatible with ESC/P2. Therefore, separate drivers may be necessary,
despite the fact that ESC/P and ESC/P2 are supposed to be compatible, if these
older printers are also to be supported.

=====Notes=====
(Unless otherwise specified, spaces are NOT literal. "SP" represents
a literal space.)
(ESC/P supported some ANSI control codes, as well as defining a few
of it's own.)
(ESC/P and ESC/P2 are mostly compatible, but separate drivers may be
easier since some manufacturers added extensions to the variant of ESC/P
that their printers supported, and these extensions are quite often incompatible
with ESC/P2.)

=====Control Codes=====
The below codes are single-character control codes recognized by ESC/P.
See also the ANSI codes for definitions of each of these.
Those which are "no longer supported" do nothing in ESC/P2 but still work
just fine on older ESC/P printers.

BEL - Sounds audible bell/beep tone (no longer supported)
BS - Backspace (no longer supported)
HT - Horizontal Tab
LF - Linefeed
VT - Vertical Tab
FF - Form Feed
CR - Carriage Return
SO - Enlarged print on
SI - Condensed print on
DC1 - Select Printer (no longer supported)
DC2 - Condensed print off
DC3 - Deselect Printer (no longer supported)
DC4 - Enlarged print off
CAN - Cancel Line (no longer supported)
DEL - No operation (no longer supported)

=====Printer Initialization/Special Modes=====
ESC @ - Initialize Printer
ESC < - Uni-directional printing only
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC s n - Half-speed printing (more accurate, precise, smooth, etc.)
                   n = 0 or 48: Half-speed printing off
                   n = 1 or 49: Half-speed printing on
ESC i - Immediate print (typewriter mode)
ESC j - Immediate temporary reverse paper feed
ESC U n - Uni-directional printing on/off
                    n = 0 or 48: Bi-directional printing on
                    n = 1 or 49: Uni-directional printing on

=====Font Options=====
ESC SO - Enlarged print on
ESC SI - Condensed print on
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC p n - Proportional printing
                   n = 0 or 48: Proportional printing off
                   n = 1 or 49: Proportional printing on
(The below command includes a "-" and a variable number n.
Spaces are, once again, not literal. The code is therefore [1B/2D/n])
ESC - n - Underline mode on/off
                  if n = 0 or 48, then underline mode = off
                  if n = 1 or 49, then underline mode = on
ESC 4 - Italics on
ESC 5 - Italics off
ESC E - Emphasized mode on
ESC F - Emphasized mode off
ESC G - Double-strike mode on
ESC H - Double-strike mode off
ESC P - Pica-sized characters on (Elite mode off)
ESC M - Elite-sized characters on (Pica mode off)
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC S n - Superscript/Subscript mode on
                    n = 0 or 48: Superscript mode on
                    n = 1 or 49: Subscript mode on
ESC T - Superscript/Subscript off
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC W n - Enlarged (double-width) mode on
                    n = 0 or 48: Enlarged print mode on
                    n = 1 or 49: Enlarged print mode off

=====Font Select=====
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC R n - Set the international character set
                    n = 0: USA
                    n = 1: France
                    n = 2: Germany
                    n = 3: England
                    n = 4: Denmark I
                    n = 5: Sweden
                    n = 6: Italy
                    n = 7: Spain
                    n = 8: Japan
                    n = 9: Norway
                    n = 10: Denmark II
ESC k - Select family of type styles
(The following command has a variable, n. Spaces are, as always, not literal.)

=====Paper/Page Control=====
ESC EM - Cut sheet feeder control
ESC 8 - Turn off paper-end detector
ESC 9 - Turn on paper-end detector
ESC z - Selects letter quality or draft

=====Print Head Control/Whitespace=====
ESC / - Set vertical tab
ESC \ - Move printhead
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC Q n - Set the right margin at column n
ASC B - Set vertical tab stop
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC C n - Set form length to n inches (n between 1 and 22)
ESC D - Set horizontal tab stop
ESC $ - Set absolute horizontal tab
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC l n - Sets left margin at column n
ESC a - Justification
ESC b - Set vertical tab

=====Graphics=====
(The following command is ESC/P only)
ESC * n - Enter graphics mode (n specifies resolution)
                   n = 0: normal density
                   n = 1: dual density
                   n = 2: double-speed dual density
                   n = 3: quadruple density
                   n = 4: CRT graphics
                   n = 6: CRT graphics II
ESC ^ - Nine-pin graphics mode
ESC K - Normal density graphics mode
ESC L - Dual-density graphics mode
ESC Y - Double-speed dual-density graphics mode
ESC Z - Quadruple density graphics mode

=====Line Spacing=====
ESC 0 - Set line spacing to 1/8"
ESC 1 - Set line spacing to 7/72"
ESC 2 - Set line spacing to 1/6"
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC 3 n - Set line spacing to n/216" (n between 0 and 255)
ESC A n - Set line spacing to n/72" (n between 0 and 85)
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC J n - Tentative n/216" line spacing

=====User Font=====
(The following command has a space character; it's hex code is [1B/20])
ESC SP - Selects character space
ESC % - Set active character set
ESC : - Copy ROM to user RAM
ESC & - Define user character(s)
(Remember: spaces are NOT literal, so the below code is [1B/28/74])
ESC ( t - Assigns character table
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC I - Controls code select
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC m n - Special character generator selection
                    n = 0: Control codes accepted
                    n = 4: Graphics characters accepted

=====Miscellaneous=====
(The following command has a variable, n. Spaces are, as always, not literal.)
ESC N n - Set number of lines to skip-over perforation (n between 1 and 127)
ESC O - Turn off skip-over perforation
ESC g - Select 15 width
ESC ! - Selects mode combinations
ESC # - Do not change MSB bit of bytes sent from CPU- ignore bit 8
ESC > - Always clear MSB bit of bytes sent from CPU
ESC = - Always set MSB bit of bytes sent from CPU
ESC 6 - Deactivate high-order control codes
ESC 7 - Restore high-order control codes

=====ESC/P2 Codes=====
The following are codes that are only supported by ESC/P2.
Remember, spaces are NOT literal.

(Each of the below commands has three characters, the first two of which
are ESC and "(", which are [1B/28] hexadecimal)
ESC ( - - Selects line/score
ESC ( ^ - Prints data as characters
ESC ( C - Sets page length (defined unit)
ESC ( G - Selects graphics mode
ESC ( U - Sets unit
ESC ( V - Sets absolute vertical position
ESC ( c - Sets page format
ESC ( i n - Microweave mode on (n=1) or off (n=0)
ESC ( v - Sets relative vertical position

=====Special Modes=====
-----Graphics-----
ESC/P had the commands
        ESC K - Normal density graphics mode (480dpi horizontally)
        ESC L - Dual-density graphics mode (960dpi vertically)
        ESC Y - Double-speed dual-density graphics mode
        ESC Z - Quadruple density graphics mode
for entering graphics mode. Dual-density was twice as slow as normal
density, unless ESC Y was used (instead of ESC L). Quadruple density
did not have a "double-speed" mode. Slower modes were slower,
but usually more accurate and precise. Often when printing graphics,
if the user is not in a hurry, half-speed printing may be turned on to increase
the quality of the output.
A 24-wire printer prints 3 lines of graphical data at a time: the first byte for the
top 8 wires, the second for the middle 8, and the last for the bottom 8.
Since ESC/P printers were monochrome, each bit represents one dot, either
black (if 1) or blank (if 0).
48-wire printers use 6 bytes, and 9 wire printers use one byte per column.
Divide the number of wires in a printer by 8, to find the number of bytes per
column, where a column is 8 dots wide.
Each graphics command is followed by two bytes, which specify the number
of bytes of graphical data that will follow.

ESC/P2 printers still support the ESC K, ESC L, ESC Y, and ESC Z commands.
However, Epson recommends using the ESC * commands, which supports
all four modes plus some others.
ESC * is followed by a number indicating the graphics mode, then the two bytes
indicating the length of the graphics data following, and finally, the graphics data
encoded.
Here is a list of all the graphics mode numbers, with details on each mode.

MODE    Horizontal DPI      Vertical DPI      Print adjacent dots?    Dots per column
                                                24-wire  48-wire
---------------------------------------------------------------------------------------------------------------------
0               60                          60           60             Yes                                   8
1               120                        60           60             Yes                                   8
2               120                        60           60             No                                     8
3               240                        60           60             No                                     8
4               80                          60           60             Yes                                   8
6               90                          60           60             Yes                                   8
32             60                         180        180            Yes                                   24
33             120                       180        180            Yes                                   24
38             90                         180        180            Yes                                   24
39            180                        180        180            Yes                                   24
40            360                        180        180             No                                    24
64             60                          N/A        60             Yes                                   48
65            120                         N/A       120            Yes                                   48
70             90                          N/A        180            Yes                                   48
71            180                          N/A       360            Yes                                   48
72            360                          N/A       360             No                                    48
73            180                          N/A       360            Yes                                   48

To use a graphics mode, send
            ESC * m n1 n2 ...
To the printer, where m is the mode number and n1 is the high-order byte and n2
is the low-order byte of the length (in bytes) of the graphical data being sent.

Newer ESC/P2 printers also support the "ESC ." (space is non-literal)
command, for raster graphics at very high resolutions.

-----Font download-----
ESC/P also supports downloadable fonts. The user can copy the ROM
font into user RAM in order to define only some characters, leaving others at
their defaults. To do this, send
        ESC : NUL n NUL
where n = 0 for Roman or 1 for Sans Serif.
Remember, spaces are not literal.
Then define the characters by sending the data
        ESC & NUL n1 n2 a1 d1...dn
n1 and n2 give the range of characters to be defined: multiple characters
may be defined with a single command. For 9-pin printers, only the top 8
or the bottom 8 rows may be used. The bottom or top 9th row is left blank
and unused. a1 is to tell the printer whether the character being sent will use
the top 8 rows (leaving the bottom one blank) or the bottom 8 rows (leaving
the top one blank).
If a character has descenders, leave the top row blank. Otherwise, it is usually
best to leave the bottom row blank.
a1 also has an extra value for starting and ending columns when proportionally
spaced characters are enabled, so here is a table to be used for calculating
a1:

First Column      Weight      Last Column      Weight      Pin Group      Weight
----------------------------------------------------------------------------------------------------------
0                            0                  1                            1                 Top 8               128
1                           16                 2                            2                 Bottom 8         0
2                           32                 3                            3
3                           48                 4                            4
4                           64                 5                            5
5                           80                 6                            6
6                           96                 7                            7
7                          112                8                            8
                                                   9                            9
                                                  10                          10
                                                  11                          11

Add the weights together to get a1. If proportionally spaced fonts are not
enabled, then a1 will always be either 128 or 0, indicating whether the upper
or lower 8 columns will be used.

Each character is sent as 11 bytes of data; the first byte is the leftmost
column, the next byte the next column to the right, and so on. There are 11
columns of 8 dots. The top row is the high order bit of each column and the
bottom row the low order bit. In older Epson printers, the speed of the printhead
and firing rate of individual wires adds a limitation: two adjacent dots in any row
cannot be printed, so a "solid" horizontal line must actually alternate between
solid and blank dots. Instead of "-----" it must be "- - -". Newer printers generally
do not have this limitation.
For text characters, the last (rightmost) two columns should be left blank; for
graphics characters, all columns may be used.