1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * GimpText
5 * Copyright (C) 2003 Sven Neumann <sven@gimp.org>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <gegl.h>
25 #include <cairo.h>
26
27 #include "libgimpbase/gimpbase.h"
28 #include "text-types.h"
29
30 #include "core/gimp.h"
31 #include "core/gimpdrawable-private.h" /* eek */
32 #include "core/gimpimage.h"
33 #include "core/gimpparasitelist.h"
34
35 #include "gimptext.h"
36 #include "gimptext-parasite.h"
37 #include "gimptextlayer.h"
38 #include "gimptextlayer-xcf.h"
39
40 #include "gimp-intl.h"
41
42
43 enum
44 {
45 TEXT_LAYER_XCF_NONE = 0,
46 TEXT_LAYER_XCF_DONT_AUTO_RENAME = 1 << 0,
47 TEXT_LAYER_XCF_MODIFIED = 1 << 1
48 };
49
50
51 static GimpLayer * gimp_text_layer_from_layer (GimpLayer *layer,
52 GimpText *text);
53
54
55 gboolean
gimp_text_layer_xcf_load_hack(GimpLayer ** layer)56 gimp_text_layer_xcf_load_hack (GimpLayer **layer)
57 {
58 const gchar *name;
59 GimpText *text = NULL;
60 const GimpParasite *parasite;
61
62 g_return_val_if_fail (layer != NULL, FALSE);
63 g_return_val_if_fail (GIMP_IS_LAYER (*layer), FALSE);
64
65 name = gimp_text_parasite_name ();
66 parasite = gimp_item_parasite_find (GIMP_ITEM (*layer), name);
67
68 if (parasite)
69 {
70 GError *error = NULL;
71
72 text = gimp_text_from_parasite (parasite, &error);
73
74 if (error)
75 {
76 gimp_message (gimp_item_get_image (GIMP_ITEM (*layer))->gimp, NULL,
77 GIMP_MESSAGE_ERROR,
78 _("Problems parsing the text parasite for layer '%s':\n"
79 "%s\n\n"
80 "Some text properties may be wrong. "
81 "Unless you want to edit the text layer, "
82 "you don't need to worry about this."),
83 gimp_object_get_name (*layer),
84 error->message);
85 g_clear_error (&error);
86 }
87 }
88 else
89 {
90 name = gimp_text_gdyntext_parasite_name ();
91
92 parasite = gimp_item_parasite_find (GIMP_ITEM (*layer), name);
93
94 if (parasite)
95 text = gimp_text_from_gdyntext_parasite (parasite);
96 }
97
98 if (text)
99 {
100 *layer = gimp_text_layer_from_layer (*layer, text);
101
102 /* let the text layer knows what parasite was used to create it */
103 GIMP_TEXT_LAYER (*layer)->text_parasite = name;
104 }
105
106 return (text != NULL);
107 }
108
109 void
gimp_text_layer_xcf_save_prepare(GimpTextLayer * layer)110 gimp_text_layer_xcf_save_prepare (GimpTextLayer *layer)
111 {
112 GimpText *text;
113
114 g_return_if_fail (GIMP_IS_TEXT_LAYER (layer));
115
116 /* If the layer has a text parasite already, it wasn't changed and we
117 * can simply save the original parasite back which is still attached.
118 */
119 if (layer->text_parasite)
120 return;
121
122 text = gimp_text_layer_get_text (layer);
123 if (text)
124 {
125 GimpParasite *parasite = gimp_text_to_parasite (text);
126
127 /* Don't push an undo because the parasite only exists temporarily
128 * while the text layer is saved to XCF.
129 */
130 gimp_item_parasite_attach (GIMP_ITEM (layer), parasite, FALSE);
131
132 gimp_parasite_free (parasite);
133 }
134 }
135
136 guint32
gimp_text_layer_get_xcf_flags(GimpTextLayer * text_layer)137 gimp_text_layer_get_xcf_flags (GimpTextLayer *text_layer)
138 {
139 guint flags = 0;
140
141 g_return_val_if_fail (GIMP_IS_TEXT_LAYER (text_layer), 0);
142
143 if (! text_layer->auto_rename)
144 flags |= TEXT_LAYER_XCF_DONT_AUTO_RENAME;
145
146 if (text_layer->modified)
147 flags |= TEXT_LAYER_XCF_MODIFIED;
148
149 return flags;
150 }
151
152 void
gimp_text_layer_set_xcf_flags(GimpTextLayer * text_layer,guint32 flags)153 gimp_text_layer_set_xcf_flags (GimpTextLayer *text_layer,
154 guint32 flags)
155 {
156 g_return_if_fail (GIMP_IS_TEXT_LAYER (text_layer));
157
158 g_object_set (text_layer,
159 "auto-rename", (flags & TEXT_LAYER_XCF_DONT_AUTO_RENAME) == 0,
160 "modified", (flags & TEXT_LAYER_XCF_MODIFIED) != 0,
161 NULL);
162 }
163
164
165 /**
166 * gimp_text_layer_from_layer:
167 * @layer: a #GimpLayer object
168 * @text: a #GimpText object
169 *
170 * Converts a standard #GimpLayer and a #GimpText object into a
171 * #GimpTextLayer. The new text layer takes ownership of the @text and
172 * @layer objects. The @layer object is rendered unusable by this
173 * function. Don't even try to use if afterwards!
174 *
175 * This is a gross hack that is needed in order to load text layers
176 * from XCF files in a backwards-compatible way. Please don't use it
177 * for anything else!
178 *
179 * Return value: a newly allocated #GimpTextLayer object
180 **/
181 static GimpLayer *
gimp_text_layer_from_layer(GimpLayer * layer,GimpText * text)182 gimp_text_layer_from_layer (GimpLayer *layer,
183 GimpText *text)
184 {
185 GimpTextLayer *text_layer;
186 GimpDrawable *drawable;
187
188 g_return_val_if_fail (GIMP_IS_LAYER (layer), NULL);
189 g_return_val_if_fail (GIMP_IS_TEXT (text), NULL);
190
191 text_layer = g_object_new (GIMP_TYPE_TEXT_LAYER,
192 "image", gimp_item_get_image (GIMP_ITEM (layer)),
193 NULL);
194
195 gimp_item_replace_item (GIMP_ITEM (text_layer), GIMP_ITEM (layer));
196
197 drawable = GIMP_DRAWABLE (text_layer);
198
199 gimp_drawable_steal_buffer (drawable, GIMP_DRAWABLE (layer));
200
201 gimp_layer_set_opacity (GIMP_LAYER (text_layer),
202 gimp_layer_get_opacity (layer), FALSE);
203 gimp_layer_set_mode (GIMP_LAYER (text_layer),
204 gimp_layer_get_mode (layer), FALSE);
205 gimp_layer_set_blend_space (GIMP_LAYER (text_layer),
206 gimp_layer_get_blend_space (layer), FALSE);
207 gimp_layer_set_composite_space (GIMP_LAYER (text_layer),
208 gimp_layer_get_composite_space (layer), FALSE);
209 gimp_layer_set_composite_mode (GIMP_LAYER (text_layer),
210 gimp_layer_get_composite_mode (layer), FALSE);
211 gimp_layer_set_lock_alpha (GIMP_LAYER (text_layer),
212 gimp_layer_get_lock_alpha (layer), FALSE);
213
214 gimp_text_layer_set_text (text_layer, text);
215
216 g_object_unref (text);
217 g_object_unref (layer);
218
219 return GIMP_LAYER (text_layer);
220 }
221