@@ -1891,28 +1891,30 @@ def _layout_mode_fonts(self) -> Dict[str, _layout_mode.Font]:
1891
1891
"""
1892
1892
# Font retrieval logic adapted from pypdf.PageObject._extract_text()
1893
1893
objr : Any = self
1894
- while NameObject (PG .RESOURCES ) not in objr :
1895
- objr = objr ["/Parent" ].get_object ()
1896
- resources_dict : Any = objr [PG .RESOURCES ]
1897
1894
fonts : Dict [str , _layout_mode .Font ] = {}
1898
- if "/Font" in resources_dict and self .pdf is not None :
1899
- for font_name in resources_dict ["/Font" ]:
1900
- * cmap , font_dict_obj = build_char_map (font_name , 200.0 , self )
1901
- font_dict = {
1902
- k : self .pdf .get_object (v )
1903
- if isinstance (v , IndirectObject )
1904
- else [
1905
- self .pdf .get_object (_v )
1906
- if isinstance (_v , IndirectObject )
1907
- else _v
1908
- for _v in v
1909
- ]
1910
- if isinstance (v , ArrayObject )
1911
- else v
1912
- for k , v in font_dict_obj .items ()
1913
- }
1914
- # mypy really sucks at unpacking
1915
- fonts [font_name ] = _layout_mode .Font (* cmap , font_dict ) # type: ignore[call-arg,arg-type]
1895
+ while objr is not None :
1896
+ try :
1897
+ resources_dict : Any = objr [PG .RESOURCES ]
1898
+ except KeyError :
1899
+ resources_dict = {}
1900
+ if "/Font" in resources_dict and self .pdf is not None :
1901
+ for font_name in resources_dict ["/Font" ]:
1902
+ * cmap , font_dict_obj = build_char_map (font_name , 200.0 , self )
1903
+ font_dict = {
1904
+ k : v .get_object ()
1905
+ if isinstance (v , IndirectObject )
1906
+ else [_v .get_object () for _v in v ]
1907
+ if isinstance (v , ArrayObject )
1908
+ else v
1909
+ for k , v in font_dict_obj .items ()
1910
+ }
1911
+ # mypy really sucks at unpacking
1912
+ fonts [font_name ] = _layout_mode .Font (* cmap , font_dict ) # type: ignore[call-arg,arg-type]
1913
+ try :
1914
+ objr = objr ["/Parent" ].get_object ()
1915
+ except KeyError :
1916
+ objr = None
1917
+
1916
1918
return fonts
1917
1919
1918
1920
def _layout_mode_text (
0 commit comments