]> granicus.if.org Git - graphviz/commitdiff
improve postscript fontname translation by use of bsearch() and caching prev result.
authorellson <devnull@localhost>
Tue, 25 Jul 2006 17:52:56 +0000 (17:52 +0000)
committerellson <devnull@localhost>
Tue, 25 Jul 2006 17:52:56 +0000 (17:52 +0000)
lib/common/Makefile.am
lib/common/colxlate.c
lib/common/fontmetrics.c
lib/common/ps_font_equiv.txt [new file with mode: 0644]

index e27043d307408929e7ca2360c5acad0de5c6acd4..f0b5c1dae52d544be63dff87947f47a62b14807d 100644 (file)
@@ -26,7 +26,13 @@ libcommon_C_la_SOURCES = arrows.c colxlate.c fontmetrics.c \
        args.c memory.c globals.c htmllex.c htmlparse.y htmltable.c input.c \
        pointset.c postproc.c routespl.c splines.c psusershape.c \
        timing.c labels.c ns.c shapes.c utils.c geom.c \
-       output.c emit.c ps.txt color_names $(CODEGENS)
+       output.c emit.c ps.txt ps_font_equiv.txt color_names $(CODEGENS)
+
+fontmetrics.o fontmetrics.lo: ps_font_equiv.h
+
+# ensure font names are properly sorted for bsearch operation 
+ps_font_equiv.h: ps_font_equiv.txt
+       LC_COLLATE=C $(SORT) <ps_font_equiv.txt >ps_font_equiv.h
 
 utils.o utils.lo : ps.h
 
@@ -38,6 +44,7 @@ colxlate.o colxlate.lo : colortbl.h
 colortbl.h : color_lib
        $(AWK) -f $(top_srcdir)/awk/colortbl.awk color_lib > colortbl.h
 
+# ensure color names are properly sorted for bsearch operation 
 color_lib : brewer_lib $(top_srcdir)/lib/common/color_names
        cat brewer_lib $(top_srcdir)/lib/common/color_names | LC_COLLATE=C $(SORT) > color_lib
 
@@ -56,9 +63,10 @@ htmlparse.c: y.output
 htmlparse.h: y.output
        @SED@ "s/yy/html/g" < y.tab.h > htmlparse.h
 
-DISTCLEANFILES = brewer_lib color_lib colortbl.h y.output y.tab.[ch] htmlparse.[ch]
+DISTCLEANFILES = brewer_lib color_lib colortbl.h ps_font_equiv.h ps.h \
+       y.output y.tab.[ch] htmlparse.[ch]
 
-EXTRA_DIST = Makefile.old README.imap chars.tcl ps.h \
+EXTRA_DIST = Makefile.old README.imap chars.tcl ps.h ps_font_equiv.h \
        strcasecmp.c strncasecmp.c htmlparse.c htmlparse.h \
        y.tab.c y.tab.h y.output entities.html entities.tcl \
        brewer_colors brewer_lib color_names color_lib colortbl.h
