
    liD*                         d Z ddlZddlZddlZddlmZ ddlmZ	 ddl
mZ ddlmZmZ  ej                  e      Z G d d      Z e       ZdZ G d d	e	j*                        Zy)
)PdfSysfontBase    N)PdfiumError)cached_propertycached_property_clearc                   .    e Zd Zd Zed        Zd Zd Zy)_DefaultSysfontInfoClassc                     d| _         y )NF)
_is_loadedselfs    [/app/cer_product_mecsu/.venv/lib/python3.12/site-packages/pypdfium2/_helpers/sysfontinfo.py__init__z!_DefaultSysfontInfoClass.__init__   s	        c                 "   d| _         t        j                  d       t        j                         }|s3t        dt        j                  dt        |       j                   d      t        j                  | j                         |j                  S )NTzLoad default sysfont infoz8No default FPDF_SYSFONTINFO available on this platform (z), cannot use .)r
   loggerdebugpdfium_cFPDF_GetDefaultSystemFontInfor   sysplatformtype__name__atexitregister_close_implcontents)r   default_ptrs     r   rawz_DefaultSysfontInfoClass.raw   s    01<<> XY\YeYeXhhvw{  }A  xB  xK  xK  wL  LM  N  O  O 	(()###r   c                     | j                   sy t        j                  d       t        j                  | j
                         t        | d       d| _         y )NzFree default sysfont infor   F)r
   pdfium_i_debug_closer   FPDF_FreeDefaultSystemFontInfor   r   r   s    r   r   z$_DefaultSysfontInfoClass._close_impl"   s>    9://9dE*r   c                 b    t        j                  | j                         | j                          y N)r   
unregisterr   r   s    r   closez_DefaultSysfontInfoClass.close*   s"    $**+r   N)r   
__module____qualname__r   r   r   r   r'    r   r   r   r      s%      	$ 	$ r   r   )Release	EnumFontsMapFontGetFontGetFontDataGetFaceNameGetFontCharset
DeleteFontc                   n    e Zd ZdZdZddZd Zd ZddZd Z	ddZ
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)r   aZ  
    Base helper class to create a ``FPDF_SYSFONTINFO`` callback system.
    Callbacks can be implemented by subclassing (see `fpdf_sysfontinfo.h` for available callouts and documentation).
    
    This constructor merely creates the underlying ``FPDF_SYSFONTINFO`` instance.
    Call :meth:`.setup` to actually register it with pdfium.
    
    System font handlers may wrap another implementation, by default the root implementation provided by pdfium.
    When a callback is not implemented, it will be automatically delegated to the default handler.
    See the example below for how to invoke the default handler in a callback:
    
    .. code-block:: python
        
        class MySysfontImpl (PdfSysfontBase):
            # substitute CallbackName accordingly
            def CallbackName(self, _, arg1, arg2, ...)
                print("Wrap before")
                # Important: Do not pass the _ argument here, that's a pointer to self.raw.
                # Pass self.default instead. The C callback expects its own struct, not the wrapper.
                out = self.default.CallbackName(self.default, arg1, arg2, ...)
                print("Wrap after")
                return out
    
    Alternatively, if using subclassing, you may want

    .. code-block:: python

        out = super().CallbackName(_, arg1, arg2, ...)

    when the next class in the MRO has an implementation that you want to call.
    
    Parameters:
        default (None | FPDF_SYSFONTINFO | PdfSysfontBase):
            The sysfont handler to be wrapped. If None (the default), pdfium's root implementation will be used.
            Otherwise, this can be either a raw ``FPDF_SYSFONTINFO`` or another :class:`.PdfSysfontBase` instance.
    
    Note:
        When another :class:`.PdfSysfontBase` is being wrapped, some tricks are applied to avoid overhead:

        - Where wrapper and child share the same callback, the child method will be forwarded to the wrapper (so, as a side effect, even stacking instances of the same class would result in only one call).
        - Also, only in the actual ``FPDF_SYSFONTINFO`` object are callbacks ever enclosed in their :func:`~ctypes.CFUNCTYPE`, whereas wrappers call the original function directly.
    
    Attributes:
        raw (FPDF_SYSFONTINFO):
            The underlying ``FPDF_SYSFONTINFO`` interface struct implemented by this class. May wrap :attr:`.default`.
        default (FPDF_SYSFONTINFO | PdfSysfontBase):
            The sysfont handler being wrapped. Wrapper callbacks typically delegate the actual work to the default implementation.
        version (int):
            The ``FPDF_SYSFONTINFO`` struct version used. Matches :attr:`.default.version` and :attr:`.raw.version`.
            This is provided for interface compatibility with ``FPDF_SYSFONTINFO``, so that :attr:`.default` can be either a raw struct or :class:`.PdfSysfontBase`.
    Nc                 8   d| _         d | _        d| _        d | _        |t        j
                  | _        nB|| _        t        | j                  t              r!| j                  | _        | j                          | j                  j                  | _
        t        j                         | _        | j                  | j
                  _
        t        D ci c]  }|t        | |       }}| j                  dk7  r|d= t        j                   | j
                  fi | y c c}w )NF   r,   )_is_installed	_reusable
_destroyed_child_DefaultSysfontInfor   default
isinstancer   _forward_default_callbacksversionr   FPDF_SYSFONTINFO_CallbackNamesgetattrr!   set_callbacks)r   r;   n	callbackss       r   r   zPdfSysfontBase.__init__g   s    "?.22DL"DL$,,7"ll//1||++,,.<<2@AQQa((A	A<<1+&txx595 Bs   Dc                     t        |       }t        D ]>  }t        | j                  |      }t        ||      |j                  u s2t        | ||       @ y r%   )r   r@   rA   r;   __func__setattr)r   reference_classcb_name	candidates       r   r=   z)PdfSysfontBase._forward_default_callbacks   sK    t*% 	2Gg6I0I4F4FFgy1	2r   c              #   P   K   | j                   }|r| |j                   }|ry y wr%   )r9   r   childs     r   	_iterkidszPdfSysfontBase._iterkids   s%     KLLE s   !&&c                    t         j                  \t        j                  dt	        |       j
                   dt         j                          t         j                  j                  d       t        d | g| j                         D              rt        d      t        j                  | j                         | t         _        d| _        || _        t        j                   | j"                         y)a  
        Install (activate) the sysfont handler.
        
        Note:

            Once this method has been called, the instance is (by default) kept alive until the end of session, through an exit handler.
            To stop the sysfont handler earlier, call :meth:`.close`.

            Sysfont handlers are singleton, i.e. only one handler can be active at a time.
            When a new handler is installed, the previous handler (if any) is implicitly closed.
        NzInstalling a new z> instance implicitly closes previous sysfont handler instance T)reusablec              3   4   K   | ]  }|j                     y wr%   )r8   ).0hs     r   	<genexpr>z'PdfSysfontBase.setup.<locals>.<genexpr>   s     ?q||?s   zYou cannot register a sysfontinfo that has been destroyed, whether directly or indirectly. Pass `reusable=True` on setup or closing of handlers as necessary. Singleton replacement can do this implicitly.)r   	SINGLETONr   infor   r   r'   anyrN   r   r   FPDF_SetSystemFontInfor   r6   r7   r   r   r   r   rP   s     r   setupzPdfSysfontBase.setup   s     ##/KK+DJ,?,?+@@~  @N  @X  @X  Y  Z  [$$**D*9?d%>T^^-=%>??  l  m  m 	''1#' !!(()r   c                    | j                   sy t        j                  d       | j                         D ]  }| j                  |_         t        j                  d        | j                  rt        j                          d t        _        y )NzClose sysfontinfo)r6   r!   r"   rN   r7   r   rX   r8   r:   r'   r   rU   rL   s     r   r   zPdfSysfontBase._close_impl   sk    !! 13 ^^% 	-E"nnEO	- 	''-??%%'#' r   c                 t    ||| _         t        j                  | j                         | j                          y)a  
        Manually close the sysfont handler.
        This unregisters the exit handler and releases the sysfont handler immediately.
        
        See the note above for how sysfont handler lifetime is managed by default.
        
        Parameters:
            reusable (bool):
                If False (the default), closing will destroy pdfium's default handler, rendering any direct or indirect wrappers thereof unusable.
                If True, however, the default handler will not be harmed, so the object can be reused, like re-installing it some time after closing, wrapping it in another object, or just preserving the default handler for a new :class:`.PdfSysfontBase` instance.
                This is automatically set to True on singleton replacement, when the previous handler is implicitly closed (i.e. ownership of the default instance is transferred to the new handler).
        N)r7   r   r&   r   rY   s     r   r'   zPdfSysfontBase.close   s1     %DN$**+r   c                     | j                   rt        j                  d       y t        j                  d| j                   d       d| _        | j
                  j                  | j
                        S )Nz.fontinfo::Release: skip because it is reusablez-fontinfo::Release: actually release (wrapped=)T)r7   r!   r"   r9   r8   r;   r+   )r   _s     r   r+   zPdfSysfontBase.Release   sX    >>!!$RT Mdkk]Z[\]||##DLL11r   c                 N    | j                   j                  | j                   |      S r%   )r;   r,   )r   r_   pMappers      r   r,   zPdfSysfontBase.EnumFonts   s    ||%%dllG<<r   c           	      X    | j                   j                  | j                   ||||||      S r%   )r;   r-   )r   r_   weightbItaliccharsetpitch_familyface_ignoreds           r   r-   zPdfSysfontBase.MapFont   s)    ||##DLL&'7LZ^`hiir   c                 N    | j                   j                  | j                   |      S r%   )r;   r.   )r   r_   rg   s      r   r.   zPdfSysfontBase.GetFont   s    ||##DLL$77r   c                 T    | j                   j                  | j                   ||||      S r%   )r;   r/   )r   r_   hFonttablebufferbuf_sizes         r   r/   zPdfSysfontBase.GetFontData   s#    ||''eUFHUUr   c                 R    | j                   j                  | j                   |||      S r%   )r;   r0   )r   r_   rk   rm   rn   s        r   r0   zPdfSysfontBase.GetFaceName   s!    ||''eVXNNr   c                 N    | j                   j                  | j                   |      S r%   )r;   r1   r   r_   rk   s      r   r1   zPdfSysfontBase.GetFontCharset   s    ||**4<<??r   c                 N    | j                   j                  | j                   |      S r%   )r;   r2   rq   s      r   r2   zPdfSysfontBase.DeleteFont   s    ||&&t||U;;r   r%   )F)r   r(   r)   __doc__rU   r   r=   rN   rZ   r   r'   r+   r,   r-   r.   r/   r0   r1   r2   r*   r   r   r   r   3   sY    .b I622!*6($2=j8VO@<r   r   )__all__r   r   loggingpypdfium2.rawr   r   pypdfium2.internalinternalr!   pypdfium2._helpers.miscr   pypdfium2._lazyr   r   	getLoggerr   r   r   r:   r@   AutoCastabler   r*   r   r   <module>r}      sa     
     % / B			8	$ : /0 }r<h++ r<r   