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_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_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_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 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_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_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_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_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_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_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_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_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_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(_)
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_check_os2_width_class(self): font = Font.get_ttfont(self.path) error = "OS/2 widthClass must be [1..9] inclusive, was %s IE9 fail" error = error % font.OS2_usWidthClass self.assertIn(font.OS2_usWidthClass, range(1, 10), error)
def setUp(self): self.font = Font.get_ttfont(self.path)
def assertExists(self, d): font = Font.get_ttfont(self.path) glyphs = font.retrieve_cmap_format_4().cmap if not bool(ord(unicodedata.lookup(d)) in glyphs): self.fail('%s does not exist in font' % d)