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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# apt install libcairo2-dev pkg-config python3-dev
# pip install pycairo
import cairo
import math
import sys
from enum import IntEnum
from ttf import create_cairo_font_face_for_file
WIDTH = 350
HEIGHT = 430
def deg2rad(degrees):
return degrees * (math.pi / 180)
class TextAlignment(IntEnum):
LEFT = 1
CENTER = 2
RIGHT = 3
def draw_text(ctx, text, pos, fsize, noise, theta = 0.0, align = TextAlignment.CENTER):
"""Dessine un texte en respectant des propriétées."""
ctx.save()
ctx.set_font_size(fsize)
fascent, fdescent, fheight, fxadvance, fyadvance = ctx.font_extents()
x_off, y_off, tw, th = ctx.text_extents(text)[:4]
if align == TextAlignment.LEFT:
nx = 0
elif align == TextAlignment.RIGHT:
nx = -tw
else:
nx = -tw / 2.0
ny = fheight / 2
ctx.translate(pos[0], pos[1])
ctx.rotate(theta)
ctx.translate(nx, ny)
ctx.move_to(0,0)
ctx.text_path(text)
ctx.set_source_surface(noise, 0, -fheight)
ctx.fill_preserve()
ctx.set_source_rgb(1.0, 1.0, 1.0)
ctx.set_line_width(1)
ctx.stroke()
ctx.restore()
if __name__ == '__main__':
"""Point d'entrée du script."""
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
cr = cairo.Context(surface)
face = create_cairo_font_face_for_file('cour10p_b.ttf')
cr.set_font_face(face)
noise = cairo.ImageSurface.create_from_png('noise.png')
# Type d'édition
if len(sys.argv) > 2:
draw_text(cr, sys.argv[2], [ 300, 25 ], 32, noise,
theta = deg2rad(270), align = TextAlignment.RIGHT)
cr.set_source_rgb(0, 0, 0)
cr.paint_with_alpha(0.4)
# Numéro de version
draw_text(cr, sys.argv[1], [ 149, 10 + 259 + 14 + 42 + 2 ], 23, noise, align=TextAlignment.LEFT)
cr.set_source_rgb(0, 0, 0)
cr.paint_with_alpha(0.5)
# Logo
logo = cairo.ImageSurface.create_from_png('logo.png')
scale_y = 259 / logo.get_height()
cr.save()
cr.translate((WIDTH - (logo.get_width() * scale_y)) / 2, 12)
cr.scale(scale_y, scale_y)
cr.set_source_surface(logo, 0, 0)
cr.paint()
cr.restore()
# Titre
draw_text(cr, 'Chrysalide', [ WIDTH / 2, 10 + 259 + 14 + 4 ], 42, noise)
# cr.rectangle(265, 30, 10, 10)
# cr.set_source_rgb(1, 0, 0)
# cr.fill()
# cr.rectangle(265, 255, 10, 10)
# cr.set_source_rgb(1, 0, 0)
# cr.fill()
surface.write_to_png('bg.png')
|