HEX
Server: Apache
System: Linux srv1.prosuiteplus.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: prosuiteplus (1001)
PHP: 8.3.20
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/ocrmypdf/pdfinfo/__pycache__/info.cpython-38.pyc
U

��Z^$i�@sxddlZddlZddlmZddlmZddlmZddlm	Z	m
Z
ddlmZddl
mZddlmZddlZdd	lmZdd
lmZddlmZmZdd
lmZddlmZmZe��Zedd�Zedd�Zej ej ej!ej!ej"ej#ej$ej%ej&ej'ej(ej ej!ej"ej%d�Z)ej*ej+ej,ej-ej*ej+ej.ej/ej0ej1ej2d�Z3ej dej!dej"dej#dej%diZ4dZ5dd�Z6eddddg�Z7eddddg�Z8ed d!d"d#g�Z9ed$d%d&d'g�Z:Gd(d)�d)�Z;d*d+�Z<e5fd,d-�Z=d.d/�Z>Gd0d1�d1�Z?d2d3�Z@d4d5�ZAd6d7�ZBd8d9�ZCdd:�d;d<�ZDd=d>�ZEd?d@�ZFeGdA�dBdC�ZHdNdEdF�ZIGdGdH�dH�ZJGdIdJ�dJ�ZKdKdL�ZLeMdMk�rteL�dS)O�N)�
namedtuple��Decimal)�Enum)�hypot�isclose)�fspath)�Path)�warn)�	PdfMatrix)�tqdm)�EncryptedPdfError�MissingDependencyError�)�	ghosttext)�get_page_analysis�get_text_boxes�
Colorspacez5gray rgb cmyk lab icc index sep devn pattern jpeg2000�Encodingz>ccitt jpeg jpeg2000 jbig2 asciihex ascii85 lzw flate runlength)z/DeviceGrayz/CalGrayz
/DeviceRGBz/CalRGBz/DeviceCMYKz/Labz	/ICCBasedz/Indexedz/Separationz/DeviceNz/Patternz/Gz/RGBz/CMYKz/I)z/CCITTFaxDecodez
/DCTDecodez
/JPXDecodez/JBIG2Decodez/CCFz/DCTz/AHxz/A85z/LZWz/Flz/RL��)��?�rrrrcCs&tt|�}t|t�}tdd�|D��S)NcSsg|]\}}t||dd��qS)g����MbP?)Zrel_tol)r)�.0�a�b�r�7/usr/lib/python3/dist-packages/ocrmypdf/pdfinfo/info.py�
<listcomp>]sz#_is_unit_square.<locals>.<listcomp>)�map�float�zip�UNIT_SQUARE�all)�	shorthand�valuesZpairwiserrr�_is_unit_squareZs

r&�XobjectSettings�namer$�stack_depth�InlineSettings�iimage�ContentsInfo�xobject_settings�
inline_images�found_vector�TextboxInfo�bbox�
is_visible�
is_corruptc@seZdZdd�ZdS)�
VectorInfocCsdS�Nr��selfrrr�__init__lszVectorInfo.__init__N)�__name__�
__module__�__qualname__r8rrrrr4ksr4ccsF|D]<\}}t|�}t�d|�r6|D]}g|fVq$q||fVqdS)z7Convert runs of qQ's in the stack into single graphobjszQ*q+$N)�str�re�match)Z	graphobjs�operands�operator�charrrr�_normalize_stackpsrBc	Csng}t|�}g}g}d}td���}td���}d�||B�}	ttt�||	���D�]
\}
}|\}}
|
dkr�|�|�t	|�dkr�t	|�dkr�t
d|
��td	�qR|
d
kr�z|��}Wnt
k
r�td�YnXqR|
dkr�t|�|}qR|
d
k�r |d}t||jt	|�d�}|�|�qR|
dk�rR|d}t||jt	|�d�}|�|�qR|
|krRd}qRt|||d�S)a�Interpret the PDF content stream.

    The stack represents the state of the PDF graphics stack.  We are only
    interested in the current transformation matrix (CTM) so we only track
    this object; a full implementation would need to track many other items.

    The CTM is initialized to the mapping from user space to device space.
    PDF units are 1/72".  In a PDF viewer or printer this matrix is initialized
    to the transformation to device space.  For example if set to
    (1/72, 0, 0, 1/72, 0, 0) then all units would be calculated in inches.

    Images are always considered to be (0, 0) -> (1, 1).  Before drawing an
    image there should be a 'cm' that sets up an image coordinate system
    where drawing from (0, 0) -> (1, 1) will draw on the desired area of the
    page.

    PDF units suit our needs so we initialize ctm to the identity matrix.

    According to the PDF specification, the maximum stack depth is 32. Other
    viewers tolerate some amount beyond this.  We issue a warning if the
    stack depth exceeds the spec limit and set a hard limit beyond this to
    bound our memory requirements.  If the stack underflows behavior is
    undefined in the spec, but we just pretend nothing happened and leave the
    CTM unchanged.
    FzS s f F f* B B* b b*zBI ID EI q Q Do cm� �q� �z5PDF graphics stack overflowed hard limit, operator %iz(PDF graphics stack overflowed spec limit�Qz5PDF graphics stack underflowed - PDF may be malformed�cmZDor)r(r$r)zINLINE IMAGE)r+r$r)T)r-r.r/)r�set�split�join�	enumeraterB�pikepdfZparse_content_stream�append�len�RuntimeErrorr
