
    ^jS                    <   d Z ddl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	 ddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlmZ ddlZdd	lmZmZmZmZmZ dd
l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,m-Z-m.Z/ ddl0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6 ddl7m8Z8m9Z9 ddl:m;Z; ddl<m=Z= ddlm>Z> ddl?m@Z@  ej                  eB      ZCd;dZDd ZEd ZFd<dZGd<dZHd<dZIeJj                  dddd d!d"      ZLd# ZM G d$ d%      ZNe G d& d'             ZO G d( d)      ZP G d* d+e      ZQ G d, d-      ZRd. ZS G d/ d0      ZT G d1 d2e@j                        ZV G d3 d4e%      ZW G d5 d6      ZX G d7 d8e#      ZYe$ZZe"j                   G d9 d:e"             Z\y)=uO   
A PDF Matplotlib backend.

Author: Jouni K Seppänen <jks@iki.fi> and others.
    N)timezone)datetime)Enum)total_ordering)BytesIO)Image)_api_text_helpers
_type1fontcbookdviread)Gcf)_BackendFigureCanvasBaseFigureManagerBaseGraphicsContextBaseRendererBase)MixedModeRenderer)Figure)FontPathget_fontfontManager)AFM)FT2Font	FaceFlags	LoadFlags
StyleFlags)Affine2DBboxBase)Path)UTC)_path   )_backend_pdf_psc                 "   d}d}g }t        |       D ]H  \  }}t        |      }||z   |k  r	||dz   z  }"|j                  dj                  | ||              |}|}J |j                  dj                  | |d              dj                  |      S )z
    Make one string from sequence of strings, with whitespace in between.

    The whitespace is chosen to form lines of at most *linelen* characters,
    if possible.
    r   r#       N   
)	enumeratelenappendjoin)stringslinelencurrposlastiresultislengths           j/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/matplotlib/backends/backend_pdf.py_fillr5   ^   s     GEF'" 1QVg%vz!GMM$))GE!$456EG MM$))GEFO,-::f    c           
         t        j                  d      }|rDt        j                  t	        |      t
        j                        }|j                  t              }nt        j                         }dt        j                   dd|  dt        j                   |d|}|j                         D ci c]  \  }}|	|| }}}d }d	|_        d
 }d|_        d }	d|	_        |||||||||	d	}
|D ]j  }||
vr&t        j                  d|dt!        |
      d       - |
|   ||         r<t        j                  d| d||   d|
|   j                   d       l d|v rt#        |d         |d<   |S c c}}w )a  
    Create a PDF infoDict based on user-supplied metadata.

    A default ``Creator``, ``Producer``, and ``CreationDate`` are added, though
    the user metadata may override it. The date may be the current time, or a
    time set by the ``SOURCE_DATE_EPOCH`` environment variable.

    Metadata is verified to have the correct keys and their expected types. Any
    unknown keys/types will raise a warning.

    Parameters
    ----------
    backend : str
        The name of the backend to use in the Producer value.

    metadata : dict[str, Union[str, datetime, Name]]
        A dictionary of metadata supplied by the user with information
        following the PDF specification, also defined in
        `~.backend_pdf.PdfPages` below.

        If any value is *None*, then the key will be removed. This can be used
        to remove any pre-defined values.

    Returns
    -------
    dict[str, Union[str, datetime, Name]]
        A validated dictionary of metadata.
    SOURCE_DATE_EPOCH)tzinfozMatplotlib vz, https://matplotlib.orgzMatplotlib z
 backend v)CreatorProducerCreationDatec                 "    t        | t              S N)
isinstancestrxs    r4   is_string_likez-_create_pdf_info_dict.<locals>.is_string_like   s    !S!!r6   zan instance of strc                 "    t        | t              S r>   )r?   r   rA   s    r4   is_datez&_create_pdf_info_dict.<locals>.is_date   s    !X&&r6   z an instance of datetime.datetimec                 F    t        | t              r| j                  dv S | dv S )N)s   Trues   Falses   Unknown)TrueFalseUnknownr?   NamenamerA   s    r4   check_trappedz,_create_pdf_info_dict.<locals>.check_trapped   s(    a66<<<444r6   z#one of {"True", "False", "Unknown"})	TitleAuthorSubjectKeywordsr:   r;   r<   ModDateTrappedzUnknown infodict keyword: z. Must be one of .zBad value for infodict keyword z. Got z which is not rS   )osgetenvr   fromtimestampintr   utcreplacer!   todaympl__version__itemstext_for_warningr	   warn_externalsetrK   )backendmetadatasource_date_epochsource_dateinfokvrC   rE   rM   keywordss              r4   _create_pdf_info_dictrj   t   s   @ 		"56,,S1B-CX\\R!)))5nn& "#//!22JK!'*S__4EF# 	D  $zz|=Vaq}AqD=D="&:N#'AG5
 &KM"   !"!" 
H  CH!;A5 A114X0A D E!T!W%!@ D&&*1g["*1+">">!?q B CC DtI/YKO >s   1
E4<E4c                 2   | j                  d      }| j                         }||j                  }n1t        j                  rt        j
                  }nt        j                  }|dk(  r|dz  }|S |dk  r|d| dz  | dz  fz  z  }|S |d|dz  |dz  fz  z  }|S )zX
    Convert a datetime to a PDF string representing it.

    Used for PDF and PGF.
    zD:%Y%m%d%H%M%Sr   Zz+%02d'%02d'i  z-%02d'%02d')strftime	utcoffsetsecondstimedaylightaltzoner   )drzs      r4   _datetime_to_pdfrv      s     	


#$A	A}II==AAAv	S
 H	 
Q	]rdlaR4K888 H 	
]a4iT222Hr6   c                    t        j                  |       }t        j                  |      }t        j                  |      }| ||z  z   }|||z  z   }| ||z  z   ||z  z   }	|||z  z
  ||z  z   }
| ||z  z   }|||z  z
  }| |f||f|	|
f||ffS )zR
    Calculate the coordinates of rectangle when rotated by angle around x, y
    )mathradianssincos)rB   ywidthheightangle	sin_angle	cos_angleabcrs   efs                r4   !_calculate_quad_point_coordinatesr      s    
 LL% EII	FYA	FYA	EI 22A	EI 22A	EIA	EIAFQFQFQF++r6   c                 6   t        | ||||      }|dz  rdnd}t        d |D              |z
  }t        d |D              |z
  }t        d |D              |z   }	t        d |D              |z   }
t        t        j
                  j                  |            |||	|
ffS )zg
    Get the coordinates of rotated rectangle and rectangle that covers the
    rotated rectangle.
    Z   gh㈵>r   c              3   &   K   | ]	  }|d      ywr   N .0rh   s     r4   	<genexpr>z,_get_coordinates_of_block.<locals>.<genexpr>       '!'   c              3   &   K   | ]	  }|d      ywr#   Nr   r   s     r4   r   z,_get_coordinates_of_block.<locals>.<genexpr>  r   r   c              3   &   K   | ]	  }|d      ywr   r   r   s     r4   r   z,_get_coordinates_of_block.<locals>.<genexpr>  r   r   c              3   &   K   | ]	  }|d      ywr   r   r   s     r4   r   z,_get_coordinates_of_block.<locals>.<genexpr>  r   r   )r   minmaxtuple	itertoolschainfrom_iterable)rB   r|   r}   r~   r   verticespadmin_xmin_ymax_xmax_ys              r4   _get_coordinates_of_blockr      s     1Au17@H RZ'QC'h''#-E'h''#-E'h''#-E'h''#-E)////9:E5%(* *r6   c                     t        |||||      \  }}t        d      t        d      |g dt        d      | j                         dd}|dz  r||d<   |S )	z=
    Create a link annotation object for embedding URLs.
    AnnotLinkr   r   r   URI)Sr   )TypeSubtypeRectBorderAr   
QuadPoints)r   rK   get_url)	gcrB   r|   r}   r~   r   
quadpointsrectlink_annotations	            r4   _get_link_annotationr     sd     1AufeLJW<e::<
	O rz(2%r6   z\\z\(z\)z\nz\r)\()
c                 ,   t        | d      r| j                         S t        | t        t        j
                  f      rEt	        j                  |       st        d      d| z  }|j                  d      j                  d      S t        | t              rddg|    S t        | t        t        j                  f      rd| z  S t        | t              rPt        | j                         r| j                  d	            S t        j                   | j                  d
      z         S t        | t"              r9d| j%                  d      j'                  t(              j                  d      z   dz   S t        | t*              rWt-        dg| j/                         D cg c]-  \  }}t1        |      j                         dz   t        |      z   / c}}d      S t        | t2        t4        f      r't-        dg| D cg c]  }t        |       c}d      S | yt        | t6              rt        t9        |             S t        | t:              r,t-        | j<                  D cg c]  }t        |       c}      S t?        dtA        |        d      c c}}w c c}w c c}w )z!Map Python objects to PDF syntax.pdfReprz%Can only output finite numbers in PDFs   %.10f   0   .s   falses   trues   %dasciizUTF-16BE   (latin-1   )s   <<r&   s   >>   [   ]s   nullz$Don't know a PDF representation for z objects)!hasattrr   r?   floatnpfloatingisfinite
ValueErrorrstripboolrX   integerr@   isasciiencodecodecsBOM_UTF16_BEbytesdecode	translate_str_escapesdictr5   r^   rK   listr   r   rv   r   bounds	TypeErrortype)objrt   rg   rh   vals        r4   r   r   *  sW    sI{{}
 
C%-	.{{3DEEsNxx~$$T** 
C	'"3'' 
C#rzz*	+s{ 
C	ckkmszz'* J 	J"//#**Z2HHJ 	J 
C	JJy!++L9@@KL	 
C	
@C		L1d1goo$&3L
 
  	 
C$	'dAc:sgcl:ADABB 
 
C	"',-- 
C	"cjj9sgcl9:: >tCyk J" " # 	#+ M ; :s   2J/J
Jc                   (    e Zd ZdZd Zd Zd Zd Zy)	ReferencezV
    PDF reference object.

    Use PdfFile.reserveObject() to create References.
    c                     || _         y r>   idselfr   s     r4   __init__zReference.__init__z  s	    r6   c                      d| j                   z  S )Nz<Reference %d>r   r   s    r4   __repr__zReference.__repr__}  s    $''))r6   c                      d| j                   z  S )Ns   %d 0 Rr   r   s    r4   r   zReference.pdfRepr  s    477""r6   c                 x    |j                   } |d| j                  z          |t        |              |d       y )N	   %d 0 obj
s   
endobj
)writer   r   )r   contentsfiler   s       r4   r   zReference.write  s1    

mdgg%&gh mr6   N)__name__
__module____qualname____doc__r   r   r   r   r   r6   r4   r   r   s  s    *#r6   r   c                       e Zd ZdZdZh  ed      h  e ed       ed      dz         z
  D  ci c]  }|d|z  
 c}} Zd Zd	 Z	d
 Z
d Zd Zd Zd Zyc c}} w )rK   zPDF name object.rL      !~r#   z#%02xc                     t        |t              r|j                  | _        y t        |t              r|j	                  d      }|j                  | j                        j                  d      | _        y )Nr   )r?   rK   rL   r   r   r   _hexifyr   )r   rL   s     r4   r   zName.__init__  sN    dD!		DI$&{{7+t||4;;GDDIr6   c                      d| j                   z  S )Nz	<Name %s>r   r   s    r4   r   zName.__repr__  s    TYY&&r6   c                 >    d| j                   j                  d      z   S )N/r   )rL   r   r   s    r4   __str__zName.__str__  s    TYY%%g...r6   c                 X    t        |t              xr | j                  |j                  k(  S r>   rJ   r   others     r4   __eq__zName.__eq__  s!    %&B499

+BBr6   c                 X    t        |t              xr | j                  |j                  k  S r>   rJ   r   s     r4   __lt__zName.__lt__  s!    %&A499uzz+AAr6   c                 ,    t        | j                        S r>   )hashrL   r   s    r4   __hash__zName.__hash__  s    DIIr6   c                      d| j                   z   S )N   /r   r   s    r4   r   zName.pdfRepr  s    diir6   N)r   r   r   r   	__slots__rangeordr   r   r   r   r   r  r  r   r   r   s   00r4   rK   rK     s~    I%%*(H%C#c(Q,*G(HHJ 'A+~ JGE'/CB 5Js   ArK   c                       e Zd ZdZd Zd Zy)VerbatimzEStore verbatim PDF command content for later inclusion in the stream.c                     || _         y r>   _x)r   rB   s     r4   r   zVerbatim.__init__  s	    r6   c                     | j                   S r>   r  r   s    r4   r   zVerbatim.pdfRepr  s    wwr6   N)r   r   r   r   r   r   r   r6   r4   r  r    s    Or6   r  c                       e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#d"Z$d#Z%d$Z&d%Z'd&Z(d'Z)d( Z*e+d)        Z,y*)+Opz'PDF operators (not an exhaustive list).   b   B   f   h   s   S   ns   BTs   ET   cs   re   l   ms   cms   Do   G   gs   RGs   rgs   CSs   css   SCNs   scn   d   j   Js   gs   q   Qs   Tds   Tfs   Tms   Tss   Tjs   TJ   w   Ws   shc                     | j                   S r>   )valuer   s    r4   r   z
Op.pdfRepr  s    zzr6   c                 n    |r|r| j                   S | j                  S |r| j                  S | j                  S )z
        Return the PDF operator to paint a path.

        Parameters
        ----------
        fill : bool
            Fill the path with the fill color.
        stroke : bool
            Stroke the outline of the path with the line color.
        )fill_strokestrokefillendpath)clsr+  r*  s      r4   
paint_pathzOp.paint_path  s4     &zz!xx{{"r6   N)-r   r   r   r   close_fill_stroker)  r+  	closepathclose_stroker*  r,  
begin_textend_textcurveto	rectanglelinetomovetoconcat_matrixuse_xobjectsetgray_strokesetgray_nonstrokesetrgb_strokesetrgb_nonstrokesetcolorspace_strokesetcolorspace_nonstrokesetcolor_strokesetcolor_nonstrokesetdashsetlinejoin
setlinecap	setgstategsavegrestoretextpos
selectfont
textmatrixtextriseshowshowkernsetlinewidthclipshadingr   classmethodr.  r   r6   r4   r  r    s    1KDILFGJHGIFFMKNM #OGKJIEHGJJHDHLDG # #r6   r  c                   4    e Zd ZdZdZd	dZd Zd Zd Zd Z	y)
Streamz
    PDF stream object.

    This has no pdfRepr method. Instead, call begin(), then output the
    contents of the stream by calling write(), and finally call end().
    )r   r)   pdfFiler   compressobjextraposNc                 ^   || _         || _        || _        |j                  | _        d| _        |t               | _        n|j                         | _        |'| j                  j                  t        d      |d       | j                  j                  | j                          t        j                  d   r-|s+t        j
                  t        j                  d         | _        | j                  t               | _        y| j!                          | j                  j#                         | _        y)a@  
        Parameters
        ----------
        id : int
            Object id of the stream.
        len : Reference or None
            An unused Reference object for the length of the stream;
            None means to use a memory buffer so the length can be inlined.
        file : PdfFile
            The underlying object to write the stream to.
        extra : dict from Name to anything, or None
            Extra key-value pairs to include in the stream header.
        png : dict or None
            If the data is already png encoded, the decode parameters.
        NFlateDecode)FilterDecodeParmspdf.compression)r   r)   rT  fhr   rU  r   rV  copyupdaterK   
recordXrefr\   rcParamszlibr   _writeHeadertellrW  )r   r   r)   r   rV  pngs         r4   r   zStream.__init__  s      GG	=DJDJ?JJd=.A.13 4 	(<<)*3#//./ 1D88	DIyy~~'DHr6   c                    | j                   j                  } |d| j                  z         | j                  }| j                  |d<   t
        j                  d   rt        d      |d<    |t        |              |d       y )Nr   Lengthr\  rY  rZ  s   
stream
)	r   r   r   rV  r)   r\   ra  rK   r   )r   r   r   s      r4   rc  zStream._writeHeader*  se    		mdgg%&zzX<<)*!-0DNgdmmr6   c                 $   | j                          | j                  | j                  j                         }t        |      | _        | j                  j
                  | _        | j                          | j                  j                  |       | j                  j                  d       y| j                  j                         | j                  z
  }| j                  j                  d       | j                  j                  | j                  |       y)zFinalize stream.Ns   
endstream
endobj
)_flushr)   r   getvaluerT  r]  rc  r   rd  rW  writeObject)r   r   r3   s      r4   endz
Stream.end5  s     	88yy))+H8}DHDIIIOOH%IIOO45YY^^%0FIIOO45LL$$TXXv6r6   c                     | j                   | j                  j                  |       y| j                   j                  |      }| j                  j                  |       y)zWrite some data on the stream.N)rU  r   r   compress)r   data
compresseds      r4   r   zStream.writeE  sF     #IIOOD!))2248JIIOOJ'r6   c                     | j                   =| j                   j                         }| j                  j                  |       d| _         yy)zFlush the compression object.N)rU  flushr   r   )r   rp  s     r4   ri  zStream._flushN  sA     '))//1JIIOOJ'#D (r6   NN)
r   r   r   r   r  r   rc  rl  r   ri  r   r6   r4   rS  rS    s)     PI%(N	7 ($r6   rS  c                    t        |       }d|j                  z  }i }|D ]e  }|j                  |t        j                        }t        |j                  |z        dt        j                  |j                  d   |z        t        j                  |j                  d   |z        t        j                  |j                  d   |z        t        j                  |j                  d   |z        g}|j                         \  }}	|dz  |z  j                         }dj                  t        t        |            j                  d      d	z   t!        j"                  t%        ||	      d d d
d dg dd      z   dz   ||j'                  |      <   h |S )N  r   r#         @    r   s    d1
F)r  r  r6   r  r  Tr  )r   units_per_EM
load_glyphr   NO_SCALEroundhoriAdvancerx   floorbboxceilget_pathr+   mapr@   r   r"   convert_to_stringr    get_glyph_name)
	font_pathglyph_indicesfontconvprocsglyph_indexgd1rh   r   s
             r4   _get_pdf_charprocsr  W  s\   ID$###DE$ OOK););<!--$&'JJqvvay4'($**QVVAY5E*FIIaffQi$&'166!9t3C)D	
 }}1Vd]!!#HHSb\"))'2X=%%Q
D$tQ-t55
  	d!!+./" Lr6   c                       e Zd ZdZd< fd	Z  ej                  d       ed             Z  ej                  d       ed             Z	  ej                  d       ed             Z
 ej                  d      ed               Zd	 Zg d
fdZed        Zed        Zd Zd Zd Zd Zd=dZd ZdddZd Zd>dZd Zd Zd Zd Zd Z ej>                  dd      d<d       Z dZ!d  Z"d! Z#d" Z$d# Z%d$ Z&d% Z'  ej                  d&       ed'             Z(d( Z)d) Z*d* Z+d+ Z,d, Z-d- Z.d<d.Z/d/ Z0d0 Z1d1 Z2d2 Z3d3 Z4ed?d4       Z5d@d5Z6dAd6Z7d7 Z8d8 Z9d9 Z:d: Z;d; Z< xZ=S )BPdfFilezPDF file object.Nc                 6   t         	|           t        j                  d      | _        g dg| _        d| _        d| _        d| _        t        j                  |dd      \  }}|s	 |j                         | _        |}d| _        || _        d| _        |j!                  d	       |j!                  d
       | j#                  d      | _        | j#                  d      | _        g | _        | j#                  d      | _        | j#                  d      | _        | j#                  d      | _        | j#                  d      | _        | j#                  d      | _        | j#                  d      | _        t7        d      | j&                  d}| j9                  | j$                  |       t;        d|xs i       | _        d t        j                  d      D        | _        i | _         i | _!        tE        jF                  tD        jH                  jK                  tL        jN                  d   d            | _(        i | _)        d t        j                  d      D        | _*        i | _+        d t        j                  d      D        | _,        g | _-        i | _.        d t        j                  d      D        | _/        g | _0        i | _1        d t        j                  d      D        | _2        i | _3        g | _4        g | _5        g | _6        djo                         D cg c]  }t7        |       }}| j*                  | j2                  | j,                  | j.                  | j0                  |d}| j9                  | j4                  |       y# t        $ r t               }|| _        Y w xY wc c}w )a  
        Parameters
        ----------
        filename : str or path-like or file-like
            Output target; if a string, a file will be opened for writing.

        metadata : dict from strings to strings and dates
            Information dictionary object (see PDF reference section 10.2.1
            'Document Information Dictionary'), e.g.:
            ``{'Creator': 'My software', 'Author': 'Me', 'Title': 'Awesome'}``.

            The standard keys are 'Title', 'Author', 'Subject', 'Keywords',
            'Creator', 'Producer', 'CreationDate', 'ModDate', and
            'Trapped'. Values have been predefined for 'Creator', 'Producer'
            and 'CreationDate'. They can be removed by setting them to `None`.
        r#   )r   i  the zero objectFNr   wbT)return_openeds	   %PDF-1.4
s   % 
rootpagesfontszextended graphics statesztiling patternszGouraud triangleszexternal objects	resourcesCatalog)r   Pagespdfc              3   8   K   | ]  }t        d |         yw)FNrK   r   r1   s     r4   r   z#PdfFile.__init__.<locals>.<genexpr>       "MQ4!A3="M   zps.fonttypec              3   8   K   | ]  }t        d |         yw)r   Nr  r  s     r4   r   z#PdfFile.__init__.<locals>.<genexpr>  s      K1!g Kr  c              3   8   K   | ]  }t        d |         yw)SMNr  r  s     r4   r   z#PdfFile.__init__.<locals>.<genexpr>  s     J!tbH~Jr  c              3   8   K   | ]  }t        d |         yw)HNr  r  s     r4   r   z#PdfFile.__init__.<locals>.<genexpr>  r  r  c              3   8   K   | ]  }t        d |         yw)INr  r  s     r4   r   z#PdfFile.__init__.<locals>.<genexpr>  s     EQ4!A3=Er  PDF Text ImageB ImageC ImageI)FontXObject	ExtGStatePatternShadingProcSet)8superr   r   count_object_seq	xrefTablepassed_in_file_objectoriginal_file_like	tell_baser   to_filehandlerd  OSErrorr   r]  currentstreamr   reserveObject
rootObjectpagesObjectpageList
fontObject_extGStateObjecthatchObjectgouraudObjectXObjectObjectresourceObjectrK   rk  rj   infoDict_internal_font_seq
_fontNames_dviFontInfor$   CharacterTracker_FONT_MAX_GLYPHgetr\   ra  _character_trackeralphaStates_alpha_state_seq_soft_mask_states_soft_mask_seq_soft_mask_groups_hatch_patterns_hatch_pattern_seqgouraudTriangles_images
_image_seqmarkerspaths_annotationspageAnnotationssplit)
r   filenamerc   r]  openedr  rB   procsetsr  	__class__s
            r4   r   zPdfFile.__init__r  s!   " 	$??1-78%*""&((4tL
F2!)
 -1*!
 	(),,V4--g6,,W5 $ 2 23M N--.?@!//0CD!//0BC"00=Y))+$/-eX^D"M)//!:L"M"1"B"B++//]0KQO#Q  K	8J K!#Jyq7IJ!#!"M)//!:L"M "E)//!2DE

   " &E%J%J%LMDGMM
 "__ $ 2 2"&"7"7 $ 0 0 $ 2 2 (*	 	,,i8S  3Y*2'3| Ns   'M5 	N5NNz3.11c                     | j                   S r>   )r  r   s    r4   <lambda>zPdfFile.<lambda>  s
    doo r6   c                     i S r>   r   _s    r4   r  zPdfFile.<lambda>  s    b r6   c                     i S r>   r   r  s    r4   r  zPdfFile.<lambda>  s    " r6   c           	         i }t        j                  t        j                  d            }| j                  j	                         D ]  \  }}||j
                     }|j                  /t        dj                  |j                  |j
                              t        j                  |||j                  |j                  |j                  |j                        ||j
                  <    |S )N
