
    BPcA,                     \   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
 ej                                        Ze
j        j        Ze
j        j        ZdZdZdZdZ G d	 d
e          Z G d d          Z G d de          Z G d de          Z G d de          Z G d d          ZdS )z&
Provides wrappers for pixmap access.
    N)List)Iterable)GtkGLib	GdkPixbuf         c                       e Zd ZdZdS )PixmapLoadErrorzFailed to load a pixmapN)__name__
__module____qualname____doc__     2/usr/share/inkscape/extensions/inkex/gui/pixmap.pyr   r   &   s        !!!!r   r   c                   j    e Zd ZU dZg Zee         ed<   g Zee         ed<   d Z	d Z
ed             ZdS )PixmapFilterzBase class for filtering the pixmaps in a manager's output.

    required - List of values required for this filter.

    Use:

    class Foo(PixmapManager):
        filters = [ PixmapFilterFoo ]

    requiredoptionalc                     d| _         | j        D ]%}||vrd| _         t          | |||                    &| j        D ]}||v rt          | |||                    d S )NTF)enabledr   setattrr   )selfkwargskeys      r   __init__zPixmapFilter.__init__9   s    = 	0 	0C&  $c6#;////= 	0 	0Cf}}c6#;///	0 	0r   c                 J    t          dt          |           j        z            )z,Run filter, replace this methodwith your ownz9Please add 'filter' method to your PixmapFilter class %s.)NotImplementedErrortyper   )r   imgr   s      r   filterzPixmapFilter.filterE   s(    !G4jj!"
 
 	
r   c                     t          | t          t          f          r| | fS t          | t                    r#t	          |           dk    r| d         | d         fS dS )z5Tries to calculate a size that will work for the datar	   r   r   N)
isinstanceintfloatr   len)dats    r   to_sizezPixmapFilter.to_sizeL   s\     cC<(( 	:c8$$ 	$SQFCF##tr   N)r   r   r   r   r   r   str__annotations__r   r   r#   staticmethodr*   r   r   r   r   r   *   s         	 	 Hd3iHd3i
0 
0 
0
 
 
   \  r   r   c                   >     e Zd ZdZg dZ fdZd ZddZd Z xZ	S )	OverlayFilteraS  Adds an overlay to output images, overlay can be any name that
    the owning pixmap manager can find.

    overlay  : Name of overlay image
    position : Location of the image:
      0      - Full size (1 to 1 overlay, default)
      (x,y)  - Percentage from one end to the other position 0-1
    alpha    : Blending alpha, 0 - 255

    )positionoverlayalphac                     d| _         d | _        d| _         t                      j        |i | |                     | j                   \  | _        | _        d S )Nr   r      )r0   r1   r2   superr   r*   pad_xpad_yr   argsr   	__class__s      r   r   zOverlayFilter.__init__d   sT    
$)&)))!%dm!<!<
DJJJr   c                     d|vrt          d          |d                             |                    dd           p| j        d          S )Nmanagerz6PixmapManager must be provided when adding an overlay.r1   T)
no_overlay)
ValueErrorgetr1   )r   r   s     r   get_overlayzOverlayFilter.get_overlayk   sX    F""UVVVi $$JJy$''74<D % 
 
 	
r   Fc                     |r|S  | j         di |}|rX|                                }|                     ||          \  }}}}|                    |||||||ddt          | j                   |S )Nr   r   )rA   copyset_position	compositeBILINEARr2   )	r   r"   r>   r   r1   xywidthheights	            r   r#   zOverlayFilter.filterr   s     	J"$",,V,, 	((**C$($5$5gs$C$C!Q5&Q5&!Q1h
   
r   c                 R   |                                 |                                }}|                                 |                                }}t          d||z
  | j        z  g          t          d||z
  | j        z  g          t          ||g          t          ||g          fS )z6Sets the position of img on the given width and heightr   )	get_width
get_heightmaxr7   r8   min)r   r1   r"   img_wimg_hovl_wovl_hs          r   rD   zOverlayFilter.set_position   s    }}(8(8u((**G,>,>,@,@uUU]dj0122UU]dj0122	
 	
r   )F)
r   r   r   r   r   r   rA   r#   rD   __classcell__r;   s   @r   r/   r/   V   s        	 	 0//H= = = = =
 
 
   	
 	
 	
 	
 	
 	
 	
