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