From ea55b7a11a33e137ce3acf15e293393af46eceb8 Mon Sep 17 00:00:00 2001 From: Jehan Date: Wed, 13 Sep 2023 19:13:51 +0200 Subject: [PATCH] =?UTF-8?q?app,=20devel-docs,=20libgimp:=20updating=20gimp?= =?UTF-8?q?=5Ftext=5Flayer=5F[gs]et=5Ffont()=20and=20new=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … function gimp_font_get_pango_font_description(). Also updating file-pdf-save which is the only plug-in using these right now. Note that I am not fully happy with the new function gimp_font_get_pango_font_description() because I experienced some weird behavior in file-pdf-save which is that some fonts were wrong if this is called after pango_cairo_font_map_set_resolution(). But let's say this is a first step looking for improvements. --- app/pdb/text-layer-cmds.c | 42 ++++++++++----------- devel-docs/reference/gimp/gimp-3.0.toml.in | 5 +++ devel-docs/reference/gimp/urlmap.js | 1 + libgimp/gimp.def | 1 + libgimp/gimp.h | 1 + libgimp/gimpfont.c | 44 ++++++++++++++++++++++ libgimp/gimpfont.h | 3 ++ libgimp/gimptextlayer.c | 4 +- libgimp/gimptextlayer_pdb.c | 15 ++++---- libgimp/gimptextlayer_pdb.h | 4 +- libgimp/meson.build | 3 ++ meson.build | 2 +- pdb/groups/text_layer.pdb | 16 ++++---- plug-ins/common/file-pdf-save.c | 29 +++++++------- plug-ins/common/meson.build | 2 +- 15 files changed, 114 insertions(+), 58 deletions(-) diff --git a/app/pdb/text-layer-cmds.c b/app/pdb/text-layer-cmds.c index 28bb6c31ca..45e08e0309 100644 --- a/app/pdb/text-layer-cmds.c +++ b/app/pdb/text-layer-cmds.c @@ -255,26 +255,26 @@ text_layer_get_font_invoker (GimpProcedure *procedure, gboolean success = TRUE; GimpValueArray *return_vals; GimpTextLayer *layer; - gchar *font = NULL; + GimpFont *font = NULL; layer = g_value_get_object (gimp_value_array_index (args, 0)); if (success) { - GimpFont *font_obj; - g_object_get (gimp_text_layer_get_text (layer), - "font", &font_obj, + "font", &font, NULL); - font = g_strdup (gimp_font_get_lookup_name (font_obj)); - g_object_unref (font_obj); + /* The GimpText keeps a reference. Therefore unref before returning the + * pointer so that we don't leak a reference. + */ + g_object_unref (font); } return_vals = gimp_procedure_get_return_values (procedure, success, error ? *error : NULL); if (success) - g_value_take_string (gimp_value_array_index (return_vals, 1), font); + g_value_set_object (gimp_value_array_index (return_vals, 1), font); return return_vals; } @@ -289,10 +289,10 @@ text_layer_set_font_invoker (GimpProcedure *procedure, { gboolean success = TRUE; GimpTextLayer *layer; - const gchar *font; + GimpFont *font; layer = g_value_get_object (gimp_value_array_index (args, 0)); - font = g_value_get_string (gimp_value_array_index (args, 1)); + font = g_value_get_object (gimp_value_array_index (args, 1)); if (success) { @@ -1182,7 +1182,7 @@ register_text_layer_procs (GimpPDB *pdb) "gimp-text-layer-get-font"); gimp_procedure_set_static_help (procedure, "Get the font from a text layer as string.", - "This procedure returns the name of the font from a text layer.", + "This procedure returns the font from a text layer.", NULL); gimp_procedure_set_static_attribution (procedure, "Marcus Heese ", @@ -1195,12 +1195,11 @@ register_text_layer_procs (GimpPDB *pdb) FALSE, GIMP_PARAM_READWRITE)); gimp_procedure_add_return_value (procedure, - gimp_param_spec_string ("font", - "font", - "The font which is used in the specified text layer.", - FALSE, FALSE, FALSE, - NULL, - GIMP_PARAM_READWRITE)); + gimp_param_spec_font ("font", + "font", + "The font which is used in the specified text layer.", + FALSE, + GIMP_PARAM_READWRITE)); gimp_pdb_register_procedure (pdb, procedure); g_object_unref (procedure); @@ -1225,12 +1224,11 @@ register_text_layer_procs (GimpPDB *pdb) FALSE, GIMP_PARAM_READWRITE)); gimp_procedure_add_argument (procedure, - gimp_param_spec_string ("font", - "font", - "The new font to use", - FALSE, FALSE, FALSE, - NULL, - GIMP_PARAM_READWRITE)); + gimp_param_spec_font ("font", + "font", + "The new font to use", + FALSE, + GIMP_PARAM_READWRITE)); gimp_pdb_register_procedure (pdb, procedure); g_object_unref (procedure); diff --git a/devel-docs/reference/gimp/gimp-3.0.toml.in b/devel-docs/reference/gimp/gimp-3.0.toml.in index 794846d542..ea19926fdd 100644 --- a/devel-docs/reference/gimp/gimp-3.0.toml.in +++ b/devel-docs/reference/gimp/gimp-3.0.toml.in @@ -61,6 +61,11 @@ search_index = true description = "A 2D graphics library with support for multiple output devices" docs_url = "https://www.cairographics.org/manual/" + [dependencies."Pango-1.0"] + name = "Pango" + description = "Internationalized text layout and rendering" + docs_url = "https://docs.gtk.org/Pango/" + [theme] name = "basic" show_index_summary = true diff --git a/devel-docs/reference/gimp/urlmap.js b/devel-docs/reference/gimp/urlmap.js index 1c98a7a02c..043cb4072b 100644 --- a/devel-docs/reference/gimp/urlmap.js +++ b/devel-docs/reference/gimp/urlmap.js @@ -8,4 +8,5 @@ baseURLs = [ [ 'GdkPixbuf', 'https://docs.gtk.org/gdk-pixbuf/' ], [ 'Gio', 'https://docs.gtk.org/gio/' ], [ 'Gtk', 'https://docs.gtk.org/gtk3/' ], + [ 'Pango', 'https://docs.gtk.org/Pango/' ], ] diff --git a/libgimp/gimp.def b/libgimp/gimp.def index 8d03505926..0940f5faeb 100644 --- a/libgimp/gimp.def +++ b/libgimp/gimp.def @@ -294,6 +294,7 @@ EXPORTS gimp_floating_sel_remove gimp_floating_sel_to_layer gimp_font_get_by_name + gimp_font_get_pango_font_description gimp_font_get_type gimp_fonts_close_popup gimp_fonts_get_list diff --git a/libgimp/gimp.h b/libgimp/gimp.h index 4123fc1d94..6e90e530ee 100644 --- a/libgimp/gimp.h +++ b/libgimp/gimp.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include diff --git a/libgimp/gimpfont.c b/libgimp/gimpfont.c index e38187ee3b..e1a2a83faf 100644 --- a/libgimp/gimpfont.c +++ b/libgimp/gimpfont.c @@ -21,6 +21,8 @@ #include "config.h" +#include + #include "gimp.h" #include "gimpfont.h" @@ -40,3 +42,45 @@ static void gimp_font_class_init (GimpFontClass *klass) static void gimp_font_init (GimpFont *font) { } + +/** + * gimp_font_get_pango_font_description: + * @font: (transfer none): the [class@Gimp.Font] + * + * Returns a [class@Pango.Font.Description] representing @font. + * + * Returns: (transfer full): a %PangoFontDescription representing @font. + * + * Since: 3.0 + **/ +PangoFontDescription * +gimp_font_get_pango_font_description (GimpFont *font) +{ + PangoFontDescription *desc = NULL; + gchar *name = NULL; + gchar *collection_id = NULL; + gboolean is_internal; + + is_internal = _gimp_resource_get_identifiers (GIMP_RESOURCE (font), + &name, &collection_id); + /* TODO: we can't create fonts from internal fonts right now, but it should + * actually be possible because these are in fact alias to non-internal fonts. + * See #9985. + */ + if (! is_internal) + { + gchar *expanded_path; + + expanded_path = gimp_config_path_expand (collection_id, FALSE, NULL); + if (expanded_path != NULL && + FcConfigAppFontAddFile (FcConfigGetCurrent (), (const FcChar8 *) expanded_path)) + desc = pango_font_description_from_string (name); + + g_free (expanded_path); + } + + g_free (name); + g_free (collection_id); + + return desc; +} diff --git a/libgimp/gimpfont.h b/libgimp/gimpfont.h index 2b4fbdfa08..85f0602ed6 100644 --- a/libgimp/gimpfont.h +++ b/libgimp/gimpfont.h @@ -38,6 +38,9 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (GimpFont, gimp_font, GIMP, FONT, GimpResource) +PangoFontDescription * gimp_font_get_pango_font_description (GimpFont *font); + + G_END_DECLS #endif /* __GIMP_FONT_H__ */ diff --git a/libgimp/gimptextlayer.c b/libgimp/gimptextlayer.c index 26946cee0c..8bf8a2d636 100644 --- a/libgimp/gimptextlayer.c +++ b/libgimp/gimptextlayer.c @@ -121,6 +121,7 @@ static GimpLayer * gimp_text_layer_copy (GimpLayer *layer) { GimpTextLayer *new_layer; + GimpFont *font; gchar *text; gchar *fontname; gdouble size; @@ -129,7 +130,8 @@ gimp_text_layer_copy (GimpLayer *layer) g_return_val_if_fail (GIMP_IS_TEXT_LAYER (layer), NULL); text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer)); - fontname = gimp_text_layer_get_font (GIMP_TEXT_LAYER (layer)); + font = gimp_text_layer_get_font (GIMP_TEXT_LAYER (layer)); + fontname = gimp_resource_get_name (GIMP_RESOURCE (font)); size = gimp_text_layer_get_font_size (GIMP_TEXT_LAYER (layer), &unit); new_layer = gimp_text_layer_new (gimp_item_get_image (GIMP_ITEM (layer)), text, fontname, size, unit); diff --git a/libgimp/gimptextlayer_pdb.c b/libgimp/gimptextlayer_pdb.c index e4c9a84daf..6e57dbf0f1 100644 --- a/libgimp/gimptextlayer_pdb.c +++ b/libgimp/gimptextlayer_pdb.c @@ -254,20 +254,19 @@ gimp_text_layer_set_markup (GimpTextLayer *layer, * * Get the font from a text layer as string. * - * This procedure returns the name of the font from a text layer. + * This procedure returns the font from a text layer. * - * Returns: (transfer full): + * Returns: (transfer none): * The font which is used in the specified text layer. - * The returned value must be freed with g_free(). * * Since: 2.6 **/ -gchar * +GimpFont * gimp_text_layer_get_font (GimpTextLayer *layer) { GimpValueArray *args; GimpValueArray *return_vals; - gchar *font = NULL; + GimpFont *font = NULL; args = gimp_value_array_new_from_types (NULL, GIMP_TYPE_TEXT_LAYER, layer, @@ -279,7 +278,7 @@ gimp_text_layer_get_font (GimpTextLayer *layer) gimp_value_array_unref (args); if (GIMP_VALUES_GET_ENUM (return_vals, 0) == GIMP_PDB_SUCCESS) - font = GIMP_VALUES_DUP_STRING (return_vals, 1); + font = GIMP_VALUES_GET_FONT (return_vals, 1); gimp_value_array_unref (return_vals); @@ -301,7 +300,7 @@ gimp_text_layer_get_font (GimpTextLayer *layer) **/ gboolean gimp_text_layer_set_font (GimpTextLayer *layer, - const gchar *font) + GimpFont *font) { GimpValueArray *args; GimpValueArray *return_vals; @@ -309,7 +308,7 @@ gimp_text_layer_set_font (GimpTextLayer *layer, args = gimp_value_array_new_from_types (NULL, GIMP_TYPE_TEXT_LAYER, layer, - G_TYPE_STRING, font, + GIMP_TYPE_FONT, font, G_TYPE_NONE); return_vals = gimp_pdb_run_procedure_array (gimp_get_pdb (), diff --git a/libgimp/gimptextlayer_pdb.h b/libgimp/gimptextlayer_pdb.h index c41ef8a7b0..46b86c2620 100644 --- a/libgimp/gimptextlayer_pdb.h +++ b/libgimp/gimptextlayer_pdb.h @@ -43,9 +43,9 @@ gboolean gimp_text_layer_set_text (GimpTextLayer gchar* gimp_text_layer_get_markup (GimpTextLayer *layer); gboolean gimp_text_layer_set_markup (GimpTextLayer *layer, const gchar *markup); -gchar* gimp_text_layer_get_font (GimpTextLayer *layer); +GimpFont* gimp_text_layer_get_font (GimpTextLayer *layer); gboolean gimp_text_layer_set_font (GimpTextLayer *layer, - const gchar *font); + GimpFont *font); gdouble gimp_text_layer_get_font_size (GimpTextLayer *layer, GimpUnit *unit); gboolean gimp_text_layer_set_font_size (GimpTextLayer *layer, diff --git a/libgimp/meson.build b/libgimp/meson.build index a1f12f9dd7..bc9727e2b5 100644 --- a/libgimp/meson.build +++ b/libgimp/meson.build @@ -344,6 +344,8 @@ libgimp = library('gimp-'+ gimp_api_version, gegl, gexiv2, drmingw, + pango, + pangoft2, ], sources: [ pdbgen @@ -465,6 +467,7 @@ else 'Gio-2.0', 'GLib-2.0', 'GObject-2.0', + 'Pango-1.0', ] gir_args = [ diff --git a/meson.build b/meson.build index 4d3663097f..4cdddacbe3 100644 --- a/meson.build +++ b/meson.build @@ -413,7 +413,7 @@ endif conf.set_quoted('MYPAINT_BRUSHES_DIR', mypaint_brushes_dir) pango_minver = '1.50.0' -pangocairo = dependency('pango', version: '>='+pango_minver) +pango = dependency('pango', version: '>='+pango_minver) pangocairo = dependency('pangocairo', version: '>='+pango_minver) pangoft2 = dependency('pangoft2', version: '>='+pango_minver) rsvg_minver = '2.40.6' diff --git a/pdb/groups/text_layer.pdb b/pdb/groups/text_layer.pdb index eaa98accee..8b5c91168f 100644 --- a/pdb/groups/text_layer.pdb +++ b/pdb/groups/text_layer.pdb @@ -231,7 +231,7 @@ sub text_layer_get_font { $blurb = 'Get the font from a text layer as string.'; $help = <<'HELP'; -This procedure returns the name of the font from a text layer. +This procedure returns the font from a text layer. HELP &marcus_pdb_misc('2008', '2.6'); @@ -242,20 +242,20 @@ HELP ); @outargs = ( - { name => 'font', type => 'string', + { name => 'font', type => 'font', desc => 'The font which is used in the specified text layer.' } ); %invoke = ( code => <<'CODE' { - GimpFont *font_obj; - g_object_get (gimp_text_layer_get_text (layer), - "font", &font_obj, + "font", &font, NULL); - font = g_strdup (gimp_font_get_lookup_name (font_obj)); - g_object_unref (font_obj); + /* The GimpText keeps a reference. Therefore unref before returning the + * pointer so that we don't leak a reference. + */ + g_object_unref (font); } CODE ); @@ -273,7 +273,7 @@ HELP @inargs = ( { name => 'layer', type => 'text_layer', desc => 'The text layer' }, - { name => 'font', type => 'string', + { name => 'font', type => 'font', desc => 'The new font to use' } ); diff --git a/plug-ins/common/file-pdf-save.c b/plug-ins/common/file-pdf-save.c index 01b0dbd4f8..452284e8bd 100644 --- a/plug-ins/common/file-pdf-save.c +++ b/plug-ins/common/file-pdf-save.c @@ -641,7 +641,7 @@ get_missing_fonts (GList *layers) } else if (gimp_item_is_text_layer (GIMP_ITEM (layer))) { - gchar *font_family; + GimpFont *gimp_font; PangoFontDescription *font_description; PangoFontDescription *font_description2; PangoFontMap *fontmap; @@ -651,8 +651,8 @@ get_missing_fonts (GList *layers) fontmap = pango_cairo_font_map_new_for_font_type (CAIRO_FONT_TYPE_FT); context = pango_font_map_create_context (fontmap); - font_family = gimp_text_layer_get_font (GIMP_TEXT_LAYER (layer)); - font_description = pango_font_description_from_string (font_family); + gimp_font = gimp_text_layer_get_font (GIMP_TEXT_LAYER (layer)); + font_description = gimp_font_get_pango_font_description (gimp_font); font = pango_font_map_load_font (fontmap, context, font_description); font_description2 = pango_font_describe (font); @@ -669,7 +669,6 @@ get_missing_fonts (GList *layers) g_object_unref (font); pango_font_description_free (font_description); pango_font_description_free (font_description2); - g_free (font_family); g_object_unref (context); g_object_unref (fontmap); } @@ -1599,7 +1598,6 @@ drawText (GimpLayer *layer, GimpImageType type = gimp_drawable_type (GIMP_DRAWABLE (layer)); gchar *text = gimp_text_layer_get_text (GIMP_TEXT_LAYER (layer)); gchar *markup = gimp_text_layer_get_markup (GIMP_TEXT_LAYER (layer)); - gchar *font_family; gchar *language; cairo_font_options_t *options; gint x; @@ -1614,6 +1612,7 @@ drawText (GimpLayer *layer, GimpTextDirection dir; PangoLayout *layout; PangoContext *context; + GimpFont *font; PangoFontDescription *font_description; gdouble indent; gdouble line_spacing; @@ -1676,6 +1675,15 @@ drawText (GimpLayer *layer, */ fontmap = pango_cairo_font_map_new_for_font_type (CAIRO_FONT_TYPE_FT); + /* Font */ + font = gimp_text_layer_get_font (GIMP_TEXT_LAYER (layer)); + font_description = gimp_font_get_pango_font_description (font); + + /* This function breaks rendering with some fonts if it's called before + * gimp_font_get_pango_font_description(). I'm still unsure why yet it + * probably means there is a bug somewhere we must fix. Until then, let's make + * sure we keep this order. XXX + */ pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (fontmap), y_res); context = pango_font_map_create_context (fontmap); @@ -1687,7 +1695,7 @@ drawText (GimpLayer *layer, language = gimp_text_layer_get_language (GIMP_TEXT_LAYER (layer)); if (language) pango_context_set_language (context, - pango_language_from_string(language)); + pango_language_from_string (language)); /* Text Direction */ dir = gimp_text_layer_get_base_direction (GIMP_TEXT_LAYER (layer)); @@ -1737,14 +1745,6 @@ drawText (GimpLayer *layer, layout = pango_layout_new (context); pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR); - /* Font */ - font_family = gimp_text_layer_get_font (GIMP_TEXT_LAYER (layer)); - - /* We need to find a way to convert GIMP's returned font name to a - * normal Pango name... Hopefully GIMP 2.8 with Pango will fix it. - */ - font_description = pango_font_description_from_string (font_family); - /* Font Size */ size = gimp_text_layer_get_font_size (GIMP_TEXT_LAYER (layer), &unit); size = gimp_units_to_pixels (size, unit, y_res); @@ -1825,7 +1825,6 @@ drawText (GimpLayer *layer, pango_cairo_show_layout (cr, layout); g_free (text); - g_free (font_family); g_free (language); g_object_unref (layout); diff --git a/plug-ins/common/meson.build b/plug-ins/common/meson.build index 2b10fefac6..f6c65af385 100644 --- a/plug-ins/common/meson.build +++ b/plug-ins/common/meson.build @@ -21,7 +21,7 @@ common_plugins_list = [ { 'name': 'destripe', }, { 'name': 'file-cel', }, { 'name': 'file-compressor', - 'deps': [ gegl, gdk_pixbuf, cairo, gio, liblzma, bz2, zlib, ], + 'deps': [ gegl, gdk_pixbuf, cairo, gio, liblzma, pango, bz2, zlib, ], }, { 'name': 'file-csource', }, { 'name': 'file-desktop-link',