pdftex.mapNNo usable font file found for {} (TeX: {}); the font may lack a Type-1 version)dvifontpdfnamefontfilebasefontencodingfileeffects)r   
PsfontsMapfind_tex_filer  r^   texnamer  r   formatpsnametypesSimpleNamespaceencodingr  )r   rs   tex_font_mapr  r  psfonts         r4   dviFontInfozPdfFile.dviFontInfo  s     ))'*?*?*MN $ 1 1 7 7 9 	GW!'//2F& 9VFMM7??;= = "'!6!6#__"Agoo	 r6   c                    | j                          ||c| _        | _        | j                  d      }| j                  d      }t	        d      | j
                  | j                  ddd|z  d|z  g||d}| j                  d      }| j                  ||       | j                  j                  |       | j                  j                  || j                  f       | j                  |j                  | j                  d             | j                  t	        d	      t        j                          | j                  t	        d	      t        j"                         | j                  t$        j&                  d
   t        j(                         g | _        y )Nzpage contentsannotationsPager   H   )r   Parent	ResourcesMediaBoxContentsAnnotspagezlength of content stream	DeviceRGBr}  )	endStreamr}   r~   r  rK   r  r  rk  r  r*   r  r  beginStreamr   outputr  r>  r?  GraphicsContextPdf
joinstylesrC  )r   r}   r~   contentObjectannotsObjectthePage
pageObjects          r4   newPagezPdfFile.newPage  sK   "'
DK**?;))-8<!-- $ 3 3 !1b5j"v+>,) ''/
W-Z(  ,0D0D!EF))++,FG	I 	D%r'>'>?D%r'A'AB&11':BNNK  "r6   r  r   r   c                 l    t        d      t        d      ||d}| j                  j                  |       y )Nr   Text)r   r   r   r   )rK   r  r*   )r   textpositionRecttheNotes       r4   newTextnotezPdfFile.newTextnote  s3    ="6l#'
 	##G,r6   c                 t    fdt        |       t        j                  dz   dz  z  } |d      }|dd dz   S )z
        Get a prefix for a subsetted font name.

        The prefix is six uppercase letters followed by a plus sign;
        see PDF reference section 5.5.3 Font Subsets.
        c                 z    | |k  rt         j                  |    S  | |z  |      t         j                  | |z     z   S r>   )stringascii_uppercase)nbasetoStrs     r4   r  z)PdfFile._get_subset_prefix.<locals>.toStr!  sD    4x--a00 !t)T*V-C-CAH-MMr6   r#   rv     N   +)r  sysmaxsize)charsethashedprefixr  s      @r4   _get_subset_prefixzPdfFile._get_subset_prefix  sD    	 g3;;?a"78vr" bqzCr6   c                 `    t         j                  t        |j                                     | z   S r>   )r  r%  	frozensetvalues)ps_namecharmaps     r4   _get_subsetted_psnamezPdfFile._get_subsetted_psname0  s$    )))GNN4D*EFPPr6   c           
      j   | j                          | j                          | j                          | j                          | j	                          | j                          | j                          | j                  j                         D ci c]	  \  }}}|| }}}}| j                  j                         D ]  }|d   ||d   <    | j                  D ]  \	  }}}}}}	}
}}|||<    | j                  | j                  |       | j                          | j                          | j                          | j                  | j                   t#        d      | j$                  t'        | j$                        d       | j)                          | j+                          | j-                          yc c}}}w )z>Write out the various deferred objects and the pdf end matter.r#   r   r  )r   KidsCountN)r  _write_annotations
writeFontswriteExtGSTates_write_soft_mask_groupswriteHatcheswriteGouraudTrianglesr  r(  r  r  rk  r  writeImageswriteMarkerswritePathCollectionTemplatesr  rK   r  r)   writeInfoDict	writeXrefwriteTrailer)r   imagerL   obxobjectstuppathtransr+   cappaddingfilledstrokeds                r4   finalizezPdfFile.finalize4  s    	!$$&""$,0LL,?,?,AC C(bD"HC C<<&&( 	&C"1vHSV	& ::	 FD$r4gvwHTN	  	++X6))+))"&w-"&--#&t}}#57	8 	 	'Cs   F.c                 $   | j                          | j                  r| j                  j                          y| j                  3| j                  j                  | j                  j                                | j                  j                          y)z)Flush all buffers and free all resources.N)r  r  r]  rr  r  r   rj  closer   s    r4   rG  zPdfFile.closeS  s^     	%%GGMMO&&2''--dgg.>.>.@AGGMMOr6   c                     | j                   | j                  j                  |       y | j                   j                  |       y r>   )r  r]  r   )r   ro  s     r4   r   zPdfFile.write^  s2    %GGMM$$$T*r6   c           
          | j                  t        |D cg c]  }t        |       c}             | j                  d       y c c}w )Nr'   )r   r5   r   )r   ro  rB   s      r4   r  zPdfFile.outputd  s3    

5d3'!*345

5 4s   Ac                 H    | j                   J t        ||| ||      | _         y r>   )r  rS  )r   r   r)   rV  re  s        r4   r  zPdfFile.beginStreamh  s)    !!)))#BT5#>r6   c                 `    | j                   "| j                   j                          d | _         y y r>   )r  rl  r   s    r4   r  zPdfFile.endStreaml  s-    )""$!%D *r6   rV  c                    | j                  |j                  d |       | j                  j                  |       | j	                          y r>   )r  r   r  r   r  )r   refro  rV  s       r4   outputStreamzPdfFile.outputStreamq  s6    u-  &r6   c                 P    | j                   D ]  \  }}| j                  ||        y r>   )r  rk  )r   r
  r  s      r4   r/  zPdfFile._write_annotationsv  s,    )-):): 	8%L+\;7	8r6   c                    t        |t              r|g}nmt        |t              rt        |d      g}nOt        j                  d   r't        j                  |dt        j                        }nt        j                  |      }d}|D ]i  }| j                  j                  ||f      }|s|}|'t        | j                        }|| j                  ||f<   t        j                  d|||       |rh|}k |S )z
        Select a font based on fontprop and return a name suitable for
        ``Op.selectfont``. If fontprop is a string, it will be interpreted
        as the filename of the font.
        r   pdf.use14corefontsafm)fontext	directoryNz"Assigning font %s (subset %d) = %r)r?   r   r@   r\   ra  _fontManager_find_fonts_by_propsRendererPdf_afm_font_dirr  r  nextr  _logdebug)r   fontpropsubset	filenamesfirst_FxfnameFxs          r4   fontNamezPdfFile.fontNamez  s     h)!
I#&!(A./I\\./$99%;3L3LI %99(CI 		"E$$eV_5Bz$112350

?VUS!H		" r6   c                     t        d|j                  j                  d             }t        j	                  d||j                         || j
                  |<   t        |      S )z
        Given a dvi font object, return a name suitable for Op.selectfont.

        Register the font internally (in ``_dviFontInfo``) if not yet registered.
        zF-r   zAssigning font %s = %s (dvi))rK   r  r   r[  r\  r  )r   r  r  s      r4   dviFontNamezPdfFile.dviFontName  sT     GOO227;<=>

17GOOL%,'"G}r6   c                    i }t        | j                  j                               D ]9  \  }}t        j	                  d|j
                         | j                  |      ||<   ; t        | j                  j                               D ]  \  \  }}}t        j	                  d||       |j                  d      r*t        j	                  d       | j                  |      ||<   [t        j	                  d       | j                  j                  |   |   }| j                  |||      ||<    | j                  | j                  |       y )Nz"Embedding Type-1 font %s from dvi.zEmbedding font %r:%d.z.afmzWriting AFM font.zWriting TrueType font.)sortedr  r^   r[  r\  r  _embedTeXFontr  endswith_write_afm_fontr  usedembedTTFrk  r  )r   r  r  r  r  r^  rb  r*  s           r4   r0  zPdfFile.writeFonts  s    &t'8'8'>'>'@ A 	9GWJJ;W__M!//8E'N	9 '-T__-B-B-D&E 
	E"XvJJ.&A  (

./ 00:b	 

341166x@H MM(FGDb	
	E 	%0r6   c                    t        |d      5 }t        |      }d d d        j                         }t        d      t        d      t        |      t        d      d}| j	                  d      }| j                  ||       |S # 1 sw Y   ixY w)Nrbr  Type1WinAnsiEncoding)r   r   BaseFontEncodingfont dictionary)openr   get_fontnamerK   r  rk  )r   r  r]  r  fontnamefontdictfontdictObjects          r4   rj  zPdfFile._write_afm_font  s    (D! 	Rr7D	$$& L#G} $X $%6 79 ++,=>2	 	s   BB
c           
         t        j                  t        j                  d            }||j                     }|j                  /t        dj                  |j                  |j                              | j                  d      }t        d      t        d      d}t        j                  |j                        }|j                  K|j                  t        t        j                  |j                              D ci c]  \  }}||
 c}}      }|j                   r|j#                  |j                         }t%        |j&                  |j(                        }	| j*                  j,                  |	   d   }
|
D ch c](  }t/        | j*                  j1                  |	d|            * }}|j3                  || j5                  |
j7                                     }t        |j8                  d         |d	<   | j;                  |      |d
<   |j8                  d   }| j=                  |      |d<   t?        |jA                         d      x}|d<   tC        |jA                         d      x}|d<   |jD                  }tG        ||dz         D cg c])  }|jI                  |      x}rd|jJ                  z  dz	  nd+ }}| j                  d      x|d<   }| jM                  ||       | jM                  ||       |S c c}}w c c}w c c}w )Nr  r  rs  r  ro  )r   r   r   FontNamerq  FontDescriptorrr  )default	FirstChar   LastCharr#   ru     zglyph widthsWidths)'r   r  r  r  r  r   r  r  r  rK   r   	Type1Fontr  with_encodingr(   
_parse_encr  	transformr   ra  
face_indexr  rk  r	  subset_to_unicoder^  r%  r(  propcreateType1Descriptor_generate_encodingr   keysr   _metricsr  get_metrics	tex_widthrk  )r   r  r  r  rx  rw  t1fontr1   r   r  r*  ccodecharsr  fclcfont_metricscharglyph_metricswidthswidthsObjects                        r4   rh  zPdfFile._embedTeXFont  s   ))'*?*?*MNgoo.??"5w79 9 ++,=>fg
 %%foo6??&))"+G,>,>v,O"PQ$!QAQF >>%%fnn5F W]]G,>,>?	))..y9!< !
  ''99)QNO
 

 ud&=&=gnn>N&OP#FKK
$;<%)%?%?%G!";;z*#66x@%(!%DDXk"$'$EEXj!'' $BQ/1 (4'?'?'EEmE -111b8LMN 1 1 -1,>,>~,NN\v.2= R
1s   <K1
8-K7.K<c                     d}g }t        |j                               D ]:  \  }}||dz   k7  r|j                  |       |}|j                  t        |             < t        d      |dS )Nr#   rr  r   Differences)rg  r^   r*   rK   )r   r  prevr0   coderL   s         r4   r  zPdfFile._generate_encoding  so     !12 	&JD$taxd#DMM$t*%		& $!
 	
r6   r  c                    | j                  d      }| j                  d      }|j                  d   }|j                  d   }d}|r|dz  }	 	 |dz  }|r|d	z  }	 	 	 |j                  d
   }dj                  t        d |j	                         D                    }	t        d      t        |j                  d         ||j                  d   ||j                  d   d   |j                  d   d   dd||j                  d   d|	d}
| j                  ||
       | j                  |dj                  |j                  d d       t        |j                  d         t        |j                  d         dd       |S )Nfont descriptorz	font fileItalicAngleisFixedPitchr   r#   rv     rx  rr   c              3   0   K   | ]  }|d k7  rd|   yw)z.notdefr   Nr   r
  s     r4   r   z0PdfFile.createType1Descriptor.<locals>.<genexpr>+  s%      	> A3s   r{  rz  FontBBoxrw  ru  i  
FamilyName2   )r   rz  Flagsr  r  AscentDescent	CapHeightXHeightFontFile
FontFamilyStemVCharSetr6   )Length1Length2Length3rL  )
r  r  r+   rg  r(  rK   rk  rO  partsr)   )r   r  r  fontdescObjectfontfileObjectitalic_anglefixed_pitchflagsr  r"  
descriptors              r4   r  zPdfFile.createType1Descriptor  s    ++,=>++K8{{=1kk.1VOE VOE
 VOE   ;;z*'' !)!2 
   01J 78 !;;z2'!;;z215!;;z215)!;;|4"

$ 	4.#((6<<3C*D,/Q,@,/Q,@,-!/ 	 	0
 r6   sO  /CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
   /Ordering (UCS)
   /Supplement 0
>> def
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
1 begincodespacerange
<0000> <ffff>
endcodespacerange
%d beginbfrange
%s
endbfrange
endcmap
CMapName currentdict /CMap defineresource pop
end
endc                 V    t              }t        j                  d   |j                  dfd fd fd} fd} j	                  |j
                  |      j                  dd      t              |j                  d	      xs d
