o
    tBh5                     @   s<  d dl Z d dlZd dlZd dlmZ d dlmZ zd dlZd dl	Z	d dl
Z
W n3 eyY   zd dlmZm	Z	m
Z
 W n eyV   d dlZedZed d Z Z	Z
Y nw Y nw ddd	Zd
d Zdd ZdddZdZdeZejejeZG dd dZG dd deZedu rdZdS G dd deZdS )    N)BinaryIO)version)Image	ImageDraw	ImageFont	pyBarcodez'Pillow not found. Image output disabled,  c                 C   s   | | d S )Nffffff9@ )mmdpir
   r
   e/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/barcode/writer.pymm2px   s   r   c                 C   s   | d S )Ng>?r
   )ptr
   r
   r   pt2mm   s   r   c                 K   s"   |  D ]
\}}| || qd S N)itemssetAttribute)element
attributeskeyvaluer
   r
   r   _set_attributes   s   r   Fc                 C   sB   t j }|ddd}|d d| r|nd }t|jddd |S )Nsvgz-//W3C//DTD SVG 1.1//ENz0http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtdz1.1zhttp://www.w3.org/2000/svg)r   xmlns)xmldomgetDOMImplementationcreateDocumentTypecreateDocumentr   documentElement)with_doctypeimpdoctypedocumentr
   r
   r   create_svg_object$   s   
r%   z	{0:.3f}mmz$Autogenerated with python-barcode {}c                   @   sF   e Zd ZdZ	dddZdddZdd	 Zd
d Zdd Zdd Z	dS )
BaseWritera  Baseclass for all writers.

    Initializes the basic writer options. Childclasses can add more
    attributes and can set them directly or using
    `self.set_options(option=value)`.

    :parameters:
        initialize : Function
            Callback for initializing the inheriting writer.
            Is called: `callback_initialize(raw_code)`
        paint_module : Function
            Callback for painting one barcode module.
            Is called: `callback_paint_module(xpos, ypos, width, color)`
        paint_text : Function
            Callback for painting the text under the barcode.
            Is called: `callback_paint_text(xpos, ypos)` using `self.text`
            as text.
        finish : Function
            Callback for doing something with the completely rendered
            output.
            Is called: `return callback_finish()` and must return the
            rendered output.
    Nc                 C   sh   ||||d| _ d| _d| _tjtdd| _d| _d| _	d| _
d| _d| _d| _d	| _d
| _d| _d S )N)
initializepaint_module
paint_textfinish
   fontszDejaVuSansMono.ttfg      @whiteblack       T)
_callbacksmodule_widthmodule_heightospathjoinPATH	font_path	font_size
quiet_zone
background
foregroundtexthumantext_distancetext_line_distancecenter_text)selfr'   r(   r)   r*   r
   r
   r   __init__P   s"   
zBaseWriter.__init__r   c                 C   s   d| j  || j  }d| j|  }t| j }| jr5| jr5|t| jd | | j 7 }|| j	|d  7 }t
t||t
t||fS )a  Calculates the size of the barcode in pixel.

        :parameters:
            modules_per_line : Integer
                Number of modules in one line.
            number_of_lines : Integer
                Number of lines of the barcode.
            dpi : Integer
                DPI to calculate.

        :returns: Width and height of the barcode in pixel.
        :rtype: Tuple
                  @r1   )r;   r3   r4   lenr>   
splitlinesr:   r   r@   rA   intr   )rC   modules_per_linenumber_of_linesr   widthheightnumber_of_text_linesr
   r
   r   calculate_sizef   s   zBaseWriter.calculate_sizec                 C   s   t )a  Saves the rendered output to `filename`.

        :parameters:
            filename : String
                Filename without extension.
            output : String
                The rendered output.

        :returns: The full filename with extension.
        :rtype: String
        )NotImplementedErrorrC   filenameoutputr
   r
   r   save~   s   zBaseWriter.savec                 C   s   || j |< dS )a2  Register one of the three callbacks if not given at instance
        creation.

        :parameters:
            action : String
                One of 'initialize', 'paint_module', 'paint_text', 'finish'.
            callback : Function
                The callback function for the given action.
        N)r2   )rC   actioncallbackr
   r
   r   register_callback   s   
zBaseWriter.register_callbackc                 C   s6   |  D ]\}}|d}t| |rt| || qdS )a  Sets the given options as instance attributes (only
        if they are known).

        :parameters:
            options : Dict
                All known instance attributes and more if the childclass
                has defined them before this call.

        :rtype: None
        _N)r   lstriphasattrsetattr)rC   optionsr   valr
   r
   r   set_options   s   

zBaseWriter.set_optionsc                 C   s  | j d dur| j d | d}t|D ]\}}	 |d7 }d}g }tdt|d D ]%}|| ||d  kr;|d7 }q*|| dkrG|| n||  d}q*| j}|}	|D ]%}
|
dk ra| j}n| j}| j d ||| jt	|
 | || jt	|
 7 }qW|}|d t|kr| j d ||| j| j || j
7 }q| jr| j d	 dur|| j7 }| jr|	||	 d
  }n|	}| j d	 || | j d  S )zRenders the barcode to whatever the inheriting writer provides,
        using the registered callbacks.

        :parameters:
            code : List
                List of strings matching the writer spec
                (only contain 0 or 1).
        r'   Ng      ? r1   r   1r(   r)   rF   r*   )r2   	enumeraterangerG   appendr;   r<   r=   r3   absr4   r>   r@   rB   )rC   codeyposcclinecmlistixposbxsmodcolorbxer
   r
   r   render   sL   	

