コード例 #1
0
ファイル: tthDupe.py プロジェクト: texervn/fontlab-scripts
def run(writeCoordinates=False):

    # Get the folder that contains the source hinting data,
    # and source font files:
    templateFolderPath = fl.GetPathName(
        "Select directory that contains the 'tthints' template file...")
    if not templateFolderPath:
        'Cancel was clicked or ESC was pressed'
        return

    tthintsFilePath = os.path.join(templateFolderPath, kTTHintsFileName)

    # Verify that the files tthints, font.pfa/ufo and font.ttf exist
    # in the folder provided:
    if not os.path.exists(tthintsFilePath):
        print "ERROR: Could not find %s file." % kTTHintsFileName
        return

    # Check if any of the possible template fonts exists -- PFA, TXT, or UFO:
    pfaFilePath = os.path.join(templateFolderPath, kPFAFileName)
    txtFilePath = os.path.join(templateFolderPath, kTXTFileName)
    ufoFilePath = os.path.join(templateFolderPath, kUFOFileName)

    if os.path.exists(pfaFilePath):
        pass
    elif os.path.exists(txtFilePath):
        pass
    elif os.path.exists(ufoFilePath):
        pass
    else:
        print "ERROR: Could not find any of the following font files: %s, %s or %s." % (
            kPFAFileName, kTXTFileName, kUFOFileName)
        return

    # Check if font.ttf exists in source folder:
    ttfFilePath = os.path.join(templateFolderPath, kTTFFileName)
    if not os.path.exists(ttfFilePath):
        print "ERROR: Could not find %s file." % kTTFFileName
        return

    # Get the (root) folder containingt the target font files:
    baseFolderPath = fl.GetPathName("Select top directory that contains the fonts to process ...")
    if not baseFolderPath:
        'Cancel was clicked or ESC key was pressed'
        return

    startTime = time.time()

    # Create a list of glyphs that have been hinted so it can be
    # used as a filter. The rawHintingDict contains a string of raw
    # hinting data for each glyph:
    glyphList, rawHintingDict = readTTHintsFile(tthintsFilePath)
    folderPathsList = getFolderPaths(baseFolderPath, templateFolderPath)

    if len(folderPathsList):
        delete_temporary_template_PFA = False
        print "Processing template files..."
        fl.Open(ttfFilePath)
        templateTTfont = fl[fl.ifont]
        if not os.path.exists(pfaFilePath) and os.path.exists(txtFilePath):
            delete_temporary_template_PFA = True
            makePFAfromTXT(txtFilePath, pfaFilePath)
        elif not os.path.exists(pfaFilePath) and os.path.exists(ufoFilePath):
            delete_temporary_template_PFA = True
            makePFAfromUFO(ufoFilePath, pfaFilePath, glyphList)
        fl.Open(pfaFilePath)
        templateT1font = fl[fl.ifont]

        # Make a Robofab font of the Type1 template font. This RB font is made
        # by copying each glyph. There does not seem to be a simpler method
        # that produces reliable results -- the challenge comes from having
        # to close the FL font downstream.
        templateT1RBfont = RFont()
        currentT1RBfont = CurrentFont()

        for gName in glyphList:
            g = currentT1RBfont[gName]
            templateT1RBfont.insertGlyph(g)

        hintedNodeDict, indexOnlyRawHintingDict, okToProcessTargetFonts = collectTemplateIndexes(
            templateTTfont, templateT1font, glyphList, rawHintingDict)
        closeAllOpenedFonts()

        if okToProcessTargetFonts:
            processTargetFonts(
                folderPathsList, templateT1RBfont, hintedNodeDict,
                glyphList, indexOnlyRawHintingDict, writeCoordinates)
        else:
            print "Can't process target fonts because of hinting errors found in template font."

        if delete_temporary_template_PFA:
            if os.path.exists(pfaFilePath):
                os.remove(pfaFilePath)

    else:
        print "Could not find suitable folders to process."

    endTime = time.time()
    elapsedSeconds = endTime-startTime

    if (elapsedSeconds/60) < 1:
        print '\nCompleted in %.1f seconds.\n' % elapsedSeconds
    else:
        print '\nCompleted in %s minutes and %s seconds.\n' % (
            elapsedSeconds/60, elapsedSeconds%60)