
    ^jf                    P   d dl mZ d dlZd dlZd dlmZmZmZmZm	Z	m
Z
mZ d dlmZ d dlZd dlmZ d dlmZmZ d dlmZmZmZmZmZ d dlmZmZmZ ej        Zej         Z ej!        Z!d	Z"d
Z#dZ$dZ%dZ&dZ'dZ(dZ)dZ*i de(de%de%de#de"de"de$de$de%de%de&de'de'de(de(d e(d!e*e)e*d"Z+d#d#d#dd$d%d&ddd'd(ddd)Z, e            r4	 d d*l-m.Z.m/Z/m0Z0m1Z1 d6d-Z2 e/e2          Z3e0d7d1            Z4 e	de/          Z5 G d2 d3ej6                  Z7 G d4 d5ej8                  Z9dS )8    )annotationsN)byrefc_int32c_void_p	string_atcastc_char_pc_float)BinaryIO)base)GlyphGlyphPosition)CGFloatcocoapykCTFontURLAttributecfnumber_to_numberkCTFontWeightTrait)harfbuzz_availableget_resource_from_ct_fontget_harfbuzz_shaped_glyphsgg333333gٿ        gq=
ףp?g333333?g?gQ?gףp=
?TFthin
extralight
ultralightlight	semilightnormalregularmediumdemiboldsemiboldbold	extrabold	ultraboldblack)heavy
extrablackg      ?g333333ӿgɿgg?g?)NF	undefinedultracondensedextracondensed	condensedsemicondensedr   r   semiexpandedexpandedextraexpandedultraexpanded)hb_libhb_destroy_func_thb_reference_table_func_tHB_MEMORY_MODE_READONLY	user_datar   c                B    | rt                               |            dS dS )z2Release the table resources once harfbuzz is done.N)cf	CFRelease)r5   s    M/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/font/quartz.pypy_coretext_table_data_destroyr:   P   s,     	$LL#####	$ 	$    facetagintc                V   t                               ||          }|dS t                              |          }t                              |          }|st                              |           dS t          |t                    }t          j	        ||t          |t                    }|S )zuThis callback is invoked by HarfBuzz for each table it needs.

        user_data is a pointer to the CGFont.
        N)quartzCGFontCopyTableForTagr7   CFDataGetLengthCFDataGetBytePtrr8   r   r	   r1   hb_blob_creater4    py_coretext_table_data_destroy_c)r<   r=   r5   
table_datalengthdata_ptrdata_ptr_charblobs           r9   py_coretext_table_callbackrK   W   s     11)SAA
4 ##J//&&z22 	LL$$$4 Xx00$]F<S%/1QS Sr;   c                  <     e Zd ZU ded<   d fdZddZddZ xZS )QuartzGlyphRenderer
QuartzFontfontreturnNonec                X    t                                          |           || _        d S N)super__init__rO   )selfrO   	__class__s     r9   rU   zQuartzGlyphRenderer.__init__v   s&    			r;   glyph_indexr>   c                   | j         j        }d}t          j        |z  |          }t                              |d|d |          }t                              |d|d |          }t          t          t          j
        |j        j                  dz             d          }t          t          t          j
        |j        j                  dz             d          }t          t          j        |j        j                             dz   }	t          t          j
        |j        j                            dz
  }
