pys60:appuifw.Text()用法


import e32
import appuifw
import graphics
import random
import key_codes
 
VERSION = "1.20"
 
# Classical sample text to show on-screen
# http://en.wikipedia.org/wiki/Pangram
pangram = u"quick brown fox jumps over the lazy dog 0123456789"
 
# Symbolic font names, always available
# Each label is associated with some real font in the device
labels = ['annotation', 'title', 'legend', 'symbol', 'dense', 'normal', 'digital']
 
# Real font names, depends on firmware. E.g. u"albi17b", u"LatinBold12"
# Do NOT count on any specific font/style/size to exist in phone
fonts = None
 
# Helper temp variable to show text on-screen
g_list = labels
# Canvas used only for justification sample
canvas = None
img = None
# Text used for showing fonts and labels
t = None
# Text style and highlight
g_style = 0
g_hilight = 0
 
def get_color():
    ''' Generate random RGB color '''
    return (random.choice(range(0,255)), random.choice(range(0,255)), random.choice(range(0,255)))
 
def show_just():
    ''' Text justification sample - for Canvas '''
    # string "2" measures for example ((0, -20, 14, 0), 16, 1)
    x, y = canvas.size
    img.clear()
 
    # Sample text at top left corner
    s = u"Left %d" % (random.randint(1, 999999))
    ((x1, y1, x2, y2), dummy, dummy) = img.measure_text(s, font="normal")
    img.text((0, -y1), s, 0, 'normal')
    # Sample text at center center screen
    s = u"Center %d" % (random.randint(1, 999999))
    ((x1, y1, x2, y2), dummy, dummy) = img.measure_text(s, font="normal")
    img.text((x/2-x2/2, y/2-y1/2), s, 0, 'normal')
    # Sample text at bottom right corner
    s = u"Right %d" % (random.randint(1, 999999))
    ((x1, y1, x2, y2), dummy, dummy) = img.measure_text(s, font="normal")
    img.text((x-x2, y+y1), s, 0, 'normal')
 
    # Bring Canvas on-screen, manual update
    appuifw.app.body = canvas
    canvas.blit(img)
 
def show_text(a_list, a_light=-1):
    ''' Main draw routine - for Text '''
    global g_list
    g_list = a_list
 
    # Hox: Can use only one highlight style at a time
    global g_hilight
    if a_light == -1:
        a_light = g_hilight
    else:
        g_hilight = a_light
 
    t.clear()
    for i in a_list:
        t.font = i
        t.style = g_style | a_light
        t.add(i + u': ' + pangram + u'\n')
 
    # Use random text color, show something happened
    t.color = (get_color())
    appuifw.app.body = t
 
def show_labels():
    ''' Show logical font label vs real used font - for Text '''
    t.clear()
    for i in labels:
        t.font = i
        i2 = t.font
        # HOX: Not documented, but seems to be this:
        # font typeface, height in twips, style flags
        t.add(i + ': ' + i2[0] + ',' + str(i2[1]) + ',' + str(i2[2]) + '\n')
    appuifw.app.body = t
 
def style_none():
    ''' Clear all text style attributes '''
    global g_style
    g_style = 0
    show_text(g_list)
 
def style_bold():
    ''' Toggle bold style on/off '''
    global g_style
    if g_style & appuifw.STYLE_BOLD:
        g_style = g_style ^ appuifw.STYLE_BOLD
    else:
        g_style = g_style | appuifw.STYLE_BOLD
    show_text(g_list)
 
def style_italics():
    ''' Toggle italics style on/off '''
    global g_style
    if g_style & appuifw.STYLE_ITALIC:
        g_style = g_style ^ appuifw.STYLE_ITALIC
    else:
        g_style = g_style | appuifw.STYLE_ITALIC
    show_text(g_list)
 
def style_strike():
    ''' Toggle strikethrough style on/off '''
    global g_style
    if g_style & appuifw.STYLE_STRIKETHROUGH:
        g_style = g_style ^ appuifw.STYLE_STRIKETHROUGH
    else:
        g_style = g_style | appuifw.STYLE_STRIKETHROUGH
    show_text(g_list)
 
def style_underline():
    ''' Toggle underline style on/off '''
    global g_style
    if g_style & appuifw.STYLE_UNDERLINE:
        g_style = g_style ^ appuifw.STYLE_UNDERLINE
    else:
        g_style = g_style | appuifw.STYLE_UNDERLINE
    show_text(g_list)
 
def style_hilight(a_style):
    ''' Change text highlight style '''
    # Use random highlight color, show something happened
    t.highlight_color = (get_color())
    show_text(g_list, a_style)
 
def menu_about():
    ''' Callback for menu item About '''
    appuifw.note(u'Font Test v'+VERSION + u'\n' +\
        u'jouni.miettunen.googlepages.com\n\u00a92009 Jouni Miettunen')
 
def cb_quit():
    ''' Cleanup before exit '''
    app_lock.signal()
 
# Initialize application
appuifw.app.screen = 'full'
appuifw.app.title = u"Font Test"
appuifw.app.exit_key_handler = cb_quit
appuifw.app.menu = [
    (u"Font Type",
        ((u"Label names", lambda:show_text(labels)),
        (u"Font names", lambda:show_text(fonts)),
        (u"Labels using Font", show_labels))),
    (u"Font Style",
        ((u"None", style_none),
        (u"Bold On/Off", style_bold),
        (u"Italics On/Off", style_italics),
        (u"Strikethrough On/Off", style_strike),
        (u"Underline On/Off", style_underline))),
    (u"Font Highlight",
        ((u"None", lambda:style_hilight(0)),
        (u"Standard", lambda:style_hilight(appuifw.HIGHLIGHT_STANDARD)),
        (u"Rounded", lambda:style_hilight(appuifw.HIGHLIGHT_ROUNDED)),
        (u"Shadow", lambda:style_hilight(appuifw.HIGHLIGHT_SHADOW)))),
    (u"Justification Test", show_just),
    (u"About", menu_about),
    (u"Exit", cb_quit)]
 
# Must set canvas to body, else canvas.size wrong
# Do not define redraw_callback, else problems with text mode
canvas = appuifw.Canvas()
appuifw.app.body = canvas
img = graphics.Image.new(canvas.size)
 
# Want to start with text mode
t = appuifw.Text()
appuifw.app.body = t
 
# Initialize application UI
fonts = appuifw.available_fonts()
show_text(g_list)
 
# Wait for user to do anything
app_lock = e32.Ao_lock()
app_lock.wait()


你可能感兴趣的:(UI,Nokia)