mirror of
https://gitlab.com/MisterBiggs/bdfparse.git
synced 2025-06-16 06:26:39 +00:00
changed code to classes
This commit is contained in:
parent
cc635ce8f6
commit
7ff36f34eb
298
bdfparse.py
298
bdfparse.py
@ -1,180 +1,198 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
|
|
||||||
def parse_properties(bdfFile):
|
class Font:
|
||||||
|
def __init__(self, bdfFile):
|
||||||
|
self.properties = self.parse_properties(bdfFile)
|
||||||
|
self.chars = self.parse_chars(bdfFile)
|
||||||
|
self.cols = self.properties["FONTBOUNDINGBOX"][0]
|
||||||
|
self.rows = self.properties["FONTBOUNDINGBOX"][1]
|
||||||
|
|
||||||
properties = {"COMMENT": []}
|
def parse_properties(self, bdfFile):
|
||||||
with open(bdfFile, "r") as bdf:
|
|
||||||
line = bdf.readline()
|
|
||||||
|
|
||||||
while not line.startswith("STARTCHAR"):
|
properties = {"COMMENT": []}
|
||||||
|
with open(bdfFile, "r") as bdf:
|
||||||
|
line = bdf.readline()
|
||||||
|
|
||||||
if line.startswith("COMMENT"):
|
while not line.startswith("STARTCHAR"):
|
||||||
properties["COMMENT"].append(
|
|
||||||
line.replace("COMMENT", "").replace("\n", "")
|
|
||||||
)
|
|
||||||
|
|
||||||
if line.startswith("FONT"):
|
if line.startswith("COMMENT"):
|
||||||
properties["FONT"] = line.replace("FONT", "").replace("\n", "")
|
properties["COMMENT"].append(
|
||||||
|
line.replace("COMMENT", "").replace("\n", "")
|
||||||
if line.startswith("SIZE"):
|
|
||||||
properties["SIZE"] = line.replace("SIZE", "").replace("\n", "")
|
|
||||||
|
|
||||||
if line.startswith("FONTBOUNDINGBOX"):
|
|
||||||
properties["FONTBOUNDINGBOX"] = [
|
|
||||||
int(x)
|
|
||||||
for x in (
|
|
||||||
line.replace("FONTBOUNDINGBOX ", "").replace("\n", "").split()
|
|
||||||
)
|
)
|
||||||
]
|
|
||||||
|
|
||||||
if line.startswith("STARTPROPERTIES"):
|
if line.startswith("FONT"):
|
||||||
properties["STARTPROPERTIES"] = line.replace(
|
properties["FONT"] = line.replace("FONT", "").replace("\n", "")
|
||||||
"STARTPROPERTIES", ""
|
|
||||||
).replace("\n", "")
|
|
||||||
|
|
||||||
if line.startswith("FONTNAME_REGISTRY"):
|
if line.startswith("SIZE"):
|
||||||
properties["FONTNAME_REGISTRY"] = line.replace(
|
properties["SIZE"] = line.replace("SIZE", "").replace("\n", "")
|
||||||
"FONTNAME_REGISTRY", ""
|
|
||||||
).replace("\n", "")
|
|
||||||
|
|
||||||
if line.startswith("FOUNDRY"):
|
if line.startswith("FONTBOUNDINGBOX"):
|
||||||
properties["FOUNDRY"] = line.replace("FOUNDRY", "").replace("\n", "")
|
properties["FONTBOUNDINGBOX"] = [
|
||||||
|
int(x)
|
||||||
|
for x in (
|
||||||
|
line.replace("FONTBOUNDINGBOX ", "")
|
||||||
|
.replace("\n", "")
|
||||||
|
.split()
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
if line.startswith("FAMILY_NAME"):
|
if line.startswith("STARTPROPERTIES"):
|
||||||
properties["FAMILY_NAME"] = line.replace("FAMILY_NAME", "").replace(
|
properties["STARTPROPERTIES"] = line.replace(
|
||||||
"\n", ""
|
"STARTPROPERTIES", ""
|
||||||
)
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("WEIGHT_NAME"):
|
if line.startswith("FONTNAME_REGISTRY"):
|
||||||
properties["WEIGHT_NAME"] = line.replace("WEIGHT_NAME", "").replace(
|
properties["FONTNAME_REGISTRY"] = line.replace(
|
||||||
"\n", ""
|
"FONTNAME_REGISTRY", ""
|
||||||
)
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("SLANT"):
|
if line.startswith("FOUNDRY"):
|
||||||
properties["SLANT"] = line.replace("SLANT", "").replace("\n", "")
|
properties["FOUNDRY"] = line.replace("FOUNDRY", "").replace(
|
||||||
|
"\n", ""
|
||||||
|
)
|
||||||
|
|
||||||
if line.startswith("SETWIDTH_NAME"):
|
if line.startswith("FAMILY_NAME"):
|
||||||
properties["SETWIDTH_NAME"] = line.replace("SETWIDTH_NAME", "").replace(
|
properties["FAMILY_NAME"] = line.replace("FAMILY_NAME", "").replace(
|
||||||
"\n", ""
|
"\n", ""
|
||||||
)
|
)
|
||||||
|
|
||||||
if line.startswith("ADD_STYLE_NAME"):
|
if line.startswith("WEIGHT_NAME"):
|
||||||
properties["ADD_STYLE_NAME"] = line.replace(
|
properties["WEIGHT_NAME"] = line.replace("WEIGHT_NAME", "").replace(
|
||||||
"ADD_STYLE_NAME", ""
|
"\n", ""
|
||||||
).replace("\n", "")
|
)
|
||||||
|
|
||||||
if line.startswith("PIXEL_SIZE"):
|
if line.startswith("SLANT"):
|
||||||
properties["PIXEL_SIZE"] = line.replace("PIXEL_SIZE", "").replace(
|
properties["SLANT"] = line.replace("SLANT", "").replace("\n", "")
|
||||||
"\n", ""
|
|
||||||
)
|
|
||||||
|
|
||||||
if line.startswith("POINT_SIZE"):
|
if line.startswith("SETWIDTH_NAME"):
|
||||||
properties["POINT_SIZE"] = line.replace("POINT_SIZE", "").replace(
|
properties["SETWIDTH_NAME"] = line.replace(
|
||||||
"\n", ""
|
"SETWIDTH_NAME", ""
|
||||||
)
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("RESOLUTION_X"):
|
if line.startswith("ADD_STYLE_NAME"):
|
||||||
properties["RESOLUTION_X"] = line.replace("RESOLUTION_X", "").replace(
|
properties["ADD_STYLE_NAME"] = line.replace(
|
||||||
"\n", ""
|
"ADD_STYLE_NAME", ""
|
||||||
)
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("RESOLUTION_Y"):
|
if line.startswith("PIXEL_SIZE"):
|
||||||
properties["RESOLUTION_Y"] = line.replace("RESOLUTION_Y", "").replace(
|
properties["PIXEL_SIZE"] = line.replace("PIXEL_SIZE", "").replace(
|
||||||
"\n", ""
|
"\n", ""
|
||||||
)
|
)
|
||||||
|
|
||||||
if line.startswith("SPACING"):
|
if line.startswith("POINT_SIZE"):
|
||||||
properties["SPACING"] = line.replace("SPACING", "").replace("\n", "")
|
properties["POINT_SIZE"] = line.replace("POINT_SIZE", "").replace(
|
||||||
|
"\n", ""
|
||||||
|
)
|
||||||
|
|
||||||
if line.startswith("AVERAGE_WIDTH"):
|
if line.startswith("RESOLUTION_X"):
|
||||||
properties["AVERAGE_WIDTH"] = line.replace("AVERAGE_WIDTH", "").replace(
|
properties["RESOLUTION_X"] = line.replace(
|
||||||
"\n", ""
|
"RESOLUTION_X", ""
|
||||||
)
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("CHARSET_REGISTRY"):
|
if line.startswith("RESOLUTION_Y"):
|
||||||
properties["CHARSET_REGISTRY"] = line.replace(
|
properties["RESOLUTION_Y"] = line.replace(
|
||||||
"CHARSET_REGISTRY", ""
|
"RESOLUTION_Y", ""
|
||||||
).replace("\n", "")
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("CHARSET_ENCODING"):
|
if line.startswith("SPACING"):
|
||||||
properties["CHARSET_ENCODING"] = line.replace(
|
properties["SPACING"] = line.replace("SPACING", "").replace(
|
||||||
"CHARSET_ENCODING", ""
|
"\n", ""
|
||||||
).replace("\n", "")
|
)
|
||||||
|
|
||||||
if line.startswith("DEFAULT_CHAR"):
|
if line.startswith("AVERAGE_WIDTH"):
|
||||||
properties["DEFAULT_CHAR"] = line.replace("DEFAULT_CHAR", "").replace(
|
properties["AVERAGE_WIDTH"] = line.replace(
|
||||||
"\n", ""
|
"AVERAGE_WIDTH", ""
|
||||||
)
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("FONT_DESCENT"):
|
if line.startswith("CHARSET_REGISTRY"):
|
||||||
properties["FONT_DESCENT"] = line.replace("FONT_DESCENT", "").replace(
|
properties["CHARSET_REGISTRY"] = line.replace(
|
||||||
"\n", ""
|
"CHARSET_REGISTRY", ""
|
||||||
)
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("FONT_ASCENT"):
|
if line.startswith("CHARSET_ENCODING"):
|
||||||
properties["FONT_ASCENT"] = line.replace("FONT_ASCENT", "").replace(
|
properties["CHARSET_ENCODING"] = line.replace(
|
||||||
"\n", ""
|
"CHARSET_ENCODING", ""
|
||||||
)
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("COPYRIGHT"):
|
if line.startswith("DEFAULT_CHAR"):
|
||||||
properties["COPYRIGHT"] = line.replace("COPYRIGHT", "").replace(
|
properties["DEFAULT_CHAR"] = line.replace(
|
||||||
"\n", ""
|
"DEFAULT_CHAR", ""
|
||||||
)
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("CAP_HEIGHT"):
|
if line.startswith("FONT_DESCENT"):
|
||||||
properties["CAP_HEIGHT"] = line.replace("CAP_HEIGHT", "").replace(
|
properties["FONT_DESCENT"] = line.replace(
|
||||||
"\n", ""
|
"FONT_DESCENT", ""
|
||||||
)
|
).replace("\n", "")
|
||||||
|
|
||||||
if line.startswith("X_HEIGHT"):
|
if line.startswith("FONT_ASCENT"):
|
||||||
properties["X_HEIGHT"] = line.replace("X_HEIGHT", "").replace("\n", "")
|
properties["FONT_ASCENT"] = line.replace("FONT_ASCENT", "").replace(
|
||||||
|
"\n", ""
|
||||||
|
)
|
||||||
|
|
||||||
if line.startswith("_GBDFED_INFO"):
|
if line.startswith("COPYRIGHT"):
|
||||||
properties["_GBDFED_INFO"] = line.replace("_GBDFED_INFO", "").replace(
|
properties["COPYRIGHT"] = line.replace("COPYRIGHT", "").replace(
|
||||||
"\n", ""
|
"\n", ""
|
||||||
)
|
)
|
||||||
|
|
||||||
line = bdf.readline()
|
if line.startswith("CAP_HEIGHT"):
|
||||||
return properties
|
properties["CAP_HEIGHT"] = line.replace("CAP_HEIGHT", "").replace(
|
||||||
|
"\n", ""
|
||||||
|
)
|
||||||
|
|
||||||
|
if line.startswith("X_HEIGHT"):
|
||||||
|
properties["X_HEIGHT"] = line.replace("X_HEIGHT", "").replace(
|
||||||
|
"\n", ""
|
||||||
|
)
|
||||||
|
|
||||||
def from_hex(values, columns):
|
if line.startswith("_GBDFED_INFO"):
|
||||||
return np.array(
|
properties["_GBDFED_INFO"] = line.replace(
|
||||||
[list(f"{int(row[:2], 16):0>{columns}b}") for row in values.split("\n")],
|
"_GBDFED_INFO", ""
|
||||||
dtype=int,
|
).replace("\n", "")
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_chars(bdfFile):
|
|
||||||
font = {}
|
|
||||||
props = parse_properties(bdfFile)
|
|
||||||
cols = props["FONTBOUNDINGBOX"][0]
|
|
||||||
rows = props["FONTBOUNDINGBOX"][1]
|
|
||||||
|
|
||||||
with open(bdfFile, "r") as bdf:
|
|
||||||
line = bdf.readline()
|
|
||||||
|
|
||||||
# Go through file until chars start.
|
|
||||||
while not line.startswith("CHARS "):
|
|
||||||
line = bdf.readline()
|
|
||||||
characters = int(line.split(" ")[1])
|
|
||||||
|
|
||||||
for character in range(characters):
|
|
||||||
line = bdf.readline()
|
|
||||||
char = line.split(" ")[1].replace("\n", "")
|
|
||||||
|
|
||||||
while not line.startswith("BITMAP"):
|
|
||||||
line = bdf.readline()
|
line = bdf.readline()
|
||||||
|
return properties
|
||||||
|
|
||||||
bits = ""
|
def parse_chars(self, bdfFile):
|
||||||
for row in range(rows):
|
font = {}
|
||||||
|
cols = self.properties["FONTBOUNDINGBOX"][0]
|
||||||
|
rows = self.properties["FONTBOUNDINGBOX"][1]
|
||||||
|
|
||||||
bits += bdf.readline()
|
with open(bdfFile, "r") as bdf:
|
||||||
font[char] = from_hex(bits.strip(), cols)
|
line = bdf.readline()
|
||||||
|
|
||||||
if bdf.readline().startswith("ENDFONT"):
|
# Go through file until chars start.
|
||||||
return font
|
while not line.startswith("CHARS "):
|
||||||
return font
|
line = bdf.readline()
|
||||||
|
characters = int(line.split(" ")[1])
|
||||||
|
|
||||||
|
for character in range(characters):
|
||||||
|
line = bdf.readline()
|
||||||
|
char = line.split(" ")[1].replace("\n", "")
|
||||||
|
|
||||||
|
while not line.startswith("BITMAP"):
|
||||||
|
line = bdf.readline()
|
||||||
|
|
||||||
|
bits = ""
|
||||||
|
for row in range(rows):
|
||||||
|
|
||||||
|
bits += bdf.readline()
|
||||||
|
font[char] = self.from_hex(bits.strip(), cols)
|
||||||
|
|
||||||
|
if bdf.readline().startswith("ENDFONT"):
|
||||||
|
return font
|
||||||
|
return font
|
||||||
|
|
||||||
|
def from_hex(self, values, columns):
|
||||||
|
return np.array(
|
||||||
|
[list(f"{int(row[:2], 16):0>{columns}b}") for row in values.split("\n")],
|
||||||
|
dtype=int,
|
||||||
|
)
|
||||||
|
|
||||||
|
def word(self, word: str):
|
||||||
|
matrix = self.chars[word[0]]
|
||||||
|
for char in word[1:]:
|
||||||
|
matrix = np.concatenate((matrix, self.chars[char]), axis=1)
|
||||||
|
|
||||||
|
return matrix
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user