d
d}|j                  d      xs ddi}|j                  }	|j                  }
d
}d}t        j                  |	v r|dz  }	 |r|dz  }n|dz  }t        j                  |
v r|dz  }	 	 	 t        d      ||j                  D cg c]  } |d       c} |j                   d       |j"                  d       |d   d       |d         |d   d   d
d
}dk(  r |||||      S dk(  r |||||      S yc c}w )z9Embed the TTF font from the named file into the document.pdf.fonttypeTc                     | |z  dz  }|rt        |      S |dk  rt        j                  |      S t        j                  |      S )z2Convert font coordinates to PDF glyph coordinates.ru  r   )r}  rx   r  r  )r3   upenearestr'  s       r4   cvtzPdfFile.embedTTF.<locals>.cvtf  sB    SL4'EU|#qyzz%((yy''r6   c           
          d}g }t        |j                               D ](  }||dz   k7  r|j                  ||g       n||d   d<   |}*  fddk(  rdnd}g }|D ]Q  \  }}dj                  fd	t	        ||dz         D              }	|j                  d
|d| dd|d| dd|	 d       S j
                  t        |      dj                  |      j                  d      fz  }
|
S )Nr  r#   c                     j                   j                  |       }|j                  d      j                         }d| dS )Nutf-16be<>)r  r  r   hex)r  r  hexstrr  r   subset_indexs      r4   _to_unicodezDPdfFile.embedTTF.<locals>.generate_unicode_cmap.<locals>._to_unicode|  sE    //AAlE3j15576(!}$r6   rw  rv  r  ry  c              3   .   K   | ]  } |        y wr>   r   )r   rB   r  s     r4   r   zBPdfFile.embedTTF.<locals>.generate_unicode_cmap.<locals>.<genexpr>  s     &S!{1~&Ss   r  0rB   z> <z> []r   r   )rg  r  r*   r+   r  _identityToUnicodeCMapr)   r   )r  r*  
last_ccodeunicode_groupsr  r}   unicode_bfrangestartrl  real_valuesunicode_cmapr  r  fonttyper   s   `          @r4   generate_unicode_cmapz/PdfFile.embedTTF.<locals>.generate_unicode_cmapq  s#   JN/ #JN*"))5%.9,1N2&q)"
#% "QAAE O, N
s!hh&SuUCPQE?R&SS&&awaZ(C%z?#k]!LNN !77 0!YY7>>wGIIL  r6   c                     "j                  d      }"j                  d      }"j                  d      }"j                  d      }"j                  d      }g }	t              t              }}
 j                  D cg c]  } |d       }}t	        d      !|
||t	        d	      |d
   |g d|t	        d      |	d||d} fd}t        |
|dz         D cg c]
  } ||       }}t        |      |d<   t        j                         D cg c]  \  }}| j                  |      f c}}      }d}|D ]:  \  }}||dz   k7  r|	j                  |       |	j                  t	        |             |}< t        j                               }i }t        |      D ]/  }||   }"j                  d      }"j                  ||       |||<   1   |      }"j                  ||       "j                  ||       "j                  ||       "j                  ||       "j                  ||       |S c c}w c c}w c c}}w )z5The Type 3-specific part of embedding a Truetype fontzfont widthsr  rs  zcharacter procsToUnicode mapFr  r  Type3rz  )MbP?r   r   r  r   r   rr  r  )r   rq  r}  r  r{  r   rK   r  
FontMatrix	CharProcsrr  r  	ToUnicodec                     j                  j                  | d      t        j                  t        j                  z        j
                  } |      S )Nr   r  )r{  r  r   r|  
NO_HINTINGr~  )charcoder}   r*  r  r  s     r4   get_char_widthz?PdfFile.embedTTF.<locals>.embedTTFType3.<locals>.get_char_width  sM    KK!,#,,y/C/CC ( EEP[  5z!r6   r#   MaxWidthr  charProc)r  r   r   r  rK   r  rg  r^   r  r*   r  r(  rO  rk  )#r  r  r*  r  r  r  rx  charprocsObjecttoUnicodeMapObjectdifferencesArray	firstcharlastcharrB   r  rw  r  r  r  r  ginddifferenceslast_cr   rL   rawcharprocs	charprocscharnamestreamcharprocObjectr  r  r  r  r)  r   s#   ` `                           r4   embedTTFType3z'PdfFile.embedTTF.<locals>.embedTTFType3  sz   --m<L!//0ABN!//0ABN"001BCO!%!3!3O!D!"%g,GxI3799=aC5)=D= V#&$"0=":. 6, ,#35 '/H&" ',Ixz&BD" %X. DF D%([Jz" !FMmmo"7Bud++D12" K F& 4
?$++A. ''T
3	 .h8HILI"<0 5%h/!%!3!3J!?!!.&9&4	(#	5 1wGL ^X6^Z8\62_i80,?!!y >4D"s   ?H8H=I
c                    j                  d      }j                  d      }j                  d      }j                  d      }j                  d      }j                  d      }	j                  d      }
t        j                  d||       t        j                  |j                               5 }t        j                  |j                        }d	d	d	       t        j                  d
|t        j                        j                  j                         j                         t        |      } t        d      t        d      dddd||	|d}t        d      t        d      t        d      |g|
d}||d<   j                  ||j!                         d|j                         j                  i       dgdz  }g }d}|j#                         D ]  \  }}j$                  |   }| j'                  |t(        j*                  t(        j,                  z        }|j/                  | |j0                        f       t3        |      ||<   t5        ||      } |j7                          |d	|dz    }d}g }d}|D ]S  \  }}||dz   k7  r$|j/                  |       |j/                  |g       n|d   j/                  |       t5        ||      }|}U dj9                  |      j;                  d      }j                  ||        ||      }j                  |
|       ||d <   j=                  ||       j=                  ||       j=                  ||       j=                  |	|       |S # 1 sw Y   xY w)!z6The Type 42-specific part of embedding a Truetype fontr  zCID font dictionaryzType 0 font dictionaryzCIDToGIDMap streamzfont file streamzType 0 widthsr  zSUBSET %r:%d characters: %sNzSUBSET %r:%d %d -> %dr  CIDFontType2AdobeIdentityr   )RegistryOrdering
Supplement)r   r   rq  CIDSystemInfor{  WCIDToGIDMapType0z
Identity-H)r   r   rq  rr  DescendantFontsr  	FontFile2r  rL   i   r  r#   r  r  r  r  r  )r  r[  r\  r$   get_glyphs_subsetr(  font_as_filer  rU   statst_size	getbuffernbytesr   rK   rO  rj  r^   glyph_index_mapr{  r   r|  r  r*   r~  chrr   sortr+   r   rk  ) r  r  r*  r  r  cidFontDictObjecttype0FontDictObjectcidToGidMapObjectr  wObjectr  r^  fontdatacidFontDicttype0FontDictcid_to_gid_mapr  	max_ccoder  orig_glyph_indexr  glyphr  w	max_widthr}   r  r  r  r  r)  r   s                               r4   embedTTFType42z(PdfFile.embedTTF.<locals>.embedTTFType42  s   !//0ABN $ 2 23H I"&"4"45M"N $ 2 23G H!//0BCN((9G!%!3!3O!DJJ4hgV 2283:>>3CE EHN*77DE JJ'<!))8+=+=+?+F+F 8$D V/# ' *"#"% #10K V=# .$5#6/M '5J{# 1 1 3 ("4"4"6"="=>  @
 #Ve^NFI+2==? 2''--.>?.7.@.@9CWCW.W ( Yuc%*;*;&<=>(+D	u%y1	2 KKM+NY];NJAI & #uJN*HHUOHHeW%bELL'	51	"
#  WW^4;;JGN/@ 1wGL0,?%.Jz" .<0-@^Z8Wa(&&gE Es   5 M..M8r   rZ   pcltr   )	capHeightxHeightpostitalicAngler   r   Fr#   r      rx  r{  r  r  r  )
r   rz  r  r  r  r  r  r  r  r  rw  *   N)r   r\   ra  rz  r+  postscript_namer   rK   get_sfnt_table
face_flagsstyle_flagsr   FIXED_WIDTHr   ITALICr  ascender	descender)r   r  r  r*  r  r  r  r  r  ffsfr  symbolicrB   r  r  r  r  r)  s   ``             @@@@r4   rl  zPdfFile.embedTTFa  s   !<</ --t 		(	 :E	" E	"N^	' ^	'D ,,T-A-A7K..)4w-""6*LA!.L""6*E}f.E__  B&VOEVOEVOE"VOE )*8<		B1Q.B$--74>>59T+.>4	?+.q1
 q= |WjII^!$gzJJ  Cs   +F&
c                     | j                   j                  |d      }||d   S t        | j                        }|t	        d      |d   |d   df| j                   |<   |S )z?Return name of an ExtGState that sets alpha to the given value.Nr   r  r#   )r   CAca)r  r  rZ  r  rK   )r   alphastaterL   s       r4   
alphaStatezPdfFile.alphaStated  ss       $$UD18OD))*D-(%(4 5 	 r6   c                    | j                   j                  |d      }||d   S t        | j                        }| j	                  d      }|t        d      dt        d      t        d      dg|d	d
f| j                   |<   | j                  j                  |t        d      t        d      dt        d      t        d      dg ddd|iig ddt        d      t        j                  gf       |S )aA  
        Return an ExtGState that sets the soft mask to the given shading.

        Parameters
        ----------
        smask : Reference
            Reference to a shading in DeviceGray color space, whose luminosity
            is to be used as the alpha channel.

        Returns
        -------
        Name
        Nr   z transparency group for soft maskr  FMask
Luminosityr#   )r   r   BCG)r   AISSMaskr  FormTransparency
DeviceGray)r   CS)r#   r   r   r#   r   r   r  r   )r   r   r#   r#   )r   r   FormTypeGroupMatrixr  BBox)
r  r  rZ  r  r  rK   r  r*   r  rP  )r   smaskr1  rL   groupObs        r4   _soft_mask_statezPdfFile._soft_mask_stateq  s    &&**5$78OD''($$%GH[) Ll+# 		)
u% 	%%Y<n-|, -'#u6$ #Y

#'
 	  r6   c                     | j                  | j                  t        g | j                  j	                         | j
                  j	                                      y r>   )rk  r  r   r  r(  r  r   s    r4   r1  zPdfFile.writeExtGSTates  sT    !! !!((*''..0 	
r6   c                     | j                   D ]B  \  }}}| j                  |j                  d |        | j                  |  | j	                          D y r>   )r  r  r   r  r  )r   r<  
attributescontents       r4   r2  zPdfFile._write_soft_mask_groups  sL    '+'='= 	#B
GRUUD*5DKK!NN	r6   c                     |\  }}}}|t        |      }|t        |      }||||f}| j                  j                  |d       }||S t        | j                        }|| j                  |<   |S r>   )r   r  r  rZ  r  )r   hatch_styleedgefacehatchlwpatternrL   s           r4   hatchPatternzPdfFile.hatchPattern  s     +dE2;D;DT5"-&&**;=ND++,,0[)r6   3.10c                     | j                   j                         D ci c]  \  }\  }}}}||||f c}}}}}S c c}}}}}w r>   )r  r^   )r   rg   r   r   hls         r4   r  zPdfFile.<lambda>  sJ    ,0,@,@,F,F,HC C(LQ1aAq!9C  Cs   =c                 >   t               }d}| j                  j                         D ]  \  }}| j                  d      }|||<   ddj	                         D cg c]  }t        |       c}i}| j                  |j                  d t        d      ddddd||g|||ddddd| j                  dz  gd		       |\  }}	}
}| j                  |d   |d   |d
   t        j                         |	M| j                  |	d   |	d   |	d
   t        j                  dd||t        j                  t        j                  
       | j                  |d   |d   |d
   t        j                         | j                  |t        j                          | j                  | j!                  t#        j$                  |
      t'               j)                  |      d        | j                  t        j*                         | j-                           | j/                  | j0                  |       y c c}w )N      R@zhatch patternProcsetsr  r  r#   r   r  )	r   PatternType	PaintType
TilingTyperA  XStepYStepr  r@  rv  Fsimplify)r   r  r^   r  r  rK   r  r   r~   r  r  r<  r=  r5  r+  rN  pathOperationsr    rM  r   scaler)  r  rk  r  )r   	hatchDictsidelenrJ  rL   r<  rB   res
stroke_rgbfill_rgbrM  rN  s               r4   r3  zPdfFile.writeHatches  s   F	!%!5!5!;!;!= "	K##O4B IdO%D%J%J%LMDGMOCti !Q1!G!q!Q4;;+;<>? /:+J%KK
1z!}jm((*#HQK!hqk//q'7BLLGG% KK
1z!}jm++- KKBOO,DKK,,

5!
  ) -   ! KK'NNE"	F 	))95? Ns   Hc                     t        dt        | j                        z        }| j                  d|       }| j                  j	                  ||||f       ||fS )a  
        Add a Gouraud triangle shading.

        Parameters
        ----------
        points : np.ndarray
            Triangle vertices, shape (n, 3, 2)
            where n = number of triangles, 3 = vertices, 2 = x, y.
        colors : np.ndarray
            Vertex colors, shape (n, 3, 1) or (n, 3, 4)
            as with points, but last dimension is either (gray,)
            or (r, g, b, alpha).

        Returns
        -------
        Name, Reference
        zGT%dzGouraud triangle )rK   r)   r  r  r*   )r   pointscolorsrL   r<  s        r4   addGouraudTriangleszPdfFile.addGouraudTriangles  s\    $ FS!6!6778"3D6 :;$$dB%?@Rxr6   c                 L   t               }| j                  D ]n  \  }}}}|||<   |j                  }|j                  |d   |d   z  df      }|j                  d   }|dv sJ |j                  |d   |d   z  |f      }	|dk(  rd}t	        j
                  |d      dz
  }
t	        j                  |d      dz   }d	||
z
  z  }| j                  |j                  d dd
ddt        |dk(  rdnd      d|
d   |d   |
d   |d   gddg|z  z   d       t	        j                  |d   |d   z  fdddd|ffg      }d|d<   ||
z
  |z  |d<   |	d d d |f   dz  |d<   | j                  |j                                | j                          q | j                  | j                  |       y )Nr   r#   rv  r#   r  r  rw  axisr   l    r      r  r<  F)ShadingTypeBitsPerCoordinateBitsPerComponentBitsPerFlag
ColorSpace	AntiAliasDecode)r  u1)rg  z>u4)rv  rh  rv  dtyper  rg  g     o@)r   r  shapereshaper   r   r   r  r   rK   emptyr   tobytesr  rk  r  )r   gouraudDictrL   r<  rg  rh  ry  flat_pointscolordimflat_colors
points_min
points_maxfactor	streamarrs                 r4   r4  zPdfFile.writeGouraudTriangles  s   f(,(=(= '	$D"ff "KLLE ..%(U1X*=q)ABK||AHv%%% ..%(U1X*=x)HIK1}!4?J!4?J:
#:;Ft !&(%& !#$,M[|  $']JqM']JqM; !fx/0 qE!H$&&. $467I
 "#Ig#.#;v"EIh"-a(l";e"CIhJJy((*+NNO'	P 	++[9r6   c                     | j                   j                  t        |      d      }||d   S t        | j                        }| j                  d|       }|||f| j                   t        |      <   |S )z=Return name of an image XObject representing the given image.Nr#   zimage )r  r  r   rZ  r  r  )r   r;  entryrL   r<  s        r4   imageObjectzPdfFile.imageObject4  so       ED18ODOO$&0#($"3RYr6   c                 B   |ddd   }|j                   dk(  r|dfS |ddddddf   }t        j                  |d      }|j                  d   dk(  rH|dddddf   d   }t        j                  |d	k(        rd}||fS t        j                  |d      }||fS d}||fS )
z
        Unpack image array *im* into ``(data, alpha)``, which have shape
        ``(height, width, 3)`` (RGB) or ``(height, width, 1)`` (grayscale or
        alpha), except that alpha is None if the image is fully opaque.
        Nr  rv  rw  C)orderr  ).Nr~  )ndimr   arrayry  all)r   imrgbr0  s       r4   _unpackzPdfFile._unpack@  s     "X77a<t8OQ2A2X,C((3c*Cxx{a1a7I.66%3,' E
 : HHU#6E : :r6   c                    t               }|j                  |d       |j                  d       d}dx}}	 t        j                  d|j                  d            \  }}|dv rQ|j                  |      }t        |      |k7  rt        d      |d	k(  rt        |d         }n+|d
k(  r|}n#|dk(  r||z  }n|dk(  rn%|j                  |d       |j                  dd       |||fS )zn
        Write the image *img* into the pdf file using png
        predictors with Flate compression.
        re  )r  rn  r6   Ns   !L4s)   IHDR   PLTE   IDATztruncated datar  r  r  s   IENDr#   r  )	r   saveseekstructunpackreadr)   RuntimeErrorrX   )	r   imgbufferpng_data	bit_depthpaletter3   r   ro  s	            r4   	_writePngzPdfFile._writePngW  s    
 &A""	G!==&++a.ALFD22{{6*t9&&'7887? #DGIW_"GW_$HFA&KK1! " G++r6   c           	      f   |j                   \  }}}t        d      t        d      ||t        ddd|         dd}|r||d<   t        j                  d	   rX|j                   d
   dk(  r|j	                  d
      }d||d}t        j                  |      }	|	j                  d      }
|dk(  r|
t        |
      }t        j                  |
D cg c]  \  }}|D ]  }|  c}}}t        j                        }|ddd   j                  t        j                        dz  |ddd   j                  t        j                        dz  z  |ddd   z  }|dddddf   j                  t        j                        dz  |dddddf   j                  t        j                        dz  z  |dddddf   z  }t        j                  |      j                  t        j                        }|t        j                  |||         }t        j                  |      j!                  d      }	|	j#                  |       | j%                  |	      \  }}}||t'        d      |d|dz   }t        d      t        d      |dz
  |g|d<   ||d<   d|d<   ||d<   n| j%                  |	      \  }}}nd}| j)                  || j+                  d      ||       |r| j,                  j/                         n)| j,                  j/                  |j1                                | j3                          yc c}}}w ) a  
        Write the image *data*, of shape ``(height, width, 1)`` (grayscale) or
        ``(height, width, 3)`` (RGB), as pdf object *id* and with the soft mask
        (alpha channel) *smask*, which should be either None or a ``(height,
        width, 1)`` array.
        r  r   r<  r  r#   rw  rn  )r   r   WidthHeightrs  rq  r9  r\  r  r#   rl  
   )	PredictorColorsColumnsr   )	maxcolorsrw  Nrw  r      rv  )sorterPzinvalid PNG headerIndexedrs  rq  r  zlength of image stream)re  )ry  rK   r\   ra  squeezer   	fromarray	getcolorsr)   r   r  uint8astypeuint32argsortsearchsortedconvert
putpaletter  r  r  r  r  r   r|  r  )r   ro  r   rB  r~   r}   color_channelsr   re  r  
img_colors
num_colorsr  colorcompr  	palette24rgb24indicesrgb8r  r  s                         r4   	_writeImgzPdfFile._writeImgt  s    )-

%~Iw-!l{"CN"ST#$&  CL<<)*zz"~"|||, "nOC//$'C5J"z'= !_
((#V#VXQPU#VD#VD#V)+3%addm22299=C%addm22299=BD$QTT]+	 q!Qw-..ryy9R?q!Qw-..ryy9Q>@aAg' **Y/66rxx@ry%PQood+33C8w'/3~~c/B,)W$&';<<!/:>2%))_d;6G%/!^W%>L!*3&' !H*3&'!%!4!QC78	 	 	 $$X.$$T\\^4G $Ws   L,c                    | j                   j                         D ]i  \  }}}| j                  |      \  }}|.| j                  d      }| j	                  ||j
                         nd }| j	                  ||j
                  |       k y )NrB  )r  r(  r  r  r  r   )r   r  rL   r<  ro  adatasmaskObjects          r4   r5  zPdfFile.writeImages  sw    !\\002 	5MCr,,s+KD% "009uknn5"NN44	5r6   c                    | j                  ||d      }t        |      t        |      t        |      ||f}	| j                  j	                  |	      }
|
nt        dt        | j                        z        }| j                  dt        | j                        z        }|j                  |      }||||g| j                  |	<   |S |
d   |k  r||
d<   |
d   }|S )z<Return name of a marker XObject representing the given path.Fr]  zM%dz	marker %dr  r   )	r_  r   r   r  r  rK   r)   r  get_extents)r   r?  r@  r+  r*  rN  	joinstylecapstylepathopskeyr0   rL   r<  r  s                 r4   markerObjectzPdfFile.markerObject  s     %%dEE%BW~tDz4<HM!!#&>DLL 112D##K#dll2C$CDB##E*D!%r4 4DLL
  bzBr
!9Dr6   c           
      b   | j                   j                         D ]  \  \  }}}}}\  }}}}	|j                  |	dz        }| j                  |j                  d t        d      t        d      t        |j                        d       | j                  t        j                  |   t        j                         | j                  t        j                  |   t        j                          | j                  |  | j                  t        j                  ||             | j!                           y )N   r  r:  r   r   rA  )r  r^   paddedr  r   rK   r   extentsr  r  r  r  rC  	capstylesrD  r.  r  )
r   r  r+  r*  r  r  rL   r<  r  rN  s
             r4   r6  zPdfFile.writeMarkers  s    &*ll&8&8&:	 #9gtVY!dBb ;;rAv&DtiT&\dll+-. KK*55i@(KK*44X>NDKK!KKdF34NN'	r6   c                    t        dt        | j                        z        }| j                  dt        | j                        z        }| j                  j	                  |||||j                         |j                         |||f	       |S )NzP%dzpath %d)rK   r)   r  r  r*   get_joinstyleget_capstyle)	r   r   r?  r@  rB  rC  rD  rL   r<  s	            r4   pathCollectionObjectzPdfFile.pathCollectionObject  sx    EC

O+,	C

O ;<

4B$4$4$68Ifg'	( r6   c           
         | j                   D ]h  \	  }}}}}}}}}	| j                  ||d      }
|j                  |      }t        j                  t        j
                  |j                              sg d}n&|j                  |      }t        |j                        }| j                  |j                  d t        d      t        d      |d       | j                  t        j                  |   t        j                          | j                  t        j"                  |   t        j$                          | j                  |
  | j                  t        j'                  ||	             | j)                          k y )NFr]  r   r   r   r   r  r:  r  )r  r_  r  r   r  r   r  r  r   r  r   rK   r  r  r  r  rC  r  rD  r.  r  )r   rL   r?  r@  r<  r  r  rB  rC  rD  r  r  r  s                r4   r7  z$PdfFile.writePathCollectionTemplates  s(   	 T4Ix&))$)FG##E*D66"++dll34&{{7+t||,tiT&\ "# KK*55i@(KK*44X>NDKK!KKfg67NN%	r6   c                    t        t        j                  | ||||dt        j                  j
                  t        j                  j
                  dt        j                  j
                  t        j                  j
                  gd            gS )Nr  r6   T)	r  r"   r  r  r7  r'  r6  r4  r0  )r?  r  rO  r^  sketchs        r4   r_  zPdfFile.pathOperations  sg    00)T8VYY__biioosBJJ4D4D\\!   	r6   c                     |r-dd| j                   dz  | j                  dz  f}|j                  }nd }d}| j                  |||||      } | j                  |  y )N        r  F)r^  r  )r}   r~   should_simplifyr_  r  )r   r?  r  rO  r  r^  cmdss          r4   	writePathzPdfFile.writePath  sg    djj2ot{{R/?@D++HDH""4D8*0 # 2Tr6   c                 ~    t        | j                        }| j                  j                  dd|g       t	        |      S )z
        Reserve an ID for an indirect object.

        The name is used for debugging in case we forget to print out
        the object with writeObject.
        Nr   )rZ  r  r  r*   r   )r   rL   r   s      r4   r  zPdfFile.reserveObject!  s6     $""#tQo.}r6   c                 r    | j                   j                         | j                  z
  | j                  |   d<   y Nr   )r]  rd  r  r  r   s     r4   r`  zPdfFile.recordXref,  s(     $ ?r1r6   c                 ^    | j                  |j                         |j                  ||        y r>   )r`  r   r   )r   objectr   s      r4   rk  zPdfFile.writeObject/  s     		"Xt$r6   c                 T   | j                   j                         | j                  z
  | _        | j	                  dt        | j                        z         t        | j                        D ]=  \  }\  }}}|t        d||fz        |dk(  rdnd}d|||fz  }| j	                  |       ? y)zWrite out the xref table.s
   xref
0 %d
NzNo offset for object %d (%s)r  r  r  s   %010d %05d %b 
)	r]  rd  r  	startxrefr   r)   r  r(   AssertionError)r   r1   offset
generationrL   r  r  s          r4   r9  zPdfFile.writeXref3  s    $..8

?S%889-6t~~-F 	!)A)
D~$2aY>@ @ #&77dT*fj#-FF

4 	!r6   c                 |    | j                  d      | _        | j                  | j                  | j                         y)z8Write out the info dictionary, checking it for good formrf   N)r  
infoObjectrk  r  r   s    r4   r8  zPdfFile.writeInfoDict@  s.     ,,V4$--8r6   c                     | j                  d       | j                  t        t        | j                        | j                  | j
                  d             | j                  d| j                  z         y)zWrite out the PDF trailer.s   trailer
)SizeRootInfos   
startxref
%d
%%%%EOF
N)r   r   r)   r  r  r  r  r   s    r4   r:  zPdfFile.writeTrailerF  sZ     	

< 

7(____&' 	(
 	

04>>ABr6   r>   rs  r   )NNNFN)r  )>r   r   r   r   r   r	   
deprecatedproperty	fontNamesmulti_byte_charprocstype1Descriptorsr  r  r  staticmethodr%  r+  rE  rG  r   r  r  r  rO  r/  rc  re  r0  rj  rh  r  delete_parameterr  r  rl  r2  rD  r1  r2  rP  hatchPatternsr3  ri  r4  r  r  r  r  r5  r  r6  r  r7  r_  r  r  r`  rk  r9  r8  r:  __classcell__r  s   @r4   r  r  o  s   e9N ('1M(NOI2?4??628L3IJ.tv.x/EFT__V  (": .@ -    , Q Q>	+?&
 04 
8B	1$
2h
 T6:.D /DL*AKF2h
" ,ODOOF+H 6 - M&6P.*:X
.,:=~5:,*  		@%!9	Cr6   r  c                        e Zd Z ej                  d      ZdZ fdZd ZddZ	d Z
ddZdd	Zdd
dZ	 ddZd ZddZd ZdddZd Zd ZddZd Z xZS )rX  zfonts/pdfcorefontsrR  c                 l    t         |   ||       || _        | j                         | _        || _        y r>   )r  r   r   new_gcr   	image_dpi)r   r   r  r~   r}   r  s        r4   r   zRendererPdf.__init__W  s-    '	++-"r6   c                 f     | j                   j                  | j                  j                           y r>   )r   r  r   rE  r   s    r4   rE  zRendererPdf.finalize]  s#    		$''**,-r6   Nc                    t        |dd      }||_        t        |dd      }|j                         |j                  dd       |j                  r|j
                  |j
                  f|_        n@|t        |      dk  r|j                  d	   d