t          t%          |                    }d}d|z  }t'          t(                                                    }t'          t(                              d |||||t          j                            }t(                              |t2          j        j                   t(                              ||
 |	           t(                              |dddd           t(                              ||           t(                              || j         j                    t(          !                    |d|           t(          "                    |dd           t          j#        dz  t          j#        dd          g }t(          $                    |||d|           t'          t(          %                    |                    }t(          &                    |          }t'          t(          '                    |                    }t'          t(          (                    |                    }tR          *                    |          }tR          +                    |          }|rtY          ||          }t(          -                    |           t(          .                    |           tR          /                    |           tR          /                    |           t2          j0        1                    ||d||          }| j         2                    |          }|3                    |	|
|           |S ti          d          )	N   r            RGBA"CG Image buffer could not be read.)5rO   ctFontr   CGGlyphctCTFontGetBoundingRectsForGlyphsCTFontGetAdvancesForGlyphsmaxr>   mathceilsizewidthheightfloororiginyxroundr   r@   CGColorSpaceCreateDeviceRGBCGBitmapContextCreatekCGImageAlphaPremultipliedLastCGContextSetShouldAntialiaspygletoptionstext_antialiasingCGContextSetTextPositionCGContextSetRGBFillColorCGContextSetFontCGContextSetFontSize
pixel_sizeCGContextTranslateCTMCGContextScaleCTMCGPointCTFontDrawGlyphsCGBitmapContextCreateImageCGImageGetBytesPerRowCGImageGetDataProviderCGDataProviderCopyDatar7   rB   rC   r   CGImageReleaseCGDataProviderReleaser8   image	ImageDatacreate_glyphset_bearings	Exception)rV   rX   ra   countglyphsrectadvancerj   rk   baselinelsbbits_per_componentsbytes_per_row
colorSpacebitmap_context	positions	image_refdata_provider
image_databuffer_size
buffer_ptrbufferglyph_imageglyphs                           r9   render_indexz QuartzGlyphRenderer.render_indexz   s   !
 /E);7711&!VT5QQ //64OO C	$)/22Q677;;S49#344q8991==
4;=11222Q6$)DKM**++a/eGnn%%  E	f@@BBCC
!&">">2#4 #4 5 5 	**>6>;[\\\''hGGG''1aCCC777##NDI4HIII$$^Q???  B777_q(GOAq,A,A+BC		1nMMM V>>~NNOO	44Y?? !>!>y!I!IJJf;;MJJKK
((44((44
 	z;77F!!),,,((444LL(((LL$$$ ,00P]^^KI**;77Exg666L<===r;   textstr
base.Glyphc                   | j         j        }t          t                              d dt          j        t          j                            }t                              |t          j	        |           t                              |t          j
        t          j                   t          j        |          }t          t                              d ||                    }t          t                              |                    }t                              |           t                              |           t                              |           t#          |          }t          j        |z  t'          t)          t*          t-          |                               }t          j        |z              }	t                              |||	|           |	d         dk    rt3                      t3                      }}
t5          t                              |t9          |
          t9          |          d                     x}}t5          |
j        |j        z             }d}|j        }n-t                              |d|	d |          }t                              |d|	d |          }tA          t5          tC          j"        |j#        j$                  dz             d          }tA          t5          tC          j"        |j#        j%                  dz             d          }t5          tC          j&        |j'        j(                             dz   }t5          tC          j"        |j'        j)                            dz
  }t5          tU          |                    }d}d|z  }t          tV          ,                                          }t          tV          -                    d |||||t          j.                            }tV          /                    |t`          j1        j2                   tV          3                    || |           tV          4                    |dddd           tV          5                    |d|           tV          6                    |dd           t          7                    ||           t                              |           t          tV          8                    |                    }tV          9                    |          }t          tV          :                    |                    }t          tV          ;                    |                    }t          <                    |          }t          =                    |          }|rt}          ||          }tV          ?                    |           tV          @                    |           t                              |           t                              |           t`          jA        B                    ||d||          }| j         C                    |          }|D                    |||           |S t          d          )	Nr[   r   rZ   r\   r]   r^   r_   r`   )FrO   ra   r   r7   CFDictionaryCreateMutablekCFTypeDictionaryKeyCallBackskCFTypeDictionaryValueCallBacksCFDictionaryAddValuer   kCTFontAttributeName*kCTForegroundColorFromContextAttributeNamekCFBooleanTrueCFSTRCFAttributedStringCreaterc    CTLineCreateWithAttributedStringr8   lenUniCharlistmapordr   rb   CTFontGetGlyphsForCharactersr   r>   CTLineGetTypographicBoundsr   valuerd   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   r@   rq   rr   rs   rt   ru   rv   rw   rx   ry   r}   r~   
CTLineDrawr   r   r   r   rB   rC   r   r   r   r   r   r   r   r   )rV   r   ra   
attributescf_strstringliner   charsr   ascentdescentr   rj   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                                 r9   renderzQuartzGlyphRenderer.render   s    ! ((q"2RTVTvww
 

 	
G,H&QQQ

G,^ahawxxxt$$"55dFJOOPP ;;FCCDD
V
Z   
V D		5(4CT0C0C+D+DE/E),,
''vuEEE !9>> &iiGF!""?"?eFmmUZ[bUcUcei"j"jkkkGe566FC}HH 55fauUUD 33FAvtUSSG DIdio66:;;Q??ETYty'7881<==qAAFDJt{}55666:Hdi..//!3C%..))G  E	f@@BBCC
!&">">2#4 #4 5 5 	**>6>;[\\\''hGGG''1aCCC$$^Q???  B777
dN+++
TV>>~NNOO	44Y?? !>!>y!I!IJJf;;MJJKK
((44((44
 	z;77F!!),,,((444LL(((LL$$$ ,00P]^^KI**;77Exg666L<===r;   )rO   rN   rP   rQ   )rX   r>   )r   r   rP   r   )__name__
__module____qualname____annotations__rU   r   r   __classcell__rW   s   @r9   rM   rM   s   s              I> I> I> I>Vg> g> g> g> g> g> g> g>r;   rM   c                       e Zd ZU eZded<   i Zded<   	 	 d4d5 fdZd Zd6dZ	e
d7d            Ze
d7d            Zd8dZd9dZ	 	 d:d;d#Zed<d&            Zed=d)            Zd>d,Zd- Zd. Zd?d1Zd@d3Z xZS )ArN   ztype[base.GlyphRenderer]glyph_renderer_classz,dict[str, dict[int, tuple[c_void_p, bytes]]]_loaded_CGFont_tabler   FNnamer   ri   floatweightitalicboolstretchdpi
int | NonerP   rQ   c                   t                                                       |pd}|pd| _        || _        ||z  dz  | _        || _        || _        || _        t          |t                    rt          |         | _        n|du rt          d         | _        nd | _        || _        d}|r|t          j        z  }t          |t                    rt          |         | _        nd | _        t          |          }|| _        |                     ||          }|r=|d         }	t%          t&                              |	| j        d d                     | _        n|                     ||| j        | j                  }
t%          t&                              |
| j        d                     | _        t0                              |
           | j        sJ d|z               t%          t&                              | j                            }t          t          j        |                    | _        t0                              |           t;          t=          j        t&                               | j                                      | _!        t;          t=          j        t&          "                    | j                                       | _#        tH          j%        j&        dk    r+tO                      rd | _(        tS          |           | _*        d S d S d S )	N	Helvetica`   g      R@Tr"   r   zCouldn't load font: harfbuzz)+rT   rU   r   ri   r|   r   r   r   
