Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/graphics/units.py
blob: 0e415e235236fe4d166e6f841b03f2c1ddb69a88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright (C) 2006, Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

""" Units conversions and constants

The purpose of the module is to keep Sugar independent from the
screen size, factory and DPI. There a few use cases that needs
to be considered:

  - The XO display. The screen DPI is 201 and the screen
    resolution is 1200x900. The screen factor is 4:3.
  - The Sugar emulator runned on traditional screens. Resolution
    is variable, ranging from 800x600 up to 1200x900. The DPI
    is usually but not necessarily 96. The screen factor is
    either 4:3 or 16:9
  - Other embedded devices. DPI, screen resolution and screen
    factor are variable.

To achieve the goal a few rules needs to be respected when
writing code for Sugar:

  - Never use absolute positioning. Use the layout facilities
    provided by HippoCanvas. If you need custom layouts make
    sure they adapt to different screen resolutions.
  - Never specify sizes, fonts, borders or padding using pixels.
    Instead use the device independt units provided by this
    module.

We are currently providing the following resolution independent
units:

  - Points.
  - Grid. One cell of the screen grid as specificed by the HIG.
  - Microgrid. One microcell of the screen grid as
    specificed by the HIG.
  - A set of icon sizes as specified by the HIG (standard, small,
    medium, large, xlarge).

Just scaling UI elements on the base of the screen DPI is not
enough to provide a good experience. For example on smaller
screens smaller fonts or icons might be acceptable to gain
screen aestate. For this reason a constant zoom factor is
applied to all the transformation from resolution independent
units to device units.

"""

import gtk

_MAX_ZOOM_FACTOR = 2.0
_ZOOM_CONSTANT   = 650.0

def _compute_zoom_factor():
    screen_width = gtk.gdk.screen_width()
    if _screen_dpi == 201.0 and screen_width == 1200:
        return 1.0
    else:
        return min(_MAX_ZOOM_FACTOR, screen_width / _ZOOM_CONSTANT)

_gtk_xft_dpi = gtk.settings_get_default().get_property('gtk-xft-dpi')
_screen_dpi = float(_gtk_xft_dpi / 1024)
_dpi_factor  = _screen_dpi / 201.0
_zoom_factor = _compute_zoom_factor()

STANDARD_ICON_SCALE = 1.0 * _dpi_factor * _zoom_factor
SMALL_ICON_SCALE    = 0.5 * _dpi_factor * _zoom_factor
MEDIUM_ICON_SCALE   = 1.5 * _dpi_factor * _zoom_factor
LARGE_ICON_SCALE    = 2.0 * _dpi_factor * _zoom_factor
XLARGE_ICON_SCALE   = 2.75 * _dpi_factor * _zoom_factor

def points_to_device(points):
    return int(points * _zoom_factor)

def points_to_pixels(points):
    return int(points * _screen_dpi / 72.0 * _zoom_factor)

def grid_to_pixels(units):
    return int(units * 75.0 * _dpi_factor * _zoom_factor)

def microgrid_to_pixels(units):
    return int(units * 15.0 * _dpi_factor * _zoom_factor)