o
    g÷§i3&  ã                   @   sP   d Z ddlZddlmZ ddlmZ ddlmZ G dd„ dƒZG dd„ dƒZ	dS )	u  
    In typography, a glyph /É¡lÉªf/ is an elemental symbol within an agreed set of symbols,
    intended to represent a readable character for the purposes of writing.
    Glyphs are considered to be unique marks that collectively add up to the spelling of a word
    or contribute to a specific meaning of what is written, with that meaning dependent on cultural and social usage.
é    N)ÚDecimal)ÚFontc                   @   sP   e Zd ZdZdededefdd„Zdefdd	„Zdefd
d„Z	defdd„Z
dS )ÚGlyphuƒ  
    In typography, a glyph /É¡lÉªf/ is an elemental symbol within an agreed set of symbols,
    intended to represent a readable character for the purposes of writing.
    Glyphs are considered to be unique marks that collectively add up to the spelling of a word
    or contribute to a specific meaning of what is written, with that meaning dependent on cultural and social usage.
    Úcharacter_codeÚunicode_strÚwidthc                 C   s   || _ || _|| _d S ©N)Ú_character_codeÚ_unicode_strÚ_width)Úselfr   r   r   © r   úS/home/ubuntu/.local/lib/python3.10/site-packages/borb/pdf/canvas/font/glyph_line.pyÚ__init__   s   
zGlyph.__init__Úreturnc                 C   ó   | j S )zv
        This function returns the character code of this Glyph object
        :return:    the character code
        )r	   ©r   r   r   r   Úget_character_code)   ó   zGlyph.get_character_codec                 C   r   )zv
        This function returns the unicode str that this Glyph represents
        :return:    the unicode str
        )r
   r   r   r   r   Úget_unicode_str0   r   zGlyph.get_unicode_strc                 C   r   )z}
        This function returns the width (in text space) of this Glyph
        :return:    the witdh (in text space)
        )r   r   r   r   r   Ú	get_width7   r   zGlyph.get_widthN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚintÚstrr   r   r   r   r   r   r   r   r   r      s    r   c                   @   sH  e Zd ZdZedƒedƒedƒfdeje dedededed	efd
d„Z	dd„ Z
dd„ Zededefdd„ƒZdejed f dd fdd„Zeedƒedƒedƒfdededededed	edd fdd„ƒZeedƒedƒedƒfdededededed	edd fdd„ƒZdefdd„Zdefdd„Zdejd  fd d!„Zdefd"d#„Zd$S )%Ú	GlyphLinez‹
    This class represents a line of Glyph objects.
    This class contains utility methods to work with collections of Glyph objects.
    r   éd   ÚglyphsÚfontÚ	font_sizeÚcharacter_spacingÚword_spacingÚhorizontal_scalingc                 C   s6   t |tƒsJ ‚|| _|| _|| _|| _|| _|| _d S r   )Ú
isinstancer   Ú_character_spacingÚ_fontÚ
_font_sizeÚ_glyphsÚ_horizontal_scalingÚ_word_spacing)r   r   r    r!   r"   r#   r$   r   r   r   r   I   s   

zGlyphLine.__init__c                 C   s
   t | jƒS r   )Úlenr)   r   r   r   r   Ú__len___   s   
zGlyphLine.__len__c                 C   s   |   ¡ S r   )Úget_textr   r   r   r   Ú__str__b   s   zGlyphLine.__str__Úcr   c                 C   s   t | ƒdv S )N)é	   é
   é   é   é   é    )Úord)r0   r   r   r   Ú_isspacee   s   zGlyphLine._isspaceÚglyph_or_glyphlinec                 C   s<   t |tƒr| j |¡ t |tƒr|jD ]}| j |¡ q| S )zü
        This function appends a Glyph (or all Glyph objects in a GlyphLine) to this GlyphLine.
        This function returns self.
        :param glyph_or_glyphline:  the glyph or glyphline to be added
        :return:                    self
        )r%   r   r)   Úappendr   )r   r9   Úgr   r   r   Úaddm   s   


