Skip to content
14 changes: 13 additions & 1 deletion pypdf/_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from dataclasses import dataclass, field
from typing import Any, Optional, Union, cast

from pypdf.generic import ArrayObject, DictionaryObject, IndirectObject
from pypdf.generic import ArrayObject, DictionaryObject, IndirectObject, PdfObject, StreamObject

from ._cmap import get_encoding
from ._codecs.adobe_glyphs import adobe_glyphs
from ._utils import logger_warning
from .errors import PdfReadError


@dataclass(frozen=True)
Expand All @@ -32,6 +33,7 @@ class FontDescriptor:
bbox: tuple[float, float, float, float] = field(default_factory=lambda: (-100.0, -200.0, 1000.0, 900.0))

character_widths: dict[str, int] = field(default_factory=lambda: {"default": 500})
font_file: Union[StreamObject, None] = None

@staticmethod
def _parse_font_descriptor(font_kwargs: dict[str, Any], font_descriptor_obj: DictionaryObject) -> dict[str, Any]:
Expand Down Expand Up @@ -59,6 +61,16 @@ def _parse_font_descriptor(font_kwargs: dict[str, Any], font_descriptor_obj: Dic
bbox_tuple = tuple(map(float, font_kwargs["bbox"]))
assert len(bbox_tuple) == 4, bbox_tuple
font_kwargs["bbox"] = bbox_tuple
# Find the binary stream for this font if there is one

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Find the binary stream for this font if there is one
# Find the binary stream for this font if there is one

for source_key in ["/FontFile", "/FontFile2", "/FontFile3"]:
if source_key in font_descriptor_dict:
if "font_file" in font_kwargs:
raise PdfReadError(f"More than one /FontFile found in {font_descriptor_obj}")

file = cast(PdfObject, font_descriptor_dict[source_key])
Comment thread
michelcrypt4d4mus marked this conversation as resolved.
Outdated
file = file.get_object() if isinstance(file, IndirectObject) else file
Comment thread
michelcrypt4d4mus marked this conversation as resolved.
Outdated
font_kwargs["font_file"] = file

return font_kwargs

@staticmethod
Expand Down
Loading