/misc/src/release/graphviz-2.18-1/src/graphviz-2.18/lib/common/fontmetrics.c

Go to the documentation of this file.
00001 /* $Id: fontmetrics.c,v 1.42 2008/03/07 19:09:56 ellson Exp $ $Revision: 1.42 $ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /**********************************************************
00005 *      This software is part of the graphviz package      *
00006 *                http://www.graphviz.org/                 *
00007 *                                                         *
00008 *            Copyright (c) 1994-2004 AT&T Corp.           *
00009 *                and is licensed under the                *
00010 *            Common Public License, Version 1.0           *
00011 *                      by AT&T Corp.                      *
00012 *                                                         *
00013 *        Information and Software Systems Research        *
00014 *              AT&T Research, Florham Park NJ             *
00015 **********************************************************/
00016 
00017 #include "render.h"
00018 
00019 static double timesFontWidth[] = {
00020     0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500,     /*          */
00021     0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500,     /*          */
00022     0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500,     /*          */
00023     0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500,     /*          */
00024     0.2500, 0.3329, 0.4079, 0.5000, 0.5000, 0.8329, 0.7779, 0.3329,     /*  !"#$%&' */
00025     0.3329, 0.3329, 0.5000, 0.5639, 0.2500, 0.3329, 0.2500, 0.2779,     /* ()*+,-./ */
00026     0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000, 0.5000,     /* 01234567 */
00027     0.5000, 0.5000, 0.2779, 0.2779, 0.5639, 0.5639, 0.5639, 0.4439,     /* 89:;<=>? */
00028     0.9209, 0.7219, 0.6669, 0.6669, 0.7219, 0.6109, 0.5559, 0.7219,     /* @ABCDEFG */
00029     0.7219, 0.3329, 0.3889, 0.7219, 0.6109, 0.8889, 0.7219, 0.7219,     /* HIJKLMNO */
00030     0.5559, 0.7219, 0.6669, 0.5559, 0.6109, 0.7219, 0.7219, 0.9439,     /* PQRSTUVW */
00031     0.7219, 0.7219, 0.6109, 0.3329, 0.2779, 0.3329, 0.4689, 0.5000,     /* XYZ[\]^_ */
00032     0.3329, 0.4439, 0.5000, 0.4439, 0.5000, 0.4439, 0.3329, 0.5000,     /* `abcdefg */
00033     0.5000, 0.2779, 0.2779, 0.5000, 0.2779, 0.7779, 0.5000, 0.5000,     /* hijklmno */
00034     0.5000, 0.5000, 0.3329, 0.3889, 0.2779, 0.5000, 0.5000, 0.7219,     /* pqrstuvw */
00035     0.5000, 0.5000, 0.4439, 0.4799, 0.1999, 0.4799, 0.5409, 0.2500,     /* xyz{|}~  */
00036     0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500,     /*          */
00037     0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500,     /*          */
00038     0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500,     /*          */
00039     0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500,     /*          */
00040     0.2500, 0.3329, 0.5000, 0.5000, 0.1669, 0.5000, 0.5000, 0.5000,     /*  ¡¢£¤¥¦§ */
00041     0.5000, 0.1799, 0.4439, 0.5000, 0.3329, 0.3329, 0.5559, 0.5559,     /* ¨©ª«¬­®¯ */
00042     0.2500, 0.5000, 0.5000, 0.5000, 0.2500, 0.2500, 0.4529, 0.3499,     /*  ±²³´ ¶· */
00043     0.3329, 0.4439, 0.4439, 0.5000, 1.0000, 1.0000, 0.2500, 0.4439,     /* ¸¹º»¼½ ¿ */
00044     0.2500, 0.3329, 0.3329, 0.3329, 0.3329, 0.3329, 0.3329, 0.3329,     /*  ÁÂÃÄÅÆÇ */
00045     0.3329, 0.2500, 0.3329, 0.3329, 0.2500, 0.3329, 0.3329, 0.3329,     /* È ÊË ÍÎÏ */
00046     1.0000, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500,     /* Ð        */
00047     0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500, 0.2500,     /*          */
00048     0.2500, 0.8889, 0.2500, 0.2759, 0.2500, 0.2500, 0.2500, 0.2500,     /*  á ã     */
00049     0.6109, 0.7219, 0.8889, 0.3099, 0.2500, 0.2500, 0.2500, 0.2500,     /* èéêë     */
00050     0.2500, 0.6669, 0.2500, 0.2500, 0.2500, 0.2779, 0.2500, 0.2500,     /*  ñ   õ   */
00051     0.2779, 0.5000, 0.7219, 0.5000, 0.2500, 0.2500, 0.2500, 0.2500,     /* øùúû     */
00052 };
00053 static double arialFontWidth[] = {
00054     0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /*          */
00055     0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /*          */
00056     0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /*          */
00057     0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /*          */
00058     0.2779, 0.2779, 0.3549, 0.5559, 0.5559, 0.8889, 0.6669, 0.2209,     /*  !"#$%&' */
00059     0.3329, 0.3329, 0.3889, 0.5839, 0.2779, 0.3329, 0.2779, 0.2779,     /* ()*+,-./ */
00060     0.5559, 0.5559, 0.5559, 0.5559, 0.5559, 0.5559, 0.5559, 0.5559,     /* 01234567 */
00061     0.5559, 0.5559, 0.2779, 0.2779, 0.5839, 0.5839, 0.5839, 0.5559,     /* 89:;<=>? */
00062     1.0149, 0.6669, 0.6669, 0.7219, 0.7219, 0.6669, 0.6109, 0.7779,     /* @ABCDEFG */
00063     0.7219, 0.2779, 0.5000, 0.6669, 0.5559, 0.8329, 0.7219, 0.7779,     /* HIJKLMNO */
00064     0.6669, 0.7779, 0.7219, 0.6669, 0.6109, 0.7219, 0.6669, 0.9439,     /* PQRSTUVW */
00065     0.6669, 0.6669, 0.6109, 0.2779, 0.2779, 0.2779, 0.4689, 0.5559,     /* XYZ[\]^_ */
00066     0.2219, 0.5559, 0.5559, 0.5000, 0.5559, 0.5559, 0.2779, 0.5559,     /* `abcdefg */
00067     0.5559, 0.2219, 0.2219, 0.5000, 0.2219, 0.8329, 0.5559, 0.5559,     /* hijklmno */
00068     0.5559, 0.5559, 0.3329, 0.5000, 0.2779, 0.5559, 0.5000, 0.7219,     /* pqrstuvw */
00069     0.5000, 0.5000, 0.5000, 0.3339, 0.2599, 0.3339, 0.5839, 0.2779,     /* xyz{|}~  */
00070     0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /*          */
00071     0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /*          */
00072     0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /*          */
00073     0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /*          */
00074     0.2779, 0.3329, 0.5559, 0.5559, 0.1669, 0.5559, 0.5559, 0.5559,     /*  ¡¢£¤¥¦§ */
00075     0.5559, 0.1909, 0.3329, 0.5559, 0.3329, 0.3329, 0.5000, 0.5000,     /* ¨©ª«¬­®¯ */
00076     0.2779, 0.5559, 0.5559, 0.5559, 0.2779, 0.2779, 0.5369, 0.3499,     /*  ±²³´ ¶· */
00077     0.2219, 0.3329, 0.3329, 0.5559, 1.0000, 1.0000, 0.2779, 0.6109,     /* ¸¹º»¼½ ¿ */
00078     0.2779, 0.3329, 0.3329, 0.3329, 0.3329, 0.3329, 0.3329, 0.3329,     /*  ÁÂÃÄÅÆÇ */
00079     0.3329, 0.2779, 0.3329, 0.3329, 0.2779, 0.3329, 0.3329, 0.3329,     /* È ÊË ÍÎÏ */
00080     1.0000, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /* Ð        */
00081     0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /*          */
00082     0.2779, 1.0000, 0.2779, 0.3699, 0.2779, 0.2779, 0.2779, 0.2779,     /*  á ã     */
00083     0.5559, 0.7779, 1.0000, 0.3649, 0.2779, 0.2779, 0.2779, 0.2779,     /* èéêë     */
00084     0.2779, 0.8889, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779, 0.2779,     /*  ñ   õ   */
00085     0.2219, 0.6109, 0.9439, 0.6109, 0.2779, 0.2779, 0.2779, 0.2779,     /* øùúû     */
00086 };
00087 static double courFontWidth[] = {
00088     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*          */
00089     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*          */
00090     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*          */
00091     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*          */
00092     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*  !"#$%&' */
00093     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* ()*+,-./ */
00094     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* 01234567 */
00095     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* 89:;<=>? */
00096     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* @ABCDEFG */
00097     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* HIJKLMNO */
00098     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* PQRSTUVW */
00099     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* XYZ[\]^_ */
00100     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* `abcdefg */
00101     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* hijklmno */
00102     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* pqrstuvw */
00103     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* xyz{|}~  */
00104     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*          */
00105     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*          */
00106     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*          */
00107     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*          */
00108     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*  ¡¢£¤¥¦§ */
00109     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* ¨©ª«¬­®¯ */
00110     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*  ±²³´ ¶· */
00111     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* ¸¹º»¼½ ¿ */
00112     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*  ÁÂÃÄÅÆÇ */
00113     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* È ÊË ÍÎÏ */
00114     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* Ð        */
00115     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*          */
00116     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*  á ã     */
00117     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* èéêë     */
00118     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /*  ñ   õ   */
00119     0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999, 0.5999,     /* øùúû     */
00120 };
00121 
00122 #if defined(WITH_CODEGENS) && !defined(HAVE_GD_FREETYPE) && defined(HAVE_LIBGD)
00123 extern codegen_t GD_CodeGen;
00124 #endif
00125 
00126 /* estimate_textsize:
00127  * Estimate width and height of text, for given face and size, in points.
00128  * Value is stored para->width.
00129  * NOTE: Tables are based on a font of size 1. Need to multiply by
00130  * fontsize to get appropriate value.
00131  */
00132 static void
00133 estimate_textlayout(graph_t *g, textpara_t * para, char **fontpath)
00134 {
00135     double *Fontwidth;
00136     char c, *p, *fp;
00137 
00138     para->width = 0.0;
00139     para->height = para->fontsize * LINESPACING;
00140     para->yoffset_layout = 0.0;
00141     para->yoffset_centerline = 0.1 * para->fontsize;
00142     para->layout = para->fontname;
00143     para->free_layout = NULL;
00144 
00145     if (!strncasecmp(para->fontname, "cour", 4)) {
00146         fp = "[internal courier]";
00147         Fontwidth = courFontWidth;
00148     } else if (!strncasecmp(para->fontname, "arial", 5)
00149                || !strncasecmp(para->fontname, "helvetica", 9)) {
00150         fp = "[internal arial]";
00151         Fontwidth = arialFontWidth;
00152     } else {
00153         fp = "[internal times]";
00154         Fontwidth = timesFontWidth;
00155     }
00156     if (fontpath)
00157         *fontpath = fp;
00158     if ((p = para->str)) {
00159         while ((c = *p++))
00160             para->width += Fontwidth[(unsigned char) c];
00161         para->width *= para->fontsize;
00162     }
00163 }
00164 
00165 /*
00166  * This table maps standard Postscript font names to URW Type 1 fonts.
00167  *
00168  *   The original source is in ps_font_equiv.txt.  This is sorted 
00169  *   during make into ps_font_equiv.h to ensure that it is in the right
00170  *   order for bsearch()
00171  */
00172 static PostscriptAlias postscript_alias[] = {
00173 #include "ps_font_equiv.h"
00174 };
00175 
00176 static int fontcmpf(const void *a, const void *b)
00177 {
00178     return (strcasecmp(((PostscriptAlias*)a)->name, ((PostscriptAlias*)b)->name));
00179 }
00180 
00181 static PostscriptAlias* translate_postscript_fontname(char* fontname)
00182 {
00183     static PostscriptAlias key;
00184     static PostscriptAlias *result;
00185 
00186     if (key.name == NULL || strcasecmp(key.name, fontname)) {
00187         key.name = fontname;
00188         result = (PostscriptAlias *) bsearch((void *) &key,
00189                         (void *) postscript_alias,
00190                         sizeof(postscript_alias) / sizeof(PostscriptAlias),
00191                         sizeof(PostscriptAlias),
00192                         fontcmpf);
00193     }
00194     return result;
00195 }
00196 
00197 pointf textsize(graph_t *g, textpara_t * para, char *fontname, double fontsize)
00198 {
00199     char **fp = NULL, *fontpath = NULL;
00200     pointf size;
00201 
00202     para->fontname = fontname;
00203     para->fontsize = fontsize;
00204 
00205     para->postscript_alias = translate_postscript_fontname(fontname);
00206 
00207     if (Verbose && emit_once(para->fontname))
00208         fp = &fontpath;
00209 
00210     if (! gvtextlayout(GD_gvc(g), para, fp))
00211         estimate_textlayout(g, para, fp);
00212 
00213     if (fp)
00214         fprintf(stderr, "fontname: \"%s\" resolved to: %s\n",
00215                     para->fontname, fontpath);
00216 
00217     size.x = para->width;
00218     size.y = para->height;
00219     return size;
00220 }
00221 
00222 /* psfontResolve:
00223  * Construct alias for postscript fontname.
00224  * NB. Uses a static array - non-reentrant.
00225  */ 
00226 
00227 #define ADD_ATTR(a) \
00228   if (a) { \
00229         strcat(buf, comma ? " " : ", "); \
00230         comma = 1; \
00231         strcat(buf, a); \
00232   }
00233 
00234 char* psfontResolve (PostscriptAlias* pa)
00235 {
00236     static char buf[1024];
00237     int comma=0;
00238     strcpy(buf, pa->family);
00239 
00240     ADD_ATTR(pa->weight);
00241     ADD_ATTR(pa->stretch);
00242     ADD_ATTR(pa->style);
00243 /*
00244     if (pa->weight) {
00245         strcat(buf, comma ? " " : ", ");
00246         comma = 1;
00247         strcat(buf, pa->weight);
00248     }
00249     if (pa->stretch) {
00250         strcat(buf, comma ? " " : ", ");
00251         comma = 1;
00252         strcat(buf, pa->stretch);
00253     }
00254     if (pa->style) {
00255                 strcat(buf, comma ? " " : ", ");
00256                 comma = 1;
00257                 strcat(buf, pa->style);
00258     }
00259 */
00260 
00261     return buf;
00262 }
00263 

Generated on Mon Mar 31 19:03:24 2008 for Graphviz by  doxygen 1.5.1