o
    jX                     @  sZ  d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ erdd dlmZ d dlmZ d dlm Z  d dl!m"Z" z
d dl#m$Z$ dZ%W n e&yy   dZ%Y nw dZ'dZ(dZ)dZ*dZ+dZ,dZ-dZ.eddG dd dZ/eddG dd dZ0eG dd  d Z1d!S )"    )annotations)Sequence)	dataclassfield)TYPE_CHECKINGAnycast)ArrayObjectDictionaryObject
NameObjectNumberObjectStreamObject   )get_encodingadobe_glyphs)logger_warning)	FontFlags)PdfReadError)BytesIO)table__h_e_a_d)table__p_o_s_t)table_O_S_2f_2)TTFontTF            	   
      )frozenc                   @  s   e Zd ZU dZdZded< dZded< dZded< dZded	< d
Z	ded< dZ
ded< dZded< dZded< dZded< edd dZded< dZded< dS )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strnamefamilyweightg     @floatascent      idescentg     @
cap_heightg     @@x_height        italic_angle    intflagsc                   C  s   dS )N)g      Yr*        @@g      @ r4   r4   r4   Q/var/www/html/fyndo/pharma/fyndo/venv/lib/python3.10/site-packages/pypdf/_font.py<lambda><   s    zFontDescriptor.<lambda>default_factoryz!tuple[float, float, float, float]bboxNzStreamObject | None	font_file)__name__
__module____qualname____doc__r%   __annotations__r&   r'   r)   r+   r,   r-   r/   r2   r   r9   r:   r4   r4   r4   r5   r"   (   s   
 r"   c                   @  s   e Zd ZU ded< ded< dS )CoreFontMetricsr"   font_descriptordict[str, int]character_widthsN)r;   r<   r=   r?   r4   r4   r4   r5   r@   @   s   
 r@   c                   @  s  e Zd ZU dZded< ded< eedZded< d	Zded
< ee	dZ
ded< edd dZded< dZded< dZded< edBddZedCd d!ZedDd$d%ZedEd&d'ZedFd*d+ZedGd,d-ZedHd4d5ZedId8d9ZdJd:d;ZdKdLd?d@ZdAS )M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%   str | dict[int, str]encodingr7   dict[Any, Any]character_mapr#   sub_typer"   rA   c                   C  s   ddiS )Ndefault  r4   r4   r4   r4   r5   r6   _   s    zFont.<lambda>rB   rC      zfloat | intspace_widthTboolinterpretablepdf_font_dictr
   char_mapcurrent_widthsreturnNonec           
        s   t t| d }| ddt ts#| fddt|D  dS t|D ]%\}}| }t|g d}||}	|	rFt	|||	< q't	|||< q'dS )zcParses a TrueType or Type1 font's /Widths array from a font dictionary and updates character widths/Widthsz
/FirstCharr   c                   s*   i | ]\}}  | t| |qS r4   )getchr).0idxwidthrF   
first_charr4   r5   
<dictcomp>o   s    z8Font._collect_tt_t1_character_widths.<locals>.<dictcomp>Nsurrogatepass)
r   r	   rV   
isinstancer$   update	enumeratebytesdecoder1   )
rP   rQ   rF   rR   widths_arrayrY   rZ   	char_coderaw_charunicode_charr4   r[   r5   _collect_tt_t1_character_widthsc   s   

z$Font._collect_tt_t1_character_widthsd_fontc              
     sb  dd |  D d}| dg }t|D ]\}}| }|r$|d8 }qt|ttfs5td| dt q||d   }t|t	rg||}}	|
fddtttt|tt|t|	 d|	D  d}qt|ttfrt||d	   ttfr||||d	   }}
 |
 fd
dttt|tt|
d dD  d	}qtd| dt qdS )zRParses the /W array from a DescendantFont dictionary and updates character widths.c                 S  s$   i | ]\}}t |trt||qS r4   )r_   r$   ord)rX   _target
_surrogater4   r4   r5   r]      s    z6Font._collect_cid_character_widths.<locals>.<dictcomp>r   z/Wr   z&Expected numeric value for width, got z. Ignoring it.c                   s"   i | ]\}}| v r | |qS r4   r4   )rX   _cidx_width)ord_mapr4   r5   r]      s
    	r   c                   s   i | ]}|v r|  qS r4   r4   )rX   rm   const_widthro   r4   r5   r]      s
    z-Invalid font width definition. Last element: .N)itemsrV   ra   
