Commits

Brendan Howell committed 623a2b1

started rewrite of pango-based ScreenText class

Comments (0)

Files changed (1)

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)
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.