
    \
j]                     f   d Z ddlZddlZddlZddlZ G d d          Zd Z edddd	d
          Z edddd          Z eddddddddddddddddd          Z	 ed  Z
 ed!d"          Z ed#d$d%          Zd&e_        d'e_        d(e_        d)e_        d*e_         ed+dd	d
          Z ed,d-d.          Z ed#d          Z ed/d0d1          Z ed2d$          Z ed2d$d3d4dd	d
          Z edd5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdD          Z edEdF          Z ed2dGdH          Z ed/d0dIdJd$dK          ZdS )Lz
Implementation of the Truetype file format.

Typical applications will not need to use this module directly; look at
`pyglet.font` instead.

References:
 * http://developer.apple.com/fonts/TTRefMan/RM06
 * http://www.microsoft.com/typography/otspec
    Nc                      e Zd ZdZi dddddddd	d
dddddddddddddddddddddd d!d"d#d$d%d&Zdddd	dd'Zd(d)d*d+d(d,Zdd-iZd. Zd/ Z	d0 Z
d1 Zd2 ZdAd4Zd5 Zd6 Zd7 Zd8 Zd9 Zd: Zd; Zd< Zd= Zd> Zd? Zd@ Zd3S )BTruetypeInfoa  Information about a single Truetype face.

    The class memory-maps the font file to read the tables, so
    it is vital that you call the `close` method to avoid large memory
    leaks.  Once closed, you cannot call any of the ``get_*`` methods.

    Not all tables have been implemented yet (or likely ever will).
    Currently only the name and metric tables are read; in particular
    there is no glyph or hinting information.
    	copyrightr   family   	subfamily   
identifier   name   version   
postscript   	trademark   manufacturer   designer	   description
   z
vendor-url   zdesigner-url   license   zlicense-url   zpreferred-family   zpreferred-subfamily         )zcompatible-namesample)unicode	macintoshiso	microsoftcustom	utf_16_be	shift_jisbig5johab)r   r	   r   r   r   	mac_romanc                 \   |s
J d            t          j        |          j        }t          j        |t           j                  | _        t          t          d          r6t          j        | j        |t          j        t          j	                  | _
        n+t          j        | j        |dt          j                  | _
        d| _        t          | j
        d          }i | _        t                              | j
        |j        |j                  D ]}|| j        |j        <   d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        t9          | j
        | j        d         j                  | _        t?          | j
        | j        d         j                  | _         dS )a3  Read the given TrueType file.

        :Parameters:
            `filename`
                The name of any Windows, OS2 or Macintosh Truetype file.

        The object must be closed (see `close`) after use.

        An exception will be raised if the file does not exist or cannot
        be read.
        zmust provide a font file name
MAP_SHAREDNFr   headhhea)!osstatst_sizeopenO_RDONLY_filenohasattrmmapr/   	PROT_READ_dataACCESS_READ_closed_read_offset_table_tables_read_table_directory_entryarraysize
num_tablestag_names_horizontal_metrics_character_advances_character_kernings_glyph_kernings_character_map
_glyph_map_font_selection_flags_read_head_tableoffsetheader_read_horizontal_headerhorizontal_header)selffilenamelengthoffsetstables        I/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/font/ttf.py__init__zTruetypeInfo.__init__H   sf    88888x""*wx554&& 	Q4<$.YYDJJ4<t?OPPDJ$TZ33066tz7<QXQcdd 	, 	,E&+DL###' #' #' #"%)"&tz4<3G3NOO!8T\RXEYE`!a!a    c                     | j         s1t          | j        | j        d         j                  }|j        | _         | j         S )z9Return the font selection flags, as defined in OS/2 tablezOS/2)rL   _read_OS2_tabler;   r?   rN   fs_selection)rR   	OS2_tables     rW   get_font_selection_flagsz%TruetypeInfo.get_font_selection_flagso   s=    ) 	@'
DL4H4OPPI)2)?D&))rY   c                 J    t          |                                 dz            S )z3Returns True iff the font describes itself as bold.    boolr^   rR   s    rW   is_boldzTruetypeInfo.is_boldv   s!    D1133d:;;;rY   c                 J    t          |                                 dz            S )z5Returns True iff the font describes itself as italic.r   ra   rc   s    rW   	is_italiczTruetypeInfo.is_italicz   s!    D1133c9:::rY   c                 ,   | j         r| j         S t          | j        | j        d         j                  }t
                              | j        | j        d         j        |j        z   |j                  }|j	        | j        d         j        z   }i | _         |D ]y}| j        |j        |z   |j        |z   |j
        z            }|j        |j        f}|j        |j        |f}|| j         vr
