
    i|6                         S SK r S SKJrJrJrJr  SSKJrJr  SSK	J
r
  SSKJrJr  SSKJrJrJrJrJr  SSKJrJrJr  S	r " S
 S\5      rg)    N)AnyOptionalUnioncast   )_default_fonts_space_widthbuild_char_map_from_dict)logger_warning)AnnotationDictionaryAttributesFieldDictionaryAttributes)DecodedStreamObjectDictionaryObject
NameObjectNumberObjectRectangleObject)ByteStringObjectTextStringObjectis_null_or_none   c                   <  ^  \ rS rSrSr        SS\S\\\      S\\	\
\\\\4   4   S\\\\4      S\S	\S
\S\S\4S jjr        SS\S\\\      S\\	\
\\\\4   4   S\\   S\S	\S
\S\SS4U 4S jjjr\  SS\S\S\S\S\SS 4S jj5       rSrU =r$ )TextStreamAppearance   a4  
A class representing the appearance stream for a text-based form field.

This class generates the content stream (the `ap_stream_data`) that dictates
how text is rendered within a form field's bounding box. It handles properties
like font, font size, color, multiline text, and text selection highlighting.
Ntext	selection	rectanglefont_glyph_byte_map	font_name	font_size
font_coloris_multilinereturnc	                    U=(       d    0 n[        U[        5      (       a  [        U5      nUS:X  a  U(       a  [        nOUR                  S-
  nUR                  S-
  U-
  n	U SU SU 3n
SUR
                  S-
   SUR                  S-
   SU
 S3R                  5       n[        UR                  SS	5      R                  S	5      5       GH  u  pU(       a@  X;   a;  US
XU-  S-  -
  S-
   SUR
                  S-
   SUS-    SU
 S3	R                  5       -  nUS:X  a  USU	 S3R                  5       -  nOUSU* S-   S3R                  5       -  nU Vs/ s H"  oR                  XR                  S5      5      PM$     nn[        S U 5       5      (       a9  USSR                  U5      R                  5       R                  5       -   S-   -  nGM  USSR                  U5      -   S-   -  nGM"     US-  nU$ s  snf )as  
Generates the raw bytes of the PDF appearance stream for a text field.

This private method assembles the PDF content stream operators to draw
the provided text within the specified rectangle. It handles text positioning,
font application, color, and special formatting like selected text.

Args:
    text: The text to be rendered in the form field.
    selection: An optional list of strings that should be highlighted as selected.
    font_glyph_byte_map: An optional dictionary mapping characters to their
        byte representation for glyph encoding.
    rect: The bounding box of the form field. Can be a `RectangleObject`
        or a tuple of four floats (x1, y1, x2, y2).
    font_name: The name of the font resource to use (e.g., "/Helv").
    font_size: The font size. If 0, it is automatically calculated
        based on whether the field is multiline or not.
    font_color: The color to apply to the font, represented as a PDF
        graphics state string (e.g., "0 g" for black).
    is_multiline: A boolean indicating if the text field is multiline.

Returns:
    A byte string containing the PDF content stream data.

r   r       z Tf zq
/Tx BMC 
q
1 1 z	 re
W
BT

z1 gffffff?z re
0.5 0.5 0.5 rg s
z2 z Td
z0 z	utf-16-bec              3   >   #    U  H  n[        U5      S :  v   M     g7f)r   N)len).0cs     Z/home/james-whalen/.local/lib/python3.13/site-packages/pypdf/generic/_appearance_stream.py	<genexpr>HTextStreamAppearance._generate_appearance_stream_data.<locals>.<genexpr>c   s     513q6Q;s      <    s   > Tj
   (s   ) Tj
s   ET
Q
EMC
Q
)
isinstancetupler   DEFAULT_FONT_SIZE_IN_MULTILINEheightwidthencode	enumeratereplacesplitgetanyjoinhex)selfr   r   r   r   r   r   r   r    y_offsetdefault_appearance	ap_streamline_numberliner*   encoded_lines                   r+    _generate_appearance_stream_data5TextStreamAppearance._generate_appearance_stream_data   s=   H 27Ri'''	2I >:	%,,q0	 ##a')3 ){!I;d:,G #9??Q#6"7q9I9IA9M8N O,-R1
&( 	
 "+4<<d+C+I+I$+O!PKT.9%<s%BCaGH)//\]J]I^^_`ilm`m_n o));(<B@&(	 ar(5188::	 r9*s"2!359@@BB	KO)KOa''88K+@A4  ) 5555TSXXl%;$@$@$B$I$I$KKiWW	TCHH\$::YFF	% "Q& 	''	)s   )G4font_resourcec	                 l  > [         TU ]  5         U(       Ga  [        [        UR	                  5       5      n[        SU5      u  pp US	 [        U[        5      (       a4  UR                  5        VVs0 s H  u  pXR                  U5      _M     nnnOUR                  5        VVs0 s H  u  pU[        U45      _M     nnnUR                  5        VVs0 s H  u  pU[        U45      _M     nnnUR                  5        H  u  nnUR                  UU5      UU'   M     O[        SU S3[        5        0 nU R                  UUUUUUUU5      n[!        S5      U [!        S5      '   [!        S5      U [!        S5      '   [#        U5      U [!        S	5      '   U R%                  ['        U5      5        [)        [+        U5      5      U [!        S
5      '   UbB  [        [!        S5      [        [!        U5      [-        USU5      05      05      U [!        S5      '   gg! [         a     GNf = fs  snnf s  snnf s  snnf )a  
Initializes a TextStreamAppearance object.

This constructor creates a new PDF stream object configured as an XObject
of subtype Form. It uses the `_appearance_stream_data` method to generate
the content for the stream.

Args:
    text: The text to be rendered in the form field.
    selection: An optional list of strings that should be highlighted as selected.
    rect: The bounding box of the form field. Can be a `RectangleObject`
        or a tuple of four floats (x1, y1, x2, y2).
    font_resource: An optional variable that represents a PDF font dictionary.
    font_name: The name of the font resource, e.g., "/Helv".
    font_size: The font size. If 0, it's auto-calculated.
    font_color: The font color string.
    is_multiline: A boolean indicating if the text field is multiline.

   zFont dictionary for z not found.z/XObject/Typez/Form/Subtype/BBox/LengthN/Fontindirect_referencez
/Resources)super__init__r   r   
get_objectr	   KeyErrorr1   stritemsr6   bytesr:   r
   __name__rE   r   r   set_datar   r   r(   getattr)r>   r   r   r   rG   r   r   r   r    _font_subtype_font_encodingfont_mapkvr   font_encoding_revkeyvalueap_stream_data	__class__s                       r+   rR   TextStreamAppearance.__init__j   s5   < 	  !1=3K3K3MNM8P]95MmRL ---;C>>;K';K41Axx..;K $ '# CPBUBUBW&XBW$!q%+~BW#&X@M@S@S@U$V@UQqd^@U!$V"*.."2JC1B1F1FsC1P'. #3 1)KH(S"$>>	
 %/z$:Z !'1'':Z
#$$3I$>Z !&~67&23~3F&GZ	"#$-=7#%5y)7=BVXe+f7 &? .DL)* %C  ' 'Y$Vs$   H 1H$%H*H0
H! H!	acro_formfield
annotationuser_font_nameuser_font_sizec                    [        [        U[        R                     5      n[        SS[	        US   US   -
  5      [	        US   US   -
  5      45      nUR                  [        R                  UR                  [        R                  S5      5      nU(       d  [        S5      nOUR                  5       n[        R                  " SU5       V	s/ s H  o(       d  M  U	PM     n
n	U
R                  U
R                  S5      S-
  5      n[        U
R                  U
R                  S5      S-
  5      5      nU
R                  S5        S	R!                  U
5      nU(       a  UnUS:  a  Un[        ["        [        ["        UR                  S
UR                  S
[#        5       5      5      5      R                  5       5      nUR                  S[#        5       5      R                  5       nX;  am  U[$        ;  ac  [        [&        [(        [(        4   UR                  S
0 5      5      nUR                  5       R                  S[#        5       5      R                  5       nUR                  US5      n[+        U5      (       d  [        ["        UR                  5       5      nSnUR                  [,        R.                  S5      nU[,        R0                  R2                  -  (       a  SnUR                  [,        R4                  S5      S:X  a{  U[,        R0                  R6                  -  S:X  aZ  SR!                  UR                  [,        R8                  / 5      5      nUR                  S/ 5      n[;        U[<        5      (       d  U/nOUR                  SS5      n/ nUR?                  SS5      R?                  SS5      R?                  SS5      nU " UUUUUUUU5      n[        R@                  U;   aV  [        ["        U[        R@                     5      R                  S0 5      RC                  5        H  u  nnUS;  d  M  UUU'   M     U$ s  sn	f )aj  
Creates a TextStreamAppearance object from a text field annotation.

This class method is a factory for creating a `TextStreamAppearance`
instance by extracting all necessary information (bounding box, font,
text content, etc.) from the PDF field and annotation dictionaries.
It respects inheritance for properties like default appearance (`/DA`).

Args:
    acro_form: The root AcroForm dictionary from the PDF catalog.
    field: The field dictionary object.
    annotation: The widget annotation dictionary object associated with the field.
    user_font_name: An optional user-provided font name to override the
        default. Defaults to an empty string.
    user_font_size: An optional user-provided font size to override the
        default. A value of -1 indicates no override.

Returns:
    A new `TextStreamAppearance` instance configured for the given field.

r   r      r#   Nz/Helv 0 Tf 0 gz\sTfr$   z/DRrO   FTz/Txz/Chr%   z/V \z\\(z\()z\)z/N>   rM   rK   /FilterrN   rL   )"r   r   r   Rectabsget_inheritedDAr:   r   rS   rer9   popindexfloatremover<   r   r   dictr   r   r   FfFfBits	MultilineFTComboOptr1   listr8   APrV   )clsrg   rh   ri   rj   rk   
_rectangler   r@   propfont_propertiesr   r   r   document_resourcesdocument_font_resourcesrG   r    field_flagsr   r   new_appearance_streamrb   rc   s                           r+   from_text_annotation)TextStreamAppearance.from_text_annotation   s   > /:6T6Y6Y+Z[
#Q3z!}z!}/L+MsS]^_S`cmnocpSpOq$rs	 (55*--MM8;;TB
 "!12B!C!3!>!>!@ -/HHU<N,OX,ODSW4,OX#''(=(=d(Ca(GH	/--o.C.CD.IA.MNO	t$XXo.
&IA&I #' ((MM%)9);< jl	#
 #5"8"8BRBT"U"`"`"b3	Ic8c!%S#XeR(" '9&C&C&E&I&I'ScSe&f&q&q&s#/33ItD}-- !1=3K3K3MNM ii 9 < <a@299CCCL		366>%G7>>DDDI99Z556O6S6SUWXYD		$+Ii..&K	99T2&DI ||D&)11#u=EEc5Q !$	!
 *,,
:%z2P2S2S'TUYYZ^`bciik U RR16)#. l
 %$U Ys   
P;P; )ro   N)        r   r   r   Nz/Helvr   z0 gF)ro   rJ   )rX   
__module____qualname____firstlineno____doc__rU   r   r   r   r   r2   r{   r}   rW   boolrE   r   rR   classmethodr   __static_attributes____classcell__)re   s   @r+   r   r      s    )-Oc:> "LL DI&L %ueU0J*KKL	L
 &d3:&67L L L L L 
L` )-Oc48 "NN DI&N %ueU0J*KKL	N
   01N N N N N 
N N`  ! "~%#~%  ~% %	~%
 ~% ~% 
 ~% ~%r/   r   )rx   typingr   r   r   r   _cmapr   r	   _utilsr
   	constantsr   r   genericr   r   r   r   r   generic._baser   r   r   r3   r   r   r/   r+   <module>r      s?    	 - - H # Q  P O!# f%. f%r/   