o
    ]i*5                     @   sv   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
 d dlmZ ddlmZmZ d	ZG d
d dZdS )    N)BytesIO)Pathunpack)TupleUnion)Url   )UndefinedImageSizeUnsupportedImageFormatg      X@c                	   @   s  e Zd ZdZdeeeef ddfddZdefddZ	e
defd	d
Ze
defddZe
defddZe
defddZe
defddZe
deedf fddZejdeddfddZe
defddZejdeddfddZe
deedf fddZejdeddfddZd.d/ddZd/dd Zd!edeeeeeef fd"d#Zd!edeeeeeef fd$d%Zd!edeeeeeef fd&d'Zd!edeeeef fd(d)Zd!edeeeeeef fd*d+Zd!edeeeeeef fd,d-Z dS )0Imagez?
    A class to represent an image in an Excel worksheet.

    sourcereturnNc                 C   s   t |ttfr|| _d| _tj|| _nt |t	r$d| _|| _d| _nt
dd| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _d| _t| _t| _d| _|   dS )	z
        Initialize an Image instance.

        Args:
            source (Union[str, Path, BytesIO]): The filename, Path or BytesIO
            object of the image.
        N z4Source must be a filename (str) or a BytesIO object.r   g      ?   F        )
isinstancestrr   filename
image_dataospathbasename
image_namer   
ValueError_row_col	_x_offset	_y_offset_x_scale_y_scale_url_anchor_description_decorative_header_position_ref_id_image_extension_width_heightDEFAULT_DPI_x_dpi_y_dpi_digest_get_image_properties)selfr    r0   U/var/www/html/stock_analysis/be/venv/lib/python3.10/site-packages/xlsxwriter/image.py__init__   s8   
zImage.__init__c                 C   s>   d| j d| jd| jd| j d| j d| j d| j dS )	zf
        Return a string representation of the main properties of the Image
        instance.
        zImage:
    filename   = z
    image_name = z
    image_type = z
    width      = z
    height     = z
    x_dpi      = z
    y_dpi      = 
)r   r   
image_typer(   r)   r+   r,   r/   r0   r0   r1   __repr__H   s    zImage.__repr__c                 C   s
   | j  S )z)Get the image type (e.g., 'PNG', 'JPEG').)r'   upperr5   r0   r0   r1   r4   X      
zImage.image_typec                 C      | j S )zGet the width of the image.)r(   r5   r0   r0   r1   width]      zImage.widthc                 C   r9   )zGet the height of the image.)r)   r5   r0   r0   r1   heightb   r;   zImage.heightc                 C   r9   )z$Get the horizontal DPI of the image.)r+   r5   r0   r0   r1   x_dpig   r;   zImage.x_dpic                 C   r9   )z"Get the vertical DPI of the image.)r,   r5   r0   r0   r1   y_dpil   r;   zImage.y_dpic                 C   r9   )z*Get the description/alt-text of the image.r#   r5   r0   r0   r1   descriptionq   r;   zImage.descriptionvaluec                 C      |r|| _ dS dS )z*Set the description/alt-text of the image.Nr?   r/   rA   r0   r0   r1   r@   v      
c                 C   r9   )z$Get whether the image is decorative.r$   r5   r0   r0   r1   
decorative|   r;   zImage.decorativec                 C   s
   || _ dS )z$Set whether the image is decorative.NrE   rC   r0   r0   r1   rF      r8   c                 C   r9   )zGet the image url.r!   r5   r0   r0   r1   url   r;   z	Image.urlc                 C   rB   )zSet the image url.NrG   rC   r0   r0   r1   rH      rD   c                 C   s   |du rdS | j st|| _ | j r| j   |d| j| _|d| j| _|d| j| _|d| j| _|d| j	| _	|d| j
| _
|d| j| _|d	| j| _|d
| j| _dS )zY
        This handles the additional optional parameters to ``insert_button()``.
        Nobject_positionx_scaley_scalex_offsety_offsetrF   r   r@   positioning)r!   r   from_options_set_object_linkgetr"   r   r    r   r   r$   r   r#   )r/   optionsr0   r0   r1   _set_user_options   s   
