3
|ўQE                 @   s   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	 dd Z
G dd deZG d	d
 d
eZejeiZG dd deZG dd deZdS )z
    cairocffi.fonts
    ~~~~~~~~~~~~~~~

    Bindings for font-related objects.

    :copyright: Copyright 2013 by Simon Sapin
    :license: BSD, see LICENSE for details.

   )fficairo_check_status	constants)Matrix)xrangec             C   s    t | ts| jd} tjd| S )z2Return a byte string, encoding Unicode with UTF-8.utf8zchar[])
isinstancebytesencoder   new)string r   O/var/www/html/enquirykeeper_venv/lib/python3.6/site-packages/cairocffi/fonts.py_encode_string   s    

r   c               @   s,   e Zd ZdZdd Zdd Zedd ZdS )	FontFacezThe base class for all font face types.

    Should not be instantiated directly, but see :doc:`cffi_api`.
    An instance may be returned for cairo font face types
    that are not (yet) defined in cairocffi.

    c             C   s   t j|tj| _| j  d S )N)r   gcr   Zcairo_font_face_destroy_pointerr   )selfpointerr   r   r   __init__!   s    zFontFace.__init__c             C   s   t tj| j d S )N)r   r   Zcairo_font_face_statusr   )r   r   r   r   r   %   s    zFontFace._check_statusc             C   sH   | t jkrtd|r tj|  tjtjtj	| t
}t
j||  |S )uM  Wrap an existing :c:type:`cairo_font_face_t *` cdata pointer.

        :type incref: bool
        :param incref:
            Whether increase the :ref:`reference count <refcounting>` now.
        :return:
            A new instance of :class:`FontFace` or one of its sub-classes,
            depending on the face’s type.

        zNull pointer)r   NULL
ValueErrorr   Zcairo_font_face_referenceobject__new__FONT_TYPE_TO_CLASSgetZcairo_font_face_get_typer   r   )r   increfr   r   r   r   _from_pointer(   s    

zFontFace._from_pointerN)__name__
__module____qualname____doc__r   r   staticmethodr   r   r   r   r   r      s   r   c               @   s<   e Zd ZdZdejejfddZdd Zdd Z	d	d
 Z
dS )ToyFontFaceu  Creates a font face from a triplet of family, slant, and weight.
    These font faces are used in implementation of cairo’s "toy" font API.

    If family is the zero-length string ``""``,
    the platform-specific default family is assumed.
    The default family then can be queried using :meth:`get_family`.

    The :meth:`Context.select_font_face` method uses this to create font faces.
    See that method for limitations and other details of toy font faces.

    :param family: a font family name, as an Unicode or UTF-8 string.
    :param slant: The :ref:`FONT_SLANT` string for the font face.
    :param weight: The :ref:`FONT_WEIGHT` string for the font face.

     c             C   s   t j| tjt||| d S )N)r   r   r   Zcairo_toy_font_face_creater   )r   familyZslantZweightr   r   r   r   N   s    
zToyFontFace.__init__c             C   s   t jtj| jjddS )u&   Return this font face’s family name.r   replace)r   r   r   Zcairo_toy_font_face_get_familyr   decode)r   r   r   r   
get_familyS   s    zToyFontFace.get_familyc             C   s   t j| jS )u3   Return this font face’s :ref:`FONT_SLANT` string.)r   Zcairo_toy_font_face_get_slantr   )r   r   r   r   	get_slantX   s    zToyFontFace.get_slantc             C   s   t j| jS )u4   Return this font face’s :ref:`FONT_WEIGHT` string.)r   Zcairo_toy_font_face_get_weightr   )r   r   r   r   
get_weight\   s    zToyFontFace.get_weightN)r   r    r!   r"   r   FONT_SLANT_NORMALFONT_WEIGHT_NORMALr   r)   r*   r+   r   r   r   r   r$   >   s   r$   c               @   s~   e Zd ZdZdddZdd Zdd Zed	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )
ScaledFonta  Creates a :class:`ScaledFont` object from a font face and matrices
    that describe the size of the font
    and the environment in which it will be used.

    :param font_face: A :class:`FontFace` object.
    :type font_matrix: Matrix
    :param font_matrix:
        Font space to user space transformation matrix for the font.
        In the simplest case of a N point font,
        this matrix is just a scale by N,
        but it can also be used to shear the font
        or stretch it unequally along the two axes.
        If omitted, a scale by 10 matrix is assumed (ie. a 10 point font size).
        See :class:`Context.set_font_matrix`.
    :type ctm: Matrix
    :param ctm:
        User to device transformation matrix with which the font will be used.
        If omitted, an identity matrix is assumed.
    :param options:
        The :class:`FontOptions` object to use
        when getting metrics for the font and rendering with it.
        If omitted, the default options are assumed.

    Nc             C   sV   |d krt  }|jd |d kr&t  }|d kr4t }| jtj|j|j|j|j d S )N
   )r   ZscaleFontOptions_init_pointerr   Zcairo_scaled_font_creater   )r   Z	font_faceZfont_matrixZctmoptionsr   r   r   r      s    
