
    ۡiB7                         d dl mZ d dlmZmZ d dlmZmZmZm	Z	 d dl
mZmZmZ ddlmZ ddlmZ ddlmZ  ed	
       G d d             Ze G d d             Zy)    )Sequence)	dataclassfield)AnyOptionalUnioncast)ArrayObjectDictionaryObjectIndirectObject   )get_encodingadobe_glyphs)logger_warningT)frozenc                      e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed<   dZe
ed	<   d
Ze
ed<   dZe
ed<   dZe
ed<   dZeed<    ed       Zee
e
e
e
f   ed<    ed       Zeeef   ed<   edeeef   dedeeef   fd       Zededeeef   deeeeef   f   deeef   ddf
d        Zed!edeeef   deeef   ddfd"       Zedeeef   ddfd#       Ze	 	 d%dedeeeeeef   f      deeeef      dd fd$       Z y)&FontDescriptorz
    Represents the FontDescriptor dictionary as defined in the PDF specification.
    This contains both descriptive and metric information.

    The defaults are derived from the mean values of the 14 core fonts, rounded
    to 100.
    Unknownnamefamilyweightg     @ascent      idescentg     @
cap_heightg     @@x_height        italic_angle    flagsc                       y)N)g      Yr   g     @@g      @ r#       U/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/pypdf/_font.py<lambda>zFontDescriptor.<lambda>    s    r$   default_factorybboxc                  
    ddiS )Ndefault  r#   r#   r$   r%   r&   zFontDescriptor.<lambda>"   s    iQTEU r$   character_widthsfont_kwargsfont_descriptor_objreturnc                     t        |t              r|j                         n|}dD ]  \  }}||v s||   | |<    d| v r6t        t	        t
        | d               }t        |      dk(  sJ |       || d<   | S )N)
)z	/FontNamer   )z/FontFamilyr   )z/FontWeightr   )z/Ascentr   )z/Descentr   )z
/CapHeightr   )z/XHeightr   )z/ItalicAngler   )z/Flagsr!   )z	/FontBBoxr)   r)      )
isinstancer   
get_objecttuplemapfloatlen)r.   r/   font_descriptor_dict
source_key
target_key
bbox_tuples         r%   _parse_font_descriptorz%FontDescriptor._parse_font_descriptor$   s     -~>  **,$ 	
'
 	K"J
 11*>z*JJ'	K [ s5+f*=>?Jz?a'33'",Kr$   pdf_font_dictchar_mapencodingcurrent_widthsNc                    t        t        | d         }| j                  dd      }t        |t              sN|j                  t        |      D ci c]'  \  }}|j                  ||z   t        ||z               |) c}}       yt        |      D ]V  \  }}||z   }t        |g      j                  |d      }	|j                  |	      }
|
rt        |      ||
<   It        |      ||	<   X yc c}}w )zcParses a TrueType or Type1 font's /Widths array from a font dictionary and updates character widths/Widthsz
/FirstCharr   Nsurrogatepass)r	   r
   getr3   strupdate	enumeratechrbytesdecodeint)r>   r?   r@   rA   widths_array
first_charidxwidth	char_coderaw_charunicode_chars              r%   _collect_tt_t1_character_widthsz.FontDescriptor._collect_tt_t1_character_widths@   s     Ky)AB"&&|Q7
(C(!!"+L"9#C S:-s33C/DEuL#   $L1 
	6JCj(Ii[)00?KH#<<1L/25z|,+.u:x(
	6#s   ,C)
d_fontc                 (   |j                         D ci c]!  \  }}t        |t              rt        |      |# }}}d}| j	                  dg       }t        |      D ]  \  }}	|	j                         }	|r|dz  }t        |	t        t        f      st        d|	 dt               J||dz      j                         }
t        |
t              rp|	|
}}|j                  t        t        t        t        |      t        t        |      t!        |      z   d      |      D ci c]  \  }}||v r||   | c}}       d}t        |
t        t        f      rt        ||dz      j                         t        t        f      ro|	|
||dz      j                         }}}|j                  t        t        t        |      t        t        |dz         d      D ci c]  }||v r||   | c}       d}t        d|	 dt                y	c c}}w c c}}w c c}w )
zRParses the /W array from a DescendantFont dictionary and updates character widths.r   z/Wr   z&Expected numeric value for width, got z. Ignoring it.   z-Invalid font width definition. Last element: .N)itemsr3   rF   ordrE   rH   r4   rL   r7   r   __name__r   rG   zipranger	   r8   )rU   r?   rA   _target
_surrogateord_map
skip_count_wrO   w_entryw_next_entry	start_idx
width_list_cidx_widthstop_idxconst_widths                    r%   _collect_cid_character_widthsz,FontDescriptor._collect_cid_character_widths`   s/    (0~~'7
#'3' L*$
 
 
ZZb!%bM 6	LC((*Ga
gU|4 !GyP^_aijcAg;113L,1(/:	%% .1! $S) 4 $S) 4s: F !
 '.)E6 !G+  . 
L3,7J37&&(3,=  sQwK**, &18	
 %% &+ i0$sHqL2I1&! !G+	  3 
 CG9ANg6	
60s   &HH	H
c                     | sd| d<   y d| v ry d| v r| d   dk7  rt        d| d   z        | d<   y | j                         D cg c]
  }|dkD  s	| }}|rt        |      t        |      z  | d<   y d| d<   y c c}w )Nr,   r+    r   rW   )rL   valuessumr8   )rA   wvalid_widthss      r%   _add_default_widthz!FontDescriptor._add_default_width   s    (+N9%&. ^C%8A%=(+As0C,C(DN9% $2#8#8#:Daa!eDDNZC$5\9J$Jy!`cy! Es   
