示例#1
0
    def test_freetype_Face_init(self):

        self.assertRaises(IOError, ft.Face, os.path.join (FONTDIR, 'nonexistant.ttf'))

        f = self._TEST_FONTS['sans']
        self.assertTrue(isinstance(f, ft.Face))

        f = self._TEST_FONTS['fixed']
        self.assertTrue(isinstance(f, ft.Face))

        f = ft.Face(None, ptsize=24)
        self.assert_(f.height > 0)
        self.assertRaises(IOError, f.__init__,
                          os.path.join(FONTDIR, 'nonexistant.ttf'))
        self.assertRaises(RuntimeError, f.get_rect, 'a', ptsize=24)
        
        # Test attribute preservation during reinitalization
        f = ft.Face(self._sans_path, ptsize=24, ucs4=True)
        self.assertEqual(f.name, 'Liberation Sans')
        self.assertFalse(f.fixed_width)
        self.assertTrue(f.antialiased)
        self.assertFalse(f.oblique)
        self.assertTrue(f.ucs4)
        f.antialiased = False
        f.oblique = True
        f.__init__(self._fixed_path)
        self.assertEqual(f.name, 'Inconsolata')
        ##self.assertTrue(f.fixed_width)
        self.assertFalse(f.fixed_width)  # need a properly marked Mono font
        self.assertFalse(f.antialiased)
        self.assertTrue(f.oblique)
        self.assertTrue(f.ucs4)
示例#2
0
    def setUp(self):
        ft.init()

        if 'fixed' not in self._TEST_FONTS:
            # Inconsolata is an open-source font designed by Raph Levien
            # Licensed under the Open Font License
            # http://www.levien.com/type/myfonts/inconsolata.html
            self._TEST_FONTS['fixed'] = ft.Face(self._fixed_path)

        if 'sans' not in self._TEST_FONTS:
            # Liberation Sans is an open-source font designed by Steve Matteson
            # Licensed under the GNU GPL
            # https://fedorahosted.org/liberation-fonts/
            self._TEST_FONTS['sans'] = ft.Face(self._sans_path)
示例#3
0
 def test_freetype_Face_resolution(self):
     text = "|"  # Differs in width and height
     resolution = ft.get_default_resolution()
     new_face = ft.Face(self._sans_path, resolution=2 * resolution)
     self.assertEqual(new_face.resolution, 2 * resolution)
     size_normal = self._TEST_FONTS['sans'].get_rect(text, ptsize=24).size
     size_scaled = new_face.get_rect(text, ptsize=24).size
     size_by_2 = size_normal[0] * 2
     self.assertTrue(size_by_2 + 2 >= size_scaled[0] >= size_by_2 - 2,
                     "%i not equal %i" % (size_scaled[1], size_by_2))
     size_by_2 = size_normal[1] * 2
     self.assertTrue(size_by_2 + 2 >= size_scaled[1] >= size_by_2 - 2,
                     "%i not equal %i" % (size_scaled[1], size_by_2))
     new_resolution = resolution + 10
     ft.set_default_resolution(new_resolution)
     try:
         new_face = ft.Face(self._sans_path, resolution=0)
         self.assertEqual(new_face.resolution, new_resolution)
     finally:
         ft.set_default_resolution()
示例#4
0
def run():
    pygame.init()

    fontdir = os.path.dirname(os.path.abspath(__file__))
    face = freetype.Face(os.path.join(fontdir, "data", "sans.ttf"))

    screen = pygame.display.set_mode((800, 600))
    screen.fill(colors["grey_light"])

    face.render((screen, 32, 32),
                "Hello World",
                colors["red"],
                colors['grey_dark'],
                ptsize=64,
                style=freetype.STYLE_UNDERLINE | freetype.STYLE_OBLIQUE)

    face.render((screen, 32, 128),
                "abcdefghijklm",
                colors["grey_dark"],
                colors["green"],
                ptsize=64)

    face.vertical = True
    face.render((screen, 32, 190),
                "Vertical?",
                colors["blue"],
                None,
                ptsize=32)
    face.vertical = False

    face.render((screen, 64, 190),
                "Let's spin!",
                colors["red"],
                None,
                ptsize=48,
                rotation=55)

    face.render((screen, 160, 290),
                "All around!",
                colors["green"],
                None,
                ptsize=48,
                rotation=-55)

    face.render((screen, 250, 220),
                "and BLEND",
                pygame.Color(255, 0, 0, 128),
                None,
                ptsize=64)

    face.render((screen, 258, 237),
                "or BLAND!",
                pygame.Color(0, 0xCC, 28, 128),
                None,
                ptsize=64)

    face.origin = True
    for angle in range(0, 360, 45):
        face.render((screen, 200, 500),
                    ")",
                    pygame.Color('black'),
                    ptsize=48,
                    rotation=angle)
    face.vertical = True
    for angle in range(15, 375, 30):
        face.render((screen, 600, 400),
                    "|^*",
                    pygame.Color('orange'),
                    ptsize=48,
                    rotation=angle)
    face.vertical = False
    face.origin = False

    utext = pygame.compat.as_unicode(r"I \u2665 Unicode")
    face.render((screen, 298, 320),
                utext,
                pygame.Color(0, 0xCC, 0xDD),
                None,
                ptsize=64)

    utext = pygame.compat.as_unicode(r"\u2665")
    face.render((screen, 480, 32),
                utext,
                colors["grey_light"],
                colors["red"],
                ptsize=148)

    face.render((screen, 380, 380),
                "...yes, this is an SDL surface",
                pygame.Color(0, 0, 0),
                None,
                ptsize=24,
                style=freetype.STYLE_STRONG)

    pygame.display.flip()

    while 1:
        if pygame.event.wait().type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):
            break

    pygame.quit()
