U
    Pe6                     @   sL  U d 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mZmZmZ d	Zd
Zd
Zd
Zedd
dkrdZdZnBedZereeZedZereeZedZereeZedddZedddZedddZedddZedddZedddZ G dd  d eZ!ed!e!Z"e!e#d!< d
S )"a  
Metrics
=======

.. versionadded:: 1.5.0

A screen is defined by its physical size, density and resolution. These
factors are essential for creating UI's with correct size everywhere.

In Kivy, all the graphics pipelines work with pixels. But using pixels as a
measurement unit is problematic because sizes change according to the
screen.

Dimensions
----------

If you want to design your UI for different screen sizes, you will want better
measurement units to work with. Kivy provides some more scalable alternatives.

:Units:
    `pt`
        Points - 1/72 of an inch based on the physical size of the screen.
        Prefer to use sp instead of pt.
    `mm`
        Millimeters - Based on the physical size of the screen.
    `cm`
        Centimeters - Based on the physical size of the screen.
    `in`
        Inches - Based on the physical size of the screen.
    `dp`
        Density-independent Pixels - An abstract unit that is based on the
        physical density of the screen. With a :attr:`~MetricsBase.density` of
        1, 1dp is equal to 1px. When running on a higher density screen, the
        number of pixels used to draw 1dp is scaled up a factor appropriate to
        the screen's dpi, and the inverse for a lower dpi.
        The ratio of dp-to-pixels will change with the screen density, but not
        necessarily in direct proportion. Using the dp unit is a simple
        solution to making the view dimensions in your layout resize
        properly for different screen densities. In others words, it
        provides consistency for the real-world size of your UI across
        different devices.
    `sp`
        Scale-independent Pixels - This is like the dp unit, but it is also
        scaled by the user's font size preference. We recommend you use this
        unit when specifying font sizes, so the font size will be adjusted to
        both the screen density and the user's preference.

Examples
--------

Here is an example of creating a label with a sp font_size and setting the
height manually with a 10dp margin::

    #:kivy 1.5.0
    <MyWidget>:
        Label:
            text: 'Hello world'
            font_size: '15sp'
            size_hint_y: None
            height: self.texture_size[1] + dp(10)

Manual control of metrics
-------------------------

The metrics cannot be changed at runtime. Once a value has been converted to
pixels, you can't retrieve the original value anymore. This stems from the fact
that the DPI and density of a device cannot be changed at runtime.

We provide some environment variables to control metrics:

- `KIVY_METRICS_DENSITY`: if set, this value will be used for
  :attr:`~MetricsBase.density` instead of the systems one. On android,
  the value varies between 0.75, 1, 1.5 and 2.

- `KIVY_METRICS_FONTSCALE`: if set, this value will be used for
  :attr:`~MetricsBase.fontscale` instead of the systems one. On android, the
  value varies between 0.8 and 1.2.

- `KIVY_DPI`: if set, this value will be used for :attr:`~MetricsBase.dpi`.
  Please
  note that setting the DPI will not impact the dp/sp notation because these
  are based on the screen density.

For example, if you want to simulate a high-density screen (like the HTC One
X)::

    KIVY_DPI=320 KIVY_METRICS_DENSITY=2 python main.py --size 1280x720

Or a medium-density (like Motorola Droid 2)::

    KIVY_DPI=240 KIVY_METRICS_DENSITY=1.5 python main.py --size 854x480

You can also simulate an alternative user preference for fontscale as follows::

    KIVY_METRICS_FONTSCALE=1.2 python main.py

    )environ)platform)AliasProperty)EventDispatcher)USE_SDL2)register_context)dpi2pxNUMERIC_FORMATSdispatch_pixel_scalesync_pixel_scale)
