Salome HOME
git: ignore VSCode cache files, sat cache file and add a pre-commit config
[tools/sat.git] / data / templates / Application / config / splash_generator / splash2.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3
4 # This script can be used to generate resources (splash screen, logo app and about screen)
5 # to customize a SALOME application.
6
7 import os, sys
8
9
10 try:
11   __LANG__ = os.environ["LANG"] # original locale
12 except:
13   __LANG__ = "en_US.utf8" #default
14
15 __FR__={
16     "This script requires the Image, ImageDraw and ImageFont libraries":
17         "Ce script a besoin de Image, ImageDraw et ImageFont python librairies",
18     "WARNING: automatic filter application name for resources:":
19         "ATTENTION: filtre automatique sur le nom de l'application pour les ressources:",
20     "generate": "génération de",
21     "in directory": "dans le répertoire",
22     "Generate resources for": "Génération des ressources pour",
23 }
24
25 def _loc(text):
26   """very easy very locale standalone translation"""
27   if "FR" in __LANG__:
28     try:
29       return __FR__[text]
30     except:
31       return text
32   return text
33
34 try:
35     import Image
36     import ImageDraw
37     import ImageFont
38 except ImportError, exc:
39     print
40     print "ERROR: " + os.path.realpath(__file__)
41     print _loc("This script requires the Image, ImageDraw and ImageFont libraries")
42     print exc
43     sys.exit(1)
44
45
46 # splash screen
47 C_FONT_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'xirod.ttf')
48 C_SPLASH_WIDTH = 600
49 C_SPLASH_HEIGHT = 300
50 C_SPLASH_COLOR = (100, 120, 160)
51 C_SPLASH_TEXT_COLOR = "rgb(255, 250, 250)"
52 C_SHADOW_COLOR = "rgb(0, 0, 0)"
53 C_LOGO_COLOR = "rgb(200, 200, 200)"
54 C_BORDER_X = 30 #50
55 C_BORDER_Y = 3 #30
56 C_SHADOW_X = 2
57 C_SHADOW_Y = C_SHADOW_X
58
59 ##
60 # create the splash screen and the about logo.
61 def generate_splash(dest, appname, version, subtext=None):
62     if subtext is None:
63         subtext="Powered by SALOME"
64     uname = unicode(appname, 'UTF-8')
65     uversion = unicode(version, 'UTF-8')
66
67     fontbig = ImageFont.truetype(C_FONT_PATH, 44)
68     fontsmall = ImageFont.truetype(C_FONT_PATH, 16)
69
70     # create the splash screen
71     # 12 characters are maximum for for C_SPLASH_WIDTH
72     nbcar = len(uname)
73     WIDTH = C_SPLASH_WIDTH
74     if nbcar > 12:
75         WIDTH=C_SPLASH_WIDTH*nbcar/12 #a little more
76     if WIDTH > 1024:
77         WIDTH = 1024 #but not too big
78     #im = Image.new("RGBA", (WIDTH, C_SPLASH_HEIGHT), C_SPLASH_COLOR)
79     d0 = os.path.dirname(os.path.realpath(__file__))
80     f0 = os.path.join(d0,"Salome6_splash_fond.png")
81     #print "cvw splash2.py",f0
82     im = Image.open(f0)
83     im = im.resize((WIDTH, C_SPLASH_HEIGHT))
84     draw = ImageDraw.Draw(im)
85     
86     # add the name of the application
87     iw, ih = draw.textsize(uname, font=fontbig)
88     x = (WIDTH - iw) / 2.0 # horizontal center
89     y = (C_SPLASH_HEIGHT - ih) / 2.0 # vertical center
90     draw.text((x+C_SHADOW_X, y+C_SHADOW_Y), uname, font=fontbig, fill=C_SHADOW_COLOR)
91     draw.text((x, y), uname, font=fontbig, fill=C_SPLASH_TEXT_COLOR)
92
93     # add powered by SALOME
94     iw, ih = draw.textsize(subtext, font=fontsmall)
95     draw.text((C_BORDER_X+C_SHADOW_X, C_SPLASH_HEIGHT+C_SHADOW_Y-C_BORDER_Y-ih),
96               subtext, font=fontsmall, fill=C_SHADOW_COLOR)
97     draw.text((C_BORDER_X, C_SPLASH_HEIGHT-C_BORDER_Y-ih),
98               subtext, font=fontsmall, fill=C_SPLASH_TEXT_COLOR)
99
100     # add the version if any
101     if len(version) > 0:
102         iw, ih = draw.textsize(uversion, font=fontsmall)
103         draw.text((WIDTH+C_SHADOW_X-C_BORDER_X-iw, C_SPLASH_HEIGHT+C_SHADOW_Y-C_BORDER_Y-ih),
104                   uversion, font=fontsmall, fill=C_SHADOW_COLOR)
105         draw.text((WIDTH-C_BORDER_X-iw, C_SPLASH_HEIGHT-C_BORDER_Y-ih),
106                   uversion, font=fontsmall, fill=C_SPLASH_TEXT_COLOR)
107
108     del draw
109     d1 = os.path.join(dest, 'icon_splash.png')
110     im.save(d1, "PNG")
111     d2 = os.path.join(dest, 'icon_about.png')
112     im.save(d2, "PNG")
113     print _loc("in directory"), os.getcwd()
114     print _loc("generate"), d1
115     print _loc("generate"), d2
116
117 ##
118 # create the application logo
119 def generate_logo(dest, appname):
120     uname = unicode(appname, 'UTF-8')
121
122     # evaluate size before deleting draw
123     im = Image.new("RGBA", (20, 20), (0, 0, 0, 0))
124     draw = ImageDraw.Draw(im)
125     font = ImageFont.truetype(C_FONT_PATH, 18)
126     iw, ih = draw.textsize(uname, font=font)
127     del draw
128
129     im = Image.new("RGBA", (iw + 5, 20), (0, 0, 0, 0))
130     draw = ImageDraw.Draw(im)
131     draw.text((0+1, 0), uname, font=font, fill=C_SHADOW_COLOR)
132     draw.text((0, -1), uname, font=font, fill=C_LOGO_COLOR)
133     del draw
134     d1 = os.path.join(dest, 'icon_applogo.png')
135     im.save(d1, "PNG")
136     print _loc("generate"), d1
137    
138 def filter_appname(appname):
139     name = appname.upper() # xirod seems not have lower character
140     for i in ["_APPLICATION","_APPLI","_APP"]:
141         res = name.split(i)
142         if len(res) > 1:
143             name = res[0]
144             return name
145     
146     for i in ["APPLICATION_","APPLI_","APP_"]:
147         res = name.split(i)
148         if len(res) > 1:
149             name = res[1]
150             return name
151
152     return name
153
154 if __name__ == "__main__":
155     if len(sys.argv) < 3:
156       print "usage: %s <dest> <name> [<version>] [<subtext>]" % sys.argv[0]
157       sys.exit(1)
158
159     dest = sys.argv[1]
160     appname = sys.argv[2]
161     version = ""
162     subtext = None
163     if len(sys.argv) > 3: version = sys.argv[3]
164     if len(sys.argv) > 4: subtext = sys.argv[4]
165
166     print
167     print _loc("Generate resources for"), appname
168     name = filter_appname(appname)
169     if name != appname:
170       print _loc("WARNING: automatic filter application name for resources:"),
171       print "'%s' -> '%s'" % (appname, name)
172     
173     generate_splash(dest, name, version, subtext)
174     generate_logo(dest, name)
175     print