U
    go                  
   @   s   zd dl Z d dlmZmZ W n, ek
rH Z zedeW 5 dZ[X Y nX d dlmZ d dlm	Z	 d dl
mZ ddgZed	d
dZdZdd ZG dd deZG dd deZdS )    N)
BaseWritermm2pxzpyhanko.pdf_utils.barcodes requires pyHanko to be installed with the [image-support] option. You can install missing dependencies by running "pip install 'pyHanko[image-support]'".)
PdfContent)BoxConstraints)rd
BarcodeBoxPdfStreamBarcodeWriterreturnc                 C   s   | dkrdS dS )NZwhites   1 1 1s   0 0 0 )Zcolourr   r   >/tmp/pip-unpacked-wheel-owvgwkas/pyhanko/pdf_utils/barcodes.pybarcode_colour_to_pdf   s    r   H   c                 C   s   t | d t S )Ngffffff9@)r   PDF_UUPI)Zmm_lenr   r   r   mm2uu!   s    r   c                   @   sP   e Zd ZdZdd Zdd Zdd Zedd	d
Ze	edddZ
dd ZdS )r   z
    Implementation of writer class for the python-barcode library to output
    PDF graphics operators.
    Note: _paint_text is intentionally dummied out.
    Please use the functionality implemented in pyhanko.pdf_utils.text instead.
    c                 C   s*   dd }t | | j| j|| j d | _d S )Nc                  _   s   d S Nr   )_args_kwargsr   r   r   dummy.   s    z.PdfStreamBarcodeWriter.__init__.<locals>.dummy)r   __init___init_paint_module_finish_command_stream)selfr   r   r   r   r   -   s        zPdfStreamBarcodeWriter.__init__c                 C   sF   |  t|d t|\}}tt|ttt|tf| _dg| _d S )Nr      q)Zcalculate_sizelenintr   r   sizer   )r   codewidthheightr   r   r   r   9   s    zPdfStreamBarcodeWriter._initc              	   C   s4   | j dt|t|t|t|t| jf  d S )Ns   %s rg %g %g %g %g re f)r   appendr   r   Zmodule_height)r   ZxposZyposr    colorr   r   r   r   >   s    z$PdfStreamBarcodeWriter._paint_moduler	   c                 C   s   | j d | jS )N   Q)r   r"   command_streamr   r   r   r   r   J   s    zPdfStreamBarcodeWriter._finishc                 C   s   d | jS )N   
)joinr   r&   r   r   r   r%   N   s    z%PdfStreamBarcodeWriter.command_streamc                 C   s   d S r   r   )r   filenameoutputr   r   r   saveS   s    zPdfStreamBarcodeWriter.saveN)__name__
__module____qualname____doc__r   r   r   bytesr   propertyr%   r+   r   r   r   r   r   %   s   c                       s.   e Zd ZdZ fddZedddZ  ZS )r   z
    Thin wrapper around python-barcode functionality.

    This will render a barcode of the specified type as PDF graphics operators.
    c                    sR   || _ || _t }tj| j ||d}| | _|j\}}t j	t
||dd d S )N)r   writer)r    r!   )Zbox)barcode_typer   r   barcodeZget_barcoderender_barcode_commandsr   superr   r   )r   r3   r   r2   bwh	__class__r   r   r   ^   s    

zBarcodeBox.__init__r	   c                 C   s   | j S r   )r6   r&   r   r   r   r5   l   s    zBarcodeBox.render)r,   r-   r.   r/   r   r0   r5   __classcell__r   r   r;   r   r   W   s   )r4   Zbarcode.writerr   r   ImportErroreZpyhanko.pdf_utils.contentr   Zpyhanko.pdf_utils.layoutr   Zpyhanko.pdf_utils.miscr   __all__r0   r   r   r   r   r   r   r   r   r   <module>   s    	2