Esempio n. 1
0
def processFontFile(fontFilePath, svgFilePathsList):
    # retrieve the font's glyph order, to determine the GID later
    font = ttLib.TTFont(fontFilePath)
    glyphOrder = font.getGlyphOrder()

    # first create a dictionary because the SVG glyphs need to be sorted in the table
    svgDocsDict = {}
    for svgFilePath in svgFilePathsList:
        gName = getGlyphNameFromFileName(svgFilePath)
        try:
            gid = glyphOrder.index(gName)
        except ValueError:
            print >> sys.stderr, "ERROR: Could not find a glyph named %s in the font %s." % (
                gName, os.path.split(fontFilePath)[1])
            continue
        svgItemsList = []
        svgItemData = readFile(svgFilePath)
        svgItemData = setIDvalue(svgItemData, gid)
        svgItemData = fixViewBox(svgItemData)
        svgItemsList.append(svgItemData)
        svgItemsList.extend([gid, gid])
        svgDocsDict[gid] = svgItemsList

    # don't do any changes to the source OTF/TTF font if there's no SVG data
    if not svgDocsDict:
        return

    svgDocsList = [svgDocsDict[index] for index in sorted(svgDocsDict.keys())]

    svgTable = S_V_G_.table_S_V_G_()
    svgTable.docList = svgDocsList
    svgTable.colorPalettes = None
    font['SVG '] = svgTable

    # FontTools can't overwrite a font on save,
    # so save to a hidden file, and then rename it
    # https://github.com/behdad/fonttools/issues/302
    folderPath, fontFileName = os.path.split(fontFilePath)
    fileNameNoExtension, fileExtension = os.path.splitext(fontFileName)
    newFontFilePath = os.path.join(
        folderPath, "%s%s%s" % ('.', fileNameNoExtension, fileExtension))

    font.save(newFontFilePath)
    font.close()
    # On windows file can't be renamed to file what already exist.
    os.remove(fontFilePath)
    os.rename(newFontFilePath, fontFilePath)

    print >> sys.stdout, "\nSVG table successfully added to %s" % fontFilePath
Esempio n. 2
0
def processFontFile(fontFilePath, svgFilePathsList):
    font = ttLib.TTFont(fontFilePath)

    # first create a dictionary because the SVG glyphs need to be sorted in the table
    svgDocsDict = {}
    for svgFilePath in svgFilePathsList:
        gName = getGlyphNameFromFileName(svgFilePath)
        try:
            gid = font.getGlyphID(gName)
        except KeyError:
            print >> sys.stderr, "ERROR: Could not find a glyph named %s in the font %s." % (
                gName,
                os.path.split(fontFilePath)[1],
            )
            continue
        svgItemsList = []
        svgItemData = readFile(svgFilePath)
        svgItemData = setIDvalue(svgItemData, gid)
        svgItemData = fixViewBox(svgItemData)
        svgItemsList.append(svgItemData)
        svgItemsList.extend([gid, gid])
        svgDocsDict[gid] = svgItemsList

        # don't do any changes to the source OTF/TTF font if there's no SVG data
    if not svgDocsDict:
        return

    svgDocsList = [svgDocsDict[index] for index in sorted(svgDocsDict.keys())]

    svgTable = S_V_G_.table_S_V_G_()
    svgTable.docList = svgDocsList
    svgTable.colorPalettes = None
    font["SVG "] = svgTable

    # FontTools can't overwrite a font on save,
    # so save to a hidden file, and then rename it
    # https://github.com/behdad/fonttools/issues/302
    folderPath, fontFileName = os.path.split(fontFilePath)
    fileNameNoExtension, fileExtension = os.path.splitext(fontFileName)
    newFontFilePath = os.path.join(folderPath, "%s%s%s" % (".", fileNameNoExtension, fileExtension))

    font.save(newFontFilePath)
    font.close()
    # On windows file can't be renamed to file what already exist.
    os.remove(fontFilePath)
    os.rename(newFontFilePath, fontFilePath)

    print >> sys.stdout, "\nSVG table successfully added to %s" % fontFilePath
Esempio n. 3
0
    def init_svg(self):
        """Call this if you expect to add SVG images in the font. This calls init_glyf since SVG
    support currently requires fallback glyf records for each SVG image."""

        if hasattr(self, 'svgs'):
            return

        # svg requires glyf
        self.init_glyf()

        font = self.font
        if 'SVG ' not in font:
            svg_table = SVG.table_S_V_G_()
            svg_table.docList = []
            svg_table.colorPalettes = None
            font['SVG '] = svg_table
        self.svgs = font['SVG '].docList
Esempio n. 4
0
  def init_svg(self):
    """Call this if you expect to add SVG images in the font. This calls init_glyf since SVG
    support currently requires fallback glyf records for each SVG image."""

    if hasattr(self, 'svgs'):
      return

    # svg requires glyf
    self.init_glyf()

    font = self.font
    if 'SVG ' not in font:
      svg_table = SVG.table_S_V_G_()
      svg_table.docList = []
      svg_table.colorPalettes = None
      font['SVG '] = svg_table
    self.svgs = font['SVG '].docList
def processFontFile(fontFilePath, svgFilePathsList):
	# retrieve the font's glyph order, to determine the GID later
	font = ttLib.TTFont(fontFilePath)
	glyphOrder = font.getGlyphOrder()

	# first create a dictionary because the SVG glyphs need to be sorted in the table
	svgDocsDict = {}
	for svgFilePath in svgFilePathsList:
		gName = getGlyphNameFromFileName(svgFilePath)
		try:
			gid = glyphOrder.index(gName)
		except ValueError:
			print >> sys.stderr, "ERROR: Could not find a glyph named %s in the font %s." % (gName, os.path.split(fontFilePath)[1])
			continue
		svgItemsList = []
		svgItemData = readFile(svgFilePath)
		svgItemData = setIDvalue(svgItemData, gid)
		svgItemData = fixViewBox(svgItemData)
		svgItemsList.append(svgItemData)
		svgItemsList.extend([gid, gid])
		svgDocsDict[gid] = svgItemsList

	# don't do any changes to the source OTF/TTF font if there's no SVG data
	if not svgDocsDict:
		return

	svgDocsList = [svgDocsDict[index] for index in sorted(svgDocsDict.keys())]

	svgTable = S_V_G_.table_S_V_G_()
	svgTable.docList = svgDocsList
	svgTable.colorPalettes = None
	font['SVG '] = svgTable

	# FontTools can't overwrite a font on save,
	# so save to a hidden file, and then rename it
	# https://github.com/behdad/fonttools/issues/302
	folderPath, fontFileName = os.path.split(fontFilePath)
	fileNameNoExtension, fileExtension = os.path.splitext(fontFileName)
	newFontFilePath = os.path.join(folderPath, "%s%s%s" % ('.', fileNameNoExtension, fileExtension))

	font.save(newFontFilePath)
	os.rename(newFontFilePath, fontFilePath)