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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jZG d
d dejjZG dd deZdS )z]
This module contains everything needed to implement a LayoutElement representing a barcode.
    N)Decimal)Image)Color)HexColor)	Alignmentc                   @   s\   e Zd ZdZdZdZdZdZdZdZ	dZ
d	ZdZdZd
ZdZdZdZdZdZdZdZdZdS )BarcodeTypezF
    This Enum represents the various types of supported barcodes
    code128code39eanean13ean14ean8isbn13gs1_128isbn10issnitfjanpznqrupcaN)__name__
__module____qualname____doc__CODE_128CODE_39EANEAN_13EAN_14EAN_8GS_1GS_128GTINISBNISBN_10ISBN_13ISSNITFJANPZNQRUPCUPC_A r.   r.   X/home/ubuntu/.local/lib/python3.10/site-packages/borb/pdf/canvas/layout/image/barcode.pyr      s*    r   c                       s:   e Zd ZdZ fddZdejfddZd
dd	Z  Z	S )InMemoryBarcodeWriterzo
    This class inherits from BarcodeImageWriter, and enables
    access to the PILImageModule being built
    c                    s   t t| jddd d | _d S )NJPEGRGB)formatmode)superr0   __init__output_imageself	__class__r.   r/   r6   9   s   
zInMemoryBarcodeWriter.__init__returnc                 C   s   | j dusJ | j S )z
        This function returns the PIL.Image.Image representing the barcode
        :return:    the output PIL.Image.Image
        Nr7   r8   r.   r.   r/   get_output_imageE   s   z&InMemoryBarcodeWriter.get_output_imageNc                 C   s
   || _ dS )z
        Saves the rendered output to `filename` storing the output.
        :param filename:    the filename (not used)
        :param output:      the rendered output
        :return:            None
        Nr=   )r9   filenameoutputr.   r.   r/   saveM   s   
zInMemoryBarcodeWriter.save)r<   N)
r   r   r   r   r6   PILImageModuler   r>   rA   __classcell__r.   r.   r:   r/   r0   /   s
    	r0   c                6       s,  e Zd ZdZdeddededededddededdejedededededededededejdfde	d	e
d
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dedededededededed ed!eje f4 fd"d#Zde	d	e
fd$d%Zde	fd&d'Z  ZS )(BarcodezD
    This implementation of LayoutElement represents a barcode.
    F000000r      ffffffNdatatype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
fill_colorheighthorizontal_alignmentmargin_bottommargin_leftmargin_right
margin_toppadding_bottompadding_leftpadding_rightpadding_topstroke_colorvertical_alignmentwidthc                    s   || _ || _|| _|| _||ksJ |tjkr| |}n| ||}tt	| j
|fi d|d|d|d|d|d|d|	d|
d	|d
|d|d|d|d|d|d|d|d|d|d|d|d| || _d S )NrJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r`   ra   )_data_type_stroke_color_fill_colorr   r+   _generate_qr_code_generate_image_except_qr_coder5   rD   r6   _background_color)r9   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   imager:   r.   r/   r6   `   sp   

	

zBarcode.__init__c                 C   st   t  }tj|j||d}|jd| j  | j  dd |	 }|d us*J |j
dks1J |jdks8J |S )N)namecodewriter )
foreground
background)optionsr   )r0   barcodegetvaluerA   rd   to_rgbto_hex_stringre   r>   ra   rU   )r9   rH   rI   rl   r
   ri   r.   r.   r/   rg      s   	z&Barcode._generate_image_except_qr_codec                 C   s\   t jd t jjddd}|| |jdd |j| j 	 | j
 	 d}|d}|S )N
      )versionerror_correctionbox_sizeborderT)fit)rT   
back_colorr2   )qrcodeQRCode	constantsERROR_CORRECT_Ladd_datamake
make_imagerd   rt   ru   re   convert)r9   rH   r   	png_image	jpg_imager.   r.   r/   rf      s   

zBarcode._generate_qr_code)r   r   r   r   r   r   r   LEFTTOPstrr   boolr   typingOptionalr6   rg   rf   rC   r.   r.   r:   r/   rD   W   s    	
IrD   )r   r   decimalr   enumrq   r~   PILr   rB   borb.pdf.canvas.color.colorr   r   "borb.pdf.canvas.layout.image.image%borb.pdf.canvas.layout.layout_elementr   Enumr   rl   ImageWriterr0   rD   r.   r.   r.   r/   <module>   s   (