Source

PyCessing / pycessing / typo.py

Full commit
"""
    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 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)
        else:
            print "Warning: font not found.  Using default."
            fnt = "freesansbold.ttf"
        self.fontName = fontName
        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

    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(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):
        self.font.set_underline(val)

    def setBold(self, val=True):
        self.font.set_bold(val)