1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995-2002 Spencer Kimball, Peter Mattis, and others
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <gdk-pixbuf/gdk-pixbuf.h>
21 #include <gegl.h>
22 
23 #include "libgimpbase/gimpbase.h"
24 #include "libgimpconfig/gimpconfig.h"
25 
26 #include "core-types.h"
27 
28 #include "config/gimprc.h"
29 
30 #include "gimp.h"
31 #include "gimp-data-factories.h"
32 #include "gimp-gradients.h"
33 #include "gimp-memsize.h"
34 #include "gimp-palettes.h"
35 #include "gimpcontainer.h"
36 #include "gimpbrush-load.h"
37 #include "gimpbrush.h"
38 #include "gimpbrushclipboard.h"
39 #include "gimpbrushgenerated-load.h"
40 #include "gimpbrushpipe-load.h"
41 #include "gimpdataloaderfactory.h"
42 #include "gimpdynamics.h"
43 #include "gimpdynamics-load.h"
44 #include "gimpgradient-load.h"
45 #include "gimpgradient.h"
46 #include "gimpmybrush-load.h"
47 #include "gimpmybrush.h"
48 #include "gimppalette-load.h"
49 #include "gimppalette.h"
50 #include "gimppattern-load.h"
51 #include "gimppattern.h"
52 #include "gimppatternclipboard.h"
53 #include "gimptagcache.h"
54 #include "gimptoolpreset.h"
55 #include "gimptoolpreset-load.h"
56 
57 #include "text/gimpfontfactory.h"
58 
59 #include "gimp-intl.h"
60 
61 
62 void
gimp_data_factories_init(Gimp * gimp)63 gimp_data_factories_init (Gimp *gimp)
64 {
65   g_return_if_fail (GIMP_IS_GIMP (gimp));
66 
67   gimp->brush_factory =
68     gimp_data_loader_factory_new (gimp,
69                                   GIMP_TYPE_BRUSH,
70                                   "brush-path",
71                                   "brush-path-writable",
72                                   gimp_brush_new,
73                                   gimp_brush_get_standard);
74   gimp_object_set_static_name (GIMP_OBJECT (gimp->brush_factory),
75                                "brush factory");
76   gimp_data_loader_factory_add_loader (gimp->brush_factory,
77                                        "GIMP Brush",
78                                        gimp_brush_load,
79                                        GIMP_BRUSH_FILE_EXTENSION,
80                                        TRUE);
81   gimp_data_loader_factory_add_loader (gimp->brush_factory,
82                                        "GIMP Brush Pixmap",
83                                        gimp_brush_load,
84                                        GIMP_BRUSH_PIXMAP_FILE_EXTENSION,
85                                        FALSE);
86   gimp_data_loader_factory_add_loader (gimp->brush_factory,
87                                        "Photoshop ABR Brush",
88                                        gimp_brush_load_abr,
89                                        GIMP_BRUSH_PS_FILE_EXTENSION,
90                                        FALSE);
91   gimp_data_loader_factory_add_loader (gimp->brush_factory,
92                                        "Paint Shop Pro JBR Brush",
93                                        gimp_brush_load_abr,
94                                        GIMP_BRUSH_PSP_FILE_EXTENSION,
95                                        FALSE);
96  gimp_data_loader_factory_add_loader (gimp->brush_factory,
97                                        "GIMP Generated Brush",
98                                        gimp_brush_generated_load,
99                                        GIMP_BRUSH_GENERATED_FILE_EXTENSION,
100                                        TRUE);
101   gimp_data_loader_factory_add_loader (gimp->brush_factory,
102                                        "GIMP Brush Pipe",
103                                        gimp_brush_pipe_load,
104                                        GIMP_BRUSH_PIPE_FILE_EXTENSION,
105                                        TRUE);
106 
107   gimp->dynamics_factory =
108     gimp_data_loader_factory_new (gimp,
109                                   GIMP_TYPE_DYNAMICS,
110                                   "dynamics-path",
111                                   "dynamics-path-writable",
112                                   gimp_dynamics_new,
113                                   gimp_dynamics_get_standard);
114   gimp_object_set_static_name (GIMP_OBJECT (gimp->dynamics_factory),
115                                "dynamics factory");
116   gimp_data_loader_factory_add_loader (gimp->dynamics_factory,
117                                        "GIMP Paint Dynamics",
118                                        gimp_dynamics_load,
119                                        GIMP_DYNAMICS_FILE_EXTENSION,
120                                        TRUE);
121 
122   gimp->mybrush_factory =
123     gimp_data_loader_factory_new (gimp,
124                                   GIMP_TYPE_MYBRUSH,
125                                   "mypaint-brush-path",
126                                   "mypaint-brush-path-writable",
127                                   NULL,
128                                   NULL);
129   gimp_object_set_static_name (GIMP_OBJECT (gimp->mybrush_factory),
130                                "mypaint brush factory");
131   gimp_data_loader_factory_add_loader (gimp->mybrush_factory,
132                                        "MyPaint Brush",
133                                        gimp_mybrush_load,
134                                        GIMP_MYBRUSH_FILE_EXTENSION,
135                                        FALSE);
136 
137   gimp->pattern_factory =
138     gimp_data_loader_factory_new (gimp,
139                                   GIMP_TYPE_PATTERN,
140                                   "pattern-path",
141                                   "pattern-path-writable",
142                                   NULL,
143                                   gimp_pattern_get_standard);
144   gimp_object_set_static_name (GIMP_OBJECT (gimp->pattern_factory),
145                                "pattern factory");
146   gimp_data_loader_factory_add_loader (gimp->pattern_factory,
147                                        "GIMP Pattern",
148                                        gimp_pattern_load,
149                                        GIMP_PATTERN_FILE_EXTENSION,
150                                        TRUE);
151   gimp_data_loader_factory_add_fallback (gimp->pattern_factory,
152                                          "Pattern from GdkPixbuf",
153                                          gimp_pattern_load_pixbuf);
154 
155   gimp->gradient_factory =
156     gimp_data_loader_factory_new (gimp,
157                                   GIMP_TYPE_GRADIENT,
158                                   "gradient-path",
159                                   "gradient-path-writable",
160                                   gimp_gradient_new,
161                                   gimp_gradient_get_standard);
162   gimp_object_set_static_name (GIMP_OBJECT (gimp->gradient_factory),
163                                "gradient factory");
164   gimp_data_loader_factory_add_loader (gimp->gradient_factory,
165                                        "GIMP Gradient",
166                                        gimp_gradient_load,
167                                        GIMP_GRADIENT_FILE_EXTENSION,
168                                        TRUE);
169   gimp_data_loader_factory_add_loader (gimp->gradient_factory,
170                                        "SVG Gradient",
171                                        gimp_gradient_load_svg,
172                                        GIMP_GRADIENT_SVG_FILE_EXTENSION,
173                                        FALSE);
174 
175   gimp->palette_factory =
176     gimp_data_loader_factory_new (gimp,
177                                   GIMP_TYPE_PALETTE,
178                                   "palette-path",
179                                   "palette-path-writable",
180                                   gimp_palette_new,
181                                   gimp_palette_get_standard);
182   gimp_object_set_static_name (GIMP_OBJECT (gimp->palette_factory),
183                                "palette factory");
184   gimp_data_loader_factory_add_loader (gimp->palette_factory,
185                                        "GIMP Palette",
186                                        gimp_palette_load,
187                                        GIMP_PALETTE_FILE_EXTENSION,
188                                        TRUE);
189 
190   gimp->font_factory =
191     gimp_font_factory_new (gimp,
192                            "font-path");
193   gimp_object_set_static_name (GIMP_OBJECT (gimp->font_factory),
194                                "font factory");
195 
196   gimp->tool_preset_factory =
197     gimp_data_loader_factory_new (gimp,
198                                   GIMP_TYPE_TOOL_PRESET,
199                                   "tool-preset-path",
200                                   "tool-preset-path-writable",
201                                   gimp_tool_preset_new,
202                                   NULL);
203   gimp_object_set_static_name (GIMP_OBJECT (gimp->tool_preset_factory),
204                                "tool preset factory");
205   gimp_data_loader_factory_add_loader (gimp->tool_preset_factory,
206                                        "GIMP Tool Preset",
207                                        gimp_tool_preset_load,
208                                        GIMP_TOOL_PRESET_FILE_EXTENSION,
209                                        TRUE);
210 
211   gimp->tag_cache = gimp_tag_cache_new ();
212 }
213 
214 void
gimp_data_factories_add_builtin(Gimp * gimp)215 gimp_data_factories_add_builtin (Gimp *gimp)
216 {
217   GimpData *data;
218 
219   g_return_if_fail (GIMP_IS_GIMP (gimp));
220 
221   /*  add the builtin FG -> BG etc. gradients  */
222   gimp_gradients_init (gimp);
223 
224   /*  add the color history palette  */
225   gimp_palettes_init (gimp);
226 
227   /*  add the clipboard brushes  */
228   data = gimp_brush_clipboard_new (gimp, FALSE);
229   gimp_data_make_internal (data, "gimp-brush-clipboard-image");
230   gimp_container_add (gimp_data_factory_get_container (gimp->brush_factory),
231                       GIMP_OBJECT (data));
232   g_object_unref (data);
233 
234   data = gimp_brush_clipboard_new (gimp, TRUE);
235   gimp_data_make_internal (data, "gimp-brush-clipboard-mask");
236   gimp_container_add (gimp_data_factory_get_container (gimp->brush_factory),
237                       GIMP_OBJECT (data));
238   g_object_unref (data);
239 
240   /*  add the clipboard pattern  */
241   data = gimp_pattern_clipboard_new (gimp);
242   gimp_data_make_internal (data, "gimp-pattern-clipboard-image");
243   gimp_container_add (gimp_data_factory_get_container (gimp->pattern_factory),
244                       GIMP_OBJECT (data));
245   g_object_unref (data);
246 }
247 
248 void
gimp_data_factories_clear(Gimp * gimp)249 gimp_data_factories_clear (Gimp *gimp)
250 {
251   g_return_if_fail (GIMP_IS_GIMP (gimp));
252 
253   if (gimp->brush_factory)
254     gimp_data_factory_data_free (gimp->brush_factory);
255 
256   if (gimp->dynamics_factory)
257     gimp_data_factory_data_free (gimp->dynamics_factory);
258 
259   if (gimp->mybrush_factory)
260     gimp_data_factory_data_free (gimp->mybrush_factory);
261 
262   if (gimp->pattern_factory)
263     gimp_data_factory_data_free (gimp->pattern_factory);
264 
265   if (gimp->gradient_factory)
266     gimp_data_factory_data_free (gimp->gradient_factory);
267 
268   if (gimp->palette_factory)
269     gimp_data_factory_data_free (gimp->palette_factory);
270 
271   if (gimp->font_factory)
272     gimp_data_factory_data_free (gimp->font_factory);
273 
274   if (gimp->tool_preset_factory)
275     gimp_data_factory_data_free (gimp->tool_preset_factory);
276 }
277 
278 void
gimp_data_factories_exit(Gimp * gimp)279 gimp_data_factories_exit (Gimp *gimp)
280 {
281   g_return_if_fail (GIMP_IS_GIMP (gimp));
282 
283   g_clear_object (&gimp->brush_factory);
284   g_clear_object (&gimp->dynamics_factory);
285   g_clear_object (&gimp->mybrush_factory);
286   g_clear_object (&gimp->pattern_factory);
287   g_clear_object (&gimp->gradient_factory);
288   g_clear_object (&gimp->palette_factory);
289   g_clear_object (&gimp->font_factory);
290   g_clear_object (&gimp->tool_preset_factory);
291   g_clear_object (&gimp->tag_cache);
292 }
293 
294 gint64
gimp_data_factories_get_memsize(Gimp * gimp,gint64 * gui_size)295 gimp_data_factories_get_memsize (Gimp   *gimp,
296                                  gint64 *gui_size)
297 {
298   gint64 memsize = 0;
299 
300   g_return_val_if_fail (GIMP_IS_GIMP (gimp), 0);
301 
302   memsize += gimp_object_get_memsize (GIMP_OBJECT (gimp->named_buffers),
303                                       gui_size);
304   memsize += gimp_object_get_memsize (GIMP_OBJECT (gimp->brush_factory),
305                                       gui_size);
306   memsize += gimp_object_get_memsize (GIMP_OBJECT (gimp->dynamics_factory),
307                                       gui_size);
308   memsize += gimp_object_get_memsize (GIMP_OBJECT (gimp->mybrush_factory),
309                                       gui_size);
310   memsize += gimp_object_get_memsize (GIMP_OBJECT (gimp->pattern_factory),
311                                       gui_size);
312   memsize += gimp_object_get_memsize (GIMP_OBJECT (gimp->gradient_factory),
313                                       gui_size);
314   memsize += gimp_object_get_memsize (GIMP_OBJECT (gimp->palette_factory),
315                                       gui_size);
316   memsize += gimp_object_get_memsize (GIMP_OBJECT (gimp->font_factory),
317                                       gui_size);
318   memsize += gimp_object_get_memsize (GIMP_OBJECT (gimp->tool_preset_factory),
319                                       gui_size);
320 
321   memsize += gimp_object_get_memsize (GIMP_OBJECT (gimp->tag_cache),
322                                       gui_size);
323 
324   return memsize;
325 }
326 
327 void
gimp_data_factories_data_clean(Gimp * gimp)328 gimp_data_factories_data_clean (Gimp *gimp)
329 {
330   g_return_if_fail (GIMP_IS_GIMP (gimp));
331 
332   gimp_data_factory_data_clean (gimp->brush_factory);
333   gimp_data_factory_data_clean (gimp->dynamics_factory);
334   gimp_data_factory_data_clean (gimp->mybrush_factory);
335   gimp_data_factory_data_clean (gimp->pattern_factory);
336   gimp_data_factory_data_clean (gimp->gradient_factory);
337   gimp_data_factory_data_clean (gimp->palette_factory);
338   gimp_data_factory_data_clean (gimp->font_factory);
339   gimp_data_factory_data_clean (gimp->tool_preset_factory);
340 }
341 
342 void
gimp_data_factories_load(Gimp * gimp,GimpInitStatusFunc status_callback)343 gimp_data_factories_load (Gimp               *gimp,
344                           GimpInitStatusFunc  status_callback)
345 {
346   g_return_if_fail (GIMP_IS_GIMP (gimp));
347 
348   /*  initialize the list of gimp brushes    */
349   status_callback (NULL, _("Brushes"), 0.1);
350   gimp_data_factory_data_init (gimp->brush_factory, gimp->user_context,
351                                gimp->no_data);
352 
353   /*  initialize the list of gimp dynamics   */
354   status_callback (NULL, _("Dynamics"), 0.15);
355   gimp_data_factory_data_init (gimp->dynamics_factory, gimp->user_context,
356                                gimp->no_data);
357 
358   /*  initialize the list of mypaint brushes    */
359   status_callback (NULL, _("MyPaint Brushes"), 0.2);
360   gimp_data_factory_data_init (gimp->mybrush_factory, gimp->user_context,
361                                gimp->no_data);
362 
363   /*  initialize the list of gimp patterns   */
364   status_callback (NULL, _("Patterns"), 0.3);
365   gimp_data_factory_data_init (gimp->pattern_factory, gimp->user_context,
366                                gimp->no_data);
367 
368   /*  initialize the list of gimp palettes   */
369   status_callback (NULL, _("Palettes"), 0.4);
370   gimp_data_factory_data_init (gimp->palette_factory, gimp->user_context,
371                                gimp->no_data);
372 
373   /*  initialize the list of gimp gradients  */
374   status_callback (NULL, _("Gradients"), 0.5);
375   gimp_data_factory_data_init (gimp->gradient_factory, gimp->user_context,
376                                gimp->no_data);
377 
378   /*  initialize the color history   */
379   status_callback (NULL, _("Color History"), 0.55);
380   gimp_palettes_load (gimp);
381 
382   /*  initialize the list of gimp fonts   */
383   status_callback (NULL, _("Fonts"), 0.6);
384   gimp_data_factory_data_init (gimp->font_factory, gimp->user_context,
385                                gimp->no_fonts);
386 
387   /*  initialize the list of gimp tool presets if we have a GUI  */
388   if (! gimp->no_interface)
389     {
390       status_callback (NULL, _("Tool Presets"), 0.7);
391       gimp_data_factory_data_init (gimp->tool_preset_factory, gimp->user_context,
392                                    gimp->no_data);
393     }
394 
395   /* update tag cache */
396   status_callback (NULL, _("Updating tag cache"), 0.75);
397   gimp_tag_cache_load (gimp->tag_cache);
398   gimp_tag_cache_add_container (gimp->tag_cache,
399                                 gimp_data_factory_get_container (gimp->brush_factory));
400   gimp_tag_cache_add_container (gimp->tag_cache,
401                                 gimp_data_factory_get_container (gimp->dynamics_factory));
402   gimp_tag_cache_add_container (gimp->tag_cache,
403                                 gimp_data_factory_get_container (gimp->mybrush_factory));
404   gimp_tag_cache_add_container (gimp->tag_cache,
405                                 gimp_data_factory_get_container (gimp->pattern_factory));
406   gimp_tag_cache_add_container (gimp->tag_cache,
407                                 gimp_data_factory_get_container (gimp->gradient_factory));
408   gimp_tag_cache_add_container (gimp->tag_cache,
409                                 gimp_data_factory_get_container (gimp->palette_factory));
410   gimp_tag_cache_add_container (gimp->tag_cache,
411                                 gimp_data_factory_get_container (gimp->font_factory));
412   gimp_tag_cache_add_container (gimp->tag_cache,
413                                 gimp_data_factory_get_container (gimp->tool_preset_factory));
414 }
415 
416 void
gimp_data_factories_save(Gimp * gimp)417 gimp_data_factories_save (Gimp *gimp)
418 {
419   g_return_if_fail (GIMP_IS_GIMP (gimp));
420 
421   gimp_tag_cache_save (gimp->tag_cache);
422 
423   gimp_data_factory_data_save (gimp->brush_factory);
424   gimp_data_factory_data_save (gimp->dynamics_factory);
425   gimp_data_factory_data_save (gimp->mybrush_factory);
426   gimp_data_factory_data_save (gimp->pattern_factory);
427   gimp_data_factory_data_save (gimp->gradient_factory);
428   gimp_data_factory_data_save (gimp->palette_factory);
429   gimp_data_factory_data_save (gimp->font_factory);
430   gimp_data_factory_data_save (gimp->tool_preset_factory);
431 
432   gimp_palettes_save (gimp);
433 }
434