Advertisement
here2share

# text2fit.py + rotate ^ 2016.05

May 10th, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # text2fit.py + rotate ^ 2016.05
  2.  
  3. from appuifw import *
  4. import e32,audio,appuifw,os,sys,key_codes,time,math,random,graphics,sensor,copy
  5. from graphics import *
  6.  
  7. class cv():
  8.     pixel=-1,-1
  9.     xyz=(0,0,0)
  10.     x=0
  11.     y=0
  12.     light=0
  13.     evt=None
  14.     tmp=0
  15.     sw=0
  16.     init=1
  17.     padx,pady=0,0
  18.     up=-1
  19.     ori='portrait'
  20. cv=cv()
  21.  
  22. def quit():
  23.     app_lock.signal()
  24. app_lock = e32.Ao_lock()
  25. app.exit_key_handler = quit
  26.  
  27. def draw(rect):
  28.     if img:
  29.         canvas.blit(img)
  30.  
  31. def e_callback(event):
  32.     cv.pixel = event['pos']
  33.  
  34. img=None
  35. def show_cv():
  36.     global canvas, img
  37.     app.screen = 'large'
  38.     app.directional_pad = False
  39.     app.orientation=cv.ori
  40.     app.body = canvas = Canvas(redraw_callback=draw, event_callback=e_callback)
  41.     img = Image.new(canvas.size)
  42.     cv.init=1
  43. show_cv()
  44.  
  45. def z_ori():
  46.     if cv.ori is 'portrait': cv.ori='landscape'
  47.     else: cv.ori='portrait'
  48.     show_cv()
  49.     demo()
  50.  
  51. def z_fit(a,b,c):
  52.     pl=canvas.measure_text(a,font=b)[1] # pixel length
  53.     cl=canvas.measure_text(a,font=b,maxwidth=c)[2] # chr length
  54.     ### note: maxwidth is not generic
  55.     return cl,pl
  56. def text2wrap(prev,new=None,gap=20,setfont=(u"dense",30)):
  57.     if type(prev) in [int,tuple]:
  58.         color=prev
  59.         if new:
  60.             if gap is not 20:
  61.                 setfont=gap
  62.             gap=new
  63.         y=40
  64.         for gen in wrapped:
  65.             img.text((gap,y),u''+gen,font=setfont,fill=color)
  66.             y += 36
  67.     else:
  68.         ww=canvas.size[0]-gap*2
  69.         if type(prev) != list: prev=[]
  70.         def wrap_gen(line):
  71.             tmp=''
  72.             if prev: tmp=prev.pop(-1)+' '
  73.             while 1:
  74.                 cl,pl=z_fit(tmp+line,setfont,ww)
  75.                 if pl > ww:
  76.                     if tmp:
  77.                         prev.append(tmp)
  78.                         tmp=''
  79.                     else:
  80.                         prev.append(line[:cl])
  81.                         line=line[cl:]
  82.                 else:
  83.                     break
  84.             prev.append(tmp+line)
  85.  
  86.         if ' ' in new: new=new.split()
  87.         else: new=[new]
  88.         for data in new: wrap_gen(u''+data)
  89.         _gv = globals()
  90.         _gv['wrapped']=prev[:]
  91. #
  92.  
  93. appuifw.app.menu=[(u'Rotate Display',z_ori)]
  94.  
  95. def demo():
  96.     img.clear(0x5555ff)
  97.  
  98.     o1st_entry='This is a "measure_text" wrap method example whereas...'
  99.     o2nd_entry=['blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah',
  100.             'should','appear','segmented','to','stay','within','the','given','boundary.']
  101.     #
  102.     text2wrap('',o1st_entry)
  103.     for divided in o2nd_entry:
  104.         text2wrap(wrapped,divided)
  105.     text2wrap(0xffffff)
  106.     draw(())
  107. demo()
  108.  
  109. app_lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement