Source code for hTools2.modules.fontinfo

# [h] hTools2.modules.fontinfo

"""
Tools to get and set different kinds of font information.

See the `UFO documentation <http://unifiedfontobject.org/versions/ufo2/fontinfo.html>`_.

"""

# imports

import os

from fileutils import get_names_from_path

# set info

[docs]def set_font_names(font, family_name, style_name): """Set several font naming fields from ``family`` and ``style`` names.""" full_name = '%s_%s' % (family_name, style_name) # main family/style names font.info.familyName = family_name font.info.styleName = style_name # style map names font.info.styleMapFamilyName = None # full_name font.info.styleMapStyleName = None # 'regular' # opentype names font.info.openTypeNamePreferredFamilyName = None # family_name font.info.openTypeNamePreferredSubfamilyName = None # style_name font.info.openTypeNameCompatibleFullName = None # full_name font.info.openTypeNameUniqueID = None # postscript names font.info.postscriptFontName = None # full_name font.info.postscriptFullName = None # full_name font.info.postscriptUniqueID = None font.info.postscriptWeightName = None # FOND names font.info.macintoshFONDFamilyID = None font.info.macintoshFONDName = None
[docs]def set_names_from_path(font, prefix=None): """Set the font naming fields using parts of the name of the font file.""" family_name, style_name = get_names_from_path(font.path) if prefix: family_name = prefix + ' ' + family_name set_font_names(font, family_name, style_name) # vertical metrics
[docs]def set_vmetrics(font, xheight, capheight, ascender, descender, emsquare, gridsize=1): font.info.xHeight = xheight * gridsize font.info.capHeight = capheight * gridsize font.info.descender = -abs(descender * gridsize) font.info.ascender = ascender * gridsize font.info.unitsPerEm = emsquare * gridsize # ps hinting
from robofab.pens.marginPen import MarginPen
[docs]def get_stems(font): ref_glyph = 'i' ref_y = font.info.xHeight / 2 g = font[ref_glyph] pen = MarginPen(g, ref_y, isHorizontal=True) g.draw(pen) left_edge, right_edge = pen.getMargins() stem = right_edge - left_edge return [ stem ]
[docs]def set_stems(font, stems): font.info.postscriptStemSnapH = stems # print info
[docs]def clear_font_info(font): """Clears all font information fields in the font.""" # print 'deleting font info' clear_generic_identification(font) clear_generic_legal(font) clear_generic_dimension(font) clear_generic_miscellaneous(font) clear_opentype_head(font) clear_opentype_hhea(font) clear_opentype_name(font) clear_opentype_os2(font) clear_opentype_vhea(font) clear_postscript_data(font)
[docs]def clear_generic_identification(font): # print 'deleting Generic Identification Information' font.info.familyName = None font.info.styleName = None font.info.styleMapFamilyName = None font.info.styleMapStyleName = None font.info.versionMajor = None font.info.versionMinor = None font.info.year = None
[docs]def clear_generic_dimension(font): # print 'deleting Generic Dimension Information' font.info.unitsPerEm = None font.info.descender = None font.info.xHeight = None font.info.capHeight = None font.info.ascender = None font.info.italicAngle = None
[docs]def clear_generic_miscellaneous(font): # print 'deleting Generic Miscellaneous Information' font.info.note = None
[docs]def clear_opentype_head(font): # print 'deleting OpenType head Table Fields' font.info.openTypeHeadCreated = None font.info.openTypeHeadLowestRecPPEM = None font.info.openTypeHeadFlags = None
[docs]def clear_opentype_hhea(font): # print 'deleting OpenType hhea Table Fields' font.info.openTypeHheaAscender = None font.info.openTypeHheaDescender = None font.info.openTypeHheaLineGap = None font.info.openTypeHheaCaretSlopeRise = None font.info.openTypeHheaCaretSlopeRun = None font.info.openTypeHheaCaretOffset = None
[docs]def clear_opentype_name(font): # print 'deleting OpenType Name Table Fields' font.info.openTypeNameDesigner = None font.info.openTypeNameDesignerURL = None font.info.openTypeNameManufacturer = None font.info.openTypeNameManufacturerURL = None font.info.openTypeNameLicense = None font.info.openTypeNameLicenseURL = None font.info.openTypeNameVersion = None font.info.openTypeNameUniqueID = None font.info.openTypeNameDescription = None font.info.openTypeNamePreferredFamilyName = None font.info.openTypeNamePreferredSubfamilyName = None font.info.openTypeNameCompatibleFullName = None font.info.openTypeNameSampleText = None font.info.openTypeNameWWSFamilyName = None font.info.openTypeNameWWSSubfamilyName = None
[docs]def clear_opentype_os2(font): # print 'deleting OpenType OS/2 Table Fields' font.info.openTypeOS2WidthClass = None font.info.openTypeOS2WeightClass = None font.info.openTypeOS2Selection = None font.info.openTypeOS2VendorID = None font.info.openTypeOS2Panose = None font.info.openTypeOS2FamilyClass = None font.info.openTypeOS2UnicodeRanges = None font.info.openTypeOS2CodePageRanges = None font.info.openTypeOS2TypoAscender = None font.info.openTypeOS2TypoDescender = None font.info.openTypeOS2TypoLineGap = None font.info.openTypeOS2WinAscent = None font.info.openTypeOS2WinDescent = None font.info.openTypeOS2Type = None font.info.openTypeOS2SubscriptXSize = None font.info.openTypeOS2SubscriptYSize = None font.info.openTypeOS2SubscriptXOffset = None font.info.openTypeOS2SubscriptYOffset = None font.info.openTypeOS2SuperscriptXSize = None font.info.openTypeOS2SuperscriptYSize = None font.info.openTypeOS2SuperscriptXOffset = None font.info.openTypeOS2SuperscriptYOffset = None font.info.openTypeOS2StrikeoutSize = None font.info.openTypeOS2StrikeoutPosition = None
[docs]def clear_opentype_vhea(font): # print 'deleting OpenType vhea Table Fields' font.info.openTypeVheaVertTypoAscender = None font.info.openTypeVheaVertTypoDescender = None font.info.openTypeVheaVertTypoLineGap = None font.info.openTypeVheaCaretSlopeRise = None font.info.openTypeVheaCaretSlopeRun = None font.info.openTypeVheaCaretOffset = None
[docs]def clear_postscript_data(font): # print 'deleting PostScript Specific Data' font.info.postscriptFontName = None font.info.postscriptFullName = None font.info.postscriptSlantAngle = None font.info.postscriptUniqueID = None font.info.postscriptUnderlineThickness = None font.info.postscriptUnderlinePosition = None font.info.postscriptIsFixedPitch = None font.info.postscriptBlueValues = None font.info.postscriptOtherBlues = None font.info.postscriptFamilyBlues = None font.info.postscriptFamilyOtherBlues = None font.info.postscriptStemSnapH = None font.info.postscriptStemSnapV = None font.info.postscriptBlueFuzz = None font.info.postscriptBlueShift = None font.info.postscriptBlueScale = None font.info.postscriptForceBold = None font.info.postscriptDefaultWidthX = None font.info.postscriptNominalWidthX = None font.info.postscriptWeightName = None font.info.postscriptDefaultCharacter = None font.info.postscriptWindowsCharacterSet = None