Port GimpText to GimpFont

In GimpText, The font used to be stored as a string containing its name,
Now, it is stored as a GimpFont object, which makes more sense and makes
operations on fonts easier (such as serialization).
This commit is contained in:
Idriss Fekir 2023-08-01 21:32:10 +01:00 committed by Jehan
parent b5895aca9a
commit a966297498
10 changed files with 66 additions and 37 deletions

View file

@ -275,6 +275,8 @@ gimp_g_value_get_memsize (GValue *value)
{
if (strcmp ("GimpPattern", G_VALUE_TYPE_NAME (value)) == 0)
memsize += gimp_g_object_get_memsize (g_value_get_object (value));
else if (strcmp ("GimpFont", G_VALUE_TYPE_NAME (value)) == 0)
memsize += gimp_g_object_get_memsize (g_value_get_object (value));
else
g_printerr ("%s: unhandled object value type: %s\n",
G_STRFUNC, G_VALUE_TYPE_NAME (value));

View file

@ -38,6 +38,7 @@
#include "core/gimpcontext.h"
#include "core/gimpimage.h"
#include "core/gimpparamspecs.h"
#include "text/gimpfont.h"
#include "text/gimptext.h"
#include "text/gimptextlayer.h"
@ -260,9 +261,13 @@ text_layer_get_font_invoker (GimpProcedure *procedure,
if (success)
{
GimpFont *font_obj;
g_object_get (gimp_text_layer_get_text (layer),
"font", &font,
"font", &font_obj,
NULL);
font = g_strdup (gimp_font_get_lookup_name (font_obj));
g_object_unref (font_obj);
}
return_vals = gimp_procedure_get_return_values (procedure, success,

View file

@ -42,6 +42,8 @@
#include "core/gimptempbuf.h"
#include "core/gimp-memsize.h"
#include "gimpfont.h"
#include "gimp-intl.h"
@ -106,6 +108,8 @@ static GimpTempBuf * gimp_font_get_new_preview (GimpViewable *viewable,
static const gchar * gimp_font_get_sample_string (PangoContext *context,
PangoFontDescription *font_desc);
static gint64 gimp_font_get_memsize (GimpObject *object,
gint64 *gui_size);
G_DEFINE_TYPE (GimpFont, gimp_font, GIMP_TYPE_DATA)
@ -122,7 +126,7 @@ gboolean
gimp_font_match_by_lookup_name (GimpFont *font,
const gchar *name)
{
return !g_strcmp0 (gimp_font_get_lookup_name (font), name);
return !g_strcmp0 (font->lookup_name, name);
}
const gchar*
@ -136,9 +140,11 @@ gimp_font_class_init (GimpFontClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpViewableClass *viewable_class = GIMP_VIEWABLE_CLASS (klass);
GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
object_class->finalize = gimp_font_finalize;
object_class->set_property = gimp_font_set_property;
gimp_object_class->get_memsize = gimp_font_get_memsize;
viewable_class->get_preview_size = gimp_font_get_preview_size;
viewable_class->get_popup_size = gimp_font_get_popup_size;
@ -192,6 +198,19 @@ gimp_font_set_property (GObject *object,
}
}
static gint64
gimp_font_get_memsize (GimpObject *object,
gint64 *gui_size)
{
GimpFont *font = GIMP_FONT (object);
gint64 memsize = 0;
memsize += gimp_string_get_memsize (font->lookup_name);
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
gui_size);
}
static void
gimp_font_get_preview_size (GimpViewable *viewable,
gint size,
@ -221,7 +240,7 @@ gimp_font_get_popup_size (GimpViewable *viewable,
if (! font->pango_context)
return FALSE;
name = gimp_font_get_lookup_name (font);
name = font->lookup_name;
font_desc = pango_font_description_from_string (name);
g_return_val_if_fail (font_desc != NULL, FALSE);
@ -276,7 +295,7 @@ gimp_font_get_new_preview (GimpViewable *viewable,
PangoFontDescription *font_desc;
const gchar *name;
name = gimp_font_get_lookup_name (font);
name = font->lookup_name;
DEBUGPRINT (("%s: ", name));

View file

@ -34,6 +34,8 @@
#include "text-types.h"
#include "gimpfont.h"
#include "core/gimp.h"
#include "core/gimp-memsize.h"
#include "core/gimp-utils.h"
@ -176,11 +178,9 @@ gimp_text_class_init (GimpTextClass *klass)
NULL,
GIMP_PARAM_STATIC_STRINGS);
GIMP_CONFIG_PROP_STRING (object_class, PROP_FONT,
"font",
NULL, NULL,
"Sans-serif",
GIMP_PARAM_STATIC_STRINGS);
GIMP_CONFIG_PROP_FONT (object_class, PROP_FONT,
"font", NULL, NULL,
GIMP_CONFIG_PARAM_FLAGS);
GIMP_CONFIG_PROP_DOUBLE (object_class, PROP_FONT_SIZE,
"font-size",
@ -415,8 +415,8 @@ gimp_text_finalize (GObject *object)
g_clear_pointer (&text->text, g_free);
g_clear_pointer (&text->markup, g_free);
g_clear_pointer (&text->font, g_free);
g_clear_pointer (&text->language, g_free);
g_clear_object (&text->font);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
@ -438,7 +438,7 @@ gimp_text_get_property (GObject *object,
g_value_set_string (value, text->markup);
break;
case PROP_FONT:
g_value_set_string (value, text->font);
g_value_set_object (value, text->font);
break;
case PROP_FONT_SIZE:
g_value_set_double (value, text->font_size);
@ -580,23 +580,10 @@ gimp_text_set_property (GObject *object,
break;
case PROP_FONT:
{
const gchar *font = g_value_get_string (value);
GimpFont *font = g_value_get_object (value);
g_free (text->font);
if (font)
{
gsize len = strlen (font);
if (g_str_has_suffix (font, " Not-Rotated"))
len -= strlen ( " Not-Rotated");
text->font = g_strndup (font, len);
}
else
{
text->font = NULL;
}
if (font != text->font)
g_set_object (&text->font, font);
}
break;
case PROP_FONT_SIZE:
@ -748,7 +735,6 @@ gimp_text_get_memsize (GimpObject *object,
memsize += gimp_string_get_memsize (text->text);
memsize += gimp_string_get_memsize (text->markup);
memsize += gimp_string_get_memsize (text->font);
memsize += gimp_string_get_memsize (text->language);
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,

View file

@ -41,7 +41,7 @@ struct _GimpText
gchar *text;
gchar *markup;
gchar *font;
GimpFont *font;
GimpUnit unit;
gdouble font_size;
gboolean antialias;

View file

@ -34,6 +34,8 @@
#include "core/gimperror.h"
#include "gimpfont.h"
#include "gimptext.h"
#include "gimptextlayout.h"
@ -116,7 +118,7 @@ gimp_text_layout_new (GimpText *text,
g_return_val_if_fail (GIMP_IS_TEXT (text), NULL);
font_desc = pango_font_description_from_string (text->font);
font_desc = pango_font_description_from_string (gimp_font_get_lookup_name (text->font));
g_return_val_if_fail (font_desc != NULL, NULL);
size = pango_units_from_double (gimp_units_to_points (text->font_size,

View file

@ -41,6 +41,8 @@
#include "text/gimptext.h"
#include "text/gimpfont.h"
#include "widgets/gimpcolorpanel.h"
#include "widgets/gimpmenufactory.h"
#include "widgets/gimppropwidgets.h"
@ -648,11 +650,14 @@ gimp_text_options_notify_font (GimpContext *context,
GParamSpec *pspec,
GimpText *text)
{
if (gimp_context_get_font (context) == text->font)
return;
g_signal_handlers_block_by_func (text,
gimp_text_options_notify_text_font,
context);
g_object_set (text, "font", gimp_context_get_font_name (context), NULL);
g_object_set (text, "font", gimp_context_get_font (context), NULL);
g_signal_handlers_unblock_by_func (text,
gimp_text_options_notify_text_font,
@ -667,7 +672,7 @@ gimp_text_options_notify_text_font (GimpText *text,
g_signal_handlers_block_by_func (context,
gimp_text_options_notify_font, text);
gimp_context_set_font_name (context, text->font);
gimp_context_set_font (context, text->font);
g_signal_handlers_unblock_by_func (context,
gimp_text_options_notify_font, text);
@ -728,7 +733,7 @@ gimp_text_options_connect_text (GimpTextOptions *options,
g_object_set (text,
"color", &color,
"font", gimp_context_get_font_name (context),
"font", gimp_context_get_font (context),
NULL);
gimp_config_connect (G_OBJECT (options), G_OBJECT (text), NULL);

View file

@ -723,7 +723,7 @@ gimp_text_style_editor_set_default_font (GimpTextStyleEditor *editor)
gimp_text_style_editor_font_changed,
editor);
gimp_context_set_font_name (editor->context, editor->text->font);
gimp_context_set_font (editor->context, editor->text->font);
g_signal_handlers_unblock_by_func (editor->context,
gimp_text_style_editor_font_changed,

View file

@ -182,6 +182,11 @@ G_BEGIN_DECLS
default,\
flags | GIMP_CONFIG_PARAM_FLAGS))
#define GIMP_CONFIG_PROP_FONT(class, id, name, nick, blurb, flags) \
g_object_class_install_property (class, id,\
g_param_spec_object (name, nick, blurb,\
GIMP_TYPE_FONT,\
flags | GIMP_CONFIG_PARAM_FLAGS))
/* object, boxed and pointer properties are _not_ G_PARAM_CONSTRUCT */

View file

@ -249,9 +249,13 @@ HELP
%invoke = (
code => <<'CODE'
{
GimpFont *font_obj;
g_object_get (gimp_text_layer_get_text (layer),
"font", &font,
"font", &font_obj,
NULL);
font = g_strdup (gimp_font_get_lookup_name (font_obj));
g_object_unref (font_obj);
}
CODE
);
@ -990,6 +994,7 @@ CODE
@headers = qw(<pango/pango.h>
"libgimpbase/gimpbase.h"
"core/gimpcontext.h"
"text/gimpfont.h"
"text/gimptext.h"
"text/gimptextlayer.h"
"gimppdb-utils.h"