�pop�
IndexErrorr'r$r*r,)Z
contentstream�initial_shorthand�stack�ctmr-r.r/Z
vector_opsZ	image_opsZoperator_whitelist�nZgraphobjr?r@Z
image_name�settingsr+�inlinerrr�_interpret_contents{sh
��
�

�
��rYc
CsT|\}}}}}}t||�}t||�}|d|}	|d|}
|	d}|
d}||fS)ayGiven the transformation matrix and image size, find the image DPI.

    PDFs do not include image resolution information within image data.
    Instead, the PDF page content stream describes the location where the
    image will be rasterized, and the effective resolution is the ratio of the
    pixel size to raster target size.

    Normally a scanned PDF has the paper size set appropriately but this is
    not guaranteed. The most common case is a cropped image will change the
    page size (/CropBox) without altering the page content stream. That means
    it is not sufficient to assume that the image fills the page, even though
    that is the most common case.

    A PDF image may be scaled (always), cropped, translated, rotated in place
    to an arbitrary angle (rarely) and skewed. Only equal area mappings can
    be expressed, that is, it is not necessary to consider distortions where
    the effective DPI varies with position.

    To determine the image scale, transform an offset axis vector v0 (0, 0),
    width-axis vector v0 (1, 0), height-axis vector vh (0, 1) with the matrix,
    which gives the dimensions of the image in PDF units. From there we can
    compare to actual image dimensions. PDF uses
    row vector * matrix_tranposed unlike the traditional
    matrix * column vector.

    The offset, width and height vectors can be combined in a matrix and
    multiplied by the transform matrix. Then we want to calculated
        magnitude(width_vector - offset_vector)
    and
        magnitude(height_vector - offset_vector)

    When the above is worked out algebraically, the effect of translation
    cancels out, and the vector magnitudes become functions of the nonzero
    transformation matrix indices. The results of the derivation are used
    in this code.

    pdfimages -list does calculate the DPI in some way that is not completely
    naive, but it does not get the DPI of rotated images right, so cannot be
    used anymore to validate this. Photoshop works, or using Acrobat to
    rotate the image back to normal.

    It does not matter if the image is partially cropped, or even out of the
    /MediaBox.

    rr�R@)r)
�
ctm_shorthandZ
image_sizerr�c�d�_Zimage_drawn_widthZimage_drawn_heightZscale_wZscale_hZdpi_wZdpi_hrrr�_get_dpi�s/

r_c@s�eZdZed�Zddddd�dd�Zedd��Zed	d
��Zedd��Z	ed
d��Z
edd��Zedd��Zedd��Z
edd��Zedd��Zedd��Zdd�ZdS)�	ImageInfoz1.000�N)r(�pdfimagerXr$cCs&t|�|_||_|dk	r&d|_|j}n|dk	r>d|_t�|�}|j|_|j	|_
|jr\d|_nd|_t
|j�|_zt�|jdd�|_Wntk
r�d|_YnXzt�|jd�|_Wntk
r�d|_YnX|jtjkr�tj|_t�|jd�|_|jdk�r"|jtjtjfk�r"ttj |_dS)NrXZxobjectZstencil�imager�?)!r<�_name�
_shorthandZ_originr+rMZPdfImage�width�_width�height�_heightZ
image_mask�_type�intZbits_per_component�_bpc�FRIENDLY_ENCODING�get�filters�_encrR�FRIENDLY_COLORSPACEZ
colorspace�_color�NotImplementedErrorr�jpeg2000r�
FRIENDLY_COMP�_comp�ccitt�jbig2�gray)r7r(rbrXr$Zpimrrrr8s6

 zImageInfo.__init__cCs|jSr5)rer6rrrr(;szImageInfo.namecCs|jSr5)rkr6rrr�type_?szImageInfo.type_cCs|jSr5)rhr6rrrrgCszImageInfo.widthcCs|jSr5)rjr6rrrriGszImageInfo.heightcCs|jSr5)rmr6rrr�bpcKsz
