o
    OZŽh°%  ã                   @   s@   d Z ddlZddlZddlZdZG dd„ dƒZeƒ Ze ¡  dS )aõ   pygame.examples.font_viewer
Scroll through your system fonts from a list of surfaces or one huge buffer.

This example exhibits:
* iterate over available fonts using font.get_fonts and font.SysFont()
* click and drag using mouse input
* scrolling with the scroll wheel
* save a surface to disk
* work with a very large surface
* simple mouse and keyboard scroll speed acceleration

By default this example uses the fonts returned by pygame.font.get_fonts()
and opens them using pygame.font.SysFont().
Alternatively, you may pass a path to the command line. The TTF files found
in that directory will be used instead.

Mouse Controls:
* Use the mouse wheel or click and drag to scroll

Keyboard Controls:
* Press up or down to scroll
* Press escape to exit
é    NFc                   @   s^   e Zd ZdZdZdZdd„ Zdd„ Zdd	d
„Zdd„ Z	dd„ Z
ddd„Zddd„Zdd„ ZdS )Ú
FontViewerz—
    This example is encapsulated by the fontviewer class
    It initializes the pygame window, handles input, and draws itself
    to the screen.
    é
   é2   c                 C   s    t  ¡  t j ¡ }|j}|j}t j t|d ƒt|d ƒf¡ |d | _t j	 
¡ | _d| _d| _|  d¡ ts<dtjv rJ|  ¡  |  ¡  |  ¡  d S |  ¡  d S )Ngš™™™™™é?é   r   Fz&N abcDEF789Úbig)ÚpgÚinitÚdisplayZInfoZ	current_wZ	current_hÚset_modeÚintÚ	font_sizeÚtimeÚClockÚclockÚy_offsetÚgrabbedÚrender_fontsÚuse_big_surfaceÚsysÚargvÚrender_surfaceÚdisplay_surfaceÚsave_pngÚdisplay_fonts)ÚselfÚinfoÚwÚh© r   úJ/var/www/auris/lib/python3.10/site-packages/pygame/examples/font_viewer.pyÚ__init__+   s   
 

zFontViewer.__init__c                 C   sr   d}t tjƒdkrtj tjd ¡rtj tjd d¡}tj |¡r0dd„ t |¡D ƒ}||fS tj	 
¡ }||fS )z|
        Generate a font list using font.get_fonts() for system fonts or
        from a path from the command line.
        Ú é   c                 S   s   g | ]	}|  d ¡r|‘qS )z.ttf)Úendswith)Ú.0Úfontr   r   r   Ú
<listcomp>J   s    z,FontViewer.get_font_list.<locals>.<listcomp>)Úlenr   r   ÚosÚpathÚexistsÚjoinÚlistdirr   r%   Z	get_fonts)r   r)   Úfontsr   r   r   Úget_font_listA   s    
ÿzFontViewer.get_font_listúA display of font &Nc                 C   sŽ  | j }d}d}d| _|  ¡ \}}g }d}d}	|rtjjntjj}
tj tj ¡ |¡}ddddd	t|ƒ› d
df}|D ]}| 	|d|| j¡}| 
||f¡ || ¡ 7 }t|	| ¡ ƒ}	q:t|ƒD ]S}z	|
|| |ƒ}W n	 tyr   Y q^w | d|¡}z| 	|d|| j¡}W n tjyœ } zt|ƒ W Y d}~ nd}~ww t|	| ¡ ƒ}	| 
||f¡ || ¡ 7 }q^|| _|	| _|| _|tj ¡  ¡  | _dS )zÇ
        Build a list that includes a surface and the running total of their
        height for each font in the font list. Store the largest width and
        other variables for later use.
        )éÿ   r0   r0   )r0   r0   r   )r   r   r   r   z&Use the scroll wheel or click and dragzto scroll up and down.z'Fonts that don't use the Latin Alphabetzmight render incorrectly.zHere are your z fontsr!   r"   z&NN)r   Ú
back_colorr.   r   r%   ZFontZSysFontZget_default_fontr'   ÚrenderÚappendÚ
get_heightÚmaxÚ	get_widthÚsortedÚOSErrorÚreplaceÚerrorÚprintÚtotal_heightÚ	max_widthÚfont_surfacesr	   Úget_surfaceÚmax_y)r   Útextr   ÚcolorZinstruction_colorr-   r)   r>   r<   r=   Z	load_fontr%   ÚlinesÚlineZsurfÚnameÚer   r   r   r   O   sV   úÿ€þzFontViewer.render_fontsc                 C   sº   t j d¡ t j ¡ }t j ¡ }| ¡ d }	 | | j¡ | j	D ]+\}}|| 
¡  }|| jkrK|| j| 
¡  krK|| ¡ d  }| |||| j f¡ q |  ¡ sRdS t j ¡  | d¡ q)zŽ
        Display the visible fonts based on the y_offset value(updated in
        handle_events) and the height of the pygame window.
        zFont Vieweré   Té   N)r   r	   Zset_captionr?   r   r   r6   Úfillr1   r>   r4   r   ÚblitÚhandle_eventsÚflipÚtick)r   r	   r   ÚcenterÚsurfaceÚtopÚbottomÚxr   r   r   r   ˆ   s$   


