Skip to content

Commit 5af9785

Browse files
committed
ENH: _font: Init FontDescriptor from font resource
This patch adds a class method to initialise FontDescriptor using a font resource dictionary. For now, this returns either a font name and FontDescriptor defaults, or, in case of one of the 14 core fonts, the associated font metrics. For future development, this class can be extended with code that collects character widths, which is now present in multple places, such as: - pypdf/_text_extraction/_layout_mode/_font.py - pypdf/_cmap.py ... and code that collects font descriptor information, which is now present nowhere yet, but would be useful for generating text streams.
1 parent 1f95c56 commit 5af9785

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

pypdf/_font.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass, field
2+
from typing import Any, Optional
23

34

45
@dataclass(frozen=True)
@@ -22,4 +23,11 @@ class FontDescriptor:
2223

2324
character_widths: dict[str, int] = field(default_factory=dict)
2425

25-
>>>>>>> aceebe0b (ENH: _font: Set some defaults for the FontDescriptor class)
26+
@classmethod
27+
def from_font_res(cls, pdf_font_dict: dict[str, Any]) -> "Optional[FontDescriptor]":
28+
from pypdf._codecs.core_fontmetrics import CORE_FONT_METRICS # noqa: PLC0415
29+
# Prioritize information from the PDF font dictionary
30+
font_name = pdf_font_dict.get("/BaseFont", "Unknown")
31+
if font_name[1:] in CORE_FONT_METRICS:
32+
return CORE_FONT_METRICS.get(font_name[1:])
33+
return cls(name=font_name)

0 commit comments

Comments
 (0)