get_objectr_   r1   r(   r   r;   r   r`   zipranger   len)ri   rQ   rR   
skip_count_wrY   w_entryw_next_entry	start_idx
width_liststop_idxr4   rp   r5   _collect_cid_character_widths   sb   




	
z"Font._collect_cid_character_widthsr2   r1   c                 C  s   | sd| d< d S d| v r.| d dkr.|t j@ t jkr"| d | d< d S td| d  | d< d S dd |  D }|rAt|t| nd| d< d S )NrK   rJ    r   r   c                 S  s   g | ]}|d kr|qS )r   r4   )rX   wr4   r4   r5   
<listcomp>   s    z+Font._add_default_width.<locals>.<listcomp>)r   FIXED_PITCHr1   valuessumrw   )rR   r2   valid_widthsr4   r4   r5   _add_default_width   s    zFont._add_default_widthc                 C  s<   |  dd}|dkr|S |tj@ tjkr| d S | d d S )Nr   r   rJ   r   )rV   r   r   )rC   r2   rM   r4   r4   r5   _add_space_width   s   zFont._add_space_widthfont_descriptor_objdict[str, Any]c                 C  s   i }dD ]\}}|| v r| | ||< qd|v r.t tt|d }t|dks*J |||d< dD ]>}|| v rnd|v rAtd|  z| |  }||d< W q0 tym } ztd|d|  d	| t W Y d }~q0d }~ww q0|S )
N)
)z	/FontNamer%   )z/FontFamilyr&   )z/FontWeightr'   )z/Ascentr)   )z/Descentr+   )z
/CapHeightr,   )z/XHeightr-   )z/ItalicAngler/   )z/Flagsr2   )	/FontBBoxr9   r9   r   )z	/FontFilez
/FontFile2z
/FontFile3r:   z!More than one /FontFile found in zFailed to get z in z: )tuplemapr(   rw   r   rt   r   r;   )r   font_descriptor_kwargs
source_key
target_key
bbox_tupler:   er4   r4   r5   _parse_font_descriptor   s,   (	zFont._parse_font_descriptorc              
   C  s.  ddl m} |ddd}|ddd}t|\}}d }i }d}	|ddv r|d	krAd
|vrAtdd |dp=g D }	|	rd|v rP| |||| n||v r^|| j}|| j}d|v r|dt	 
 }
d|
v rztt|
d 
 |d< tdi | |
}nhd|v rttttt|d }t|dksJ |t||d}nGttt|d D ]=\}}tt	|
 }|tt|d |< | ||| d|v rtt|d 
 |d< |dt	 
 }
tdi | |
}q|st|d}|dddkr| ||j | ||j}| ||||||||	dS )Nr   )CORE_FONT_METRICS	/BaseFontr#   //SubtypeT)/Type1z/MMType1z	/TrueTypez/Type3Type3z
/ToUnicodec                 s  s    | ]}|t v V  qd S )Nr   )rX   cnamer4   r4   r5   	<genexpr>$  s
    
z*Font.from_font_resource.<locals>.<genexpr>z
/CharProcsrU   z/FontDescriptorz/MissingWidthrJ   r   r   )r%   r9   z/DescendantFontsz/DW)r%   r%   rI   rF   rA   rH   rC   rM   rO   r4   )pypdf._codecs.core_font_metricsr   rV   removeprefixr   allrh   rA   rC   r
   rt   r   r1   r"   r   r   r   r(   r	   rw   ra   r   r   r2   r   )clsrP   r   r%   rI   rF   rH   rA   rC   rO   r   r   
d_font_idxri   rM   r4   r4   r5   from_font_resource  sr   


zFont.from_font_resourceheaderr   
postscriptr   os2r   c                 C  s  |r
|j }|jd? }d}| jt@ s|r|jt@ r|tjO }|r+|j}|dkr+|tjO }|r2|j	t
ks9|r>|jdkr>|tjO }|rN|tksI|jtkrN|tjO }|rpd|j  kr[dkskn d|  krfdkskn |dkrp|tjO }|r|tks}|jtthv r|tjO }|S |tjO }|S )	N   r   r.   r   r   r   r      )panosesFamilyClassmacStyleHEADER_MACSTYLE_ITALICfsSelectionOS2_FSSELECTION_ITALICr   ITALICitalicAnglebProportion!OS2_PANOSE_BPROPORTION_MONOSPACEDisFixedPitchr   OS2_SFAMILYSCLASS_SCRIPTSbFamilyTypeOS2_PANOSE_BFAMILYTYPE_SCRIPTSCRIPTbSerifStyleSERIFOS2_SFAMILYSCLASS_SYMBOLIC!OS2_PANOSE_BFAMILYTYPE_DECORATIVE OS2_PANOSE_BFAMILYTYPE_PICTORIALSYMBOLICNONSYMBOLIC)r   r   r   r   family_classr2   r/   r4   r4   r5   %_font_flags_from_truetype_font_tables`  s>   









z*Font._font_flags_from_truetype_font_tablesr:   r   c              
   C  s  t stdt|5}|d }|d }|d j}|dd }|dd }|j}d| }	i }
