Exemple #1
0
def main():
    argparser = ArgumentParser(
        description=
        'Generate info on name, unicodes and color mark for all glyphs')

    argparser.add_argument(
        '-ucd',
        dest='ucdFile',
        metavar='<file>',
        type=str,
        help='UnicodeData.txt file from http://www.unicode.org/')

    argparser.add_argument('fontPath', metavar='<ufofile>', type=str)

    args = argparser.parse_args()
    font = Font(args.fontPath)
    ucd = {}
    if args.ucdFile:
        ucd = parseUnicodeDataFile(args.ucdFile)

    glyphs = []  # contains final glyph data printed as JSON
    seenGlyphnames = set()

    for name in font.lib['public.glyphOrder']:
        g = font[name]
        glyph = processGlyph(g, ucd, seenGlyphnames)
        if glyph is not None:
            glyphs.append(glyph)

    unorderedGlyphs = []
    for g in font:
        glyph = processGlyph(g, ucd, seenGlyphnames)
        if glyph is not None:
            unorderedGlyphs.append(glyph)

    if unorderedGlyphs:
        # sort by unicode
        glyphs = glyphs + sorted(unorderedGlyphs, key=lambda g: g[2])

    print('{"glyphs":[')
    prefix = '  '
    for g in glyphs:
        print(prefix + json.dumps(g))
        if prefix == '  ':
            prefix = ', '
    print(']}')
Exemple #2
0
def main():
  argparser = ArgumentParser(
    description='Set robofont color marks on glyphs based on unicode categories')

  argparser.add_argument(
    '-dry', dest='dryRun', action='store_const', const=True, default=False,
    help='Do not modify anything, but instead just print what would happen.')

  argparser.add_argument(
    '-ucd', dest='ucdFile', metavar='<file>', type=str,
    help='UnicodeData.txt file from http://www.unicode.org/')

  argparser.add_argument(
    'fontPaths', metavar='<ufofile>', type=str, nargs='+', help='UFO fonts to update')

  args = argparser.parse_args()
  dryRun = args.dryRun
  markLibKey = 'com.typemytype.robofont.mark'

  ucd = {}
  if args.ucdFile:
    ucd = parseUnicodeDataFile(args.ucdFile)

  for fontPath in args.fontPaths:
    font = OpenFont(fontPath)
    for g in font:
      rgba = colorForGlyph(g.name, g.unicodes, ucd)
      if rgba is None:
        if markLibKey in g.lib:
          del g.lib[markLibKey]
      else:
        g.lib[markLibKey] = [float(n) for n in rgba]

    print('Write', fontPath)
    if not dryRun:
      font.save()
Exemple #3
0
def main():
    argparser = ArgumentParser(
        description=
        'Generate info on name, unicodes and color mark for all glyphs')

    argparser.add_argument(
        '-ucd',
        dest='ucdFile',
        metavar='<file>',
        type=str,
        help='UnicodeData.txt file from http://www.unicode.org/')

    argparser.add_argument('fontPaths',
                           metavar='<ufofile>',
                           type=str,
                           nargs='+',
                           help='UFO fonts to update')

    args = argparser.parse_args()

    fontPaths = []
    for fontPath in args.fontPaths:
        fontPath = fontPath.rstrip('/ ')
        if 'regular' or 'Regular' in fontPath:
            fontPaths = [fontPath] + fontPaths
        else:
            fontPaths.append(fontPath)

    fonts = [Font(fontPath) for fontPath in args.fontPaths]

    ucd = {}
    if args.ucdFile:
        ucd = parseUnicodeDataFile(args.ucdFile)

    glyphs = []  # contains final glyph data printed as JSON
    visitedGlyphNames = set()

    for font in fonts:
        glyphorder = font.lib['public.glyphOrder']
        for name in glyphorder:
            if name in visitedGlyphNames:
                continue

            if name not in font:
                print(
                    "warning: %r in public.glyphOrder but doesn't exist in font"
                    % name,
                    file=sys.stderr)
                continue

            g = font[name]

            # color
            color = None
            if 'public.markColor' in g.lib:
                rgba = [
                    float(c.strip())
                    for c in g.lib['public.markColor'].strip().split(',')
                ]
                color = rgbaToCSSColor(*rgba)

            # mtime
            mtime = None
            if 'com.schriftgestaltung.Glyphs.lastChange' in g.lib:
                datetimestr = g.lib['com.schriftgestaltung.Glyphs.lastChange']
                mtime = localDateTimeToUTCStr(datetimestr)

            # name[, unicode[, unicodeName[, color]]]
            glyph = None
            ucs = g.unicodes
            if len(ucs):
                for uc in ucs:
                    ucName = unicodeName(ucd.get(uc))
                    if not ucName and uc >= 0xE000 and uc <= 0xF8FF:
                        ucName = '[private use %04X]' % uc

                    if color:
                        glyph = [name, uc, ucName, mtime, color]
                    elif mtime:
                        glyph = [name, uc, ucName, mtime]
                    elif ucName:
                        glyph = [name, uc, ucName]
                    else:
                        glyph = [name, uc]
            else:
                if color:
                    glyph = [name, None, None, mtime, color]
                elif mtime:
                    glyph = [name, None, None, mtime]
                else:
                    glyph = [name]

            glyphs.append(glyph)
            visitedGlyphNames.add(name)

    print('{"glyphs":[')
    prefix = '  '
    for g in glyphs:
        print(prefix + json.dumps(g))
        if prefix == '  ':
            prefix = ', '
    print(']}')
