@@ -261,27 +261,36 @@ def __init__(
261261 if font_resource :
262262 font_resource = cast (DictionaryObject , font_resource .get_object ())
263263 font_descriptor = FontDescriptor .from_font_resource (font_resource )
264- _font_subtype , _ , font_encoding , font_map = build_char_map_from_dict (
265- 200 , font_resource
266- )
267- try : # remove width stored in -1 key
268- del font_map [- 1 ]
269- except KeyError :
270- pass
271- font_glyph_byte_map : dict [str , bytes ]
272- if isinstance (font_encoding , str ):
273- font_glyph_byte_map = {
274- v : k .encode (font_encoding ) for k , v in font_map .items ()
275- }
276- else :
277- font_glyph_byte_map = {v : bytes ((k ,)) for k , v in font_encoding .items ()}
278- font_encoding_rev = {v : bytes ((k ,)) for k , v in font_encoding .items ()}
279- for key , value in font_map .items ():
280- font_glyph_byte_map [value ] = font_encoding_rev .get (key , key )
281264 else :
282- logger_warning (f"Font dictionary for { font_name } not found." , __name__ )
283- font_glyph_byte_map = {}
284- font_descriptor = FontDescriptor ()
265+ logger_warning (f"Font dictionary for { font_name } not found; defaulting to Helvetica." , __name__ )
266+ font_name = "/Helv"
267+ font_resource = DictionaryObject ({
268+ NameObject ("/Subtype" ): NameObject ("/Type1" ),
269+ NameObject ("/Name" ): NameObject ("/Helv" ),
270+ NameObject ("/Type" ): NameObject ("/Font" ),
271+ NameObject ("/BaseFont" ): NameObject ("/Helvetica" ),
272+ NameObject ("/Encoding" ): NameObject ("/WinAnsiEncoding" )
273+ })
274+ font_descriptor = CORE_FONT_METRICS ["Helvetica" ]
275+
276+ # Get the font glyph data
277+ _font_subtype , _ , font_encoding , font_map = build_char_map_from_dict (
278+ 200 , font_resource
279+ )
280+ try : # remove width stored in -1 key
281+ del font_map [- 1 ]
282+ except KeyError :
283+ pass
284+ font_glyph_byte_map : dict [str , bytes ]
285+ if isinstance (font_encoding , str ):
286+ font_glyph_byte_map = {
287+ v : k .encode (font_encoding ) for k , v in font_map .items ()
288+ }
289+ else :
290+ font_glyph_byte_map = {v : bytes ((k ,)) for k , v in font_encoding .items ()}
291+ font_encoding_rev = {v : bytes ((k ,)) for k , v in font_encoding .items ()}
292+ for key , value in font_map .items ():
293+ font_glyph_byte_map [value ] = font_encoding_rev .get (key , key )
285294
286295 ap_stream_data = self ._generate_appearance_stream_data (
287296 text ,
@@ -300,13 +309,12 @@ def __init__(
300309 self [NameObject ("/BBox" )] = RectangleObject (rectangle )
301310 self .set_data (ByteStringObject (ap_stream_data ))
302311 self [NameObject ("/Length" )] = NumberObject (len (ap_stream_data ))
303- # Update Resources with font information if necessary
304- if font_resource is not None :
305- self [NameObject ("/Resources" )] = DictionaryObject ({
306- NameObject ("/Font" ): DictionaryObject ({
307- NameObject (font_name ): getattr (font_resource , "indirect_reference" , font_resource )
308- })
312+ # Update Resources with font information
313+ self [NameObject ("/Resources" )] = DictionaryObject ({
314+ NameObject ("/Font" ): DictionaryObject ({
315+ NameObject (font_name ): getattr (font_resource , "indirect_reference" , font_resource )
309316 })
317+ })
310318
311319 @classmethod
312320 def from_text_annotation (
0 commit comments