Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # text2fit.py + rotate ^ 2016.05
- from appuifw import *
- import e32,audio,appuifw,os,sys,key_codes,time,math,random,graphics,sensor,copy
- from graphics import *
- class cv():
- pixel=-1,-1
- xyz=(0,0,0)
- x=0
- y=0
- light=0
- evt=None
- tmp=0
- sw=0
- init=1
- padx,pady=0,0
- up=-1
- ori='portrait'
- cv=cv()
- def quit():
- app_lock.signal()
- app_lock = e32.Ao_lock()
- app.exit_key_handler = quit
- def draw(rect):
- if img:
- canvas.blit(img)
- def e_callback(event):
- cv.pixel = event['pos']
- img=None
- def show_cv():
- global canvas, img
- app.screen = 'large'
- app.directional_pad = False
- app.orientation=cv.ori
- app.body = canvas = Canvas(redraw_callback=draw, event_callback=e_callback)
- img = Image.new(canvas.size)
- cv.init=1
- show_cv()
- def z_ori():
- if cv.ori is 'portrait': cv.ori='landscape'
- else: cv.ori='portrait'
- show_cv()
- demo()
- def z_fit(a,b,c):
- pl=canvas.measure_text(a,font=b)[1] # pixel length
- cl=canvas.measure_text(a,font=b,maxwidth=c)[2] # chr length
- ### note: maxwidth is not generic
- return cl,pl
- def text2wrap(prev,new=None,gap=20,setfont=(u"dense",30)):
- if type(prev) in [int,tuple]:
- color=prev
- if new:
- if gap is not 20:
- setfont=gap
- gap=new
- y=40
- for gen in wrapped:
- img.text((gap,y),u''+gen,font=setfont,fill=color)
- y += 36
- else:
- ww=canvas.size[0]-gap*2
- if type(prev) != list: prev=[]
- def wrap_gen(line):
- tmp=''
- if prev: tmp=prev.pop(-1)+' '
- while 1:
- cl,pl=z_fit(tmp+line,setfont,ww)
- if pl > ww:
- if tmp:
- prev.append(tmp)
- tmp=''
- else:
- prev.append(line[:cl])
- line=line[cl:]
- else:
- break
- prev.append(tmp+line)
- if ' ' in new: new=new.split()
- else: new=[new]
- for data in new: wrap_gen(u''+data)
- _gv = globals()
- _gv['wrapped']=prev[:]
- #
- appuifw.app.menu=[(u'Rotate Display',z_ori)]
- def demo():
- img.clear(0x5555ff)
- o1st_entry='This is a "measure_text" wrap method example whereas...'
- o2nd_entry=['blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah',
- 'should','appear','segmented','to','stay','within','the','given','boundary.']
- #
- text2wrap('',o1st_entry)
- for divided in o2nd_entry:
- text2wrap(wrapped,divided)
- text2wrap(0xffffff)
- draw(())
- demo()
- app_lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement