B
    Mô`w  ã               @   s0   d Z ddlmZmZmZ ddd„Zd	dd„ZdS )
zE
Helper module for the *bbox_inches* parameter in `.Figure.savefig`.
é    )ÚBboxÚTransformedBboxÚAffine2DNc                s†  ˆj ‰ˆj‰ˆ ¡ ‰ˆjj‰ ˆ d¡ g ‰g ‰tƒ ‰xlˆjD ]b}ˆ | 	¡ ¡ |j
dd� ¡ }| |fdd„¡ d|jkrˆˆ |j¡ n
ˆ ˆ¡ ddd„|_q<W ‡ ‡‡‡‡‡‡‡fdd	„}|dkrÊˆj}tƒ  |¡}|ˆj }t||ƒ}t d
d
|j|j¡ˆ_|j|j }	}
ˆj j| ˆj j|  }}t |	 |
 ||¡ˆj_ˆj ¡  tˆj|ƒˆ_ ˆj |	| |
| ˆj j| ˆj j| ¡ |S )aO  
    Temporarily adjust the figure so that only the specified area
    (bbox_inches) is saved.

    It modifies fig.bbox, fig.bbox_inches,
    fig.transFigure._boxout, and fig.patch.  While the figure size
    changes, the scale of the original figure is conserved.  A
    function which restores the original values are returned.
    F)Úoriginalc             S   s   |S )N© )ÚaÚrÚ_posr   r   ú9/tmp/pip-unpacked-wheel-0c1wfn4v/matplotlib/tight_bbox.pyÚ<lambda>   s    zadjust_bbox.<locals>.<lambda>Úapply_aspectNc             S   s   d S )Nr   )Úposr   r   r
   r   %   s    c                 sz   x:t ˆjˆˆƒD ](\} }}|  |¡ |ˆkr2| `q|| _qW ˆˆ_ˆˆ_ˆ ˆ¡ ˆ ˆj_ˆj 	¡  ˆj
 dddd¡ d S )Nr   é   )ÚzipÚaxesÚset_axes_locatorr   ÚbboxÚbbox_inchesÚset_tight_layoutÚtransFigureÚ_boxoutÚ
invalidateÚpatchÚ
set_bounds)ÚaxÚlocZaspect)r   ÚfigÚlocator_listÚ
old_aspectÚorigBboxÚorigBboxInchesÚorig_tight_layoutÚsentinelr   r
   Úrestore_bbox'   s    



z!adjust_bbox.<locals>.restore_bboxr   )N)r   r   Zget_tight_layoutr   r   r   Úobjectr   ÚappendZget_axes_locatorÚget_positionÚfrozenr   Ú__dict__r   Zdpir   Zscaler   r   Zfrom_boundsÚwidthÚheightÚx0Úy0r   r   r   )r   r   Ú	fixed_dpir   Zcurrent_posr#   ÚtrZ	dpi_scaleZ_bboxr+   r,   Zw1Úh1r   )r   r   r   r   r   r    r!   r"   r
   Úadjust_bbox   s@    






r0   c             C   s"   |\}}|ƒ  t | ||ƒ}||fS )z­
    A function that needs to be called when figure dpi changes during the
    drawing (e.g., rasterizing).  It recovers the bbox and re-adjust it with
    the new dpi.
    )r0   )r   Zbbox_inches_restorer-   r   r#   r   r   r   r
   Úprocess_figure_for_rasterizingM   s    r1   )N)N)Ú__doc__Zmatplotlib.transformsr   r   r   r0   r1   r   r   r   r
   Ú<module>   s   
E