ImageInfo.bpccCs|jSr5)rsr6rrr�colorOszImageInfo.colorcCs|jSr5)rwr6rrr�compSszImageInfo.compcCs|jSr5)rqr6rrr�encWsz
ImageInfo.enccCst|j|j|jf�dS)Nr�r_rfrhrjr6rrr�xres[szImageInfo.xrescCst|j|j|jf�dS)Nrr�r6rrr�yres_szImageInfo.yrescs"�fdd�t��D�}djf|�S)Ncs$i|]}|�d�s|t�|d��qS)r^N)�
startswith�getattr)r�attrr6rr�
<dictcomp>ds
�z&ImageInfo.__repr__.<locals>.<dictcomp>zV<ImageInfo '{name}' {type_} {width}x{height} {color} {comp} {bpc} {enc} {xres}x{yres}>)�dir�format)r7Zclass_localsrr6r�__repr__cs
���zImageInfo.__repr__)r9r:r;rZDPI_PRECr8�propertyr(r{rgrir|r}r~rr�r�r�rrrrr`s.,









r`ccs.t|j�D]\}}td||j|d�Vq
dS)z'Find inline images in the contentstreamzinline-%02d)r(r$rXN)rLr.r`r$)�contentsinforVrXrrr�_find_inline_imagesos�r�ccsfd|krdS|d}d|kr dS|d��}|D]0}||}d|krFq0|ddkr0|}||fVq0dS)a�Search for all XObject-based images in the container

    Usually the container is a page, but it could also be a Form XObject
    that contains images. Filter out the Form XObjects which are dealt with
    elsewhere.

    Generate a sequence of tuples (image, xobj container), where container,
    where xobj is the name of the object and image is the object itself,
    since the object does not know its own name.

    �
/ResourcesN�/XObject�/Subtypez/Image)�as_dict)�	container�	resources�xobjs�xobj�	candidaterbrrr�_image_xobjectsxs
r�ccsXt|�D]J\}}|jD]:}|j|kr&q|jdkr<t|j�r<qt|j||jd�VqqdS)z�Find images stored in the container's /Resources /XObject

    Usually the container is a page, but it could also be a Form XObject
    that contains images.

    Generates images with their DPI at time of drawing.
    r)r(rbr$N)r�r-r(r)r&r$r`)r�r�rbr�Zdrawrrr�_find_regular_images�s	

r�c
cs�d|krdS|d}d|kr dS|d��}|D]P}||}|ddkrJq0|}|jD]*}|j|krdqT|j}	t|||	d�EdHqTq0dS)z|Find any images that are in Form XObjects in the container

    The container may be a page, or a parent Form XObject.

    r�Nr�r��/Form��pdfr�r$)r�r-r(r$�_process_content_streams)
r�r�r�r�r�r�r�Zform_xobjectrWr[rrr�_find_form_xobject_images�s(

�r�)r$ccs�|�d�dkr d|kr |pt}nZ|�d�dkrv|ddkrv|rFt|�nt��}|�dt���}t|�}||}|j}ndSt||�}|jr�t�Vt|�EdHt	||�EdHt
|||�EdHdS)	a�Find all individual instances of images drawn in the container

    Usually the container is a page, but it may also be a Form XObject.

    On a typical page images are stored inline or as regular images
    in an XObject.

    Form XObjects may include inline images, XObject images,
    and recursively, other Form XObjects; and also vector graphic objects.

    Every instance of an image being drawn somewhere is flattened and
    treated as a unique image, since if the same image is drawn multiple times
    on one page it may be drawn at differing resolutions, and our objective
    is to find the resolution at which the page can be rastered without
    downsampling.

    z/Typez/Pagez	/Contentsr�r�r�z/MatrixN)ror"rZidentityr$rYr/r4r�r�r�)r�r�r$rSrUZform_shorthandZform_matrixr�rrrr��s

r�c
Csft|�t|�}}d}||d||d||||f}dd�}d}|D]}	||	|�rJd}qbqJ|S)z3Smarter text detection that ignores text in marginsg�?rcSs@|d|dko>|d|dko>|d|dko>|d|dkS)z�
        Where (a,b) are 4-tuple rects (left-0, top-1, right-2, bottom-3)
        https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other
        Formula assumes all boxes are in first quadrant
        r�rrr)rrrrr�rects_intersectsz'_page_has_text.<locals>.rects_intersectFT)r )
Ztext_blocksZ
page_widthZpage_heightZpwZphZmargin_ratioZ
interior_bboxr��has_textr1rrr�_page_has_text�s

�
r�ccsL||�D]>}|jd}|jd}|jdk}|��dk}t|j||�VqdS)z|Extract only limited content from text boxes

    We do this to save memory and ensure that our objects are pickleable.
    rru�N)Z_objsZ
rendermodeZget_textr0r1)�minerZtextbox_getter�boxZ
first_lineZ
first_char�visible�corruptrrr�simplify_textboxess


r�)�pagenocCs8i}||d<g|d<|j|}dd�|j��D�}|d|d}|d|d}|dk	rztjt|�|||d	�}	|	|d
<nFt|j�d���	d�}
t
|||
�}tt|t
��|d
<dd�|d
D�}	t|	||�|d<|�dtd��}t|t�s�t|�}||d<||td�|d<||td�|d<zt|d�|d<Wntk
�rTd|d<YnX|dd|ddf}
dd�t|||
d�D�}d|d<tdd�|D���r�d|d<dd�|D�|d<|d�r4ttd d�|dD���}ttd!d�|dD���}|||d"<|d#<tt||d��|d$<tt||d��|d%<|S)&Nr��imagescSsg|]}t|��qSrr)rr]rrrr0sz%_pdf_get_pageinfo.<locals>.<listcomp>r�rrr)�xmltextri�bboxesz/CreatorZPScript5�	textboxescss|]}|jVqdSr5�r1)rr�rrr�	<genexpr>=sz$_pdf_get_pageinfo.<locals>.<genexpr>r�z	/UserUnitr�userunitrZ�width_inches�
height_inchesz/Rotate�rotatecSsg|]}|�qSrr�rZcirrrrNs�r�F�
has_vectorcss|]}t|t�VqdSr5)�
isinstancer4r�rrrr�VsTcSsg|]}t|t�r|�qSr)r�r`)rZimrrrrYs
css|]}|jVqdSr5)r��rrcrrrr�[scss|]}|jVqdSr5)r�r�rrrr�\sr�r��width_pixels�
height_pixels)�pagesZMediaBoxZas_listrZpage_get_textblocksrr<Zdocinforor�r�listr�rr�rr�rl�KeyErrorr��any�max�round)r�r��infiler�Zpageinfo�pageZmediaboxZwidth_ptZ	height_ptr�Z
pscript5_moder�r�Zuserunit_shorthandr�r�r�rrr�_pdf_get_pageinfo*sb
�

