def test_find_font_match_italic(self): font_match = get_fontconfig().find_font('arial', size=12.0, italic=True) self.assertIsNotNone(font_match) self.assertEqual(font_match.name.lower(), 'arial') self.assertIn('arial', font_match.file.lower()) self.assertEqual(font_match.size, 12.0) self.assertFalse(font_match.bold) self.assertTrue(font_match.italic)
def test_find_font_no_match(self): """Even if the font does not exist we get a default result.""" font_match = get_fontconfig().find_font('unknown') self.assertIsNotNone(font_match) self.assertIsNotNone(font_match.name) self.assertIsNotNone(font_match.file) self.assertNotEqual(font_match.name.lower(), 'unknown') self.assertNotIn('unknown', font_match.file.lower())
def test_find_font_match_bold(self): font_match = get_fontconfig().find_font('arial', size=12.0, bold=True) self.assertIsNotNone(font_match) self.assertEqual(font_match.name.lower(), 'arial') self.assertIn('arial', font_match.file.lower()) self.assertEqual(font_match.size, 12.0) self.assertTrue(font_match.bold) self.assertFalse(font_match.italic)
def _get_font_metrics_workaround(self): # Workaround broken fonts with no metrics. Has been observed with # courR12-ISO8859-1.pcf.gz: "Courier" "Regular" # # None of the metrics fields are filled in, so render a glyph and # grab its height as the ascent, and make up an arbitrary # descent. i = get_fontconfig().char_index(self.face, 'X') error = FT_Load_Glyph(self.face, i, FT_LOAD_RENDER) FreeTypeError.check_and_raise_on_error('Could load glyph for "X"', error) self.ascent = self.face.available_sizes.contents.height self.descent = -self.ascent // 4 # arbitrary.
def test_face_from_fontconfig(font_name, bold, italic): """Test loading a font face from the system using font config.""" match = get_fontconfig().find_font(font_name, 16, bold, italic) assert match is not None face = FreeTypeFace.from_fontconfig(match) assert face.name == font_name assert face.family_name == font_name assert face.bold == bold assert face.italic == italic del face
def _load_font_face_from_system(self): match = get_fontconfig().find_font(self.name, self.size, self.bold, self.italic) if not match: raise base.FontException('Could not match font "%s"' % self.name) font_face = match.face if not font_face: # Try to load from file directly if not match.file: raise base.FontException('No filename for "%s"' % self.name) font_face = self._load_font_face_from_file(match.file) return font_face
def have_font(cls, name): # Check memory cache first name = name.lower() for font in list(cls._memory_fonts.values()): if font.name.lower() == name: return True # Check system match = get_fontconfig().find_font(name, 12, False, False) if match: # Check the name matches, fontconfig can return a default if name and match.name and match.name.lower() != name.lower(): return False return True else: return False
def have_font(cls, name): # Check memory cache first name = name.lower() for font in cls._memory_fonts.values(): if font.name.lower() == name: return True # Check system match = get_fontconfig().find_font(name, 12, False, False) if match: # Check the name matches, fontconfig can return a default if name and match.name and match.name.lower() != name.lower(): return False return True else: return False
def test_find_font_match_name(self): font_match = get_fontconfig().find_font('arial') self.assertIsNotNone(font_match) self.assertEqual(font_match.name.lower(), 'arial') self.assertIn('arial', font_match.file.lower())
def test_find_font_existing(self): font_match = get_fontconfig().find_font('arial') self.assertIsNotNone(font_match) self.assertIsNotNone(font_match.name) self.assertIsNotNone(font_match.file)
def test_find_font_default(self): font_match = get_fontconfig().find_font(None) self.assertIsNotNone(font_match) self.assertIsNotNone(font_match.name) self.assertIsNotNone(font_match.file)
def get_character_index(self, character): assert self.face return get_fontconfig().char_index(self.face, character)
def have_font(cls, name): if cls._memory_faces.contains(name): return True else: return get_fontconfig().have_font(name)
def _load_font_face_from_system(self): match = get_fontconfig().find_font(self.name, self.size, self.bold, self.italic) if not match: raise base.FontException('Could not match font "%s"' % self.name) self.face = FreeTypeFace.from_fontconfig(match)
def get_character_index(self, character): return get_fontconfig().char_index(self.ft_face, character)