From 07d47765aad69c6ee5512e3b0580b9ce1f7e95e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Elsd=C3=B6rfer?= Date: Fri, 26 Aug 2016 10:38:36 +0200 Subject: [PATCH] Allow linespacing reset. Make this the default. --- src/escpos/constants.py | 1 + src/escpos/escpos.py | 8 +++++++- test/test_functions.py | 8 +++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/escpos/constants.py b/src/escpos/constants.py index 703e48b..999dba1 100644 --- a/src/escpos/constants.py +++ b/src/escpos/constants.py @@ -102,6 +102,7 @@ TXT_INVERT_ON = GS + b'\x42\x01' # Inverse Printing ON TXT_INVERT_OFF = GS + b'\x42\x00' # Inverse Printing OFF # 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 diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index 99c9308..797b5ed 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -565,9 +565,11 @@ class Escpos(object): else: self._raw(TXT_INVERT_OFF) - def line_spacing(self, spacing=30, divisor=180): + 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: @@ -578,6 +580,10 @@ class Escpos(object): 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] \ diff --git a/test/test_functions.py b/test/test_functions.py index a78cc14..f35de54 100644 --- a/test/test_functions.py +++ b/test/test_functions.py @@ -8,10 +8,16 @@ def test_line_spacing_code_gen(): assert printer.output == '\x1b3\n' +def test_line_spacing_rest(): + printer = Dummy() + printer.line_spacing() + assert printer.output == '\x1b2' + + def test_line_spacing_error_handling(): printer = Dummy() with assert_raises(ValueError): - printer.line_spacing(divisor=44) + printer.line_spacing(99, divisor=44) with assert_raises(ValueError): printer.line_spacing(divisor=80, spacing=86) with assert_raises(ValueError):