File: convert-font

package info (click to toggle)
chocolate-doom 3.0.1%2Breally3.0.0%2Bgit1471-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,872 kB
  • sloc: ansic: 220,535; makefile: 955; objc: 951; python: 681; sh: 85; xml: 7
file content (104 lines) | stat: -rwxr-xr-x 3,344 bytes parent folder | download | duplicates (10)
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
#!/usr/bin/env python3
#
# Copyright(C) 2016 Simon Howard
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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.
#
#
# Converts images into libtextscreen fonts.
#

import sys
import os
import re

GRID_COLUMNS = 16
GRID_ROWS = 16

try:
    import Image
except ImportError:
    try:
        from PIL import Image
    except ImportError:
        print("WARNING: Could not update %s. "
              "Please install the Python Imaging library or Pillow."
              % sys.argv[3])
        sys.exit(0)


def generate_font_data(filename):
    """Read font data from the given file.

    Args:
      filename: Image file to read.
    Returns:
      Tuple containing:
          (width, height) dimensions of each font character.
          Array of byte data for font.
    """
    im = Image.open(filename)
    width, height = im.size
    assert (width % GRID_COLUMNS) == 0
    assert (height % GRID_ROWS) == 0

    char_w, char_h = width // GRID_COLUMNS, height // GRID_ROWS
    font_data_len = (width * height) // 8
    font_data = [0] * font_data_len

    for y in range(height):
        for x in range(width):
            px = im.getpixel((x, y))
            if px > (127, 127, 127):
                char_x, char_y = x // char_w, y // char_h
                x1, y1 = x % char_w, y % char_h
                bit_index = (
                    (char_y * GRID_COLUMNS + char_x) * (char_w * char_h) +
                    (y1 * char_w) + x1)
                font_data[bit_index // 8] |= 1 << (bit_index % 8)

    return (char_w, char_h), font_data

def convert_image(font_prefix, filename, output_filename):
    """Convert the given image to a text output file.

    Args:
      font_prefix: Prefix string to attach to constants.
      filename: Input image file to read.
      output_filename: Header file to write.
    """
    dimensions, data = generate_font_data(filename)

    with open(output_filename, "w") as outfile:
        outfile.write("/* Font data generated from %s; do not edit.\n"
                      "   Please see textscreen/fonts/README for copyright\n"
                      "   information. */\n\n" % filename)
        outfile.write("static const uint8_t %s_font_data[] =\n{\n" %
                      font_prefix)
        for index, b in enumerate(data):
            if (index % 8) == 0:
                outfile.write("    ")
            outfile.write("0x%02x," % b)
            if ((index + 1) % 8) == 0:
                outfile.write("\n")
            else:
                outfile.write(" ")
        outfile.write("};\n")

        outfile.write("\n")
        outfile.write("static const txt_font_t %s_font =\n{\n" % font_prefix)
        outfile.write("    \"%s\", %s_font_data, %d, %d,\n" % (
                          font_prefix, font_prefix,
                          dimensions[0], dimensions[1]))
        outfile.write("};\n")

convert_image(sys.argv[1], sys.argv[2], sys.argv[3])