
    ^j^                       d Z ddlmZ ddlZddlZddlZddlZddlmZ  G d d          Z	dPd	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
d0d1d2          Z e
d'd          Z e
d3d4d5          Z e
d6d(          Z e
d6d(d7d8ddd          Z e
dd9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdH          Z e
dIdJ          Z e
d6dKdL          Z e
d3d4dMdNd(dO          ZdS )Qz
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
    )annotationsN)Anyc                  N   e Zd ZU 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.ed/<   dWd4Z	dXd6Z
dYd8ZdYd9ZdZd;Z	 	 d[d\dBZd]dDZd^dEZd_dGZd`dIZd`dJZdK ZdadMZdbdOZdcdQZdddTZdedUZdedVZd<S )f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_romandict[str, float] | None_character_advancesfilenamestrreturnNonec                \   |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 )a#  Read the given TrueType file.

        Args:
            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mmapr7   	PROT_READ_dataACCESS_READ_closed_read_offset_table_tables_read_table_directory_entryarraysize
num_tablestag_names_horizontal_metricsr1   _character_kernings_glyph_kernings_character_map
_glyph_map_font_selection_flags_read_head_tableoffsetheader_read_horizontal_headerhorizontal_header)selfr2   lengthoffsetstables        J/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/font/ttf.py__init__zTruetypeInfo.__init__L   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    intc                    | j         s1t          | j        | j        d         j                  }|j        | _         | j         S )z9Return the font selection flags, as defined in OS/2 tablezOS/2)rS   _read_OS2_tablerC   rG   rU   fs_selection)rY   	OS2_tables     r]   get_font_selection_flagsz%TruetypeInfo.get_font_selection_flagsr   s=    ) 	@'
DL4H4OPPI)2)?D&))r_   boolc                J    t          |                                 dz            S )z3Returns True iff the font describes itself as bold.    rf   re   rY   s    r]   is_boldzTruetypeInfo.is_boldy   s!    D1133d:;;;r_   c                J    t          |                                 dz            S )z5Returns True iff the font describes itself as italic.r	   ri   rj   s    r]   	is_italiczTruetypeInfo.is_italic}   s!    D1133c9:::r_   +dict[tuple[int, int], tuple[int, int, str]]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   )rM   _read_naming_tablerC   rG   rU   _read_name_recordrI   rJ   countstring_offsetrZ   platform_idname_idencoding_idlanguage_idappend)rY   naming_tablename_recordsstoragerecordvaluekeys          r]   	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****{r_   N	int | strplatformint | str | None	languages
str | Nonec                   |                                  }t          |t                    r| j        |         }|s"dD ]}|                     |||          }|r|c S  t          |t                    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 )	a^  Returns the value of the given name in this font.

        Args:
            `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   )
r   
isinstancer3   _name_id_lookupget_name_platform_id_lookup_microsoft_encoding_lookup(_TruetypeInfo__macintosh_encoding_lookupcodecs
getdecoder)	rY   r   r   r   namesr}   	encodingsr|   decoders	            r]   r   zTruetypeInfo.get_name   s[   j   dC   	.'-D 	!6 ! !dHi@@ ! LLL!h$$ 	:/9Hd5((4q==7I JI	]]8I ! 	Xt,- 	- 	-FayI%%&)y*@*@ +IfQi,@AAwvay))!,,,,tr_   listc                    | j         sBt                              | j        | j        d         j        | j        j                  }|| _         | j         S )z5Return all horizontal metric entries in table format.hmtx)rN   _read_long_hor_metricrI   rC   rG   rU   rX   number_of_h_metrics)rY   ars     r]   get_horizontal_metricsz#TruetypeInfo.get_horizontal_metrics   sQ    ' 	*&,,TZ-1\&-A-H-1-C-WY YB (*D$''r_   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.
        )r1   get_glyph_advancesget_glyph_maprangelen)rY   gagmapis       r]   get_character_advancesz#TruetypeInfo.get_character_advances   s     # 	,++$$&&!!###% s2ww 	: 	:ADyyT!WD,DDD46qE(a1''r_   list[float]c                H                                       } fd|D             S )zReturn a list of advances.c                R    g | ]#}t          |j                  j        j        z  $S  )floatadvance_widthrV   units_per_em).0mrY   s     r]   
<listcomp>z3TruetypeInfo.get_glyph_advances.<locals>.<listcomp>  s-    NNNaao&&)AANNNr_   )r   )rY   hms   ` r]   r   zTruetypeInfo.get_glyph_advances  s.    ((**NNNN2NNNNr_   dict[tuple[str, str], int]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)rO   r   get_glyph_kerningsitems)	rY   r   kernspairr}   lglyphrglyphlcharrchars	            r]   get_character_kerningsz#TruetypeInfo.get_character_kernings  s     ' 		E%%''D++--E')D$${{}} E Ee!%$74<?4$74<?4 EU E?DD,eU^<''r_   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   )rP   _read_kern_header_tablerC   rG   rU   rJ   r   n_tables_read_kern_subtable_headercoveragehorizontal_maskminimum_maskperpendicular_maskformat_mask_add_kernings_format0rZ   )rY   rV   rU   kerningsr   s        r]   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'r_   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_format0rC   _read_kern_subtable_format0PairrI   rJ   n_pairsleftrightr}   r   rV   r   )rY   r   rU   rV   kerning_pairsr   s         r]   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r_   dict[int, str]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.
        )rR   get_character_mapr   )rY   cmapchglyphs       r]   r   zTruetypeInfo.get_glyph_mapE  sk     ? 	#?"%%'' 	, 	,IBDO++)+&r_   dict[str, int]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   )rQ   _read_cmap_headerrC   rG   rU   _read_cmap_encoding_recordrI   rJ   rK   rt   rv   _read_cmap_format_headerformat_get_character_map_format4)rY   r   recordsr|   rU   format_headers         r]   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""r_   rU   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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_format4HeaderrC   seg_count_x2structcalcsize_read_arrayrJ   r   unpackchr)rY   rU   rV   	seg_count
