a
    èÝEbÛ  ã                   @   sl   d Z ddlZddlZddlZe e¡Zg d¢Zdd„ Zdd„ Z	e	fdd	„Z
e	fd
d„Zeeedœdd„ZdS )z#
Various round-to-integer helpers.
é    N)ÚnoRoundÚotRoundÚ
maybeRoundÚ	roundFuncc                 C   s   | S ©N© ©Úvaluer   r   úY/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/fontTools/misc/roundTools.pyr      s    r   c                 C   s   t t | d ¡ƒS )aÝ  Round float value to nearest integer towards ``+Infinity``.

	The OpenType spec (in the section on `"normalization" of OpenType Font Variations <https://docs.microsoft.com/en-us/typography/opentype/spec/otvaroverview#coordinate-scales-and-normalization>`_)
	defines the required method for converting floating point values to
	fixed-point. In particular it specifies the following rounding strategy:

		for fractional values of 0.5 and higher, take the next higher integer;
		for other fractional values, truncate.

	This function rounds the floating-point value according to this strategy
	in preparation for conversion to fixed-point.

	Args:
		value (float): The input floating-point value.

	Returns
		float: The rounded value.
	ç      à?)ÚintÚmathÚfloorr   r   r   r
   r      s    r   c                 C   s    || ƒ}t ||  ƒ|kr|S | S r   )Úabs)ÚvÚ	toleranceÚroundZroundedr   r   r
   r   ,   s    r   c                 C   s8   | dk rt dƒ‚| dkrtS | dkr(|S tjt| |d�S )Nr   z#Rounding tolerance must be positiver   ©r   r   )Ú
ValueErrorr   Ú	functoolsÚpartialr   r   r   r   r
   r   0   s    r   )r	   ÚfactorÚreturnc                 C   sÐ   | sdS t | | ƒ| } d| }| | }| | }t|ƒt|ƒkrPttt| ƒƒƒS d}|| }|| }t|ƒt|ƒkr|||ks€J ‚tt|ƒƒD ]}|| || krŒ q¦qŒ| d¡}||k s¼J ‚d||  }||  S )a"  Round to nearest multiple of factor and return shortest decimal representation.

    This chooses the float that is closer to a multiple of the given factor while
    having the shortest decimal representation (the least number of fractional decimal
    digits).

    For example, given the following:

    >>> nearestMultipleShortestRepr(-0.61883544921875, 1.0/(1<<14))
    '-0.61884'

    Useful when you need to serialize or print a fixed-point number (or multiples
    thereof, such as F2Dot14 fractions of 180 degrees in COLRv1 PaintRotate) in
    a human-readable form.

    Args:
        value (value): The value to be rounded and serialized.
        factor (float): The value which the result is a close multiple of.

    Returns:
        str: A compact string representation of the value.
    z0.0r   z%.8fÚ.z%%.%df)r   r   ÚstrÚfloatr   ÚlenÚrangeÚfind)r	   r   ZepsÚloÚhiÚfmtÚiZperiodr   r   r
   ÚnearestMultipleShortestRepr=   s&    
r#   )Ú__doc__r   r   ÚloggingÚ	getLoggerÚ__name__ÚlogÚ__all__r   r   r   r   r   r   r#   r   r   r   r
   Ú<module>   s   
