Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # fonts_demo.py
- # Copyright (c) 2008 Nokia Corporation
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- from appuifw import *
- from graphics import *
- import e32
- import time
- def draw(rect):
- try: c.blit(img) # copy the image's contents to the lower part of the canvas
- except: pass
- app.body=c=Canvas(redraw_callback=draw)
- lock=e32.Ao_lock()
- app.directional_pad = False
- app.screen = 'large'
- app.exit_key_handler=lock.signal
- img = Image.new(c.size) # create a buffer
- fonts=[None,
- (None,30),
- 'normal',
- ('normal',30),
- (u'fasfsafs',30),
- (u'Nokia Sans S60',20,0),
- (u'Nokia Sans S60',30),
- 'dense',
- 'title',
- (None,30,FONT_BOLD),
- (None,30,FONT_ITALIC),
- (None,30,FONT_ANTIALIAS),
- (None,30,FONT_NO_ANTIALIAS),
- ('normal',None,FONT_BOLD),
- ('normal',None,FONT_ITALIC),
- ('normal',None,FONT_NO_ANTIALIAS)
- ]
- def font_image():
- app.body=c
- app.orientation='portrait'
- img.clear(0)
- y = 20
- for font in fonts:
- text_to_draw=u'font:'+str(font)
- # Draw the full text
- img.text((0, y), text_to_draw, fill = 0x00ffff, font = font) # blue text on the image
- y += 25
- draw(())
- def measure_text():
- app.body=c
- img.clear()
- tl=(0,0)
- font='normal'
- for maxwidth in (50,100,150,200,250):
- text_to_draw=u'ABCDEabcde12345@#$%&*'
- (bbox,advance,maxchars)=img.measure_text(text_to_draw, font=font, maxwidth=maxwidth)
- print bbox, advance, maxchars
- # Position the next piece of text so the top-left corner of its bounding box is at tl.
- o=(tl[0]-bbox[0],tl[1]-bbox[1])
- # Draw the bounding box of the text as returned by measure_text
- img.rectangle((o[0]+bbox[0],o[1]+bbox[1],o[0]+bbox[2],o[1]+bbox[3]),outline=0x00ff00)
- # Draw the maximum width specification line.
- img.line((tl[0],tl[1]+10,tl[0]+maxwidth,tl[1]+10), outline=0xff0000)
- # Draw the full text
- img.text(o, text_to_draw, font=font, fill=0x000080)
- # Draw the text limited to given number of chars.
- img.text(o, text_to_draw[0:maxchars], font=font, fill=0)
- # Compute the new top-left.
- tl=(0,o[1]+bbox[3]+3)
- draw(())
- def font_attributes():
- app.orientation='landscape'
- app.body=c
- img.clear()
- y=20
- for font in [(None,None,0),
- (None,None,FONT_ITALIC),
- (None,None,FONT_BOLD),
- (None,None,FONT_ITALIC|FONT_BOLD),
- (None,None,FONT_ANTIALIAS),
- (None,None,FONT_SUPERSCRIPT),
- (None,None,FONT_SUBSCRIPT),
- (None,None,FONT_NO_ANTIALIAS)]:
- img.text((20,y), u'abcd '+str(font), font=font)
- y+=20
- draw(())
- def font_text():
- app.body=t=Text()
- for font in fonts:
- t.font=None
- t.add(u'font:'+str(font))
- t.font=font
- t.add(u'abcd1234')
- app.menu=[(u'Fonts on image', font_image),
- (u'Measure text', measure_text),
- (u'Font attributes', font_attributes),
- (u'Fonts on Text', font_text)]
- font_image()
- lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement