U
    Ñtœd’2  ã                   @   sR   d Z ddlZddlmZ ddlmZ dZG dd„ deƒZdd	„ Ze	d
krNeƒ  dS )z
Drawers for labels on plots.
é    N)Úwarn)ÚAbstractCairoDrawer)ÚCairoTextDrawerc                       s„   e Zd ZdZd\ZZZd\ZZd‡ fdd„	Z	dd	d
„Z
ddd„Zddd„Zdd„ Zdd„ Zedd„ ƒZejdd„ ƒZdd„ Z‡  ZS )r   z†Class that draws text on a Cairo context.

    This class supports multi-line text unlike the original Cairo text
    drawing methods.©ÚleftÚcenterÚright)ÚtopÚbottomÚ r   c                    s$   t ƒ  |d¡ || _|| _|| _dS )zjConstructs a new instance that will draw the given C{text} on
        the given Cairo C{context}.
        )r   r   N)ÚsuperÚ__init__ÚtextÚhalignÚvalign)ÚselfÚcontextr   r   r   ©Ú	__class__© úR/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/drawing/cairo/text.pyr      s    zCairoTextDrawer.__init__Fc                 C   sä   | j }| j}|  |j|j|j|¡}|s*dS | ¡ dd… \}}}| |d d ¡d }t|ƒ| }	| j	| j
kr‚|j|	 | | }
n,| j	| jkrª|j|	 | | | d }
n|}
|D ]$\}}}| |||
 ¡ | |¡ q²| ¡  dS )a`  Draws the text in the current bounding box of the drawer.

        Since the class itself is an instance of L{AbstractCairoDrawer}, it
        has an attribute named C{bbox} which will be used as a bounding box.

        @param wrap: whether to allow re-wrapping of the text if it does not
            fit within the bounding box horizontally.
        Né   r   é   é   ç       @)r   ÚbboxÚget_text_layoutr   r	   ÚwidthÚfont_extentsÚtext_extentsÚlenr   ÚBOTTOMÚheightÚCENTERÚmove_toÚ	show_textÚnew_path)r   ÚwrapÚctxr   Ztext_layoutÚ_Zfont_descentÚline_heightZybZtotal_heightZdyÚref_xÚref_yÚliner   r   r   Údraw   s"    	zCairoTextDrawer.drawNc                 C   sT  | j }|dks|dkr"| ¡ \}}| ¡ d }|rJ|rJ|dkrJ|  |¡}n|r`tdƒ |  ¡ }n|  ¡ }g }| j| jkrÆ|dkrŒ|  ¡ d }|D ]2\}	}
}| 	|||
 d  | ||	f¡ ||7 }qnŠ| j| j
kr"|dkrè|  ¡ d }||7 }|D ]*\}	}
}| 	||
 | ||	f¡ ||7 }qôn.|D ](\}	}}| 	|| ||	f¡ ||7 }q&|S )a‹  Calculates the layout of the current text. C{x} and C{y} denote the
        coordinates where the drawing should start. If they are both C{None},
        the current position of the context will be used.

        Vertical alignment settings are not taken into account in this method
        as the text is not drawn within a box.

        @param x: The X coordinate of the reference point where the layout should
            start.
        @param y: The Y coordinate of the reference point where the layout should
            start.
        @param width: The width of the box in which the text will be fitted. It
            matters only when the text is right-aligned or centered. The text
            will overflow the box if any of the lines is longer than the box
            width and C{wrap} is C{False}.
        @param wrap: whether to allow re-wrapping of the text if it does not
            fit within the given width.

        @return: a list consisting of C{(x, y, line)} tuples where C{x} and
            C{y} refer to reference points on the Cairo canvas and C{line}
            refers to the corresponding text that should be plotted there.
        Nr   r   z,ignoring wrap=True as no width was specifiedr   )r   Úget_current_pointr   Ú_iterlines_wrappedr   Ú
_iterlinesr   r#   r   ÚappendÚRIGHT)r   ÚxÚyr   r'   r(   r*   Z	iterlinesÚresultr-   Ú
line_widthÚ	x_bearingr)   r   r   r   r   B   s8    
 zCairoTextDrawer.get_text_layoutc           	      C   sD   | j }|  ||||¡D ] \}}}| ||¡ | |¡ q| ¡  dS )aÛ  Draws the text by setting up an appropriate path on the Cairo
        context and filling it. C{x} and C{y} denote the coordinates where the
        drawing should start. If they are both C{None}, the current position
        of the context will be used.

        Vertical alignment settings are not taken into account in this method
        as the text is not drawn within a box.

        @param x: The X coordinate of the reference point where the layout should
            start.
        @param y: The Y coordinate of the reference point where the layout should
            start.
        @param width: The width of the box in which the text will be fitted. It
            matters only when the text is right-aligned or centered. The text
            will overflow the box if any of the lines is longer than the box
            width and C{wrap} is C{False}.
        @param wrap: whether to allow re-wrapping of the text if it does not
            fit within the given width.
        N)r   r   r$   r%   r&   )	r   r4   r5   r   r'   r(   r+   r,   r-   r   r   r   Údraw_atƒ   s
    zCairoTextDrawer.draw_atc                 c   s>   | j }| j d¡D ]&}| |¡\}}}}}}|||fV  qdS )zµIterates over the label line by line and returns a tuple containing
        the folloing for each line: the line itself, the width of the line and
        the X-bearing of the line.Ú
N)r   Ú_textÚsplitr   )r   r(   r-   Úxbr)   r7   r   r   r   r1      s    zCairoTextDrawer._iterlinesc                 c   s(  | j }| j d¡D ]}| |¡\}}}}}}||krD|||fV  qg dd  }}}	t d|¡D ]ª}
|
 ¡ \}}| |¡d }|r’| |¡d }nd}||7 }||krà|ràd |¡|| dfV  |g| }}|dk	rÞ| |¡ n&||	7 }| |¡ |dk	r| |¡ |}	q`|rd |¡|dfV  qdS )aÚ  Iterates over the label line by line and returns a tuple containing
        the folloing for each line: the line itself, the width of the line and
        the X-bearing of the line.

        The difference between this method and L{_iterlines()} is that this
        method is allowed to re-wrap the line if necessary.

        @param width: The width of the box in which the text will be fitted.
            Lines will be wrapped if they are wider than this width.
        r:   r   z(\S+)(\s+)?é   r   N)	r   r;   r<   r   ÚreÚfinditerÚgroupsÚjoinr2   )r   r   r(   r-   r=   r)   r7   Úcurrent_lineZcurrent_widthZlast_sep_widthÚmatchÚwordÚsepZ
word_widthZ	sep_widthr   r   r   r0   ¦   s4    


z"CairoTextDrawer._iterlines_wrappedc                 C   s   | j S )zReturns the text to be drawn.)r;   )r   r   r   r   r   Ñ   s    zCairoTextDrawer.textc                 C   s   |dkrd| _ n
t|ƒ| _ dS )zœSets the text that will be drawn.

        If C{text} is C{None}, it will be mapped to an empty string; otherwise,
        it will be converted to a string.Nr   )r;   Ústr)r   r   r   r   r   r   Ö   s    c                 C   s¤   | j  d¡}t|ƒdkr&| j | j ¡S | j |d ¡\}}}}}}| j ¡ d }|dd… D ]6}	| j |	¡\}
}
}}
}}t||ƒ}||7 }||7 }q\||||||fS )a~  Returns the X-bearing, Y-bearing, width, height, X-advance and
        Y-advance of the text.

        For multi-line text, the X-bearing and Y-bearing correspond to the
        first line, while the X-advance is extracted from the last line.
        and the Y-advance is the sum of all the Y-advances. The width and
        height correspond to the entire bounding box of the text.r:   r   r   r   N)r   r<   r    r   r   r   Úmax)r   Úlinesr8   Z	y_bearingr   r"   Z	x_advanceZ	y_advancer*   r-   r)   ÚwZyar   r   r   r   á   s$    	ù

zCairoTextDrawer.text_extents)r   r   r   )F)NNNF)NNNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚLEFTr#   r3   ÚTOPr!   r   r.   r   r9   r1   r0   Úpropertyr   Úsetterr   Ú__classcell__r   r   r   r   r      s   
	
#
A
	+


r   c                     s(  ddl ‰ddlm}  ddlm} | ƒ }d}d\}}| |j||¡}| |¡‰ tˆ |ƒ}ˆ  	ddd¡ ˆ  
d¡ ˆ  dd||¡ ˆ  ¡  ˆ  	d	d	d	¡ td
|d
ƒD ]$}ˆ  |d¡ ˆ  ||¡ ˆ  ¡  q td
|d
ƒD ]$}ˆ  d|¡ ˆ  ||¡ ˆ  ¡  qÒˆ  	ddd¡ ˆ  d	¡ td|d
ƒD ]&}ˆ  |d¡ ˆ  ||¡ ˆ  ¡  qtd|d
ƒD ]&}ˆ  d|¡ ˆ  ||¡ ˆ  ¡  qP‡ ‡fdd„}	|j|j|jf}
t|
ƒD ]†\}}ˆ  |d
 d¡ |	dddƒ ˆ  |d
 d¡ |	dddƒ ˆ  	ddd¡ ||_| |d
 d¡ |j|d
 dd
d |	dddƒ qžtdƒD ]v\}}tdƒD ]b\}}ˆ  	ddd¡ ||_||_|d
 |d
 d
 |d
 d
 |d
 d f|_| ¡  |	dddƒ q>q.d|_|j|_tdƒD ]\\}}ˆ  |d
 d¡ |	dddƒ ˆ  	ddd¡ ||_|j|d
 dddd |	dddƒ q¼| d¡ dS )z&Testing routine for L{CairoTextDrawer}r   N)Ú
find_cairo)ÚTextAlignmentz)The quick brown fox
jumps over a
lazy dog)iX  iè  r   g      0@ç      à?éÈ   g      è?éd   c                    s@   ˆ   ¡ \}}ˆ  | ||d¡ ˆ  ||dddˆj ¡ ˆ  ¡  dS )z8Marks the current point on the canvas by the given colorrV   r>   r   r   N)r/   Zset_source_rgbaÚarcÚpiÚfill)ÚredÚgreenÚbluer4   r5   ©r   Úmathr   r   Ú
mark_point(  s    ztest.<locals>.mark_pointé(   éŒ   )r   r   )r	   r   r
   i  zŠJackdaws love my big sphinx of quartz. Yay, wrapping! Jackdaws love my big sphinx of quartz.

Jackdaws  love  my  big  sphinx  of  quartz.iH  éÇ   T)r   r'   ztest.png)r`   Zigraph.drawing.cairo.utilsrT   Zigraph.drawing.textrU   ZImageSurfaceZFORMAT_ARGB32ÚContextr   Zset_source_rgbZset_font_sizeZ	rectangler[   Úranger$   Zline_toÚstrokeZset_line_widthrO   r#   r3   Ú	enumerater   r9   r   r   r.   r   rP   Zwrite_to_png)rT   rU   Zcairor   r   r"   ZsurfaceZdrawerÚira   Z
alignmentsr   Újr   r   r_   r   Útest   s~    





*ÿrk   Ú__main__)
rN   r?   Úwarningsr   Zigraph.drawing.cairo.baser   Ú__all__r   rk   rK   r   r   r   r   Ú<module>   s    te