zBaseWriter.render)NNNNr   )
__name__
__module____qualname____doc__rD   rO   rT   rW   r^   rq   r
   r
   r
   r   r&   7   s    

r&   c                   @   sJ   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zde	fddZ
dS )	SVGWriterc                 C   sB   t | | j| j| j| j d| _d| _d| _d | _	d | _
d | _d S )NFr	   T)r&   rD   _init_create_module_create_text_finishcompressr   r!   	_document_root_grouprC   r
   r
   r   rD      s   
zSVGWriter.__init__c                 C   s   |  t|d t|| j\}}t| j| _| jj| _t	|t	|d}t
| jfi | | j| jt | jd}ddi}t
|fi | | j|| _| jd}ddd	| jd	}t
|fi | | j| d S )
Nr   )rL   rM   gidbarcode_grouprectz100%zfill:{})rL   rM   style)rO   rG   r   r%   r!   r}   r    r~   SIZEformatr   appendChildcreateCommentCOMMENTcreateElementr   r<   )rC   re   rL   rM   r   groupr<   r
   r
   r   rx      s&    

zSVGWriter._initc                 C   s\   | j d}t|t|t|t| jd|d}t|fi | | j| d S )Nr   zfill:{};)xyrL   rM   r   )r}   r   r   r   r4   r   r   r   )rC   rl   rf   rL   ro   r   r   r
   r
   r   ry     s   
zSVGWriter._create_modulec                 C   s   | j dkr	| j }n| j}|dD ]=}| jd}t|t|d| j| jd}t	|fi | | j
|}|| | j| |t| j| j 7 }qd S )Nr/   
r>   z*fill:{};font-size:{}pt;text-anchor:middle;)r   r   r   )r?   r>   splitr}   r   r   r   r=   r:   r   createTextNoder   r   r   rA   )rC   rl   rf   barcodetextsubtextr   r   text_elementr
   r
   r   rz     s"   

zSVGWriter._create_textc                 C   s(   | j r
| jjddS | jjdtjddS )NzUTF-8)encodingz    )indentnewlr   )r|   r}   toxmltoprettyxmlr5   linesepr   r
   r
   r   r{   .  s
   zSVGWriter._finishc                 C   sv   | j rd|}t|d}|| |  |S d|}t|d}|| W d    |S 1 s4w   Y  |S )Nz{}.svgzwbz{}.svg)r|   r   gzipopenwriteclose)rC   rR   rS   	_filenamefr
   r
   r   rT   6  s   



zSVGWriter.savefpc                 C   s   | | dS )zoWrite `content` into a file-like object.

        Content should be a barcode rendered by this writer.
        N)r   rC   contentr   r
   r
   r   r   B  s   zSVGWriter.writeN)rs   rt   ru   rD   rx   ry   rz   r{   rT   r   r   r
   r
   r
   r   rw      s    rw   c                   @   sf   e Zd ZU eed< eed< eed< dddZdd	 Zd
d Zdd Z	dd Z
dd ZdefddZdS )ImageWriterr   moder   PNGRGBc                 C   s<   t | | j| j| j| j || _|| _d| _d| _	d| _
dS )aL  Initialise a new write instance.

            :params format: The file format for the generated image. This parameter can
                take any value that Pillow accepts.
            :params mode: The colour-mode for the generated image. Set this to RGBA if
                you wish to use colours with transparency.
            r   N)r&   rD   rx   _paint_module_paint_textr{   r   r   r   _image_draw)rC   r   r   r
   r
   r   rD   S  s   
zImageWriter.__init__c                 C   sB   |  t|d t|| j}t| j|| j| _t	| j| _
d S )Nr   )rO   rG   r   r   newr   r<   r   r   Drawr   )rC   re   sizer
   r
   r   rx   d  s   zImageWriter._initc                 C   sP   t || jt || jft || | jt || j | jfg}| jj|||d d S )N)outlinefill)r   r   r4   r   	rectangle)rC   rl   rf   rL   ro   r   r
   r
   r   r   i  s   zImageWriter._paint_modulec                 C   s   t | j| jd }| jdD ]4}||\}}t|| j|d  t|| j|d  f}| j	j|||| j
d |t| jd | j 7 }qd S )NrE   r      )fontr   )r   truetyper9   r:   r>   r   getsizer   r   r   r=   r   rA   )rC   rl   rf   r   r   rL   rM   posr
   r
   r   r   s  s   zImageWriter._paint_textc                 C   s   | j S r   )r   r   r
   r
   r   r{     s   zImageWriter._finishc                 C   s(   d || j  }||| j   |S )Nz{}.{})r   lowerrT   upperrQ   r
   r
   r   rT     s   zImageWriter.saver   c                 C   s   |j || jd dS )zwWrite `content` into a file-like object.

            Content should be a barcode rendered by this writer.
            )r   N)rT   r   r   r
   r
   r   r     s   zImageWriter.writeN)r   r   )rs   rt   ru   str__annotations__rI   rD   rx   r   r   r{   rT   r   r   r
   r
   r
   r   r   N  s   
 

r   rr   )F) r   r5   xml.domr   	typing.ior   barcode.versionr   r   r   r   ImportErrorPILlogging	getLoggerloginfor   r   r   r%   r   r   r   r6   dirnameabspath__file__r8   r&   rw   r   r
   r
   r
   r   <module>   sB    




 1c