zScaledFont.__init__c             C   s   t j|tj| _| j  d S )N)r   r   r   Zcairo_scaled_font_destroyr   r   )r   r   r   r   r   r1      s    zScaledFont._init_pointerc             C   s   t tj| j d S )N)r   r   Zcairo_scaled_font_statusr   )r   r   r   r   r      s    zScaledFont._check_statusc             C   s:   | t jkrtd|r tj|  tjt}tj||  |S )zWrap an existing :c:type:`cairo_scaled_font_t *` cdata pointer.

        :type incref: bool
        :param incref:
            Whether increase the :ref:`reference count <refcounting>` now.
        :return: A new :class:`ScaledFont` instance.

        zNull pointer)	r   r   r   r   Zcairo_scaled_font_referencer   r   r.   r1   )r   r   r   r   r   r   r      s    



zScaledFont._from_pointerc             C   s   t jtj| jddS )a&  Return the font face that this scaled font uses.

        :returns:
            A new instance of :class:`FontFace` (or one of its sub-classes).
            Might wrap be the same font face passed to :class:`ScaledFont`,
            but this does not hold true for all possible cases.

        T)r   )r   r   r   Zcairo_scaled_font_get_font_facer   )r   r   r   r   get_font_face   s    	zScaledFont.get_font_facec             C   s   t  }tj| j|j |S )uc   Copies the scaled font’s options.

        :returns: A new :class:`FontOptions` object.

        )r0   r   Z"cairo_scaled_font_get_font_optionsr   )r   Zfont_optionsr   r   r   get_font_options   s    zScaledFont.get_font_optionsc             C   s"   t  }tj| j|j | j  |S )ub   Copies the scaled font’s font matrix.

        :returns: A new :class:`Matrix` object.

        )r   r   Z!cairo_scaled_font_get_font_matrixr   r   )r   matrixr   r   r   get_font_matrix   s    zScaledFont.get_font_matrixc             C   s"   t  }tj| j|j | j  |S )u1  Copies the scaled font’s font current transform matrix.

        Note that the translation offsets ``(x0, y0)`` of the CTM
        are ignored by :class:`ScaledFont`.
        So, the matrix this method returns always has 0 as ``x0`` and ``y0``.

        :returns: A new :class:`Matrix` object.

        )r   r   Zcairo_scaled_font_get_ctmr   r   )r   r5   r   r   r   get_ctm   s    
zScaledFont.get_ctmc             C   s"   t  }tj| j|j | j  |S )u  Copies the scaled font’s scaled matrix.

        The scale matrix is product of the font matrix
        and the ctm associated with the scaled font,
        and hence is the matrix mapping from font space to device space.

        :returns: A new :class:`Matrix` object.

        )r   r   Z"cairo_scaled_font_get_scale_matrixr   r   )r   r5   r   r   r   get_scale_matrix   s
    
zScaledFont.get_scale_matrixc             C   s8   t jd}tj| j| | j  |j|j|j|j	|j
fS )u   Return the scaled font’s extents.
        See :meth:`Context.font_extents`.

        :returns:
            A ``(ascent, descent, height, max_x_advance, max_y_advance)``
            tuple of floats.

        zcairo_font_extents_t *)r   r   r   Zcairo_scaled_font_extentsr   r   ZascentZdescentheightZmax_x_advanceZmax_y_advance)r   extentsr   r   r   r:      s
    	
zScaledFont.extentsc             C   sB   t jd}tj| jt|| | j  |j|j|j	|j
|j|jfS )a  Returns the extents for a string of text.

        The extents describe a user-space rectangle
        that encloses the "inked" portion of the text,
        (as it would be drawn by :meth:`show_text`).
        Additionally, the :obj:`x_advance` and :obj:`y_advance` values
        indicate the amount by which the current point would be advanced
        by :meth:`show_text`.

        :param text: The text to measure, as an Unicode or UTF-8 string.
        :returns:
            A ``(x_bearing, y_bearing, width, height, x_advance, y_advance)``
            tuple of floats.
            See :meth:`Context.text_extents` for details.

        zcairo_text_extents_t *)r   r   r   Zcairo_scaled_font_text_extentsr   r   r   	x_bearing	y_bearingwidthr9   	x_advance	y_advance)r   textr:   r   r   r   text_extents   s    
