
    ""f?                        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Zd dl	Z	d dl
Z
nY# e$ rQ 	 d dlmZ d dlm	Z	 d dlm
Z
 n:# e$ r2 d dlZ ej        d          Ze                    d           dxZxZ	Z
Y nw xY wY nw xY wdd
Zd Zd ZddZdZde Zej                            ej                            e                    Z G d d          Z G d de          ZedZdS  G d de          ZdS )    N)BinaryIO)version)Image)	ImageDraw)	ImageFont	pyBarcodez'Pillow not found. Image output disabled,  c                     | |z  dz  S )Ngffffff9@ )mmdpis     E/var/www/html/api/venv/lib/python3.11/site-packages/barcode/writer.pymm2pxr      s    H    c                     | dz  S )Ng>?r   )pts    r   pt2mmr      s    r   c                 f    |                                 D ]\  }}|                     ||           d S N)itemssetAttribute)element
attributeskeyvalues       r   _set_attributesr   !   sD     &&(( ) )
US%(((() )r   Fc                     t           j                                        }|                    ddd          }|                    d d| r|nd           }t          |j        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documents       r   create_svg_objectr*   &   s~    
'
&
&
(
(C$$!: G
 !!$,/PwwDQQH %7S    Or   z	{0:.3f}mmz"Autogenerated with python-barcode c                   J    e Zd ZdZ	 	 	 	 d	 ddZd Zd Zd Zd Zd	 Z	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.
    Nreturnc                 8   ||||d| _         d| _        d| _        t          j                            t          dd          | _        d| _        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       Tg?)
_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guard_height_factor
margin_topmargin_bottom)selfr/   r0   r1   r2   s        r   __init__zBaseWriter.__init__R   s     %($	
 
 dG5IJJ!!	
"##& r   c                 :   d| j         z  || j        z  z   }| j        | j        z   | j        |z  z   }t          | j                                                  }| j        r<| j        r5|t          | j                  dz  |z  | j
        z   z  }|| j        |dz
  z  z  }||fS )aJ  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.

        :returns: Width and height of the barcode in pixel.
        :rtype: Tuple
           r9   )rC   r;   rM   rL   r<   lenrF   
splitlinesrB   r   rH   rI   )rN   modules_per_linenumber_of_lineswidthheightnumber_of_text_liness         r   calculate_sizezBaseWriter.calculate_sizeo   s     DO#&69J&JJ043E3WW 	  #49#7#7#9#9::> 	Kdi 	Kdn%%),@@4CUUF d-1E1IJJFf}r   c                     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rN   filenameoutputs      r   savezBaseWriter.save   s
     "!r   c                     || 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)r:   )rN   actioncallbacks      r   register_callbackzBaseWriter.register_callback   s     #+r   c                     |                                 D ];\  }}|                    d          }t          | |          rt          | ||           <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)rN   optionsr   vals       r   set_optionszBaseWriter.set_options   s]       	( 	(HC**S//CtS!! (c3'''	( 	(r   c              #     K   |dz  }d}t          dt          |          dz
            D ]V}||         ||dz            k    r|dz  }||         dk    r|dfV  n$||         dk    r|| j        fV  n| | j        fV  d}WdS )am  
        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).

        :parameters:
            line: String
                A string matching the writer spec
                (only contain 0 or 1 or G).
         r9   r   1GN)rangerR   rK   )rN   linecis       r   packedzBaseWriter.packed   s       	q#d))a-(( 
	 
	AAw$q1u+%%Q7c>>a&LLLL!W^^d6777772t78888
	 
	r   c           
      T   | j         d          | j         d         |           | j        }| j        }t          |          D ]b\  }}| j        }|}g g g dd}|                     |          D ]\  }	}
|	dk     r| j        }nd| j        }|d         r'|
dk    r!|d                             |           d|d<   n.|d         s&|
dk    r |d                             |           d	|d<   ||
z  | _         | j         d
         ||| j	        t          |	          z  |           || j	        t          |	          z  z  }|
