Pyqt 中的 SVG 字形

发布于 2024-09-18 16:42:09 字数 41 浏览 6 评论 0原文

如何使用 svggraphicsItem 在 pyqt 中渲染字形?

How do I render glyphs in pyqt using the svggraphicsItem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏已如深秋| 2024-09-25 16:42:09

最近我发现 Cairo 生成的 svg 文件在 pyqt 中无法正确绘制。该错误来自于使用似乎未在 pyqt 中显示的字形(这可能是错误的,但我找不到任何获取字形渲染的方法)。

我最终编写了一组函数,将字形转换为 svg 路径,以便文件正常渲染。

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

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

我只是希望人们拥有这些,这样他们就不必像我一样寻找在 pyqt 中渲染字形的方法。

希望能做到最好,
凯尔

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)

Recently I found that svg files generated by Cairo do not plot properly in pyqt. The error comes from the use of glyphs which seem not to be shown in pyqt (this might be wrong but I couldn't find any way of getting glyphs to render).

I ended up writing a set of functions that will convert the glyphs to svg paths so the file will render normally.

These could still use some improvements for rendering color and other style elements (which are locked in the functions that I wrote).

These functions will need to be embedded in a class or have self removed to be used elsewhere.

I just wanted people to have these so they wouldn't have to search high and low like I did to find a way to render glyphs in pyqt.

Hope for the best,
Kyle

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文