
    h                        S r SSKJrJr  SSKJrJr  SSK	r	SSK
r
SrSr " S S\5      rS	 rSS
 jrSS jrS r\
R&                  " S5      r\
R&                  " S5      r\
R&                  " S5      rSSSS.r0 rSS jrS r\S:X  a  \" 5         gg)aw  sstruct.py -- SuperStruct

Higher level layer on top of the struct module, enabling to
bind names to struct elements. The interface is similar to
struct, except the objects passed and returned are not tuples
(or argument lists), but dictionaries or instances.

Just like struct, we use fmt strings to describe a data
structure, except we use one line per element. Lines are
separated by newlines or semi-colons. Each line contains
either one of the special struct characters ('@', '=', '<',
'>' or '!') or a 'name:formatchar' combo (eg. 'myFloat:f').
Repetitions, like the struct module offers them are not useful
in this context, except for fixed length strings  (eg. 'myInt:5h'
is not allowed but 'myString:5s' is). The 'x' fmt character
(pad byte) is treated as 'special', since it is by definition
anonymous. Extra whitespace is allowed everywhere.

The sstruct module offers one feature that the "normal" struct
module doesn't: support for fixed point numbers. These are spelled
as "n.mF", where n is the number of bits before the point, and m
the number of bits after the point. Fixed point numbers get
converted to floats.

pack(fmt, object):
        'object' is either a dictionary or an instance (or actually
        anything that has a __dict__ attribute). If it is a dictionary,
        its keys are used for names. If it is an instance, it's
        attributes are used to grab struct elements from. Returns
        a string containing the data.

unpack(fmt, data, object=None)
        If 'object' is omitted (or None), a new dictionary will be
        returned. If 'object' is a dictionary, it will be used to add
        struct elements to. If it is an instance (or in fact anything
        that has a __dict__ attribute), an attribute will be added for
        each struct element. In the latter two cases, 'object' itself
        is returned.

unpack2(fmt, data, object=None)
        Convenience function. Same as unpack, except data may be longer
        than needed. The returned value is a tuple: (object, leftoverdata).

calcsize(fmt)
        like struct.calcsize(), but uses our own fmt strings:
        it returns the size of the data in bytes.
    )fixedToFloatfloatToFixed)tobytestostrNz1.2z4Copyright 1998, Just van Rossum <just@letterror.com>c                       \ rS rSrSrg)Error:    N__name__