r   r/   c                   :     e Zd ZdZdgZdgZ fdZd Zd Z xZ	S )
SizeFilterzResizes images to a certain size:

    resize_mode - Way in which the size is calculated
      0 - Best Aspect, don't grow
      1 - Best Aspect, grow
      2 - Cropped Aspect
      3 - Stretch
    sizeresize_modec                     d | _         t          | _         t                      j        |i | |                     | j                   pd\  | _        | _        d S )Nr4   )rX   SIZE_ASPECTrY   r6   r   r*   rP   rQ   r9   s      r   r   zSizeFilter.__init__   sS    	&$)&)))!%di!8!8!BF
DJJJr   c                 b   | j         t          k    r| j        | j        fS | j         t          k    r|| j        k     r|| j        k     r||fS | j        |z  | j        |z  }}| j         t
          k    rt          ||          nt          ||          }t          ||z            t          ||z            fS )z)Get the aspect ratio of the image resized)	rY   SIZE_STRETCHrP   rQ   r[   SIZE_ASPECT_CROPrN   rO   r&   )r   rP   rQ   pcwpchfactors         r   aspectzSizeFilter.aspect   s    |++J
++ ++
""
""5>!j5($*u*<c!-1AAACSMMMs3PS}} 	 EFN##S%8%899r   c                     | j         Y|                     |                                |                                          \  }}|                    ||t
                    S |S )N)rX   rb   rL   rM   scale_simpleHYPER)r   r"   r   rI   rJ   s        r   r#   zSizeFilter.filter   sP    9 "kk#--//3>>;K;KLLOUF##E65999
r   )
r   r   r   r   r   r   r   rb   r#   rT   rU   s   @r   rW   rW      su          xHHC C C C C: : :"      r   rW   c                   .     e Zd ZdZdgZ fdZd Z xZS )	PadFilterz3Add padding to the image to make it a standard sizepaddingc                     d | _         d| _         t                      j        |i | |                     | j                  \  | _        | _        d S )Ng      ?)rX   rh   r6   r   r*   r7   r8   r9   s      r   r   zPadFilter.__init__   sM    	$)&)))!%dl!;!;
DJJJr   c                 R   |                                 |                                }}|| j        k     s|| j        k     rt          j                            |                                d|                                t          || j        g          t          || j        g                    }|
                    d           |                                 |z
  | j        z  }|                                |z
  | j        z  }|                    |||||||ddt          d           |S |S )NTr   r   r5   )rL   rM   rP   rQ   r   Pixbufnewget_colorspaceget_bits_per_samplerN   fillr7   r8   rE   rF   )r   r"   r   rI   rJ   targetrG   rH   s           r   r#   zPadFilter.filter   s   ==??CNN,<,<4:$*!4!4%))""$$''))UDJ'((VTZ()) F KK!!##e+tz9A""$$v-;AMM&!Qvq!Q8SQQQM
r   )r   r   r   r   r   r   r#   rT   rU   s   @r   rg   rg      sT        =={H< < < < <      r   rg   c                       e Zd ZU dZdZdZeZdZg Z	e
e         ed<   dZddZd	 Zd
 Zed             Zd Zd Zd Zd ZddZd ZdS )PixmapManagerz|Manage a set of cached pixmaps, returns the default image
    if it can't find one or the missing image if that's available.image-missingzapplication-default-icon    filtersN c                 j   || _         | j        rIt          j                            |          s*t          j                            | j        |          | _         t                              |                    dd                     | _	        g | _
        |                    dg           | j        z   D ]a}t          |t                    r| j
                            |           2t          |          r | j
                             |di |           bi | _        |                     | j                   d S )N	load_sizeru   r   )location
pixmap_dirospathisabsjoinr   r*   poploader_size_filtersr@   ru   r%   appendcallablecache
get_pixmapdefault_image)r   ry   r   items       r   r   zPixmapManager.__init__   s    ? 	D27==#:#: 	DGLL(CCDM'//

