Brendan Howell avatar Brendan Howell committed 45e81b8

pango based font selection

Comments (0)

Files changed (3)

pycessing/typo.py

 
 import pygame
 import os
-from itertools import chain
+import pango
 
-SCREEN = None
 PROJECT_DIR = None
- 
-def truncline(text, font, maxwidth):
-    """wraps a line to fit the font and spits out the rest"""
-    real=len(text)       
-    stext=text           
-    l=font.size(text)[0]
-    cut=0
-    a=0                  
-    done=1
-    old = None
-    while l > maxwidth:
-        a=a+1
-        n=text.rsplit(None, a)[0]
-        if stext == n:
-            cut += 1
-            stext= n[:-cut]
-        else:
-            stext = n
-        l=font.size(stext)[0]
-        real=len(stext)               
-        done=0                        
-    return real, done, stext             
-        
-def wrapline(text, font, maxwidth): 
-    done=0                      
-    wrapped=[]                  
-                               
-    while not done:             
-        nl, done, stext = truncline(text, font, maxwidth) 
-        wrapped.append(stext.strip())                  
-        text=text[nl:]                                 
-    return wrapped 
- 
-def wrap_multi_line(text, font, maxwidth):
-    """ returns text taking new lines into account."""
-    lines = chain(*(wrapline(line, font, maxwidth) for line in text.splitlines()))
-    return list(lines)
+DRAWING = None
+
 
 class ScreenText:
-    def __init__(self, text=None, fontName=None, size=30, color=(255,255,255)):
-        self.text=text
+    def __init__(self, text=None, fontName=None, size=30, color=(255, 255, 255)):
+        self.text = text
         self.fontName = fontName
         self.surface = None
         self.setFont(fontName, size)
     def setText(self, text):
         self.text = text
 
+    def listFonts(self):
+        print "Fonts available:"
+        for font in DRAWING.families:
+            print font.get_name()
+
     def setFont(self, fontName, size=30):
         if (fontName):
             fnt = pygame.font.match_font(fontName)
             print "Warning: font not found.  Using default."
             fnt = "freesansbold.ttf"
         self.fontName = fontName
-        self.font = pygame.font.Font(fnt, size)
         self.size = size
-        
+
     def setFontFromFile(self, filename, size=30):
         #fp = open(filename)
         if not(os.path.exists(filename)):
-            filename = os.path.join(PROJECT_DIR,filename)
+            filename = os.path.join(PROJECT_DIR, filename)
             if not(os.path.exists(filename)):
                 print "Error: font file " + filename + " not found."
                 return
 
     def setSize(self, size):
         self.size = size
-        self.setFont(self.fontName, size)
 
     def setColor(self, *args):
         if len(args) == 1:
-            self.color = args[0]
+            self.color = (args[0] / 255.0, args[0] / 255.0, args[0] / 255.0, 1.0)
         elif len(args) == 3:
-            self.color = (args[0], args[1], args[2])
+            self.color = (args[0] / 255.0, args[1] / 255.0, args[2] / 255.0, 1.0)
         elif len(args) == 4:
-            self.color = (args[0], args[1], args[2], args[3])
+            self.color = (args[0] / 255.0, args[1] / 255.0, args[2] / 255.0, args[3] / 255.0)
         else:
             print "ScreenText: error invalid color specified."
 
-    def draw(self, x, y, sx=None, sy=None, width=None, height=None):
-        self.surface = self.font.render(self.text, True, self.color)
-        SCREEN.unlock()
-        SCREEN.blit(self.surface, (x, y))
-        
-    def draw_wrapped(self, x, y, max_width, lineheight=None):
-        if not(lineheight):
-            lineheight = self.size 
-        for line in wrapline(self.text, self.font, max_width):
-            self.surface = self.font.render(line, True, self.color)
-            SCREEN.unlock()
-            SCREEN.blit(self.surface, (x, y))
-            y += lineheight
-        
-    def getFonts(self):
-        print pygame.font.get_fonts()
-        
-    def getWidth(self):
-        w,h = self.font.size(self.text)
-        return w
-        
-    def getHeight(self):
-        w,h = self.font.size(self.text)
-        return h
-
-    def setItalic(self,val=True):
+    def draw(self, x, y, width=-1):
+        DRAWING.ctx.save()
+        DRAWING.ctx.translate(x, y)
+        r, g, b, a = self.color
+        DRAWING.ctx.set_source_rgba(r, g, b, a)
+        layout = DRAWING.pangoctx.create_layout()
+        layout.set_font_description(pango.FontDescription(self.fontName + " " + str(self.size)))
+        layout.set_width(width)
+        layout.set_text(self.text)
+        DRAWING.pangoctx.update_layout(layout)
+        DRAWING.pangoctx.show_layout(layout)
+        DRAWING.ctx.restore()
+
+    def setItalic(self, val=True):
         self.font.set_italic(val)
 
     def setUnderline(self, val=True):

pycessing/typo2.py

-"""
-    typo.py
-    Copyright 2012 Brendan Howell (brendan@howell-ersatz.com)
-
-    This file is part of PyCessing.
-
-    PyCessing is free software: you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation, either version 3 of the License, or
-    (at your option) any later version.
-
-    PyCessing is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with PyCessing.  If not, see <http://www.gnu.org/licenses/>.
-"""
-
-import pygame
-import os
-import pango
-
-PROJECT_DIR = None
-DRAWING = None
-
-
-class ScreenText:
-    def __init__(self, text=None, fontName=None, size=30, color=(255, 255, 255)):
-        self.text = text
-        self.fontName = fontName
-        self.surface = None
-        self.setFont(fontName, size)
-        self.color = color
-
-    def setText(self, text):
-        self.text = text
-
-    def setFont(self, fontName, size=30):
-        if (fontName):
-            fnt = pygame.font.match_font(fontName)
-        else:
-            print "Warning: font not found.  Using default."
-            fnt = "freesansbold.ttf"
-        self.fontName = fontName
-        self.font = pygame.font.Font(fnt, size)
-        self.size = size
-
-    def setFontFromFile(self, filename, size=30):
-        #fp = open(filename)
-        if not(os.path.exists(filename)):
-            filename = os.path.join(PROJECT_DIR, filename)
-            if not(os.path.exists(filename)):
-                print "Error: font file " + filename + " not found."
-                return
-        self.font = pygame.font.Font(filename, size)
-        self.size = size
-
-    def setSize(self, size):
-        self.size = size
-        self.setFont(self.fontName, size)
-
-    def setColor(self, *args):
-        if len(args) == 1:
-            self.color = (args[0] / 255.0, args[0] / 255.0, args[0] / 255.0, 1.0)
-        elif len(args) == 3:
-            self.color = (args[0] / 255.0, args[1] / 255.0, args[2] / 255.0, 1.0)
-        elif len(args) == 4:
-            self.color = (args[0] / 255.0, args[1] / 255.0, args[2] / 255.0, args[3] / 255.0)
-        else:
-            print "ScreenText: error invalid color specified."
-
-    def draw(self, x, y, width=-1):
-        DRAWING.ctx.save()
-        DRAWING.ctx.translate(x, y)
-        r, g, b, a = self.color
-        DRAWING.ctx.set_source_rgba(r, g, b, a)
-        layout = DRAWING.pangoctx.create_layout()
-        layout.set_font_description(pango.FontDescription(DRAWING.families[0].get_name() + " 25"))
-        layout.set_width(width)
-        layout.set_text(self.text)
-        DRAWING.pangoctx.update_layout(layout)
-        DRAWING.pangoctx.show_layout(layout)
-        DRAWING.ctx.restore()
-
-    def getFonts(self):
-        print pygame.font.get_fonts()
-
-    def getWidth(self):
-        w, h = self.font.size(self.text)
-        return w
-
-    def getHeight(self):
-        w, h = self.font.size(self.text)
-        return h
-
-    def setItalic(self, val=True):
-        self.font.set_italic(val)
-
-    def setUnderline(self, val=True):
-        self.font.set_underline(val)
-
-    def setBold(self, val=True):
-        self.font.set_bold(val)