dk    r|d                             |           || _        |}|dz   t          |          k    r# | j         d
         ||| j        | j                   || j        z  }d| j        rh| j         d         Z|d         s7|| j        z  }| j        r|||z
  dz  z   n|} | j         d         ||           n|d| j	        z  z
  g|d<   |d                             d           t!          |d         |d                   D ])\  }}|d                             |||z
  dz  z              *|d                             |d         d         d| j	        z  z              | j                            d          | _        |t%          | j                  z  }| j        }t!          ||d                   D ]#\  }}|| _         | j         d         ||           $ | j         d                     S )a  Renders 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 or G).
        r/   NF)startendxpos	was_guardr9   ry   rw   rv   Tr0   r1   g       @   rx   r   rQ   rm   r2   )r:   rL   r<   	enumeraterC   rt   rD   rE   appendr;   absrR   rF   rH   rJ   popzipsplitr   rB   )rN   codeyposbase_heightccrq   rx   bxsrF   modheight_factorcolorbxeseblockstext_s                    r   renderzBaseWriter.render   s    ?<(4)DOL)$///(!$ -	' -	'HB?DC" D '+kk$&7&7 1 1"]77 OEE OEK( 1]a-?-?U**4000,1[))!+. 1=A3E3EW,,T222,0[)%0=%@"//$ 1CHH <e   )CHH44 A%%K&&t,,,%0"C Q3t99$$//$   D&&DD9 	>6B= >**262BKscCi3...--dD9999 !$a$*;&; ;<V W!!!$$$WtE{;; 9 9DAqL''QUaK8888 V##DKOa$:K6K$KLLL !IOOC00	dn---#&vtF|#<#< > >KE4 %DI1DOL1$====(tx(***r   )NNNNr-   N)__name__
__module____qualname____doc__rO   rY   r_   rc   rk   rt   r   r   r   r   r,   r,   9   s         4  
   :  0" " "
+ 
+ 
+( ( (   6Z+ Z+ Z+ Z+ Z+r   r,   c                   @    e Zd ZddZd Zd Zd Zd Zd Zd	e	fd
Z
dS )	SVGWriterr-   Nc                     t                               | | j        | j        | j        | j                   d| _        d| _        d | _        d | _	        d | _
        d S )NFT)r,   rO   _init_create_module_create_text_finishcompressr&   	_document_root_grouprN   s    r   rO   zSVGWriter.__init__*  sY    $*d143Ddl	
 	
 	
  
