Advertisement
here2share

# fonts_demo.py

Jan 12th, 2015
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # fonts_demo.py
  2.  
  3. # Copyright (c) 2008 Nokia Corporation
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. #    http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16.  
  17. from appuifw import *
  18. from graphics import *
  19. import e32
  20. import time
  21.  
  22. def draw(rect):
  23.     try: c.blit(img) # copy the image's contents to the lower part of the canvas
  24.     except: pass
  25.  
  26. app.body=c=Canvas(redraw_callback=draw)
  27. lock=e32.Ao_lock()
  28. app.directional_pad = False
  29. app.screen = 'large'
  30. app.exit_key_handler=lock.signal
  31. img = Image.new(c.size) # create a buffer
  32.  
  33. fonts=[None,
  34.        (None,30),
  35.        'normal',
  36.        ('normal',30),
  37.        (u'fasfsafs',30),
  38.        (u'Nokia Sans S60',20,0),
  39.        (u'Nokia Sans S60',30),
  40.        'dense',
  41.        'title',
  42.        (None,30,FONT_BOLD),
  43.        (None,30,FONT_ITALIC),
  44.        (None,30,FONT_ANTIALIAS),
  45.        (None,30,FONT_NO_ANTIALIAS),
  46.        ('normal',None,FONT_BOLD),
  47.        ('normal',None,FONT_ITALIC),
  48.        ('normal',None,FONT_NO_ANTIALIAS)     
  49.        ]
  50.  
  51. def font_image():
  52.     app.body=c
  53.     app.orientation='portrait'
  54.     img.clear(0)
  55.     y = 20 
  56.     for font in fonts:
  57.         text_to_draw=u'font:'+str(font)
  58.         # Draw the full text
  59.         img.text((0, y), text_to_draw, fill = 0x00ffff, font = font) # blue text on the image
  60.         y += 25
  61.     draw(())
  62.  
  63. def measure_text():
  64.     app.body=c
  65.     img.clear()
  66.     tl=(0,0)
  67.     font='normal'
  68.     for maxwidth in (50,100,150,200,250):
  69.         text_to_draw=u'ABCDEabcde12345@#$%&*'
  70.         (bbox,advance,maxchars)=img.measure_text(text_to_draw, font=font, maxwidth=maxwidth)
  71.         print bbox, advance, maxchars
  72.         # Position the next piece of text so the top-left corner of its bounding box is at tl. 
  73.         o=(tl[0]-bbox[0],tl[1]-bbox[1])
  74.         # Draw the bounding box of the text as returned by measure_text
  75.         img.rectangle((o[0]+bbox[0],o[1]+bbox[1],o[0]+bbox[2],o[1]+bbox[3]),outline=0x00ff00)
  76.         # Draw the maximum width specification line.
  77.         img.line((tl[0],tl[1]+10,tl[0]+maxwidth,tl[1]+10), outline=0xff0000)
  78.         # Draw the full text
  79.         img.text(o, text_to_draw, font=font, fill=0x000080)            
  80.         # Draw the text limited to given number of chars.
  81.         img.text(o, text_to_draw[0:maxchars], font=font, fill=0)               
  82.         # Compute the new top-left.
  83.         tl=(0,o[1]+bbox[3]+3)
  84.     draw(())
  85.  
  86. def font_attributes():
  87.     app.orientation='landscape'
  88.     app.body=c
  89.     img.clear()
  90.     y=20
  91.     for font in [(None,None,0),
  92.                  (None,None,FONT_ITALIC),
  93.                  (None,None,FONT_BOLD),
  94.                  (None,None,FONT_ITALIC|FONT_BOLD),
  95.                  (None,None,FONT_ANTIALIAS),
  96.                  (None,None,FONT_SUPERSCRIPT),
  97.                  (None,None,FONT_SUBSCRIPT),
  98.                  (None,None,FONT_NO_ANTIALIAS)]:
  99.         img.text((20,y), u'abcd '+str(font), font=font)
  100.         y+=20
  101.     draw(())
  102.  
  103. def font_text():
  104.     app.body=t=Text()
  105.     for font in fonts:
  106.         t.font=None
  107.         t.add(u'font:'+str(font))
  108.         t.font=font
  109.         t.add(u'abcd1234')
  110.  
  111. app.menu=[(u'Fonts on image', font_image),
  112.           (u'Measure text', measure_text),
  113.           (u'Font attributes', font_attributes),
  114.           (u'Fonts on Text', font_text)]
  115.  
  116. font_image()
  117. lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement