diff --git a/pdfmajor/fonts/standard14.py b/pdfmajor/fonts/standard14.py new file mode 100644 index 0000000..a13bc28 --- /dev/null +++ b/pdfmajor/fonts/standard14.py @@ -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 diff --git a/tests/test_fonts.py b/tests/test_fonts.py new file mode 100644 index 0000000..337c276 --- /dev/null +++ b/tests/test_fonts.py @@ -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!"))