Exemple #4
0
def main():
    argparser = ArgumentParser(
        description=
        'Generate info on name, unicodes and color mark for all glyphs')

    argparser.add_argument(
        '-ucd',
        dest='ucdFile',
        metavar='<file>',
        type=str,
        help='UnicodeData.txt file from http://www.unicode.org/')

    argparser.add_argument('fontPaths',
                           metavar='<ufofile>',
                           type=str,
                           nargs='+',
                           help='UFO fonts to update')

    args = argparser.parse_args()
    markLibKey = 'com.typemytype.robofont.mark'

    fontPaths = []
    for fontPath in args.fontPaths:
        fontPath = fontPath.rstrip('/ ')
        if 'regular' or 'Regular' in fontPath:
            fontPaths = [fontPath] + fontPaths
        else:
            fontPaths.append(fontPath)

    fonts = [OpenFont(fontPath) for fontPath in args.fontPaths]

    agl = loadAGL('src/glyphlist.txt')  # { 2126: 'Omega', ... }
    diacriticComps = loadGlyphCompositions('src/diacritics.txt')
    uc2names, name2ucs, allNames = loadLocalNamesDB(fonts, agl, diacriticComps)

    ucd = {}
    if args.ucdFile:
        ucd = parseUnicodeDataFile(args.ucdFile)

    glyphorder = OrderedDict()
    with open(
            os.path.join(os.path.dirname(args.fontPaths[0]), 'glyphorder.txt'),
            'r') as f:
        for name in f.read().splitlines():
            if len(name) and name[0] != '#':
                glyphorder[name] = True

    for name in diacriticComps.iterkeys():
        glyphorder[name] = True

    glyphNames = glyphorder.keys()
    visitedGlyphNames = set()
    glyphs = []

    for font in fonts:
        for name, v in glyphorder.iteritems():
            if name in visitedGlyphNames:
                continue

            g = None
            ucs = []
            try:
                g = font[name]
                ucs = g.unicodes
            except:
                ucs = name2ucs.get(name)
                if ucs is None:
                    continue

            color = None
            if g is not None and markLibKey in g.lib:
                # TODO: translate from (r,g,b,a) to #RRGGBB (skip A)
                rgba = g.lib[markLibKey]
                if isinstance(rgba, list) or isinstance(rgba, tuple):
                    color = rgbaToCSSColor(*rgba)
            elif name in diacriticComps:
                color = '<derived>'

            # name[, unicode[, unicodeName[, color]]]
            if len(ucs):
                for uc in ucs:
                    ucName = unicodeName(ucd.get(uc))

                    if not ucName and uc >= 0xE000 and uc <= 0xF8FF:
                        ucName = '[private use %04X]' % uc

                    if color:
                        glyph = [name, uc, ucName, color]
                    elif ucName:
                        glyph = [name, uc, ucName]
                    else:
                        glyph = [name, uc]

                    glyphs.append(glyph)
            else:
                glyph = [name, None, None, color] if color else [name]
                glyphs.append(glyph)

            visitedGlyphNames.add(name)

    print('{"glyphs":[')
    prefix = '  '
    for g in glyphs:
        print(prefix + json.dumps(g))
        if prefix == '  ':
            prefix = ', '
    print(']}')
def main():
    argparser = ArgumentParser(
        description=
        'Generate info on name, unicodes and color mark for all glyphs')

    argparser.add_argument(
        '-ucd',
        dest='ucdFile',
        metavar='<file>',
        type=str,
        help='UnicodeData.txt file from http://www.unicode.org/')

    argparser.add_argument('fontPath', metavar='<ufofile>', type=str)

    args = argparser.parse_args()
    font = Font(args.fontPath)
    ucd = {}
    if args.ucdFile:
        ucd = parseUnicodeDataFile(args.ucdFile)

    glyphs = []  # contains final glyph data printed as JSON

    for name in font.lib['public.glyphOrder']:
        g = font[name]

        # not exported?
        if 'com.schriftgestaltung.Glyphs.Export' in g.lib:
            if not g.lib['com.schriftgestaltung.Glyphs.Export']:
                continue

        # color
        color = None
        if 'public.markColor' in g.lib:
            rgba = [
                float(c.strip())
                for c in g.lib['public.markColor'].strip().split(',')
            ]
            color = rgbaToCSSColor(*rgba)

        isEmpty = 0
        if not g.bounds or g.bounds[3] == 0:
            isEmpty = 1

        # name, isEmpty, unicode, unicodeName, color
        glyph = None
        ucs = g.unicodes
        if len(ucs):
            for uc in ucs:
                ucName = unicodeName(ucd.get(uc))
                # if not ucName and uc >= 0xE000 and uc <= 0xF8FF:
                #   ucName = '[private use %04X]' % uc

                ucstr = '%04X' % uc
                if color:
                    glyph = [name, isEmpty, ucstr, ucName, color]
                elif ucName:
                    glyph = [name, isEmpty, ucstr, ucName]
                else:
                    glyph = [name, isEmpty, ucstr]
        else:
            if color:
                glyph = [name, isEmpty, None, None, color]
            else:
                glyph = [name, isEmpty]

        glyphs.append(glyph)

    print('{"glyphs":[')
    prefix = '  '
    for g in glyphs:
        print(prefix + json.dumps(g))
        if prefix == '  ':
            prefix = ', '
    print(']}')