Skip to content

Commit

Permalink
Fix TypeError by safely parsing floats for cid character widths.
Browse files Browse the repository at this point in the history
  • Loading branch information
pietermarsman committed Jul 9, 2024
1 parent 88139ad commit dcc5c42
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions pdfminer/pdffont.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)

from pdfminer import settings
from pdfminer.casting import safe_float
from pdfminer.cmapdb import (
CMap,
CMapBase,
Expand Down Expand Up @@ -852,7 +853,7 @@ class PDFUnicodeNotDefined(PDFFontError):

# Font widths are maintained in a dict type that maps from *either* unicode
# chars or integer character IDs.
FontWidthDict = Union[Dict[int, float], Dict[str, float]]
FontWidthDict = Union[Dict[Union[int, str], float]]


class PDFFont:
Expand Down Expand Up @@ -925,14 +926,20 @@ def get_height(self) -> float:
def char_width(self, cid: int) -> float:
# Because character widths may be mapping either IDs or strings,
# we try to lookup the character ID first, then its str equivalent.
cid_width = safe_float(self.widths.get(cid))
if cid_width is not None:
return cid_width * self.hscale

try:
return cast(Dict[int, float], self.widths)[cid] * self.hscale
except KeyError:
str_widths = cast(Dict[str, float], self.widths)
try:
return str_widths[self.to_unichr(cid)] * self.hscale
except (KeyError, PDFUnicodeNotDefined):
return self.default_width * self.hscale
str_cid = self.to_unichr(cid)
cid_width = safe_float(self.widths.get(str_cid))
if cid_width is not None:
return cid_width * self.hscale

except PDFUnicodeNotDefined:
pass

return self.default_width * self.hscale

def char_disp(self, cid: int) -> Union[float, Tuple[Optional[float], float]]:
"""Returns an integer for horizontal fonts, a tuple for vertical fonts."""
Expand Down

0 comments on commit dcc5c42

Please sign in to comment.