[gnumeric] GnmFont: simplify and handle context better.



commit 0542b168170688f200ce1b0065e1a25bf9e27595
Author: Morten Welinder <terra gnome org>
Date:   Fri Sep 4 21:46:35 2009 -0400

    GnmFont: simplify and handle context better.

 ChangeLog            |   20 +++++++++++++++
 NEWS                 |    1 +
 src/gnm-style-impl.h |    3 +-
 src/item-edit.c      |    2 +-
 src/mstyle.c         |   19 ++++++++------
 src/mstyle.h         |    3 +-
 src/print.c          |    6 +---
 src/rendered-value.c |   10 ++++----
 src/style-font.h     |    5 +--
 src/style.c          |   64 ++++++++++++++++++++++++++++----------------------
 10 files changed, 81 insertions(+), 52 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 00c51d5..4a5a9b7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,23 @@
+2009-09-04  Morten Welinder  <terra gnome org>
+
+	* src/rendered-value.c (calc_indent): Drop zoom argument.  All
+	callers changed.
+
+	* src/style.c (style_font_new_simple): Put context into hash key
+	as the font metrics depends on that.  Drop scale argument.  Caller
+	changed.
+	(delete_neg_font, gnm_font_unref): Unref ->context.
+	(gnm_font_hash): Take context, is_bold, and is_italic into
+	account.
+
+	* src/mstyle.c (gnm_style_clear_font): Clear font_context here.
+	(gnm_style_dup): Copy font_context here.
+	(gnm_style_get_font): Check font_context, not font_zoom here.
+	Drop zoom argument.  All callers changed.
+
+	* src/gnm-style-impl.h (GnmStyle): Drop font_zoom, but add
+	font_context.
+
 2009-09-03  Morten Welinder  <terra gnome org>
 
 	* src/style-font.h (GnmFont): Remove unused pango.font.
