Advertisement
here2share

# text2fit.py

Nov 29th, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # text2fit.py
  2.  
  3. from appuifw import *
  4. from graphics import *
  5. import e32,key_codes
  6.  
  7. lock=e32.Ao_lock()
  8.  
  9.  
  10. def draw(rect):
  11.     try: canvas.blit(img)
  12.     except: pass
  13.  
  14. def exit(pos):
  15.     app_lock.signal()
  16.  
  17. def redraw():
  18.     global xm,ym
  19.     ym,xm,zm=(sens.x/2.0,sens.y/2.0,sens.z/2.0) ### Z axis is not needed
  20.  
  21. img=Image.new((640,360))
  22. app.directional_pad=False
  23. canvas=Canvas(redraw_callback=draw)
  24. app.body=canvas
  25. app.orientation='landscape'
  26. app.screen='full'
  27.  
  28. canvas.bind(key_codes.EButton1Down,exit)
  29.  
  30. width=canvas.size[0]-20
  31. img.clear(0x555555ff)
  32.  
  33. setfont=(u"dense",30,None)
  34.  
  35. txtEntry=[u'This is a "measure_text" wrap method example whereas...']
  36. words=[u'blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah',
  37.         u'should',u'appear',u'segmented',u'to',u'stay',u'within',u'the',u'given',u'boundary.']
  38.  
  39. def text_wrap(txtEntry=None):
  40.     global linewrap
  41.     w2=word[:]
  42.     try:
  43.         if linewrap:
  44.             pass
  45.     except:
  46.         linewrap=[u'']
  47.         try:
  48.             txtEntry=''.join(txtEntry)
  49.             txtEntry=txtEntry.split() # needed to be converted to a string
  50.             for line in txtEntry:
  51.                 active=linewrap[0]+line
  52.                 fits=canvas.measure_text(active,font=setfont)[1]
  53.                 if fits > width:
  54.                     while 1:
  55.                         fits=canvas.measure_text(line,font=setfont)[1]
  56.                         kb=canvas.measure_text(line,font=setfont,maxwidth=width)[2]
  57.                         if fits > width:
  58.                             linewrap.insert(0,line[:kb])
  59.                             line=line[kb:]
  60.                         else:
  61.                             break
  62.                     linewrap.insert(0,line+' ')
  63.                 else: # fits entire entry
  64.                     linewrap[0]+=line+' '
  65.         except:
  66.             pass
  67.  
  68.     active=linewrap[0]+w2
  69.     fits=canvas.measure_text(active,font=setfont)[1]
  70.     if fits > width:
  71.         while 1:
  72.             fits=canvas.measure_text(w2,font=setfont)[1]
  73.             kb=canvas.measure_text(w2,font=setfont,maxwidth=width)[2]
  74.             if fits > width:
  75.                 linewrap.insert(0,w2[:kb])
  76.                 w2=w2[kb:]
  77.             else:
  78.                 break
  79.         linewrap.insert(0,w2+' ')
  80.     else: # fits entire entry
  81.         linewrap[0]+=w2+' '
  82.  
  83.     y=40
  84.     LnWr=linewrap[:]
  85.     LnWr.reverse()
  86.     img.clear(0x555555ff)
  87.     for line in LnWr:                      
  88.         img.text((10,y),line,font=setfont,fill=0xffffff) # text on blue img
  89.         y += 36
  90.  
  91. ### demo
  92.  
  93. for word in words:
  94.     text_wrap(txtEntry)
  95. canvas.blit(img)
  96.  
  97.  
  98. e32.ao_yield()
  99.  
  100. app_lock=e32.Ao_lock()
  101. app_lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement