a
    V$c^i                     @   s  d dl mZmZmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlZzd dlm  mZ W n ey   Y n0 d dlZd dlm  mZ d dlm  mZ e  dZ dZ!dd Z"G dd	 d	e#Z$G d
d de$Z%G dd de$Z&dd Z'G dd de$Z(G dd de$Z)ddddddddd i dfddZ*ddddddddd i f
ddZ+d(ddZ,i Z-dd Z.i Z/i Z0i Z1da2d d! Z3d"d# Z4d$d% Z5G d&d' d'e#Z6dS ))    )divisionabsolute_importwith_statementprint_functionunicode_literals)PY2
basestringbchrbordchropenpystrrangeroundstrtobytesunicodeN)   r   r   r   )r   r   r   r   c                 C   s@   | dkrdS | dkrdS | dkr$dS | dkr0dS | dkr<dS dS )Ni   Ti   i   i`   i  F )charr   r   renpy/text\font.pyis_zerowidth,   s    r   c                   @   sD   e Zd ZdZi ZdZdZi Zi Zi Z	i Z
dd Zdd Zdd Zd	S )
	ImageFontr   g        c                 C   s   g }|s|S |D ]}t  }t||_| j|_| j|_t|js~| j	
|d }|d u rdtd|| j	| |_	| j| |_nd|_	d|_|| qtt|d D ]6}| j
|| ||d   | j}||  j|7  _q|S )Nz.Character {0!r} not found in image-based font.r      )textsupportGlyphord	characterbaselineascentheightline_spacingr   widthget	Exceptionformatadvanceappendr   lenkernsdefault_kern)selfsrvcgr"   ikernr   r   r   glyphs]   s*    

 zImageFont.glyphsc                 C   s   |S Nr   )r+   r2   boundsr   r   r   r4      s    zImageFont.boundsc	                 C   s   |d u rd S |D ]}	|	j sqt|	j}
| j|
 \}}|	j| | }|	j| | |	j }| j|
 }tj	j
r|tksx|tkrtjj| d}tjj|||| |}||||f qd S )NT)r"   r   r   offsetsxyr   charsrenpyconfigrecolor_sfontsWHITEBLACKdisplaypgrendersurfaceget_sizemoduletwomapblit)r+   targetxoyocolorr2   	underlinestrikethroughblack_colorr/   r.   cxocyor6   r7   	char_surfnew_surfr   r   r   draw   s     

zImageFont.drawN)__name__
__module____qualname__r    r)   r*   r   r"   r&   r5   r8   r2   r4   rP   r   r   r   r   r   ?   s   $r   c                       s&   e Zd Zd fdd	Zdd Z  ZS )SFontNc                    s6   t t|   || _|| _|| _|| _|| _|| _d S r3   )	superrT   __init__filename
spacewidthr*   r)   charsetr   )r+   rW   rX   r*   r)   rY   r   	__class__r   r   rV      s    zSFont.__init__c                 C   s6  i | _ i | _i | _i | _tjj| jj	dd}|
 \}}|}|| _| jd u rX|| _n| jdk rn|| j | _tjj| j|fd| j d< | j| jd< | j| jd< d| jd< tjjd|fd| j d< d| jd< d| jd< d| jd< | j d | j d< | jd | jd< | jd | jd< | jd | jd< d}||df}|d dkrHqT|d	7 }q(d}||k r2|t| jk r2||df|kr&|}|d	7 }||k r||df|krq|d	7 }q| j| }	|d	7 }||d|| |f}
tjj|
}
|
| j |	< || | j|	< || | j|	< d| j|	< |d	7 }qXd S )
NTunscaledr    r   r      ​       r   )r8   r"   r&   r5   r9   r>   imImagerW   loadrA   r    r   r?   r@   rX   get_atr(   rY   
subsurfacescalesurface_scale)r+   surfswshr    r0   keycistartr.   ssr   r   r   re      s^    









z
SFont.load)NrQ   rR   rS   rV   re   __classcell__r   r   rZ   r   rT      s    rT   c                       s$   e Zd Z fddZdd Z  ZS )	MudgeFontc                    s0   t t|   || _|| _|| _|| _|| _d S r3   )rU   rs   rV   rW   xmlrX   r*   r)   )r+   rW   rt   rX   r*   r)   rZ   r   r   rV      s    zMudgeFont.__init__c                 C   s  i | _ i | _i | _i | _tjj| jj	dd}tj
	| j}t| }W d    n1 sb0    Y  d}|dD ]}t|jd }|dk rqzt|}t|jd }t|jd }	t|jd }
t|jd	 }|||	|
|f}tjj|}|| j |< |
| j|< |
| j|< d
| j|< t||}qz|| _|| _d| j vrtjj| j|fd| j d< | j| jd< | j| jd< d
| jd< d| j vr| j d | j d< | jd | jd< | jd | jd< | jd | jd< tjjd|fd| j d< d| jd< d| jd< d
| jd< d S )NTr\   r   r   idr6   r7   r"   r    r_   r^   ra   r`   )r8   r"   r&   r5   r9   r>   rc   rd   rW   re   loaderrt   etree
fromstringreadfindallintattribr   rg   rh   ri   maxr    r   r?   r@   rX   )r+   rj   ftreer    er   r.   r6   r7   whrp   r   r   r   re     sP    ,






zMudgeFont.loadrq   r   r   rZ   r   rs      s   rs   c                 C   s   d}g }d}| D ]N}|dks|dkr&q|dkrF|sF|r| | d}q|dkrV| }q||7 }q|rn| | |sxdg}tdd |d	d  D }|d
 |fS )N F
r^   "c                 s   s   | ]}| d dV  qdS )=r   N)split.0r0   r   r   r   	<genexpr>e      z$parse_bmfont_line.<locals>.<genexpr>r   r   )r'   dict)lr   linequoter.   mapr   r   r   parse_bmfont_lineI  s*    


r   c                       s$   e Zd Z fddZdd Z  ZS )BMFontc                    s   t t|   || _d S r3   )rU   r   rV   rW   )r+   rW   rZ   r   r   rV   k  s    zBMFont.__init__c                 C   s`  i | _ i | _i | _i | _i | _d| _i }tj| j	}|D ]t}t
|\}}|dkrvt|d | _t|d | _q>|dkrtjj|d jdd|t|d	 < q>|d
krvtt|d	 }t|d }t|d }t|d }	t|d }
t|d }t|d }t|d }t|d }|| |||	|
f}tjj|}|| j |< |	| | j|< || j|< ||f| j|< q>|dkr>tt|d }tt|d }t|d | j|| < q>W d    n1 s0    Y  d| j vr"| j d | j d< | jd | jd< | jd | jd< | jd | jd< tjjd| jfd| j d< d| jd< d| jd< d| jd< d S )Nr   common
lineHeightbasepagefileTr\   ru   r   r6   r7   r"   r    xoffsetyoffsetxadvancekerningfirstsecondamountra   r^   r`   r_   )r8   r"   r&   r5   r)   r*   r9   rv   re   rW   r   r{   r    r   r>   rc   rd   r   rg   rh   ri   r?   r@   )r+   pagesr~   r   kindargsr.   r6   r7   r   r   rF   rG   r   r   rp   r   r   r   r   r   re   p  sX    
(


8

zBMFont.loadrq   r   r   rZ   r   r   i  s   r   c                   @   s   e Zd ZdZdd ZdS )ScaledImageFontz;
    Represents an imagefont scaled by a given factor.
    c           	         s    fdd|j | _ |j| _|j| _fdd|j D | _fdd|j D | _fdd|j D | _fdd|j D | _i | _|j D ]>\}}|	 \}}|}|}t
jj|||f| j|< qd S )Nc                    s   t |   S r3   )r   )n)factorr   r   rh     s    z'ScaledImageFont.__init__.<locals>.scalec                    s   i | ]\}}| |qS r   r   r   kvrh   r   r   
<dictcomp>  r   z,ScaledImageFont.__init__.<locals>.<dictcomp>c                    s   i | ]\}}| |qS r   r   r   r   r   r   r     r   c                    s*   i | ]"\}}| |d   |d fqS )r   r   r   r   r   r   r   r     r   c                    s   i | ]\}}| |qS r   r   r   r   r   r   r     r   )r    r   r*   r"   itemsr&   r5   r)   r8   rA   r9   r>   rh   smoothscale)	r+   parentr   r   r   r   r   nwnhr   )r   rh   r   rV     s    zScaledImageFont.__init__N)rQ   rR   rS   __doc__rV   r   r   r   r   r     s   r   F
   z^!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~c                 C   sF   | du s|du s|du r t dt||||	|
