added pagecount

master
Your Name 6 years ago
parent bf0e171755
commit 138b75009f

@ -56,7 +56,7 @@ def _int_low_high(inp_number, out_bytes):
return outp return outp
def print(filename): def print(filename, getlines=False):
im = Image.open(filename) im = Image.open(filename)
basewidth = 350 basewidth = 350
@ -83,10 +83,17 @@ def print(filename):
height_pixels, width_pixels = im.size height_pixels, width_pixels = im.size
if getlines:
return len(blobs)
else:
with open('/dev/usb/lp0', 'wb') as fp: with open('/dev/usb/lp0', 'wb') as fp:
fp.write(ESC+b"@") fp.write(ESC+b"@")
fp.write(ESC+b"\x7B"+b"\x41") fp.write(ESC+b"\x7B"+b"\x41")
fp.write(ESC + b"3" + six.int2byte(22)); # Adjust line-feed size #fp.write(ESC + b"3" + six.int2byte(22)); # Adjust line-feed size
fp.write(ESC + b"\x33" + bytes([22]));
#fp.write(ESC + )
fp.write(CARIDGE_RET) fp.write(CARIDGE_RET)
for blob in blobs: for blob in blobs:
fp.write(GRAPHIC + _int_low_high( width_pixels, 2 ) + blob) fp.write(GRAPHIC + _int_low_high( width_pixels, 2 ) + blob)

