@@ -447,6 +447,7 @@ def justify(self, value):
447
447
else :
448
448
pos = 0
449
449
self .writeBytes (0x1B , 0x61 , pos )
450
+ self .justification = c
450
451
451
452
# Feeds by the specified number of lines
452
453
def feed (self , x = 1 ):
@@ -502,7 +503,7 @@ def underlineOn(self, weight=1):
502
503
def underlineOff (self ):
503
504
self .writeBytes (27 , 45 , 0 )
504
505
505
- def printBitmap (self , w , h , bitmap , LaaT = False ):
506
+ def printBitmap (self , w , h , bitmap , LaaT = False , justify = False ):
506
507
rowBytes = math .floor ((w + 7 ) / 8 ) # Round up to next byte boundary
507
508
if rowBytes >= 48 :
508
509
rowBytesClipped = 48 # 384 pixels max width
@@ -518,16 +519,29 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
518
519
if LaaT : maxChunkHeight = 1
519
520
else : maxChunkHeight = 255
520
521
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
+
521
531
i = 0
522
532
for rowStart in range (0 , h , maxChunkHeight ):
523
533
chunkHeight = h - rowStart
524
534
if chunkHeight > maxChunkHeight :
525
535
chunkHeight = maxChunkHeight
526
536
527
537
# Timeout wait happens here
528
- self .writeBytes (18 , 42 , chunkHeight , rowBytesClipped )
538
+ self .writeBytes (18 , 42 , chunkHeight , rowBytesClipped + padBytes )
529
539
530
540
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
+
531
545
for x in range (rowBytesClipped ):
532
546
if self .writeToStdout :
533
547
sys .stdout .write (bytes ([bitmap [i ]]))
@@ -547,7 +561,7 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
547
561
# For any other behavior (scale, B&W threshold, etc.), use
548
562
# the Imaging Library to perform such operations before
549
563
# passing the result to this function.
550
- def printImage (self , image_file , LaaT = False ):
564
+ def printImage (self , image_file , LaaT = False , justify = False ):
551
565
from PIL import Image
552
566
# image = Image.open(image_file)
553
567
image = image_file
@@ -576,7 +590,7 @@ def printImage(self, image_file, LaaT=False):
576
590
bit >>= 1
577
591
bitmap [n + b ] = sum
578
592
579
- self .printBitmap (width , height , bitmap , LaaT )
593
+ self .printBitmap (width , height , bitmap , LaaT , justify )
580
594
581
595
# Take the printer offline. Print commands sent after this
582
596
# will be ignored until 'online' is called.
0 commit comments