Beispiel #1
0
def main():
    argparser = ArgumentParser(
        description=
        'Generate kerning samples by providing the left-hand side glyph')

    argparser.add_argument(
        '-u',
        dest='formatAsUnicode',
        action='store_const',
        const=True,
        default=False,
        help='Format output as unicode escape sequences instead of glyphnames. '
        + 'E.g. "\\u2126" instead of "\\Omega"')

    argparser.add_argument('-prefix',
                           dest='prefix',
                           metavar='<text>',
                           type=str,
                           help='Text to append before each pair')

    argparser.add_argument('-suffix',
                           dest='suffix',
                           metavar='<text>',
                           type=str,
                           help='Text to append after each pair')

    argparser.add_argument(
        '-no-prefix-autocase',
        dest='noPrefixAutocase',
        action='store_const',
        const=True,
        default=False,
        help='Do not convert -prefix and -suffix to match case')

    argparser.add_argument(
        '-all-in-groups',
        dest='includeAllInGroup',
        action='store_const',
        const=True,
        default=False,
        help=
        'Include all glyphs for groups rather than just the first glyph listed.'
    )

    argparser.add_argument(
        '-left',
        dest='asLeft',
        action='store_const',
        const=True,
        default=False,
        help='Only include pairs where the glyphnames are on the left side.')

    argparser.add_argument(
        '-right',
        dest='asRight',
        action='store_const',
        const=True,
        default=False,
        help='Only include pairs where the glyphnames are on the right side.' +
        ' When neither -left or -right is provided, include all pairs.')

    argparser.add_argument('fontPath',
                           metavar='<ufofile>',
                           type=str,
                           help='UFO font source')

    argparser.add_argument(
        'glyphnames',
        metavar='<glyphname>',
        type=str,
        nargs='+',
        help='Name of glyphs to generate samples for. ' +
        'You can also provide a Unicode code point using the syntax "U+XXXX"')

    args = argparser.parse_args()

    font = Font(args.fontPath)

    groupsFilename = os.path.join(args.fontPath, 'groups.plist')
    kerningFilename = os.path.join(args.fontPath, 'kerning.plist')

    groups = plistlib.readPlist(groupsFilename)  # { groupName => [glyphName] }
    kerning = plistlib.readPlist(
        kerningFilename)  # { leftName => {rightName => kernVal} }
    groupmap = mapGroups(groups)  # { glyphname => set(groupname, ...), ... }

    if not args.asLeft and not args.asRight:
        args.asLeft = True
        args.asRight = True

    # expand any unicode codepoints
    glyphnames = []
    for glyphname in args.glyphnames:
        if len(glyphname) > 2 and glyphname[:2] == 'U+':
            cp = int(glyphname[2:], 16)
            ucmap = font.getCharacterMapping()  # { 2126: ['Omega', ...], ...}
            for glyphname2 in ucmap[cp]:
                glyphnames.append(glyphname2)
        else:
            glyphnames.append(glyphname)

    for glyphname in glyphnames:
        if args.asLeft:
            samplesForGlyphnameL(font, groups, groupmap, kerning, glyphname,
                                 args)
        if args.asRight:
            samplesForGlyphnameR(font, groups, groupmap, kerning, glyphname,
                                 args)