g | j         |<   | j         |                             |           z| j         S )a`  Returns a dictionary of names defined in the file.

        The key of each item is a tuple of ``platform_id``, ``name_id``,
        where each ID is the number as described in the Truetype format.

        The value of each item is a tuple of 
        ``encoding_id``, ``language_id``, ``value``, where ``value`` is
        an encoded string.
        r   )rE   _read_naming_tabler;   r?   rN   _read_name_recordrA   rB   countstring_offsetrT   platform_idname_idencoding_idlanguage_idappend)rR   naming_tablename_recordsstoragerecordvaluekeys          rW   	get_nameszTruetypeInfo.get_names~   s    ; 	;)$*dl66J6QRR(..JV,3l6GGI[] ],t|F/C/JJ" 	+ 	+FJv}w68ORXR_8__`E$fn4C');UCE$+%%#%C K##E****{rY   Nc                    |                                  }t          |          t          k    r| j        |         }|s"dD ]}|                     |||          }|r|c S  t          |          t          k    r| j        |         }||f|vrdS |dk    r| j        }|sd}n|dk    r| j        }|sd}|||f         D ]O}|d         |v rC|d         |v r9t          j	        ||d                            } ||d                   d         c S PdS )	af  Returns the value of the given name in this font.

        :Parameters:
            `name`
                Either an integer, representing the name_id desired (see
                font format); or a string describing it, see below for 
                valid names.
            `platform`
                Platform for the requested name.  Can be the integer ID,
                or a string describing it.  By default, the Microsoft
                platform is searched first, then Macintosh.
            `languages`
                A list of language IDs to search.  The first language
                which defines the requested name will be used.  By default,
                all English dialects are searched.

        If the name is not found, ``None`` is returned.  If the name
        is found, the value will be decoded and returned as a unicode
        string.  Currently only some common encodings are supported.

        Valid names to request are (supply as a string)::

            'copyright'
            'family'
            'subfamily'
            'identifier'
            'name'
            'version'
            'postscript'
            'trademark'
            'manufacturer'
            'designer'
            'description'
            'vendor-url'
            'designer-url'
            'license'
            'license-url'
            'preferred-family'
            'preferred-subfamily'
            'compatible-name'
            'sample'

        Valid platforms to request are (supply as a string)::

            'unicode'
            'macintosh'
            'iso'
            'microsoft'
            'custom'
        )r'   r%   Nr   )i	  i	  i	  i	  i	  i	  r   )r   r   r	   )
rw   typestr_name_id_lookupget_name_platform_id_lookup_microsoft_encoding_lookup(_TruetypeInfo__macintosh_encoding_lookupcodecs
getdecoder)	rR   r   platform	languagesnamesru   	encodingsrt   decoders	            rW   r|   zTruetypeInfo.get_name   sY   h   ::'-D 	!6 ! !dHi@@ ! LLL!>>S  /9H$5((4q==7I JI	]]8I ! 	Xt,- 	- 	-FayI%%&)y*@*@ +IfQi,@AAwvay))!,,,,trY   c                     | j         sBt                              | j        | j        d         j        | j        j                  }|| _         | j         S )z5Return all horizontal metric entries in table format.hmtx)rF   _read_long_hor_metricrA   r;   r?   rN   rQ   number_of_h_metrics)rR   ars     rW   get_horizontal_metricsz#TruetypeInfo.get_horizontal_metrics   sQ    ' 	*&,,TZ-1\&-A-H-1-C-WY YB (*D$''rY   c                    | j         r| j         S |                                 }|                                 }i | _         t          t	          |                    D ]+}||v r%||         | j         vr||         | j         ||         <   ,| j         S )zReturn a dictionary of character->advance.

        They key of the dictionary is a unit-length unicode string,
        and the value is a float giving the horizontal advance in
        em.
        )rG   get_glyph_advancesget_glyph_maprangelen)rR   gagmapis       rW   get_character_advancesz#TruetypeInfo.get_character_advances   s     # 	,++$$&&!!###% s2ww 	: 	:ADyyaD,D!D!D46qE(a1''rY   c                 H                                       } fd|D             S )zReturn a dictionary of glyph->advance.

        They key of the dictionary is the glyph index and the value is a float
        giving the horizontal advance in em.
        c                 R    g | ]#}t          |j                  j        j        z  $S  )floatadvance_widthrO   units_per_em).0mrR   s     rW   
