Esempio n. 1
0
def convertFont(fontPath, fontType):
    options = Options()

    tmpOutputTtf = tmpFileName(".ttf")
    tmpOutputWoff = tmpFileName(".woff")

    font = TTFont(fontPath)

    ttfOptions = Options()
    # export the font as woff for web use
    woffOptions = Options()
    woffOptions.with_zopfli = True
    woffOptions.flavor = "woff"

    if fontType == "otf":
        # convert the font to ttf
        ttfFont = otf_to_ttf(font)
        # save font can also convert to woff!
        save_font(ttfFont, tmpOutputTtf, ttfOptions)
        save_font(ttfFont, tmpOutputWoff, woffOptions)
    elif fontType == "ttf":
        save_font(font, tmpOutputTtf, ttfOptions)
        save_font(font, tmpOutputWoff, woffOptions)
    else:
        print "wrong type"

    ttfBase64 = "data:;base64," + toBase64(tmpOutputTtf)
    woffBase64 = "data:;base64," + toBase64(tmpOutputWoff)

    #cleanup files
    cleanUp([tmpOutputWoff, tmpOutputTtf])
    print woffBase64.replace("\n", "")
    print ttfBase64.replace("\n", "")
Esempio n. 2
0
def subsetFont(base64, subset):
    # tmp file names
    tmpInputFontName = tmpFileName(".ttf")
    tmpOutputFontName = tmpFileName(".woff")

    # remove data header from base64
    fontbase64 = base64.split(",")[1]

    with open(tmpInputFontName, "wb") as f:
        fontinput = f.write(fontbase64.decode('base64'))
        f.close()

    # open the font with fontTools
    font = TTFont(tmpInputFontName)

    options = Options()
    options.desubroutinize = True

    # export the font as woff for web use
    options.with_zopfli = True
    options.flavor = "woff"

    subsetter = Subsetter(options=options)
    subsetter.populate(text=subset)
    subsetter.subset(font)

    save_font(font, tmpOutputFontName, options)

    subsettedFont = open(tmpOutputFontName, "rb").read().encode("base64")

    os.unlink(tmpOutputFontName)
    os.unlink(tmpInputFontName)

    return {'subset': subsettedFont}
Esempio n. 3
0
def convertFont(base64, type):
    options = Options()

    # tmp file names
    tmpInputFontName = tmpFileName("." + type)
    tmpOutputTtf = tmpFileName(".ttf")
    tmpOutputWoff = tmpFileName(".woff")

    # remove data header from base64
    # now we have a clean input source
    fontbase64 = base64.split(",")[1]
    with open(tmpInputFontName, "wb") as f:
        fontinput = f.write(fontbase64.decode('base64'))
        f.close()

    # we always work from a TTFont Object (also takes OTF)
    font = TTFont(tmpInputFontName)

    ttfOptions = Options()
    # export the font as woff for web use
    woffOptions = Options()
    woffOptions.with_zopfli = True
    woffOptions.flavor = "woff"

    if type == 'otf':
        # convert the font to ttf
        ttfFont = otf_to_ttf(font)
        # save font can also convert to woff!
        save_font(ttfFont, tmpOutputTtf, ttfOptions)
        save_font(ttfFont, tmpOutputWoff, woffOptions)
    elif type == 'ttf':
        save_font(font, tmpOutputTtf, ttfOptions)
        save_font(font, tmpOutputWoff, woffOptions)
    else:
        return {'error': 'please give a valid type'}

    ttfBase64 = toBase64(tmpOutputTtf)
    woffBase64 = toBase64(tmpOutputWoff)

    #cleanup files
    cleanUp([tmpInputFontName, tmpOutputWoff, tmpOutputTtf])

    return {'woff': woffBase64, 'ttf': ttfBase64}
Esempio n. 4
0
def subsetFont(fontPath, subset):
    tmpOutputFontName = os.path.dirname(
        os.path.abspath(__file__)) + "/tmp/" + str(uuid.uuid4()) + ".woff"

    font = TTFont(fontPath)

    options = Options()
    options.desubroutinize = True

    options.with_zopfli = True
    options.flavor = "woff"

    subsetter = Subsetter(options=options)
    subsetter.populate(text=subset)
    subsetter.subset(font)

    save_font(font, tmpOutputFontName, options)
    subsettedFont = 'data:;base64,' + open(tmpOutputFontName,
                                           "rb").read().encode("base64")

    cleanUp([tmpOutputFontName])

    print subsettedFont.replace('\n', '')