;0M0MNN JJy"--< 	5 	5D$-- 5$$T****$ 5$$TT^^F^^444
*+++++r   c                      | j         |i |S )zGet a pixmap of any kind)r   )r   r:   r   s      r   r@   zPixmapManager.get   s    t////r   c                 6    |                      | j                  S )z2Get a missing image when other images aren't found)r@   missing_image)r   s    r   get_missing_imagezPixmapManager.get_missing_image   s    xx*+++r   c                 4    t          | t                    od| vS )z.Test the file to see if it's a filename or notz<svg)r%   r+   )datas    r   data_is_filezPixmapManager.data_is_file   s     $$$;t);;r   c                    d|vr| |d<   |s| j         sdS | j         }|dd         }|| j        vrd}	 |                     |          r|                     |          }n|                     |          }nL# t
          $ r?}t          j        t          |                     | 	                                cY d}~S d}~ww xY w| | j
        |fi || j        |<   | j        |         S )a	  
        There are three types of images this might return.

         1. A named gtk-image such as "gtk-stop"
         2. A file on the disk such as "/tmp/a.png"
         3. Data as either svg or binary png

        All pixmaps are cached for multiple use.
        r=   Ni)r   r   r   load_from_nameload_from_datar   loggingwarningr+   r   apply_filters)r   r   r   r   r"   errs         r   r   zPixmapManager.get_pixmap  s%    F"" $F9 	&% t%D344jdj  C0$$T** 4--d33CC--d33C" 0 0 0C)))--////////0 "4$"4S"C"CF"C"C
3z#s   A A3 3
B<=4B71B<7B<c                 D    | j         D ]}|j        r |j        |fi |}|S )z(Apply all the filters to the given image)r   r   r#   )r   r"   r   lenss       r   r   zPixmapManager.apply_filters&  s<    M 	1 	1D| 1!dk#0000
r   c                 x   t          j                    }| j        r |j        | j          	 t	          |t
                    r|                    d          }|                    |           |                                 n)# t          j
        $ r}t          d|           d}~ww xY w|                                S )z&Load in memory picture file (jpeg etc)zutf-8z Faled to load pixbuf from data: N)r   PixbufLoaderr   set_sizer%   r+   encodewritecloser   GErrorr   
get_pixbuf)r   r   loaderr   s       r   r   zPixmapManager.load_from_data-  s     ')) 	/FOT-..	L$$$ ,{{7++LLLLNNNN{ 	L 	L 	L!"JS"J"JKKK	L  """s   AA? ?B%B  B%c                    |                      |          }t          j                            |          rG	 t          j                            |          S # t          $ r}t          d| d|           d}~ww xY w| j	        r(d|vr$d|vr d|vr| 
                    || j                  S t          d| d	| j                   )
z6Load a pixbuf from a name, filename or theme icon namezFaild to load pixmap 'z', N/.<)rX   zFailed to find pixmap 'z' in )pixmap_pathr{   r|   existsr   rk   new_from_fileRuntimeErrorr   
icon_themetheme_pixmap
theme_sizery   )r   namer   msgs       r   r   zPixmapManager.load_from_name<  s    &&t,,7>>+&& 	AV '55kBBB V V V%&T{&T&Ts&T&TUUUV O	A #4CtOOSW$$T$@@@RRR4=RRSSSs   A 
A9A44A9c                 z    |pd}| j                             |          sd}| j                             ||d          S )z)Internal user: get image from gnome themert   rs   r   )r   has_icon	load_icon)r   r   rX   s      r   r   zPixmapManager.theme_pixmapJ  sB    zr''-- 	#"D((tQ777r   c                 x   |t           j                            | j        | d          t           j                            | j        | d          fD ]D}t           j                            |          r#t           j                            |          r|c S Et           j                            | j        |          S )z0Returns the pixmap path based on stored locationz.svgz.png)r{   r|   r~   ry   r   isfile)r   r   filenames      r   r   zPixmapManager.pixmap_pathQ  s     GLL466GLL466
 	 	H
 w~~h'' BGNN8,D,D w||DM4000r   )rv   )rt   )r   r   r   r   r   r   
ICON_THEMEr   r   ru   r   r!   r,   rz   r   r@   r   r-   r   r   r   r   r   r   r   r   r   r   rr   rr      s        F F $M.MJJGT$ZJ, , , ,&0 0 0, , , < < \<" " "H  # # #T T T8 8 8 8	1 	1 	1 	1 	1r   rr   )r   r{   r   typingr   collections.abcr   gi.repositoryr   r   r   	IconThemeget_defaultr   
InterpTyperF   re   r[   SIZE_ASPECT_GROWr^   r]   r?   r   r   r/   rW   rg   rr   r   r   r   <module>r      s  "  
			        $ $ $ $ $ $ . . . . . . . . . .]&&((
("  " " " " "j " " ") ) ) ) ) ) ) )X4
 4
 4
 4
 4
L 4
 4
 4
n( ( ( ( ( ( ( (V    
   >C1 C1 C1 C1 C1 C1 C1 C1 C1 C1r   