从临时文件加载的字体文件似乎不正确


for key in fnt.keys():
    str1 = base64.b64decode(fnt[key])
    strA = XOR(str1, coder)
    temp = tempfile.TemporaryFile()
    temp.seek(0)
    temp.write(strA)
    temp.seek(0)
    if key == "calibri.ttf":
        FONT = temp
    if key == "calibrib.ttf":
        FONTB = temp
        temp.close()
return (FONT, FONTB)

在上面的代码中,我检索了在字典中保存为字符串的字体。当我使用此代码时,它会返回一个错误 - 运行时错误:无法在流中查找。使用下面的代码,它可以完美运行。有人请解释如何使用临时文件方法来获取字体而不是写入文件。这可能吗?

for key in fnt.keys():
    str1 = base64.b64decode(fnt[key])
    strA = XOR(str1, coder)
    with open(home + "\" + key, "wb") as to_disk:
        to_disk.write(strA)
        FONT = "calibri.ttf"
        FONTB = "calibrib.ttf"
return (FONT, FONTB)

我在下面添加完整的代码。看看这是否有助于给我一个可行的答案。

import string
import base64
import os
from fnt import fnt
import string
import tempfile
from itertools import cycle, izip
def XOR(message, key):
    cryptedMessage = ''.join(chr(ord(c)^ord(k)) for c,k in izip(message, cycle(key)))
    return cryptedMessage
#----------------------------------------------------------------------
def main():
    """extracts and returns the font files from fnt.py"""
    coder ="PHdzDER+@k7pcm!LX8gufh=y9A^UaMsn-oW6"
    home = os.getcwd()
    for key in fnt.keys():
        str1 = base64.b64decode(fnt[key])
        strA = XOR(str1, coder)
        #temp = tempfile.TemporaryFile()
        #temp.seek(0)
        #temp.write(strA)
        #temp.seek(0)

        #if key == "calibri.ttf":
            #FONT = temp
        #if key == "calibrib.ttf":
            #FONTB = temp

        with open(home + "\" + key, "wb") as to_disk:
            to_disk.write(strA)
            FONT = "calibri.ttf"
            FONTB = "calibrib.ttf"
    return (FONT, FONTB)
if __name__=='__main__':
    main()

当 temp 关闭时,它会被销毁,因此您将无法从中读取以提取字体数据。

您在代码的哪一行收到RuntimeError: Can't seek in stream错误消息?正如Michael Petch所说,我们需要知道你的操作系统。TemporaryFile(( 在 Linux 上肯定是可以搜索的。

顺便说一句,在

if key == "calibrib.ttf":
    FONTB = temp
    temp.close()

temp.close()位于if块内。我想这可能只是一个复制和粘贴错误。但正如我上面所说,在从它们中读取之前,您不想关闭这些文件。

据推测,您的原始字体读取功能使用文件名从磁盘打开字体文件,然后使用open()的文件句柄访问字体数据。因此,正如Michael Petch所说,您需要更改它以接受已打开文件的句柄。

编辑

当您说"没有其他字体读取功能"时,我之前感到困惑,因为我无法弄清楚您实际上要对解密的字体数据做什么。

我想我也对你调用字体提取函数main()感到困惑,因为main((通常用作程序的入口点,所以程序通常不会在不同的模块中运行一堆不同的main()函数。

无论如何。。。

我从未使用过pygame,但是通过快速浏览文档,您可以pygame.font.Font()提供字体名称字符串或打开的字体文件句柄作为第一个参数,例如

myfont = pygame.font.Font(font_filehandle, size)

据推测,您的fnt模块有一个字典,其中包含两个 base64 编码、XOR 加密字体数据的字符串,由字体名称"calibrib.ttf"键入。

所以这应该有效,假设我没有误解那个 pygame 文档。 :)

警告:未经测试的代码

def xor_str(message, key):
    return ''.join([chr(ord(c)^ord(k)) for c,k in izip(message, cycle(key))])
def decode_fonts(fnt):
    coder = "PHdzDER+@k7pcm!LX8gufh=y9A^UaMsn-oW6"
    font_fh = {}
    for key in fnt.keys():
        xorfont = base64.b64decode(fnt[key])
        temp = tempfile.TemporaryFile()
        temp.write(xor_str(xorfont, coder))
        temp.seek(0)
        font_fh[key] = temp
    return font_fh
#...............
    #In your pygame function:
    font_fh = decode_fonts(fnt)
    font_obj = {}
    for key in font_fh.keys():
        font_obj[key] = pygame.font.Font(font_fh[key], point_size)
    #Now you can do
    surface = font_obj["calibri.ttf"].render(text, antialias, color, background=None)

最新更新