Rename senfscons to site_scons and move python files arround
[senf.git] / site_scons / site_tools / Dia2Png.py
1 ## \file
2 # \brief Dia2Png builder
3
4 ## \package senfscons.Dia2Png
5 # \brief Build a PNG file from a DIA file
6 #
7 # This builder will convert a given DIA file into a PNG image. The
8 # size of the target file is specified by giving a preferred DPI value
9 # and a maximum width. The Builder will automatically fetch the
10 # correct aspect ratio from the dia file.
11 #
12 # \par Construction Envrionment Variables:
13 # <table class="senf">
14 # <tr><td>\c DIACOM</td><td>dia command, defaults to \c dia</td></tr>
15 # <tr><td>\c DIA2PNGDPI</td><td>resolution of converted image, defaults to 115</td></tr>
16 # <tr><td>\c DIA2PNGMAXWIDTH</td><td>maximum image width, defaults to 800</td></tr>
17 # </table>
18 #
19 # \ingroup builder
20
21 import os
22 import SCons.Builder, SCons.Action
23
24 def dia_getSize(env,source):
25     size = None
26     for line in os.popen(env['DIACOM']+" -e /proc/self/fd/1 -t eps "+str(source[0]),"r"):
27         if line.startswith("%%BoundingBox:"):
28             size=map(int,line.split()[3:])
29             break
30     return size
31
32 def dia2png_generator(source, target, env, for_signature):
33     if for_signature:
34         return "$DIACOM -t png -s $DIA2PNGDPI,$DIA2PNGMAXWIDTH $TARGET $SOURCE"
35     size = dia_getSize(env,source)
36     if not size: return None;
37     size[0] = size[0]*int(env['DIA2PNGDPI'])/72
38     size[1] = size[1]*int(env['DIA2PNGDPI'])/72
39     if size[0] > env['DIA2PNGMAXWIDTH']:
40         size[1] = size[1]*env['DIA2PNGMAXWIDTH']/size[0]
41         size[0] = env['DIA2PNGMAXWIDTH']
42     return SCons.Action.Action("$DIACOM -t png -s %dx%d -e $TARGET $SOURCE" % tuple(size))
43
44 Dia2Png = SCons.Builder.Builder(suffix = ".png",
45                                 src_suffix = ".dia",
46                                 generator = dia2png_generator,
47                                 single_source = 1)
48
49 def generate(env):
50     env['BUILDERS']['Dia2Png'] = Dia2Png
51     env['DIACOM'] = "dia"
52     env['DIA2PNGDPI'] = 115
53     env['DIA2PNGMAXWIDTH'] = 800
54
55 def exists(env):
56     return env.Detect("dia")