Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

Support image justification #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Adafruit_Thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ def justify(self, value):
else:
pos = 0
self.writeBytes(0x1B, 0x61, pos)
self.justification = c

# Feeds by the specified number of lines
def feed(self, x=1):
Expand Down Expand Up @@ -502,7 +503,7 @@ def underlineOn(self, weight=1):
def underlineOff(self):
self.writeBytes(27, 45, 0)

def printBitmap(self, w, h, bitmap, LaaT=False):
def printBitmap(self, w, h, bitmap, LaaT=False, justify=False):
rowBytes = math.floor((w + 7) / 8) # Round up to next byte boundary
if rowBytes >= 48:
rowBytesClipped = 48 # 384 pixels max width
Expand All @@ -518,16 +519,29 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
if LaaT: maxChunkHeight = 1
else: maxChunkHeight = 255

# if justify is True, respect the text justification
# setting by prefixing each row with empty bytes
padBytes = 0
if justify:
rowBytesRemaining = 48 - rowBytesClipped
if self.justification == 'R': padBytes = rowBytesRemaining
elif self.justification == 'C': padBytes = rowBytesRemaining // 2
padding = bytes([0x00] * padBytes)

i = 0
for rowStart in range(0, h, maxChunkHeight):
chunkHeight = h - rowStart
if chunkHeight > maxChunkHeight:
chunkHeight = maxChunkHeight

# Timeout wait happens here
self.writeBytes(18, 42, chunkHeight, rowBytesClipped)
self.writeBytes(18, 42, chunkHeight, rowBytesClipped + padBytes)

for y in range(chunkHeight):
if padding:
if self.writeToStdout: sys.stdout.write(padding)
else: super(Adafruit_Thermal, self).write(padding)

for x in range(rowBytesClipped):
if self.writeToStdout:
sys.stdout.write(bytes([bitmap[i]]))
Expand All @@ -547,7 +561,7 @@ def printBitmap(self, w, h, bitmap, LaaT=False):
# For any other behavior (scale, B&W threshold, etc.), use
# the Imaging Library to perform such operations before
# passing the result to this function.
def printImage(self, image_file, LaaT=False):
def printImage(self, image_file, LaaT=False, justify=False):
from PIL import Image
# image = Image.open(image_file)
image = image_file
Expand Down Expand Up @@ -576,7 +590,7 @@ def printImage(self, image_file, LaaT=False):
bit >>= 1
bitmap[n + b] = sum

self.printBitmap(width, height, bitmap, LaaT)
self.printBitmap(width, height, bitmap, LaaT, justify)

# Take the printer offline. Print commands sent after this
# will be ignored until 'online' is called.
Expand Down