A5A5c           	      :   ddl m} |j                  dd      j                  d      }di i}|j                  d      dv rd	|v r*|r|st	        |      \  }}| j                  ||||d          n&||v r"||   }| j                  |j                         |S d
|v r|j                  d
t                     j                         }t        t        |      }	d|	v r|	d   j                         |d   d<   | j                  ||j                  d
t                           }d|d   vr| j                  |d           | di |S |r|st	        |      \  }}t        t        t        |d               D ]  \  }
}t        t        |j                               }|t        t        |d         |
<   | j                  |||d          d|v r|d   j                         |d   d<   n| j                  |d          | j                  ||j                  d
t                           }  | di |S )Nr   )CORE_FONT_METRICS	/BaseFontr   /r-   /Subtype)z/Type1z/MMType1z	/TrueTypez/Type3rC   z/FontDescriptorz/MissingWidthr+   z/DescendantFontsz/DWr#   )pypdf._codecs.core_fontmetricsrt   rE   removeprefixr   rT   rr   r-   r   r4   r	   r=   rH   r
   rk   )clsr>   r@   r?   rt   	font_namer.   font_descriptorfont_descriptor_resourcer/   
d_font_idxrU   s               r%   from_font_resourcez!FontDescriptor.from_font_resource   s^    	E!%%k9=JJ3O	'92&> Z(,YYM) X)5m)D&Hh33!8X{CU7V //"3I">&&'G'GH&& M1+8+<+<=NP`Pb+c+n+n+p(&*+;=U&V#"&99ATUdAeApApArK 23I>!88!2!23DFVFX!Y ,> ??&&{3E'FG%%%
 X!-m!<Hh"+m,>?@#
 	J *F,=,=,?@FOUDm,>?@L--+.@"A =CE]=U=U=W./	:&&{3E'FG44VZZ(9;K;MNK	  ![!!r$   )NN)!r[   
__module____qualname____doc__r   rF   __annotations__r   r   r   r7   r   r   r   r   r!   rL   r   r)   r5   r-   dictstaticmethodr   r   r=   r   rT   rk   rr   classmethodr   r   r#   r$   r%   r   r      s    D#FCFCFEGUJHeL%E3O.3Dk.lD%ueU*
+l',=U'Vd38nVDcN Qa fjknpsksft  6 6'6sCx.6 T#s(^+,6 S#X	6
 
6 6> F F,0cNFLPQTVYQYNF	F FP d4S> dd d d"  :>-1	="'=" 5d38n!456=" 4S>*	="
 
=" ="r$   r   c                      e Zd ZU dZeed<   eeeeef   f   ed<    e	e      Z
eeef   ed<   dZeed<    e	e      Zeed<    e	e      Zeeef   ed	<   d
Zeeef   ed<   dZeed<   ededd fd       ZddedefdZy)Fonta  
    A font object for use during text extraction and for producing
    text appearance streams.

    Attributes:
        name: Font name, derived from font["/BaseFont"]
        character_map: The font's character map
        encoding: Font encoding
        sub_type: The font type, such as Type1, TrueType, or Type3.
        font_descriptor: Font metrics, including a mapping of characters to widths
        character_widths: A mapping of characters to widths
        space_width: The width of a space, or an approximation
        interpretable: Default True. If False, the font glyphs cannot
            be translated to characters, e.g. Type3 fonts that do not define
            a '/ToUnicode' mapping.

    r   r@   r'   character_mapr   sub_typer|   r-      space_widthTinterpretabler>   r0   c           
         |j                  dd      j                  d      }|j                  dd      j                  d      }t        |      \  }}d}|dk(  r)d|vr%t        d |j                  d	      xs g D              }|rt        j                  |||      }n
t	               }|j                  }|j                  j                  d
      }	|	r|	dk(  r|j                  d   dz  }	 | |||||||	|      S )Nru   r   rv   rw   TType3z
/ToUnicodec              3   ,   K   | ]  }|t         v   y w)Nr   ).0cnames     r%   	<genexpr>z*Font.from_font_resource.<locals>.<genexpr>)  s        % s   z
/CharProcsrm   r   r+   rW   )r   r   r@   r|   r   r-   r   r   )rE   ry   r   allr   r   r-   )
rz   r>   r   r   r@   r   r   r|   r-   r   s
             r%   r   zFont.from_font_resource  s      i8EEcJ $$Z;HHM".}"=- w<}#D  *..|<B  M
 ,??xYfgO,.O*;;%66::3?kQ.)::9EJK+'-#'	
 		
r$   textc           
          t        |D cg c]+  }| j                  j                  || j                  d         - c}d      S c c}w )zDSum of character widths specified in PDF font for the supplied text.r+   r   )ro   r-   rE   )selfr   chars      r%   
text_widthzFont.text_widthC  sB    [_`SWT""&&tT-B-B9-MN`be
 	
`s   0AN) )r[   r   r   r   rF   r   r   r   rL   r   r   r   r   r   r|   r-   r   r7   r   boolr   r   r   r   r#   r$   r%   r   r      s    $ ICc3h'(($)$$?M4S>?Hc&+N&KO^K',T'Bd38nB%(Kucz"(M4'
''
 
'
 '
R
s 
E 
r$   r   N)collections.abcr   dataclassesr   r   typingr   r   r   r	   pypdf.genericr
   r   r   _cmapr   _codecs.adobe_glyphsr   _utilsr   r   r   r#   r$   r%   <module>r      s^    $ ( - - G G  . " $l" l" l"^ J
 J
 J
r$   