Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # text2fit.py
- from appuifw import *
- from graphics import *
- import e32,key_codes
- lock=e32.Ao_lock()
- def draw(rect):
- try: canvas.blit(img)
- except: pass
- def exit(pos):
- app_lock.signal()
- def redraw():
- global xm,ym
- ym,xm,zm=(sens.x/2.0,sens.y/2.0,sens.z/2.0) ### Z axis is not needed
- img=Image.new((640,360))
- app.directional_pad=False
- canvas=Canvas(redraw_callback=draw)
- app.body=canvas
- app.orientation='landscape'
- app.screen='full'
- canvas.bind(key_codes.EButton1Down,exit)
- width=canvas.size[0]-20
- img.clear(0x555555ff)
- setfont=(u"dense",30,None)
- txtEntry=[u'This is a "measure_text" wrap method example whereas...']
- words=[u'blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah',
- u'should',u'appear',u'segmented',u'to',u'stay',u'within',u'the',u'given',u'boundary.']
- def text_wrap(txtEntry=None):
- global linewrap
- w2=word[:]
- try:
- if linewrap:
- pass
- except:
- linewrap=[u'']
- try:
- txtEntry=''.join(txtEntry)
- txtEntry=txtEntry.split() # needed to be converted to a string
- for line in txtEntry:
- active=linewrap[0]+line
- fits=canvas.measure_text(active,font=setfont)[1]
- if fits > width:
- while 1:
- fits=canvas.measure_text(line,font=setfont)[1]
- kb=canvas.measure_text(line,font=setfont,maxwidth=width)[2]
- if fits > width:
- linewrap.insert(0,line[:kb])
- line=line[kb:]
- else:
- break
- linewrap.insert(0,line+' ')
- else: # fits entire entry
- linewrap[0]+=line+' '
- except:
- pass
- active=linewrap[0]+w2
- fits=canvas.measure_text(active,font=setfont)[1]
- if fits > width:
- while 1:
- fits=canvas.measure_text(w2,font=setfont)[1]
- kb=canvas.measure_text(w2,font=setfont,maxwidth=width)[2]
- if fits > width:
- linewrap.insert(0,w2[:kb])
- w2=w2[kb:]
- else:
- break
- linewrap.insert(0,w2+' ')
- else: # fits entire entry
- linewrap[0]+=w2+' '
- y=40
- LnWr=linewrap[:]
- LnWr.reverse()
- img.clear(0x555555ff)
- for line in LnWr:
- img.text((10,y),line,font=setfont,fill=0xffffff) # text on blue img
- y += 36
- ### demo
- for word in words:
- text_wrap(txtEntry)
- canvas.blit(img)
- e32.ao_yield()
- app_lock=e32.Ao_lock()
- app_lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement