From 7dc8fefbfd1426ab5dde875a86372a6796e28992 Mon Sep 17 00:00:00 2001 From: Jehan Date: Mon, 6 Apr 2026 14:38:00 +0200 Subject: [PATCH] app, libgimp, pdb, plug-ins: new gimp_resources_loaded() function. And use the function in script-fu. While we want wrong resource names to trigger WARNINGs in Script-Fu scripts and plug-ins (because these can be considered as a bug), we don't want it to happen when the resource is not loaded on purpose (mostly it means that we run GIMP with --no-data or --no-fonts). This was happening in our own builds when running GIMP through the in-build-gimp.py script and that was always very annoying. Now in such a case, we will only output an INFO message on stderr (it's still a good idea to warn about this, because if we imagined someone was running a script non-interactively, which called one of these procedures, they may have relied on the default value of some arguments. Therefore they'd want to know about such a behavior change). --- app/pdb/internal-procs.c | 2 +- app/pdb/resource-cmds.c | 63 +++++++++++++++++++ libgimp/gimp.def | 1 + libgimp/gimpresource.c | 25 ++++++++ libgimp/gimpresource.h | 2 + libgimp/gimpresource_pdb.c | 38 +++++++++++ libgimp/gimpresource_pdb.h | 1 + pdb/groups/resource.pdb | 38 ++++++++++- .../script-fu/libscriptfu/script-fu-arg.c | 10 ++- 9 files changed, 177 insertions(+), 3 deletions(-) diff --git a/app/pdb/internal-procs.c b/app/pdb/internal-procs.c index f9d6a3319a..5ae707bbc2 100644 --- a/app/pdb/internal-procs.c +++ b/app/pdb/internal-procs.c @@ -30,7 +30,7 @@ #include "internal-procs.h" -/* 785 procedures registered total */ +/* 786 procedures registered total */ void internal_procs_init (GimpPDB *pdb) diff --git a/app/pdb/resource-cmds.c b/app/pdb/resource-cmds.c index 1d9b62b558..78ebff4783 100644 --- a/app/pdb/resource-cmds.c +++ b/app/pdb/resource-cmds.c @@ -48,6 +48,38 @@ #include "gimp-intl.h" +static GimpValueArray * +resources_loaded_invoker (GimpProcedure *procedure, + Gimp *gimp, + GimpContext *context, + GimpProgress *progress, + const GimpValueArray *args, + GError **error) +{ + gboolean success = TRUE; + GimpValueArray *return_vals; + const gchar *type_name; + gboolean loaded = FALSE; + + type_name = g_value_get_string (gimp_value_array_index (args, 0)); + + if (success) + { + if (g_type_from_name (type_name) == GIMP_TYPE_FONT) + loaded = ! gimp->no_fonts; + else + loaded = ! gimp->no_data; + } + + return_vals = gimp_procedure_get_return_values (procedure, success, + error ? *error : NULL); + + if (success) + g_value_set_boolean (gimp_value_array_index (return_vals, 1), loaded); + + return return_vals; +} + static GimpValueArray * resource_get_by_name_invoker (GimpProcedure *procedure, Gimp *gimp, @@ -510,6 +542,37 @@ register_resource_procs (GimpPDB *pdb) { GimpProcedure *procedure; + /* + * gimp-resources-loaded + */ + procedure = gimp_procedure_new (resources_loaded_invoker, TRUE); + gimp_object_set_static_name (GIMP_OBJECT (procedure), + "gimp-resources-loaded"); + gimp_procedure_set_static_help (procedure, + "Returns whether resource of a given type were loaded.", + "Returns whether resources of a given type were loaded.\n" + "In particular, it would return FALSE if GIMP was started with `--no-data` or `--no-fonts` for fonts.", + NULL); + gimp_procedure_set_static_attribution (procedure, + "Jehan", + "Jehan", + "2026"); + gimp_procedure_add_argument (procedure, + gimp_param_spec_string ("type-name", + "type name", + "The name of the resource type e.g. GimpFont", + FALSE, FALSE, TRUE, + NULL, + GIMP_PARAM_READWRITE)); + gimp_procedure_add_return_value (procedure, + g_param_spec_boolean ("loaded", + "loaded", + "Whether resources of @type_name were loaded", + FALSE, + GIMP_PARAM_READWRITE)); + gimp_pdb_register_procedure (pdb, procedure); + g_object_unref (procedure); + /* * gimp-resource-get-by-name */ diff --git a/libgimp/gimp.def b/libgimp/gimp.def index 6d1693f7e8..f53193283d 100644 --- a/libgimp/gimp.def +++ b/libgimp/gimp.def @@ -1059,6 +1059,7 @@ EXPORTS gimp_resource_rename gimp_resource_select_new gimp_resource_select_set + gimp_resources_loaded gimp_selection_all gimp_selection_border gimp_selection_bounds diff --git a/libgimp/gimpresource.c b/libgimp/gimpresource.c index 064af208ad..c5461c9428 100644 --- a/libgimp/gimpresource.c +++ b/libgimp/gimpresource.c @@ -273,6 +273,31 @@ gimp_resource_deserialize_create (GType type, return GIMP_CONFIG (resource);; } + +/* Public functions */ + +/** + * gimp_resources_loaded: + * @resource_type: The #GType of the resource. + * + * Returns whether the resource with the given @resource_type were + * loaded. + * + * In particular, it would return FALSE if GIMP was started with + * `--no-data` (or `--no-fonts` for fonts). + * + * Returns: Whether resources of @resource_type were loaded. + * + * Since: 3.2.4 + **/ +gboolean +gimp_resources_loaded (GType resource_type) +{ + g_return_val_if_fail (g_type_is_a (resource_type, GIMP_TYPE_RESOURCE), FALSE); + + return _gimp_resources_loaded (g_type_name (resource_type)); +} + /** * gimp_resource_get_id: * @resource: The resource. diff --git a/libgimp/gimpresource.h b/libgimp/gimpresource.h index ec0384bed8..ab94998086 100644 --- a/libgimp/gimpresource.h +++ b/libgimp/gimpresource.h @@ -49,6 +49,8 @@ struct _GimpResourceClass void (*_gimp_reserved9) (void); }; +gboolean gimp_resources_loaded (GType resource_type); + gint32 gimp_resource_get_id (GimpResource *resource); GimpResource * gimp_resource_get_by_id (gint32 resource_id); GimpResource * gimp_resource_get_by_name (GType resource_type, diff --git a/libgimp/gimpresource_pdb.c b/libgimp/gimpresource_pdb.c index 6420ab8d03..6fc49eb0a1 100644 --- a/libgimp/gimpresource_pdb.c +++ b/libgimp/gimpresource_pdb.c @@ -36,6 +36,44 @@ **/ +/** + * _gimp_resources_loaded: + * @type_name: The name of the resource type e.g. GimpFont. + * + * Returns whether resource of a given type were loaded. + * + * Returns whether resources of a given type were loaded. + * In particular, it would return FALSE if GIMP was started with + * `--no-data` or `--no-fonts` for fonts. + * + * Returns: Whether resources of @type_name were loaded. + * + * Since: 3.2.4 + **/ +gboolean +_gimp_resources_loaded (const gchar *type_name) +{ + GimpValueArray *args; + GimpValueArray *return_vals; + gboolean loaded = FALSE; + + args = gimp_value_array_new_from_types (NULL, + G_TYPE_STRING, type_name, + G_TYPE_NONE); + + return_vals = _gimp_pdb_run_procedure_array (gimp_get_pdb (), + "gimp-resources-loaded", + args); + gimp_value_array_unref (args); + + if (GIMP_VALUES_GET_ENUM (return_vals, 0) == GIMP_PDB_SUCCESS) + loaded = GIMP_VALUES_GET_BOOLEAN (return_vals, 1); + + gimp_value_array_unref (return_vals); + + return loaded; +} + /** * _gimp_resource_get_by_name: * @type_name: The name of the resource type e.g. GimpFont. diff --git a/libgimp/gimpresource_pdb.h b/libgimp/gimpresource_pdb.h index 3722440b51..4998f2f38d 100644 --- a/libgimp/gimpresource_pdb.h +++ b/libgimp/gimpresource_pdb.h @@ -32,6 +32,7 @@ G_BEGIN_DECLS /* For information look into the C source or the html documentation */ +G_GNUC_INTERNAL gboolean _gimp_resources_loaded (const gchar *type_name); G_GNUC_INTERNAL GimpResource* _gimp_resource_get_by_name (const gchar *type_name, const gchar *resource_name); G_GNUC_INTERNAL GimpResource* _gimp_resource_get_by_identifiers (const gchar *type_name, diff --git a/pdb/groups/resource.pdb b/pdb/groups/resource.pdb index c75e86b717..c7c5813660 100644 --- a/pdb/groups/resource.pdb +++ b/pdb/groups/resource.pdb @@ -16,6 +16,41 @@ # "Perlized" from C source by Manish Singh +sub resources_loaded { + $blurb = "Returns whether resource of a given type were loaded."; + $help = < 'type_name', type => 'string', non_empty => 1, + desc => 'The name of the resource type e.g. GimpFont' } + ); + + @outargs = ( + { name => 'loaded', type => 'boolean', + desc => 'Whether resources of @type_name were loaded' } + ); + + %invoke = ( + code => <<'CODE' +{ + if (g_type_from_name (type_name) == GIMP_TYPE_FONT) + loaded = ! gimp->no_fonts; + else + loaded = ! gimp->no_data; +} +CODE + ); +} + sub resource_get_by_name { $blurb = "Returns a resource with the given name."; $help = <default_value.sfa_resource.resource_type != GIMP_TYPE_FONT) - g_warning ("%s declared resource name is invalid %s", G_STRFUNC, declared_name_of_default); + { + if (gimp_resources_loaded (arg->default_value.sfa_resource.resource_type)) + g_warning ("%s declared resource name is invalid %s", G_STRFUNC, declared_name_of_default); + else + g_printerr ("INFO: argument '%s' (value \"%s\") of procedure '%s' ignored " + "because resources of type %s are not loaded.\n", + name, declared_name_of_default, gimp_procedure_get_name (procedure), + g_type_name (arg->default_value.sfa_resource.resource_type)); + } script_fu_add_resource_arg_default_from_context (procedure, name, nick, blurb, func); } else