# -*- coding: cp1252 -*- # TXT2CBR V0.1 for Nokia S60 5th Ed, From JamieFuller.com # Copyright (C) 2009 Jamie Fuller # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import appuifw, e32, graphics, zipfile, os, math, time def convert_text(input_file,output_file,page_width,page_height): global c pb_width=(c.size[0]-50) c.rectangle((0, 0, c.size[0], c.size[1]), fill=0x000000) c.rectangle((25,100,pb_width,116),0xffffff) txt_f = open(input_file, "r") total_lines=0 for line in txt_f.readlines(): total_lines+=1 txt_f.close() txt_f = open(input_file, "r") txt_zip=zipfile.ZipFile(output_file,"w") txt_y=0 txt_img=graphics.Image.new((page_width,page_height),mode='L') txt_img.rectangle((0,0,page_width,page_height),fill=0x000000) txt_page=0 txt_cnt=0 for line in txt_f.readlines(): line=line.replace("“","'") line=line.replace("”","'") line=line.replace("’","'") txt_x=5 txt_y+=20 if txt_y>(page_height-20): txt_y=20 txt_img.save("e:\\"+str(txt_page+1000)+".png", bpp=1, compression='fast') txt_zip.write("e:\\"+str(txt_page+1000)+".png") os.remove("e:\\"+str(txt_page+1000)+".png") pb=float(float(txt_cnt)/float(total_lines))*pb_width c.rectangle((25,100,pb,116),fill=0xff0000) c.rectangle((25,100,pb_width,116),0xffffff) txt_page+=1 txt_img.rectangle((0,0,page_width,page_height),fill=0x000000) words=line.split() txt_cnt+=1 for word in words: ii=unicode(word,'ascii',errors='replace') ((x1, y1, x2, y2), advance, dummy) = txt_img.measure_text(ii) advance+=10 if (advance+txt_x)>page_width: txt_x=5 txt_y+=20 if txt_y>(page_height-20): txt_y=20 txt_img.save("e:\\"+str(txt_page+1000)+".png", bpp=1, compression='fast') txt_zip.write("e:\\"+str(txt_page+1000)+".png") os.remove("e:\\"+str(txt_page+1000)+".png") pb=float(float(txt_cnt)/float(total_lines))*pb_width ## c.rectangle((25,100,pb,116),fill=0xff0000) ## c.rectangle((25,100,pb_width,116),0xffffff) txt_page+=1 txt_img.rectangle((0,0,page_width,page_height),fill=0x000000) txt_img.text((txt_x, txt_y), unicode(ii), (255, 255, 255)) txt_x+=advance txt_img.save("e:\\"+str(txt_page+1000)+".png", bpp=1, compression='fast') txt_zip.write("e:\\"+str(txt_page+1000)+".png") os.remove("e:\\"+str(txt_page+1000)+".png") txt_zip.close() txt_f.close() c.rectangle((0, 0, c.size[0], c.size[1]), fill=0x000000) c.text((100,100),unicode("Done!"),fill=0xffffff) def do_conversion(): global converting,filenm,drive_letter,drive_folder if converting<1: filenm="" select_drive() if filenm: converting=1 convert_text(drive_letter+"\\"+drive_folder+"\\"+filenm , drive_letter+"\\"+drive_folder+"\\"+filenm[:-4]+".cbz",640,360) converting=0 def quit(): app_lock.signal() def select_drive(): global drive_folder,drive_letter global converting if converting<1: drives=e32.drive_list() t=appuifw.selection_list(drives) if t: drive_letter=drives[t] files=os.listdir(drive_letter) folders=[] for fld in files: if os.path.isdir(unicode(drive_letter+"\\"+fld)): folders.append(fld) folders.sort(lambda x, y: cmp(x.lower(), y.lower())) t=appuifw.selection_list(folders) if t: drive_folder=folders[t] loadTXT() def loadTXT(): global filenm global drive_letter,drive_folder z=None tf=[] files=map(unicode,os.listdir(drive_letter+"\\"+drive_folder)) for i in files: if i[-3:]=="txt" or i[-3:]=="TXT": tf.append(i) tf.sort() index=appuifw.selection_list(tf,search_field=1) if index>-2 and index!=None: filenm=tf[index]; def about(): data = appuifw.note(u"TXT2CBZ V0.1\n by JamieFuller.com:", "info") app_lock = e32.Ao_lock() appuifw.app.title = u"TXT2CBZ V0.1" appuifw.app.screen = 'normal' appuifw.app.menu = [(u"Choose File to Convert", do_conversion),(u"About", about),(u"Exit", quit)] appuifw.app.exit_key_handler = quit appuifw.app.directional_pad = False drive_letter="e:" drive_folder="comics" filenm="" def handle_redraw(rect): pass def handle_resize(dummy=(0, 0, 0, 0)): pass c = appuifw.Canvas(resize_callback = handle_resize,redraw_callback = handle_redraw) appuifw.app.body = c converting=0 appuifw.app.exit_key_handler = quit app_lock.wait()