isinstancer   name_to_weightweight_valuer   kCTFontItalicTraitname_to_stretchstretch_valuetraits#_lookup_font_with_family_and_traitsr   rc   CTFontCreateWithGraphicsFontra   _create_font_descriptorCTFontCreateWithFontDescriptorr7   r8   CTFontCopyFamilyNamecfstring_to_string_family_namer>   rg   rh   CTFontGetAscentr   CTFontGetDescentr   ru   rv   text_shapingr   _cg_fontr   hb_resource)rV   r   ri   r   r   r   r   r   resultcgFont
descriptorr   rW   s               r9   rU   zQuartzFont.__init__3  s    	"{9"	*t+fc"" 	% .v 6Dt^^ .v 6D $D  	1g00Fgs## 	&!0!9D!%D4yy99$GG 		>AYF"2#B#B64?\`bf#g#ghhDKK 55dFDDUW[WijjJ"2#D#DZQUQ`bf#g#ghhDKLL$$$;== 6 ===;"11$+>>?? :6 B BCC
V$)B$6$6t{$C$CDDEEDIb&9&9$+&F&FGGHHH>&*449K9M9M4 DM8>>D 5444r;   c                    | j         J t                              | j        d           | _         | j         t	          d          t          j        t          | j         t                    S )Nz Could not get CGFont from CTFont)	r   r@   CTFontCopyGraphicsFontra   
ValueErrorr1   hb_face_create_for_tablesrK   _destroy_callback_nullrV   s    r9   _get_hb_facezQuartzFont._get_hb_faceo  s]    }$$$ 55dk4HH= ?@@@ /0JDM[qrrrr;   bytesc                   | j         }|dk    r7|                     | j        | j                  }|r|\  }}nKt	          d          t          |d          5 }|                                }ddd           n# 1 swxY w Y   |S )ab  Get the font file in bytes if possible.

        Unfortunately CoreText doesn't have a good way to retrieve directly from a font object. Attempt to get the
        filename from the system. If the filename is unknown, it most likely was loaded from memory. In which case,
        the data was added and cached at some point with `add_font_data`.
        UnknownzOCouldn't load font data by name and traits. Report as a bug with the font file.rbN)filenamer   r   r   r   openread)rV   r   r   _datafs         r9   get_font_datazQuartzFont.get_font_dataz  s     =y  ==diUUF s 44 qrrrh%%  vvxx                              s   A66A:=A:c                   |                      | j        | j        | j        | j                  }t          t                              |t                              }|rpt          j
        |          }|                                                                }t                              |           t                              |           |S t                              |           dS )Nr   )r   r   r   r   r   r   rc   CTFontDescriptorCopyAttributer   r   ObjCInstancefileSystemRepresentationdecoder7   r8   )rV   r   refurlfilepaths        r9   r   zQuartzFont.filename  s    11$)T[$J[]a]opp