��
r�FcCs�t�|�}|jr|��t��|r(d}ntj||d|d�}g}tt|j	�t
|j	�dd|d�D]2\}}|rr||nd}	t||||	|�}
|�|
�q^||fS)N)r��logZScanr�)ZtotalZdescZunit�disable)
rM�openZis_encrypted�closer
rZextract_text_xmlrrLr�rO�PageInforN)r��detailed_analysisr��progbarr�Z	pages_xmlr�rVr^Zpage_xmlr�rrr�_pdf_get_all_pageinfods&
�r�c@s�eZdZd&dd�Zedd��Zedd��Zedd	��Zed
d��Zedd
��Z	edd��Z
edd��Zedd��Zedd��Z
e
jdd��Z
edd��Zd'dd�Zedd��Zedd��Zed d!��Zed"d#��Zd$d%�ZdS)(r�FcCs&||_||_t||||�|_||_dSr5)�_pageno�_infiler��	_pageinfo�_detailed_analysis)r7r�r�r�r�r�rrrr8~szPageInfo.__init__cCs|jSr5)r�r6rrrr��szPageInfo.pagenocCs
|jdS)Nr��r�r6rrrr��szPageInfo.has_textcCs&|jstd��tdd�|jdD��S)NzDid not do detailed analysiscss|]}|jVqdSr5)r3)rZtboxrrrr��sz,PageInfo.has_corrupt_text.<locals>.<genexpr>r�)r�rtr�r�r6rrr�has_corrupt_text�szPageInfo.has_corrupt_textcCs
|jdS)Nr�r�r6rrrr��szPageInfo.has_vectorcCs
|jdS)Nr�r�r6rrrr��szPageInfo.width_inchescCs
|jdS)Nr�r�r6rrrr��szPageInfo.height_inchescCstt|j|j��Sr5)rlr�r�r�r6rrrr��szPageInfo.width_pixelscCstt|j|j��Sr5)rlr�r�r�r6rrrr��szPageInfo.height_pixelscCs|j�dd�S)Nr��r�ror6rrr�rotation�szPageInfo.rotationcCs |dkr||jd<ntd��dS)N)r�Z�iihi����iL���i�r�z!rotation must be a cardinal angle)r��
ValueError)r7�valuerrrr��scCs
|jdS)Nr�r�r6rrrr��szPageInfo.imagesNcsPdd��d|jkr4�dk	r*�dk	r*td��|jdS���fdd�|jdD�S)NcSs4d}|dk	r|j|krd}|dk	r0|j|kr0d}|S)NTF)r2r3)�objZwant_visibleZwant_corrupt�resultrrr�	predicate�s