f|_        n|j                  d	   |d	   f|_        | j                  j                  |      }|r | j                  j                  |  ||_        ||_        y )N
_fillcolorr  r  r  _effective_alphas      ?r  r  T)isRGBAr  rw  r  )getattrr  get_rgbset_foreground_forced_alpha_alphar  r)   _rgbr   deltar   r  )r   r   	fillcolor	orig_fillorig_alphasr  s         r4   check_gczRendererPdf.check_gc`  s    Bl;	!b"5zB::< l48$&IIryy#9B #i.1"4$&GGAJ#4B $&GGAJ	!#=B b!DIIe$ "*r6   c                      | j                   dz  S )NrV  )r  r   s    r4   get_image_magnificationz#RendererPdf.get_image_magnification{  s    ~~d""r6   c                    |j                   d d \  }}|dk(  s|dk(  ry ||j                  d       | j                  |       d|z  | j                  z  }d|z  | j                  z  }| j                  j                  |      }|^| j                  j                  t        j                  |dd|||t        j                  |t        j                  t        j                         y |j                         j                         \  }	}
}}}}| j                  j                  t        j                  dddd||t        j                  |	|
||||t        j                  |t        j                  t        j                         y )Nrv  r   r  rV  r#   )ry  	set_alphar  r  r   r  r  r  rF  r8  r9  rG  frozen	to_values)r   r   rB   r|   r  r  rS  r  imobtr1tr2tr3tr4tr5tr6s                  r4   
draw_imagezRendererPdf.draw_image~  s8    xx|16Q!VLLb1Ht~~%1Ht~~%yy$$R(IIRXX1aAr/?/?!2>>2;;@ ,5+;+;+=+G+G+I(Cc3SIIRXX1aAr/?/? #sCc2;K;K!2>>2;;@r6   c                    | j                  ||       | j                  j                  |||d u xr |j                         d u |j	                                | j                  j                  | j                  j                                y r>   )r  r   r  get_hatch_pathget_sketch_paramsr  r   paint)r   r   r?  r  rgbFaces        r4   	draw_pathzRendererPdf.draw_path  sk    b'"		)tO; 1 1 3t ;  "	$ 			)r6   hatchcolorsc                   d}t        j                  |      }t        j                  |      }|g }t        |      sd}|j                          }n-t        j                  |d d df   |d   k(        r	|d   dk7  }nd}t        |      sd}n[t        j                  t        j                  |	      dk(        rd}n-t        j                  |d d df   |d   k(        r	|d   dk7  }nd}t        |      dkD  rt        |d   j
                        nd}| j                  |||||      }||z   dz   ||z  k  }|r|s$t        j                  | |||||||||	|
||||      S t        j                  |	      }g }g }t        | j                  |||            D ]  \  }\  }}| j                  j                  ||||      }|j                  |       t        |j
                        r?|j                  |      }|j                  |j                   d	z  |j"                  d	z  f       |j                  d
        t%        t'        ||            }| j                  j                   dz  }| j                  j"                  dz  }| j                  j(                  }  | | j*                  j-                           d
\  }!}"| j/                  |||||||	|
||||      D ]  \  }#}$}%}&}'d|#cxk  r|k  r^n n[d|$cxk  r|k  rPn nM| j1                  |&|'       |#|!z
  |$|"z
  })}( | dddd|(|)t2        j4                  |%t2        j6                  	       |#|$}"}!r||%   \  }*}+|* |#cxk  r||*z   k  rn n|+ |$cxk  r||+z   k  sn | j1                  |&|'       |#|!z
  |$|"z
  })}( | dddd|(|)t2        j4                  |%t2        j6                  	       |#|$}"}!  | | j*                  j9                           y )NTFrw  )r   rw  r  r   r  r&  rv  r  r  r#   )r   asarrayr)   	get_hatchr  r   _iter_collection_uses_per_pathr   draw_path_collectionr   r(   _iter_collection_raw_pathsr   r  r*   r  r}   r~   r   zipr  r   push_iter_collectionr  r  r8  r9  pop),r   r   master_transformr  all_transformsoffsetsoffset_trans
facecolors
edgecolors
linewidths
linestylesantialiasedsurlsoffset_positionr'  can_do_optimizationrC  rD  len_pathuses_per_pathshould_do_optimizationrB  
path_codespath_extentsr1   r?  r  rL   r  path_extent_mapcanvas_widthcanvas_heightr  lastxlastyxoyopath_idgc0r$  dxdyextent_xextent_ys,                                               r4   r,  z RendererPdf.draw_path_collection  s    #ZZ
+
ZZ
+
K:F&(lln"4vvjA&*T*::;#D)S0&+#:Gvvbjj,34
1a4(Jt,<<=$T*c1&+# .1Z!^3uQx(();;>7J
D }$q(8m+CC 	 $-C44b*E>z:Jd[	: : &&$
$-d.M.M %/9 %: 	, A i9911D)Wfg?Dd#
 4==!''	2##TZZ!^T[[1_$EF##F+	," s:|<=yy+		((2-!!u-1-B-BJJ
JdO .C .N 	")BGS' B&,&1+Cm+Cc7+eR%ZBq!Q2r2+;+;W~~'!2u "1!9HhI>|h'>>!	RC=8+CCMM#w'%ZeB1aAr2r'7'7>>#r5E?	"@ 	r6   c                    t        |      }t        |      }||z  ||z   dz   k  rt        j                  | ||||||       y | j                  ||       |j	                  |      }	|j                         }
| j                  j                  }| j                  j                  |||	|
| j                  j                  |j                         |j                               } |t        j                         d\  }}|j                  |dd| j                  j                   dz  | j                  j"                  dz  fd      D ]  \  }}t        |      s|dd  \  }}d|cxk  r| j                  j                   dz  k  r'n n$d|cxk  r| j                  j"                  dz  k  sn d||z
  ||z
  }} |dddd||t        j$                  |t        j&                  	       ||}}  |t        j(                         y )	Nr  r  r   r  F)rO  r^  r  r#   )r)   r   draw_markersr  r+  r*  r   r  r  r   
_linewidthr  r  r  rF  iter_segmentsr}   r~   r8  r9  rG  )r   r   marker_pathmarker_transr?  r@  r$  len_marker_pathusesr+  r*  r  markerrF  rG  r   r  rB   r|   rL  rM  s                        r4   rQ  zRendererPdf.draw_markers  s   
 k*4yT!Od$:Q$>>%%dB\&*E7<b'"www!!''tVTWW5G5G 13 	rxxu"00DIIOOB.		0@0@0CD 1   	$NHd 8}}1Q6$))//B"66;dii&6&6&;;UAIBq!Q2r2+;+;r~~/ !u	$ 	r{{r6   c                    t        |      t        |      k(  sJ t        |      dk(  ry |j                  dk(  sJ |j                  d   dk(  sJ |j                  d   dk(  sJ |j                  dk(  sJ |j                  d   dk(  sJ |j                  d   dv sJ |j                  }|j                  |d   |d   z  df      }|j	                  |      }|j                  |      }| j
                  j                  ||      \  }}| j
                  j                  }	|j                  d   dk(  r:|j                  d       | j                  |        |	|t        j                         y |d   }
t        j                  |
|d d d d df         r:|j                  |
       | j                  |        |	|t        j                         y |d d d d df   d d d d d f   }
| j
                  j                  ||
      \  }}| j
                  j                  |      } |	t        j                  |t        j                   |t        j                  t        j"                         y )Nr   rw  r#   rv  rk  r  )r   r   rw  )r)   r  ry  rz  r  r   ri  r  r  r  r  rP  r   allcloserD  rF  rE  rG  )r   r   rg  rh  r@  ry  tpointsrL   r  r  r0  smask_obgstates                r4   draw_gouraud_trianglesz"RendererPdf.draw_gouraud_triangles;  s   6{c&k)))v;!{{a||A!###||A!###{{a||A!###||A&(((qE!H!4a 89//&)//%())//@a!!<<?aLLMM"4$w;;ufQ1Wo.LLMM"4$ 1a7OAq$J/E))77GKAxYY//9F288VR\\;; r6   c           	         ||cxk(  rdk(  r5n n2| j                   j                  ||z
  ||z
  t        j                         y t	        j
                  |      }| j                   j                  t	        j                  |      t	        j                  |      t	        j                  |       t	        j                  |      ||t        j                         | j                   j                  ddt        j                         y r  )	r   r  r  rH  rx   ry   r{   rz   rJ  )r   rB   r|   r   oldxoldyoldangles          r4   _setup_textposzRendererPdf._setup_textposc  s    H!!IIQXq4x<LL'EIITXXe_dhhuo"hhuo-txx2==2 IIQ2::.r6   c                    | j                   j                  j                  |d|      \  }}}	}
}|j                         9| j                  j
                  d   d   j                  t        ||||||             t        j                  d   }t        j                  |      }| j                  j                  t        j                         | j                  j                  t        j                  |      t        j                   |      t        j                   |       t        j                  |      ||t        j"                         | j%                  ||j&                         d}d\  }}| j                  j                  t        j(                         |
D ]  \  }}}}}}| j                  j*                  j-                  |||      \  }}t/        |j0                  |j2                        }| j5                  ||d||       ||}}|||f|k7  rJ| j                  j                  | j                  j7                  ||      |t        j8                         |||f}| j                  j                  | j;                  |g|      t        j<                          | j                  j                  t        j>                         |D ]a  \  }}}}| j                  j                  t        j                  ||||t        j@                  t        jB                  t        jD                         c | j                  j                  t        jD                         y )Nr  r  r#   r  rs  r  r   )#
_text2pathmathtext_parserparser   r   r  r*   r   r\   ra  rx   ry   r  r  rF  r{   rz   r8  r  r  r2  r  track_glyphr   ra  r  rc  rc  rI  _encode_glyphsrL  r3  r5  r+  rG  )r   r   rB   r|   r2   r  r   r}   r~   descentglyphsrectsr  r   	prev_fontr`  ra  r  fontsizer  r  oxoyr  subset_charcoder  s                             r4   draw_mathtextzRendererPdf.draw_mathtextm  s    OO++11!R> 	.vw ::<#II""2&q)001EAq%20 1 <</ LL		"		!dhhqk((1+txx{Ar//	1 	b"''"	
d		':@ 	&6D(E;B,0II,H,H,T,Te[-*)L/ T__=IB46R$D<2i?		  !3!3I|!Lh!#0%|X=	IIT00/1BHMWW&	& 			% &+ 	A!BE6IIRXXr2uf\\277BKKA	A
 			%r6   )mtextc          
       
   | j                         }|j                         }	|j                  ||	      }
