1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """
22 This module contains simple functions needed by all other modules.
23
24 Author: Sven Festersen (sven@sven-festersen.de)
25 """
26 __docformat__ = "epytext"
27 import cairo
28 import os
29
30 REF_BOTTOM_LEFT = 0
31 REF_TOP_LEFT = 1
32 REF_TOP_RIGHT = 2
33 REF_BOTTOM_RIGHT = 4
34
35
37 """
38 Use this method to test whether M{xmin <= x <= xmax}.
39
40 @type x: number
41 @type xmin: number
42 @type xmax: number
43 """
44 return (xmin <= x and xmax >= x)
45
47 min_a, max_a = range_a
48 min_b, max_b = range_b
49 return max(min_a, min_b), min(max_a, max_b)
50
52 """
53 Find the center point of a rectangle.
54
55 @type rect: gtk.gdk.Rectangle
56 @param rect: The rectangle.
57 @return: A (x, y) tuple specifying the center point.
58 """
59 return rect.width / 2, rect.height / 2
60
62 """
63 Convert a 8 bit RGB value to cairo color.
64
65 @type color: a triple of integers between 0 and 255
66 @param color: The color to convert.
67 @return: A color in cairo format.
68 """
69 return (color[0] / 255.0, color[1] / 255.0, color[2] / 255.0)
70
72 """
73 Convert a html (hex) RGB value to cairo color.
74
75 @type color: html color string
76 @param color: The color to convert.
77 @return: A color in cairo format.
78 """
79 if color[0] == '#':
80 color = color[1:]
81 (r, g, b) = (int(color[:2], 16),
82 int(color[2:4], 16),
83 int(color[4:], 16))
84 return color_rgb_to_cairo((r, g, b))
85
87 """
88 Read a file with one html hex color per line and return a list
89 of cairo colors.
90 """
91 result = []
92 if os.path.exists(filename):
93 f = open(filename, "r")
94 for line in f.readlines():
95 line = line.strip()
96 result.append(color_html_to_cairo(line))
97 return result
98
99 -def show_text(context, rect, x, y, text, font, size, slant=cairo.FONT_SLANT_NORMAL, weight=cairo.FONT_WEIGHT_NORMAL, underline=False, reference_point=REF_BOTTOM_LEFT):
100 context.set_font_size(max(size, 8))
101 context.select_font_face(font, slant, weight)
102
103 text_dimensions = context.text_extents(text)
104 text_width = text_dimensions[2]
105 text_height = text_dimensions[3]
106
107 ref = (0, 0)
108 if reference_point == REF_TOP_LEFT:
109 ref = (0, text_height)
110 elif reference_point == REF_TOP_RIGHT:
111 ref = (-text_width, text_height)
112 elif reference_point == REF_BOTTOM_RIGHT:
113 ref = (-text_width, 0)
114 x = x + ref[0]
115 y = y + ref[1]
116
117 context.move_to(x, y)
118 context.show_text(text)
119