|}|t| |||f< dS )a  
    :doc: image_fonts

    This registers an SFont with the given details. Please note that size, bold,
    italic, and underline are all advisory (used for matching), and do not
    change the appearance of the font.

    `More information about SFont. <http://www.linux-games.com/sfont/>`_

    `name`
        The name of the font being registered, a string.

    `size`
        The size of the font being registered, an integer.

    `bold`
        The boldness of the font being registered, a boolean.

    `italics`
        The italicness of the font being registered, a boolean.

    `underline`
        An ignored parameter.

    `filename`
        The file containing the sfont image, a string.

    `spacewidth`
        The width of a space character, an integer in pixels.

    `baseline`
        The distance from the top of the font to the baseline (the invisible
        line letters sit on), an integer in pixels.  If this font is mixed with
        other fonts, their baselines will be aligned.  Negative values indicate
        distance from the bottom of the font instead, and ``None`` means the
        baseline equals the height (i.e., is at the very bottom of the font).

    `default_kern`
        The default kern spacing between characters, in pixels.

    `kerns`
        A map from two-character strings to the kern that should be used between
        those characters.

    `charset`
        The character set of the font. A string containing characters in
        the order in which they are found in the image. The default character
        set for a SFont is::

            ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
            @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _
            ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
    NzOWhen registering an SFont, the font name, font size, and filename are required.)r$   rT   image_fonts)namesizebolditalicsrI   rW   rX   r   r*   r)   rY   sfr   r   r   register_sfont  s    9r   c
                 C   sL   | du s |du s |du s |du r(t dt|||||	}
|
t| |||f< dS )as  
    :doc: image_fonts

    This registers a MudgeFont with the given details. Please note that size,
    bold, italic, and underline are all advisory (used for matching), and do not
    change the appearance of the font.

    Please see the `MudgeFont home page <http://www.larryhastings.com/programming/mudgefont/>`_
    for the tool that creates MudgeFonts. Ren'Py assumes that character codes
    found in the MudgeFont xml file are unicode character numbers, and ignores
    negative character codes.

    `name`
        The name of the font being registered, a string.

    `size`
        The size of the font being registered, an integer.

    `bold`
        The boldness of the font being registered, a boolean.

    `italics`
        The italicness of the font being registered, a boolean.

    `underline`
        An ignored parameter.

    `filename`
        The file containing the MudgeFont image, a string. The image is usually
        a TGA file, but could be a PNG or other format that Ren'Py supports.

    `xml`
        The xml file containing information generated by the MudgeFont tool.

    `spacewidth`
        The width of a space character, an integer in pixels.

    `default_kern`
        The default kern spacing between characters, in pixels.

    `kerns`
        A map from two-character strings to the kern that should be used between
        those characters.
    NzaWhen registering a Mudge Font, the font name, font size, filename, and xml filename are required.)r$   rs   r   )r   r   r   r   rI   rW   rt   rX   r*   r)   mfr   r   r   register_mudgefont  s    / r   c                 C   s   t |}|t| |||f< dS )a  
    :doc: image_fonts

    This registers a BMFont with the given details. Please note that size, bold,
    italic, and underline are all advisory (used for matching), and do not
    change the appearance of the font.

    Please see the `BMFont home page <http://www.angelcode.com/products/bmfont/>`_
    for the tool that creates BMFonts. Ren'Py expects that the filename
    parameter will be to a file in the BMFont text format, that describes a
    32-bit font. The Alpha channel should contain the font information, while
    the Red, Green, and Blue channels should be set to one. The image files,
    kerning, and other control information is read out of the BMFont file.

    We recommend including Latin and General Punctuation as part of your BMFont,
    to ensure all of the Ren'Py interface can render.

    `name`
        The name of the font being registered, a string.

    `size`
        The size of the font being registered, an integer.

    `bold`
        The boldness of the font being registered, a boolean.

    `italics`
        The italicness of the font being registered, a boolean.

    `underline`
        An ignored parameter.

    `filename`
        The file containing BMFont control information.
    N)r   r   )r   r   r   r   rI   rW   bmfr   r   r   register_bmfont:  s    &r   c           
   	   C   s"  | t v rt |  S | }d}d| v r8| dd\}} t|}d }ztj| }W n ty   tjjrjtjj	rdd | dD }t
j  t
jj D ]T}|d ur| D ]6\}}|D ] }| |rt|d} qq|r qq|r qqY n0 |d u rtd|t|||}	|	t |< |	S )	Nr   @r   c                 S   s   g | ]}|   qS r   )striplowerr   r   r   r   
<listcomp>  r   zload_face.<locals>.<listcomp>,rbzCould not find font {0!r}.)
face_cacher   r{   r9   rv   re   IOErrorr:   	developerallow_sysfontspygamesysfontinitsysfontsSysfontsvaluesr   r   endswithr   r$   r%   ftfontFTFace)
fnorig_fnindex	font_filefontsr   _flagsffnr0   r-   r   r   r   	load_faceh  s<    


r         ?c	              
   C   s   |dkr$|t kr$t  t  |a | ||f}	tjj|	|	\} }}| |||f}
t|
d }|d ur|dkr|
tv r~t|
 }nt	||}|t|
