1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
"""text and font classes, helps everyone to text"""
import pygame, pygame.font, gfx
#old versions of SysFont were buggy
if pygame.ver <= '1.6.1':
from mysysfont import SysFont
else:
SysFont = pygame.font.SysFont
FontPool = {}
def initialize():
pygame.font.init()
return 1
class Font:
def __init__(self, name, size, bold=0, italic=0):
val = name, size
if val in FontPool:
font = FontPool[val]
else:
font = SysFont(name, size, bold, italic)
FontPool[val] = font
self.font = font
if size >= 20:
self.text = self.textshadowed
def render(self, *args):
return self.font.render(*args)
def set_underline(self, *args):
return self.font.set_underline(*args)
def set_italic(self, *args):
return self.font.set_italic(*args)
def set_bold(self, *args):
return self.font.set_bold(*args)
def _positionrect(self, img, center, pos):
r = img.get_rect()
if center:
setattr(r, pos, center)
return r
def _render(self, text, color, bgd=(0,0,0)):
return img
def get_height(self):
return self.font.get_height()
def get_linesize(self):
return self.font.get_linesize()
def text(self, color, text, center=None, pos='center', bgd=(0,0,0)):
if text is None: text = ' '
try:
if gfx.surface.get_bytesize()>1:
img = self.font.render(text, 1, color, bgd)
img.set_colorkey(bgd, pygame.RLEACCEL)
else:
img = self.font.render(text, 0, color)
except (pygame.error, TypeError):
img = pygame.Surface((10, 10))
img = img.convert()
r = self._positionrect(img, center, pos)
return [img, r]
def textlined(self, color, text, center=None, pos='center'):
darkcolor = [int(c/2) for c in color]
if text is None: text = ' '
try:
if gfx.surface.get_bytesize()>1:
img1 = self.font.render(text, 1, color)
img2 = self.font.render(text, 1, darkcolor)
else:
img1 = img2 = self.font.render(text, 0, color)
img2 = self.font.render(text, 0, darkcolor)
except (pygame.error, TypeError):
img1 = img2 = pygame.Surface((10, 10))
newsize = img1.get_width()+4, img1.get_height()+4
img = pygame.Surface(newsize)
img.blit(img2, (0, 0))
img.blit(img2, (0, 4))
img.blit(img2, (4, 0))
img.blit(img2, (4, 4))
img.blit(img1, (2, 2))
img = img.convert()
img.set_colorkey((0,0,0), pygame.RLEACCEL)
r = self._positionrect(img, center, pos)
return [img, r]
def textshadowed(self, color, text, center=None, pos='center'):
darkcolor = [int(c/2) for c in color]
if text is None: text = ' '
try:
if gfx.surface.get_bytesize()>1:
img1 = self.font.render(text, 1, color)
img2 = self.font.render(text, 1, darkcolor)
else:
img1 = img2 = self.font.render(text, 0, color)
img2 = self.font.render(text, 0, darkcolor)
except (pygame.error, TypeError):
img1 = img2 = pygame.Surface((10, 10))
newsize = img1.get_width()+2, img1.get_height()+2
img = pygame.Surface(newsize)
img.blit(img2, (2, 2))
img.blit(img1, (0, 0))
img = img.convert()
img.set_colorkey((0,0,0), pygame.RLEACCEL)
r = self._positionrect(img, center, pos)
return [img, r]
def textbox(self, color, text, width, bgcolor, topmargin=6):
sidemargin = 6
lines = []
for line in text.splitlines():
cursize = 0
build = ''
for word in line.split():
wordspace = word + ' '
size = self.font.size(wordspace)[0]
if size + cursize >= width:
lines.append(build)
cursize = size
build = wordspace
else:
build += wordspace
cursize += size
lines.append(build)
lineheight = self.font.get_linesize()
height = len(lines) * lineheight + topmargin + sidemargin
width += sidemargin * 2
surf = pygame.Surface((width, height))
surf.fill(bgcolor)
pos = topmargin
for line in lines:
if line:
img = self.font.render(line, 1, color, bgcolor)
img.set_colorkey(bgcolor)
surf.blit(img, (sidemargin, pos))
pos += lineheight
return surf
|