t        j                  |
d      5 }|\  }d d d        |j                         M| j                  j                  d   d   j                  t        |||j                  |j                  |             d g }}j                  D ]  }|j                  |k7  rN| j                  j                  |j                        }|d||j                  j                  ggz  }|j                  }|d|j                   |j"                  t%        |j&                  g      g|j                   |j                  z   ggz  }| j                  j(                  j+                  |j                  |j&                  |j,                          d\  }}}	|t/        |      dz
  k  r|||dz    \  }}|d   dk(  r|d   }	n|d   |d   cxk(  rdk(  r~n n{|d   |d   k(  rp|d	   |d   z
  }t1        |      d
k  r-|d   dxx   |d   d   z  cc<   |d	xx   |d	   |d   z
  z  cc<   n#|dxx   |dz  |	z  |d   d   gz  cc<   |d	   |d	<   ||dz   = |dz  }|t/        |      dz
  k  rt3               j5                  |      j7                  ||      }| j9                  ||j:                         | j                  j=                  t>        j@                         d\  }}}}|D ]  }|d   dk(  r2| j                  j=                  |d   |d   t>        jB                         =|d   dk(  r|jE                  |d   |d   f      \  }}| jG                  |||||       ||}}t/        |d         dk(  r1| j                  j=                  |d   d   t>        jH                         | j                  j=                  |d   t>        jJ                         J  | j                  j=                  t>        jL                         | jO                         }|jQ                  |       |jS                  d       tT        jV                  tT        jX                  tT        jX                  tT        jX                  tT        jZ                  g}|j\                  D ]L  \  }}}}tU        ||g||z   |g||z   ||z   g|||z   gddgg|      } | j_                  || ||j:                         N y # 1 sw Y   xY w)Nr  r  r#   r  r  )r   r   Nrv  r   r  g?rw  g     @@r  )0get_texmanagerget_size_in_pointsmake_dvir   Dvir   r   r  r*   r   r}   r~   r  r  re  sizerB   r|   r   r  r  rh  indexr)   absr   