diff --git a/NEWS b/NEWS
index 4e703bc..1428c9f 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,7 @@ Morten:
 	* Fix menu sensitivity problem.  [#593624]
 	* Simplify GnmColor.
 	* Fix grab problem.  [Debian #544975]
+	* Simplify GnmFont.
 
 --------------------------------------------------------------------------
 Gnumeric 1.9.11
diff --git a/src/gnm-style-impl.h b/src/gnm-style-impl.h
index 9201d46..49df7f1 100644
--- a/src/gnm-style-impl.h
+++ b/src/gnm-style-impl.h
@@ -24,7 +24,9 @@ struct _GnmStyle {
 	PangoAttrList *pango_attrs;
 	float          pango_attrs_zoom;
 	int            pango_attrs_height;
+
 	GnmFont       *font;
+	PangoContext  *font_context;
 
 /* public */
 	struct {
@@ -45,7 +47,6 @@ struct _GnmStyle {
 		GOFontScript	script;
 		float		size;
 	} font_detail;
-	float            font_zoom;
 
 	GOFormat *format;
 	GnmHAlign	 h_align;
diff --git a/src/item-edit.c b/src/item-edit.c
index b148f82..d90c34f 100644
--- a/src/item-edit.c
+++ b/src/item-edit.c
@@ -451,7 +451,7 @@ item_edit_set_property (GObject *gobject, guint param_id,
 		ie->style = gnm_style_dup (
 			sheet_style_get (sheet, ie->pos.col, ie->pos.row));
 		ie->gfont = gnm_style_get_font (ie->style,
-			sheet->context, sheet->last_zoom_factor_used);
+						sheet->context);
 		gnm_font_ref (ie->gfont);
 
 		if (gnm_style_get_align_h (ie->style) == HALIGN_GENERAL)
diff --git a/src/mstyle.c b/src/mstyle.c
index 79e1448..475cd54 100644
--- a/src/mstyle.c
+++ b/src/mstyle.c
@@ -491,6 +491,10 @@ gnm_style_clear_font (GnmStyle *style)
 		gnm_font_unref (style->font);
 		style->font = NULL;
 	}
+	if (style->font_context) {
+		g_object_unref (style->font_context);
+		style->font_context = NULL;
+	}
 }
 
 /**
@@ -593,7 +597,7 @@ gnm_style_dup (GnmStyle const *src)
 
 	if ((new_style->font = src->font)) {
 		gnm_font_ref (new_style->font);
-		new_style->font_zoom = src->font_zoom;
+		new_style->font_context = g_object_ref (src->font_context);
 	}
 
 	d(("dup %p\n", new_style));
@@ -1114,14 +1118,14 @@ gnm_style_get_pattern (GnmStyle const *style)
 }
 
 GnmFont *
-gnm_style_get_font (GnmStyle const *style, PangoContext *context, float zoom)
+gnm_style_get_font (GnmStyle const *style, PangoContext *context)
 {
 	g_return_val_if_fail (style != NULL, NULL);
 
-	if (!style->font || style->font_zoom != zoom) {
+	if (!style->font || style->font_context != context) {
 		char const *name;
 		gboolean bold, italic;
-		float size;
+		double size;
 
 		gnm_style_clear_font ((GnmStyle *)style);
 
@@ -1146,9 +1150,8 @@ gnm_style_get_font (GnmStyle const *style, PangoContext *context, float zoom)
 			size = DEFAULT_SIZE;
 
 		((GnmStyle *)style)->font =
-			gnm_font_new (context, name, size,
-				      zoom, bold, italic);
-		((GnmStyle *)style)->font_zoom = zoom;
+			gnm_font_new (context, name, size, bold, italic);
+		((GnmStyle *)style)->font_context = g_object_ref (context);
 	}
 
 	return style->font;
@@ -1693,7 +1696,7 @@ gnm_style_get_pango_attrs (GnmStyle const *style,
 	}
 
 	{
-		GnmFont *font = gnm_style_get_font (style, context, zoom);
+		GnmFont *font = gnm_style_get_font (style, context);
 		add_attr (l, pango_attr_font_desc_new (font->go.font->desc));
 	}
 
diff --git a/src/mstyle.h b/src/mstyle.h
index 4f9e77d..f788da5 100644
--- a/src/mstyle.h
+++ b/src/mstyle.h
@@ -115,8 +115,7 @@ void         gnm_style_set_font_size	   (GnmStyle *style, float size);
 float        gnm_style_get_font_size	   (GnmStyle const *style);
 
 GnmFont     *gnm_style_get_font		   (GnmStyle const *style,
-					    PangoContext *context,
-					    float zoom);
+					    PangoContext *context);
 void         gnm_style_set_format	   (GnmStyle *style, GOFormat const *fmt);
 void         gnm_style_set_format_text	   (GnmStyle *style, char const *fmt);
 GOFormat    *gnm_style_get_format	   (GnmStyle const *style);
diff --git a/src/print.c b/src/print.c
index 5ecfa32..9851fe3 100644
--- a/src/print.c
+++ b/src/print.c
@@ -311,7 +311,7 @@ print_page_row_headers (GtkPrintContext   *context, PrintingInstance * pi,
 }
 
 static PangoLayout *
-ensure_decoration_layout (GtkPrintContext   *context)
+ensure_decoration_layout (GtkPrintContext *context)
 {
 	GnmStyle *style;
 	GnmFont *font;
@@ -320,9 +320,7 @@ ensure_decoration_layout (GtkPrintContext   *context)
 	layout = gtk_print_context_create_pango_layout (context);
 	style = gnm_conf_get_printer_decoration_font ();
 	font = gnm_style_get_font
-		(style,
-		 pango_layout_get_context (layout),
-		 1.);
+		(style, pango_layout_get_context (layout));
 	pango_layout_set_font_description (layout, font->go.font->desc);
 	gnm_style_unref (style);
 
diff --git a/src/rendered-value.c b/src/rendered-value.c
index b88d5d3..c839fa9 100644
--- a/src/rendered-value.c
+++ b/src/rendered-value.c
@@ -71,13 +71,13 @@ static int rv_allocations;
 
 
 static guint16
-calc_indent (PangoContext *context, const GnmStyle *mstyle, double zoom)
+calc_indent (PangoContext *context, const GnmStyle *mstyle)
 {
 	int indent = 0;
 	if (gnm_style_is_element_set (mstyle, MSTYLE_INDENT)) {
 		int n = gnm_style_get_indent (mstyle);
 		if (n) {
-			GnmFont *style_font = gnm_style_get_font (mstyle, context, zoom);
+			GnmFont *style_font = gnm_style_get_font (mstyle, context);
 			indent = PANGO_PIXELS (n * style_font->go.metrics->avg_digit_width);
 		}
 	}
@@ -298,7 +298,7 @@ gnm_rendered_value_new (GnmCell *cell, GnmStyle const *mstyle,
 	res->indent_left = res->indent_right = 0;
 	switch (res->effective_halign) {
 	case HALIGN_LEFT:
-		res->indent_left = calc_indent (context, mstyle, zoom);
+		res->indent_left = calc_indent (context, mstyle);
 		pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
 		break;
 
@@ -322,7 +322,7 @@ gnm_rendered_value_new (GnmCell *cell, GnmStyle const *mstyle,
 		break;
 
 	case HALIGN_RIGHT:
-		res->indent_right = calc_indent (context, mstyle, zoom);
+		res->indent_right = calc_indent (context, mstyle);
 		pango_layout_set_alignment (layout, PANGO_ALIGN_RIGHT);
 		break;
 
@@ -367,7 +367,7 @@ gnm_rendered_value_new (GnmCell *cell, GnmStyle const *mstyle,
 		GODateConventions const *date_conv = sheet->workbook
 			? workbook_date_conv (sheet->workbook)
 			: NULL;
-		GnmFont *font = gnm_style_get_font (mstyle, context, zoom);
+		GnmFont *font = gnm_style_get_font (mstyle, context);
 		gboolean is_rotated = (rotation != 0);
 		gboolean variable;
 		GOFormatNumberError err;
diff --git a/src/style-font.h b/src/style-font.h
index 097b674..13bbb6c 100644
--- a/src/style-font.h
+++ b/src/style-font.h
@@ -12,11 +12,11 @@ struct _GnmFont {
 	int	 ref_count;
 	char	*font_name;
 	double	 size_pts;
-	double	 scale;
 	struct {
 		GOFont const *font;
 		GOFontMetrics *metrics;
 	} go;
+	PangoContext *context;
 
 	unsigned int is_bold : 1;
 	unsigned int is_italic : 1;
@@ -24,8 +24,7 @@ struct _GnmFont {
 
 GnmFont *gnm_font_new   (PangoContext *context,
 			 char const *font_name,
-			 double size_pts, double scale,
-			 gboolean bold, gboolean italic);
+			 double size_pts, gboolean bold, gboolean italic);
 void     gnm_font_ref   (GnmFont *gfont);
 void     gnm_font_unref (GnmFont *gfont);
 guint    gnm_font_hash  (gconstpointer v);
diff --git a/src/style.c b/src/style.c
index a3e374b..34ccdc7 100644
--- a/src/style.c
+++ b/src/style.c
@@ -77,7 +77,7 @@ get_substitute_font (gchar const *fontname)
 
 static GnmFont *
 style_font_new_simple (PangoContext *context,
-		       char const *font_name, double size_pts, double scale,
+		       char const *font_name, double size_pts,
 		       gboolean bold, gboolean italic)
 {
 	GnmFont *font;
@@ -97,7 +97,7 @@ style_font_new_simple (PangoContext *context,
 	key.size_pts  = size_pts;
 	key.is_bold   = bold;
 	key.is_italic = italic;
-	key.scale     = scale;
+	key.context   = context;
 
 	font = (GnmFont *) g_hash_table_lookup (style_font_hash, &key);
 	if (font == NULL) {
@@ -110,13 +110,13 @@ style_font_new_simple (PangoContext *context,
 		font = g_new0 (GnmFont, 1);
 		font->font_name = g_strdup (font_name);
 		font->size_pts  = size_pts;
-		font->scale     = scale;
 		font->is_bold   = bold;
 		font->is_italic = italic;
+		font->context = g_object_ref (context);
 		/* One reference for the cache, one for the caller. */
 		font->ref_count = 2;
 
-		desc = pango_font_description_copy (pango_context_get_font_description (context));
+		desc = pango_font_description_new ();
 
 		pango_font_description_set_family (desc, font_name);
 		pango_font_description_set_weight (desc,
@@ -132,7 +132,7 @@ style_font_new_simple (PangoContext *context,
 			if (sub != NULL) {
 				pango_font_description_set_family (desc, font_name);
 				pango_font = pango_context_load_font (context,
-									    desc);
+								      desc);
 			}
 
 			if (pango_font == NULL) {
@@ -164,7 +164,7 @@ style_font_new_simple (PangoContext *context,
 
 GnmFont *
 gnm_font_new (PangoContext *context,
-	      char const *font_name, double size_pts, double scale,
+	      char const *font_name, double size_pts,
 	      gboolean bold, gboolean italic)
 {
 	GnmFont *font;
@@ -173,27 +173,27 @@ gnm_font_new (PangoContext *context,
 	g_return_val_if_fail (size_pts > 0, NULL);
 
 	font = style_font_new_simple (context, font_name, size_pts,
-				      scale, bold, italic);
+				      bold, italic);
 	if (font) return font;
 
 	font_name = gnumeric_default_font_name;
 	font = style_font_new_simple (context, font_name, size_pts,
-				      scale, bold, italic);
+				      bold, italic);
 	if (font) return font;
 
 	size_pts = gnumeric_default_font_size;
 	font = style_font_new_simple (context, font_name, size_pts,
-				      scale, bold, italic);
+				      bold, italic);
 	if (font) return font;
 
 	bold = FALSE;
 	font = style_font_new_simple (context, font_name, size_pts,
-				      scale, bold, italic);
+				      bold, italic);
 	if (font) return font;
 
 	italic = FALSE;
 	font = style_font_new_simple (context, font_name, size_pts,
-				      scale, bold, italic);
+				      bold, italic);
 	if (font) return font;
 
 	/*
@@ -236,16 +236,25 @@ gnm_font_unref (GnmFont *sf)
 	if (sf->ref_count != 0)
 		return;
 
+	g_hash_table_remove (style_font_hash, sf);
+	/* hash-changing operations after above line.  */
+
 	if (sf->go.font) {
 		go_font_unref (sf->go.font);
 		sf->go.font = NULL;
 	}
+
 	if (sf->go.metrics) {
 		go_font_metrics_free (sf->go.metrics);
 		sf->go.metrics = NULL;
 	}
-	g_hash_table_remove (style_font_hash, sf);
+
+	g_object_unref (sf->context);
+	sf->context = NULL;
+
 	g_free (sf->font_name);
+	sf->font_name = NULL;
+
 	g_free (sf);
 }
 
@@ -255,24 +264,22 @@ gnm_font_equal (gconstpointer v, gconstpointer v2)
 	GnmFont const *k1 = (GnmFont const *) v;
 	GnmFont const *k2 = (GnmFont const *) v2;
 
-	if (k1->size_pts != k2->size_pts)
-		return 0;
-
-	if (k1->is_bold != k2->is_bold)
-		return 0;
-	if (k1->is_italic != k2->is_italic)
-		return 0;
-	if (k1->scale != k2->scale)
-		return 0;
-
-	return !strcmp (k1->font_name, k2->font_name);
+	return (k1->size_pts == k2->size_pts &&
+		k1->is_bold == k2->is_bold &&
+		k1->is_italic == k2->is_italic &&
+		k1->context == k2->context &&
+		strcmp (k1->font_name, k2->font_name) == 0);
 }
