首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Pyqt中的SVG字形

Pyqt中的SVG字形
EN

Stack Overflow用户
提问于 2010-09-10 11:43:13
回答 1查看 796关注 0票数 1

如何使用svggraphicsItem在pyqt中呈现字形?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-13 02:27:33

最近,我发现Cairo生成的svg文件在pyqt中不能正确打印。这个错误来自于字形的使用,这些字形似乎没有显示在pyqt中(这可能是错误的,但我找不到任何让字形呈现的方法)。

最后,我编写了一组将字形转换为svg路径的函数,以便正常呈现文件。

它们仍然可以使用一些改进来呈现颜色和其他样式元素(这些元素在我编写的函数中是锁定的)。

这些函数需要嵌入到一个类中,或者自行移除才能在其他地方使用。

我只是想让人们拥有这些,这样他们就不必像我那样到处寻找,找到一种在pyqt中渲染字形的方法。

往最好的方面想,凯尔

代码语言:javascript
运行
复制
def convertSVG(self, file): 
    dom = self._getsvgdom(file) 
    print dom 
    self._switchGlyphsForPaths(dom) 
    self._commitSVG(file, dom) 
def _commitSVG(self, file, dom): 
    f = open(file, 'w') 
    dom.writexml(f) 
    f.close() 
def _getsvgdom(self, file): 
    print 'getting DOM model' 
    import xml.dom 
    import xml.dom.minidom as mini 
    f = open(file, 'r') 
    svg = f.read() 
    f.close() 
    dom = mini.parseString(svg) 
    return dom 
def _getGlyphPaths(self, dom): 
    symbols = dom.getElementsByTagName('symbol') 
    glyphPaths = {} 
    for s in symbols: 
        pathNode = [p for p in s.childNodes if 'tagName' in dir(p) and p.tagName == 'path'] 
        glyphPaths[s.getAttribute('id')] = pathNode[0].getAttribute('d') 
    return glyphPaths 
def _switchGlyphsForPaths(self, dom): 
    glyphs = self._getGlyphPaths(dom) 
    use = self._getUseTags(dom) 
    for glyph in glyphs.keys(): 
        print glyph 
        nl = self.makeNewList(glyphs[glyph].split(' ')) 
        u = self._matchUseGlyphs(use, glyph) 
        for u2 in u: 
            print u2, 'brefore' 
            self._convertUseToPath(u2, nl) 
            print u2, 'after' 

def _getUseTags(self, dom): 
    return dom.getElementsByTagName('use') 
def _matchUseGlyphs(self, use, glyph): 
    matches = [] 
    for i in use: 
        print i.getAttribute('xlink:href') 
        if i.getAttribute('xlink:href') == '#'+glyph: 
            matches.append(i) 
    print matches 
    return matches 
def _convertUseToPath(self, use, strokeD): 
    ## strokeD is a list of lists of strokes to make the glyph 
    newD = self.nltostring(self.resetStrokeD(strokeD, use.getAttribute('x'), use.getAttribute('y'))) 
    use.tagName = 'path' 
    use.removeAttribute('xlink:href') 
    use.removeAttribute('x') 
    use.removeAttribute('y') 
    use.setAttribute('style', 'fill: rgb(0%,0%,0%); stroke-width: 0.5; stroke-linecap: round; stroke-linejoin: round; stroke: rgb(0%,0%,0%); stroke-opacity: 1;stroke-miterlimit: 10; ') 
    use.setAttribute('d', newD) 
def makeNewList(self, inList): 
    i = 0 
    nt = [] 
    while i < len(inList): 
        start = i + self.listFind(inList[i:], ['M', 'L', 'C', 'Z']) 
        end = start + self.listFind(inList[start+1:], ['M', 'L', 'C', 'Z', '', ' ']) 
        nt.append(inList[start:end+1]) 
        i = end + 1 
    return nt 
def listFind(self, x, query): 
    for i in range(len(x)): 
        if x[i] in query: 
            return i 
    return len(x) 
def resetStrokeD(self, strokeD, x, y): 
    nsd = [] 
    for i in strokeD: 
        nsd.append(self.resetXY(i, x, y)) 
    return nsd 
def resetXY(self, nl, x, y): # convert a list of strokes to xy coords 
    nl2 = [] 
    for i in range(len(nl)): 
        if i == 0: 
            nl2.append(nl[i]) 
        elif i%2: # it's odd 
            nl2.append(float(nl[i]) + float(x)) 
        elif not i%2: # it's even 
            nl2.append(float(nl[i]) + float(y)) 
        else: 
            print i, nl[i], 'error' 
    return nl2 
def nltostring(self, nl): # convert a colection of nl's to a string 
    col = [] 
    for l in nl: 
        templ = [] 
        for c in l: 
            templ.append(str(c)) 
        templ = ' '.join(templ) 
        col.append(templ) 
    return ' '.join(col)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3682127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档