o
    @@iv                     @   s   d Z ddl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 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ZdS )a  
This class attempts to optimize a Type0Font by performing subsetting.
Subsetting is performed using fontTools.subset, after which the dictionaries are updated in the
Type0Font, the page content is also changed, updating the TJ instructions to refer to the new GIDs.
    N)	Subsetter)TTFont)Decimal)Name)CharacterSetListener)CopyCommandOperator)"SubSetShowTextWithGlyphPositioning)Canvas)CanvasStreamProcessor)	Type0Font)Font)TrueTypeFont)Pagec                   @   sr   e Zd ZdZedddejeeje	 f fddZ
ededeje deje fd	d
ZededefddZdS )r   a  
    This class attempts to optimize a Type0Font by performing subsetting.
    Subsetting is performed using fontTools.subset, after which the dictionaries are updated in the
    Type0Font, the page content is also changed, updating the TJ instructions to refer to the new GIDs.
    pager   returnc                 C   s8   t | t g }t }|t| d d |g | S )NContentsDecodedBytes)r
   r	   r   readioBytesIOget_character_set_per_font)r   cspl r   Z/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/borb/io/write/font/subsetter.py_extract_text_per_font1   s   z Subsetter._extract_text_per_font	old_fonts	new_fontsc                 C   s   t | t g }t }|j D ]\}}t|||j|< qt||||jd< |t	| d d  || d t
d< t|d| d t
d< t
d| d t
d< tt|| d t
d< | S )	NTJr   r   	   BytesFlateDecodeFilterLength)r
   r	   	bytearray_canvas_operatorsitemsr   r   r   r   r   r   zlibcompressbDecimallen)r   r   r   r   new_content_streamkvr   r   r   _modify_page_content_stream8   s   
z%Subsetter._modify_page_content_streamc              	      s  d| vr| S t | d ts| S d| d vr| S dd | d d  D }t|dkr,| S t|   fdd|D }t|dkrB| S g }|D ]}|d d d d	 d
 }tt|}t	 }|j
ddd  | D d d|j_d|j_d|j_|| t }|| | }W d   n1 sw   Y  t|}ddd tddD }	t|	d t|td  |td< |td |d td< t||td< |td || qFt| || | d d  D ]\}
}||vrq||| }|| d d |
< q| S )z
        This function applies Font subsetting to the input Page and returns the Page afterwards
        :param page:    the Page on which to apply Font subsetting
        :return:        the Page with Font subsetting
        	Resourcesr   c                 S   s   g | ]	}t |tr|qS r   )
isinstancer   .0xr   r   r   
<listcomp>j   s
    
z#Subsetter.apply.<locals>.<listcomp>r   c                    s    g | ]}t  | d k r|qS )   )r*   r1   characters_per_fontr   r   r4   v   s    DescendantFontsFontDescriptor	FontFile2r    c                 S   s   g | ]}|qS r   r   r1   r   r   r   r4      s    )textTNc                 S   s   g | ]}t d qS )ABCDEFGHIJKLMNOPQRSTUVWXYZ)randomchoice)r2   _r   r   r   r4      s       +BaseFontFontName	ToUnicodeEncoding)r0   dictvaluesr*   r   r   r   r   r   
fSubsetterpopulatejoinoptionsglyph_namesrecalc_boundsrecalc_average_widthsubsetsavegetvaluer   true_type_font_from_fileranger   str"_build_custom_cmap_for_type_0_fontpopappendr.   r&   index)r   fonts_to_be_subsetsubset_fontsold_fontfont_file_bytes_001ttfont	subsetterfont_file_bytes_002new_fontbasefont_prefixold_font_namer   r6   r   apply[   sd   
 




$zSubsetter.applyN)__name__
__module____qualname____doc__staticmethodtypingDictr   SetrU   r   r   Listr.   rd   r   r   r   r   r   "   s    $"r   ) rh   r   r>   rj   r'   fontTools.subsetr   rI   fontTools.ttLibr   borb.io.read.typesr   r)   r   )borb.io.write.font.character_set_listenerr   (borb.io.write.font.copy_command_operatorr   :borb.io.write.font.subset_show_text_with_glyph_positioningr   borb.pdf.canvas.canvasr	   'borb.pdf.canvas.canvas_stream_processorr
   /borb.pdf.canvas.font.composite_font.font_type_0r   borb.pdf.canvas.font.fontr   /borb.pdf.canvas.font.simple_font.true_type_fontr   borb.pdf.page.pager   r   r   r   r   <module>   s&   