r77
DWXXYY 	&s++C3355<<>>HLLLL$$$O
Z   yr;   c                    | j         S rS   )r   r   s    r9   r   zQuartzFont.name  s      r;   c                D    t                               | j                   d S rS   )r7   r8   ra   r   s    r9   __del__zQuartzFont.__del__  s    
T[!!!!!r;   familyr   r>   tuple[c_void_p, bytes] | Nonec                    || j         vrd S | j         |         }|sd S ||v r||         S |                                D ]\  }}||z  r|c S d|v r|d         S t          |                                          d         S Nr   )r   itemsr   values)rV   r  r   fontstr   s         r9   r   z.QuartzFont._lookup_font_with_family_and_traits  s     2224)&1 	4U??= kkmm 	 	FQz  ::8OELLNN##A&&r;   family_namefloat | Noner   c           	        t          t                              d dt          j        t          j                            }t          j        |          }t                              |t
          j        |           t          	                    |           t          |          }t          t                              d t
          j        t          |                              }|rt          t                              d dt          j        t          j                            }	|	r|t          |          }
t          t                              d t
          j        t          |
                              }|r@t                              |	t
          j        |           t          	                    |           |t          |          }t          t                              d t
          j        t          |                              }|r@t                              |	t
          j        |           t          	                    |           t                              |	t
          j        |           t                              |t
          j        |	           t          	                    |	           t          	                    |           t          t(                              |                    }t          	                    |           |S r
  )r   r7   r   r   r   r   r   r   kCTFontFamilyNameAttributer8   r   CFNumberCreatekCFNumberSInt32Typer   r
   kCFNumberFloatTyper   kCTFontWidthTraitkCTFontSymbolicTraitkCTFontTraitsAttributerc   $CTFontDescriptorCreateWithAttributes)rV   r  r   r   r   r   cfnameitraits	symTraits
traitsDictr   cfWeightr   cfWidthr   s                  r9   r   z"QuartzFont._create_font_descriptor  sh    ((q"2RTVTvwwy y
 {++

G,NPVWWW
V &//R..tW5PRWX_R`R`aabb	 	$!"">">tQHh?A?a#c #c d dJ )%#*6??L'(9(9$@Z\abn\o\o(p(pqqH ///
G<VX`aaaX...&$+G$4$4M&r'8'8w?Y[`an[o[o'p'pqqG .//
G<UW^___W--- ''
G4PR[\\\''
G4RT^___Z(((LL###bEEjQQRR

Z   r;   clstype[QuartzFont]c                F   t          |          }|| j        v rdS t                              t	          j        |          d          }t                              |d           }|rdnd}|rt                              |           |rt                              |           |S )NTr   F)	r   r   rc   %CTFontDescriptorCreateWithNameAndSizer   r   ,CTFontDescriptorCreateMatchingFontDescriptorr7   r8   )r   r   r   matchedexistss        r9   	have_fontzQuartzFont.have_font  s    4yy3+++4 ==gmD>Q>QSVWW
AA*dSS +e 	%LL$$$ 	"LL!!!r;   r   r   c           	     N   t          t                              d |t          |                              }t          t                              |                    }t          t                              |                    }t                              |           t                              |           t          t          
                    |dd d                     }t          t                              |                    }t          t          j        |                    }t                              |           t          t                              |                    }t          t          j        |                    }t                              |           t                              |          }	t                              |           || j        vr
i | j        |<   ||f| j        |         |	<   || j        vr
i | j        |<   ||f| j        |         |	<   d S NrZ   )r   r7   CFDataCreater   r@   CGDataProviderCreateWithCFDataCGFontCreateWithDataProviderr8   r   rc   r   r   r   r   r   CTFontCopyFullNameCTFontGetSymbolicTraitsr   )
r   r   dataRefproviderr   ra   r   
familyNamefullNamer   s
             r9   add_font_datazQuartzFont.add_font_data  s    2??4s4yyAABBFAA'JJKK&==hGGHH
W$$X... "99&!T4PPQQ "11&99::3F;;<<

V"//7788w1&99::
V++F33
V S55535C$Z08>~ ,V4333313C$X.6<d^ *6222r;   r   tuple[int, int]c                p   | j         }t          t                              d dt          j        t          j                            }t                              |t          j        |           t          j	        |          }t          t          
                    d ||                    }t          t                              |                    }t                      t                      }}t                              |t          |          t          |          d           }	|j        |j        z   }
t                              |           t                              |           t                              |           t                              |           t%          |	          t%          |
          fS r)  )ra   r   r7   r   r   r   r   r   r   r   r   rc   r   r   r   r   r   r8   rp   )rV   r   ra   r   r   r   r   r   r   rj   rk   s              r9   get_text_sizezQuartzFont.get_text_size'  s@   ((q"2RTVTvww
 

 	
