Exemplo n.º 1
0
def add_paths(layer, t, additional_transform=Identity):
    commands = ""
    for p in layer.paths:
        svgpen = SVGPathPen(None)
        transformpen = TransformPen(svgpen, t)
        componenttransformpen = TransformPen(transformpen, additional_transform)
        p.draw(componenttransformpen)
        commands += svgpen.getCommands()
    return commands
Exemplo n.º 2
0
    def copy(self):
        font = self._font
        widget = self.stackWidget.currentWidget()
        clipboard = QApplication.clipboard()
        mimeData = QMimeData()
        if self.isGlyphTab():
            glyph = widget.activeGlyph()
            copyGlyph = glyph.getRepresentation("TruFont.FilterSelection")
            packGlyphs = (copyGlyph,)
        else:
            glyphs = self.glyphCellView.glyphs()
            packGlyphs = (
                glyphs[index] for index in sorted(self.glyphCellView.selection())
            )

        svgGlyphs = []
        pickled = []
        for i, glyph in enumerate(packGlyphs):
            pickled.append(glyph.serialize(blacklist=("name", "unicodes")))

            pen = SVGPathPen(font)
            glyph.draw(pen)
            col = i % 5
            row = i // 5
            g = '<g transform="matrix(1,0,0,-1,{:f},{:f})"><path d="{}"/></g>'.format(
                font.info.unitsPerEm * col,
                font.info.unitsPerEm * row,
                pen.getCommands(),
            )
            svgGlyphs.append(g)

        mimeData.setData("application/x-trufont-glyph-data", pickle.dumps(pickled))

        svg = """\
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg">
%s
</svg>
""" % "\n".join(
            svgGlyphs
        )
        mimeData.setData("image/svg+xml", svg.encode("utf-8"))

        clipboard.setMimeData(mimeData)
Exemplo n.º 3
0
def _draw_svg_path(ttfont, icon_name, dest_region):
    glyph_name = icon_font.resolve_ligature(ttfont, icon_name)
    upem = ttfont["head"].unitsPerEm
    transform = _map_font_to_viewbox(upem, dest_region)
    transform = transform.translate(0, 0.7942 * upem)

    svg_pen = SVGPathPen(ttfont.getGlyphSet())
    ttfont.getGlyphSet()[glyph_name].draw(TransformPen(svg_pen, transform))
    return " ".join(svg_pen._commands)
def update_symbol(symbol, ttfont, icon_name, symbol_wght_name):
    glyph_name = icon_font.resolve_ligature(ttfont, icon_name)
    upem = ttfont["head"].unitsPerEm
    # For Icon fonts, the Glyphs are Y shifted by upem and the Y axis is flipped.
    symbol.write_icon(
        symbol_wght_name,
        ttfont.getGlyphSet()[glyph_name],
        SVGPathPen(ttfont.getGlyphSet()),
        Rect(0, upem, upem, -upem),
    )
Exemplo n.º 5
0
def _draw_svg_path(ttfont, icon_name, dest_region):
    glyph_name = icon_font.resolve_ligature(ttfont, icon_name)
    upem = ttfont["head"].unitsPerEm
    transform = _map_font_to_viewbox(upem, dest_region)
    # TODO(rsheeter) should use better targeting :)
    # move up by 80% of upem to hit target box
    transform = transform.translate(0, 0.8 * upem)

    svg_pen = SVGPathPen(ttfont.getGlyphSet())
    ttfont.getGlyphSet()[glyph_name].draw(TransformPen(svg_pen, transform))
    return " ".join(svg_pen._commands)
def main(argv):
    if len(argv) > 2:
        sys.exit("Expected Only 1 non-flag Argument.")
    symbol = Symbol()
    pico = SVG.parse(argv[1]).topicosvg()
    main_svg = pico.xpath_one("//svg:svg")
    symbol.write_icon(
        _REQUIRED_SYMBOL,
        svgLib.SVGPath.fromstring(pico.tostring()),
        SVGPathPen(None),
        Rect(
            0,
            0,
            parse_float(main_svg.get("width")),
            parse_float(main_svg.get("height")),
        ),
    )
    symbol.drop_empty_icons()
    symbol.write_to(FLAGS.out)