index 822265bb4664aeb65839e9736244fb6566e73924..e8c74f8277be05db29d81ee85201582e5e5872fb 100644 (file)
@@ -128,11 +128,7 @@ static void rgb2cmyk(double r, double g, double b, double *c, double *m,
 
 static int colorcmpf(const void *p0, const void *p1)
 {
-    /* fast comparison of first character */
-    int i = (((hsvrgbacolor_t *) p0)->name[0] - ((hsvrgbacolor_t *) p1)->name[0]);
-    /* if first character matches then compare full color name */
-    return (i ? i :
-           strcmp(((hsvrgbacolor_t *) p0)->name, ((hsvrgbacolor_t *) p1)->name));
+    return strcasecmp(((hsvrgbacolor_t *) p0)->name, ((hsvrgbacolor_t *) p1)->name);
 }
 
 char *canontoken(char *str)
index 470410fc8bc1ec83072616096dc011a72826568d..1361cb03522b22b22f2df912386accec919b52af 100644 (file)
@@ -162,67 +162,41 @@ estimate_textlayout(graph_t *g, textpara_t * para, char **fontpath)
 
 typedef struct _PostscriptAlias {
     char* name;
-    char* string_desc;
+    char* translated_name;
 } PostscriptAlias;
 
-/* This table maps standard Postscript font names to URW Type 1 fonts */
+/*
+ * This table maps standard Postscript font names to URW Type 1 fonts.
+ *
+ *   The original source is in ps_font_equiv.txt.  This is sorted 
+ *   during make into ps_font_equiv.h to ensure that it is in the right
+ *   order for bsearch()
+ */
 static PostscriptAlias postscript_alias[] = {
-    { "AvantGarde-Book", "URW Gothic L, Book" },
-    { "AvantGarde-BookOblique", "URW Gothic L, Book, Oblique" },
-    { "AvantGarde-Demi", "URW Gothic L, Demi" },
-    { "AvantGarde-DemiOblique", "URW Gothic L, Demi, Oblique" },
-
-    { "Bookman-Demi", "URW Bookman L, Demi, Bold" },
-    { "Bookman-DemiItalic", "URW Bookman L, Demi, Bold, Italic" },
-    { "Bookman-Light", "URW Bookman L, Light" },
-    { "Bookman-LightItalic", "URW Bookman L, Light, Italic" },
-
-    { "Courier", "Nimbus Mono L, Regular" },
-    { "Courier-Oblique", "Nimbus Mono L, Regular, Oblique" },
-    { "Courier-Bold", "Nimbus Mono L, Bold" },
-    { "Courier-BoldOblique", "Nimbus Mono L, Bold, Oblique" },
-
-    { "Helvetica", "Nimbus Sans L, Regular" },
-    { "Helvetica-Oblique", "Nimbus Sans L, Regular, Italic" },
-    { "Helvetica-Bold", "Nimbus Sans L, Bold" },
-    { "Helvetica-BoldOblique", "Nimbus Sans L, Bold, Italic" },
-
-    { "Helvetica-Narrow", "Nimbus Sans L, Regular, Condensed" },
-    { "Helvetica-Narrow-Oblique", "Nimbus Sans L, Regular, Condensed, Italic" },
-    { "Helvetica-Narrow-Bold", "Nimbus Sans L, Bold, Condensed" },
-    { "Helvetica-Narrow-BoldOblique", "Nimbus Sans L, Bold, Condensed, Italic" },
-
-    { "NewCenturySchlbk-Roman", "Century Schoolbook L, Roman" },
-    { "NewCenturySchlbk-Italic", "Century Schoolbook L, Italic" },
-    { "NewCenturySchlbk-Bold", "Century Schoolbook L, Bold" },
-    { "NewCenturySchlbk-BoldItalic", "Century Schoolbook L, Bold, Italic" },
-
-    { "Palatino-Roman", "URW Palladio L, Roman" },
-    { "Palatino-Italic", "URW Palladio L, Italic" },
-    { "Palatino-Bold", "URW Palladio L, Bold" },
-    { "Palatino-BoldItalic", "URW Palladio L, Bold, Italic" },
-
-    { "Symbol", "Standard Symbols L, Regular" },
-
-    { "Times-Roman", "Nimbus Roman No9 L, Regular" },
-    { "Times-Italic", "Nimbus Roman No9 L, Regular, Italic" },
-    { "Times-Bold", "Nimbus Roman No9 L, Medium" },
-    { "Times-BoldItalic", "Nimbus Roman No9 L, Medium, Italic" },
-
-    { "ZapfChancery-MediumItalic", "URW Chancery L, Medium, Italic" },
-    { "ZapfDingbats", "Dingbats" },
+#include "ps_font_equiv.h"
 };
 
-static char* translate_postscript_fontname(char* fontname)
+static int fontcmpf(const void *a, const void *b)
 {
-  int i;
+    return (strcasecmp(((PostscriptAlias*)a)->name, ((PostscriptAlias*)b)->name));
+}
 
-  for (i = 0; i < sizeof(postscript_alias)/sizeof(*postscript_alias); i++) {
-    if (strcmp(fontname, postscript_alias[i].name) == 0) {
-        return postscript_alias[i].string_desc;
+static char* translate_postscript_fontname(char* fontname)
+{
+    static PostscriptAlias key;
+    static PostscriptAlias *result;
+
+    if (key.name == NULL || strcasecmp(key.name, fontname)) {
+        key.name = fontname;
+        result = (PostscriptAlias *) bsearch((void *) &key,
+                       (void *) postscript_alias,
+                       sizeof(postscript_alias) / sizeof(PostscriptAlias),
+                       sizeof(PostscriptAlias),
+                        fontcmpf);
     }
-  }
-  return NULL;
+    if (result == NULL)
+       return NULL;
+    return result->translated_name;
 }
 
 pointf textsize(graph_t *g, textpara_t * para, char *fontname, double fontsize)
diff --git a/lib/common/ps_font_equiv.txt b/lib/common/ps_font_equiv.txt
new file mode 100644 (file)
index 0000000..52c22bb
--- /dev/null
@@ -0,0 +1,35 @@
+    { "AvantGarde-Book", "URW Gothic L, Book" },
+    { "AvantGarde-BookOblique", "URW Gothic L, Book, Oblique" },
+    { "AvantGarde-Demi", "URW Gothic L, Demi" },
+    { "AvantGarde-DemiOblique", "URW Gothic L, Demi, Oblique" },
+    { "Bookman-Demi", "URW Bookman L, Demi, Bold" },
+    { "Bookman-DemiItalic", "URW Bookman L, Demi, Bold, Italic" },
+    { "Bookman-Light", "URW Bookman L, Light" },
+    { "Bookman-LightItalic", "URW Bookman L, Light, Italic" },
+    { "Courier", "Nimbus Mono L, Regular" },
+    { "Courier-Bold", "Nimbus Mono L, Bold" },
+    { "Courier-BoldOblique", "Nimbus Mono L, Bold, Oblique" },
+    { "Courier-Oblique", "Nimbus Mono L, Regular, Oblique" },
+    { "Helvetica", "Nimbus Sans L, Regular" },
+    { "Helvetica-Bold", "Nimbus Sans L, Bold" },
+    { "Helvetica-BoldOblique", "Nimbus Sans L, Bold, Italic" },
+    { "Helvetica-Narrow", "Nimbus Sans L, Regular, Condensed" },
+    { "Helvetica-Narrow-Bold", "Nimbus Sans L, Bold, Condensed" },
+    { "Helvetica-Narrow-BoldOblique", "Nimbus Sans L, Bold, Condensed, Italic" },
+    { "Helvetica-Narrow-Oblique", "Nimbus Sans L, Regular, Condensed, Italic" },
+    { "Helvetica-Oblique", "Nimbus Sans L, Regular, Italic" },
+    { "NewCenturySchlbk-Bold", "Century Schoolbook L, Bold" },
+    { "NewCenturySchlbk-BoldItalic", "Century Schoolbook L, Bold, Italic" },
+    { "NewCenturySchlbk-Italic", "Century Schoolbook L, Italic" },
+    { "NewCenturySchlbk-Roman", "Century Schoolbook L, Roman" },
+    { "Palatino-Bold", "URW Palladio L, Bold" },
+    { "Palatino-BoldItalic", "URW Palladio L, Bold, Italic" },
+    { "Palatino-Italic", "URW Palladio L, Italic" },
+    { "Palatino-Roman", "URW Palladio L, Roman" },
+    { "Symbol", "Standard Symbols L, Regular" },
+    { "Times-Bold", "Nimbus Roman No9 L, Medium" },
+    { "Times-BoldItalic", "Nimbus Roman No9 L, Medium, Italic" },
+    { "Times-Italic", "Nimbus Roman No9 L, Regular, Italic" },
+    { "Times-Roman", "Nimbus Roman No9 L, Regular" },
+    { "ZapfChancery-MediumItalic", "URW Chancery L, Medium, Italic" },
+    { "ZapfDingbats", "Dingbats" },