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).
This commit is contained in:
Jehan 2026-04-06 14:38:00 +02:00
parent 9cab16c193
commit 7dc8fefbfd
9 changed files with 177 additions and 3 deletions

View file

@ -30,7 +30,7 @@
#include "internal-procs.h" #include "internal-procs.h"
/* 785 procedures registered total */ /* 786 procedures registered total */
void void
internal_procs_init (GimpPDB *pdb) internal_procs_init (GimpPDB *pdb)

View file

@ -48,6 +48,38 @@
#include "gimp-intl.h" #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 * static GimpValueArray *
resource_get_by_name_invoker (GimpProcedure *procedure, resource_get_by_name_invoker (GimpProcedure *procedure,
Gimp *gimp, Gimp *gimp,
@ -510,6 +542,37 @@ register_resource_procs (GimpPDB *pdb)
{ {
GimpProcedure *procedure; 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 * gimp-resource-get-by-name
*/ */

View file

@ -1059,6 +1059,7 @@ EXPORTS
gimp_resource_rename gimp_resource_rename
gimp_resource_select_new gimp_resource_select_new
gimp_resource_select_set gimp_resource_select_set
gimp_resources_loaded
gimp_selection_all gimp_selection_all
gimp_selection_border gimp_selection_border
gimp_selection_bounds gimp_selection_bounds

View file

@ -273,6 +273,31 @@ gimp_resource_deserialize_create (GType type,
return GIMP_CONFIG (resource);; 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: * gimp_resource_get_id:
* @resource: The resource. * @resource: The resource.

View file

@ -49,6 +49,8 @@ struct _GimpResourceClass
void (*_gimp_reserved9) (void); void (*_gimp_reserved9) (void);
}; };
gboolean gimp_resources_loaded (GType resource_type);
gint32 gimp_resource_get_id (GimpResource *resource); gint32 gimp_resource_get_id (GimpResource *resource);
GimpResource * gimp_resource_get_by_id (gint32 resource_id); GimpResource * gimp_resource_get_by_id (gint32 resource_id);
GimpResource * gimp_resource_get_by_name (GType resource_type, GimpResource * gimp_resource_get_by_name (GType resource_type,

View file

@ -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: * _gimp_resource_get_by_name:
* @type_name: The name of the resource type e.g. GimpFont. * @type_name: The name of the resource type e.g. GimpFont.

View file

@ -32,6 +32,7 @@ G_BEGIN_DECLS
/* For information look into the C source or the html documentation */ /* 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, G_GNUC_INTERNAL GimpResource* _gimp_resource_get_by_name (const gchar *type_name,
const gchar *resource_name); const gchar *resource_name);
G_GNUC_INTERNAL GimpResource* _gimp_resource_get_by_identifiers (const gchar *type_name, G_GNUC_INTERNAL GimpResource* _gimp_resource_get_by_identifiers (const gchar *type_name,

View file

@ -16,6 +16,41 @@
# "Perlized" from C source by Manish Singh <yosh@gimp.org> # "Perlized" from C source by Manish Singh <yosh@gimp.org>
sub resources_loaded {
$blurb = "Returns whether resource of a given type were loaded.";
$help = <<HELP;
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.
HELP
&jehan_pdb_misc('2026', '3.2.4');
$lib_private = 1;
@inargs = (
{ name => '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 { sub resource_get_by_name {
$blurb = "Returns a resource with the given name."; $blurb = "Returns a resource with the given name.";
$help = <<HELP; $help = <<HELP;
@ -497,7 +532,8 @@ CODE
"gimppdberror.h" "gimppdberror.h"
"gimp-intl.h"); "gimp-intl.h");
@procs = qw(resource_get_by_name @procs = qw(resources_loaded
resource_get_by_name
resource_get_by_identifiers resource_get_by_identifiers
resource_id_is_valid resource_id_is_valid
resource_id_is_brush resource_id_is_brush

View file

@ -311,7 +311,15 @@ script_fu_add_resource_arg (
* later on, the fonts will likely have been loaded by then. * later on, the fonts will likely have been loaded by then.
*/ */
if (arg->default_value.sfa_resource.resource_type != GIMP_TYPE_FONT) if (arg->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); script_fu_add_resource_arg_default_from_context (procedure, name, nick, blurb, func);
} }
else else