1
0
mirror of https://gitlab.com/MisterBiggs/bdfparse.git synced 2025-06-16 14:36:40 +00:00

changed code to classes

This commit is contained in:
Anson 2019-07-16 19:22:40 -07:00
parent cc635ce8f6
commit 7ff36f34eb

View File

@ -1,8 +1,14 @@
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]
def parse_properties(self, bdfFile):
properties = {"COMMENT": []} properties = {"COMMENT": []}
with open(bdfFile, "r") as bdf: with open(bdfFile, "r") as bdf:
@ -25,7 +31,9 @@ def parse_properties(bdfFile):
properties["FONTBOUNDINGBOX"] = [ properties["FONTBOUNDINGBOX"] = [
int(x) int(x)
for x in ( for x in (
line.replace("FONTBOUNDINGBOX ", "").replace("\n", "").split() line.replace("FONTBOUNDINGBOX ", "")
.replace("\n", "")
.split()
) )
] ]
@ -40,7 +48,9 @@ def parse_properties(bdfFile):
).replace("\n", "") ).replace("\n", "")
if line.startswith("FOUNDRY"): if line.startswith("FOUNDRY"):
properties["FOUNDRY"] = line.replace("FOUNDRY", "").replace("\n", "") properties["FOUNDRY"] = line.replace("FOUNDRY", "").replace(
"\n", ""
)
if line.startswith("FAMILY_NAME"): if line.startswith("FAMILY_NAME"):
properties["FAMILY_NAME"] = line.replace("FAMILY_NAME", "").replace( properties["FAMILY_NAME"] = line.replace("FAMILY_NAME", "").replace(
@ -56,9 +66,9 @@ def parse_properties(bdfFile):
properties["SLANT"] = line.replace("SLANT", "").replace("\n", "") properties["SLANT"] = line.replace("SLANT", "").replace("\n", "")
if line.startswith("SETWIDTH_NAME"): if line.startswith("SETWIDTH_NAME"):
properties["SETWIDTH_NAME"] = line.replace("SETWIDTH_NAME", "").replace( properties["SETWIDTH_NAME"] = line.replace(
"\n", "" "SETWIDTH_NAME", ""
) ).replace("\n", "")
if line.startswith("ADD_STYLE_NAME"): if line.startswith("ADD_STYLE_NAME"):
properties["ADD_STYLE_NAME"] = line.replace( properties["ADD_STYLE_NAME"] = line.replace(
@ -76,23 +86,25 @@ def parse_properties(bdfFile):
) )
if line.startswith("RESOLUTION_X"): if line.startswith("RESOLUTION_X"):
properties["RESOLUTION_X"] = line.replace("RESOLUTION_X", "").replace( properties["RESOLUTION_X"] = line.replace(
"\n", "" "RESOLUTION_X", ""
) ).replace("\n", "")
if line.startswith("RESOLUTION_Y"): if line.startswith("RESOLUTION_Y"):
properties["RESOLUTION_Y"] = line.replace("RESOLUTION_Y", "").replace( properties["RESOLUTION_Y"] = line.replace(
"\n", "" "RESOLUTION_Y", ""
) ).replace("\n", "")
if line.startswith("SPACING"): if line.startswith("SPACING"):
properties["SPACING"] = line.replace("SPACING", "").replace("\n", "") properties["SPACING"] = line.replace("SPACING", "").replace(
if line.startswith("AVERAGE_WIDTH"):
properties["AVERAGE_WIDTH"] = line.replace("AVERAGE_WIDTH", "").replace(
"\n", "" "\n", ""
) )
if line.startswith("AVERAGE_WIDTH"):
properties["AVERAGE_WIDTH"] = line.replace(
"AVERAGE_WIDTH", ""
).replace("\n", "")
if line.startswith("CHARSET_REGISTRY"): if line.startswith("CHARSET_REGISTRY"):
properties["CHARSET_REGISTRY"] = line.replace( properties["CHARSET_REGISTRY"] = line.replace(
"CHARSET_REGISTRY", "" "CHARSET_REGISTRY", ""
@ -104,14 +116,14 @@ def parse_properties(bdfFile):
).replace("\n", "") ).replace("\n", "")
if line.startswith("DEFAULT_CHAR"): if line.startswith("DEFAULT_CHAR"):
properties["DEFAULT_CHAR"] = line.replace("DEFAULT_CHAR", "").replace( properties["DEFAULT_CHAR"] = line.replace(
"\n", "" "DEFAULT_CHAR", ""
) ).replace("\n", "")
if line.startswith("FONT_DESCENT"): if line.startswith("FONT_DESCENT"):
properties["FONT_DESCENT"] = line.replace("FONT_DESCENT", "").replace( properties["FONT_DESCENT"] = line.replace(
"\n", "" "FONT_DESCENT", ""
) ).replace("\n", "")
if line.startswith("FONT_ASCENT"): if line.startswith("FONT_ASCENT"):
properties["FONT_ASCENT"] = line.replace("FONT_ASCENT", "").replace( properties["FONT_ASCENT"] = line.replace("FONT_ASCENT", "").replace(
@ -129,29 +141,22 @@ def parse_properties(bdfFile):
) )
if line.startswith("X_HEIGHT"): if line.startswith("X_HEIGHT"):
properties["X_HEIGHT"] = line.replace("X_HEIGHT", "").replace("\n", "") properties["X_HEIGHT"] = line.replace("X_HEIGHT", "").replace(
if line.startswith("_GBDFED_INFO"):
properties["_GBDFED_INFO"] = line.replace("_GBDFED_INFO", "").replace(
"\n", "" "\n", ""
) )
if line.startswith("_GBDFED_INFO"):
properties["_GBDFED_INFO"] = line.replace(
"_GBDFED_INFO", ""
).replace("\n", "")
line = bdf.readline() line = bdf.readline()
return properties return properties
def parse_chars(self, bdfFile):
def from_hex(values, columns):
return np.array(
[list(f"{int(row[:2], 16):0>{columns}b}") for row in values.split("\n")],
dtype=int,
)
def parse_chars(bdfFile):
font = {} font = {}
props = parse_properties(bdfFile) cols = self.properties["FONTBOUNDINGBOX"][0]
cols = props["FONTBOUNDINGBOX"][0] rows = self.properties["FONTBOUNDINGBOX"][1]
rows = props["FONTBOUNDINGBOX"][1]
with open(bdfFile, "r") as bdf: with open(bdfFile, "r") as bdf:
line = bdf.readline() line = bdf.readline()
@ -172,9 +177,22 @@ def parse_chars(bdfFile):
for row in range(rows): for row in range(rows):
bits += bdf.readline() bits += bdf.readline()
font[char] = from_hex(bits.strip(), cols) font[char] = self.from_hex(bits.strip(), cols)
if bdf.readline().startswith("ENDFONT"): if bdf.readline().startswith("ENDFONT"):
return font return font
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