
    Mf                     >    d Z ddlmZmZ ddlmZ d
dZd
dZd
dZd	S )ui  
From BIFF8 on, strings are always stored using UTF-16LE  text encoding. The
character  array  is  a  sequence  of  16-bit  values4.  Additionally it is
possible  to  use  a  compressed  format, which omits the high bytes of all
characters, if they are all zero.

The following tables describe the standard format of the entire string, but
in many records the strings differ from this format. This will be mentioned
separately. It is possible (but not required) to store Rich-Text formatting
information  and  Asian  phonetic information inside a Unicode string. This
results  in  four  different  ways  to  store a string. The character array
is not zero-terminated.

The  string  consists  of  the  character count (as usual an 8-bit value or
a  16-bit value), option flags, the character array and optional formatting
information.  If the string is empty, sometimes the option flags field will
not occur. This is mentioned at the respective place.

Offset  Size    Contents
0       1 or 2  Length of the string (character count, ln)
1 or 2  1       Option flags:
                  Bit   Mask Contents
                  0     01H  Character compression (ccompr):
                               0 = Compressed (8-bit characters)
                               1 = Uncompressed (16-bit characters)
                  2     04H  Asian phonetic settings (phonetic):
                               0 = Does not contain Asian phonetic settings
                               1 = Contains Asian phonetic settings
                  3     08H  Rich-Text settings (richtext):
                               0 = Does not contain Rich-Text settings
                               1 = Contains Rich-Text settings
[2 or 3] 2      (optional, only if richtext=1) Number of Rich-Text formatting runs (rt)
[var.]   4      (optional, only if phonetic=1) Size of Asian phonetic settings block (in bytes, sz)
var.     ln or 
         2·ln   Character array (8-bit characters or 16-bit characters, dependent on ccompr)
[var.]   4·rt   (optional, only if richtext=1) List of rt formatting runs 
[var.]   sz     (optional, only if phonetic=1) Asian Phonetic Settings Block 
   )unicodeunicode_type    )packasciic                 j   t          | t                    r| }nt          | |          }t          |          }|dk    rt	          d          	 |                    d          }d}|}n9# t          $ r, |                    d          }d}t          |          dz  }Y nw xY wt          d||          |z   S )	N  #String longer than 32767 characterslatin1r   	utf_16_ler      z<HB
isinstancer   r   len	ExceptionencodeUnicodeEncodeErrorr   sencodinguslen_usencsflagn_itemss          P/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/xlwt/UnicodeUtils.pyupack2r   -   s    !\"" "Q!! WWF~~=>>>	!yy""  ! ! !yy%%d))q.! w%%,,   A( (3BBc                 ,   d}d}d}| D ]m\  }}t          |t                    st          ||          }||z  }||t          d||          z  }|t	          |                    d                    dz  z  }nt	          |          dz  }|dk    rt          d	          	 |                    d
          }d}	t	          |          }
n9# t          $ r, |                    d          }d}	t	          |          dz  }
Y nw xY wt          d|
|	|          |z   |fS )N     r   z<HHr   r      r	   r
   r      	   z<HBH)r   r   r   r   r   r   r   r   )rtr   r   froffsetr   fontxnum_frr   r   r   s              r   upack2rtr*   I   sJ   	B	BF  
2 
25!\** 	%8$$A
a $ufe,,,B 	#ahh{++,,11WW\F~~=>>>	!yy"" d)) ! ! !yy%%d))q.! v..5r99s   &C 3C<;C<c                 j   t          | t                    r| }nt          | |          }t          |          }|dk    rt	          d          	 |                    d          }d}|}n9# t          $ r, |                    d          }d}t          |          dz  }Y nw xY wt          d||          |z   S )	N   z!String longer than 255 charactersr   r   r   r   r   z<BBr   r   s          r   upack1r-   i   s    !\"" "Q!!WWF||;<<<!yy"" ! ! !yy%%d))q.! w%%,,r   N)r   )	__doc__compatr   r   structr   r   r*   r-    r!   r   <module>r2      s   % %N * ) ) ) ) ) ) )      - - - -8: : : :@- - - - - -r!   