rotate_degr   r  r  r  r  r2  rI  r  rc  rL  rM  r3  r  copy_propertiesset_linewidthr    MOVETOLINETO	CLOSEPOLYboxesr%  )!r   r   rB   r|   r2   r  r   rs  
texmanagerrn  dvifiledvir  oldfontseqr  r  r1   curxeltnxtr  mytranscuryr`  ra  boxgcr  x1y1rS  r  r?  s!                                    r4   draw_texzRendererPdf.draw_tex  s   ((*
**,%%a2[["% 	ED	 ::<#II""2&q)001EAq$**dkk52: ; RII 	XDyyG#))//		:$))..9::))VTVVTVVeTZZL.A-BDFF4::DUVWWCII((44TYY

DJJW	X '4#c(1*n1QqSzHC1vq6Q3q6+V+A#a&0@Q#a&v;$F2J#a&)+JFc!fSVm+FFvf}X5s1vayAAF VCF!HFA #c(1*n" *''.88A> 	b"''"		'!+dD$ 	C1v		  QQ?Q6!$..AA/?@
d##D$tTB!4ds1v;!#II$$SVAY8II$$SVR[[9u	 			% b!A;;T[[$++>># JJ 	:LBAq"bBqD":1bd|b"Q$ZQ!")+DNN5$9	:U	 	s   	S33S=c                 P    |dv rt        |      S dj                  d |D              S )Nr  r6   c              3   @   K   | ]  }|j                  d d        yw)rv  bigN)to_bytes)r   r  s     r4   r   z-RendererPdf._encode_glyphs.<locals>.<genexpr>  s     EUq%0Es   )r   r+   )r   r^  r  s      r4   ri  zRendererPdf._encode_glyphs  s)    v= xxEfEEEr6   c                 8    dddd|   }|j                  |d      S )Ncp1252r   r  )r#   rw  r!  rZ   )r   )r   r2   r  r  s       r4   encode_stringzRendererPdf.encode_string  s$    I:>xHxx),,r6   c	           
      B
     j                  ||j                         |r j                  ||||||      S |j                         |!|j	                         }	|j                         }
nd x}	}
t        j                  d   rÉ j                  |      } j                  j                  t        j                   j                  j                  |      t        j                          j                  |||        j                  j                   j!                  |d      t        j"                  t        j$                         nd j'                  |      }t        j                  d    fd} j                  j                  t        j(                         t+        j,                  |      } j                  j                  t+        j.                  |      t+        j0                  |      t+        j0                  |       t+        j.                  |      ||t        j2                         g }d }d} j                  j                  t        j                         t5        j6                  |||	|
      D ]  } j                  j8                  j;                  |j<                  |j>                  |j@                        \  }}|j<                  |f|k7  r ||       tC        |j<                  jD                  |j<                  jF                        } j                  j                  ||      } j                  j                  |t        j                          j                  |jH                  dd|dd       |j<                  |f}|jH                  }|jJ                  r< ||        j                  j                  |jJ                  t        jL                         |jN                  r|jQ                  |jN                         |jQ                  |       |jJ                  s ||        j                  j                  dt        jL                           ||        j                  j                  t        j$                          j                  j                  t        jR                         |jU                         g|jW                  ||	|
       |jY                         \  }} j                  jZ                  d   d   jQ                  t]        ||||d	z  |d	z  |             y y )
NrR  r#   )r  r  c           
      6   | sy j                   j                  t        j                  | t              D cg c]1  \  }}|t
        k(  rdt        |      z  z  nj                  |      3 c}}t        j                         | j                          y c c}}w )Ni)r   r  r   groupbyr   r   rZ  ri  r  rM  clear)kerns_or_charstpgrouprn  r  r   s      r4   output_singlebyte_chunkz6RendererPdf.draw_text.<locals>.output_singlebyte_chunk 	  s    %		  
 '0&7&7&MO"U :<ued5k)H4--eX>? O KK! $$&	Os   6B
r   )featureslanguager  rx  )/r  r  rr  rv  get_fontfeaturesget_languager\   ra  _get_font_afmr   r  r  r2  rc  rI  rc  r  rL  r3  _get_font_ttfrF  rx   ry   r{   rz   r8  r
   layoutr  rh  	ft_objectr  r  r   ra  r  rB   r|   rK  	prev_kernr*   rG  r   set_textget_width_heightr  r   )r   r   rB   r|   r2   r  r   ismathrs  r  r  r  r  r   singlebyte_chunkrm  prev_start_xitemr^  r  r  ft_namer}   r~   rn  r  s   `                       @@r4   	draw_textzRendererPdf.draw_text  s   
 	b"''"%%b!Q4??**,--/H))+H"&&Hx <<,-%%d+DIIR]]!YY//5xP1e,IIT//A/>WWbkk3  %%d+D||N3H
' IIRXX&U#AIITXXa[$((1+"hhqk\488A;2#3#35  "ILIIR]]+%,,Qx6>@ 5#'99#?#?#K#KNNDIIt/?/?$A NNF+y8+,<= ()=)=)-)B)B!DI"ii00FCGII$$WhF''1lAqI!% 8I#'66L66+,<=II$$TVVR[[9>>$++DNN; ''166+,<=II$$Q4+5, $$45IIR[[)IIR[[)::<#MM!hMB 113ME6II""2&q)001EAq%"*frk52: ; $r6   c                 ,    t        | j                        S r>   )r  r   r   s    r4   r  zRendererPdf.new_gcX	  s    !$)),,r6   r>   r   r  )r   r   r   r   _get_data_pathrY  _use_afm_rc_namer   rE  r  r  r  r%  r,  rQ  r^  rc  rr  r  ri  r  r  r  r  r  s   @r4   rX  rX  R  s    (E(()=>M+#.+6#@<* >Bnb "$L& P/+&Z ;? R:hF
-_;B-r6   rX  c            
            e Zd Z fdZd Zd Zd Zd Zdddd	Zdddd