MetricsMetricsBaseptinchcmmmdpspr   r	   NZKIVY_DOC_INCLUDE1g     `@   ZKIVY_DPIZKIVY_METRICS_DENSITYZKIVY_METRICS_FONTSCALE)returnc                 C   s
   t | dS )z"Convert from points to pixels
    r   r   value r   0/tmp/pip-unpacked-wheel-xzebddm3/kivy/metrics.pyr      s    r   c                 C   s
   t | dS )z"Convert from inches to pixels
    inr   r   r   r   r   r      s    r   c                 C   s
   t | dS )z'Convert from centimeters to pixels
    r   r   r   r   r   r   r      s    r   c                 C   s
   t | dS )z'Convert from millimeters to pixels
    r   r   r   r   r   r   r      s    r   c                 C   s
   t | dS )z6Convert from density-independent pixels to pixels
    r   r   r   r   r   r   r      s    r   c                 C   s
   t | dS )z4Convert from scale-independent pixels to pixels
    r   r   r   r   r   r   r      s    r   c                       s  e Zd ZU dZeZeZeZ	 fddZ
d5ddZdd Zeeed	d
Zeed< dd Zeeddd	dZeed< d6ddZdd Zeeedd	dZeed< d7ddZdd Zeeed	d
Zeed< dd Zeeddd	dZeed< d d! Zeeddd	dZeed"< d#d$ Zeeddd	dZeed%< d&d' Z ee ddd	dZ!eed(< d)d* Z"ee"ddd	dZ#eed+< d,d- Z$ee$ddd	dZ%eed.< d/d0 Z&d1d2 Z'd3d4 Z(  Z)S )8r   zClass that contains the default attributes for Metrics. Don't use this
    class directly, but use the `Metrics` instance.
    c                    s6   t  jf | | dt | dt | dt d S )Ndpidensity	fontscale)super__init__Zfbindr
   )selfkwargs	__class__r   r   r!      s    zMetricsBase.__init__Fc                 C   s   |s| j d k	r| j S tdkrNtr<dd l}|d}| }qdd l}| }n6tdkrhdd l}| }nddl	m
} |  |jj}t|d |S )Nandroidr   org.renpy.android.Hardwareios)	EventLoopr   )_dpir   r   jnius	autoclassZgetDPIr&   get_dpir(   Z	kivy.baser)   Zensure_windowZwindowr   r   )r"   force_recomputer,   Hardwarer   r&   r(   r)   r   r   r   r.      s"    




zMetricsBase.get_dpic                 C   s   || _ t|d dS )Nr*   T)r+   r   r"   r   r   r   r   set_dpi   s    
zMetricsBase.set_dpiT)cacher   c                 C   s.   | j }|dk rdS |dk rdS |dk r*dS dS )N   x         i     i@  r*   )r"   r   r   r   r   get_dpi_rounded   s    zMetricsBase.get_dpi_roundedNr*   )bindr3   dpi_roundedc                 C   sv   |s| j d k	r| j S d}tdkr<dd l}|d}|jj}n,tdkrVdd l}| }ntdkrh| jd }t	|d |S )	N      ?r&   r   r'   r(   )macosxwing      X@r   )
_densityr   r,   r-   ZmetricsZscaledDensityr(   Z	get_scaler   r   )r"   r/   r   r,   r0   r(   r   r   r   get_density   s    




zMetricsBase.get_densityc                 C   s   || _ t|d dS )Nr?   T)r@   r   r1   r   r   r   set_density  s    
zMetricsBase.set_densityr   c                 C   sd   |s| j d k	r| j S d}tdkrVddlm} tr:|d}n|d}|j  }|j}t	|d |S )Nr<   r&   r   )r-   zorg.kivy.android.PythonActivityz org.renpy.android.PythonActivityr   )

_fontscaler   r,   r-   r   Z	mActivityZgetResourcesZgetConfigurationZ	fontScaler   )r"   r/   r   r-   ZPythonActivityconfigr   r   r   get_fontscale  s    

zMetricsBase.get_fontscalec                 C   s   || _ t|d dS )NrC   T)rD   r   r1   r   r   r   set_fontscale.  s    
zMetricsBase.set_fontscaler   c                 C   s
   t ddS )Nr   r   r   r"   r   r   r   get_in<  s    zMetricsBase.get_in)r   r   r   r   c                 C   s
   t ddS )Nr   r   r   rH   r   r   r   get_dpL  s    zMetricsBase.get_dpr   c                 C   s
   t ddS )Nr   r   r   rH   r   r   r   get_spY  s    zMetricsBase.get_spr   c                 C   s
   t ddS )Nr   r   r   rH   r   r   r   get_ptf  s    zMetricsBase.get_ptr   c                 C   s
   t ddS )Nr   r   r   rH   r   r   r   get_cmr  s    zMetricsBase.get_cmr   c                 C   s
   t ddS )Nr   r   r   rH   r   r   r   get_mm~  s    zMetricsBase.get_mmr   c                 C   s.   | j dd| _| jdd| _| jdd| _dS )znResets the dpi/density/fontscale to the platform values, overwriting
        any manually set values.
        Tr/   N)r.   r   rA   r   rF   r   rH   r   r   r   reset_metrics  s    zMetricsBase.reset_metricsc                 G   s   | j dd| _dS )zsResets the dpi (and possibly density) to the platform values,
        overwriting any manually set values.
        TrO   N)r.   r   )r"   argsr   r   r   	reset_dpi  s    zMetricsBase.reset_dpic                 C   s
   t   d S )N)r
   rH   r   r   r   _set_cached_scaling  s    zMetricsBase._set_cached_scaling)F)F)F)*__name__
__module____qualname____doc___default_dpir+   _default_densityr@   _default_fontscalerD   r!   r.   r2   r   r   float__annotations__r9   r;   intrA   rB   r   rF   rG   r   rI   r   rJ   r   rK   r   rL   r   rM   r   rN   r   rP   rR   rS   __classcell__r   r   r$   r   r      s   


   	
   
	   	   
   
   	   	   	r   r   )$rW   osr   Z
kivy.utilsr   Zkivy.propertiesr   Z
kivy.eventr   Zkivy.setupconfigr   Zkivy.contextr   Zkivy._metricsr   r	   r
   r   __all__rX   rY   rZ   getZ_custom_dpir[   Z_custom_densityZ_custom_fontscaler   r   r   r   r   r   r   r   r\   r   r   r   r   <module>   sB   c


 r