From: ellson Date: Mon, 24 Jul 2006 14:25:02 +0000 (+0000) Subject: make translated fontnames (from postscript names) available to SVG renderer X-Git-Tag: LAST_LIBGRAPH~32^2~6057 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eec6c822102b63e518dcde27cce9e5b25cbee0ed;p=graphviz make translated fontnames (from postscript names) available to SVG renderer --- diff --git a/lib/common/fontmetrics.c b/lib/common/fontmetrics.c index 43620bb6a..470410fc8 100644 --- a/lib/common/fontmetrics.c +++ b/lib/common/fontmetrics.c @@ -160,6 +160,71 @@ estimate_textlayout(graph_t *g, textpara_t * para, char **fontpath) } } +typedef struct _PostscriptAlias { + char* name; + char* string_desc; +} PostscriptAlias; + +/* This table maps standard Postscript font names to URW Type 1 fonts */ +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" }, +}; + +static char* translate_postscript_fontname(char* fontname) +{ + int i; + + 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; + } + } + return NULL; +} + pointf textsize(graph_t *g, textpara_t * para, char *fontname, double fontsize) { char *fontpath = NULL; @@ -168,6 +233,8 @@ pointf textsize(graph_t *g, textpara_t * para, char *fontname, double fontsize) para->fontname = fontname; para->fontsize = fontsize; + para->translated_fontname = translate_postscript_fontname(fontname); + if (! gvtextlayout(GD_gvc(g), para, &fontpath) || !fontpath) estimate_textlayout(g, para, &fontpath); diff --git a/lib/common/htmltable.c b/lib/common/htmltable.c index 4cf382729..6352343a1 100644 --- a/lib/common/htmltable.c +++ b/lib/common/htmltable.c @@ -111,34 +111,34 @@ emit_html_txt(GVJ_t * job, htmltxt_t * tp, htmlenv_t * env, void *obj) /* make sure that there is something to do */ if (tp->nparas < 1) - return; + return; /* set font attributes */ - if (tp->font) { - if (tp->font->size > 0.0) - fsize = tp->font->size; - else - fsize = env->finfo.size; - if (tp->font->name) - fname = tp->font->name; - else - fname = env->finfo.name; - if (tp->font->color) - fcolor = tp->font->color; - else - fcolor = env->finfo.color; - } else { - fsize = env->finfo.size; - fname = env->finfo.name; - fcolor = env->finfo.color; - } - halfwidth_x = ((double)(tp->box.UR.x - tp->box.LL.x))/2.0; - p.x = env->p.x + ((double)(tp->box.UR.x + tp->box.LL.x))/2.0; - p.y = env->p.y + ((double)(tp->box.UR.y + tp->box.LL.y))/2.0; - - emit_textparas(job, tp->nparas, tp->para, p, - halfwidth_x, fname, fsize, fcolor); - } + if (tp->font) { + if (tp->font->size > 0.0) + fsize = tp->font->size; + else + fsize = env->finfo.size; + if (tp->font->name) + fname = tp->font->name; + else + fname = env->finfo.name; + if (tp->font->color) + fcolor = tp->font->color; + else + fcolor = env->finfo.color; + } else { + fsize = env->finfo.size; + fname = env->finfo.name; + fcolor = env->finfo.color; + } + halfwidth_x = ((double)(tp->box.UR.x - tp->box.LL.x))/2.0; + p.x = env->p.x + ((double)(tp->box.UR.x + tp->box.LL.x))/2.0; + p.y = env->p.y + ((double)(tp->box.UR.y + tp->box.LL.y))/2.0; + + emit_textparas(job, tp->nparas, tp->para, p, + halfwidth_x, fname, fsize, fcolor); +} #endif static void diff --git a/lib/common/labels.c b/lib/common/labels.c index b7e75a29d..9a2ff9112 100644 --- a/lib/common/labels.c +++ b/lib/common/labels.c @@ -174,7 +174,7 @@ emit_textparas(GVJ_t* job, int nparas, textpara_t paras[], pointf p, paraspacing = (int) (fsize * LINESPACING); /* position for first para */ - p.y += paraspacing * (nparas - 1) / 2 /* cl of toppara */ + p.y += paraspacing * (nparas - 1) / 2 /* cl of top para */ - fsize * 0.30; /* Empirically determined fudge factor */ gvrender_begin_context(job); diff --git a/lib/common/textpara.h b/lib/common/textpara.h index c8828b85c..cfb6c0947 100644 --- a/lib/common/textpara.h +++ b/lib/common/textpara.h @@ -24,6 +24,7 @@ extern "C" { typedef struct textpara_t { char *str; /* stored in utf-8 */ char *fontname; + char *translated_fontname; char *xshow; void *layout; void (*free_layout) (void *layout); /* FIXME - this is ugly */ diff --git a/lib/gvc/gvcjob.h b/lib/gvc/gvcjob.h index 8b90311a1..2514b8cb0 100644 --- a/lib/gvc/gvcjob.h +++ b/lib/gvc/gvcjob.h @@ -41,12 +41,10 @@ extern "C" { typedef enum { GVATTR_STRING, GVATTR_BOOL, GVATTR_COLOR } gvattr_t; typedef struct { - char *fontfam, fontopt; gvcolor_t pencolor, fillcolor; pen_type pen; fill_type fill; double penwidth; - double fontsz; } gvstyle_t; #define EMIT_SORTED (1<<0) diff --git a/lib/gvc/gvrender.c b/lib/gvc/gvrender.c index 657c2667f..c438ead00 100644 --- a/lib/gvc/gvrender.c +++ b/lib/gvc/gvrender.c @@ -426,9 +426,6 @@ void gvrender_begin_graph(GVJ_t * job, graph_t * g) /* init stack */ gvc->SP = 0; job->style = &(gvc->styles[0]); - job->style->fontfam = DEFAULT_FONTNAME; - job->style->fontsz = DEFAULT_FONTSIZE; - job->style->fontopt = FONT_REGULAR; job->style->pen = PEN_SOLID; job->style->fill = FILL_NONE; job->style->penwidth = PENWIDTH_NORMAL; @@ -1528,12 +1525,8 @@ void gvrender_set_font(GVJ_t * job, char *fontname, double fontsize) { gvrender_engine_t *gvre = job->render.engine; - if (gvre) { - job->style->fontfam = fontname; - job->style->fontsz = fontsize; - } #ifdef WITH_CODEGENS - else { + if (!gvre) { codegen_t *cg = job->codegen; if (cg && cg->set_font) diff --git a/plugin/core/gvrender_core_svg.c b/plugin/core/gvrender_core_svg.c index 0d11cb4c3..8819dcff4 100644 --- a/plugin/core/gvrender_core_svg.c +++ b/plugin/core/gvrender_core_svg.c @@ -117,42 +117,31 @@ static void svggen_print_color(GVJ_t * job, gvcolor_t color) } } -static void svggen_font(GVJ_t * job) +static void svggen_font(GVJ_t * job, char *fontname, double fontsize) { gvstyle_t *style = job->style; char buf[BUFSIZ]; - int needstyle = 0; strcpy(buf, " style=\""); - if (strcasecmp(style->fontfam, DEFAULT_FONTNAME)) { - sprintf(buf + strlen(buf), "font-family:%s;", style->fontfam); - needstyle++; - } - if (style->fontsz != DEFAULT_FONTSIZE) { - sprintf(buf + strlen(buf), "font-size:%.2f;", (style->fontsz)); - needstyle++; - } + sprintf(buf + strlen(buf), "font-family:%s;", fontname); + sprintf(buf + strlen(buf), "font-size:%.2f;", fontsize); switch (style->pencolor.type) { case COLOR_STRING: if (strcasecmp(style->pencolor.u.string, "black")) { sprintf(buf + strlen(buf), "fill:%s;", style->pencolor.u.string); - needstyle++; } break; case RGBA_BYTE: sprintf(buf + strlen(buf), "fill:#%02x%02x%02x;", style->pencolor.u.rgba[0], style->pencolor.u.rgba[1], style->pencolor.u.rgba[2]); - needstyle++; break; default: assert(0); /* internal error */ } - if (needstyle) { - strcat(buf, "\""); - svggen_fputs(job, buf); - } + strcat(buf, "\""); + svggen_fputs(job, buf); } @@ -306,13 +295,10 @@ static void svggen_begin_page(GVJ_t * job) svggen_printf(job, "common->viewNum); svggen_printf(job, - " transform=\"scale(%g %g) rotate(%d) translate(%g %g)\"", + " transform=\"scale(%g %g) rotate(%d) translate(%g %g)\">\n", job->scale.x, job->scale.y, job->rotation, job->translation.x, job->translation.y); /* default style */ - svggen_fputs(job, " style=\"font-family:"); - svggen_fputs(job, job->style->fontfam); - svggen_printf(job, ";font-size:%.2f;\">\n", job->style->fontsz); if (obj->g->name[0]) { svggen_fputs(job, ""); svggen_fputs(job, xml_string(obj->g->name)); @@ -418,7 +404,7 @@ static void svggen_textpara(GVJ_t * job, pointf p, textpara_t * para) svggen_printf(job, "<text text-anchor=\"%s\"", anchor); svggen_printf(job, " x=\"%g\" y=\"%g\"", p.x, -p.y); - svggen_font(job); + svggen_font(job, para->translated_fontname, para->fontsize); svggen_fputs(job, ">"); svggen_fputs(job, xml_string(para->str)); svggen_fputs(job, "</text>\n"); diff --git a/plugin/pango/gvtextlayout_pango.c b/plugin/pango/gvtextlayout_pango.c index 2b09eadb9..46677ff9c 100644 --- a/plugin/pango/gvtextlayout_pango.c +++ b/plugin/pango/gvtextlayout_pango.c @@ -24,71 +24,6 @@ #ifdef HAVE_PANGOCAIRO #include <pango/pangocairo.h> -typedef struct _PostscriptAlias { - char* name; - char* string_desc; -} PostscriptAlias; - -/* This table maps standard Postscript font names to URW Type 1 fonts */ -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" }, -}; - -static char* find_postscript_font(char* fontname) -{ - int i; - - 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; - } - } - return NULL; -} - static void pango_free_layout (void *layout) { g_object_unref((PangoLayout*)layout); @@ -110,7 +45,7 @@ static void pango_textlayout(GVCOMMON_t *common, textpara_t * para, char **fontp PangoAttrList *attrs; GError *error = NULL; #endif - char *text, *fontreq; + char *text; const char *family; if (!fontmap) @@ -119,15 +54,11 @@ static void pango_textlayout(GVCOMMON_t *common, textpara_t * para, char **fontp if (!context) context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP(fontmap)); - if (!fontname || strcmp(fontname, para->fontname)) { - fontname = para->fontname; + if (!fontname || strcmp(fontname, para->translated_fontname)) { + fontname = para->translated_fontname; pango_font_description_free (desc); - /* try to find a match for a PostScript font - * - or just get best available match */ - if (! (fontreq = find_postscript_font(fontname))) - fontreq = fontname; - desc = pango_font_description_from_string(fontreq); + desc = pango_font_description_from_string(fontname); } family = pango_font_description_get_family (desc); *fontpath = (char *)family;