From 8e97736527156d22fa17f93f82072346efcd0daf Mon Sep 17 00:00:00 2001
From: erg <devnull@localhost>
Date: Thu, 8 Nov 2007 19:42:15 +0000
Subject: [PATCH] Add code so that, for a given font, fontconfig is always
 called with the same string.

---
 lib/common/fontmetrics.c          | 43 +++++++++++++++++++++++++++++++
 plugin/gd/gvrender_gd.c           | 12 ++++++++-
 plugin/gd/gvtextlayout_gd.c       |  7 ++++-
 plugin/pango/gvtextlayout_pango.c | 21 +++------------
 4 files changed, 63 insertions(+), 20 deletions(-)

diff --git a/lib/common/fontmetrics.c b/lib/common/fontmetrics.c
index 981b4eb93..f126569f0 100644
--- a/lib/common/fontmetrics.c
+++ b/lib/common/fontmetrics.c
@@ -219,3 +219,46 @@ pointf textsize(graph_t *g, textpara_t * para, char *fontname, double fontsize)
     size.y = para->height;
     return size;
 }
+
+/* psfontResolve:
+ * Construct alias for postscript fontname.
+ * NB. Uses a static array - non-reentrant.
+ */ 
+
+#define ADD_ATTR(a) \
+  if (a) { \
+	strcat(buf, comma ? " " : ", "); \
+	comma = 1; \
+	strcat(buf, a); \
+  }
+
+char* psfontResolve (PostscriptAlias* pa)
+{
+    static char buf[1024];
+    int comma=0;
+    strcpy(buf, pa->family);
+
+    ADD_ATTR(pa->weight);
+    ADD_ATTR(pa->stretch);
+    ADD_ATTR(pa->style);
+/*
+    if (pa->weight) {
+	strcat(buf, comma ? " " : ", ");
+	comma = 1;
+	strcat(buf, pa->weight);
+    }
+    if (pa->stretch) {
+	strcat(buf, comma ? " " : ", ");
+	comma = 1;
+	strcat(buf, pa->stretch);
+    }
+    if (pa->style) {
+                strcat(buf, comma ? " " : ", ");
+                comma = 1;
+                strcat(buf, pa->style);
+    }
+*/
+
+    return buf;
+}
+
diff --git a/plugin/gd/gvrender_gd.c b/plugin/gd/gvrender_gd.c
index 7b7fc7bae..d648c049a 100644
--- a/plugin/gd/gvrender_gd.c
+++ b/plugin/gd/gvrender_gd.c
@@ -333,11 +333,14 @@ void gdgen_text(gdImagePtr im, pointf spf, pointf epf, int fontcolor, double fon
     }
 }
 
+extern char* psfontResolve (PostscriptAlias* pa);
+
 static void gdgen_textpara(GVJ_t * job, pointf p, textpara_t * para)
 {
     gdImagePtr im = (gdImagePtr) job->context;
     pointf spf, epf;
     double parawidth = para->width * job->scale.x;
+    char* fontname;
 
     if (!im)
 	return;
@@ -367,12 +370,19 @@ static void gdgen_textpara(GVJ_t * job, pointf p, textpara_t * para)
 	epf.y = spf.y = p.y - para->yoffset_centerline * job->scale.y;
     }
 
+#ifdef HAVE_GD_FONTCONFIG
+    if (para->postscript_alias)
+	fontname = psfontResolve (para->postscript_alias);
+    else
+#endif
+	fontname = para->fontname;
+
     gdgen_text(im, spf, epf,
             job->obj->pencolor.u.index,
             para->fontsize,
             ROUND(POINTS_PER_INCH * job->scale.x),
             job->rotation ? (PI / 2) : 0,
-            para->fontname,
+            fontname,
             para->str);
 }
 
diff --git a/plugin/gd/gvtextlayout_gd.c b/plugin/gd/gvtextlayout_gd.c
index f00560633..09abb4d9b 100644
--- a/plugin/gd/gvtextlayout_gd.c
+++ b/plugin/gd/gvtextlayout_gd.c
@@ -100,6 +100,8 @@ char *gd_alternate_fontlist(char *font)
 }
 #endif				/* HAVE_GD_FONTCONFIG */
 
+extern char* psfontResolve (PostscriptAlias* pa);
+
 static boolean gd_textlayout(textpara_t * para, char **fontpath)
 {
     char *err;
@@ -139,7 +141,10 @@ static boolean gd_textlayout(textpara_t * para, char **fontpath)
 	}
 	/* call gdImageStringFT with null *im to get brect and to set font cache */
 #ifdef HAVE_GD_FONTCONFIG
-	fontlist = para->fontname;
+	if (para->postscript_alias)
+	    fontlist = psfontResolve (para->postscript_alias);
+	else
+	    fontlist = para->fontname;
 #else
 	fontlist = gd_alternate_fontlist(para->fontname);
 #endif
diff --git a/plugin/pango/gvtextlayout_pango.c b/plugin/pango/gvtextlayout_pango.c
index a3047917b..b89da5771 100644
--- a/plugin/pango/gvtextlayout_pango.c
+++ b/plugin/pango/gvtextlayout_pango.c
@@ -35,6 +35,8 @@ static void pango_free_layout (void *layout)
 
 #define FONT_DPI 96.
 
+extern char* psfontResolve (PostscriptAlias* pa);
+
 static boolean pango_textlayout(textpara_t * para, char **fontpath)
 {
     static char buf[1024];  /* returned in fontpath, only good until next call */
@@ -69,24 +71,7 @@ static boolean pango_textlayout(textpara_t * para, char **fontpath)
 	pango_font_description_free (desc);
 
 	if (para->postscript_alias) {
-	    int comma=0;
-	    strcpy(buf, para->postscript_alias->family);
-	    if (para->postscript_alias->weight) {
-		strcat(buf, comma ? " " : ", ");
-		comma = 1;
-		strcat(buf, para->postscript_alias->weight);
-	    }
-	    if (para->postscript_alias->stretch) {
-		strcat(buf, comma ? " " : ", ");
-		comma = 1;
-		strcat(buf, para->postscript_alias->stretch);
-	    }
-	    if (para->postscript_alias->style) {
-		strcat(buf, comma ? " " : ", ");
-		comma = 1;
-		strcat(buf, para->postscript_alias->style);
-	    }
-	    fnt = buf;
+	    fnt = psfontResolve (para->postscript_alias);
 	}
 	else
 	    fnt = fontname;
-- 
2.40.0