added standard fornt loader
This commit is contained in:
1 parent
11e466cd5d
commit
3a7cffb10b
2 files changed
+58
No files matched your search
@@ -0,0 +1,31 @@
|
|||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Dict, List, NamedTuple, Optional
|
||||||
|
|
||||||
|
CORE_14_FOLDER = Path(__file__).parent / "core14"
|
||||||
|
ALL_FILES = {fpath.stem: fpath for fpath in CORE_14_FOLDER.iterdir()}
|
||||||
|
|
||||||
|
|
||||||
|
class StandardFontMetric(NamedTuple):
|
||||||
|
descriptor: Dict[str, Any]
|
||||||
|
char_codes: List[int]
|
||||||
|
widths: List[int]
|
||||||
|
|
||||||
|
|
||||||
|
def get_ifexists(font_name: str) -> Optional[StandardFontMetric]:
|
||||||
|
"""checks if have the font's parsed afm file, and return it's metadata if we do
|
||||||
|
|
||||||
|
Args:
|
||||||
|
font_name (str)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[StandardFontMetric]
|
||||||
|
"""
|
||||||
|
if font_name in ALL_FILES:
|
||||||
|
data = json.loads(ALL_FILES[font_name].read_text())
|
||||||
|
return StandardFontMetric(
|
||||||
|
descriptor=data["descriptor"],
|
||||||
|
char_codes=data["metrics"]["codes"],
|
||||||
|
widths=data["metrics"]["widths"],
|
||||||
|
)
|
||||||
|
return None
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
from unittest import TestCase
|
||||||
|
from pdfmajor.fonts import standard14
|
||||||
|
|
||||||
|
|
||||||
|
class Standard14(TestCase):
|
||||||
|
def test_loading_exists(self):
|
||||||
|
# pdf spec 1.7 section 9.6.2.2
|
||||||
|
standard_list = [
|
||||||
|
"Helvetica",
|
||||||
|
"Courier",
|
||||||
|
"Symbol",
|
||||||
|
"Times-Bold",
|
||||||
|
"Helvetica-Bold",
|
||||||
|
"Courier-Bold",
|
||||||
|
"ZapfDingbats",
|
||||||
|
"Times-Italic",
|
||||||
|
"Helvetica-Oblique",
|
||||||
|
"Courier-Oblique",
|
||||||
|
"Times-BoldItalic",
|
||||||
|
"Helvetica-BoldOblique",
|
||||||
|
"Courier-BoldOblique",
|
||||||
|
]
|
||||||
|
for fontname in standard_list:
|
||||||
|
self.assertIsNotNone(standard14.get_ifexists(fontname))
|
||||||
|
|
||||||
|
def test_invalid_name(self):
|
||||||
|
self.assertIsNone(standard14.get_ifexists("Clowns!"))
|
||||||
Reference in new issue
Block a user