From e823f939cf36d6cbc7b6fdd134df5292164c866e Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Fri, 6 Nov 2020 14:01:33 -0500 Subject: [PATCH] initial font format --- pdfmajor/fonts/__init__.py | 2 ++ pdfmajor/fonts/base.py | 27 ++++++++++++++++++ pdfmajor/fonts/type1.py | 56 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 pdfmajor/fonts/__init__.py create mode 100644 pdfmajor/fonts/base.py create mode 100644 pdfmajor/fonts/type1.py diff --git a/pdfmajor/fonts/__init__.py b/pdfmajor/fonts/__init__.py new file mode 100644 index 0000000..2050bc6 --- /dev/null +++ b/pdfmajor/fonts/__init__.py @@ -0,0 +1,2 @@ +from .base import PDFFont +from .type1 import PDFType1 diff --git a/pdfmajor/fonts/base.py b/pdfmajor/fonts/base.py new file mode 100644 index 0000000..f4cdf0c --- /dev/null +++ b/pdfmajor/fonts/base.py @@ -0,0 +1,27 @@ +from abc import ABCMeta, abstractclassmethod +from typing import Any, Dict, Literal, Union + +FontType = Union[ + Literal["Type0"], + Literal["Type1"], + Literal["MMType1"], + Literal["Type3"], + Literal["TrueType"], + Literal["CIDFontType0"], + Literal["CIDFontType2"], +] + + +class PDFFont(metaclass=ABCMeta): + """an object reprsenting a PDF Font as decribed in PDF Spec 1.7 section 9.5""" + + def __init__(self, subtype: FontType, **kwrg: Any) -> None: + self.subtype = subtype + self.raw = kwrg + + def __repr__(self) -> str: + return f"PDFFont:{self.subtype}({self.raw})" + + @abstractclassmethod + def from_dict(cls, d: Dict[str, Any]) -> "PDFFont": + raise NotImplementedError diff --git a/pdfmajor/fonts/type1.py b/pdfmajor/fonts/type1.py new file mode 100644 index 0000000..13a99fd --- /dev/null +++ b/pdfmajor/fonts/type1.py @@ -0,0 +1,56 @@ +from typing import Any, Dict, Union + +from .base import PDFFont +from . import standard14 + + +class PDFType1(PDFFont): + """A standard font in the PDF, as is defined by PDF spec 1.7 section 9.6.2""" + + def __init__( + self, + base_font: str, + widths: Dict[int, int], + font_descriptor: Dict[str, int], + encoding: Union[str, Dict[str, Any], None], + to_unicode: Any, + ) -> None: + PDFFont.__init__( + self, + "Type1", + base_font=base_font, + widths=widths, + font_descriptor=font_descriptor, + encoding=encoding, + to_unicode=to_unicode, + ) + + @classmethod + def from_dict(cls, d: Dict[str, Any]) -> "PDFType1": + base_font = str(d["BaseFont"]) + stnd_font = standard14.get_ifexists(base_font) + encoding = d.get("Encoding", None) + to_unicode = d.get("ToUnicode", None) + if stnd_font is not None: + return cls( + base_font=base_font, + widths={k: v for k, v in zip(stnd_font.char_codes, stnd_font.widths)}, + font_descriptor=stnd_font.descriptor, + encoding=encoding, + to_unicode=to_unicode, + ) + else: + first_char = int(d.get("FirstChar", 0)) + last_char = int(d.get("FirstChar", 255)) + if d.get("Widths"): + widths_array = map(int, d["Widths"]) + else: + widths_array = (0 for _ in range((last_char - first_char + 1))) + font_descriptor = dict(d["FontDescriptor"]) # type: Any + return cls( + base_font=base_font, + widths={(i + first_char): w for (i, w) in enumerate(widths_array)}, + font_descriptor=font_descriptor, + encoding=encoding, + to_unicode=to_unicode, + )