def test_check_names_are_ascii_only(self): """ NAME and CFF tables must not contain non-ascii characters """ font = Font.get_ttfont(self.path) for name_record in font.names: string = Font.bin2unistring(name_record) try: string.encode('ascii') except UnicodeEncodeError: self.fail("%s contain non-ascii chars" % name_record.nameID)
def test_check_italic_angle_agreement(self): """ Check italicangle property zero or negative """ font = Font.get_ttfont(self.path) if font.italicAngle > 0: self.fail('italicAngle must be less or equal zero') if abs(font.italicAngle) > 20: self.fail('italicAngle can\'t be larger than 20 degrees')
def test_metadata_family(self): """ Font and METADATA.json have the same name """ for font_metadata in self.metadata.fonts: font = Font.get_ttfont_from_metadata(self.path, font_metadata) if font.familyname != font_metadata.name: msg = 'Family name in TTF is "%s" but in METADATA "%s"' self.fail(msg % (font.familyname, font_metadata.name))
def test_metrics_descents_equal_bbox(self): """ Check that descents values are same as min glyph point """ contents = self.read_metadata_contents() family_metadata = Metadata.get_family_metadata(contents) fonts_descents_not_bbox = [] ymin = 0 _cache = {} for font_metadata in family_metadata.fonts: ttfont = Font.get_ttfont_from_metadata(self.path, font_metadata) ymin_, ymax_ = ttfont.get_bounding() ymin = min(ymin, ymin_) _cache[font_metadata.filename] = { 'os2typo': abs(ttfont.descents.os2typo), 'os2win': abs(ttfont.descents.os2win), 'hhea': abs(ttfont.descents.hhea) } for filename, data in _cache.items(): if [data['os2typo'], data['os2win'], data['hhea']] != [abs(ymin)] * 3: fonts_descents_not_bbox.append(filename) if fonts_descents_not_bbox: _ = '[%s] ascents differ to minimum value: %s' self.fail(_ % (', '.join(fonts_descents_not_bbox), ymin))
def test_check_font_has_dsig_table(self): """ Check that font has DSIG table """ font = Font.get_ttfont(self.path) try: font['DSIG'] except KeyError: self.fail('Font does not have "DSIG" table')
def test_check_menu_contains_proper_glyphs(self): """ Check menu file contains proper glyphs """ contents = self.read_metadata_contents() fm = Metadata.get_family_metadata(contents) for font_metadata in fm.fonts: tf = Font.get_ttfont_from_metadata(self.path, font_metadata, is_menu=True) self.check_retrieve_glyphs(tf, font_metadata)
def test_check_normal_style_matches_names(self): """ Check metadata.json font.style `italic` matches font internal """ contents = self.read_metadata_contents() family = Metadata.get_family_metadata(contents) for font_metadata in family.fonts: if font_metadata.style != 'normal': continue font = Font.get_ttfont_from_metadata(self.path, font_metadata) if bool(font.macStyle & 0b10): self.fail(('Metadata style has been set to normal' ' but font second bit (italic) in macStyle has' ' been set')) style = font.familyname.split('-')[-1] if style.endswith('Italic'): self.fail(('macStyle second bit is not set but postScriptName "%s"' ' is ended with "Italic"') % font.familyname) style = font.fullname.split('-')[-1] if style.endswith('Italic'): self.fail(('macStyle second bit is not set but fullName "%s"' ' is ended with "Italic"') % font.fullname)
def test_fontname_is_equal_to_macstyle(self): """ Check that fontname is equal to macstyle flags """ font = Font.get_ttfont(self.path) fontname = font.fullname macStyle = font.macStyle try: fontname_style = fontname.split('-')[1] except IndexError: self.fail(('Fontname is not canonical. Expected it contains ' 'style. eg.: Italic, BoldItalic, Regular')) style = '' if macStyle & 0b01: style += 'Bold' if macStyle & 0b10: style += 'Italic' if not bool(macStyle & 0b11): style = 'Regular' if not fontname_style.endswith(style): _ = 'macStyle (%s) supposed style ended with "%s" but ends with "%s"' self.fail(_ % (bin(macStyle)[-2:], style, fontname_style))
def test_check_names_same_across_platforms(self): """ Font names are same across specific-platforms """ font = Font.get_ttfont(self.path) for name in font.names: for name2 in font.names: if name.nameID != name2.nameID: continue if self.diff_platform(name, name2) or self.diff_platform(name2, name): _name = Font.bin2unistring(name) _name2 = Font.bin2unistring(name2) if _name != _name2: msg = ('Names in "name" table are not the same' ' across specific-platforms') self.fail(msg)
def test_check_upm_heigths_less_120(self): """ Check if UPM Heights NOT more than 120% """ ttfont = Font.get_ttfont(self.path) value = ttfont.ascents.get_max() + abs(ttfont.descents.get_min()) value = value * 100 / float(ttfont.get_upm_height()) if value > 120: _ = "UPM:Height is %d%%, consider redesigning to 120%% or less" self.fail(_ % value)
def metricfix(fonts): from cli.ttfont import Font ymin = 0 ymax = 0 for f in fonts: metrics = Font(f) font_ymin, font_ymax = metrics.get_bounding() ymin = min(font_ymin, ymin) ymax = max(font_ymax, ymax) for f in fonts: metrics = Font(f) metrics.ascents.set(ymax) metrics.descents.set(ymin) metrics.linegaps.set(0) metrics.save(f + '.fix')
def test_no_kern_table_exists(self): """ Check that no "KERN" table exists """ font = Font.get_ttfont(self.path) try: font['KERN'] self.fail('Font does not have "DSIG" table') except KeyError: pass
def test_font_name_matches_family(self): """ METADATA.json fonts 'name' property should be same as font familyname """ for font_metadata in self.metadata.fonts: font = Font.get_ttfont_from_metadata(self.path, font_metadata) if font_metadata.name != font.familyname: msg = '"fonts.name" property is not the same as TTF familyname' self.fail(msg)
def test_prep_magic_code(self): """ Font contains in PREP table magic code """ magiccode = '\xb8\x01\xff\x85\xb0\x04\x8d' font = Font.get_ttfont(self.path) try: bytecode = font.get_program_bytecode() except KeyError: bytecode = '' self.assertTrue(bytecode == magiccode, msg='PREP does not contain magic code')
def test_the_same_names_of_glyphs_across_family(self): """ The same names of glyphs across family? """ glyphs = None for font_metadata in self.familymetadata.fonts: ttfont = Font.get_ttfont_from_metadata(self.path, font_metadata) if not glyphs: glyphs = len(ttfont.glyphs) if glyphs != len(ttfont.glyphs): self.fail('Family has a different glyphs\'s names in fonts')
def test_the_same_number_of_glyphs_across_family(self): """ The same number of glyphs across family? """ glyphs_count = 0 for font_metadata in self.familymetadata.fonts: ttfont = Font.get_ttfont_from_metadata(self.path, font_metadata) if not glyphs_count: glyphs_count = len(ttfont.glyphs) if glyphs_count != len(ttfont.glyphs): self.fail('Family has a different glyphs\'s count in fonts')
def test_check_hmtx_hhea_max_advance_width_agreement(self): """ Check if MaxAdvanceWidth agree in the Hmtx and Hhea tables """ font = Font.get_ttfont(self.path) hmtx_advance_width_max = font.get_hmtx_max_advanced_width() hhea_advance_width_max = font.advance_width_max error = ("AdvanceWidthMax mismatch: expected %s (from hmtx);" " got %s (from hhea)") % (hmtx_advance_width_max, hhea_advance_width_max) self.assertEqual(hmtx_advance_width_max, hhea_advance_width_max, error)
def test_check_full_font_name_begins_with_family_name(self): """ Check if full font name begins with the font family name """ font = Font.get_ttfont(self.path) for entry in font.names: if entry.nameID != 1: continue for entry2 in font.names: if entry2.nameID != 4: continue if (entry.platformID == entry2.platformID and entry.platEncID == entry2.platEncID and entry.langID == entry2.langID): entry2value = Font.bin2unistring(entry2) entryvalue = Font.bin2unistring(entry) if not entry2value.startswith(entryvalue): _ = ('Full font name does not begin with family' ' name: FontFamilyName = "%s";' ' FullFontName = "%s"') self.fail(_ % (entryvalue, entry2value))
def test_postscriptname_contains_correct_weight(self): """ Metadata weight matches postScriptName """ contents = self.read_metadata_contents() fm = Metadata.get_family_metadata(contents) for font_metadata in fm.fonts: font = Font.get_ttfont_from_metadata(self.path, font_metadata) if font.OS2_usWeightClass != font_metadata.weight: msg = 'METADATA.JSON has weight %s but in TTF it is %s' self.fail(msg % (font_metadata.weight, font.OS2_usWeightClass))
def test_check_metadata_matches_nametable(self): contents = self.read_metadata_contents() fm = Metadata.get_family_metadata(contents) for font_metadata in fm.fonts: ttfont = Font.get_ttfont_from_metadata(self.path, font_metadata) report = '%s: Family name was supposed to be "%s" but is "%s"' report = report % (font_metadata.name, fm.name, ttfont.familyname) self.assertEqual(ttfont.familyname, fm.name, report) self.assertEqual(ttfont.fullname, font_metadata.full_name)
def test_metadata_fonts_fields_have_fontname(self): """ METADATA.json fonts items fields "name", "postScriptName", "fullName", "filename" contains font name right format """ for x in self.metadata.fonts: font = Font.get_ttfont_from_metadata(self.path, x) self.assertIn(font.familyname, x.name) self.assertIn(font.familyname, x.full_name) self.assertIn("".join(str(font.familyname).split()), x.filename) self.assertIn("".join(str(font.familyname).split()), x.post_script_name)
def test_the_same_encodings_of_glyphs_across_family(self): """ The same unicode encodings of glyphs across family? """ encoding = None for font_metadata in self.familymetadata.fonts: ttfont = Font.get_ttfont_from_metadata(self.path, font_metadata) cmap = ttfont.retrieve_cmap_format_4() if not encoding: encoding = cmap.platEncID if encoding != cmap.platEncID: self.fail('Family has different encoding across fonts')
def test_check_unused_glyph_data(self): f = Font.get_ttfont(self.path) glyf_length = f.get_glyf_length() loca_num_glyphs = f.get_loca_num_glyphs() last_glyph_offset = f.get_loca_glyph_offset(loca_num_glyphs - 1) last_glyph_length = f.get_loca_glyph_length(loca_num_glyphs - 1) unused_data = glyf_length - (last_glyph_offset + last_glyph_length) error = ("there were %s bytes of unused data at the end" " of the glyf table") % unused_data self.assertEqual(unused_data, 0, error)
def test_metrics_linegaps_are_zero(self): """ Check that linegaps in tables are zero """ contents = self.read_metadata_contents() family_metadata = Metadata.get_family_metadata(contents) fonts_gaps_are_not_zero = [] for font_metadata in family_metadata.fonts: ttfont = Font.get_ttfont_from_metadata(self.path, font_metadata) if bool(ttfont.linegaps.os2typo) or bool(ttfont.linegaps.hhea): fonts_gaps_are_not_zero.append(font_metadata.filename) if fonts_gaps_are_not_zero: _ = '[%s] have not zero linegaps' self.fail(_ % ', '.join(fonts_gaps_are_not_zero))
def test_license_included_in_font_names(self): """ Check font has a correct license url """ font = Font.get_ttfont(self.path) regex = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+' r'(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain... r'localhost|' # localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) if not regex.match(font.license_url): self.fail("LicenseUrl is required and must be correct url")
def test_gpos_table_has_kerning_info(self): """ GPOS table has kerning information """ font = Font.get_ttfont(self.path) try: font['GPOS'] except KeyError: self.fail('"GPOS" does not exist in font') flaglookup = False for lookup in font['GPOS'].table.LookupList.Lookup: if lookup.LookupType == 2: # Adjust position of a pair of glyphs flaglookup = lookup break # break for..loop to avoid reading all kerning info self.assertTrue(flaglookup, msg='GPOS doesnt have kerning information') self.assertGreater(flaglookup.SubTableCount, 0) self.assertGreater(flaglookup.SubTable[0].PairSetCount, 0)
def test_Check_Postscriptname_Glyph_Conventions(self): """ Check glyphs names comply with PostScript conventions """ font = Font.get_ttfont(self.path) for glyph in font.glyphs(): if glyph == '.notdef': continue if not re.match('(^[.0-9])[a-zA-Z_][a-zA-Z_0-9]{,30}', glyph): self.fail(('Glyph "%s" does not comply conventions.' ' A glyph name may be up to 31 characters in length,' ' must be entirely comprised of characters from' ' the following set:' ' A-Z a-z 0-9 .(period) _(underscore). and must not' ' start with a digit or period. The only exception' ' is the special character ".notdef". "twocents",' ' "a1", and "_" are valid glyph names. "2cents"' ' and ".twocents" are not.'))
def test_check_nbsp_width_matches_sp_width(self): """ Check NO-BREAK SPACE advanceWidth is the same as SPACE """ contents = self.read_metadata_contents() fm = Metadata.get_family_metadata(contents) for font_metadata in fm.fonts: tf = Font.get_ttfont_from_metadata(self.path, font_metadata) space_advance_width = tf.advance_width('space') nbsp_advance_width = tf.advance_width('uni00A0') _ = "%s: The font does not contain a sp glyph" self.assertTrue(space_advance_width, _ % font_metadata.filename) _ = "%s: The font does not contain a nbsp glyph" self.assertTrue(nbsp_advance_width, _ % font_metadata.filename) _ = ("%s: The nbsp advance width does not match " "the sp advance width") % font_metadata.filename self.assertEqual(space_advance_width, nbsp_advance_width, _)
def test_check_gasp_table_type(self): """ Font table gasp should be 15 """ font = Font.get_ttfont(self.path) try: font['gasp'] except KeyError: self.fail('"GASP" table not found') if not isinstance(font['gasp'].gaspRange, dict): self.fail('GASP.gaspRange method value have wrong type') if 65535 not in font['gasp'].gaspRange: self.fail("GASP does not have 65535 gaspRange") # XXX: Needs review if font['gasp'].gaspRange[65535] != 15: self.fail('gaspRange[65535] value is not 15')
def test_check_glyf_table_length(self): """ Check if there is unused data at the end of the glyf table """ font = Font.get_ttfont(self.path) expected = font.get_loca_length() actual = font.get_glyf_length() diff = actual - expected # allow up to 3 bytes of padding if diff > 3: _ = ("Glyf table has unreachable data at the end of the table." " Expected glyf table length %s (from loca table), got length" " %s (difference: %s)") % (expected, actual, diff) self.fail(_) elif diff < 0: _ = ("Loca table references data beyond the end of the glyf table." " Expected glyf table length %s (from loca table), got length" " %s (difference: %s)") % (expected, actual, diff) self.fail(_)