pycessing/typo_old.py

+"""
+    typo.py
+    Copyright 2012 Brendan Howell (brendan@howell-ersatz.com)
+
+    This file is part of PyCessing.
+
+    PyCessing is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    PyCessing is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with PyCessing.  If not, see <http://www.gnu.org/licenses/>.
+"""
+
+import pygame
+import os
+from itertools import chain
+
+SCREEN = None
+PROJECT_DIR = None
+ 
+def truncline(text, font, maxwidth):
+    """wraps a line to fit the font and spits out the rest"""
+    real=len(text)       
+    stext=text           
+    l=font.size(text)[0]
+    cut=0
+    a=0                  
+    done=1
+    old = None
+    while l > maxwidth:
+        a=a+1
+        n=text.rsplit(None, a)[0]
+        if stext == n:
+            cut += 1
+            stext= n[:-cut]
+        else:
+            stext = n
+        l=font.size(stext)[0]
+        real=len(stext)               
+        done=0                        
+    return real, done, stext             
+        
+def wrapline(text, font, maxwidth): 
+    done=0                      
+    wrapped=[]                  
+                               
+    while not done:             
+        nl, done, stext = truncline(text, font, maxwidth) 
+        wrapped.append(stext.strip())                  
+        text=text[nl:]                                 
+    return wrapped 
+ 
+def wrap_multi_line(text, font, maxwidth):
+    """ returns text taking new lines into account."""
+    lines = chain(*(wrapline(line, font, maxwidth) for line in text.splitlines()))
+    return list(lines)
+
+class ScreenText:
+    def __init__(self, text=None, fontName=None, size=30, color=(255,255,255)):
+        self.text=text
+        self.fontName = fontName
+        self.surface = None
+        self.setFont(fontName, size)
+        self.color = color
+
+    def setText(self, text):
+        self.text = text
+
+    def setFont(self, fontName, size=30):
+        if (fontName):
+            fnt = pygame.font.match_font(fontName)
+        else:
+            print "Warning: font not found.  Using default."
+            fnt = "freesansbold.ttf"
+        self.fontName = fontName
+        self.font = pygame.font.Font(fnt, size)
+        self.size = size
+        
+    def setFontFromFile(self, filename, size=30):
+        #fp = open(filename)
+        if not(os.path.exists(filename)):
+            filename = os.path.join(PROJECT_DIR,filename)
+            if not(os.path.exists(filename)):
+                print "Error: font file " + filename + " not found."
+                return
+        self.font = pygame.font.Font(filename, size)
+        self.size = size
+
+    def setSize(self, size):
+        self.size = size
+        self.setFont(self.fontName, size)
+
+    def setColor(self, *args):
+        if len(args) == 1:
+            self.color = args[0]
+        elif len(args) == 3:
+            self.color = (args[0], args[1], args[2])
+        elif len(args) == 4:
+            self.color = (args[0], args[1], args[2], args[3])
+        else:
+            print "ScreenText: error invalid color specified."
+
+    def draw(self, x, y, sx=None, sy=None, width=None, height=None):
+        self.surface = self.font.render(self.text, True, self.color)
+        SCREEN.unlock()
+        SCREEN.blit(self.surface, (x, y))
+        
+    def draw_wrapped(self, x, y, max_width, lineheight=None):
+        if not(lineheight):
+            lineheight = self.size 
+        for line in wrapline(self.text, self.font, max_width):
+            self.surface = self.font.render(line, True, self.color)
+            SCREEN.unlock()
+            SCREEN.blit(self.surface, (x, y))
+            y += lineheight
+        
+    def getFonts(self):
+        print pygame.font.get_fonts()
+        
+    def getWidth(self):
+        w,h = self.font.size(self.text)
+        return w
+        
+    def getHeight(self):
+        w,h = self.font.size(self.text)
+        return h
+
+    def setItalic(self,val=True):
+        self.font.set_italic(val)
+
+    def setUnderline(self, val=True):
+        self.font.set_underline(val)
+
+    def setBold(self, val=True):
+        self.font.set_bold(val)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.