€

ñzFontViewer.display_fontsc           
      C   sØ   t j | j| jf¡ ¡ }| | j¡ tdƒ | 	¡ }|| j| j  }td 
| j| j||d ¡ƒ d}t| jd ƒ}| jD ]\}}| ¡ }|t|d ƒ }	| ||	|f¡ || ¡ 7 }q<| ¡ t j ¡  ¡  | _|| _dS )a\  
        Note: this method uses twice the memory and is only called if
        big_surface is set to true or big is added to the command line.

        Optionally generates one large buffer to draw all the font surfaces
        into. This is necessary to save the display to a png file and may
        be useful for testing large surfaces.
        zscrolling surface createdz'Surface Size = {}x{} @ {}bpp: {:,.3f}mbg    €„.Ar   rG   N)r   rO   ZSurfacer=   r<   ÚconvertrI   r1   r;   Zget_bytesizeÚformatr   r>   r6   rJ   r4   r	   r?   r@   )
r   Zlarge_surfaceZ	byte_sizeÚ
total_sizeÚyrN   rO   rP   r   rR   r   r   r   r   £   s.   

ÿþÿÿ
zFontViewer.render_surfacec              	   C   sª   t j ¡ }t j dd| j ¡ t| j ¡ | ¡ ƒ¡}t	| ¡ | j ¡  d ƒ}d}|rS|  
¡ s0d}| | j¡ | j|_| | j|df|¡ t j ¡  | j d¡ |s*dS dS )zÀ
        Display the large surface created by the render_surface method. Scrolls
        based on the y_offset value(set in handle_events) and the height of the
        pygame window.
        r   rG   TFr   N)r   r	   r?   ÚrectZRectrO   r6   Úminr4   r   rK   rI   r1   r   rP   rJ   rL   r   rM   )r   r   ÚscreenrW   rR   Zgoingr   r   r   r   Æ   s$   
ü
ùzFontViewer.display_surfaceúfont_viewer.pngc                 C   s<   t j | j|¡ tj |¡d }td|› d|d›dƒ d S )Ni   zfont surface saved to z
size: ú,ZKb)r   ÚimageÚsaverO   r(   r)   Úgetsizer;   )r   rE   Ú	file_sizer   r   r   r   ã   s   zFontViewer.save_pngc                 C   sn  t j ¡ }|D ]N}|jt jkr dS |jt jkr"|jt jkr! dS q|jt jkr6|  j	|j
| j d 7  _	q|jt jkrFd| _t j d¡ q|jt jkrUd| _t j d¡ qt j ¡ }|t j rw|  jd7  _|  j	t| j| jd  ƒ8  _	n|t j r“|  jd7  _|  j	t| j| jd  ƒ7  _	nd| _t j ¡ d }|r©| jr©|  j	|8  _	tt| j	dƒ| jfƒ| _	dS )a   
        This method handles user input. It returns False when it receives
        a pygame.QUIT event or the user presses escape. The y_offset is
        changed based on mouse and keyboard input. display_fonts() and
        display_surface() use the y_offset to scroll display.
        FéÿÿÿÿTr"   r   r   r   )r   ÚeventÚgetÚtypeZQUITZKEYDOWNÚkeyZK_ESCAPEZ
MOUSEWHEELr   rV   ÚMOUSE_SCROLL_SPEEDZMOUSEBUTTONDOWNr   Zset_grabZMOUSEBUTTONUPZget_pressedZK_UPZkey_heldr   ÚKEY_SCROLL_SPEEDZK_DOWNÚmouseZget_relrX   r5   r@   )r   ÚeventsrF   ÚkeysrV   r   r   r   rK   è   s<   
ÿ€

 
 
zFontViewer.handle_eventsN)r/   )r   )rZ   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rf   re   r    r.   r   r   r   r   r   rK   r   r   r   r   r   !   s    
9
#
r   )	rm   r   r(   Zpygamer   r   r   ZviewerÚquitr   r   r   r   Ú<module>   s    u