@@ -447,6 +447,7 @@ def justify(self, value):
447447 else :
448448 pos = 0
449449 self .writeBytes (0x1B , 0x61 , pos )
450+ self .justification = c
450451
451452 # Feeds by the specified number of lines
452453 def feed (self , x = 1 ):
@@ -502,7 +503,7 @@ def underlineOn(self, weight=1):
502503 def underlineOff (self ):
503504 self .writeBytes (27 , 45 , 0 )
504505
505- def printBitmap (self , w , h , bitmap , LaaT = False ):
506+ def printBitmap (self , w , h , bitmap , LaaT = False , justify = False ):
506507 rowBytes = math .floor ((w + 7 ) / 8 ) # Round up to next byte boundary
507508 if rowBytes >= 48 :
508509 rowBytesClipped = 48 # 384 pixels max width
@@ -518,16 +519,29 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
518519 if LaaT : maxChunkHeight = 1
519520 else : maxChunkHeight = 255
520521
522+ # if justify is True, respect the text justification
523+ # setting by prefixing each row with empty bytes
524+ padBytes = 0
525+ if justify :
526+ rowBytesRemaining = 48 - rowBytesClipped
527+ if self .justification == 'R' : padBytes = rowBytesRemaining
528+ elif self .justification == 'C' : padBytes = rowBytesRemaining // 2
529+ padding = bytes ([0x00 ] * padBytes )
530+
521531 i = 0
522532 for rowStart in range (0 , h , maxChunkHeight ):
523533 chunkHeight = h - rowStart
524534 if chunkHeight > maxChunkHeight :
525535 chunkHeight = maxChunkHeight
526536
527537 # Timeout wait happens here
528- self .writeBytes (18 , 42 , chunkHeight , rowBytesClipped )
538+ self .writeBytes (18 , 42 , chunkHeight , rowBytesClipped + padBytes )
529539
530540 for y in range (chunkHeight ):
541+ if padding :
542+ if self .writeToStdout : sys .stdout .write (padding )
543+ else : super (Adafruit_Thermal , self ).write (padding )
544+
531545 for x in range (rowBytesClipped ):
532546 if self .writeToStdout :
533547 sys .stdout .write (bytes ([bitmap [i ]]))
@@ -547,7 +561,7 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
547561 # For any other behavior (scale, B&W threshold, etc.), use
548562 # the Imaging Library to perform such operations before
549563 # passing the result to this function.
550- def printImage (self , image_file , LaaT = False ):
564+ def printImage (self , image_file , LaaT = False , justify = False ):
551565 from PIL import Image
552566 # image = Image.open(image_file)
553567 image = image_file
@@ -576,7 +590,7 @@ def printImage(self, image_file, LaaT=False):
576590 bit >>= 1
577591 bitmap [n + b ] = sum
578592
579- self .printBitmap (width , height , bitmap , LaaT )
593+ self .printBitmap (width , height , bitmap , LaaT , justify )
580594
581595 # Take the printer offline. Print commands sent after this
582596 # will be ignored until 'online' is called.
0 commit comments