|dd }|rH| |
d< | |
d	< | |
d
< t	t
|j|	 d|
d< t	t
|j|	 d|
d< |rzt	t
|j|	 d|
d< t	t
|j|	 d|
d< W n	 ty   Y nw | ||||
d< t
|j|	 dt
|j|	 dt
|j|	 dt
|j|	 df|
d< t }| }|| |tdtt|i ||
d< tdi |
}d}i }i }| }|d}|r%| }t|D ]6\}}||}|d u rqt |}|!|}|"dd}|#dd}|||< t	t
|| d |	 d||< qnt$d| %||
d  | &||
d }W d    n	1 sDw   Y  | |j'd|||||ddS )NzDThe 'fontTools' library is required to use 'from_truetype_font_file'headhheahmtxpostzOS/2r3   r%   r&   r'   r   r)   r+   r,   r-   r2   r9   z/Length1r:   	utf_16_becmapr   bigz	utf-16-ber^   z$Font file does not have a cmap tableTrueTypeTr   r4   )(HAS_FONTTOOLSImportErrorr   metricsrV   
unitsPerEmgetBestFullNamegetBestFamilyNamegetBestSubFamilyNamer1   roundr)   r+   
sCapHeightsxHeightAttributeErrorr   xMinyMinxMaxyMaxr   getvalueset_datar`   r   r   rw   r"   getGlyphOrderbuildReversedMinra   rW   
getGlyphIDto_bytesrc   r   r   r   r%   )r   r:   tt_font_objectr   horizontal_headerr   r   r   units_per_emscale_factorr   namesfont_file_datafont_file_raw_bytesrA   rF   rC   rH   glyph_ordertt_font_cmap_tablereverse_cmapgidglyphre   char	gid_bytesgid_key_stringrM   r4   r4   r5   from_truetype_font_file  s   




 MzFont.from_truetype_font_filec                 C  sT   t tdtdtdtd| j tdtdtdtd| j tdtd	iS )
Nr   r   z/Namer   z/Typez/Fontr   z	/Encodingz/WinAnsiEncoding)r
   r   r%   selfr4   r4   r5   as_font_resource  s   zFont.as_font_resource textr(   c                   s   t  fdd|D dS )zDSum of character widths specified in PDF font for the supplied text.c                   s    g | ]} j | j d  qS )rJ   )rC   rV   )rX   r   r   r4   r5   r      s     z#Font.text_width.<locals>.<listcomp>r.   )r   )r   r   r4   r   r5   
text_width  s   zFont.text_widthN)
rP   r
   rQ   rG   rF   rE   rR   rB   rS   rT   )ri   r
   rQ   rG   rR   rB   rS   rT   )rR   rB   r2   r1   rS   rT   )rC   rB   r2   r1   rS   r1   )r   r
   rS   r   )rP   r
   rS   rD   )r   r   r   r   r   r   rS   r1   )r:   r   rS   rD   )rS   r
   )r   )r   r$   rS   r(   )r;   r<   r=   r>   r?   r   dictrH   rI   r"   rA   rC   rM   rO   staticmethodrh   r   r   r   r   classmethodr   r   r   r   r   r4   r4   r4   r5   rD   F   s8   
 H
#Q4
[rD   N)2
__future__r   collections.abcr   dataclassesr   r   typingr   r   r   pypdf.genericr	   r
   r   r   r   _cmapr   _codecs.adobe_glyphsr   _utilsr   	constantsr   errorsr   ior   fontTools.ttLib.tables._h_e_a_dr   fontTools.ttLib.tables._p_o_s_tr   fontTools.ttLib.tables.O_S_2f_2r   fontTools.ttLibr   r   r   r   r   r   r   r   r   r   r   r"   r@   rD   r4   r4   r4   r5   <module>   sF    