diff --git a/src/escpos/constants.py b/src/escpos/constants.py index 9ec5eb7..e4dd944 100644 --- a/src/escpos/constants.py +++ b/src/escpos/constants.py @@ -112,6 +112,13 @@ TXT_INVERT_OFF = GS + b'\x42\x00' # Inverse Printing OFF TXT_COLOR_BLACK = ESC + b'\x72\x00' # Default Color TXT_COLOR_RED = ESC + b'\x72\x01' # Alternative Color (Usually Red) +# Spacing +LINESPACING_RESET = ESC + b'2' +LINESPACING_FUNCS = { + 60: ESC + b'A', # line_spacing/60 of an inch, 0 <= line_spacing <= 85 + 360: ESC + b'+', # line_spacing/360 of an inch, 0 <= line_spacing <= 255 + 180: ESC + b'3', # line_spacing/180 of an inch, 0 <= line_spacing <= 255 +} # Char code table CHARCODE_PC437 = ESC + b'\x74\x00' # USA: Standard Europe @@ -198,7 +205,7 @@ QR_ECLEVEL_L = 0 QR_ECLEVEL_M = 1 QR_ECLEVEL_Q = 2 QR_ECLEVEL_H = 3 - + # QRcode models QR_MODEL_1 = 1 QR_MODEL_2 = 2 diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index c0df537..c102e2c 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -565,6 +565,35 @@ class Escpos(object): else: self._raw(TXT_INVERT_OFF) + def line_spacing(self, spacing=None, divisor=180): + """ Set line character spacing. + + If no spacing is given, we reset it to the default. + + There are different commands for setting the line spacing, using + a different denominator: + + '+'' line_spacing/360 of an inch, 0 <= line_spacing <= 255 + '3' line_spacing/180 of an inch, 0 <= line_spacing <= 255 + 'A' line_spacing/60 of an inch, 0 <= line_spacing <= 85 + + Some printers may not support all of them. The most commonly + available command (using a divisor of 180) is chosen. + """ + if spacing is None: + self._raw(LINESPACING_RESET) + return + + if divisor not in LINESPACING_FUNCS: + raise ValueError("divisor must be either 360, 180 or 60") + if (divisor in [360, 180] \ + and (not(0 <= spacing <= 255))): + raise ValueError("spacing must be a int between 0 and 255 when divisor is 360 or 180") + if divisor == 60 and (not(0 <= spacing <= 85)): + raise ValueError("spacing must be a int between 0 and 85 when divisor is 60") + + self._raw(LINESPACING_FUNCS[divisor] + six.int2byte(spacing)) + def cut(self, mode=''): """ Cut paper. diff --git a/test/test_functions.py b/test/test_functions.py new file mode 100644 index 0000000..22ee737 --- /dev/null +++ b/test/test_functions.py @@ -0,0 +1,26 @@ +from nose.tools import assert_raises +from escpos.printer import Dummy + + +def test_line_spacing_code_gen(): + printer = Dummy() + printer.line_spacing(10) + assert printer.output == b'\x1b3\n' + + +def test_line_spacing_rest(): + printer = Dummy() + printer.line_spacing() + assert printer.output == b'\x1b2' + + +def test_line_spacing_error_handling(): + printer = Dummy() + with assert_raises(ValueError): + printer.line_spacing(99, divisor=44) + with assert_raises(ValueError): + printer.line_spacing(divisor=80, spacing=86) + with assert_raises(ValueError): + printer.line_spacing(divisor=360, spacing=256) + with assert_raises(ValueError): + printer.line_spacing(divisor=180, spacing=256) \ No newline at end of file