Exemplo n.º 7
0
from frontTools import ttLib
tt=ttLib.TTFont('fonts/NotoSansSC-Regular.otf')
# number of glyphs
tt['maxp'].numGlyphs
s='汉'
u=s.encode('unicode_escape')
# extract hex
gs=tt.getGlyphSet()
tt.getGlyphName(27721)
gl=gs.get('cid44176')
from fontTools.pens.svgPathPen import SVGPathPen
>>> pen = SVGPathPen(gs)
>>> pen.moveTo((0,0))
>>> pen.getCommands()
'M0 0'
gl.draw(pen) # returns a ton of what seems to be garbage?
Exemplo n.º 8
0
    def draw(self,
             text,
             scale=0.1,
             precision=10,
             space=0.1,
             ignoreLimit=False):
        # 使用geometry转换fontTools生成的svg path
        source = "测试demo"
        if type(text) != str:
            text = str(text)
        if len(text) > 0:
            source = text
        source = re.sub(r"[^ ^\n\S]", " ", source)  # 不可见的那些特殊符号当成空格处理
        source = source.replace("\\", "\\\\")
        if not ignoreLimit:
            if len(source) > 45:
                source = source[:45]
                logConsole.warning(
                    FutureWarning("单行模式暂时只支持45字符以内的文本. 内容`%s`被忽略" % text[46:]))
        elif len(text) == 0:
            logConsole.warning(Warning("输入不能为空. 默认内容为`测试demo`"))

        self.clean()  # 每次draw前清理一下
        self.source = list(source)

        cursor = [0, 0]
        for i, char in enumerate(source):  # 依次绘制每个字符

            if char != "\n":  # 换行符直接跳过
                try:  # 这个版本暂时没做failedback字体的功能
                    # TODO: 加上failedback字体
                    charName = self.bestcmap[ord(char)]
                    glyph = self.glyphSet[charName]
                    charWidth, charHeight = self.getCharSize(glyph=glyph,
                                                             scale=scale)
                except Exception as e:
                    logConsole.warning(f"取字`{char}`信息失败: {e}")
                    charName = self.bestcmap[ord("口")]
                    glyph = self.glyphSet[charName]
                    charWidth, charHeight = self.getCharSize(glyph=glyph,
                                                             scale=scale)
                if i == 0:
                    self.charPos.append([0, 0])  # 每个字的起始总是上一个字的起始加上(字宽+间距)
                    cursor = [(charWidth * (1 + space)), 0]
                else:
                    self.charPos.append([cursor[0], cursor[1] * charHeight])
                    cursor[0] += (charWidth * (1 + space))
            else:
                cursor[0] = 0  # \r
                cursor[1] += 1  # \n
                continue

            try:
                pen = SVGPathPen(
                    self.glyphSet
                )  # TODO: 待搞明白: 以前的看的例程都是传参`None`, 但实际使用发现这样会造成少数字符无法绘制的情况, 尚未找到原因.
                glyph.draw(pen)
                penCommandList = pen._commands
            except Exception as e:
                logConsole.error(f"绘制`{char}`错误: {e}")
                print(FutureWarning(f"绘制字符`{char}`时出错({e}), 跳过..."))
                continue

            x_previous = y_previous = 0.0  # 上一步操作的结果记录
            x_current = y_current = 0.0  # 当前正在操作的过程
            x_start = y_start = 0.0  # 每个路径的起点

            items = []  # 临时储存字的笔画

            for command in penCommandList:
                cmd = common.getCode(command)
                vec = common.getVert(command)

                flag_absolute = (cmd == cmd.upper())
                cmd = cmd.upper()

                if cmd == 'M':  # MoveTo
                    x_start = x_previous = vec[0]
                    y_start = y_previous = vec[1]
                    if flag_absolute is True:
                        x_current = vec[0]
                        y_current = vec[1]
                    else:
                        x_current = x_previous + vec[0]
                        y_current = y_previous + vec[1]

                    items.append(MoveTo(Point(x_current, y_current)))
                    x_previous, y_previous = x_current, y_current

                elif cmd in 'LHV':  # LineTo, Horizontal 和 Vertical line
                    # H和V需要单独取顶点
                    if flag_absolute is True:
                        x_current, y_current = x_previous, y_previous
                    else:
                        x_current, y_current = [0, 0]

                    if cmd == "H":  # 水平线共用Y, 更新X
                        x_current = vec[0]
                        y_current = y_previous
                    if cmd == "V":  # 垂直线更新Y
                        x_current = x_previous
                        y_current = vec[0]
                    if cmd == "L" and len(vec) == 2:
                        x_current, y_current = vec

                    if flag_absolute is False:
                        x_current += x_previous
                        y_current += y_previous

                    items.append(
                        Segment(Point(x_previous, y_previous),
                                Point(x_current, y_current)))
                    x_previous, y_previous = x_current, y_current

                elif cmd in 'CQ':
                    dimension = {'Q': 2, 'C': 3}
                    bezier_pts = []
                    bezier_pts.append(Point(x_previous, y_previous))

                    for i in range(dimension[cmd]):
                        x_current = vec[2 * i]
                        y_current = vec[2 * i + 1]
                        if flag_absolute is False:
                            x_current += x_previous
                            y_current += y_previous
                        bezier_pts.append(Point(x_current, y_current))

                    items.append(Bezier(bezier_pts))
                    x_previous, y_previous = x_current, y_current

                elif cmd == 'Z':  # 闭合
                    items.append(
                        Segment(Point(x_previous, y_previous),
                                Point(x_start, y_start)))

                else:
                    pass  # A暂时不能处理

            for item in items:
                item.scale(scale)
            self.Items.append(items)