示例#5
0
 def test_freetype_defaultfont(self):
     face = ft.Face(None)
     self.assertEqual(face.name, "FreeSans")
示例#6
0
    def test_freetype_Face_cache(self):
        glyphs = "abcde"
        glen = len(glyphs)
        other_glyphs = "123"
        oglen = len(other_glyphs)
        uempty = unicode_("")
##        many_glyphs = (uempty.join([unichr_(i) for i in range(32,127)] +
##                                   [unichr_(i) for i in range(161,172)] +
##                                   [unichr_(i) for i in range(174,239)]))
        many_glyphs = uempty.join([unichr_(i) for i in range(32,127)])
        mglen = len(many_glyphs)

        count = 0
        access = 0
        hit = 0
        miss = 0

        f = ft.Face(None, ptsize=24, style=ft.STYLE_NORMAL, vertical=False)
        f.antialiased = True

        # Ensure debug counters are zero
        self.assertEqual(f._debug_cache_stats, (0, 0, 0, 0, 0))
        # Load some basic glyphs
        count = access = miss = glen
        f.render_raw(glyphs)
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # Vertical should not affect the cache
        access += glen
        hit += glen
        f.vertical = True
        f.render_raw(glyphs)
        f.vertical = False
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # New glyphs will
        count += oglen
        access += oglen
        miss += oglen
        f.render_raw(other_glyphs)
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # Point size does
        count += glen
        access += glen
        miss += glen
        f.render_raw(glyphs, ptsize=12)
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # Underline style does not
        access += oglen
        hit += oglen
        f.underline = True
        f.render_raw(other_glyphs)
        f.underline = False
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # Oblique style does
        count += glen
        access += glen
        miss += glen
        f.oblique = True
        f.render_raw(glyphs)
        f.oblique = False
        self.assertEqual(f._debug_cache_stats, (count, 0, access, hit, miss))
        # Strong style does; by this point cache clears can happen
        count += glen
        access += glen
        miss += glen
        f.strong = True
        f.render_raw(glyphs)
        f.strong = False
        ccount, cdelete_count, caccess, chit, cmiss = f._debug_cache_stats
        self.assertEqual((ccount + cdelete_count, caccess, chit, cmiss),
                         (count, access, hit, miss))
        # Rotation does
        count += glen
        access += glen
        miss += glen
        f.render_raw(glyphs, rotation=10)
        ccount, cdelete_count, caccess, chit, cmiss = f._debug_cache_stats
        self.assertEqual((ccount + cdelete_count, caccess, chit, cmiss),
                         (count, access, hit, miss))
        # aliased (mono) glyphs do
        count += oglen
        access += oglen
        miss += oglen
        f.antialiased = False
        f.render_raw(other_glyphs)
        f.antialiased = True
        ccount, cdelete_count, caccess, chit, cmiss = f._debug_cache_stats
        self.assertEqual((ccount + cdelete_count, caccess, chit, cmiss),
                         (count, access, hit, miss))
        # Trigger a cleanup for sure.
        count += 2 * mglen
        access += 2 * mglen
        miss += 2 * mglen
        f.get_metrics(many_glyphs, ptsize=8)
        f.get_metrics(many_glyphs, ptsize=10)
        ccount, cdelete_count, caccess, chit, cmiss = f._debug_cache_stats
        print (ccount, cdelete_count, caccess, chit, cmiss)
        self.assertTrue(ccount < count)
        self.assertEqual((ccount + cdelete_count, caccess, chit, cmiss),
                         (count, access, hit, miss))