Exemple #1
0
def _MakeMetadata(fontdir):
    """Builds a dictionary matching a METADATA.pb file.

  Args:
    fontdir: Directory containing font files for which we want metadata.
  Returns:
    OrderedDict of a complete METADATA.pb structure.
  """
    file_family_style_weights = _FileFamilyStyleWeights(fontdir)

    first_file = file_family_style_weights[0].file
    subsets = ['menu'] + [
        s[0] for s in fonts.SubsetsInFont(first_file, FLAGS.min_pct,
                                          FLAGS.min_pct_ext)
    ]
    old_metadata_file = os.path.join(fontdir, 'METADATA.pb')

    font_license = fonts.LicenseFromPath(fontdir)

    metadata = fonts_pb2.FamilyProto()
    metadata.name = file_family_style_weights[0].family

    if FLAGS.update and os.path.isfile(old_metadata_file):
        old_metadata = fonts_pb2.FamilyProto()
        with open(old_metadata_file, "rb") as old_meta:
            text_format.Parse(old_meta.read(), old_metadata)
            metadata.designer = old_metadata.designer
            metadata.category = old_metadata.category
            metadata.date_added = old_metadata.date_added
    else:
        metadata.designer = 'UNKNOWN'
        metadata.category = 'SANS_SERIF'
        metadata.date_added = time.strftime('%Y-%m-%d')

    metadata.license = font_license
    subsets = sorted(subsets)
    for subset in subsets:
        metadata.subsets.append(subset)

    for (fontfile, family, style, weight) in file_family_style_weights:
        filename = os.path.basename(fontfile)
        font_psname = fonts.ExtractName(fontfile, fonts.NAME_PSNAME,
                                        os.path.splitext(filename)[0])
        font_copyright = fonts.ExtractName(fontfile, fonts.NAME_COPYRIGHT,
                                           '???.')

        font_metadata = metadata.fonts.add()
        font_metadata.name = family
        font_metadata.style = style
        font_metadata.weight = weight
        font_metadata.filename = filename
        font_metadata.post_script_name = font_psname
        default_fullname = os.path.splitext(filename)[0].replace('-', ' ')
        font_metadata.full_name = fonts.ExtractName(fontfile,
                                                    fonts.NAME_FULLNAME,
                                                    default_fullname)
        font_metadata.copyright = font_copyright

    return metadata
def _get_metadata_category(meta_file):
    category = None
    metadata = fonts_pb2.FamilyProto()
    with open(meta_file, "rb") as meta:
        text_format.Parse(meta.read(), metadata)
        category = metadata.category
    return METADATA_CATEGORIES[category]
Exemple #3
0
def Metadata(file_or_dir):
    """Returns fonts_metadata.proto object for a metadata file.

  If file_or_dir is a file named METADATA.pb, load it. If file_or_dir is a
  directory, load the METADATA.pb file in that directory.

  Args:
    file_or_dir: A file or directory.
  Returns:
    Python object loaded from METADATA.pb content.
  Raises:
    ValueError: if file_or_dir isn't a METADATA.pb file or dir containing one.
  """
    if (os.path.isfile(file_or_dir)
            and os.path.basename(file_or_dir) == 'METADATA.pb'):
        metadata_file = file_or_dir
    elif os.path.isdir(file_or_dir):
        metadata_file = os.path.join(file_or_dir, 'METADATA.pb')
    else:
        raise ValueError('%s is neither METADATA.pb file or a directory' %
                         file_or_dir)

    msg = fonts_pb2.FamilyProto()
    with codecs.open(metadata_file, encoding='utf-8') as f:
        text_format.Merge(f.read(), msg)

    return msg
Exemple #4
0
def _MakeMetadata(fontdir):
  """Builds a dictionary matching a METADATA.pb file.

  Args:
    fontdir: Directory containing font files for which we want metadata.
  Returns:
    OrderedDict of a complete METADATA.pb structure.
  """
  file_family_style_weights = _FileFamilyStyleWeights(fontdir)

  first_file = file_family_style_weights[0].file
  subsets = ['menu'] + [s[0] for s in fonts.SubsetsInFont(first_file, 50)]

  font_license = fonts.LicenseFromPath(fontdir)


  metadata = fonts_pb2.FamilyProto()
  metadata.name = file_family_style_weights[0].family
  metadata.designer = 'Unknown'
  metadata.category = 'SANS_SERIF'
  metadata.license = font_license
  for subset in subsets:
    metadata.subsets.append(subset)
  metadata.date_added = time.strftime('%Y-%m-%d')

  for (fontfile, family, style, weight) in file_family_style_weights:
    filename = os.path.basename(fontfile)
    font_psname = fonts.ExtractName(fontfile, fonts.NAME_PSNAME,
                                    os.path.splitext(filename)[0])
    font_copyright = fonts.ExtractName(fontfile, fonts.NAME_COPYRIGHT, '???.')

    font_metadata = metadata.fonts.add()
    font_metadata.name = family
    font_metadata.style = style
    font_metadata.weight = weight
    font_metadata.filename = filename
    font_metadata.post_script_name = font_psname
    default_fullname = os.path.splitext(filename)[0].replace('-', ' ')
    font_metadata.full_name = fonts.ExtractName(fontfile, fonts.NAME_FULLNAME,
                                                default_fullname)
    font_metadata.copyright = font_copyright

  return metadata
    return font_new_name


def clean_str(s):
    return s.replace(",", " ").replace("\"", "").replace("\n", "")


if __name__ == "__main__":
    font_metadata_path = sys.argv[1]
    font_dir_path = os.path.dirname(font_metadata_path)

    try:
        # Parse Google fonts metadata in protobuf format
        protobuf_file = open(font_metadata_path, 'r')
        protobuf = protobuf_file.read()
        font_family = fonts_public_pb2.FamilyProto()
        text_format.Merge(protobuf, font_family)
    except Exception as e:
        sys.stderr.write("Error: %s in %s%s" %
                         (str(e), font_dir_path, os.linesep))
        print(font_dir_path, "[Failed]")
        exit()

    with open(os.path.join(font_dir_path, "synbols_metadata"), "w") as f_md:
        with open(os.path.join(font_dir_path, "synbols_licenses"),
                  "w") as f_li:
            for font in font_family.fonts:
                alphabets = font_family.subsets

                # Edit TTF file
                ttf_path = os.path.join(font_dir_path, font.filename)