o
    >@iLE                     @  s  U 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 er[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G dd de
Zzd d
lmZ d dlmZ d dlmZ W n ey   d dlZedZed d Z ZZY nw d,ddZd-ddZdd Z d.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)e,d*< dS G d+d* d*e)Z+dS )0    )annotationsN)TYPE_CHECKING)BinaryIO)Callable)	TypedDict)version)	Generator)Literal)Image)	ImageDrawc                   @  s.   e Zd ZU ded< ded< ded< ded< dS )InternalTextliststartendxposbool	was_guardN__name__
__module____qualname____annotations__ r   r   L/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/barcode/writer.pyr      
   
 r   c                   @  s.   e Zd ZU ded< ded< ded< ded< dS )	CallbacksCallable | None
initializer   paint_module
paint_textfinishNr   r   r   r   r   r      r   r   )	ImageFont	pyBarcodez'Pillow not found. Image output disabledmmfloatdpiintreturnc                 C  s   | | d S )Ngffffff9@r   )r#   r%   r   r   r   mm2px-   s   r(   ptc                 C  s   | d S )Ng>?r   )r)   r   r   r   pt2mm1   s   r*   c                 K  s"   |  D ]
\}}| || qd S N)itemssetAttribute)element
attributeskeyvaluer   r   r   _set_attributes5   s   r2   Fxml.dom.minidom.Documentc                 C  sP   t jj }|d us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minidomgetDOMImplementationcreateDocumentTypecreateDocumentr2   documentElement)with_doctypeimpdoctypedocumentr   r   r   create_svg_object:   s   rA   z	{0:.3f}mmz"Autogenerated with python-barcode c                   @  s   e Zd ZU dZded< ded< ded< ded< ded	< ded
< ded< ded< ded< ded< ded< ded< ded< ded< ded< ded< d@dd ZdAd%d&ZdBd(d)ZdCd-d.ZdDd1d2Z	dEd5d6Z
dFd9d:ZdGd=d>Zd?S )H
BaseWritera   Baseclass for all writers.

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

    :param initialize: Callback for initializing the inheriting writer.
        Is called: ``callback_initialize(raw_code)``
    :param paint_module:
        Callback for painting one barcode module.
        Is called: ``callback_paint_module(xpos, ypos, width, color)``
    :param paint_text: Callback for painting the text under the barcode.
        Is called: ``callback_paint_text(xpos, ypos)`` using `self.text`
        as text.
    :param finish: Callback for doing something with the completely rendered
        output. Is called: ``return callback_finish()`` and must return the
        rendered output.
    r   
_callbacksr$   module_widthmodule_heightstr	font_path	font_size
quiet_zonez	str | int
background
foregroundtexthumantext_distancetext_line_distancer   center_textguard_height_factor
margin_topmargin_bottomr   r   r   r   r   r    r'   Nonec                 C  sz   ||||d| _ d| _d| _tjtdd| _d| _d| _	d| _
d| _d| _d| _d	| _d
| _d| _d| _d
| _d
| _d S )N)r   r   r   r    
   fontszDejaVuSansMono.ttfg      @whiteblack       Tg?)rC   rD   rE   ospathjoinPATHrG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   )selfr   r   r   r    r   r   r   __init__r   s(   
zBaseWriter.__init__modules_per_liner&   number_of_linestuplec                 C  sz   d| j  || j  }| j| j | j|  }t| j }| jr9| jr9|t	| jd | | j
 7 }|| j|d  7 }||fS )zCalculates the size of the barcode in pixel.

        :param modules_per_line: Number of modules in one line.
        :param number_of_lines: Number of lines of the barcode.

        :returns: Width and height of the barcode in pixel.
           r[   )rI   rD   rS   rR   rE   lenrL   
splitlinesrH   r*   rN   rO   )r`   rb   rc   widthheightnumber_of_text_linesr   r   r   calculate_size   s   zBaseWriter.calculate_sizefilenamec                 C     t )zSaves the rendered output to `filename`.

        :param filename: Filename without extension.
        :param output: The rendered output.

        :returns: The full filename with extension.
        NotImplementedErrorr`   rl   outputr   r   r   save   s   zBaseWriter.saveaction=Literal['initialize', 'paint_module', 'paint_text', 'finish']callbackc                 C  s   || j |< dS )zRegister one of the three callbacks if not given at instance creation.

        :param action: One of 'initialize', 'paint_module', 'paint_text', 'finish'.
        :param callback: The callback function for the given action.
        N)rC   )r`   rs   ru   r   r   r   register_callback   s   
zBaseWriter.register_callbackoptionsdictc                 C  s6   |  D ]\}}|d}t| |rt| || qdS )zSets the given options as instance attributes (only
        if they are known).

        :param options: All known instance attributes and more if the child class
            has defined them before this call.
        _N)r,   lstriphasattrsetattr)r`   rw   r0   valr   r   r   set_options   s   

zBaseWriter.set_optionsline'Generator[tuple[int, float], str, None]c                 c  s    |d7 }d}t t|d D ]3}|| ||d  kr |d7 }q|| dkr,|dfV  n|| dkr9|| jfV  n| | jfV  d}qdS )a4  
        Pack line to list give better gfx result, otherwise in can
        result in aliasing gaps
        '11010111' -> [2, -1, 1, -1, 3]

        This method will yield a sequence of pairs (width, height_factor).

        :param line: A string matching the writer spec (only contain 0 or 1 or G).
         r[   1GN)rangerf   rQ   )r`   r   cir   r   r   packed   s   

zBaseWriter.packedcode	list[str]c                 C  s\  | j d dur| j d | | j}| j}t|dkrtd|d }| j}|}g g g dd}| |D ]S\}}	|dk r?| j}
n*| j}
|d rV|	dkrV|d	 	| d|d< n|d si|	dkri|d
 	| d|d< ||	 | _| j d ||| j
t| |
 || j
t| 7 }q3|	dkr|d	 	| || _|}|| j7 }| jr(| j d dur(|d
 s|| j7 }| jr||| d  n|}| j d || na|d| j
  g|d< |d
 d t|d
 |d	 D ]\}}|d 	||| d   q|d 	|d	 d d| j
   |t| j7 }| jd}t||d D ]\}}|| _| j d || q| j d  S )a  Renders the barcode to whatever the inheriting writer provides,
        using the registered callbacks.

        :parameters:
            code : List
                List consisting of a single string matching the writer spec
                (only contain 0 or 1 or G).
        r   Nr[   "Only one line of code is supportedr   F)r   r   r   r   r   r   r   Tr   r   g       @   r   re   r   r    )rC   rR   rE   rf   ro   rI   r   rJ   rK   appendrD   absrL   rN   rP   popzipr*   rH   split)r`   r   yposbase_heightr   r   bxsrL   modheight_factorcolorbxeseblockstext_r   r   r   render   sd   	



 zBaseWriter.renderfpr   c                 C  rm   r+   rn   r`   contentr   r   r   r   write2  s   zBaseWriter.writeN)
r   r   r   r   r   r   r    r   r'   rT   )rb   r&   rc   r&   r'   rd   rl   rF   r'   rF   )rs   rt   ru   r   r'   rT   )rw   rx   r'   rT   )r   rF   r'   r   r   r   r   r   r'   rT   )r   r   r   __doc__r   ra   rk   rr   rv   r~   r   r   r   r   r   r   r   rB   N   s4   
 







UrB   c                      sT   e Zd Zd fddZdddZd	d
 Zdd Zdd ZdddZdddZ	  Z
S )	SVGWriterr'   rT   c                   s6   t  | j| j| j| j d| _d| _|  |  |  d S )NFT)superra   _init_create_module_create_text_finishcompressr=   r`   	__class__r   r   ra   7  s   zSVGWriter.__init__r   r   c                 C  s  t |dkr
td|d }| t |d\}}t| j| _| jj| _t	|t	|d}t
| jfi | trC| j| jt | jd}ddi}t
|fi | | j|| _| jd ur| jd}d	d	d
| j d}t
|fi | | j| d S d S )Nr[   r   r   )rh   ri   gidbarcode_grouprectz100%fill:)rh   ri   style)rf   ro   rk   rA   r=   	_documentr<   _rootSIZEformatr2   COMMENTappendChildcreateCommentcreateElement_grouprJ   )r`   r   r   rh   ri   r/   grouprJ   r   r   r   r   D  s2   


zSVGWriter._initc                 C  sl   || j kr4| jd}t|t|t|t| jd| dd}t|fi | | j| d S d S )Nr   r   ;)xyrh   ri   r   )	rJ   r   r   r   r   rE   r2   r   r   )r`   r   r   rh   r   r.   r/   r   r   r   r   b  s   


