Example #1
0
 def test_TTF_FontLineSkip(self):
     last = cur = 0
     for ptsize in range(6, 26):
         font = sdlttf.TTF_OpenFont(fontfile, ptsize)
         cur = sdlttf.TTF_FontLineSkip(font)
         self.assertGreaterEqual(cur, last)
         last = cur
         sdlttf.TTF_CloseFont(font)
 def test_TTF_FontAscent(self):
     last = cur = 0
     for ptsize in range(-10, 10):
         font = sdlttf.TTF_OpenFont(fontfile, ptsize)
         cur = sdlttf.TTF_FontAscent(font)
         self.assertGreaterEqual(cur, last)
         last = cur
         sdlttf.TTF_CloseFont(font)
Example #3
0
 def test_TTF_FontFaces(self):
     font = sdlttf.TTF_OpenFont(fontfile, 10)
     self.assertGreaterEqual(sdlttf.TTF_FontFaces(font), 1)
     #        self.assertRaises((AttributeError, TypeError), sdlttf.font_faces,None)
     #        self.assertRaises((AttributeError, TypeError),
     #                          sdlttf.font_faces, "test")
     #        self.assertRaises((AttributeError, TypeError), sdlttf.font_faces,1234)
     sdlttf.TTF_CloseFont(font)
Example #4
0
 def test_TTF_FontDescent(self):
     last = cur = 0
     for ptsize in range(6, 26):
         font = sdlttf.TTF_OpenFont(fontfile, ptsize)
         cur = sdlttf.TTF_FontDescent(font)
         self.assertLessEqual(cur, last)
         last = cur
         sdlttf.TTF_CloseFont(font)
Example #5
0
    def printD(self,
               size,
               x,
               y,
               text,
               font="SanFranciscoRegular",
               color=[0, 0, 0]):

        pas = size
        L = []
        k = 0
        MOD = 20

        i = len(text) - 1
        while i > 0:
            if text[i] == '\t' or text[i] == '\n':
                text = text[:i] + text[i + 1:]
            i = i - 1

        M = ""
        for i in range(len(text)):
            M += text[i]
            if i % MOD == 0 and i != 0:
                if text[i] != ' ' and text[i + 1] != ' ':
                    M += '-'
                M += '\n'
        text = M
        while k < len(text):
            if text[k] == '\n':
                L.append(text[:k])
                text = text[(k + 1):]
                k = 0
                if L[-1][0] == " ":
                    L[-1] = L[-1][1:]

            else:
                k += 1

        L.append(text)

        for i in range(len(L)):
            if L[i] == '':
                del L[i]

        police = sdlttf.TTF_OpenFont(str.encode("Fonts/" + font + ".ttf"),
                                     size)

        P = 0

        for elem in L:
            Text = sdlttf.TTF_RenderUTF8_Blended(
                police, str.encode(elem),
                SDL_Color(color[0], color[1], color[2]))
            a = [Text, SDL_Rect(x, y + P)]
            self.elmt_afficher.append(a)
            P += pas

        sdlttf.TTF_CloseFont(police)
    def test_TTF_FontFaceFamilyName(self):
        font = sdlttf.TTF_OpenFont(fontfile, 10)
        self.assertEqual(sdlttf.TTF_FontFaceFamilyName(font), b"Tuffy")
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.font_face_family_name, None)
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.font_face_family_name, "test")
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.font_face_family_name, 1234)
        sdlttf.TTF_CloseFont(font)
    def test_TTF_FontFaceStyleName(self):
        font = sdlttf.TTF_OpenFont(fontfile, 10)
        self.assertEqual(sdlttf.TTF_FontFaceStyleName(font), b"Regular")
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.font_face_style_name, None)
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.font_face_style_name, "test")
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.font_face_style_name, 1234)
        sdlttf.TTF_CloseFont(font)
    def test_TTF_FontFaceIsFixedWidth(self):
        font = sdlttf.TTF_OpenFont(fontfile, 10)
        self.assertFalse(sdlttf.TTF_FontFaceIsFixedWidth(font))
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.font_face_is_fixed_width, None)
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.font_face_is_fixed_width, "test")
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.font_face_is_fixed_width, 1234)
        sdlttf.TTF_CloseFont(font)
 def test_TTF_GetSetFontOutline(self):
     font = sdlttf.TTF_OpenFont(fontfile, 10)
     self.assertEqual(sdlttf.TTF_GetFontOutline(font), 0)
     for x in range(-10, 10):
         sdlttf.TTF_SetFontOutline(font, x)
         self.assertEqual(sdlttf.TTF_GetFontOutline(font), x)
     #self.assertRaises(TypeError, sdlttf.set_font_outline, None, None)
     #self.assertRaises(TypeError, sdlttf.set_font_outline, font, None)
     #self.assertRaises(ValueError, sdlttf.set_font_outline, font, "test")
     #self.assertRaises(ValueError, sdlttf.set_font_outline, None, "test")
     #self.assertRaises((AttributeError, TypeError),
     #                  sdlttf.set_font_outline, None, 123)
     #self.assertRaises((AttributeError, TypeError),
     #                  sdlttf.get_font_outline, None)
     #self.assertRaises((AttributeError, TypeError),
     #                  sdlttf.get_font_outline, None)
     sdlttf.TTF_CloseFont(font)
Example #10
0
    def printT(self,
               size,
               x,
               y,
               text,
               font="SanFranciscoRegular",
               color=[0, 0, 0]):

        pas = size
        L = []
        k = 0
        while k < len(text):
            if text[k] == '\n':
                L.append(text[:k])
                text = text[(k + 1):]
                k = 0
                if L[-1][0] == " ":
                    L[-1] = L[-1][1:]

            else:
                k += 1

        L.append(text)
        try:
            if L[-1][0] == " ":
                L[-1] = L[-1][1:]
        except:
            pass

        police = sdlttf.TTF_OpenFont(str.encode("Fonts/" + font + ".ttf"),
                                     size)

        P = 0

        for elem in L:
            Text = sdlttf.TTF_RenderUTF8_Blended(
                police, str.encode(elem),
                SDL_Color(color[0], color[1], color[2]))
            a = [Text, SDL_Rect(x, y + P)]
            self.elmt_afficher.append(a)
            P += pas

        sdlttf.TTF_CloseFont(police)
    def test_TTF_GetSetFontHinting(self):
        font = sdlttf.TTF_OpenFont(fontfile, 10)
        self.assertEqual(sdlttf.TTF_GetFontHinting(font),
                         sdlttf.TTF_HINTING_NORMAL)
        for hint in (sdlttf.TTF_HINTING_NORMAL, sdlttf.TTF_HINTING_LIGHT,
                     sdlttf.TTF_HINTING_MONO, sdlttf.TTF_HINTING_NONE):
            sdlttf.TTF_SetFontHinting(font, hint)
            self.assertEqual(sdlttf.TTF_GetFontHinting(font), hint)
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.set_font_hinting, None, None)
#        self.assertRaises(ArgumentError, sdlttf.set_font_hinting, font, None)
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.set_font_hinting, None, 1)
#        self.assertRaises(ArgumentError, sdlttf.set_font_hinting, font,"test")
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.set_font_hinting, None, "test")
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.get_font_hinting, None)
        sdlttf.TTF_CloseFont(font)
Example #12
0
    def addFont(self, font_path, size):
        font_hash = font_path + "***" + str(size)
        if font_hash in self.font_hashes:
            # If the font already exist, return its font_id
            return self.font_hashes[font_hash]

        if font_path in self.supported_fonts_path:
            # Supported path
            font_path = self.supported_fonts_path[font_path]

        self.font_hashes[font_hash] = self.font_id

        font = sdl2ttf.TTF_OpenFont(font_path.encode(), size)
        # Check for errors
        errors = sdl2ttf.TTF_GetError()
        if errors:
            print("Add font", errors)

        self.font_table.append((font, GlyphTable(font)))
        self.font_id += 1
        return self.font_id - 1
    def test_TTF_GetSetFontKerning(self):
        font = sdlttf.TTF_OpenFont(fontfile, 10)
        self.assertEqual(sdlttf.TTF_GetFontKerning(font), 1)
        sdlttf.TTF_SetFontKerning(font, 0)
        self.assertEqual(sdlttf.TTF_GetFontKerning(font), 0)
        sdlttf.TTF_SetFontKerning(font, 1)
        self.assertEqual(sdlttf.TTF_GetFontKerning(font), 1)
        sdlttf.TTF_SetFontKerning(font, 0)
        self.assertEqual(sdlttf.TTF_GetFontKerning(font), 0)
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.get_font_kerning, None)
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.get_font_kerning, "test")
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.get_font_kerning, 1234)
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.set_font_kerning, None, None)
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.set_font_kerning, "test", "test")
#        self.assertRaises((AttributeError, TypeError),
#                          sdlttf.set_font_kerning, 1234, None)
        sdlttf.TTF_CloseFont(font)
Example #14
0
    def printTexte(self,
                   size,
                   text,
                   font="SanFranciscoRegular",
                   color=[0, 0, 0]):

        definition = []
        mots_definie = []
        pas = size
        L = []
        k = 0
        MOD = 45

        Deb = 100
        Deby = 120

        i = len(text) - 1
        while i > 0:
            if text[i] == '\t' or text[i] == '\n':
                text = text[:i] + text[i + 1:]
            i = i - 1

        if "<def" in text:
            a = text.find("<def")
            b = text.find(">")
            definition.append(text[(a + 6):].split(
                text[b:])[0])  #on recupere la definition et le mot associƩ
            c = text.find("</def>")
            mots_definie.append(text[(b + 1):].split(text[c:])[0])

            text = text[:a] + text[(b + 1):]  #on supprime les balises du texte
            c = text.find("</def>")
            text = text[:c] + text[(c + 6):]

        M = ""
        for i in range(len(text)):
            M += text[i]
            if i % MOD == 0 and i != 0:
                try:
                    if text[i] != ' ' and text[i + 1] != ' ':
                        M += '-'
                except:
                    pass
                M += '\n'

        text = M
        while k < len(text):
            if text[k] == '\n':
                L.append(text[:k])
                text = text[(k + 1):]
                k = 0
                if L[-1][0] == " ":
                    L[-1] = L[-1][1:]

            else:
                k += 1

        L.append(text)

        for i in range(len(L)):
            if L[i] == '':
                del L[i]

        #print(L)
        if L[-1][0] == " ":
            L[-1] = L[-1][1:]

        police = sdlttf.TTF_OpenFont(str.encode("Fonts/" + font + ".ttf"),
                                     size)

        P = 0

        #print(L)

        for elem in L:
            afficher = False
            for i in range(len(mots_definie)):
                if mots_definie[i] in elem:
                    mots = mots_definie[i]
                    afficher = True
                    #print(mots)
                    position = elem.find(mots)
                    fin_mots = len(mots) + position
                    Text = sdlttf.TTF_RenderUTF8_Blended(
                        police, str.encode(elem[:position]),
                        SDL_Color(0, 0, 0))
                    a = [Text, SDL_Rect(Deb, Deby + P)]
                    self.elmt_afficher.append(a)

                    Text = sdlttf.TTF_RenderUTF8_Blended(
                        police,
                        str.encode(elem[position:].split(elem[fin_mots:])[0]),
                        SDL_Color(50, 50, 150))
                    pos = Deb + int(position * 8.5)
                    a = [Text, SDL_Rect(pos, Deby + P)]
                    self.elmt_afficher.append(a)
                    self.definitions.append([
                        definition[i], pos, Deby + P + 2,
                        Deb + fin_mots * 9 + 2, P + pas + Deby
                    ])

                    Text = sdlttf.TTF_RenderUTF8_Blended(
                        police, str.encode(elem[fin_mots:]),
                        SDL_Color(color[0], color[1], color[2]))
                    a = [Text, SDL_Rect(Deb + fin_mots * 9 + 2, Deby + P)]
                    self.elmt_afficher.append(a)

                    P += pas

            if not afficher:
                #print(elem)
                Text = sdlttf.TTF_RenderUTF8_Blended(
                    police, str.encode(elem),
                    SDL_Color(color[0], color[1], color[2]))
                a = [Text, SDL_Rect(Deb, Deby + P)]
                self.elmt_afficher.append(a)
                P += pas

        sdlttf.TTF_CloseFont(police)
 def __init__(self):
     self.w = sdl.Window("Adnesh's Snake vs Block Game",
                         (gameinfo.WINDOW_WIDTH, gameinfo.WINDOW_HEIGHT))
     sdl.Renderer.__init__(self, self.w)
     self.mode = gameinfo.BLOCK_IN_MOTION
     self.t = ttf.TTF_OpenFont(b"support/font.ttf", 30)
Example #16
0
 def __init__(self, path, size):
     self._font = ttf.TTF_OpenFont(bytes(path, "utf8"), size)
     self.size = size
 def test_TTF_OpenCloseFont(self):
     for x in range(-10, 15):
         font = sdlttf.TTF_OpenFont(fontfile, x)
         self.assertIsInstance(font.contents, sdlttf.TTF_Font)
         sdlttf.TTF_CloseFont(font)