o
    gig(                     @   s   d Z ddlZddlm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 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dS )a
  
A watermark is an identifying image or pattern in paper that appears as various shades of lightness/darkness when viewed
by transmitted light (or when viewed by reflected light, atop a dark background), caused by thickness or density variations in the paper.

Watermarks have been used on postage stamps, currency, and other government documents to discourage counterfeiting.
There are two main ways of producing watermarks in paper; the dandy roll process, and the more complex cylinder mould process.

Watermarks vary greatly in their visibility; while some are obvious on casual inspection,
others require some study to pick out. Various aids have been developed, such as watermark fluid that wets the paper without damaging it.

A watermark is very useful in the examination of paper because it can be used for dating documents and artworks,
identifying sizes, mill trademarks and locations, and determining the quality of a sheet of paper.

The word is also used for digital practices that share similarities with physical watermarks.
In one case, overprint on computer-printed output may be used to identify output from an unlicensed trial version of a program.
In another instance, identifying codes can be encoded as a digital watermark for a music, video, picture, or other file.
    N)Decimal)Image)	ImageDraw)	ImageFont)Color)HexColor)	Rectangle)	Alignment)Pagec                :       s  e Zd ZdZdddeddededededddedededdejddddededededd	ejdf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deje dedeje deje deje deje ded ed!ed"ed#e
d$ed%eje f8 fd&d'Zed(ejej fd)d*Ze		+	,		-	d7d.e	de
d/e	dedeje d#e
d%eje d(ejfd0d1Zd2d3d4ed(df fd5d6Z  ZS )8	Watermarka:  
    A watermark is an identifying image or pattern in paper that appears as various shades of lightness/darkness when viewed
    by transmitted light (or when viewed by reflected light, atop a dark background), caused by thickness or density variations in the paper.

    Watermarks have been used on postage stamps, currency, and other government documents to discourage counterfeiting.
    There are two main ways of producing watermarks in paper; the dandy roll process, and the more complex cylinder mould process.

    Watermarks vary greatly in their visibility; while some are obvious on casual inspection,
    others require some study to pick out. Various aids have been developed, such as watermark fluid that wets the paper without damaging it.

    A watermark is very useful in the examination of paper because it can be used for dating documents and artworks,
    identifying sizes, mill trademarks and locations, and determining the quality of a sheet of paper.

    The word is also used for digital practices that share similarities with physical watermarks.
    In one case, overprint on computer-printed output may be used to identify output from an unlicensed trial version of a program.
    In another instance, identifying codes can be encoded as a digital watermark for a music, video, picture, or other file.
    -   NF000000r         333333?textangle_in_degreesbackground_color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
font_color	font_sizeheighthorizontal_alignmentmargin_bottommargin_leftmargin_right
margin_toppadding_bottompadding_leftpadding_rightpadding_toptransparencyvertical_alignmentwidthc                    s^  dt |  krdksJ  J d|  k rdksJ  J tt| jdi dtj|||  t||||dd|d|d|d	|d
|d|d|	d|
d|d|d|d|d|d urc|ntdd|d urn|ntdd|d ury|ntdd|d ur|ntdd|d|d|d|d| d S d|d|d|d|d| d S )Nr   @   r   image)r   font_color_as_hex_strr   r    r*   r,   r   r   r   r   r   r   r   r   r   r   r   r!   r"      r#   r$   r%   r&   r'   r(   r)   r+    )	lensuperr   __init___get_watermark_imageto_rgbto_hex_stringintr   )selfr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   	__class__r1   Z/home/ubuntu/.local/lib/python3.10/site-packages/borb/pdf/canvas/layout/image/watermark.pyr4   ;   s    

zWatermark.__init__returnc                  C   s*   t dg} | D ]
}| r|  S qd S )Nz7/usr/share/fonts/truetype/noto/NotoSansMono-Regular.ttf)pathlibPathexists)potential_fontsxr1   r1   r<   _get_font_path   s   zWatermark._get_font_path#000000         ?sr/   c                 C   s0  t |ptd}t |ptd}tjd||fd}	tdt |d  dd  }
t|	}t }d }|d urBtj	t
||d}d}nt }||d	d
  d }|d usWJ |j|d |d f|| ||
 ||
 d |	j||d |d fd}	|}|}d
}d
}|	 }td
|D ](}td
|D ] }||| |  dkrt||}t||}t||}t||}qq|| }|| }|	j||||fd}	|dkrt || }t || }|	j||ftjd}	tjd||fd}td
|D ])}td
|D ]!}|j||d  |d  ||d  |d  f|	j||fdd qq|S )NS  J  RGBA)modesize      )rL   r   rB   r   r   )xyfontr   fillstroke_fill)anglecenter)r   r   r   r   )box)rL   resample)rO   )rO   value)r8   r   PILImageModulenewhexr   r   rC   r   truetypestrload_defaultgetsizer   rotategetdatarangeminmaxcropresizeLANCZOSputpixelgetpixel)rG   r   r/   r   r    r*   r,   WHtext_imgtransparency_hexdraw	font_pathrP   scale_factormin_xmin_ymax_xmax_ydatayrB   whbackground_imgr1   r1   r<   r5      sh   
	



&zWatermark._get_watermark_imagepager
   available_spacec              	      s   t |  p
td}t |  ptd}|  j}|  j}t 	|t
t|d |d  t|d |d  t|t| dS )aJ  
        This method paints this LayoutElement on the given Page, in the available space
        :param page:                the Page on which to paint this LayoutElement
        :param available_space:     the available space (as a Rectangle) on which to paint this LayoutElement
        :return:                    None
        rH   rI   rN   N)r8   get_page_info	get_widthr   
get_heightget_PIL_imager,   r    r3   paintr   )r9   ry   rz   ri   rj   rv   rw   r:   r1   r<   r      s   

zWatermark.paint)r   rD   rE   NrF   N)__name__
__module____qualname____doc__r   r   r	   LEFTTOPr\   floattypingOptionalr   boolr4   staticmethodr>   r?   rC   r8   rX   r   r5   r   r   __classcell__r1   r1   r:   r<   r   $   s    	
F"Vr   )r   r   decimalr   r>   PILr   rX   r   r   borb.pdf.canvas.color.colorr   r   "borb.pdf.canvas.geometry.rectangler   "borb.pdf.canvas.layout.image.image%borb.pdf.canvas.layout.layout_elementr	   borb.pdf.page.pager
   r   r1   r1   r1   r<   <module>   s   