o
    @@i$K                     @   s   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ G d
d deZdS )uC  
In metal typesetting, a font was a particular size, weight and style of a typeface.
Each font was a matched set of type, one piece (called a "sort") for each glyph, and a typeface consisting of a range of fonts that shared an overall design.

In modern usage, with the advent of digital typography, "font" is frequently synonymous with "typeface".
Each style is in a separate "font file"—for instance, the typeface "Bulmer" may include the fonts "Bulmer roman",
"Bulmer", "Bulmer bold" and "Bulmer extended"—but the term "font" might be applied either to one of these alone or to the whole typeface.

In both traditional typesetting and modern usage, the word "font" refers to the delivery mechanism of the typeface design.
In traditional typesetting, the font would be made from metal or wood.
Today, the font is a digital file.
    N)Decimal)HighLevelTokenizer)Token)	TokenType)
Dictionary)List)Namec                   @   s   e Zd ZdZi fdddZdedefddZddd	Zed
e	de
jeef fddZdede
je fddZdefddZdefddZde
je fddZdefddZdede
je fddZdede
je fddZdS )Fontz
    In both traditional typesetting and modern usage, the word "font" refers to the delivery mechanism of the typeface design.
    In traditional typesetting, the font would be made from metal or wood.
    Today, the font is a digital file.
    returnc                 C   s  |   }td|td< d| v rtt| d |td< d| v r(| d |td< d| v r4| d |td< d| v rOt |td< | d D ]}|td | qCd| v r^| | d |td< d| v rt| d trutt| d |td< t| d trt |td< td|d td< d	| d v rtt| d d	 |d td	< d
| d v rt }| d d
 D ]}|| q||d td
< d| v rt	| d |td< d| v rt |td< | d D ]	}|d | qd| v rt |td< | d D ]	}|d | qd| v r4t |td< | d d |d td< | d d |d td< | d d |d td< d| v rA| d |td< d| v r}t |td< | d D ]+}t|t
r`|d | t|tr{t }|D ]}|| qk|d | qQd| v rt |td< |d | d d | d| v rt |td< | d D ]
}|d | q|  D ]\}}||vrt	||||< q|S )Nr	   TypeBaseFont	FirstCharLastCharWidthsFontDescriptorEncodingBaseEncodingDifferences	ToUnicodeFontBBox
FontMatrixCIDSystemInfoRegistryOrdering
SupplementDWWDescendantFontsr   DW2)_empty_copyr   strr   append_copy_font_descriptor
isinstancer   copydeepcopybDecimal__deepcopy__items)selfmemodictoutklxyv r1   W/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/borb/pdf/canvas/font/font.pyr'   -   s   






zFont.__deepcopy__font_descriptor_to_copyc                 C   s  |}| d }| dtd|td< |d |td< d|v r&|d |td< d|v r2|d |td< d|v r>|d |td< |d |td< d|v rK	 |d |td< d|v r_|d |td< d|v rk|d |td< d|v rw|d |td< d|v r|d |td< d|v r|d |td< d|v r|d |td< d|v r|d |td< d|v r|d |td< d|v r|d |td< d|v r|d |td< d|v r	 d|v r	 d|v r	 d|v r	 |	 D ]\}}||vrt|||< q|S )Nr   r   FontName
FontFamilyFontStretch
FontWeightFlagsr   FTr   ItalicAngleAscentDescentLeading	CapHeightXHeightStemVStemHAvgWidthMaxWidthMissingWidthFontFile	FontFile2	FontFile3CharSet)
getr   r   set_is_inlinerangelenr!   r$   r%   r(   )r)   r3   f0f1ir,   r0   r1   r1   r2   r"      sT   




zFont._copy_font_descriptorc                 C   s   J )Nr1   r)   r1   r1   r2   r      s   zFont._empty_copy
cmap_bytesc              
      sZ  i }t t| }d }t| }| |k r+| }|d us J | dkr|d us,J t| }td|D ]c}| }|d usCJ t| dd d}| }|d usXJ | 	dr| 
drt| dd dd	d	fd
dtdtdD n| 	drJ dJ d||< q7| dkr|d usJ t| }	td|	D ]X}| }|d usJ t| dd d}
| }|d usJ t| dd d}| }|d usJ | tjkrt| }t|
|d D ]}t|||
  ||< qq| dkr#|d usJ t| }td|D ]}| }|d us2J t| dd d}| }|d usHJ t| dd d}| }|d us^J | tjkrt| dd }t||d D ]=}tt|d||  dd   d	dd tddt d  D    d	 fddtdt dD   ||< qwq%| tjkr!t||d D ]C}| }|d usJ t| dd }d	dd tddt|d  D |  d	 fddtdt dD   ||< q| }|d usJ | tjksJ q%q%|}| |k s|S )Nbeginbfcharr         <>  c                    &   g | ]}t t ||d   dqS    rT   chrint.0j)unicode_strr1   r2   
<listcomp>      & z#Font._read_cmap.<locals>.<listcomp>r[   /FzUnsupported CMAP syntaxzInvalid CMAPbegincidrangebeginbfrange   c                 S      g | ]}d qS 0r1   r`   _r1   r1   r2   rc         c                    rY   rZ   r\   r_   unicode_hexr1   r2   rc     rd   c                 S   ri   rj   r1   rl   r1   r1   r2   rc     rn   c                    rY   rZ   r\   r_   ro   r1   r2   rc     rd   )r   ioBytesIOrK   tellnext_non_comment_tokenget_textr^   rJ   
startswithendswithr    replacejoinget_token_typer   NUMBERr]   
HEX_STRINGhexSTART_ARRAY	END_ARRAY)rP   out_mapcmap_tokenizer
prev_tokennumber_of_bytestokennumber_of_lines_001rm   	char_codenumber_of_lines_002char_code_start_002char_code_stop_002unicode_baserN   number_of_lines_003char_code_start_003char_code_stop_003unicode_base_strunicode_base_str_003r1   )rp   rb   r2   
_read_cmap   s   &
*$*$]zFont._read_cmapcharacter_identifierc                 C      dS )a  
        This function maps a character identifier to its unicode str.
        If no such mapping exists, this function returns None.
        :param character_identifier:    the character identifier
        :return:                        the matching unicode str
        Nr1   r)   r   r1   r1   r2   character_identifier_to_unicode2  s   	z$Font.character_identifier_to_unicodec                 C      t dS )z
        This function returns the maximum height above the baseline reached by glyphs in this font.
        The height of glyphs for accented characters shall be excluded.
        :return:    the ascent
        r   r&   rO   r1   r1   r2   