+
 guint
 gnm_font_hash (gconstpointer v)
 {
 	GnmFont const *k = (GnmFont const *) v;
-
-	return k->size_pts + g_str_hash (k->font_name);
+	return (guint)k->size_pts ^
+		g_str_hash (k->font_name) ^
+		(k->is_bold ? 0x33333333 : 0) ^
+		(k->is_italic ? 0xcccccccc : 0) ^
+		GPOINTER_TO_UINT (k->context);
 }
 
 static PangoFontMap *fontmap;
@@ -325,12 +332,12 @@ gnm_font_init (void)
 	if (gnumeric_default_font_name && gnumeric_default_font_size >= 1)
 		gnumeric_default_font = style_font_new_simple (context,
 			gnumeric_default_font_name, gnumeric_default_font_size,
-			1., FALSE, FALSE);
+			FALSE, FALSE);
 	if (gnumeric_default_font == NULL) {
 		g_warning ("Configured default font '%s %f' not available, trying fallback...",
 			   gnumeric_default_font_name, gnumeric_default_font_size);
 		gnumeric_default_font = style_font_new_simple (context,
-			DEFAULT_FONT, DEFAULT_SIZE, 1., FALSE, FALSE);
+			DEFAULT_FONT, DEFAULT_SIZE, FALSE, FALSE);
 		if (gnumeric_default_font != NULL) {
 			g_free (gnumeric_default_font_name);
 			gnumeric_default_font_name = g_strdup (DEFAULT_FONT);
@@ -339,7 +346,7 @@ gnm_font_init (void)
 			g_warning ("Fallback font '%s %f' not available, trying 'fixed'...",
 				   DEFAULT_FONT, DEFAULT_SIZE);
 			gnumeric_default_font = style_font_new_simple (context,
-				"fixed", 10, 1., FALSE, FALSE);
+				"fixed", 10, FALSE, FALSE);
 			if (gnumeric_default_font != NULL) {
 				g_free (gnumeric_default_font_name);
 				gnumeric_default_font_name = g_strdup ("fixed");
@@ -361,6 +368,7 @@ gnm_font_init (void)
 static void
 delete_neg_font (GnmFont *sf, gpointer value, gpointer user_data)
 {
+	g_object_unref (sf->context);
 	g_free (sf->font_name);
 	g_free (sf);
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]