o
    @@i+                     @   sJ   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Z	G dd dZ
dS )z
This object represents the base class for everything related to IO in borb.
It has some convenience methods that allow you to specify how the object
should be persisted, as well as some methods to traverse the object-graph.
    N)Imagec                   @   s   e Zd ZdZdd Zei fdejfddZedejdejfdd	Z	dej
d  fd
dZdej
d fddZdej
d  fddZdefddZdefddZdedd fddZdedd fddZd$ddZd%dd Zdejfd!d"Zd#S )&	PDFObjectz
    This object represents the base class for everything related to IO in borb.
    It has some convenience methods that allow you to specify how the object
    should be persisted, as well as some methods to traverse the object-graph.
    c                 C   s   d| _ d| _d | _d | _d S )NF)
_is_inline
_is_unique_parent
_referenceself r
   U/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/borb/io/read/pdf_object.py__init__   s   
zPDFObject.__init__returnc                 C   s  t | tr| S t | tjjjjrt| S t | tjjjjr!t| S t | tjjjj	r.t
| S t | tj	r8t
| S t | t
sBt | trD| S t | trMt| S t | tjjjjrsi }||t| < |  D ]\}}t|||t|< qb|S t | tri }||t| < |  D ]\}}t|||t|< q|S t | tjjjjrdd l}t|jj| S t | tjjjjrt| S t | tjjjjrt| S t | tjjjjrg }||t| < | D ]}|t|| q|S t | tjjjjrd| jpd| jpdf S d S )Nr   z%d %d R) 
isinstanceboolborbioreadtypesBooleanCanvasOperatorNamestrDecimalfloatdecimalintbytes
Dictionaryiditemsr   _to_jsondictElementxml.etree.ElementTreeetreeElementTreetostringNameStringListappend	Referencegeneration_numberobject_number)r	   	memo_dictoutkvdict_outxmllist_outr
   r
   r   r   '   sT   


	zPDFObject._to_jsonnon_borb_objectc                 C   sX  i fdd}dt jt fdd}dt jd fdd}dtfd	d
}dtfdd}dtfdd}dd }dtdtfdd}dtdtfdd}	dtdtfdd}
dddtfdd}t|
| | _t|| | _t|| | _t|| | _	t|| | _
t|| | _t|| | _t|	| | _t|| | _t|| | _t| tjrt|| | _| S )as  
        This method allows you to pretend an object is actually a PDFObject.
        It adds all the methods that are present for a PDFObject.
        It also adds a utility hashing method for images (since PIL normally does not hash images)
        :param non_borb_object: a (non-borb) object
        :return:                an object that plays nice with borb
        c                 S   s,   | j }d | _ t| |}|| _ t| |S N)__deepcopy__copydeepcopyr   add_pdf_object_methods)r	   memodictprev_function_ptrr.   r
   r
   r   _deepcopy_and_add_methods   s   
zCPDFObject.add_pdf_object_methods.<locals>._deepcopy_and_add_methodsr   c                 S      dt | vrt| dd  | jS Nr   varssetattrr   r   r
   r
   r   _get_parent      z5PDFObject.add_pdf_object_methods.<locals>._get_parentr*   c                 S   r=   Nr   r@   rA   r   r   r
   r
   r   _get_reference   rC   z8PDFObject.add_pdf_object_methods.<locals>._get_referencec                 S   s(   | }|  d ur|  }|  d us|S r5   