r   c                    |                      t          |d                   t          |                    \  }}t          | j                  | _        | j        j        | _        t                              |          t                              |          d}t          | j        fi | t          r7| 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   )rV   rW   gidbarcode_grouprectz100%fill:)rV   rW   style)rY   rR   r*   r&   r   r%   r   SIZEformatr   COMMENTappendChildcreateCommentcreateElementr   rD   )rN   r   rV   rW   r   grouprD   s          r   r   zSVGWriter._init4  sr   ++CQLL#d))DDv*4+<==^3
[[''kk&))
 

 	
11j111 	JJ""4>#?#?#H#HIII ,,S11O,
,,,,,j,,U33^11&99
.T_..
 


 	
11j111
+++++r   c                    || j         k    r| j                            d          }t                              |          t                              |          t                              |          t                              | j                  d| dd}t          |fi | | j                            |           d S d S )Nr   r   ;)xyrV   rW   r   )	rD   r   r   r   r   r<   r   r   r   )rN   rx   r   rV   r   r   r   s          r   r   zSVGWriter._create_moduleN  s    DO##n226::G[[&&[[&&U++++d&899)))) J G22z222K##G,,,,, $#r   c                 .   | 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        z   z  }d S )Nr7   
rF   z*fill:{};font-size:{}pt;text-anchor:middle;)r   r   r   )rG   rF   r   r   r   r   r   rE   rB   r   createTextNoder   r   r   rI   )rN   rx   r   barcodetextsubtextr   r   text_elements           r   r   zSVGWriter._create_text\  s    %)J"$4$4djj$)"((.. 	D 	DGn226::G[[&&[[&&ELLON  J G22z222>88AAL---K##G,,,E$.))D,CCCDD	D 	Dr   c                     | j         r| j                            d          S | j                            dt          j        d          S )NzUTF-8)encodingz    )indentnewlr   )r   r   toxmltoprettyxmlr=   linesepr   s    r   r   zSVGWriter._finishp  sM    = 	:>'''999~))g * 
 
 	
r   c                     | j         rD| d}t          j        |d          }|                    |           |                                 nB| d}t          |d          5 }|                    |           d d d            n# 1 swxY w Y   |S )Nz.svgzwbz.svg)r   gzipopenwriteclose)rN   r]   r^   	_filenamefs        r   r_   zSVGWriter.savex  s    = 	 #***I	)T**AGGFOOOGGIIII#)))Ii&&  !                             s   !BB
Bfpc                 0    |                     |           dS )zoWrite `content` into a file-like object.

        Content should be a barcode rendered by this writer.
        N)r   rN   contentr   s      r   r   zSVGWriter.write  s    
 	r   r   )r   r   r   rO   r   r   r   r   r_   r   r   r   r   r   r   r   )  s           , , ,4- - -D D D(
 
 

 
 
      r   r   c                   b    e Zd ZU eed<   eed<   eed<   dddZd	 Zd
 Zd Z	d Z
d ZdefdZdS )ImageWriterr   moder   PNGRGBr-   Nc                     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,   rO   r   _paint_module_paint_textr   r   r   r   _image_draw)rN   r   r   s      r   rO   zImageWriter.__init__  sZ     dj$"4d6F   !DKDIDHDKDJJJr   c                    |                      t          |d                   t          |                    \  }}t          t          || j                            t          t          || j                            f}t          j        | j        || j                  | _	        t          j        | j	                  | _        d S )Nr   )rY   rR   intr   r   r   newr   rD   r   r   Drawr   )rN   r   rV   rW   sizes        r   r   zImageWriter._init  s     //DGc$iiHHME6eTX..//U6485L5L1M1MND)DItT_EEDK"44DJJJr   c                    t          || j                  t          || j                  ft          ||z   | j                  dz
  t          || j        z   | j                  fg}| j                            |||           d S )Nr9   )outlinefill)r   r   r<   r   	rectangle)rN   rx   r   rV   r   r   s         r   r   zImageWriter._paint_module  s    tTX&&dDH(=(=>$,11A5$!33TX>>D J  u5 AAAAAr   c                    t          t          t          | j                  | j                            }t          j        | j        |          }| j        	                    d          D ]r}t          || j                  t          || j                  f}| j
                            |||| j        d           |t          | j                  dz  | j        z   z  }sd S )Nr   md)fontr   anchorrQ   )r   r   r   rB   r   r   truetyperA   rF   r   r   rE   rI   )rN   rx   r   rB   r   r   poss          r   r   zImageWriter._paint_text  s    E%"7"7BBCCI%dni@@D9??400 L L$))$)) 
t$/$      dn--1D4KKKL Lr   c                     | j         S r   )r   r   s    r   r   zImageWriter._finish  s
    ;r   c                     | d| j                                          }|                    || j                                                    |S )N.)r   lowerr_   upperr\   s      r   r_   zImageWriter.save  sJ    "::T[%6%6%8%8::HKK$+"3"3"5"5666Or   r   c                 >    |                     || j                   dS )zwWrite `content` into a file-like object.

            Content should be a barcode rendered by this writer.
            )r   N)r_   r   r   s      r   r   zImageWriter.write  s"    
 LLDKL00000r   )r   r   r   )r   r   r   str__annotations__r   rO   r   r   r   r   r_   r   r   r   r   r   r   r     s         				 	 	 	 	"	5 	5 	5	B 	B 	B	L 	L 	L	 	 		 	 	
	1X 	1 	1 	1 	1 	1 	1r   r   )r	   )F)r   r=   xml.domr    typingr   barcode.versionr   r   r   r   ImportErrorPILlogging	getLoggerloginfor   r   r   r*   r   r   r>   dirnameabspath__file__r@   r,   r   r   r   r   r   <module>r      sQ    				        # # # # # #-LLL 
- 
- 
-	-!!!!!!!!!!!!! - - -g,,:;;;(,,,	III-
-     ) ) )
    
8w
8
8	wrwx0011m+ m+ m+ m+ m+ m+ m+ m+`` ` ` ` `
 ` ` `F 	=KKK@1 @1 @1 @1 @1j @1 @1 @1 @1 @1s1   ' A=A A= 4A74A=6A77A=<A=