aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/edje/utils/gimp-edje-export.py
blob: 88e1cb61079d146595488b79d3f098968b0c83c2 (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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python

#   Gimp-Python - allows the writing of Gimp plugins in Python.
#   Copyright (C) 2007  Renato Chencarek <renato.chencarek@openbossa.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program 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 General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# INSTALL:
#
#      cp gimp-edje-export.py ~/.gimp/plug-ins/
#      chmod +x ~/.gimp/plug-ins/gimp-edje-export.py
#
#    Notes:
#     - ~/.gimp may change depending on gimp version, ie: ~/.gimp-2.4.
#     - Make sure this file is marked as executable!
#
# USAGE:
#    Load an image and go to "Python-Fu > Edje Save" that will
#    appear on the image's window.
#

from gimpfu import *
import os.path
import re, gettext


def save_layer(layer, filename):
    temp_image = pdb.gimp_image_new (layer.width, layer.height, layer.image.base_type)
    temp_drawable = pdb.gimp_layer_new_from_drawable (layer, temp_image)
    temp_image.add_layer (temp_drawable, -1)
    pdb.gimp_file_save(temp_image, temp_drawable, filename, filename)
    gimp.delete(temp_image)
    return

def write_image_section(layers, edje_file, filenames):
    edje_file.write("images {\n")
    for layer in layers:
        edje_file.write('   image: "%s" COMP;\n' % filenames[layer.name])
    edje_file.write("\n}\n")

def write_layer_data(layer, edje_file, filename):
    expr = re.compile('\W')
    name = expr.sub("_", layer.name)
    data = {'name':name, 'width':layer.width, 'height':layer.height,
            'x0':layer.offsets[0], 'y0':layer.offsets[1],
            'x1':layer.offsets[0] + layer.width, 'y1':layer.offsets[1] + layer.height,
            'path':filename, 'visible':int(layer.visible)
            }
    text = """\
         part {
            name: "%(name)s";
            type: IMAGE;
            mouse_events: 0;
            description {
               state: "default" 0.0;
               min: %(width)d %(height)d;
               max: %(width)d %(height)d;
               rel1 {
                  relative: 0.0 0.0;
                  offset: %(x0)d %(y0)d;
               }
               rel2 {
                  relative: 0.0 0.0;
                  offset: %(x1)d %(y1)d;
               }
               image {
                  normal: "%(path)s";
               }
               visible: %(visible)d;
            }
         }\n""" % data
    edje_file.write(text)

def fix_layers_name(img, save_path):
    filenames = {}
    for layer in img.layers:
        expr = re.compile('\W')
        name = expr.sub("_", layer.name)
        if not name:
            name = "unnamed"
        layer.name = name
        if name in filenames.keys():
            continue
        filename = os.path.join(save_path, name + ".png")
        filenames[name] = filename

    return filenames

def edje_save(img, drawable, save_path, save_layers, edje_filename, invisible):
    layers = []
    filenames = {}
    for layer in img.layers:
        if layer.visible or invisible:
            expr = re.compile('\W')
            name = expr.sub("_", layer.name)
            layer.name = name
            if name in filenames.keys():
                continue
            filename = os.path.join(save_path, name + ".png")
            filenames[name] = filename
            layers.append(layer)

    edje_file = open(os.path.join(save_path, edje_filename), 'w')
    write_image_section(layers, edje_file, filenames)

    edje_file.write('collections {\n   group {\n      name: "%s";\n' % img.name)
    edje_file.write('      min: %d %d;\n      max: %d %d;\n      parts {\n' % (img.width, img.height,img.width, img.height))
    layers.reverse()
    for l in layers:
        if save_layers:
            save_layer(l, filenames[l.name])
        write_layer_data(l, edje_file,filenames[l.name])
    edje_file.write('      }\n   }\n}\n')
    edje_file.close()

register(
    "python_fu_edje_save",
    "Export the Image as Edje file",
    "Export the Image as Edje file",
    "Renato Chencarek",
    "Renato Chencarek",
    "2007",
    "<Image>/Python-Fu/_Edje Save",
    "RGBA, GRAYA",
    [
        (PF_STRING, "save_path",  "Path",  "/tmp/"),
        (PF_TOGGLE, "save_layers",  "Save Layers ?", True),
        (PF_STRING, "edje_filename",  "Filename for edje",  "edje.edc"),
        (PF_TOGGLE, "export_invisible_layers",  "Export invisible layers", False)
    ],
    [],
    edje_save)

main()