get_parentr	   pr
   r
   r   	_get_root   s
   z3PDFObject.add_pdf_object_methods.<locals>._get_rootc                 S      dt | vrt| dd | jS Nr   Fr@   rA   r   r   r
   r
   r   r      rC   z4PDFObject.add_pdf_object_methods.<locals>._is_inlinec                 S   rL   Nr   Fr@   rA   r   r   r
   r
   r   r      rC   z4PDFObject.add_pdf_object_methods.<locals>._is_uniquec                 S   s   | j }| j}| d| d|d f| |d df| |d |d fg}d}|D ]!}t|tjs8t|tjrC|d| t| 7 }q*|d| | 7 }q*|S )N)r   r   r          )widthheightgetpixelr   typingr(   Tuplesum)r	   whpixelshashcoderJ   r
   r
   r   _pil_image_hash   s   z9PDFObject.add_pdf_object_methods.<locals>._pil_image_hash	is_inlinec                 S   "   dt | vrt| dd || _| S rM   rN   r	   r^   r
   r
   r   _set_is_inline      z8PDFObject.add_pdf_object_methods.<locals>._set_is_inline	is_uniquec                 S   r_   rO   rP   r	   rc   r
   r
   r   _set_is_unique   rb   z8PDFObject.add_pdf_object_methods.<locals>._set_is_uniqueparentc                 S   "   dt | vrt| dd  || _| S r>   r?   r	   rf   r
   r
   r   _set_parent   rb   z5PDFObject.add_pdf_object_methods.<locals>._set_parent	referencec                 S   rg   rD   rE   r	   rj   r
   r
   r   _set_reference   rb   z8PDFObject.add_pdf_object_methods.<locals>._set_reference)rV   Optionalr   r   r   
MethodType
set_parentrH   get_rootset_referenceget_referenceset_is_inliner^   set_is_uniquerc   r6   r   PILImageModuler   __hash__)r4   r<   rB   rF   rK   r   r   r]   ra   re   ri   rl   r
   r
   r   r9      sD   
z PDFObject.add_pdf_object_methodsc                 C      | j S )z
        This function returns the parent of this PDFObject, or None if no such PDFObject exists
        :return:    the parent of this PDFObject
        r   r   r
   r
   r   rH         zPDFObject.get_parentr*   c                 C   rw   )z
        This function gets the Reference being used for this PDFObject
        :return:    the Reference being used for this PDFObject
        r   r   r
   r
   r   rr      ry   zPDFObject.get_referencec                 C   s8   | }|dur|  dur|  }|dur|  dus|S )z
        This function returns the root (of the parent hierarchy) of this PDFObject, or None if no such PDFObject exists
        :return:    the root of this PDFObject
        NrG   rI   r
   r
   r   rp     s
   zPDFObject.get_rootc                 C   rw   )z
        This function returns True if this PDFObject should be persisted inline, False otherwise
        :return:    whether this PDFObject should be persisted online
        r   r   r
   r
   r   r^     ry   zPDFObject.is_inlinec                 C   rw   )z
        This function returns True if this PDFObject should always be treated
        as if it is unique (for IO purposes), regardless of hashing equality.
        :return:    whether this PDFObject is unique
        r   r   r
   r
   r   rc     s   zPDFObject.is_uniquer^   c                 C   
   || _ | S )a,  
        This function sets the is_inline flag of this PDFObject.
        An inline object is always persisted immediately when needed, it is never turned into a reference.
        :param is_inline:   whether this PDFObject should be persisted inline, or not
        :return:            self
        r{   r`   r
   r
   r   rs     s   zPDFObject.set_is_inlinerc   c                 C   r}   )a;  
        This function sets the is_unique flag of this PDFObject.
        A unique object is always persisted as itself,
        or its own reference even if it should be equal to another PDFObject.
        :param is_unique:   whether this PDFObject should be unique or not
        :return:            self
        r|   rd   r
   r
   r   rt   %  s   zPDFObject.set_is_uniquerf   c                 C   r}   )z
        This function sets the parent (PDFObject) of this PDFObject
        :param parent:  the parent (PDFObject)
        :return:        self
        rx   rh   r
   r
   r   ro   0     zPDFObject.set_parentrj   c                 C   r}   )z
        This function sets the Reference to be used for this PDFObject
        :param reference:   the Reference to be used for this PDFObject
        :return:            self
        rz   rk   r
   r
   r   rq   9  r~   zPDFObject.set_referencec                 C   s
   t | S )z
        This function converts this PDFObject into a set of nested dictionaries, lists and primitives
        :return:    a JSON-like object
        )r   r   r   r
   r
   r   to_jsonB  s   
zPDFObject.to_jsonN)rf   r   r   r   )rj   r*   r   r   )__name__
__module____qualname____doc__r   staticmethodrV   Anyr   r9   rm   rH   rr   rp   r   r^   rc   rs   rt   ro   rq   r   r
   r
   r
   r   r      s"    

Zq



		r   )r   r7   r   rV   r   PILr   ru   borb.io.read.typesr   r   r
   r
   r
   r   <module>   s   