)EjYGUdZddlZddlZddlmZddlmZddlmZddl m Z m Z ddl m Z m Z mZmZmZmZmZmZmZmZdd lmZmZmZmZe r dd lmZdd lmZd eee fd eee ffdZ! dFdeee fdedededede"d efdZ#de$ed efdZ%de&d efdZ' dGde(e)zezd ed!d"ee&d efd#Z*d$e$ed e$efd%Z+Gd&d'eZ,Gd(d)eZ-Gd*d+Z.Gd,d-e.Z/ dGd.eee fd/ed!d0ee&d eee ffd1Z0d2ed efd3Z1Gd4d5eZ2Gd6d7Z3Gd8d9e$e Z4Gd:d;eZ5Gd<d=e5Z6eGd>d?eZ7ee,e-ee)e"ded@fZ8ee9dA<e8edBzedCze7zZ:ee9dB<dDe:d e,fdEZ;dS)Ha Classes & functions that represent core elements of the PDF syntax Most of what happens in a PDF happens in objects, which are formatted like so: ``` 3 0 obj <> endobj ``` The first line says that this is the third object in the structure of the document. There are 8 kinds of objects (Adobe Reference, 51): * Boolean values * Integer and real numbers * Strings * Names * Arrays * Dictionaries * Streams * The null object The `<<` in the second line and the `>>` in the line preceding `endobj` denote that it is a dictionary object. Dictionaries map Names to other objects. Names are the strings preceded by `/`, valid Names do not have to start with a capital letter, they can be any ascii characters, # and two characters can escape non-printable ascii characters, described on page 57. `3 0 obj` means what follows here is the third object, but the name Type (represented here by `/Type`) is mapped to an indirect object reference: `0 obj` vs `0 R`. The structure of this data, in python/dict form, is thus: ``` third_obj = { '/Type': '/Page'), '/Parent': iobj_ref(1), '/Resources': iobj_ref(2), '/Contents': iobj_ref(4), } ``` Content streams are of the form: ``` 4 0 obj <> stream 68656c6c6f20776f726c64 endstream endobj ``` The contents of this module are internal to fpdf2, and not part of the public API. They may change at any time without prior warning or any deprecation period, in non-backward-compatible ways. N)ABC)hexlify) BOM_UTF16_BE)datetimetimezone) TYPE_CHECKINGAnyDictMappingOptionalProtocolSequence TypeAliasUnionruntime_checkable)Number NumberClass escape_parens number_to_str) InheritType)StandardSecurityHandlerdreturnc>d|DS)Nci|] \}}||| Sr).0kvs =C:\PYTHON\MyICR_Suite\python\Lib\site-packages\fpdf/syntax.py z&clear_empty_fields..\s# , , ,TQ! ,Aq , , ,)items)rs r!clear_empty_fieldsr%[s , ,QWWYY , , ,,r#<<>>  Fdict_ open_dict close_dict field_joinkey_value_joinhas_empty_fieldsc|rt|}d||fd|D|gS)a{format dictionary as PDF dictionary @param dict_: dictionary of values to render @param open_dict: string to open PDF dictionary @param close_dict: string to close PDF dictionary @param field_join: string to join fields with @param key_value_join: string to join key to value with @param has_empty_fields: whether or not to clear_empty_fields first. c3fK|]+\}}|t|fV,dSN)joinstr)rrr r.s r! z+create_dictionary_string..vs?WWAN//CFF <<WWWWWWr#)r%r4r$)r*r+r,r-r.r/s ` r!create_dictionary_stringr7_sg"*"5)) 77  OOWWWWWWW W W    r#list_c4dd|dS)z#format list of strings as PDF array[r)])r4)r8s r!create_list_stringr<|s !sxx ! ! !!r#nc |dS)z:format an indirect PDF Object reference from its id numberz 0 Rr)r=s r!iobj_refr?s:::r#streamencryption_handlerrobj_idct|ttfrt|d}|r|J|||dd|dgS)Nlatin-1r(r@ endstream) isinstance bytearraybytesr5encryptr4)r@rArBs r! create_streamrJsm &9e,--(VY''3!!!""66222 99h 4 5 55r# draw_commandscdg|zdgzS)z Wrap a series of draw commands (list of strings) in a local context marker, so that changes to draw style only apply to these commands. qQr)rKs r!wrap_in_local_contextrOs 5= C5 ((r#ceZdZdZdS)RawzVstr subclass signifying raw data to be directly emitted to PDF without transformation.N)__name__ __module__ __qualname____doc__rr#r!rQrQs````r#rQc eZdZdZejdededdDzdzZ d de d d e e d e fd Z dS)NamezVstr subclass signifying a PDF name, which are emitted differently than normal strings.s[^c#"K|] }|dv|V dS)s ()<>[]{}/%#\Nr)rr s r!r6zName.s-MMA1z Name.serialize..sh1a(r#/)NAME_ESCsubencodedecode)selfr\r]escapeds r! serializezName.serializesE -## ( ($++--  &(( 7}}r#NN) rRrSrTrUrecompilerHrangercr intr5rirr#r!rWrWs``rz MMr3MMMMMMPTTH BF!%#$=># r#rWceZdZddZedefdZejdeddfdZedefdZ dde e ee fd e d defd Z defd Z dd e d de ee ffdZdS) PDFObjectrNcd|_dSr3_idrgs r!__init__zPDFObject.__init__s "&r#cV|jt|jjd|jS)Nz has not been assigned an ID yet)rsAttributeError __class__rRrts r!idz PDFObject.ids6 8  >*LLL xr#r=c||_dSr3rr)rgr=s r!ryz PDFObject.ids r#c*t|jSr3)r?ryrts r!refz PDFObject.refs   r#obj_dictr\rcg}||jd|d|s||}|t|dd|d|}|r"|t ||dd|S)z8Serialize the PDF object as an obj<>endobj text blockz 0 objr&r1)r+r,r'endobjr()appendry_build_obj_dictr7content_streamrJr4)rgr}r\outputrs r!rizPDFObject.serializes  ((())) d ?++,=>>H .x2RTUUUVVV d,,..  9 MM-77 8 8 8 hyy   r#cdS)zPSubclasses can override this method to indicate the presence of a content streamr#rrts r!rzPDFObject.content_streamssr#security_handlercdtfdtD|jS)z Build the PDF Object associative map to serialize, based on this class instance properties. The property names are converted from snake_case to CamelCase, and prefixed with a slash character "/". c2i|]}|t|Sr)getattr)rkeyrgs r!r"z-PDFObject._build_obj_dict..s% : : :S'$$$ : : :r#r\r])build_obj_dictdirry)rgrs` r!rzPDFObject._build_obj_dictsA : : : :D : : :.G    r#)rNrjr3)rRrSrTrupropertyrnrysetterr5r|r r objectrirHrrrr#r!rprpsQ ''''CXYCDY!S!!!X! 15AE!!4V ,-!$$=>! !!!!( GK   ()B C  c6k        r#rpceZdZdZd deezdeffd ZdefdZ dd e e e e fd e d de ffd Z xZS)PDFContentStreamFcontentscompressc t|rtj||jnt ||_|rtdnd|_t|j|_ dS)N)level FlateDecode) superruzlibr_COMPRESSION_LEVELrH _contentsrWfilterlenlength)rgrrrxs r!ruzPDFContentStream.__init__sz  !DM($*A B B B Bx  .6?d=)))4 $.)) r#rc|jSr3)rrts r!rzPDFContentStream.content_streams ~r#Nr}r\rcP|r|rJt|jttfs|jd|_||j|j|_t|j|_t ||S)NrD) rFrrGrHreencrypt_streamryrrrri)rgr}r\rxs r!rizPDFContentStream.serializes  .  <dny%.@AA B!%!6!6y!A!A.==dndgVVDNdn--DKww  +<===r#Frj)rRrSrTrrHrGboolrurr r r5rri __classcell__rxs@r!rrs**!2*d****** 15AE > >4V ,- >$$=> > > > > > > > > > > >r#r key_valuesr\r]ci}|D]\}}t|s|ds|dvs|0t|dr|j}t |t r|j}n^t|dr|||}n6t |tr!t| }||dt|<|S)z Build the PDF Object associative map to serialize, based on a key-values dict. The property names are converted from snake_case to CamelCase, and prefixed with a slash character "/". _)ryr|Nvaluerirrb) r$callable startswithhasattrrrFrpr|rirr5lower camel_case)rr\r]r}rrs r!rrsH &&((00 U UOO ~~c"" m##}  5' " " KE eY ' ' 'IEE UK ( ( 'OO"3W$EEt $ $ 'JJ$$&&E*/&Z__&&'' Or# snake_casecddd|DS)Nr1c3&K|] }|dk|V dS)rNr)rxs r!r6zcamel_case..8s&==AHH1HHHH==r#)r4title)rs r!rr7s/ 77==j..00=== = ==r#cveZdZUdZdZeed< d dededdffd Z d de d d e e defd Z xZ S) PDFStringTFrIcontentrcZt||}||_|S)z Args: content (str): text encrypt (bool): if document encryption is enabled, should this string be encrypted? )r__new__rI)clsrrIrgrxs r!rzPDFString.__new__Cs( wwsG,,  r#Nr\rr]c|r!|jr|J|||S |ddt|dS#t$rYnwxYw|jrCt t|dzd}d|dSd|dddS) Nascii()z utf-16-berD<>zUTF-16) rIencrypt_stringrerUnicodeEncodeErrorUSE_HEX_ENCODINGrrrf)rgr\r]hex_strs r!rizPDFString.serializeMs  C C&&&$33D'BB B  KK -}T**--- -!    D    "lT[[-E-EEFFMMiXXG!w>>> !=4;;x((// ::====s'A AArrj) rRrSrTrrIr__annotations__r5rr rnrirrs@r!rr;sGT cD[BF!%>>#$=>>#> >>>>>>>>r#rc heZdZ d dedededdfdZdefdZ dd ed d ee defd Z dS)PDFDateFdatewith_tzrIrNc0||_||_||_dS)z Args: date (datetime): self-explanatory with_tz (bool): should the timezone be encoded in included in the date? encrypt (bool): if document encryption is enabled, should this string be encrypted? NrrrI)rgrrrIs r!ruzPDFDate.__init__cs   r#c8d|jd|jd|jdS)NzPDFDate(z , with_tz=z , encrypt=rrrts r!__repr__zPDFDate.__repr__ps)V$)VVt|VVt|VVVVr#r\rr]c.|jr[|jjsJ|jjtjkr d|jd}n2d|jd}|dddz|ddzdz}n d|jd}|r!|jr|sJ|||Sd|dS) NzD:z %Y%m%d%H%M%SZz%Y%m%d%H%M%S%z'z %Y%m%d%H%M%Srr)rrtzinforutcrIr)rgr\r]out_strs r!rizPDFDate.serializess < 49# # ##y8<//8ty8889ty999!#2#,,wrss|;cA349333G  F FNN7$33GWEE E7~~~r#)FFrj) rRrSrTrrrur5rr rnrirr#r!rrbsEJ   '+ >B      W#WWWW BF!%#$=># r#rc@eZdZ ddeddeedefdZdS)PDFArrayNr\rr]rcJtd|Drd|}nntd|Dr!dd|D}n4g}|D]}t|tr||j3t |dr+||||nt|tr5|t| t|ttfr#|t||t|d|}d|d S) Nc3@K|]}t|tVdSr3)rFr5relems r!r6z%PDFArray.serialize..s,66z$$$666666r#r)c3NK|] }t|ttfV!dSr3)rFrnfloatrs r!r6z%PDFArray.serialize..s0AADD3,//AAAAAAr#c34K|]}t|VdSr3)r5rs r!r6z%PDFArray.serialize..s('C'CdD 'C'C'C'C'C'Cr#rirr(r:r;) allr4rFrprr|rrirr5rrnr)rgr\r]serialized_elemsserialized_chunksrs r!rizPDFArray.serializes 66666 6 6 <"xx~~   AADAAA A A <"xx'C'Cd'C'C'CCC  +-  8 8dI.. 8%,,TX6666T;// 8%,,.?'  d++8%,,SYY__->->????sEl338%,,SYY7777%,,SYY7777#yy):;; &#&&&&r#rjrRrSrTr rnr5rirr#r!rrs]BF!%''#$=>'#' ''''''r#rc@eZdZ ddeddeedefdZdS) DestinationNr\rr]rctr3)NotImplementedError)rgr\r]s r!rizDestination.serializes "!r#rjrrr#r!rrs]BF!%""#$=>"#" """"""r#rc eZdZ ddedeededeezddf d Zd ede fd Z defd Z defd Z ddeddeedefdZ ddeedeedeedeeezddf dZdS)DestinationXYZrnullpagetopleftzoomrNcL||_||_||_||_d|_dSr3 page_numberrrrpage_refrgrrrrs r!ruzDestinationXYZ.__init__s,   '+ r#destct|tsdS|j|jko/|j|jko|j|jko|j|jkS)NF)rFrrrrr)rgrs r!__eq__zDestinationXYZ.__eq__sa$// 5   0 0 'DH$ ' TY& ' TY&  r#cPt|j|j|j|jfSr3)hashrrrrrts r!__hash__zDestinationXYZ.__hash__s"T%txDIFGGGr#c Xd|jd|jd|jd|jd|jd S)NzDestinationXYZ(page_number=z, top=z, left=z, zoom="z ", page_ref=rrrts r!rzDestinationXYZ.__repr__ssQT-=QQTXQQVZV_QQimirQQAEANQQQ Qr#r\rr]c t|jtrt|jdn|j}t|jtrt|jdn|j}|jsJd|jd|d|d|jd S)Nr:z /XYZ r)r;)rFrrroundrrr)rgr\r]rrs r!rizDestinationXYZ.serializes '1E&B&BQuTY""" $.tx$?$?MeDHa   TX}}A4=AAAAsAATYAAAAr#c|jr Jdt||jn|||jn|||jn|||jn|S)Nz7DestinationXYZ should not be copied after serialization)rrrr)rrrrrrrs r!replacezDestinationXYZ.replacest  E E D E E %)\!!tKS"l"l     r#)rrrj)NNNN)rRrSrTrnr rr5rurrrrrrirrr#r!rrs " , , ,e_ , , Ek ,  , , , , 6 d    H#HHHHQ#QQQQ BF!%BB#$=>B#B BBBB## $&*   sm e_ uo  sU{#         r#rceZdZdefdZdS)PrimitiveSerializablercdSr3rrts r!rizPrimitiveSerializable.serializesr#N)rRrSrTr5rirr#r!rrs!#3######r#rr PDFScalar PDFPrimitive)rWr primitivect|tr|St|tr|}n|d}nt|trdt |d}not|t rd|d}n@t|tr ddg|}nt|trt|}nt|ttfr&d d d |Dzd z}nt|trg}|D]^\}}t|t st#d |t'|d zt'|z_dd |zdz}nt)d|t|S)a Render a Python value as a PDF primitive type. Container types (tuples/lists and dicts) are rendered recursively. This supports values of the type Name, str, bytes, numbers, booleans, list/tuple, and dict. Any custom type can be passed in as long as it provides a `serialize` method that takes no arguments and returns a string. The primitive object is returned directly if it is an instance of the `Raw` class. Otherwise, The existence of the `serialize` method is checked before any other type checking is performed, so, for example, a `dict` subclass with a `serialize` method would be converted using its `pdf_repr` method rather than the built-in `dict` conversion process. Args: primitive: the primitive value to convert to its PDF representation. Returns: Raw-wrapped str of the PDF representation. Raises: ValueError: if a dictionary key is not a Name. TypeError: if `primitive` does not have a known conversion to a PDF representation. Nrrrrrfalsetruer:r)c34K|]}t|VdSr3)render_pdf_primitive)rvals r!r6z'render_pdf_primitive..#s+OOc 4S 9 9OOOOOOr#r;zdict keys must be Namesz<< r(z >>z,cannot produce PDF representation for value )rFrQrrir5rrHhexrrrlisttupler4dictr$rW ValueErrorrr TypeError)rr item_listrrs r!rrs4)S!!)233V$$&&   Is # #V0]9--000 Iu % %V'Y]]__''' It $ $V6"9- I{ + +Vy)) Ie} - -VsxxOOYOOOOOORUU It $ $ V! !))  HCc4(( < !:;;;   $S))C/2Fs2K2KK    9---5TyTTUUU v;;r#)r&r'r(r)Frj)rs<<<@ ''''''''                        DCCCCCCCCCCC4$$$$$$333333-'#v+.-73;3G---- "  3;       :"d3i"C"""" ?C  6 6   # 6 !:; 6 SM 6 6 6 6 6)c)tCy))))aaaaa#aaa3$> > > > > > > > B>>>>>y>>>F>B!!!S&[!! 9:!c]! #v+ !!!!H>3>3>>>>$>$>$>$>$>$>$>$>N""""""""J'''''tCy'''@"""""#"""7 7 7 7 7 [7 7 7 t$$$$$H$$$S$UD$ UV 9VVV~ $%& i9L9S999999r#