@ -14,20 +14,42 @@ import subprocess
from subprocess import check_output from subprocess import check_output
import datetime import datetime
import time import time
import re
class Printer(): class Printer():
ESC = b'\x1B' ESC = b'\x1B'
FORMFEED = b'\x0C'
UNDERLINEDON = b'\x1B'+b'\x2D'+b'\x31'
ITALICON = b'\x1B'+b'\x34'
ITALICOFF = b'\x1B'+b'\x35'
BOLDON = b'\x1B'+b'\x45'
BOLDOFF = b'\x1B'+b'\x46'
DHON = b'\x1B'+b'\x77'+b'\x01'
DHOFF = b'\x1B'+b'\x77'+b'\x00'
COMPRON = b'\x1B'+b'\x0f'
COMPROFF = b'\x1B'+b'\x12'
CHANGE_EMULATION = ESC+b'\x7B'+b'\x65' CHANGE_EMULATION = ESC+b'\x7B'+b'\x65'
LINE_FEED = b'\n' LINE_FEED = b'\n'
CARIDGE_RET = b'\x0D' CARIDGE_RET = b'\x0D'
CHARWIDTH = 78 CHARWIDTH = 78
printedheight = 0
pages = 1
strokes = 0 strokes = 0
debug = False debug = True
target = "/dev/usb/lp0" target = "/dev/usb/lp0"
if debug: if debug:
target = "/dev/tty" target = "/dev/tty"
def formfeed(self):
with open(self.target, 'wb') as printer:
if not self.debug:
while self.checkpagebreak(printer) == False:
self.printedheight+=(44/216)
printer.write(self.LINE_FEED)
def printchar(self,char): def printchar(self,char):
with open(self.target, 'wb') as printer: with open(self.target, 'wb') as printer:
if self.debug: if self.debug:
@ -39,11 +61,13 @@ class Printer():
printer.write(self.ESC+b"\x25"+b"\x46"+b"\x0F"+b"\x00"+b"\x04"+b"\x08"+b"\x00") printer.write(self.ESC+b"\x25"+b"\x46"+b"\x0F"+b"\x00"+b"\x04"+b"\x08"+b"\x00")
self.strokes += 1 self.strokes += 1
if(self.strokes>self.CHARWIDTH): if(self.strokes>self.CHARWIDTH):
self.checkpagebreak(printer)
self.printedheight+=(44/216)
printer.write(self.LINE_FEED) printer.write(self.LINE_FEED)
self.strokes = 0 self.strokes = 0
def printstring(self,string, formating="p"): def printstring(self,string, formating="p"):
strokesinline = 0
with open(self.target, 'wb') as printer: with open(self.target, 'wb') as printer:
if self.debug: if self.debug:
if formating == "u": if formating == "u":
@ -52,10 +76,49 @@ class Printer():
printer.write(b"\x1B"+b"\x5B"+b"\x30"+b"\x6D") printer.write(b"\x1B"+b"\x5B"+b"\x30"+b"\x6D")
else: else:
printer.write(bytes(str(string), 'utf-8')) for word in re.split(r'(\s+)', string):
strokesinline += len(word)
if "\n" in word:
strokesinline = 0
if strokesinline > self.CHARWIDTH:
printer.write(self.LINE_FEED)
strokesinline = len(word)
if not (strokesinline == 1 and word == " "):
printer.write(bytes(str(word), 'utf-8'))
else: else:
printer.write(self.ESC+b"\x7B"+b"\x41") printer.write(self.ESC+b"\x7B"+b"\x41")
printer.write(bytes(str(string), 'utf-8')) printer.write(self.ESC + b"\x33" + bytes([44]));
if formating == "u":
printer.write(self.BOLDON)
if formating == "i":
printer.write(self.ITALICON)
if formating == "dh":
printer.write(self.DHON)
if formating == "compr":
printer.write(self.COMPRON)
if self.printedheight > 11.69:
printer.write(bytes(str("NEW PAGE YESY"), 'utf-8'));
self.printedheight=0
for word in re.split(r'(\s+)', string):
strokesinline += len(word)
if "\n" in word:
strokesinline = 0
self.printedheight+=(44/216)
if strokesinline > self.CHARWIDTH:
self.checkpagebreak(printer)
self.printedheight+=(44/216)
printer.write(self.LINE_FEED)
strokesinline = len(word)
if not (strokesinline == 1 and word == " "):
printer.write(bytes(str(word), 'utf-8'))
if formating == "u":
printer.write(self.BOLDOFF)
if formating == "i":
printer.write(self.ITALICOFF)
if formating == "dh":
printer.write(self.DHOFF)
if formating == "compr":
printer.write(self.COMPROFF)
printer.write(self.ESC+b"\x25"+b"\x46"+b"\x0F"+b"\x00"+b"\x04"+b"\x08"+b"\x00") printer.write(self.ESC+b"\x25"+b"\x46"+b"\x0F"+b"\x00"+b"\x04"+b"\x08"+b"\x00")
try: try:
self.newline() self.newline()
@ -66,9 +129,31 @@ class Printer():
def newline(self): def newline(self):
global strokes global strokes
with open(self.target, 'wb') as printer: with open(self.target, 'wb') as printer:
if not self.debug:
self.checkpagebreak(printer)
printer.write(self.ESC + b"\x33" + bytes([44]));
self.printedheight+=(44/216)
printer.write(self.LINE_FEED) printer.write(self.LINE_FEED)
self.strokes = 0 self.strokes = 0
def checkpagebreak(self, printer):
if self.printedheight+(44/216)*4 > 11.69:
printer.write(self.LINE_FEED)
printer.write(self.LINE_FEED)
printer.write(bytes(str("PAGE "+str(self.pages)), 'utf-8'));
printer.write(self.ESC + b"\x33" + bytes([44]));
printer.write(self.LINE_FEED)
printer.write(self.LINE_FEED)
printer.write(self.LINE_FEED)
printer.write(self.COMPRON)
printer.write(bytes(str("------------------------------------------------------ POETIC SOFTWARE PUBLICATION -----------------------------------------------------"), 'utf-8'));
printer.write(self.COMPROFF)
printer.write(self.LINE_FEED)
self.printedheight=(44/216)
self.pages+=1
return True
else:
return False
def printimagefromurl(self, url): def printimagefromurl(self, url):
page = requests.get(url) page = requests.get(url)
@ -77,11 +162,26 @@ class Printer():
with open(f_name, 'wb') as f: with open(f_name, 'wb') as f:
f.write(page.content) f.write(page.content)
if printer.debug: if self.debug:
rows, columns = os.popen('stty size', 'r').read().split() rows, columns = os.popen('stty size', 'r').read().split()
subprocess.call(["catimg","-w",columns,f_name]) #subprocess.call(["catimg","-w",columns,f_name])
subprocess.call(["feh",f_name])
else: else:
imagelines = imageprinter.print(f_name,True)
isodd = False
if imagelines % 2 == 1:
isodd = True
imagelines+=1
if self.printedheight + (22/216)*imagelines +(44/216)*4 > 11.69:
self.formfeed()
self.printedheight += (22/216)*imagelines
imageprinter.print(f_name) imageprinter.print(f_name)
if isodd:
with open('/dev/usb/lp0', 'wb') as printer:
printer.write(ESC + b"\x33" + bytes([22]));
printer.write(self.LINE_FEED)
printer.write(ESC + b"\x33" + bytes([44]));
@ -96,6 +196,7 @@ class Terminal():
"list":"list theses and exercises", "list":"list theses and exercises",
"exercise <number>":"see the exercise with the <number> specified", "exercise <number>":"see the exercise with the <number> specified",
"thesis <number>":"see the thesis with the <number> specified", "thesis <number>":"see the thesis with the <number> specified",
"whois <person>":"get biography of person given",
"exit":"finish the publication", "exit":"finish the publication",
} }
@ -176,6 +277,9 @@ class Terminal():
elif "start" == str(command) or "restart" == str(command): elif "start" == str(command) or "restart" == str(command):
main() main()
elif "ff" == str(command):
printer.formfeed()
elif str(command).split(" ")[0] == "exercise" or str(command).split(" ")[0] == "thesis": elif str(command).split(" ")[0] == "exercise" or str(command).split(" ")[0] == "thesis":
part = str(command).split(" ")[0] part = str(command).split(" ")[0]
arguments = str(command.strip()).split(" ") arguments = str(command.strip()).split(" ")
@ -233,7 +337,8 @@ class Terminal():
persons = { persons = {
"Kittler":"Friedrich A. Kittler (June 12, 1943 October 18, 2011) was a literary scholar and a media theorist. His works relate to media, technology, and the military. ", "Kittler":"Friedrich A. Kittler (June 12, 1943 October 18, 2011) was a literary scholar and a media theorist. His works relate to media, technology, and the military. ",
"Alex":"Alexander Roidl created Poetic Software as part of his Master's thesis" "Alex":"Alexander Roidl created Poetic Software as part of his Master's thesis.",
"Jule":"Jule Ulrych is an evironmental activist and passionate vegan food lover living in Den Haag."
} }
printer = Printer() printer = Printer()
term = Terminal(printer, persons) term = Terminal(printer, persons)
@ -241,24 +346,26 @@ now = datetime.datetime.now()
def main(): def main():
term.waittostart() term.waittostart()
printer.printstring("W E L C O M E !") printer.printstring("W E L C O M E !", "dh")
printer.newline() printer.newline()
#printimagefromurl('https://apod.nasa.gov/apod/image/1701/potw1636aN159_HST_2048.jpg') printer.printstring(now.strftime("%Y-%m-%d %H:%M"), "compr")
printer.printstring(now.strftime("%Y-%m-%d %H:%M"))
printer.newline() printer.newline()
printer.printstring("---------------") # printer.formfeed()
printer.printstring("POETIC SOFTWARE") printer.printimagefromurl("https://www.sanfranciscobaycoffee.com/wp-content/uploads/2017/03/52279468.jpg")
printer.printstring("---------------")
printer.printstring("type help for help") # printer.printstring("---------------")
printer.newline() # printer.printstring("POETIC SOFTWARE")
result = apirequest.gettitle("introduction", 1) # printer.printstring("---------------")
for line in result: # printer.printstring("type help for help")
printer.printstring(line, "u") # printer.newline()
result = apirequest.getcontent("introduction", 1) # result = apirequest.gettitle("introduction", 1)
for line in result: # for line in result:
printer.printstring(line) # printer.printstring(line, "u")
# result = apirequest.getcontent("introduction", 1)
# for line in result:
# printer.printstring(line)
printer.newline() printer.newline()
term.list() # term.list()
printer.printstring("SELECT which one to see, for exercise type: exercise <number> and for thesis type: thesis <number>") printer.printstring("SELECT which one to see, for exercise type: exercise <number> and for thesis type: thesis <number>")
printer.newline() printer.newline()
term.waitforinput() term.waitforinput()

Loading…
Cancel
Save