o
    gi                     @   s   d Z ddlZddlm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 ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ G dd deZdS )z:
This implementation of LayoutElement represents an Image
    N)Decimal)Image)	PDFObject)
Dictionary)Name)Color)HexColor)	Rectangle)	Alignment)LayoutElement)Pagec                2       sh  e Zd ZdZddeddededededddeddejddddededededejdfde	j
eejejf de	je d	ed
ededededededededede	je dede	je de	je de	je de	je dededededede	je f0 fdd Zd!ed"efd#d$Zded%efd&d'Zd%ed(efd)d*Zd/d+d,Zd"ejfd-d.Z  ZS )0r   zB
    This implementation of LayoutElement represents an Image
    NF000000r      imagebackground_colorborder_bottomborder_colorborder_leftborder_radius_bottom_leftborder_radius_bottom_rightborder_radius_top_leftborder_radius_top_rightborder_right
border_topborder_widthheighthorizontal_alignmentmargin_bottommargin_leftmargin_right
margin_toppadding_bottompadding_leftpadding_rightpadding_topvertical_alignmentwidthc                    s:  t t| jdi d|d|d|d|d|d|d|d|	d	|
d
|d|dddtddtdd|d|d ur?|ntdd|d urJ|ntdd|d urU|ntdd|d ur`|n tdd|d|d|d|d| || _|| _|| _d S d|d|d|d|d| || _|| _|| _d S )Nr   r   r   r   r   r   r   r   r   r   r   font	Helvetica
font_colorz#000000	font_size   r   r      r   r   r    r!   r"   r#   r$   r%    )superr   __init__r   r   _image_width_height)selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   	__class__r-   V/home/ubuntu/.local/lib/python3.10/site-packages/borb/pdf/canvas/layout/image/image.pyr/   "   s   	


zImage.__init__available_spacereturnc                 C   sL   |    | jd usJ | jd usJ t| | |  | j | j| jS )N)force_load_imager1   r2   r	   get_xget_y
get_height)r3   r7   r-   r-   r6   _get_content_box_   s   zImage._get_content_boxpagec                    s   d|vrt  ||td< d|d vrt  |d td< |   tt fdd|d d  D d }|d ur;|S t|d d d } |d d td| < td| S )N	ResourcesXObjectc                    s   g | ]
\}}| kr|qS r-   r-   ).0kvr   r-   r6   
<listcomp>t   s    z2Image._get_image_resource_name.<locals>.<listcomp>r   zIm%d)r   
set_parentr   r9   nextiteritemslen)r3   r   r>   image_resource_nameimage_indexr-   rD   r6   _get_image_resource_namej   s    zImage._get_image_resource_namebounding_boxc                 C   s|   |    | | j|}| jd usJ | jd usJ dt| jt| jt| t| |  | j |f }|	| d S )Nz q %f 0 0 %f %f %f cm /%s Do Q )
r9   rM   r0   r1   r2   floatr:   r;   r<   append_to_content_stream)r3   r>   rN   rK   contentr-   r-   r6   _paint_content_box~   s   
	zImage._paint_content_boxc                 C   s   t | jtrttj| jdddidj| _t | jtj	r$t| j| _t | jtj
s-J t| j | jdu r?t| jj| _| jdu rMt| jj| _dS dS )a  
        This function forces the underlying PIL Image to load.
        Images can be specified by providing a Path, str, or PIL Image.
        The underlying image is only loaded when needed (such as when performing layout)
        :return:    None
        TzAccept-Encoding )streamheadersN)
isinstancer0   strPILImageModuleopenrequestsgetrawpathlibPathr   r   add_pdf_object_methodsr1   r   r&   r2   r   r3   r-   r-   r6   r9      s&   

zImage.force_load_imagec                 C   s    |    t| jtjsJ | jS )z
        This function returns the PIL.Image.Image underlying this borb Image
        :return:    the PIL.Image.Image underlying this borb Image
        )r9   rV   r0   rX   r   r`   r-   r-   r6   get_PIL_image   s   zImage.get_PIL_image)r8   N)__name__
__module____qualname____doc__r   r   r
   LEFTTOPtypingUnionrW   r]   r^   rX   r   Optionalr   boolr/   r	   r=   r   rM   rR   r9   ra   __classcell__r-   r-   r4   r6   r      s    	
=
"r   )re   rh   decimalr   r]   rZ   PILr   rX   borb.io.read.pdf_objectr   borb.io.read.typesr   r   borb.pdf.canvas.color.colorr   r   "borb.pdf.canvas.geometry.rectangler	   %borb.pdf.canvas.layout.layout_elementr
   r   borb.pdf.page.pager   r-   r-   r-   r6   <module>   s    