zScaledFont.text_extentsc             C   sP   t jd|}t jd}tj| j|t|| | j  |j|j|j	|j
|j|jfS )a  Returns the extents for a list of glyphs.

        The extents describe a user-space rectangle
        that encloses the "inked" portion of the glyphs,
        (as it would be drawn by :meth:`show_glyphs`).
        Additionally, the :obj:`x_advance` and :obj:`y_advance` values
        indicate the amount by which the current point would be advanced
        by :meth:`show_glyphs`.

        :param glyphs:
            A list of glyphs, as returned by :meth:`text_to_glyphs`.
            Each glyph is a ``(glyph_id, x, y)`` tuple
            of an integer and two floats.
        :returns:
            A ``(x_bearing, y_bearing, width, height, x_advance, y_advance)``
            tuple of floats.
            See :meth:`Context.text_extents` for details.

        zcairo_glyph_t[]zcairo_text_extents_t *)r   r   r   Zcairo_scaled_font_glyph_extentsr   lenr   r;   r<   r=   r9   r>   r?   )r   glyphsr:   r   r   r   glyph_extents  s    
zScaledFont.glyph_extentsc       	         s   t jdt jt jd}|r@t jdt j t jd}t jd}nt j t j}t j}tj| j||t|d| ||
}t jd tj|rt j d tj	 t
| fddt|d D |r fd	dt|d D   |d fS S d
S )a`  Converts a string of text to a list of glyphs,
        optionally with cluster mapping,
        that can be used to render later using this scaled font.

        The output values can be readily passed to
        :meth:`Context.show_text_glyphs`, :meth:`Context.show_glyphs`
        or related methods,
        assuming that the exact same :class:`ScaledFont`
        is used for the operation.

        :type x: float
        :type y: float
        :type with_clusters: bool
        :param x: X position to place first glyph.
        :param y: Y position to place first glyph.
        :param text: The text to convert, as an Unicode or UTF-8 string.
        :param with_clusters: Whether to compute the cluster mapping.
        :returns:
            A ``(glyphs, clusters, clusters_flags)`` tuple
            if :obj:`with_clusters` is true, otherwise just :obj:`glyphs`.
            See :meth:`Context.show_text_glyphs` for the data structure.

        .. note::

            This method is part of
            what the cairo designers call the "toy" text API.
            It is convenient for short demos and simple programs,
            but it is not expected to be adequate
            for serious text-using applications.
            See :ref:`fonts` for details
            and :meth:`Context.show_glyphs`
            for the "real" text display API in cairo.

        zcairo_glyph_t **zint *zcairo_text_cluster_t **zcairo_text_cluster_flags_t *r       c                s,   g | ]$} | gD ]}|j |j|jfqqS r   )indexxy).0iZglyph)rC   r   r   
<listcomp>c  s   z-ScaledFont.text_to_glyphs.<locals>.<listcomp>c                s(   g | ] } | gD ]}|j |jfqqS r   )Z	num_bytes
num_glyphs)rI   rJ   Zcluster)clustersr   r   rK   h  s   N)r   r   r   r   Z cairo_scaled_font_text_to_glyphsr   r   r   Zcairo_glyph_freeZcairo_text_cluster_freer   r   )	r   rG   rH   r@   Zwith_clustersrL   Znum_clustersZcluster_flagsstatusr   )rM   rC   r   text_to_glyphs-  s.    #



zScaledFont.text_to_glyphs)NNN)r   r    r!   r"   r   r1   r   r#   r   r3   r4   r6   r7   r8   r:   rA   rD   rP   r   r   r   r   r.   f   s   
r.   c               @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Ze
Ze	Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"S )#r0   a`  An opaque object holding all options that are used when rendering fonts.

    Individual features of a :class:`FontOptions`
    can be set or accessed using method
    named :meth:`set_FEATURE_NAME` and :meth:`get_FEATURE_NAME`,
    like :meth:`set_antialias` and :meth:`get_antialias`.

    New features may be added to :class:`FontOptions` in the future.
    For this reason, ``==``, :meth:`copy`, :meth:`merge`, and :func:`hash`
    should be used to check for equality copy,, merge,
    or compute a hash value of :class:`FontOptions` objects.

    :param values:
        Call the corresponding :meth:`set_FEATURE_NAME` methods
        after creating a new :class:`FontOptions`::

            options = FontOptions()
            options.set_antialias(cairocffi.ANTIALIAS_BEST)
            assert FontOptions(antialias=cairocffi.ANTIALIAS_BEST) == options

    c             K   s:   | j tj  x&|j D ]\}}t| d| | qW d S )Nset_)r1   r   Zcairo_font_options_createitemsgetattr)r   valuesnamevaluer   r   r   r     s    zFontOptions.__init__c             C   s   t j|tj| _| j  d S )N)r   r   r   Zcairo_font_options_destroyr   r   )r   r   r   r   r   r1     s    zFontOptions._init_pointerc             C   s   t tj| j d S )N)r   r   cairo_font_options_statusr   )r   r   r   r   r     s    zFontOptions._check_statusc             C   s*   t | }tj|}|j|tj| j |S )z7Return a new :class:`FontOptions` with the same values.)typer   r   r1   r   Zcairo_font_options_copyr   )r   clsotherr   r   r   copy  s    