__module____qualname____firstlineno____static_attributes__r
       H/var/www/html/env/lib/python3.13/site-packages/fontTools/misc/sstruct.pyr   r   :   s    r   r   c           
      B   [        U SS9u  p#n/ n[        U[        5      (       d  UR                  nUnUR	                  S5      (       a  USS  n[        UR                  5       5       Hh  u  pxX   n	X;   a  [        XU   5      n	O [        U	[        5      (       a  [        U	5      n	UR                  U	5         [        R                  " X8   U	5        Mj     [        R                  " U4[        U5      -   6 nU$ ! [         a  n
[        SU	< SX8   < SU< 35      U
eS n
A
ff = f)NT)keep_pad_byte>   zValue z does not fit in format z for )	getformat
isinstancedict__dict__
startswith	enumeratekeysfl2fistrr   appendstructpack	Exception
ValueErrortuple)fmtobjformatstringnamesfixeselementsstring_indexixnamevalueedatas               r   r"   r"   >   s   !*3d!CLHc4  llLs###AB'ejjl+	=%t-Es##ENE	KKU+ , ;;%/9:DK  	?DekSWX	s   6C55
D?DDc                    Uc  0 n[        U5      n[        U 5      u  p4n[        U[        5      (       a  UnOUR                  n[
        R                  " X15      n[        [        U5      5       H\  n[        UR                  5       5      U   n	Xx   n
X;   a  [        XU	   5      n
O![        U
[        5      (       a   [        U
5      n
XU	'   M^     U$ ! [         a     Nf = fN)r   r   r   r   r   r!   unpackrangelenlistr   fi2flbytesr   UnicodeDecodeError)r&   r1   r'   r(   r)   r*   dr+   ir.   r/   s              r   r4   r4   Y   s    
{4=D!*3L#tLL}}\0H3u:EJJL!!$=%t-Eu%%e $  J & s   :C
CCc                 >    [        U 5      n[        XS U U5      XS  4$ r3   )calcsizer4   )r&   r1   r'   lengths       r   unpack2r@   r   s'    c]F#GV}c*DM99r   c                 J    [        U 5      u  pn[        R                  " U5      $ r3   )r   r!   r>   )r&   r(   r)   r*   s       r   r>   r>   w   s    !*3L??<((r   z`\s*([A-Za-z_][A-Za-z_0-9]*)\s*:\s*([xcbB?hHiIlLqQfd]|[0-9]+[ps]|([0-9]+)\.([0-9]+)(F))\s*(#.*)?$z\s*([x@=<>!])\s*(#.*)?$z
\s*(#.*)?$bhl          c                 V   [        U SS9n  [        U    u  p#nX#U4$ ! [         Ga    [        R                  " SU 5      nSn0 n0 nU GHN  n[
        R                  U5      (       a  M   [        R                  U5      nU(       a*  UR                  S5      nUS:w  a  U(       a  [        S5      eO[        R                  U5      nU(       d  [        SU-  5      eUR                  S5      n	UR                  S	5      nU(       d  US:w  a  XU	'   UR                  S
5      (       aq  [        UR                  S
5      5      n
[        UR                  S5      5      nX-   nUS;  a  [        S5      e[        U   nXU	'   UR                  S5      S:X  d   eXU	'   X(-  nGMQ     X#U4[        U '    GNf = f)Nascii)encodingz[
;] r   xz a special fmt char must be firstzsyntax error in fmt: '%s'         rE   z)fixed point must be 8, 16 or 32 bits long   F)r   _formatcacheKeyErrorresplit_emptyREmatch_extraREgroupr   
_elementREint_fixedpointmappings)r&   r   r(   r)   r*   lineslinem
formatcharr.   beforeafterbitss                r   r   r      s   
g
&C#7%1#%6"UF %%E  !7#&D~~d##t$AWWQZ
$ BCC$$T* ;d BCCwwqzWWQZ
 J#$5",$K771:: _F
OE!>D;.#$OPP!4T!:J",$K771:,,,"'$K&L7 8 )6SC!7s    FF('F(c                     Sn [        S[        U 5      5         " S S[        5      nU" 5       nSUl        SUl        SUl        SUl        S	Ul        S
Ul        S
Ul	        SUl
        SUl        [        X5      n[        S[        U5      5        [        [        X5      5        U" 5       n[        XU5        [        [        U5      5        g )Na  
		# comments are allowed
		>  # big endian (see documentation for struct)
		# empty lines are allowed:

		ashort: h
		along: l
		abyte: b	# a byte
		achar: c
		astr: 5s
		afloat: f; adouble: d	# multiple "statements" are allowed
		afixed: 16.16F
		abool: ?
		apad: x
	zsize:c                       \ rS rSrSrg)_test.<locals>.foo   r
   Nr   r
   r   r   foorg      s    r   ri   i  i   a12345g      ?g      ?Tzdata:)printr>   objectashortalongabyteacharastrafloatadoubleafixedaboolr"   reprr4   vars)r&   ri   r<   r1   i2s        r   _testr{      s    C  
'8C=!f  	AAHAGAGAGAFAHAIAHAG<D	'4:	&
	B
3b	$r(Or   __main__r3   )F)__doc__fontTools.misc.fixedToolsr   r8   r   r   fontTools.misc.textToolsr   r   r!   rU   __version____copyright__r#   r   r"   r4   r@   r>   compiler[   rY   rW   r]   rS   r   r{   r   r
   r   r   <module>r      s   .` S 3  	F	I 	62:
) ZZ	
 ::01 ::m$3C0 &&R'T z	G r   