zSVGWriter._create_modulec                 C  s   | j dkr| j n| j}|dD ]@}| jd}t|t|d| j d| j dd}t	|fi | | j
|}|| | j| |t| j| j 7 }qd S )NrY   
rL   r   z;font-size:zpt;text-anchor:middle;)r   r   r   )rM   rL   r   r   r   r   r   rK   rH   r2   createTextNoder   r   r*   rO   )r`   r   r   barcodetextsubtextr.   r/   text_elementr   r   r   r   p  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toprettyxmlr\   linesepr   r   r   r   r     s
   zSVGWriter._finishrl   rF   c                 C  s   | j r*| d}t|d}|| |  W d    |S 1 s#w   Y  |S | d}t|d}|| W d    |S 1 sEw   Y  |S )Nz.svgzwbz.svg)r   gzipopenr   close)r`   rl   rq   	_filenamefr   r   r   rr     s    





zSVGWriter.saver   r   c                 C  s   | | dS )zoWrite `content` into a file-like object.

        Content should be a barcode rendered by this writer.
        N)r   r   r   r   r   r     s   zSVGWriter.writer'   rT   r   r   r   )r   r   r   ra   r   r   r   r   rr   r   __classcell__r   r   r   r   r   6  s    

r   ztype | NoneImageWriterc                      st   e Zd ZU ded< ded< ded< d#d$ fddZd%ddZd&ddZdd Zd'ddZd(ddZ	d)d!d"Z
  ZS )*r   rF   r   moder&   r%   PNGRGB,  r'   rT   c                   s8   t  | j| j| j| j || _|| _|| _|  |  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.
            N)	r   ra   r   _paint_module_paint_textr   r   r   r%   )r`   r   r   r%   r   r   r   ra     s   zImageWriter.__init__r   r   c                 C  s   t d u rtdt|dkrtd|d }| t|d\}}tt|| jtt|| jf}t	| j
|| j| _t | j| _d S )Nz&Pillow not found. Cannot create image.r[   r   r   )r   RuntimeErrorrf   ro   rk   r&   r(   r%   r
   newr   rJ   _imageDraw_draw)r`   r   r   rh   ri   sizer   r   r   r     s    zImageWriter._initr   r$   r   rh   c                 C  sT   t || jt || jft || | jd t || j | jfg}| jj|||d d S )Nr[   )outlinefill)r(   r%   rE   r   	rectangle)r`   r   r   rh   r   r   r   r   r   r     s   zImageWriter._paint_modulec                 C  s   t d usJ | jdkr| jn| j}ttt| j| j}|dkr"d S t | j	|}|
dD ]&}t|| jt|| jf}| jj|||| jdd |t| jd | j 7 }q.d S )NrY   r   r   md)fontr   anchorre   )r!   rM   rL   r&   r(   r*   rH   r%   truetyperG   r   r   rK   rO   )r`   r   r   r   rH   r   r   posr   r   r   r     s   

zImageWriter._paint_textT_Imagec                 C  s   | j S r+   )r   r   r   r   r   r     s   zImageWriter._finishrl   c                 C  s*   | d| j   }||| j   |S )N.)r   lowerrr   upperrp   r   r   r   rr     s   zImageWriter.saver   r   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)rr   r   r   r   r   r   r     s   zImageWriter.write)r   r   r   r   )r   r   r'   rT   )r   r$   r   r$   rh   r$   )r'   r   r   r   )r   r   r   r   ra   r   r   r   r   rr   r   r   r   r   r   r   r     s   
 




)r#   r$   r%   r&   r'   r$   )r)   r$   r'   r$   )F)r'   r3   )-
__future__r   r   r\   xml.dom.minidomr6   typingr   r   r   r   barcode.versionr   r   r	   	PIL.Imager
   r   PIL.ImageDrawr   T_ImageDrawr   r   PILr!   ImportErrorlogging	getLoggerloginfor(   r*   r2   rA   r   r   r]   dirnameabspath__file__r_   rB   r   r   r   r   r   r   r   <module>   sN    




 ij