z)PageInfo.get_textareas.<locals>.predicater�z*Ghostscript textboxes cannot be classifiedr�c3s |]}�|���r|jVqdSr5r�)rr��r�r�r�rrr��s�z)PageInfo.get_textareas.<locals>.<genexpr>)r�rt)r7r�r�rr�r�
get_textareas�s


�zPageInfo.get_textareascCs|j�dd�S)Nr�r�r6rrrr��sz
PageInfo.xrescCs|j�dd�S)Nr�r�r6rrrr��sz
PageInfo.yrescCs|j�dd�S)Nr�r�r6rrrr��szPageInfo.userunitcCs|jdk	rdSdSdS)Nz1.6z1.5�r�r6rrr�min_version�s
zPageInfo.min_versionc	Cs$d�|j|j|j|j|j|j|j�S)Nz><PageInfo pageno={} {}"x{}" rotation={} res={}x{} has_text={}>)r�r�r�r�r�r�r�r�r6rrrr��s��zPageInfo.__repr__)F)NN)r9r:r;r8r�r�r�r�r�r�r�r�r�r��setterr�r�r�r�r�r�r�rrrrr�}sB
















r�c@s�eZdZdZdedfdd�Zedd��Zedd��Zed	d
��Z	edd��Z
ed
d��Zedd��Zdd�Z
dd�Zdd�ZdS)�PdfInfoz#Get summary information about a PDFFcCsz||_t||||d�\|_}|j�dd�|_d|_d|jkrnt|jj�dg��dkr\d|_nd|jjkrnd|_|�	�dS)	N)r�r�z/NeedsRenderingFz	/AcroFormz/FieldsrTz/XFA)
r�r��_pages�rootro�_needs_rendering�
_has_acroformrOZAcroFormr�)r7r�Zdetailed_page_analysisr�r�r�rrrr8�s�
zPdfInfo.__init__cCs|jSr5�r�r6rrrr��sz
PdfInfo.pagescCstdd�|jD��S)Ncss|]}|jVqdSr5)r��rr�rrrr�sz&PdfInfo.min_version.<locals>.<genexpr>)r�r�r6rrrr�szPdfInfo.min_versioncCstdd�|jD��S)Ncss|]}|jdkVqdS)rNr�r�rrrr�sz'PdfInfo.has_userunit.<locals>.<genexpr>)r�r�r6rrr�has_userunitszPdfInfo.has_userunitcCs|jSr5)r�r6rrr�has_acroform
szPdfInfo.has_acroformcCst|jttf�std��|jS)Nzcan't get filename from stream)r�r�r<r	rtr6rrr�filenameszPdfInfo.filenamecCs|jSr5)r�r6rrr�needs_renderingszPdfInfo.needs_renderingcCs
|j|Sr5r�)r7�itemrrr�__getitem__szPdfInfo.__getitem__cCs
t|j�Sr5)rOr�r6rrr�__len__szPdfInfo.__len__cCsdt|��d�S)Nz<PdfInfo('...'), page count=�>)rOr6rrrr�szPdfInfo.__repr__N)r9r:r;�__doc__�loggerr8r�r�r�r�r�r�r�r�r�r�rrrrr��s"





r�cCsDddl}|��}|�d�|��}t|j�}ddlm}||�dS)Nrr�)�pprint)�argparse�ArgumentParser�add_argument�
parse_argsr�r�r�)r��parser�args�infor�rrr�main"s

r��__main__)FNF)NZloggingr=�collectionsrZdecimalr�enumrZmathrr�osr�pathlibr	�warningsr
rMrrZocrmypdf.exceptionsr
rrarZlayoutrrZ	getLoggerr�rrrzZrgbZcmykZlabZicc�index�sepZdevn�patternrrrxZjpegruryZasciihexZascii85ZlzwZflateZ	runlengthrnrvr"r&r'r*r,r0r4rBrYr_r`r�r�r�r�r�r�r�rlr�r�r�r�r�r9rrrr�<module>s�
����	�Q@c	 /:
o6