
    h?N                        S r SSKrSSKrSSKrSSKJr  SSKJr	  SSK
Jr  SSKJr  SSKJr  SSKJr  \R$                  r\R&                  " \5      r " S S\	R,                  5      rS	 r             SS
 jr " S S5      rg))PdfPagePdfColorScheme    N)PdfiumError)	PdfBitmap)PdfTextPage)	PdfObjectc                   4  ^  \ rS rSrSrU 4S jr\S 5       r\S 5       r	S r
S rS rS	 rS
 rS rS%S jrS rS%S jrS rS%S jrS rS%S jrS rS%S jrS rS rS rS rS rS rS&S jr\ RB                  4S jr"S SS!S\#RH                  SS"4S# jr%S$r&U =r'$ )'r      z
Page helper class.

Attributes:
    raw (FPDF_PAGE): The underlying PDFium page handle.
    pdf (PdfDocument): Reference to the document this page belongs to.
c                    > XUsU l         U l        U l        [        TU ]  [
        R                  U R                  5        g N)rawpdfformenvsuper__init__r   _close_impl)selfr   r   r   	__class__s       O/var/www/html/shao/venv/lib/python3.13/site-packages/pypdfium2/_helpers/page.pyr   PdfPage.__init__   s0    +.W($(DL,,dll;    c                 j    U(       a  [         R                  " X5        [         R                  " U 5        g r   )pdfium_cFORM_OnBeforeClosePageFPDF_ClosePage)r   r   s     r   r   PdfPage._close_impl"   s!    ++C9$r   c                     U R                   $ r   )r   r   s    r   parentPdfPage.parent)   s     xxr   c                 .    [         R                  " U 5      $ )zH
Returns:
    float: Page width (horizontal size), in PDF canvas units.
)r   FPDF_GetPageWidthFr   s    r   	get_widthPdfPage.get_width/   s    
 **400r   c                 .    [         R                  " U 5      $ )zG
Returns:
    float: Page height (vertical size), in PDF canvas units.
)r   FPDF_GetPageHeightFr   s    r   
get_heightPdfPage.get_height6   s    
 ++D11r   c                 B    U R                  5       U R                  5       4$ )zJ
Returns:
    (float, float): Page width and height, in PDF canvas units.
)r#   r'   r   s    r   get_sizePdfPage.get_size=   s    
  $//"344r   c                 P    [         R                  [        R                  " U 5         $ )z7
