
    lj%                        d dl mZ d dlmZ d dlmZmZ d dlZd dl	m
Z
 ddlmZmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZ erd dlmZ d dlmZ ddlmZ dgZ G d d          ZdS )    )annotations)Iterable)TYPE_CHECKINGAnyN)get_hash_from_play_call   )configlogger)Camera)Mobject_AnimationBuilder)SceneFileWriter)EndSceneEarlyException)list_update)	Animation)Scene)
PixelArrayCairoRendererc                      e Zd ZdZeddfd/dZd0dZd1dZ	 	 	 d2d3dZ	 d4d5dZ	d6d Z
d7d8d%Zd9d'Zd0d(Zd:d,Zd;d-Zd0d.ZdS )<r   zA renderer using Cairo.

    Attributes
    ----------
    num_plays : int
        Number of play() functions in the scene.

    time : float
        Time elapsed since initialisation of scene.
    NFfile_writer_classtype[SceneFileWriter]camera_classtype[Camera] | Noneskip_animationsboolkwargsr   c                    || _         ||nt          } |            | _        || _        || _        g | _        d| _        d| _        d | _        d S )Nr   g        )	_file_writer_classr   camera_original_skipping_statusr   animations_hashes	num_playstimestatic_image)selfr   r   r   r   
camera_clss         X/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/renderer/cairo_renderer.py__init__zCairoRenderer.__init__&   s[     #4%1%=\\6
 jll)8&.35	/3    scener   returnNonec                P    |                      | |j        j                  | _        d S N)r   	__class____name__file_writerr%   r*   s     r'   
init_scenezCairoRenderer.init_scene:   s+     $ 7 7O$!
 !
r)   args'Animation | Mobject | _AnimationBuilderc           	        | j         | _        |                                   |j        |i | | j        r4t	          j        d| j                    d }| xj        |j        z  c_        nt          d         r t	          j
        d           d| j        d}n|j        J t          || j        |j        |j                  }| j                            |          r<t	          j
        d| j         dd|i           d	| _        | xj        |j        z  c_        | j                            |           | j                            |           t	          j        d
dt)          | j        d d                   i           | j                            | j                    |                                 |                     ||j                   |                                r7|                     ||j                   |                     |j                   n|                                 | j                            | j                    | xj        dz  c_        d S )NzSkipping animation disable_cachingzCaching disabled.	uncached_05z
Animation z8 : Using cached data (hash : %(hash_current_animation)s)hash_current_animationTz:List of the first few animation hashes of the scene: %(h)sh   mobjects   )r    r   update_skipping_statuscompile_animation_datar
   debugr"   r#   durationr	   info
animationsr   r   r>   r1   is_already_cachedadd_partial_movie_filer!   appendstrbegin_animationbegin_animationssave_static_frame_datastatic_mobjects!is_current_animation_frozen_frameupdate_framemoving_mobjectsfreeze_current_frameplay_internalend_animation)r%   r*   r4   r   r:   s        r'   playzCairoRenderer.play@   s     $=##%%%$$d5f555 	0L?t~??@@@%)"II'III'( 0/000)HT^)H)H)H&&'333)@K$N	* *& #556LMM 0KmT^mmm13IJ   ,0D(II/II//0FGGG%%&<===H#d,RaR0112	
 	
 	

 	((T-A)ABBB    	##E5+@AAA2244 	"ee.CDDD %%en5555!!!&&4+?'?@@@!r)   Tr>   Iterable[Mobject] | Noneinclude_submobjectsignore_skippingc                   | j         r|sdS |st          |j        |j                  }| j         | j                            | j                   n| j                                         ||d<    | j        j        |fi | dS )zUpdate the frame.

        Parameters
        ----------
        scene

        mobjects
            list of mobjects

        include_submobjects

        ignore_skipping

        **kwargs
        NrV   )	r   r   r>   foreground_mobjectsr$   r   set_frame_to_backgroundresetcapture_mobjects)r%   r*   r>   rV   rW   r   s         r'   rO   zCairoRenderer.update_frame}   s    .  	 	F 	") H (K//0ABBBBK(;$%$$X8888888r)   r#   floatrP   c                    |                      ||           |                     |                                            d S r.   )rO   	add_frame	get_frame)r%   r*   r#   rP   s       r'   renderzCairoRenderer.render   s<     	%111t~~''(((((r)   r   c                >    t          j        | j        j                  S )zGets the current frame as NumPy array.

        Returns
        -------
        PixelArray
            NumPy array of pixel values of each pixel in screen.
            The shape of the array is height x width x 3.
        )nparrayr   pixel_arrayr%   s    r'   r`   zCairoRenderer.get_frame   s     x/000r)   r?   frame
num_framesintc                    d| j         j        z  }| j        rdS | xj        ||z  z  c_        | j                            ||           dS )zAdds a frame to the video_file_stream

        Parameters
        ----------
        frame
            The frame to add, as a pixel array.
        num_frames
            The number of times to add frame.
        r?   Nrh   )r   