< |S | ||||||||f	}
t|
d }|d ur|S t
| }t|t|| ||||||}|t|
< |S )Nr   )
last_scalescaled_image_fontsclear
font_cacher9   r:   font_replacement_mapr#   r   r   r   r   FTFontr{   )r   r   r   r   outline	antialiasverticalhintingrh   trm   r-   facer   r   r   get_font  s.    


 r   c                   C   s   t   t  dS )z 
    Clears the font cache.
    N)r   r   r   r   r   r   r   free_memory  s    r   c                  C   s0   t  D ]} |   qtjjD ]} t|  qd S r3   )r   r   re   r9   r:   preload_fontsr   )r0   r   r   r   
load_fonts  s    
r   c                   @   s8   e Zd ZdZe Zdd ZdddZdd	 Zd
d Z	dS )	FontGroupza
    :doc: font_group
    :args: ()

    A group of fonts that can be used as a single font.
    c                 C   s   i | _ i | _d S r3   )r   char_mapr+   r   r   r   rV     s    zFontGroup.__init__NFc           	      C   s   |du rTt |tr<|j D ]\}}|| jvr|| j|< qnd| jvrP|| jd< | S t |tsft|}t |tsxt|}|rt |tst|}||k rtdt||d D ]6}|| jvr|| j|< |dur|| j|< |r|d7 }q| S )a  
        :doc: font_group

        Associates a range of characters with a `font`.

        `start`
            The start of the range. This may be a single-character string, or
            an integer giving a unicode code point. If start is None, then the
            font is used as the default.

        `end`
            The end of the range. This may be a single-character string, or an
            integer giving a unicode code point. This is ignored if start is
            None.

        `target`
            If given, associates the given range of characters with specific
            characters from the given font, depending on target_increment.
            This may be a single-character string, or an integer giving a
            unicode code point. This is ignored if the character had already
            been added.

        `target_increment`
            If True, the [start, end] range is mapped to the
            [target, target+end-start] range. If False, every character from the
            range is associated with the target character.

        When multiple .add() calls include the same character, the first call
        takes precedence.

        This returns the FontGroup, so that multiple calls to .add() can be
        chained together.
        NzUIn FontGroup.add, the start of a character range must be before the end of the range.r   )	
isinstancer   r   r   r{   r   r$   r   r   )	r+   fontro   endrE   target_incrementr   r   r0   r   r   r   add  s0    #









zFontGroup.addc                 C   s   t |ts$t |ttfr*t|dkr*|f}t |ts<t|}|D ]L}t |tsVt|}d| jv sx|| jv sxtd||| j	vr@|| j	|< q@| S )a  
        :doc: font_group

        Remaps one or a set of characters to a single target character.

        `cha`
            The character or characters to remap. This may be a single-character
            string, or an integer giving a unicode code point, or an iterable of
            either.

        `target`
            The character to remap to. This may be a single-character string, or
            an integer giving a unicode code point.

        Any given character having already been remapped (either with add or with
        remap) will be ignored. However, if the FontGroup has no default font, any
        given character must have been previously added.

        This method also returns the FontGroup, for the same reasons.
        r   Nz1Character U+{0:04x} has no font in this FontGroup)
r   r{   r   bytesr(   r   r   r$   r%   r   )r+   charE   r0   r   r   r   remapA  s    $


zFontGroup.remapc           	      #   s   d}d}d}d} j r<dd |D }d fdd|D }t|D ]|\}}t|} jt|d}|du r jdd}|du rtd|||kr|r|||| fV  |}|}|d	7 }qD|dur|||d fV  dS )
zK
        Segments `s` into fonts. Generates (font, string) tuples.
        r   Nc                 S   s   g | ]}t |qS r   )r   r   r   r   r   r   t  r   z%FontGroup.segment.<locals>.<listcomp>r   c                 3   s    | ]}t  j||V  qd S r3   )r   r   r#   r   r   r   r   r   u  r   z$FontGroup.segment.<locals>.<genexpr>z*Character U+{0:04x} not found in FontGroupr   )r   join	enumerater   r   r#   r$   r%   )	r+   r,   markposr   old_fontr0   r.   r   r   r   r   segmenth  s,    
zFontGroup.segment)NF)
rQ   rR   rS   r   r   r   rV   r   r   r   r   r   r   r   r     s   	
G'r   )NNFFFN)7
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   pygame_sdl2r   xml.etree.ElementTreerw   ElementTreer$   r9   renpy.text.ftfonttextr   renpy.text.textsupportr   initr<   r=   r   objectr   rT   rs   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sN   8a[N ?


@


6  
+4-	