Z	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zdefdefde
fdefdefdefdefdefdeff	Zd Z fd Zd! Z xZS )"r  c                 Z    t         |           d| _        d| _        || _        d | _        y )Nr  r  )r  r   r  r  r   parent)r   r   r  s     r4   r   zGraphicsContextPdf.__init___	  s+    )!+	r6   c                 N    t        | j                        }|d= |d= t        |      S )Nr   r  )r   __dict__repr)r   rs   s     r4   r   zGraphicsContextPdf.__repr__f	  s'    fIhKAwr6   c                     | j                   dkD  xr= | j                  dkD  xr, t        | j                        dk  xs | j                  d   dk7  S )z
        Predicate: does the path need to be stroked (its outline drawn)?
        This tests for the various conditions that disable stroking
        the path, in which case it would presumably be filled.
        r   rw  r  )rR  r  r)   r  r   s    r4   r*  zGraphicsContextPdf.strokel	  sK     !# =a =TYY1$;		!(;	>r6   c                     t        |      r|d   }n| j                  }| j                  xs |duxr t        |      dk  xs |d   dk7  S )z
        Predicate: does the path need to be filled?

        An optional argument can be used to specify an alternative
        _fillcolor, as needed by RendererPdf.draw_markers.
        r   Nrw  r  )r)   r  _hatch)r   argsr  s      r4   r+  zGraphicsContextPdf.fillw	  sX     t9aJJ A4' @j/Q&>*Q-3*>	Br6   c                 f    t         j                  | j                         | j                               S )zo
        Return the appropriate pdf operator to cause the path to be
        stroked, filled, or both.
        )r  r.  r+  r*  r   s    r4   r#  zGraphicsContextPdf.paint	  s!    
 }}TYY[$++-88r6   r   r#   rv  )buttr}  
projecting)miterr}  bevelc                 @    | j                   |   t        j                  gS r>   )r  r  rD  r   styles     r4   capstyle_cmdzGraphicsContextPdf.capstyle_cmd	  s    u%r}}55r6   c                 @    | j                   |   t        j                  gS r>   )r  r  rC  r  s     r4   joinstyle_cmdz GraphicsContextPdf.joinstyle_cmd	  s    &77r6   c                 &    |t         j                  gS r>   )r  rN  )r   r}   s     r4   linewidth_cmdz GraphicsContextPdf.linewidth_cmd	  s    r''r6   c                 P    |\  }}|g }d}t        |      |t        j                  gS r  )r   r  rB  )r   dashesr  dashs       r4   dash_cmdzGraphicsContextPdf.dash_cmd	  s/    <DFT
FBJJ//r6   c                 \    | j                   j                  |      }|t        j                  gS r>   )r   r2  r  rE  )r   r0  forcedeffective_alphasrL   s        r4   	alpha_cmdzGraphicsContextPdf.alpha_cmd	  s&    yy##$45bll##r6   c                 6   |sB| j                   | j                  | j                         S t        d      t        j                  gS || j                   ||f}| j
                  j                  |      }t        d      t        j                  |t        j                  gS )Nr  r  )r  fillcolor_cmdrK   r  r?  r   rP  rA  )r   rM  hatch_colorhatch_linewidthrJ  rL   s         r4   	hatch_cmdzGraphicsContextPdf.hatch_cmd	  s    *))$//::[)2+E+EFF&PK99))+6DOR%?%?"//1 1r6   c                     t         j                  d   rg S |d   |d   cxk(  r|d   k(  rn n|d   t        j                  gS g |d d t        j                  S Nzpdf.inheritcolorr   r#   rv  rw  )r\   ra  r  r:  r<  r   r  s     r4   rgb_cmdzGraphicsContextPdf.rgb_cmd	  s^    <<*+Iq6SV%s1v%FB--../S!W/b..//r6   c                     |t         j                  d   rg S |d   |d   cxk(  r|d   k(  rn n|d   t        j                  gS g |d d t        j                  S r  )r\   ra  r  r;  r=  r  s     r4   r  z GraphicsContextPdf.fillcolor_cmd	  sb    ;#,,'9:IVs1v'Q'FB00112S!W2b1122r6   c                     t        | j                        }|j                  |        | j                  |_        || _        t        j
                  gS r>   )r  r   r}  r  r  rF  )r   r  s     r4   r/  zGraphicsContextPdf.push	  s<    #DII.t$zr6   c                     | j                   J | j                  | j                          | j                   j                   | _         t        j                  gS r>   )r  r}  r  rG  r   s    r4   r1  zGraphicsContextPdf.pop	  sA    {{&&&T[[)kk((}r6   c                    g }| j                   | j                  f||fk7  rU| j                  I|j                  | j	                                | j                   | j                  f||fk7  r| j                  I| j                   | j                  f||fk7  s| j                  |j                  | j                                | j                   |k7  r?|j                  |t        j                  t        j                  t        j                  g       | j                  |k7  r[|j                         \  }}|j                  t        j                  ||d      t        j                  t        j                  gz          |S )z1Set clip rectangle. Calls `.pop()` and `.push()`.Fr]  )	_cliprect	_clippathr  extendr1  r/  r  r5  rO  r,  get_transformed_path_and_affiner  r_  )r   cliprectclippathr  r?  affines         r4   clip_cmdzGraphicsContextPdf.clip_cmd	  s#   /Hh3GGKK+KK
# /Hh3GGKK+ ^^T^^,80DD#KK		$~~)Xr||RWWbjjIJ~~)'GGIf**4%*HWWbjj)*+ r6   )r  r  )r  r
  r  )	_capstyle)r  )
_joinstyle)rR  )_dashes)r  )r  _hatch_color_hatch_linewidthc           
      ~   g }d}| j                   D ]  \  }}d}|D ]6  }t        | |      }t        ||      }		 ||	||	u}nt        ||	k7        }|s6 n |j                  dk(  r|rd}|sY|j                  dk(  rd}|D cg c]  }t        ||       }	}|j                   || g|	        |D ]  }t        | |t        ||               |S # t        $ r` t	        j
                  |      }t	        j
                  |	      }	|j                  |	j                  k7  xs t	        j                  ||	k7        }Y w xY wc c}w )z}
        Copy properties of other into self and return PDF commands
        needed to transform *self* into *other*.
        Fr  Tr  )commandsr  r   r   r   r)  ry  anyr   r  setattr)
r   r   r  fill_performedparamscmd	differentpourstheirss
             r4   r  zGraphicsContextPdf.delta	  s\   
 == 	8KFCI tQ' *	9|v~$($6	$($8	 " ||{*~ 	<<?2%)N5;<'%+<<C.v./ 8AD!WUA%6787	8: ' " 9::d+DZZ/F!%v||!; "8!#!7 9 =s   C?D:A&D76D7c                     t         |   |       t        |d| j                        }t        |d| j                        }|| _        || _        y)z5
        Copy properties of other into self.
        r  r  N)r  r}  r  r  r  )r   r   r  r  r  s       r4   r}  z"GraphicsContextPdf.copy_properties
  sL     	&E<A	"5*=#'#9#9;#!1r6   c                 z    g }| j                   ,|j                  | j                                | j                   ,|S )zB
        Make sure every pushed graphics state is popped.
        )r  r  r1  )r   r  s     r4   rE  zGraphicsContextPdf.finalize 
  s6     kk%KK
# kk%r6   )r   r   r   r   r   r*  r+  r#  r  r  r  r  r  r  r  r  r  r  r/  r1  r  r  r  r}  rE  r  r  s   @r4   r  r  ]	  s    	>B9 Qa8Iq15J68(0$
103, 
$X.	99E	&	-(	-(	-(	x 	G	7CH$L	2r6   r  c                       e Zd ZdZ ej
                  ddd      dd       Zd Zd	 Zd
 Z	d Z
d ZddZd Zg dfdZy)PdfPagesa=  
    A multi-page PDF file.

    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> # Initialize:
    >>> with PdfPages('foo.pdf') as pdf:
    ...     # As many times as you like, create a figure fig and save it:
    ...     fig = plt.figure()
    ...     pdf.savefig(fig)
    ...     # When no figure is specified the current figure is saved
    ...     pdf.savefig()

    Notes
    -----
    In reality `PdfPages` is a thin wrapper around `PdfFile`, in order to avoid
    confusion when using `~.pyplot.savefig` and forgetting the format argument.
    rQ  
keep_emptyzThis parameter does nothing.)addendumNc                 .    || _         || _        d| _        y)a  
        Create a new PdfPages object.

        Parameters
        ----------
        filename : str or path-like or file-like
            Plots using `PdfPages.savefig` will be written to a file at this location.
            The file is opened when a figure is saved for the first time (overwriting
            any older file with the same name).

        metadata : dict, optional
            Information dictionary object (see PDF reference section 10.2.1
            'Document Information Dictionary'), e.g.:
            ``{'Creator': 'My software', 'Author': 'Me', 'Title': 'Awesome'}``.

            The standard keys are 'Title', 'Author', 'Subject', 'Keywords',
            'Creator', 'Producer', 'CreationDate', 'ModDate', and
            'Trapped'. Values have been predefined for 'Creator', 'Producer'
            and 'CreationDate'. They can be removed by setting them to `None`.
        N)	_filename	_metadata_file)r   r  r  rc   s       r4   r   zPdfPages.__init__?
  s    . "!
r6   c                     | S r>   r   r   s    r4   	__enter__zPdfPages.__enter__Z
  s    r6   c                 $    | j                          y r>   )rG  )r   exc_typeexc_valexc_tbs       r4   __exit__zPdfPages.__exit__]
  s    

r6   c                 ~    | j                   &t        | j                  | j                        | _         | j                   S )Nrc   )r  r  r  r   r   s    r4   _ensure_filezPdfPages._ensure_file`
  s,    :: $..IDJzzr6   c                     | j                   <| j                   j                          | j                   j                          d| _         yy)z_
        Finalize this object, making the underlying file a complete
        PDF file.
        N)r  rE  rG  r   s    r4   rG  zPdfPages.closee
  s:    
 ::!JJ!JJDJ "r6   c                 6    | j                         j                  S )z
        Return a modifiable information dictionary object
        (see PDF reference section 10.2.1 'Document Information
        Dictionary').
        )r  r  r   s    r4   infodictzPdfPages.infodicto
  s       "+++r6   c                     t        |t              sR|t        j                         }nt        j                  |      }|t        d|       |j                  j                  } |j                  | fddd| y)a5  
        Save a `.Figure` to this file as a new page.

        Any other keyword arguments are passed to `~.Figure.savefig`.

        Parameters
        ----------
        figure : `.Figure` or int, default: the active figure
            The figure, or index of the figure, that is saved to the file.
        Nz
No figure r  )r  rb   )	r?   r   r   
get_activeget_fig_managerr   canvasfiguresavefig)r   r  kwargsmanagers       r4   r  zPdfPages.savefigw
  so     &&)~..*--f5 :fX!677^^**FtCE5CFCr6   c                 H    t        | j                         j                        S )z=Return the current number of pages in the multipage pdf file.)r)   r  r  r   s    r4   get_pagecountzPdfPages.get_pagecount
  s    4$$&//00r6   r  c                 D    | j                         j                  ||       y)z
        Add a new text note to the page to be saved next. The optional
        positionRect specifies the position of the new note on the
        page. It is outside the page per default to make sure it is
        invisible on printouts.
        N)r  r  )r   r  r  s      r4   attach_notezPdfPages.attach_note
  s     	''l;r6   rs  r>   )r   r   r   r   r	   r  r   r  r  r  rG  r  r  r  r  r   r6   r4   r  r  *
  s`    ( T6<$BDD2
,D,1 .@ <r6   r  c                   >     e Zd ZdZddiZd ZddddZ fdZ xZS )	FigureCanvasPdfr  r  zPortable Document Formatc                      y)Nr  r   r   s    r4   get_default_filetypez$FigureCanvasPdf.get_default_filetype
  s    r6   N)bbox_inches_restorerc   c                   | j                   j                  }d| j                   _        | j                   j                         \  }}t        |t              r|j                         }nt        ||      }	 |j                  ||       t        | j                   |||t        ||||      |      }| j                   j                  |       |j                          t        |t              s|j                          t        |t              r|j                          y |j                          y # t        |t              r|j                          w |j                          w xY w)Nr  r
  )r  )r  dpiget_size_inchesr?   r  r  r  r  r   rX  drawrE  r  rG  )	r   r  r  rc   r!  r}   r~   r   renderers	            r4   	print_pdfzFigureCanvasPdf.print_pdf
  s    kkoo335vh)((*D8h7D	LL'(UFCD#vu5$79H KKX&h1(H- 

 (H- 

s   4BD* *3Ec                 T    | j                   j                          t        |          S r>   )r  draw_without_renderingr  r#  )r   r  s    r4   r#  zFigureCanvasPdf.draw
  s    **,w|~r6   )	r   r   r   	fixed_dpi	filetypesr  r%  r#  r  r  s   @r4   r  r  
  s0     I23I '+T4 r6   r  c                       e Zd ZeZy)_BackendPdfN)r   r   r   r  FigureCanvasr   r6   r4   r+  r+  
  s    "Lr6   r+  )K   r  )]r   r   r   r   enumr   	functoolsr   ior   r   loggingrx   rU   r  r  r   rp   r  rb  numpyr   PILr   
matplotlibr\   r	   r
   r   r   r   matplotlib._pylab_helpersr   matplotlib.backend_basesr   r   r   r   r   !matplotlib.backends.backend_mixedr   matplotlib.figurer   matplotlib.font_managerr   r   r   rV  matplotlib._afmr   matplotlib.ft2fontr   r   r   r   matplotlib.transformsr   r   matplotlib.pathr    matplotlib.datesr!   r"   r  r$   	getLoggerr   r[  r5   rj   rv   r   r   r   r@   	maketransr   r   r   rK   r  r  rS  r  r  RendererPDFPSBaserX  r  r  r  FigureManagerPdfexportr+  r   r6   r4   <module>rD     s       $     	   
       F F )  @ $ S S  H H 4      w"d,Tn0,"*04 }}
u5UD EF#R .      B B# B#JZ$ Z$z0`C `CF'H-/33 H-VJ, JZn< n<b%& %P %  
#( # #r6   