get_ascent=     zFont.get_ascentc                 C   r   )z
        This function returns the maximum depth below the baseline reached by glyphs in this font.
        The value shall be a negative number.
        :return:    the descent
        r   r   rO   r1   r1   r2   get_descentE  r   zFont.get_descentc                 C   s,   d| v r
t | d S d| v rt | d S dS )z
        This function returns the name of the Font
        e.g. "Helvetica"
        :return:    the name of this Font
        r   r5   N)r    rO   r1   r1   r2   get_font_nameM  s
   zFont.get_font_namec                 C   sp  |  d}d}|dur| |}|dur|S d| v r*d| d v r*| d d td S d| v r>d| d v r>| d d td S d| v ret| d tret| d dkred	| d d
 v re| d d
 d	 td S i dtddtddtddtddtddtddtddtddtddtddtddtddtddtddtddtdd tdi d!td"d#tdd$tdd%tdd&tdd'td(d)tdd*tdd+tdd,tdd-tdd.tdd/tdd0tdd1tdd2tdd3tdi d4tdd5tdd6td7d8td7d9td(d:td(d;td7d<td=d>td?d@td(dAtddBtddCtd7dDtddEtddFtd(dGtd?td7td?td(td7td=td(td7tdHtd7td7td=dI}| D ]!\}}|  |}|dur| |}|durt||   S qtdJS )Ka  
        This function estimates the width of the space character (unicode 32) in this Font.
        If the Font contains the character, this Font will return the corresponding width.

        If the Font does not contain the character, its width may be derived from the
        MissingWidth entry in the FontDescriptor, or the AvgWidth entry.

        If the Font is a composite Font, the DW entry of its DescendantFont is used.

        If all previously mentioned approaches fail, the width is estimated based on characters
        that may be present in the Font. (e.g. the width of 'A' is typically twice that of ' ').
        :return:    (an estimate of) the width of the space character
        rW   Nr   rC   rh   rA   r   rR   r   r   az0.500bcz0.556defz1.000ghrN   z1.252ra   r,   r-   mz0.334nopqrz0.835stur0   wz0.385r.   r/   zrk   123456789Az0.417BCDEFz0.455Gz0.357HIJKLMNOz0.294)PQRSTUVr   XYZi  )unicode_to_character_identifier	get_widthr   r#   r   rK   r(   r&   )r)   r   widthchar_to_space_width_ratior,   r0   r1   r1   r2   "get_space_character_width_estimateY  s
  
			






z'Font.get_space_character_width_estimatec                 C   r   )a  
        This function returns the width (in text space) of a given character identifier.
        If this Font is unable to represent the glyph that corresponds to the character identifier,
        this function returns None
        :param character_identifier:    the character_identifier
        :return:                        the width (in text space) of the character identifier
        Nr1   r   r1   r1   r2   r     s   zFont.get_widthunicodec                 C   r   )a$  
        This function maps a unicode str to its character identifier.
        If no such mapping exists, this function returns None.
        :param unicode:             the unicode character
        :return:                    the character identifier matching the unicode character
        Nr1   )r)   r   r1   r1   r2   r     s   z$Font.unicode_to_character_identifierN)r
   r	   )__name__
__module____qualname____doc__r'   r   r"   r   staticmethodbytestypingDictr^   r    r   Optionalr   r&   r   r   r   r   r   r   r   r1   r1   r1   r2   r	      s$    a
3l
I
r	   )r   r$   rq   r   decimalr   *borb.io.read.tokenize.high_level_tokenizerr   )borb.io.read.tokenize.low_level_tokenizerr   r   borb.io.read.typesr&   r   r   r   r	   r1   r1   r1   r2   <module>   s   