[goffice] GOFormat: tweak bold handling.



commit 0116fbd41a65677040e2bac3e7512b82ebeb4fae
Author: Morten Welinder <terra gnome org>
Date:   Wed Mar 7 14:18:40 2012 -0500

    GOFormat: tweak bold handling.
    
    PANGO_WEIGHT_NORMAL becomes 0
    PANGO_WEIGHT_BOLD becomes 1
    
    We still prevent anything less than or equal to -1 by capping at -0.999
    That's a minor distortion, but it goes away when mapping back to the
    integer PangoWeight.

 ChangeLog                 |    7 +++++++
 NEWS                      |    1 +
 goffice/utils/go-format.c |   36 +++++++++++++++++++++++-------------
 3 files changed, 31 insertions(+), 13 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 74eb51d..de1a7fb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2012-03-07  Morten Welinder  <terra gnome org>
+
+	* goffice/utils/go-format.c (cb_attrs_as_string): Tweak handling
+	of bold so PANGO_WEIGHT_NORMAL becomes 0 and PANGO_WEIGHT_BOLD
+	becomes 1.  PangoWeight->double->PangoWeight still round trips
+	unless you start below 100.
+
 2012-03-06  Morten Welinder  <terra gnome org>
 
 	* goffice/utils/go-image.c (go_image_new_from_data): Make fallback
diff --git a/NEWS b/NEWS
index 0880f23..b45833e 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@ goffice 0.9.3:
 
 Morten:
 	* Fix GOImage fallback for non-GUI case.
+	* Tweak bold attribute for rich text.
 
 --------------------------------------------------------------------------
 goffice 0.9.2:
diff --git a/goffice/utils/go-format.c b/goffice/utils/go-format.c
index 0f55cc9..9622613 100644
--- a/goffice/utils/go-format.c
+++ b/goffice/utils/go-format.c
@@ -5549,11 +5549,11 @@ cb_attrs_as_string (PangoAttribute *a, GString *accum)
 		return FALSE;
 
 	switch (a->klass->type) {
-	case PANGO_ATTR_FAMILY :
+	case PANGO_ATTR_FAMILY:
 		g_string_append_printf (accum, "[family=%s",
 			((PangoAttrString *)a)->value);
 		break;
-	case PANGO_ATTR_SIZE :
+	case PANGO_ATTR_SIZE:
 		g_string_append_printf (accum, "[size=%d",
 			((PangoAttrInt *)a)->value);
 		break;
@@ -5567,23 +5567,29 @@ cb_attrs_as_string (PangoAttribute *a, GString *accum)
 				((PangoAttrFloat *)a)->value);
 		g_string_append (accum, buf);
 		break;
-	case PANGO_ATTR_STYLE :
+	case PANGO_ATTR_STYLE:
 		g_string_append_printf (accum, "[italic=%d",
 			(((PangoAttrInt *)a)->value == PANGO_STYLE_ITALIC) ? 1 : 0);
 		break;
-	case PANGO_ATTR_WEIGHT :
+	case PANGO_ATTR_WEIGHT: {
+		int w = ((PangoAttrInt *)a)->value;
 		/* We are scaling the weight so that earlier versions that used only 0/1 */
 		/* can still read the new formats and we can read the old ones. */
+		const int Z = PANGO_WEIGHT_NORMAL;
+		const double R = PANGO_WEIGHT_BOLD - Z;
+		double d = (w - Z) / R;
+		/* We cap to prevent older versions from seeing -1 or less.  */
+		if (d <= -1) d = -0.999;
 		g_string_append (accum, "[bold=");
-		g_ascii_formatd (buf, sizeof (buf), "%.3f",
-				(((PangoAttrInt *)a)->value - 399.)/300.);
+		g_ascii_formatd (buf, sizeof (buf), "%.3g", d);
 		g_string_append (accum, buf);
 		break;
-	case PANGO_ATTR_STRIKETHROUGH :
+	}
+	case PANGO_ATTR_STRIKETHROUGH:
 		g_string_append_printf (accum, "[strikethrough=%d",
 			((PangoAttrInt *)a)->value ? 1 : 0);
 		break;
-	case PANGO_ATTR_UNDERLINE :
+	case PANGO_ATTR_UNDERLINE:
 		switch (((PangoAttrInt *)a)->value) {
 		case PANGO_UNDERLINE_NONE :
 			g_string_append (accum, "[underline=none");
@@ -5603,14 +5609,14 @@ cb_attrs_as_string (PangoAttribute *a, GString *accum)
 		}
 		break;
 
-	case PANGO_ATTR_FOREGROUND :
+	case PANGO_ATTR_FOREGROUND:
 		c = &((PangoAttrColor *)a)->color;
 		g_string_append_printf (accum, "[color=%02xx%02xx%02x",
 			((c->red & 0xff00) >> 8),
 			((c->green & 0xff00) >> 8),
 			((c->blue & 0xff00) >> 8));
 		break;
-	default :
+	default:
 		if (a->klass->type == go_pango_attr_subscript_get_type ()) {
 			g_string_append_printf (accum, "[subscript=%d",
 						((GOPangoAttrSubscript *)a)->val ?
@@ -5663,9 +5669,13 @@ go_format_parse_markup (char *str)
 		case 4:
 			if (0 == strncmp (str, "size", 4))
 				a = pango_attr_size_new (atoi (val));
-			else if (0 == strncmp (str, "bold", 4))
-				a = pango_attr_weight_new ((int)(g_ascii_strtod (val, NULL) * 300. + 399.));
-			else if (0 == strncmp (str, "rise", 4))
+			else if (0 == strncmp (str, "bold", 4)) {
+				double d = g_ascii_strtod (val, NULL);
+				const int Z = PANGO_WEIGHT_NORMAL;
+				const double R = PANGO_WEIGHT_BOLD - Z;
+				int w = (int)(d * R + Z);
+				a = pango_attr_weight_new (w);
+			} else if (0 == strncmp (str, "rise", 4))
 				a = pango_attr_rise_new (atoi (val));
 			break;
 



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