G,H&QQQt$$"55dFJOOPP;;FCCDD!))WYY--dE&MM5>>SWXX-
V
Z   
T
VU||U6]]**r;   c                    t                               | j                  }t          t	          t
                              |t                                        }t
                              |           |S rS   )	rc   CTFontCopyTraitsra   r   r   r7   CFDictionaryGetValuer   r8   )rV   r   font_weights      r9   _get_font_weightzQuartzFont._get_font_weight=  sU    $$T[11("2I2I&Rd2e2e)f)fgg
Vr;   c                    t                               | j                  }t          t	          t
                              |t          j                                      }t
          	                    |           |S rS   )
rc   r8  ra   r   r   r7   r9  r   r  r8   )rV   r   
font_widths      r9   _get_font_stretchzQuartzFont._get_font_stretchC  sX    $$T[11'1H1HQXQj1k1k(l(lmm

Vr;   indices	list[int]c                    |                                   t                      }t          |          D ] }|| j        vr|                    |           !|D ]$}| j                            |          | j        |<   %d S rS   )_initialize_renderersetr   add_glyph_rendererr   )rV   r?  missingglyph_indices       r9   render_glyph_indiceszQuartzFont.render_glyph_indicesI  s    !!###%%LL 	* 	*L4;..L))) $ 	X 	XL(,(<(I(I,(W(WDK%%	X 	Xr;   'tuple[list[Glyph], list[GlyphPosition]]c           
        |                                   t          j        j        dk    rt	                      rt          | |          S g }g }t          j        t          |                    D ]}|dk    rd}|| j	        vr"| j
                            |          | j	        |<   |                    | j	        |                    |                    t          j        dddd                     t          |          dk    rft          t          |          dz
            D ]F}|                    | j                   |                    t          j        dddd                     G||fS )zCreate and return a list of Glyphs for `text`.

        If any characters do not have a known glyph representation in this
        font, a substitution will be made.

        Args:
            text:
                Text to render.
        r   	 r   rZ   )rB  ru   rv   r   r   r   r   get_grapheme_clustersr   r   rE  r   appendr   r   range_zero_glyph)rV   r   r   offsetscis         r9   
get_glyphszQuartzFont.get_glyphsV  sZ    	!!###>&*449K9M9M4-dD999+CII66 	C 	CADyy##!%!5!<!<Q!?!?AMM$+a.)))NN4-aAq99::: 1vvzzs1vvz** C CAMM$"2333NN4#5aAq#A#ABBBBwr;   )r   FFN)r   r   ri   r   r   r   r   r   r   r   r   r   rP   rQ   )rP   r   )rP   r   )rP   rQ   )r  r   r   r>   rP   r  )NN)
r  r   r   r>   r   r  r   r  rP   r   )r   r!  r   r   rP   r   )r   r!  r   r   rP   rQ   )r   r   rP   r4  )r?  r@  rP   rQ   )r   r   rP   rI  )r   r   r   rM   r   r   r   rU   r   r   propertyr   r   r  r   r   classmethodr'  r3  r6  r;  r>  rH  rT  r   r   s   @r9   rN   rN   /  s        5HHHHHIKKKKKmr#'9? 9? 9? 9? 9? 9? 9?x	s 	s 	s   (    X ! ! ! X!" " " "' ' ' '8 8<8<+ + + + +Z    [& %D %D %D [%DN+ + + +,    X X X X       r;   rN   )r5   r   )r<   r   r=   r>   r5   r   ):
__future__r   rg   warningsctypesr   r   r   r   r   r	   r
   typingr   pyglet.imageru   pyglet.fontr   pyglet.font.baser   r   pyglet.libs.darwinr   r   r   r   r   pyglet.font.harfbuzzr   r   r   r7   rc   r@   UIFontWeightUltraLightUIFontWeightThinUIFontWeightLightUIFontWeightRegularUIFontWeightMediumUIFontWeightSemiboldUIFontWeightBoldUIFontWeightHeavyUIFontWeightBlackr   r   !pyglet.font.harfbuzz.harfbuzz_libr1   r2   r3   r4   r:   rE   rK   r   GlyphRendererrM   FontrN    r;   r9   <module>rm     s   " " " " " "   O O O O O O O O O O O O O O O O O O                 1 1 1 1 1 1 1 1                       ZZ	         
	 	
 	
 ( (  " ! "   $ $  !  !!" #$ #'  .  "  *; @$ $ $ $
 (9'89W'X'X$   2 "T$(9::y> y> y> y> y>$, y> y> y>xF F F F F F F F F Fr;   