python - fontforge无法解析Tornado服务器中的svg文件

标签 python ubuntu svg tornado fontforge

我正在尝试在 Ubuntu VM 上安装 Tornado 服务器,其任务是从 .svg 文件开始生成完整的字体包。 我在没有服务器的情况下运行了以下脚本,效果相当完美。 https://gist.github.com/jorgegarciadev/6127832

现在我尝试在我的服务器上执行此任务,Fontforge 似乎在处理 .svg 文件时遇到问题。首先,我认为这是一个 block 问题,就好像我的文件没有完全读取或发送一样,但是当我在服务器上打开最近上传的文件时,它是完整的并且与原始文件具有相同的大小。更令人不安的是 Fontforge 告诉我这个文件不是字体文件。这就是让我思考 block 的原因(因为它似乎是解析错误)

pastebin 上的 SVG 文件就在那里: http://pastebin.com/ai5pr1DG

Python服务器代码:

import tornado, tornado.ioloop, tornado.web
import os, uuid
import fontforge, re, zipfile

__UPLOADS__ = "uploads/"
EXTS = [".woff", ".ttf", ".otf", ".svg", ".eot"]

class Userform(tornado.web.RequestHandler):
    def get(self):
        self.render("fileuploadform.html")

class Upload(tornado.web.RequestHandler):
    @staticmethod
    def cssGenerator(name, fullname):
        cssFile = name + ".css"
        template = "@font-face {\
            \n\tfont-family: '" + fullname + "';\
            \n\tsrc: url('" + name + ".eot');\
            \n\tsrc: url('" + name + ".eot?#iefix') format('embedded-opentype'),\
            \n\turl('" + name + ".woff') format('woff'),\
            \n\turl('" + name + ".ttf') format('truetype'),\
            \n\turl('" + name + ".svg#ywftsvg') format('svg');\
            \n\tfont-style: normal;\
            \n\tfont-weight: normal;\
            \n}\n\n"
        open(cssFile, 'w+').writelines(template)

    @staticmethod
    def fontGenerator(filename):
        #exts = ["woff", "ttf", "otf", "svg", "eot"]
        name = os.path.splitext(filename)[0]
        '''if not os.path.exists(name):
            os.makedirs(name)'''

        font = fontforge.open(filename)
        fullname = font.fullname

        for ext in EXTS:
            f = name + ext
            font.generate(f)

    def post(self):
        fileinfo = self.request.files['filearg'][0]
        fname = fileinfo['filename']
        extn = os.path.splitext(fname)[1]
        cname = str(uuid.uuid4()) + extn
        zname = os.path.splitext(fname)[0]
        fh = open(cname, 'w+')
        fh.write(fileinfo['body'])
        os.system("mv " + cname + " " + zname + extn)
        #zname is default, extn is .svg, fname is default.svg, cname is file name in the server
        self.cssGenerator(zname, 'testfont')
        self.fontGenerator(zname + extn)

application = tornado.web.Application([
    (r"/", Userform),
    (r"/upload", Upload),
    ], debug=True)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

启动服务器并单击客户端上的“上传”按钮后的输出:

strippedname:/home/bill/proto.svg
/home/bill/proto.svg:109: parser error : AttValue: ' expected
-18 403 -18 L 403 -18 C 352 -18 301 0 251 0 C 225 0 210 15 210 41 C 210 63 225 7
                                                                               ^
/home/bill/proto.svg:109: parser error : attributes construct error
-18 403 -18 L 403 -18 C 352 -18 301 0 251 0 C 225 0 210 15 210 41 C 210 63 225 7
                                                                               ^
/home/bill/proto.svg:109: parser error : Couldn't find end of Start Tag glyph line 109
-18 403 -18 L 403 -18 C 352 -18 301 0 251 0 C 225 0 210 15 210 41 C 210 63 225 7
                                                                               ^
/home/bill/proto.svg:109: parser error : Premature end of data in tag font line 6
-18 403 -18 L 403 -18 C 352 -18 301 0 251 0 C 225 0 210 15 210 41 C 210 63 225 7
                                                                               ^
/home/bill/proto.svg:109: parser error : Premature end of data in tag defs line 5
-18 403 -18 L 403 -18 C 352 -18 301 0 251 0 C 225 0 210 15 210 41 C 210 63 225 7
                                                                               ^
/home/bill/proto.svg:109: parser error : Premature end of data in tag svg line 3
-18 403 -18 L 403 -18 C 352 -18 301 0 251 0 C 225 0 210 15 210 41 C 210 63 225 7
                                                                               ^
Couldn't find a font file named /home/bill/proto.svg
proto.svg is not in a known format (or uses features of that format fontforge does not support, or is so badly corrupted as to be unreadable)
ERROR:tornado.application:Uncaught exception POST /upload (82.225.61.131)
HTTPServerRequest(protocol='http', host='py-fontconv.cloudapp.net', method='POST', uri='/upload', version='HTTP/1.1', remote_ip='82.225.61.131', headers={'Content-Length': '32378', 'Accept-Language': 'en-us,en;q=0.8,fr;q=0.5,fr-fr;q=0.3', 'Accept-Encoding': 'gzip, deflate', 'Host': 'py-fontconv.cloudapp.net', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:28.0) Gecko/20100101 Firefox/28.0', 'Connection': 'keep-alive', 'Content-Type': 'multipart/form-data; boundary=---------------------------191335620930032341323215978'})
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/web.py", line 1332, in _execute
    result = method(*self.path_args, **self.path_kwargs)
  File "tornadofileupload.py", line 53, in post
    self.fontGenerator(zname + extn)
  File "tornadofileupload.py", line 35, in fontGenerator
    font = fontforge.open(filename)
EnvironmentError: Open failed
ERROR:tornado.access:500 POST /upload (82.225.61.131) 283.75ms

如果有人发现这里缺少什么,那将是一种解脱,因为我真的在这方面很挣扎。

最佳答案

写入后需要刷新或关闭fh。最好的方法是使用 with 语句:

with open(cname, 'w+') as f:
    f.write(fileinfo['body'])

在下一行使用 mv 是非常不安全的。有人可以上传一个名为 $(rm -rf *) 的文件,并且该命令将被执行。切勿将 os.system 与不受信任的输入一起使用。在这种情况下,shutil 模块提供了替代方案。直接使用用户提供的文件名作为 zname 也是有问题的 - 生成您自己的文件名总是比使用客户端提供的文件名更好。

关于python - fontforge无法解析Tornado服务器中的svg文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25187115/

相关文章:

html - 我们可以跨多个 <svg> 元素引用 <defs> 的内容吗?

python - 如何进行线程安全字典操作

python - Tensorflow tf.switch_case 不适用于 keras 输入

python - 如何限制列表中的位数?

linux - ubuntu bash 在系统中打印结果带有额外的 ""

html - Bootstrap 导航栏图像大小

python - 使用Python删除文件夹中的所有内容

ubuntu - 如何使终端打开带有焦点的gui应用程序?

linux - Hadoop在两台不同名称的机器上配置多节点集群

javascript - 强制导向图停止响应