zImage._set_user_optionsc                 C   s  d}d}t }t }| jr| j }nt| jd}| }W d    n1 s&w   Y  t| }t	d|dd d }t	d|d d d }	t	d	|d d d }
t	d
|d d d }t	d
|dd d }t	d|d d d }|dkr| 
|\}}}}}nU|	dkr| |\}}}}}nF|
dkr| |\}}}n9|dkr| |\}}}}}n*|dkr|dkr| |\}}}}}n|dkr| |\}}}}}nt| j d|r|st| j d|dkrt }|dkrt }|| _|| _|| _|| _|| _|| _d S )Nr   rb3sr	      r   >Hr   2s4s(   ,   <Ls   PNGi  s   BMl   M5 s    EMFs   GIF8z+: Unknown or unsupported image file format.z#: no size data found in image file.)r*   r   getvalueopenr   readhashlibsha256	hexdigestr   _process_png_process_jpg_process_bmp_process_wmf_process_emf_process_gifr   r
   r'   r(   r)   r+   r,   r-   )r/   r<   r:   r=   r>   datafhdigest
png_marker
jpg_marker
bmp_marker
gif_marker
emf_markeremf_marker1r4   r0   r0   r1   r.      sZ   



zImage._get_image_propertiesri   c                 C   sj  d}t |}d}d}d}t}t}|s||k rtd||d |d  d }	td||d |d  d }
|
dkrVtd||d |d	  d }td||d	 |d
  d }|
dkrtd||d |d	  d }td||d	 |d
  d }td||d
 |d  d }|dkr|dkr|dkr|d }|d }|
dkrd}q||	 d	 }|s||k sd||||fS )N   Fr   z>Ir   rV   rY   s   IHDR      s   pHYsb   r	   g
F%u?s   IENDTpnglenr*   r   )r/   ri   offsetdata_length
end_markerr:   r<   r=   r>   lengthmarker	x_density	y_densityunitsr0   r0   r1   rc      s4   zImage._process_pngc                 C   s  d}t |}d}d}d}t}t}|s||k rtd||d |d  d }	td||d |d  d }
|	d@ dkrd|	d	krd|	d
krd|	dkrdtd||d |d  d }td||d |d  d }|	dkrtd||d |d  d }td||d |d  d }td||d |d  d }|dkr|}|}|dkr|d }|d }|dkrt}|dkrt}|	dkrd}q||
 d }|s||k sd||||fS )Nr   Fr   rW   r   rV   i  i  i  i  i        	   i  ru      rs      rt   r	   gRQ@i  Tjpegrx   )r/   ri   rz   r{   r|   r:   r<   r=   r>   r~   r}   r   r   r   r0   r0   r1   rd     sP   
)zImage._process_jpgc                 C   sB   t }t }td|dd d }td|dd d }d||||fS )N<h   rr   r   
   gif)r*   r   )r/   ri   r=   r>   r:   r<   r0   r0   r1   rh   M  s
   zImage._process_gifc                 C   s6   t d|dd d }t d|dd d }d||fS )Nr\         r      bmpr   )r/   ri   r:   r<   r0   r0   r1   re   W  s   
zImage._process_bmpc                 C   s   t }t }td|dd d }td|dd d }td|dd d }td|dd d }td|dd	 d }t|| | | }	t|| | | }
d
|	|
||fS )Nr   r   rr   r   r   rs   r   z<Hrt   wmf)r*   r   float)r/   ri   r=   r>   x1y1x2y2inchr:   r<   r0   r0   r1   rf   ]  s   zImage._process_wmfc                 C   s  t d|dd d }t d|dd d }t d|dd d }t d|dd d }|| }|| }t d|dd d }t d|dd	 d }	t d|d	d
 d }
t d|d
d d }d|
|  }d||	  }|d | }|d | }|d7 }|d7 }d||||fS )Nz<lrr   rs   r   rt                $   rZ   g{Gz?gffffff9@r	   emfr   )r/   ri   bound_x1bound_y1bound_x2bound_y2r:   r<   frame_x1frame_y1frame_x2frame_y2width_mm	height_mmr=   r>   r0   r0   r1   rg   q  s"   zImage._process_emf)N)r   N)!__name__
__module____qualname____doc__r   r   r   r   r2   r6   propertyr4   r   r:   r<   r=   r>   r@   setterboolrF   r   rH   rS   r.   bytesr   rc   rd   rh   re   rf   rg   r0   r0   r0   r1   r      sL    *
D
 * 6
 $r   )r`   r   ior   pathlibr   structr   typingr   r   xlsxwriter.urlr   
exceptionsr
   r   r*   r   r0   r0   r0   r1   <module>   s   	