-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfont.py
65 lines (61 loc) · 3.02 KB
/
font.py
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
import pygame #import all of pygame
from pygame.locals import *
import settings #load settings
import data
#class for managing a font
class Font:
def __init__(self, file):
self.letters = {} #dictionary of letters
font_dom = data.load_xml(file).documentElement #load and parse font
image_file = font_dom.getAttribute("image") #get font bitmap file name
self.image = data.load_image(image_file) #load font bitmap
self.image.convert_alpha() #convert it to draw faster
self.height = int(font_dom.getAttribute("height")) #get height of characters
self.id = font_dom.getAttribute("id") #get font ID
#load all the letters
for letter in font_dom.getElementsByTagName("letter"):
name = letter.getAttribute("name") #get name of letter
width = int(letter.getAttribute("size")) #get width of letter
coords = letter.getAttribute("coord").split(",") #get coordinate of letter
#make a rect of the letter bitmap
rect = pygame.Rect(int(coords[0].strip()), int(coords[1].strip()), \
width, self.height)
self.letters[name] = rect #store the letter
#build a list of letter rects from a string
def get_letters(self, str):
letter_rects = []
code = None
for letter in str: #loop through all the letters in the string
if letter == "{" and code is None: #if it's the start of a code
code = "" #clear out the code
continue #go to next letter
if letter == "}" and code is not None: #if it's the end of a code
if code not in self.letters: #if we don't know what it is
code = None #clear code
continue #go to next letter
letter_rects.append(self.letters[code]) #save rect
code = None #clear code
continue #go to next letter
if letter not in self.letters: #if we don't know what it is
continue #go to next letter
if code is not None: #if we're in a code
code += letter #add a letter to the code
else: #if we're not
letter_rects.append(self.letters[letter]) #add the letter rect
return letter_rects #return result
#get the width of a string
def get_width(self, str):
rects = self.get_letters(str) #get the list of rectangles
total = 0 #total width
for letter in rects: #loop through all the rects
total += letter.width #add the widths together
return total #return the total width
#render a string
def render(self, str, dest, where):
rects = self.get_letters(str) #get the letter rects
x = 0 #current X position
for letter in rects: #loop through them
#render a letter
dest.blit(self.image, (where[0]+x, where[1]), letter)
x += letter.width #go to the position of the next letter
return x #return the width of the rendered string