frame_rater   r#   r1   write_frame)r%   rg   rh   dts       r'   r_   zCairoRenderer.add_frame   sY     '' 	F		Z"_$		$$Uz$BBBBBr)   rC   c                    d| j         j        z  }|                     |                                 t	          ||z                       dS )zAdds a static frame to the movie for a given duration. The static frame is the current frame.

        Parameters
        ----------
        duration
            [description]
        r?   rk   N)r   rl   r_   r`   ri   )r%   rC   rn   s      r'   rQ   z"CairoRenderer.freeze_current_frame   sT     ''NN8b=)) 	 	
 	
 	
 	
 	
r)   c                    |                      |d           | j                                                                         dS )zTOpens the current frame in the Default Image Viewer
        of your system.
        T)rW   N)rO   r   	get_imageshowr2   s     r'   
show_framezCairoRenderer.show_frame   sC     	%666$$&&&&&r)   rM   Iterable[Mobject]PixelArray | Nonec                    d| _         |sdS |                     ||           |                                 | _         | j         S )a  Compute and save the static frame, that will be reused at each frame
        to avoid unnecessarily computing static mobjects.

        Parameters
        ----------
        scene
            The scene played.
        static_mobjects
            Static mobjects of the scene. If None, self.static_image is set to None.

        Returns
        -------
        PixelArray | None
            The static image computed. The return value is None if there are no static mobjects in the scene.
        Nr=   )r$   rO   r`   )r%   r*   rM   s      r'   rL   z$CairoRenderer.save_static_frame_data   sM    ( ! 	4%/::: NN,,  r)   c                :   | j         j        d         j        rd| _        t          d         rd| _        t          j        dk    r| j        t          j        k     rd| _        t          j        dk    r*| j        t          j        k    rd| _        t                      dS dS )a?  This method is used internally to check if the current
        animation needs to be skipped or not. It also checks if
        the number of animations that were played correspond to
        the number of animations that need to be played, and
        raises an EndSceneEarlyException if they don't correspond.
        Tsave_last_framer   N)r1   sectionsr   r	   from_animation_numberr"   upto_animation_numberr   rf   s    r'   r@   z$CairoRenderer.update_skipping_status   s     $R(8 	(#'D #$ 	(#'D (1,,!===#'D (A--!===#'D (***	 .-==r)   c                   | j         r| j                                         nAt          j        rdt          _        dt          _        nd | _        |                     |           t          d         rOd | _        |                     |           | j                            | j	        
                                           d S d S )NTFry   )r"   r1   finishr	   write_to_moviery   r$   rO   
save_imager   rq   r2   s     r'   scene_finishedzCairoRenderer.scene_finished  s    > 	%##%%%%" 	%%)F"$)F!! $De$$$#$ 	A $De$$$''(=(=(?(?@@@@@	A 	Ar)   )r   r   r   r   r   r   r   r   )r*   r   r+   r,   )r*   r   r4   r5   r   r   r+   r,   )NTT)r*   r   r>   rU   rV   r   rW   r   r   r   r+   r,   r.   )r*   r   r#   r]   rP   rU   r+   r,   )r+   r   )r?   )rg   r   rh   ri   r+   r,   )rC   r]   r+   r,   )r*   r   rM   rt   r+   ru   )r+   r,   )r0   
__module____qualname____doc__r   r(   r3   rT   rO   ra   r`   r_   rQ   rs   rL   r@   r    r)   r'   r   r      sK       	 	 4C,0 %	4 4 4 4 4(
 
 
 
; ; ; ;@ .2$( $$9 $9 $9 $9 $9T 59	) ) ) ) )	1 	1 	1 	1C C C C C 
 
 
 
' ' ' '! ! ! !6+ + + +0A A A A A Ar)   ) 
__future__r   collections.abcr   typingr   r   numpyrc   manim.utils.hashingr    r	   r
   camera.camerar   mobject.mobjectr   r   scene.scene_file_writerr   utils.exceptionsr   utils.iterablesr   manim.animation.animationr   manim.scene.scener   r   __all__r   r   r)   r'   <module>r      sc   " " " " " " $ $ $ $ $ $ % % % % % % % %     7 7 7 7 7 7         " " " " " " 8 8 8 8 8 8 8 8 5 5 5 5 5 5 5 5 5 5 5 5 ) ) ) ) ) ) $333333''''''######
CA CA CA CA CA CA CA CA CA CAr)   