| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: BSD-3-Clause |
| 3 |
# |
| 4 |
# Script to generate menu and intermission screen 'text' graphics for |
| 5 |
# Freedoom, by compositing individual font character graphics together. |
| 6 |
# |
| 7 |
|
| 8 |
from PIL import Image |
| 9 |
from glob import glob |
| 10 |
import re |
| 11 |
import sys |
| 12 |
|
| 13 |
from config import * |
| 14 |
from image_dimensions import * |
| 15 |
from tint import image_tint |
| 16 |
|
| 17 |
|
| 18 |
class TextGenerator(object): |
| 19 |
def __init__(self, fontdir, kerning_table={}): |
| 20 |
self.fontdir = fontdir |
| 21 |
self.kerning_table = self.compile_kerning_table(kerning_table) |
| 22 |
self.get_font_widths() |
| 23 |
|
| 24 |
# Tinting parameters for colorizing text: |
| 25 |
COLOR_BLUE = "#000001" |
| 26 |
COLOR_RED = "#010000" |
| 27 |
COLOR_WHITE = None |
| 28 |
|
| 29 |
# Height of font in pixels. |
| 30 |
FONT_HEIGHT = 15 |
| 31 |
FONT_LC_HEIGHT = 15 # 12 |
| 32 |
|
| 33 |
# If true, the font only has uppercase characters. |
| 34 |
UPPERCASE_FONT = False |
| 35 |
|
| 36 |
# Width of a space character in pixels. |
| 37 |
SPACE_WIDTH = 7 |
| 38 |
LOWERCASE_RE = re.compile(r"^[a-z\!\. ]*$") |
| 39 |
|
| 40 |
def compile_kerning_table(self, kerning_table): |
| 41 |
"""Given a dictionary of kerning patterns, compile Regexps.""" |
| 42 |
|
| 43 |
result = {} |
| 44 |
for pattern, adjust in kerning_table.items(): |
| 45 |
result[re.compile(pattern)] = adjust |
| 46 |
return result |
| 47 |
|
| 48 |
def get_font_widths(self): |
| 49 |
charfiles = glob("%s/font*.png" % self.fontdir) |
| 50 |
self.char_widths = {} |
| 51 |
for c in range(128): |
| 52 |
filename = self.char_filename(chr(c)) |
| 53 |
if filename not in charfiles: |
| 54 |
continue |
| 55 |
w, _ = get_image_dimensions(filename) |
| 56 |
self.char_widths[chr(c)] = w |
| 57 |
|
| 58 |
def __contains__(self, c): |
| 59 |
return c in self.char_widths |
| 60 |
|
| 61 |
def char_width(self, c): |
| 62 |
return self.char_widths[c] |
| 63 |
|
| 64 |
def char_filename(self, c): |
| 65 |
return "%s/font%03d.png" % (self.fontdir, ord(c)) |
| 66 |
|
| 67 |
def kerning_adjust(self, char_1, char_2): |
| 68 |
"""Get kerning adjustment for pair of characters. |
| 69 |
|
| 70 |
Zero means no adjustment. A negative value adjusts to the |
| 71 |
left and a positive value adjusts to the right. |
| 72 |
""" |
| 73 |
for pattern, adjust in self.kerning_table.items(): |
| 74 |
if pattern.match(char_1 + char_2): |
| 75 |
return adjust |
| 76 |
else: |
| 77 |
return 0 |
| 78 |
|
| 79 |
def iterate_char_positions(self, text): |
| 80 |
"""Iterate over characters in string, yielding character with |
| 81 |
position it should be placed at in the output file. |
| 82 |
""" |
| 83 |
x = 0 |
| 84 |
last_c = " " |
| 85 |
for c in text: |
| 86 |
if c == " ": |
| 87 |
x += self.SPACE_WIDTH |
| 88 |
|
| 89 |
if c in self: |
| 90 |
x += self.kerning_adjust(last_c, c) |
| 91 |
|
| 92 |
yield c, x |
| 93 |
|
| 94 |
# Characters overlap by one pixel. |
| 95 |
x += self.char_width(c) - 1 |
| 96 |
|
| 97 |
last_c = c |
| 98 |
|
| 99 |
# We need to add back the missing pixel from the right side |
| 100 |
# of the last char. |
| 101 |
x += 1 |
| 102 |
yield None, x |
| 103 |
|
| 104 |
def text_width(self, text): |
| 105 |
"""Given a string of text, get text width in pixels.""" |
| 106 |
for c, x in self.iterate_char_positions(text): |
| 107 |
if c is None: |
| 108 |
return x |
| 109 |
|
| 110 |
def generate_graphic(self, text, color=None): |
| 111 |
"""Get command to render text to a file |
| 112 |
with the given background color. |
| 113 |
""" |
| 114 |
|
| 115 |
if self.UPPERCASE_FONT: |
| 116 |
text = text.upper() |
| 117 |
"""Command line construction helper, used in render functions""" |
| 118 |
width = self.text_width(text) |
| 119 |
|
| 120 |
if self.LOWERCASE_RE.match(text): |
| 121 |
height = self.FONT_LC_HEIGHT |
| 122 |
else: |
| 123 |
height = self.FONT_HEIGHT |
| 124 |
|
| 125 |
txt_image = Image.new("RGBA", (width, height), (0, 0, 0, 0)) |
| 126 |
for c, x in self.iterate_char_positions(text): |
| 127 |
if c is None: |
| 128 |
break |
| 129 |
|
| 130 |
filename = self.char_filename(c) |
| 131 |
char_image = Image.open(filename) |
| 132 |
char_image.load() |
| 133 |
int_image = Image.new("RGBA", txt_image.size, (0, 0, 0, 0)) |
| 134 |
int_image.paste(char_image, (x, height - self.FONT_HEIGHT)) |
| 135 |
txt_image = Image.alpha_composite(txt_image, int_image) |
| 136 |
|
| 137 |
txt_image = image_tint(txt_image, color) |
| 138 |
return txt_image |
| 139 |
|
| 140 |
|
| 141 |
def generate_graphics(font, graphics, color=None): |
| 142 |
for name, text in sorted(graphics.items()): |
| 143 |
# write a makefile fragment |
| 144 |
target = "%s.png" % name |
| 145 |
image = font.generate_graphic(text, color=color) |
| 146 |
image.save(target) |
| 147 |
|
| 148 |
|
| 149 |
def generate_kerning_test(font): |
| 150 |
pairs = [] |
| 151 |
for c1 in sorted(font.char_widths): |
| 152 |
char1 = "%c" % c1 |
| 153 |
for c2 in sorted(font.char_widths): |
| 154 |
char2 = "%c" % c2 |
| 155 |
if font.kerning_adjust(char1, char2) != 0: |
| 156 |
pairs.append(char1 + char2) |
| 157 |
|
| 158 |
image = font.generate_graphic(" ".join(pairs)) |
| 159 |
image.save("kerning.png") |
| 160 |
|
| 161 |
|
| 162 |
if __name__ == "__main__": |
| 163 |
|
| 164 |
font = TextGenerator("fontchars", kerning_table=FONT_KERNING_RULES) |
| 165 |
generate_graphics(font, red_graphics, color=font.COLOR_RED) |
| 166 |
generate_graphics(font, blue_graphics, color=font.COLOR_BLUE) |
| 167 |
generate_graphics(font, white_graphics, color=font.COLOR_WHITE) |