zFontOptions.copyc             C   s$   t j| j|j tt j| j dS )zMerges non-default options from :obj:`other`,
        replacing existing values.
        This operation can be thought of as somewhat similar
        to compositing other onto options
        with the operation of :obj:`OVER <OPERATOR_OVER>`.

        N)r   Zcairo_font_options_merger   r   rW   )r   rZ   r   r   r   merge  s    zFontOptions.mergec             C   s   t j| jS )N)r   Zcairo_font_options_hashr   )r   r   r   r   __hash__  s    zFontOptions.__hash__c             C   s   t j| j|jS )N)r   Zcairo_font_options_equalr   )r   rZ   r   r   r   __eq__  s    zFontOptions.__eq__c             C   s
   | |k S )Nr   )r   rZ   r   r   r   __ne__  s    zFontOptions.__ne__c             C   s   t j| j| | j  dS )zChanges the :ref:`ANTIALIAS` for the font options object.
        This specifies the type of antialiasing to do when rendering text.

        N)r   Z cairo_font_options_set_antialiasr   r   )r   Z	antialiasr   r   r   set_antialias  s    zFontOptions.set_antialiasc             C   s   t j| jS )z?Return the :ref:`ANTIALIAS` string for the font options object.)r   Z cairo_font_options_get_antialiasr   )r   r   r   r   get_antialias  s    zFontOptions.get_antialiasc             C   s   t j| j| | j  dS )a  Changes the :ref:`SUBPIXEL_ORDER` for the font options object.
         The subpixel order specifies the order of color elements
         within each pixel on the display device
         when rendering with an antialiasing mode of
         :obj:`SUBPIXEL <ANTIALIAS_SUBPIXEL>`.

        N)r   Z%cairo_font_options_set_subpixel_orderr   r   )r   Zsubpixel_orderr   r   r   set_subpixel_order  s    
zFontOptions.set_subpixel_orderc             C   s   t j| jS )zVReturn the :ref:`SUBPIXEL_ORDER` string
        for the font options object.

        )r   Z%cairo_font_options_get_subpixel_orderr   )r   r   r   r   get_subpixel_order  s    zFontOptions.get_subpixel_orderc             C   s   t j| j| | j  dS )zChanges the :ref:`HINT_STYLE` for the font options object.
        This controls whether to fit font outlines to the pixel grid,
        and if so, whether to optimize for fidelity or contrast.

        N)r   Z!cairo_font_options_set_hint_styler   r   )r   Z
hint_styler   r   r   set_hint_style  s    zFontOptions.set_hint_stylec             C   s   t j| jS )z@Return the :ref:`HINT_STYLE` string for the font options object.)r   Z!cairo_font_options_get_hint_styler   )r   r   r   r   get_hint_style  s    zFontOptions.get_hint_stylec             C   s   t j| j| | j  dS )zChanges the :ref:`HINT_METRICS` for the font options object.
        This controls whether metrics are quantized
        to integer values in device units.

        N)r   Z#cairo_font_options_set_hint_metricsr   r   )r   Zhint_metricsr   r   r   set_hint_metrics  s    zFontOptions.set_hint_metricsc             C   s   t j| jS )zTReturn the :ref:`HINT_METRICS` string
        for the font options object.

        )r   Z#cairo_font_options_get_hint_metricsr   )r   r   r   r   get_hint_metrics  s    zFontOptions.get_hint_metricsN)r   r    r!   r"   r   r1   r   r[   r\   r]   r^   r_   equalhashr`   ra   rb   rc   rd   re   rf   rg   r   r   r   r   r0   p  s&   		r0   N)r"   r%   r   r   r   r   r5   r   compatr   r   r   r   r$   FONT_TYPE_TOYr   r.   r0   r   r   r   r   <module>   s   %$
  