将字体数据保存在内存中

时间:2014-09-26 07:03:40

标签: python pygame

这个问题与此问题有关。 Font file loaded from temp file seems incorrect

Pygame似乎习惯在阅​​读后关闭字体文件,但是当它在磁盘上时可以一遍又一遍地读取。我们如何将读取的字体数据保存到内存中,以便可以多次调用它,但不会丢失其格式?在这种情况下,保存到磁盘的数据应保留为FILE OBJECT?我尝试过深度扫描没有成功。下面给出的代码

def xor(message, key):
    cryptedMessage = ''.join(chr(ord(c)^ord(k)) for c,k in izip(message, cycle(key)))

    return cryptedMessage

#-------------------------------------------------------------------------------
def decode_fonts(fnt = fnt):

    """extracts and returns the font files from fnt.py in a dictionary"""
    coder = "QZY0r[CIlVwq1O#SxBt_2eij%b;3$vJK4,FN&TG5PHdzDER+@k7pcm!LX8gufh=y9A^UaMsn-oW6"
    font_fh = {}
    font_obj = {}
    point_size = 5

    for key in fnt.keys():

        xorfont = base64.b64decode(fnt[key])

        tempbuffer = StringIO()
        tempbuffer.write(xor(xorfont, coder))
        tempbuffer.seek(0)
        font_fh[key] = tempbuffer
        #print type(temp), type(font_fh)

    for key in font_fh.keys():
        font_obj[key] = pygame.font.Font(font_fh[key], point_size)


    return font_fh, font_obj

在印刷方面

font_fh, font_obj = gs.fontstore.decode_fonts()

def maketext(msg, fontsize, colour, bgcolour, underline, FONTNAME):
    """returns the text supplied as an image with x and y diamentions of the image"""

    mafont = font_obj[FONTNAME]
    font_fh[FONTNAME].seek(0)
    mafont = pygame.font.Font(font_fh[FONTNAME], fontsize)

    mafont.set_underline(underline)

    fontxy = mafont.size(msg)  # get image size x, y for print formatting

    if bgcolour is None:

        matext = mafont.render(msg, True, colour)
    else:
        matext = mafont.render(msg, True, colour, bgcolour)

    matext = matext.convert_alpha()

    return [matext, fontxy]

    text1 = maketext(msg1, 40, WHITE, None, False, FONT1)
    text2 = maketext(msg2, 40, WHITE, None, False, FONT2)
    text3 = maketext(msg2, 40, WHITE, None, False, FONT3)
    text4 = maketext(msg2, 40, WHITE, None, False, FONT4)
    text5 = maketext(msg2, 40, WHITE, None, False, FONT5)
    text6 = maketext(msg2, 40, WHITE, None, False, FONT6)


    def update_all():
        SCREEN.fill((BLACK))
        SCREEN.blit(text1[0], (0, 0))
        SCREEN.blit(text2[0], (0, 50))
        SCREEN.blit(text3[0], (0, 100))
        SCREEN.blit(text4[0], (0, 150))
        SCREEN.blit(text5[0], (0, 200))
        SCREEN.blit(text6[0], (0, 250))


        pygame.display.update()

    def get_keys():
        global run
        for event in pygame.event.get():

            MOUSEPOS = pygame.mouse.get_pos()

            if event.type is KEYDOWN:
                if event.key is K_ESCAPE:
                    run = False

    while run:

        clock.tick(60)
        get_keys()
        #text6 = maketext(msg2, 40, WHITE, None, False, FONT6)

        update_all()

if __name__=='__main__':
    main()

如果我取消注释'#text6 = maketext(msg2,40,WHITE,None,False,FONT6)'在while循环中,程序停止抛出错误

ValueError: I/O operation on closed file

需要支持。

1 个答案:

答案 0 :(得分:0)

在考虑了上面的建议后,下面的代码是成功的。 将字体构建例程移动到单独的函数中。

    def makefont(FONTNAME, fontsize):
        """the font type and size given the custom made font is returned"""

        encryptedfont = base64.b64decode(fntdata[FONTNAME])

        tempbuffer = StringIO()
        tempbuffer.write(XOR(encryptedfont, coder))
        tempbuffer.seek(0)

        madefont = pygame.font.Font(tempbuffer, fontsize)

        return madefont

def maketext(msg, colour, bgcolour, underline, madefont):
    """returns the text supplied as an image with x and y diamentions of the image"""

    madefont.set_underline(underline)

    fontxy = madefont.size(msg)  # get image size x, y for print formatting

    if bgcolour is None:

        matext = madefont.render(msg, True, colour)
    else:
        matext = madefont.render(msg, True, colour, bgcolour)

    matext = matext.convert_alpha()

    return [matext, fontxy]

当我需要更改字体大小时调用maketext(),否则maketext会重新使用madefont