<listcomp>z3TruetypeInfo.get_glyph_advances.<locals>.<listcomp>  s-    NNNaao&&)AANNNrY   )r   )rR   hms   ` rW   r   zTruetypeInfo.get_glyph_advances  s0     ((**NNNN2NNNNrY   c                    | j         sz|                                 }|                                 }i | _         |                                D ]6\  }}|\  }}||v r||         pd}||v r||         pd}|r|r|| j         ||f<   7| j         S )a  Return a dictionary of (left,right)->kerning

        The key of the dictionary is a tuple of ``(left, right)``
        where each element is a unit-length unicode string.  The
        value of the dictionary is the horizontal pairwise kerning
        in em.
        N)rH   r   get_glyph_kerningsitems)	rR   r   kernspairru   lglyphrglyphlcharrchars	            rW   get_character_kerningsz#TruetypeInfo.get_character_kernings  s     ' 		E%%''D++--E')D$${{}} E Ee!%$74<?4$74<?4 EU E?DD,eU^<''rY   c                    | j         r| j         S t          | j        | j        d         j                  }| j        d         j        |j        z   }i }t          |j                  D ]}t          | j        |          }|j	        |j
        z  rO|j	        |j        z  s@|j	        |j        z  s1|j	        |j        z  dk    r|                     |||j        z              ||j        z  }|| _         |S )zReturn a dictionary of (left,right)->kerning

        The key of the dictionary is a tuple of ``(left, right)``
        where each element is a glyph index.  The value of the dictionary is
        the horizontal pairwise kerning in em.
        kernr   )rI   _read_kern_header_tabler;   r?   rN   rB   r   n_tables_read_kern_subtable_headercoveragehorizontal_maskminimum_maskperpendicular_maskformat_mask_add_kernings_format0rT   )rR   rO   rN   kerningsr   s        rW   r   zTruetypeInfo.get_glyph_kernings!  s     	(''#DJV0D0KLL 	f%,v{:v'' 	$ 	$A/
FCCF!77 O"Of.AAO"Of.GGO ?V%771<<..x&+9MNNNfm#FF'rY   c                    t          | j        |          }t                              | j        ||j        z   |j                  }|D ]~}|j        |j        f|v r<||j        |j        fxx         |j        t          | j
        j                  z  z  cc<   N|j        t          | j
        j                  z  ||j        |j        f<   d S N)_read_kern_subtable_format0r;   _read_kern_subtable_format0PairrA   rB   n_pairsleftrightru   r   rO   r   )rR   r   rN   rO   kerning_pairsr   s         rW   r   z"TruetypeInfo._add_kernings_format09  s    ,TZ@@7==dj>Dv{>RTZTbd d! 	V 	VD	4:&(22$)TZ0111TZ7<T[=U7V7V6W W1111 59J6;DK<T6U6U5V$)TZ011	V 	VrY   c                     | j         r| j         S |                                 }i | _         |                                D ]\  }}|| j         vr
|| j         |<   | j         S )zCalculate and return a reverse character map.

        Returns a dictionary where the key is a glyph index and the
        value is a unit-length unicode string.
        )rK   get_character_mapr   )rR   cmapchglyphs       rW   r   zTruetypeInfo.get_glyph_mapE  sk     ? 	#?"%%'' 	, 	,IBDO++)+&rY   c                    | j         r| j         S t          | j        | j        d         j                  }t
                              | j        | j        d         j        |j        z   |j                  }i | _         |D ]n}|j	        dk    ra|j
        dk    rV| j        d         j        |j        z   }t          | j        |          }|j        dk    r|                     |          | _          no| j         S )zReturn the character map.

        Returns a dictionary where the key is a unit-length unicode
        string and the value is a glyph index.  Currently only 
        format 4 character maps are read.
        r   r   r   r   )rJ   _read_cmap_headerr;   r?   rN   _read_cmap_encoding_recordrA   rB   rC   rl   rn   _read_cmap_format_headerformat_get_character_map_format4)rR   r   recordsrt   rN   format_headers         rW   r   zTruetypeInfo.get_character_mapT  s      	'&& T\&-A-HII,224:37<3G3NQUQZ3Z\`\km m  	 	F!Q&&6+=+B+Bf-4v}D 8V L L '1,,77?? 'E""rY   c           	         t          | j        |          }|j        dz  }t          j        d| d          }|                     d| d||j        z             }|                     d| d||j        z   |z   dz             }|                     d| d||j        z   |z   dz   |z             }||j        z   |z   dz   |z   |z   }|                     d| d|          }	i }
t          d|          D ]}|	|         dk    r|	|         dk    rt          ||         ||         dz             D ]q}|	|         d|||         z
  z  z   |z   d|z  z   }t          j        d| j        ||dz                      d         }|dk    r|||         z   dz  |
t          |          <   rt          ||         ||         dz             D ](}|||         z   dz  }|dk    r||
t          |          <   )|
S )	Nr	   >Hr   i  r   z>Hi   )
_read_cmap_format4Headerr;   seg_count_x2structcalcsize_read_arrayrB   r   unpackchr)rR   rN   rO   	seg_count
array_size	end_countstart_countid_deltaid_range_offset_addressid_range_offsetcharacter_mapr   caddrgs                  rW   r   z'TruetypeInfo._get_character_map_format4l  s{    *$*f=='1,	_%5%5%5%566
$$%5%5%5%5%+fk%9; ;	&&'79'7'7'7'-';j'H1'LN N##$4	$4$4$4$*V[$8:$E$IJ$VX X V[ :-1J>K 	 **+;y+;+;+;+BD Dq)$$ 	2 	2Aq!Q&&"1%..{1~y|a/?@@ J JA*1-QQ5G0HH2356U;DdDJtD1H},EFFqIAAvv12Xa[E0Ic!ff-J {1~y|a/?@@ 2 2AXa[E1AAvv01c!ff-2 rY   c                 t    t          j        |          }t          j        || j        |||z                      S r   )r   r   r   r;   )rR   r   rN   rB   s       rW   r   zTruetypeInfo._read_array  s3    v&&}VTZv}0D%EFFFrY   c                 x    | j                                          t          j        | j                   d| _        dS )zClose the font file.

        This is a good idea, since the entire file is memory mapped in
        until this method is called.  After closing cannot rely on the
        ``get_*`` methods.
        TN)r;   closer2   r7   r=   rc   s    rW   r   zTruetypeInfo.close  s5     	

rY   c                 @    | j         s|                                  d S d S r   )r=   r   rc   s    rW   __del__zTruetypeInfo.__del__  s'    | 	JJLLLLL	 	rY   )NN)__name__
__module____qualname____doc__r{   r}   r~   _macintosh_encoding_lookuprX   r^   rd   rf   rw   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   rY   rW   r   r      su       	 	Q! 	Q 	a	
 	 	1 	a 	Q 	 	A 	r 	b 	 	2 	r  	B!" 	r#$ '  O.   " " 	
;"%b %b %bN* * *< < <; ; ;  4P P P Pd( ( (( ( ("O O O( ( ((  0
V 
V 
V  # # #0! ! !FG G G
 
 
    rY   r   c                      dg | D ]4}|                     d          \  }}                    |           |z  5 G fdd          S )zM Generic table constructor used for table formats listed at
     end of file.r   :c                   \    e Zd Z ej                  ZfdZd Ze fd            Z	dS )_read_table.<locals>.TableClassc                 &   t          j        |||| j        z                      }t          t	          |                    | _        | j        D ]@\  }}t          |t                    r|                    d          }t          | ||           Ad S )Nzutf-8)
r   r   rB   listzippairs
isinstancebytesdecodesetattr)rR   datarN   r   pnamepvaluefmtr   s         rW   rX   z(_read_table.<locals>.TableClass.__init__  s    M#tF6DI3E,E'FGGEc%//00DJ!% - -vfe,, 4#]]733FeV,,,,- -rY   c                 V    dd                     d | j        D                       z   dz   S )N{z, c                 "    g | ]\  }}| d | S )z = r   )r   r   r   s      rW   r   z<_read_table.<locals>.TableClass.__repr__.<locals>.<listcomp>  s*    !X!X!XME6U"7"7v"7"7!X!X!XrY   })joinr   rc   s    rW   __repr__z(_read_table.<locals>.TableClass.__repr__  s0    tyy!X!XTZ!X!X!XYYYZ]]]rY   c                     g }t          |          D ]+}|                     | |                     |j        z  },|S r   )r   rp   rB   )r   rN   rj   tablesr   
TableClasss        rW   rA   z%_read_table.<locals>.TableClass.array  sN    F5\\ * *jjv66777*/)MrY   N)
r   r   r   r   r   rB   rX   r   staticmethodrA   )r  r   r   s   rW   r  r     s{        vs##	- 	- 	- 	- 	- 	-	^ 	^ 	^ 
	 	 	 	 
	 	 	rY   r  )splitrp   )entriesentryr   
entry_typer  r   r   s       @@@rW   _read_tabler    s     CE   ;;s++jTz           , rY   zscalertype:Iznum_tables:Hzsearch_range:Hzentry_selector:Hzrange_shift:Hztag:4szcheck_sum:Izoffset:Izlength:Iz	version:izfont_revision:izcheck_sum_adjustment:Lzmagic_number:Lzflags:Hzunits_per_em:Hz	created:Qz
modified:Qzx_min:hzy_min:hzx_max:hzy_max:hzmac_style:Hzlowest_rec_p_pEM:Hzfont_direction_hint:hzindex_to_loc_format:hzglyph_data_format:h).	version:Hzx_avg_char_width:hzus_weight_class:Hzus_width_class:Hz	fs_type:Hzy_subscript_x_size:hzy_subscript_y_size:hzy_subscript_x_offset:hzy_subscript_y_offset:hzy_superscript_x_size:hzy_superscript_y_size:hzy_superscript_x_offset:hzy_superscript_y_offset:hzy_strikeout_size:hzy_strikeout_position:hzs_family_class:hz	panose1:Bz	panose2:Bz	panose3:Bz	panose4:Bz	panose5:Bz	panose6:Bz	panose7:Bz	panose8:Bz	panose9:Bz
panose10:Bzul_unicode_range1:Lzul_unicode_range2:Lzul_unicode_range3:Lzul_unicode_range4:Lzach_vend_id:Izfs_selection:Hzus_first_char_index:Hzus_last_char_index:Hzs_typo_ascender:hzs_typo_descender:hzs_typo_line_gap:hzus_win_ascent:Hzus_win_descent:Hzul_code_page_range1:Lzul_code_page_range2:Lzsx_height:hzs_cap_height:hzus_default_char:Hzus_break_char:Hzus_max_context:Hzversion_num:Hz
n_tables:Hr	  zlength:Hz
coverage:Hr   r	   r   r      z	n_pairs:Hzleft:Hzright:Hzvalue:hzplatform_id:Hzencoding_id:Hzoffset:Lzformat:Hz
language:Hzseg_count_x2:Hz	Advance:hzDescender:hz	LineGap:hzadvance_width_max:Hzmin_left_side_bearing:hzmin_right_side_bearing:hzx_max_extent:hzcaret_slope_rise:hzcaret_slope_run:hzcaret_offset:hzreserved1:hzreserved2:hzreserved3:hzreserved4:hzmetric_data_format:hznumber_of_h_metrics:Hzadvance_width:Hzlsb:hzcount:Hzstring_offset:Hzlanguage_id:Hz	name_id:Hzoffset:H)r   r2   r9   r   r   r   r  r>   r@   rM   r[   r   r   r   r   r   override_maskr   r   r   r   r   r   r   rP   r   rh   ri   r   rY   rW   <module>r     s  	 	 
			   O O O O O O O Od! ! !L ![!/!1!3!0	2 2  *k(*7*4*46 6 
 ;{07/(/*+((((,3664!6 6 $ + -2 -2^ &+o&24 4  )[)3)57 7  .1  **-  '03  -+.  ()-  &)k+*:*<*9; ;  #.+h.7.7#9 #9   K .0 0  )[)8)35 5  ';z'13 3 &;z'1'3'7'7'9'68 8  &+k&1&3&1&;&?&@&6&:&9&6&3&3&3&3&<&=!? ? $ $$5$+- -  ![!*!24 4   K / / + * *, ,   rY   