Returns:
    int: Clockwise page rotation in degrees.
)pdfium_iRotationToDegreesr   FPDFPage_GetRotationr   s    r   get_rotationPdfPage.get_rotationG   s     
 ))8+H+H+NPPr   c                 T    [         R                  " U [        R                  U   5        g)zL
Define the absolute, clockwise page rotation (0, 90, 180, or 270 degrees).
N)r   FPDFPage_SetRotationr-   RotationToConst)r   rotations     r   set_rotationPdfPage.set_rotationN   s     	%%dH,D,DX,NOr   c                     [        5       [        5       [        5       [        5       4u  pEpgU" XXVU5      nU(       d  U(       a  U" 5       $ S $ UR                  UR                  UR                  UR                  4$ r   )c_floatvalue)	r   box_funcfallback_funcfallback_okleftbottomrighttopoks	            r   _get_boxPdfPage._get_boxU   s\    #*9giGI#M ed&5'2MO==

FLL%++syyAAr   Tc                 F    U R                  [        R                  S U5      $ )a  
Returns:
    (float, float, float, float) | None:
    The page MediaBox in PDF canvas units, consisting of four coordinates (usually x0, y0, x1, y1).
    If MediaBox is not defined, returns ANSI A (0, 0, 612, 792) if ``fallback_ok=True``, None otherwise.
Note:
    Due to quirks in PDFium's public API, all ``get_*box()`` functions except :meth:`.get_bbox`
    do not inherit from parent nodes in the page tree (as of PDFium 5418).
c                      g)N)r   r   id  i   rG   r   r   <lambda>&PdfPage.get_mediabox.<locals>.<lambda>i   s    DTr   )rC   r   FPDFPage_GetMediaBoxr   r=   s     r   get_mediaboxPdfPage.get_mediabox^   s     }}X::<TVabbr   c                 4    [         R                  " XX#U5        g)z^
Set the page's MediaBox by passing four :class:`float` coordinates (usually x0, y0, x1, y1).
N)r   FPDFPage_SetMediaBoxr   lbrts        r   set_mediaboxPdfPage.set_mediaboxk        	%%dqQ7r   c                 X    U R                  [        R                  U R                  U5      $ )zK
Returns:
    The page's CropBox (If not defined, falls back to MediaBox).
)rC   r   FPDFPage_GetCropBoxrL   rK   s     r   get_cropboxPdfPage.get_cropboxq   s#    
 }}X994;L;LkZZr   c                 4    [         R                  " XX#U5        g)z
Set the page's CropBox.
N)r   FPDFPage_SetCropBoxrP   s        r   set_cropboxPdfPage.set_cropboxx        	$$TaA6r   c                 X    U R                  [        R                  U R                  U5      $ )zK
Returns:
    The page's BleedBox (If not defined, falls back to CropBox).
)rC   r   FPDFPage_GetBleedBoxrZ   rK   s     r   get_bleedboxPdfPage.get_bleedbox~   s#    
 }}X::D<L<LkZZr   c                 4    [         R                  " XX#U5        g)z
Set the page's BleedBox.
N)r   FPDFPage_SetBleedBoxrP   s        r   set_bleedboxPdfPage.set_bleedbox   rW   r   c                 X    U R                  [        R                  U R                  U5      $ )zJ
Returns:
    The page's TrimBox (If not defined, falls back to CropBox).
)rC   r   FPDFPage_GetTrimBoxrZ   rK   s     r   get_trimboxPdfPage.get_trimbox   s#    
 }}X994;K;K[YYr   c                 4    [         R                  " XX#U5        g)z
Set the page's TrimBox.
N)r   FPDFPage_SetTrimBoxrP   s        r   set_trimboxPdfPage.set_trimbox   r`   r   c                 X    U R                  [        R                  U R                  U5      $ )zI
Returns:
    The page's ArtBox (If not defined, falls back to CropBox).
)rC   r   FPDFPage_GetArtBoxrZ   rK   s     r   
get_artboxPdfPage.get_artbox   s#    
 }}X88$:J:JKXXr   c                 4    [         R                  " XX#U5        g)z
Set the page's ArtBox.
N)r   FPDFPage_SetArtBoxrP   s        r   
set_artboxPdfPage.set_artbox   s     	##DQ15r   c                     [         R                  " 5       n[         R                  " X5      nU(       d  [        S5      eUR                  UR
                  UR                  UR                  4$ )zb
Returns:
    The bounding box of the page (the intersection between its media box and crop box).
z Failed to get page bounding box.)r   FS_RECTFFPDF_GetPageBoundingBoxr   r>   r?   r@   rA   )r   rectrB   s      r   get_bboxPdfPage.get_bbox   sP    
   "--d9@AA		4;;

DHH==r   c                     [         R                  " U 5      nU(       d  [        S5      e[        X5      nU R	                  U5        U$ )zA
Returns:
    PdfTextPage: A new text page handle for this page.
zFailed to load text page.)r   FPDFText_LoadPager   r   _add_kid)r   raw_textpagetextpages      r   get_textpagePdfPage.get_textpage   s>    
  11$79::|2hr   c                     UR                   (       a  [        S5      eUR                  (       a$  UR                  U R                  La  [        S5      e[        R                  " X5        UR                  5         Xl         U R                  Ul        g)aj  
Insert a page object into the page.

The page object must not belong to a page yet. If it belongs to a PDF, this page must be part of the PDF.

Position and form are defined by the object's matrix.
If it is the identity matrix, the object will appear as-is on the bottom left corner of the page.

Parameters:
    pageobj (PdfObject): The page object to insert.
zAThe pageobject you attempted to insert already belongs to a page.zBThe pageobject you attempted to insert belongs to a different PDF.N)page
ValueErrorr   r   FPDFPage_InsertObject_detach_finalizer)r   pageobjs     r   
insert_objPdfPage.insert_obj   sc     <<`aa;;GKKtxx7abb&&t5!!#hhr   c                     UR                   U La  [        S5      e[        R                  " X5      nU(       d  [	        S5      eSUl         UR                  5         g)a*  
Remove a page object from the page.
As of PDFium 5692, detached page objects may be only re-inserted into existing pages of the same document.
If the page object is not re-inserted into a page, its ``close()`` method may be called.

Parameters:
    pageobj (PdfObject): The page object to remove.
zAThe page object you attempted to remove is not part of this page.zFailed to remove pageobject.N)r   r   r   FPDFPage_RemoveObjectr   _attach_finalizer)r   r   rB   s      r   
remove_objPdfPage.remove_obj   sN     <<t#`aa++D:<==!!#r   c                 T    [         R                  " U 5      nU(       d  [        S5      eg)z
Generate page content to apply additions, removals or modifications of page objects.

If page content was changed, this function should be called once before saving the document or re-loading the page.
z Failed to generate page content.N)r   FPDFPage_GenerateContentr   )r   rB   s     r   gen_contentPdfPage.gen_content   s(     ..t4@AA r   Nr   c              #   :  #    U(       a#  [         R                  n[         R                  nUnO"[         R                  n[         R                  nU nU" U5      nUS:  a  [        S5      e[        U5       H  n	U" Xy5      n
U
c  [        S5      e[        XU R                  US9nU R                  U5        U(       a  UR                  U;   a  Uv   UR                  [         R                  :X  d  Mz  XBS-
  :  d  M  U R                  UUU
US-   S9 Sh  vN   M     g N	7f)a  
Iterate through the page objects on this page.

Parameters:
    filter (list[int] | None):
        An optional list of page object types to filter (:attr:`FPDF_PAGEOBJ_*`).
        Any objects whose type is not contained will be skipped.
        If None or empty, all objects will be provided, regardless of their type.
    max_depth (int):
        Maximum recursion depth to consider when descending into Form XObjects.

Yields:
    :class:`.PdfObject`: A page object.
r   z%Failed to get number of page objects.NzFailed to get page object.)r   r   level   )filter	max_depthformr   )r   FPDFFormObj_CountObjectsFPDFFormObj_GetObjectFPDFPage_CountObjectsFPDFPage_GetObjectr   ranger   r   r   typeFPDF_PAGEOBJ_FORMget_objects)r   r   r   r   r   count_objects
get_objectr   	n_objectsiraw_obj
helper_objs               r   r   PdfPage.get_objects   s    $ $==M!77JF$::M!44JF!&)	q=EFFy!A +G!">??"74885QJMM*%Z__6  ("<"<<STAT++# )"!AI	 ,    "s   C'D-D7DD
Dc                 p    [         R                  " X5      nU[         R                  :X  a  [        S5      eU$ )z
Attempt to flatten annotations and form fields into the page contents.

Parameters:
    flag (int): PDFium flattening target (:attr:`FLAT_*`)
Returns:
    int: PDFium flattening status (:attr:`FLATTEN_*`). :attr:`FLATTEN_FAIL` is handled internally.
z,Failed to flatten annotations / form fields.)r   FPDFPage_FlattenFLATTEN_FAILr   )r   flagrcs      r   _flattenPdfPage._flatten-  s3     &&t2&&&LMM	r   r   )r   r   r   r   Fc                    [         R                  " U R                  5       U-  5      n	[         R                  " U R                  5       U-  5      n
US;   a  XpU Vs/ sH  n[         R                  " X-  5      PM     nnXS   -
  US   -
  nXS   -
  US   -
  n[	        S X4 5       5      (       a  [        S5      e[        S0 UD6u  pnnUb  U(       a  U[        R                  -  nU" XXS9nUR                  SSXU5        UXS   * US   * X[        R                  U   U4nUc  [        R                  " U6   O[        R                  " SS	9n[        R                  " US
S 5        UR                  U5      n[        R                   " / UQUPUP76 nU[        R"                  :X  d   e[        R$                  " U 5        U(       a2  U R&                  (       a!  [        R(                  " U R&                  /UQ76   U$ s  snf )af  
Rasterize the page to a :class:`.PdfBitmap`.

Parameters:
    
    scale (float):
        A factor scaling the number of pixels per PDF canvas unit. This defines the resolution of the image.
        To convert a DPI value to a scale factor, multiply it by the size of 1 canvas unit in inches (usually 1/72in). [#user_unit]_
        
    rotation (int):
        Additional rotation in degrees (0, 90, 180, or 270).
        
    crop (tuple[float, float, float, float]):
        Amount in PDF canvas units to cut off from page borders (left, bottom, right, top). Crop is applied after rotation.
        
    may_draw_forms (bool):
        If True, render form fields (provided the document has forms and :meth:`~PdfDocument.init_forms` was called).
    
    bitmap_maker (typing.Callable):
        Callback function used to create the :class:`.PdfBitmap`.
        
    color_scheme (PdfColorScheme | None):
        An optional, custom rendering color scheme.
        
    fill_to_stroke (bool):
        If True and rendering with custom color scheme, fill paths will be stroked.
        
    fill_color (tuple[int, int, int, int]):
        Color the bitmap will be filled with before rendering (RGBA values from 0 to 255).
        
    grayscale (bool):
        If True, render in grayscale mode.
        
    optimize_mode (None | str):
        Page rendering optimization mode (None, "lcd", "print").
        
    draw_annots (bool):
        If True, render page annotations.
        
    no_smoothtext (bool):
        If True, disable text anti-aliasing. Overrides ``optimize_mode="lcd"``.
        
    no_smoothimage (bool):
        If True, disable image anti-aliasing.
        
    no_smoothpath (bool):
        If True, disable path anti-aliasing.
        
    force_halftone (bool):
        If True, always use halftone for image stretching.
        
    limit_image_cache (bool):
        If True, limit image cache size.
        
    rev_byteorder (bool):
        If True, render with reverse byte order, leading to ``RGB(A/X)`` output instead of ``BGR(A/X)``.
        Other pixel formats are not affected.
        
    prefer_bgrx (bool):
        If True, prefer four-channel over three-channel pixel formats, even if the alpha byte is unused.
        Other pixel formats are not affected.
        
    force_bitmap_format (int | None):
        If given, override automatic pixel format selection and enforce use of the given format (one of the :attr:`FPDFBitmap_*` constants).
        
    extra_flags (int):
        Additional PDFium rendering flags. May be combined with bitwise OR (``|`` operator).

Returns:
    PdfBitmap: Bitmap of the rendered page.

.. [#user_unit] Since PDF 1.6, pages may define an additional user unit factor. In this case, 1 canvas unit is equivalent to ``user_unit * (1/72)`` inches. PDFium currently does not have an API to get the user unit, so this is not taken into account.
)Z   i  r      r      c              3   (   #    U H	  oS :  v   M     g7f)r   NrG   ).0ds     r   	<genexpr>!PdfPage.render.<locals>.<genexpr>  s     .o1uos   zCrop exceeds page dimensions)formatrev_byteorder)versionNeedToPauseNowc                     g)NFrG   )_s    r   rH    PdfPage.render.<locals>.<lambda>  s    Ur   rG   )mathceilr#   r'   anyr   _parse_renderoptsr   FPDF_CONVERT_FILL_TO_STROKE	fill_rectr-   r4   FPDF_RenderPageBitmapIFSDK_PAUSEset_callbackconvert*FPDF_RenderPageBitmapWithColorScheme_StartFPDF_RENDER_DONEFPDF_RenderPage_Closer   FPDF_FFLDraw)r   scaler5   cropmay_draw_formsbitmap_makercolor_schemefill_to_strokekwargs	src_width
src_heightcwidthheight	cl_formatr   
fill_colorflagsbitmaprender_argspausefpdf_csstatuss                          r   renderPdfPage.renderB  s   j YYt~~/589	YYt0589
y $.z,01Dq		!'"D11g%Q/1g%Q/.uo...;<<6G6Q&6Q3	*e$.X999EeI[Auj9t1gXQxPXPhPhiqPrtyz**K8 ((3E!!%)9?K"**=9GHHf+fW^f`efFX66666**40dll!!$,,=== 2s   !G9)r   r   r   )T)Nr   Nr   )(__name__
__module____qualname____firstlineno____doc__r   staticmethodr   propertyr   r#   r'   r*   r0   r6   rC   rL   rU   rZ   r^   rc   rg   rk   ro   rs   rw   r}   r   r   r   r   r   r   FLAT_NORMALDISPLAYr   r   
new_nativer   __static_attributes____classcell__)r   s   @r   r   r      s    <
 % %  
125QPBc8[7[8Z7Y6	>
0$(B0h %77 . !$//"x xr   r   c                     U S   S:  a  [         R                  $ U(       a  [         R                  $ U(       a  [         R                  $ [         R                  $ )Nr      )r   FPDFBitmap_BGRAFPDFBitmap_GrayFPDFBitmap_BGRxFPDFBitmap_BGR)r   	grayscaleprefer_bgrxs      r   _auto_bitmap_formatr     sF     	1'''	'''	'''&&&r   c                    Uc  [        XU
5      nOUnU[        R                  :X  a  Sn	UnU(       a  U[        R                  -  nU(       a  U[        R                  -  nU(       a  U[        R
                  -  nU(       a  U[        R                  -  nU(       a  U[        R                  -  nU(       a  U[        R                  -  nU(       a  U[        R                  -  nU	(       a  U[        R                  -  nU(       aR  UR                  5       nUS:X  a  U[        R                  -  nO(US:X  a  U[        R                  -  nO[        SU 35      eXX4$ )NFlcdprintzInvalid optimize_mode )r   r   r   FPDF_GRAYSCALE
FPDF_ANNOTFPDF_RENDER_NO_SMOOTHTEXTFPDF_RENDER_NO_SMOOTHIMAGEFPDF_RENDER_NO_SMOOTHPATHFPDF_RENDER_FORCEHALFTONEFPDF_RENDER_LIMITEDIMAGECACHEFPDF_REVERSE_BYTE_ORDERlowerFPDF_LCD_TEXTFPDF_PRINTINGr   )r   r   optimize_modedraw_annotsno_smoothtextno_smoothimageno_smoothpathforce_halftonelimit_image_cacher   r   force_bitmap_formatextra_flagsr   r   s                  r   r   r     s,     "'
{K	'	H,,,E((($$$333444333333777111%++-E!X+++Eg%X+++E5m_EFF Z66r   c                   $    \ rS rSrSrS rS rSrg)r   i  z
Rendering color scheme.
Each color shall be provided as a list of values for red, green, blue and alpha, ranging from 0 to 255.
c                 "    [        XX4S9U l        g )N)path_fill_colorpath_stroke_colortext_fill_colortext_stroke_color)dictcolors)r   	path_fillpath_stroke	text_filltext_strokes        r   r   PdfColorScheme.__init__  s    %%
r   c           	          [         R                  " 5       nU R                  R                  5        H%  u  p4[	        X#[
        R                  " XA5      5        M'     U$ )zD
Returns:
    The color scheme as :class:`FPDF_COLORSCHEME` object.
)r   FPDF_COLORSCHEMEr  itemssetattrr-   color_tohex)r   r   r   keyr:   s        r   r   PdfColorScheme.convert  sE    
 ++-++++-JCG("6"6u"LM .r   )r  N)r   r   r   r   r   r   r   r   rG   r   r   r   r     s    

r   r   ))r   r   r   r   FNTFFFFFFFNr   )__all__r   ctypesloggingpypdfium2.rawr   r   pypdfium2.internalinternalr-   pypdfium2._helpers.miscr   pypdfium2._helpers.bitmapr   pypdfium2._helpers.textpager   pypdfium2._helpers.pageobjectsr   r9   	getLoggerr   loggerAutoCloseabler   r   r   r   rG   r   r   <module>r3     s    (      % / / 3 4
..			8	$fx%% fR
' *!"47n r   