array_size	end_countstart_countid_deltaid_range_offset_addressid_range_offsetcharacter_mapr   caddrgs                  r]   r   z'TruetypeInfo._get_character_map_format4k  sy    *$*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y!! 	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 r_   fmttuple[Any, ...]c                t    t          j        |          }t          j        || j        |||z                      S r   )r   r   r   rC   )rY   r   rU   rJ   s       r]   r   zTruetypeInfo._read_array  s3    s##}S$*VFTM-A"BCCCr_   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)rC   closer:   r?   rE   rj   s    r]   r   zTruetypeInfo.close  s5     	

r_   c                @    | j         s|                                  d S d S r   )rE   r   rj   s    r]   __del__zTruetypeInfo.__del__  s'    | 	JJLLLLL	 	r_   )r2   r3   r4   r5   )r4   r`   )r4   rf   )r4   rn   )NN)r   r   r   r   r   r   r4   r   )r4   r   )r4   r0   )r4   r   )r4   r   )r4   r   )r4   r   )rU   r`   r4   r   )r   r3   rU   r`   r4   r   )r4   r5   )__name__
__module____qualname____doc__r   r   r   _macintosh_encoding_lookup__annotations__r^   re   rk   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r_   r]   r   r      s        	 	Q! 	Q 	a	
 	 	1 	a 	Q 	 	A 	r 	b 	 	2 	r  	B!" 	r#$ '  O.   " " 	
;" 1000$b $b $b $bL* * * *< < < <; ; ; ;   4 FJ/3Q Q Q Q Qf( ( ( (( ( ( ("O O O O
( ( ( ((   0
V 
V 
V   # # # #.! ! ! !FD D D D
 
 
 
     r_   r   entriesr3   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                  b    e Zd Z ej                  ZdfdZdd	Zed fd            Z	dS )_read_table.<locals>.TableClassdata	mmap.mmaprU   r`   r4   r5   c                &   t          j        |||| j        z                      }t          t	          |                    | _        | j        D ]@\  }}t          |t                    r|                    d          }t          | ||           Ad S )Nzutf-8)
r   r   rJ   r   zippairsr   bytesdecodesetattr)rY   r  rU   r   pnamepvaluer   r   s         r]   r^   z(_read_table.<locals>.TableClass.__init__  s    M#tF6DI3E,E'FGGEc%//00DJ!% - -vfe,, 4#]]733FeV,,,,- -r_   r3   c                V    dd                     d | j        D                       z   dz   S )N{z, c                "    g | ]\  }}| d | S )z = r   )r   r	  r
  s      r]   r   z<_read_table.<locals>.TableClass.__repr__.<locals>.<listcomp>  s*    #Z#Z#ZmeVu$9$9$9$9#Z#Z#Zr_   })joinr  rj   s    r]   __repr__z(_read_table.<locals>.TableClass.__repr__  s0    #Z#Ztz#Z#Z#Z[[[^aaar_   rr   list[TableClass]c                    g }t          |          D ]+}|                     | |                     |j        z  },|S r   )r   rx   rJ   )r  rU   rr   tablesr   
TableClasss        r]   rI   z%_read_table.<locals>.TableClass.array  sN    F5\\ * *jjv66777*/)Mr_   N)r  r  rU   r`   r4   r5   )r4   r3   )r  r  rU   r`   rr   r`   r4   r  )
r   r   r   r   r   rJ   r^   r  staticmethodrI   )r  r   r   s   r]   r  r     s        vs##	- 	- 	- 	- 	- 	- 	-	b 	b 	b 	b 
	 	 	 	 	 
	 	 	r_   r  )splitrx   )r   entryr   
entry_typer  r   r   s       @@@r]   _read_tabler    s     CE   ;;s++jTz           , r_   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   r3   ) r   
__future__r   r   rA   r:   r   typingr   r   r  rF   rH   rT   rb   r   r   r   r   r   override_maskr   r   r   r   r   r   r   rW   r   rp   rq   r   r_   r]   <module>r     s  	 	 # " " " " "   				       L L L L L L L L^! ! ! !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 / / + * *, ,   r_   