class Font:
    __module__ = __name__

    def __init__(s):
        import graphics
        import os
        import e32
        (s.G, s.O, s.fonts, s.default,) = (graphics, os, {}, '')
        if (e32.s60_version_info >= (3, 0)):
            s.fontspath = 'z:\\PyS60\\PyFonts\\'
        else:
            s.fontspath = 'z:\\system\\libs\\pyfonts\\'

    def load(s, name):
        path = ((s.fontspath + name) + '.fnt')
        if s.O.path.exists(path):
            try:
                f = open(path)
                t = f.read().split('FONTINFO')
                f.close()
                f = open((path + '.png'), 'w')
                (f.write(t[0].decode('zlib')),
                 f.close())
                img = s.G.Image.open((path + '.png'))
                font = s.G.Image.new(img.size, '1')
                (font.load((path + '.png')),
                 img.clear(0),
                 s.O.remove((path + '.png')))
                s.fonts[name] = {'font': font, 'img': img}
                t = unicode(t[1].decode('zlib'), 'utf-8').split('\n')
                (s.fonts[name]['name'], w,) = (t[0], t[1].split('x'))
                (w1, w2, x, y,) = (int(w[0]), int(w[1]), 0, 0)
                (s.fonts[name]['size'], s.fonts[name]['color'],) = ((w1, w2), 0)
                del t[0:2]
                for i in t:
                    for j in range(0, len(i), 3):
                        s.fonts[name][i[j]] = (((x, y), ((x + w1), (y + w2))),\
                                               int(i[(j + 1):(j + 3)]))
                        x += w1

                    (x, y,) = (0,  (y + w2))

                if (not s.default):
                    s.default = name
                return 1
            except:
                if s.fonts.has_key(name):
                    del s.fonts[name]
        else:
            return 0

    def pyfonts(s):
        (l, sp,) = (s.O.listdir(s.fontspath), [])
        for i in l:
            j = s.O.path.splitext(i)
            if (j[1] == '.fnt'):
                sp.append(j[0])

        return sp

    def get(s, font, name):
        if (s.fonts.has_key(font) and s.fonts[font].has_key(name)):
            return s.fonts[font][name]

    def list(s):
        return list(s.fonts)

    def len(s, txt, font):
        if s.fonts.has_key(font):
            l = 0
            for i in list(txt):
                if s.fonts[font].has_key(i):
                    l += s.fonts[font][i][1]

            return l

    def text(s, pos, img, txt, color = 0, font = None):
        if ((not font) and s.default):
            font = s.default
        if s.fonts.has_key(font):
            if (color and (s.fonts[font]['color'] != color)):
                s.fonts[font]['img'].clear(color)
                s.fonts[font]['color'] = color
            (x, y,) = (pos[0], (pos[1] - s.fonts[font]['size'][1]))
            for i in list(txt):
                if s.fonts[font].has_key(i):
                    img.blit(s.fonts[font]['img'], target=(x, y),\
                             source=s.fonts[font][i][0],\
                             mask=s.fonts[font]['font'])
                    x += s.fonts[font][i][1]

font = Font()
del Font