zGlyphLine.addÚtextc           
   	   C   s  g }d}|t | ƒk r…d}|d t | ƒk r>| | d | |d   }	| |	¡}|dur>| t|	|| |	¡p6tdƒƒ¡ |d7 }q|t | ƒk rh| | | ¡}|durh| t| | || | | ¡p`tdƒƒ¡ |d7 }q|t | ƒk r| t| | dtdƒƒ¡ |d7 }|t | ƒk s
t||||||ƒS )a0  
        This method constructs a new GlyphLine from text (represented as character ids)
        :param text:                a byte-array containing the character ids
        :param font:                the font
        :param font_size:           the font-size
        :param character_spacing:   the (additional) space between characters
        :param word_spacing:        the (additional) space between words
        :param horizontal_scaling:  the horizontal scaling factor (100 represents no zoom)
        :return:                    a GlyphLine
        r   Né   é   é   u   ï¿½éú   )r,   Úcharacter_identifier_to_unicoder:   r   r   ÚbDecimalr   r   )
r=   r    r!   r"   r#   r$   r   ÚiÚunicode_charsÚmulti_byte_char_coder   r   r   Ú
from_bytes{   sL   ÿýÿýÿÞ#ÿzGlyphLine.from_bytesc                    s<   ‡ fdd„ˆD ƒ}‡ ‡fdd„t |ƒD ƒ}t|ˆ ||||ƒS )a  
        This method constructs a new GlyphLine from text
        :param text:                a string which will be decoded into character-ids
        :param font:                the font
        :param font_size:           the font-size
        :param character_spacing:   the (additional) space between characters
        :param word_spacing:        the (additional) space between words
        :param horizontal_scaling:  the horizontal scaling factor (100 represents no zoom)
        :return:                    a GlyphLine
        c                    s   g | ]	}ˆ   |¡p
d ‘qS ©r   )Úunicode_to_character_identifier)Ú.0r0   )r    r   r   Ú
<listcomp>Ì   s    ÿz&GlyphLine.from_str.<locals>.<listcomp>c                    s.   g | ]\}}t |ˆ| ˆ  |¡ptd ƒƒ‘qS rH   )r   r   r   )rJ   rD   Úcid©r    r=   r   r   rK   Ï   s    ûýÿ)Ú	enumerater   )r=   r    r!   r"   r#   r$   Úcharacter_idsr   r   rM   r   Úfrom_str¸   s   
ÿúÿzGlyphLine.from_strc                 C   s   d  dd„ | jD ƒ¡S )z‡
        This function returns the unicode str represented by the Glyph objects in this GlyphLine
        :return:    the text
        Ú c                 S   s   g | ]}|  ¡ ‘qS r   )r   ©rJ   Úxr   r   r   rK   à   ó    z&GlyphLine.get_text.<locals>.<listcomp>)Újoinr)   r   r   r   r   r.   Û   ó   zGlyphLine.get_textc                 C   s„   t dƒ}| jD ]3}| ¡ | j t dƒ }t| ¡ ƒdkr(t | ¡ ¡r(|| j7 }|| j	t dƒ 9 }|| j
7 }||7 }q|| j
8 }|S )z„
        This function calculates the width (in text space) of this GlyphLine
        :return:    the width (in text space)
        r   gü©ñÒMbP?r>   r   )r   r)   r   r(   r,   r   r   r8   r+   r*   r&   )r   Úwr;   Úglyph_width_in_text_spacer   r   r   Úget_width_in_text_spaceâ   s   
ÿ



z!GlyphLine.get_width_in_text_spacec              
   C   s8   g }| j D ]}| t|g| j| j| j| j| jƒ¡ q|S )zÝ
        This function splits the GlyphLine into several GlyphLine objects,
        one per Glyph in the (original, this) GlyphLine.
        :return:    the resulting GlyphLine objects (as typing.List[GlyphLine])
        )r)   r:   r   r'   r(   r&   r+   r*   )r   Úoutr;   r   r   r   Úsplit   s   
úÿ
zGlyphLine.splitc                 C   s   t dd„ |  ¡ D ƒƒS )zâ
        This function returns True if any of the Glyph objects in the GlyphLine has a non-zero descent, False otherwise
        :return:    True if this GlyphLine has characters with non-zero descent, False otherwise
        c                 S   s   g | ]}|d v ‘qS ))ÚyÚpÚqÚfr;   Újr   rR   r   r   r   rK     rT   z*GlyphLine.uses_descent.<locals>.<listcomp>)Úanyr.   r   r   r   r   Úuses_descent  rV   zGlyphLine.uses_descentN)r   r   r   r   r   ÚtypingÚListr   r   r   r-   r/   Ústaticmethodr   Úboolr8   ÚUnionr<   ÚbytesrG   rP   r.   rY   r[   rb   r   r   r   r   r   ?   s‚    ùþýüûú
ùúÿþýüûúù<úÿþýüûúù"r   )
r   rc   Údecimalr   Úborb.io.read.typesrC   Úborb.pdf.canvas.font.fontr   r   r   r   r   r   r   Ú<module>   s   /