-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfonts.py
71 lines (57 loc) · 2.38 KB
/
fonts.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
66
67
68
69
70
71
from pixels import Pixels
from PIL import Image, ImageDraw, ImageFont
import math
class Fonts:
def __init__(self, fontPath, size, effect, resolution):
self.size = size
self.effect = effect
self.height, self.width = resolution
if size == 'small':
self.font = ImageFont.truetype(fontPath, self.height)
else:
self.font = ImageFont.truetype(fontPath, 2 * self.height)
def __getImage(self, text, colour):
text_width, text_height = self.font.getsize(text)
width = text_width
if text_width < self.width:
width = self.width
height = text_height
if text_height < self.height:
height = self.height
image = Image.new('RGB', (width, height), (0, 0, 0))
draw = ImageDraw.Draw(image)
draw.text((0, 0), text, colour, self.font)
return image
def getText(self, text, colour):
pixelsArray = []
resolution = (self.height, self.width)
lines = None
if self.effect == 'scroll':
lines = [text.replace("|", " ")]
else:
lines = text.split("|")
for line in lines:
image = self.__getImage(line, colour)
textPixels = Pixels(image)
if self.effect == 'scroll':
for i in range(0, image.width + self.width + 1):
columns = (i, i + self.width)
pixelArray = textPixels.convert(resolution, self.effect, columns)
pixelsArray.append(pixelArray)
else:
frameWidth, letterWidth = self.__getFrameWidth(line)
frames = 1
if frameWidth + letterWidth <= image.width:
frames = math.ceil(image.width / frameWidth)
columns = (0, frameWidth)
for i in range(0, frames):
pixelArray = textPixels.convert(resolution, self.effect, columns)
pixelsArray.append(pixelArray)
columns = (columns[0] + frameWidth, columns[1] + frameWidth)
return pixelsArray
def __getFrameWidth(self, text):
letterWidth, letterHeight = self.font.getsize(text[0])
letterCounter = 1
while letterWidth * letterCounter < self.width:
letterCounter = letterCounter + 1
return (letterWidth * (letterCounter - 1), letterWidth)