From: sfeam (E. Merritt) <eam...@gm...> - 2013-04-26 03:51:22
|
On Thursday, 25 April 2013, Maxim Nikulin wrote: > Handle font style and weight in enhanced strings > (e.g. "sin({/Serif:Italic x})") for cairo-based terminals. > > Colon is replaced by spaces and the string is passed > to pango_font_description_from_string(). > No parsing of 'Italic' and 'Bold' withing gnuplot > is required anymore. Font size set by pango is ignored > (e.g. "{/Times:12 x}"). Font size still managed > by gnuplot in traditional ways ("Serif,16" in term options > and enhanced strings "{/=16 x}" or "{/*1.4 x}"). > Hyphens in font names should not be touched. > > --- > It was a bad surprise when I faced that, unlike postscript > terminal, modern pdfcairo does not allow to set italic font > in the part of axis label (set xlabel "Distance {/Italic L}, m"). > > The implemented simple approach does not support > inheritance of font attributes in nested braces. > The string "{/Bold {/Italic Italic}}" produces > normal italic, not bold. > > The quite close issues have been recently addressed in > https://groups.google.com/forum/?fromgroups=#!topic/comp.graphics.apps.gnuplot/fzs_uV6Oe8Y > https://groups.google.com/forum/?fromgroups=#!topic/comp.graphics.apps.gnuplot/YZGWsVBpuTI > > I have no experience with gnuplot sources and pango library, > so the path below should be carefully inspected. So in a nutshell, the basic idea in your patch is to set the font using pango_font_description_from_string(). That does seem like a promising approach, although it would only address the cairo terminals. I'll have a look at it and think some more about whether it's worth changing only the cairo terminals rather than trying for a general extension to the enhanced text mode syntax that could be used by other terminals as well. Thanks for the patch. Ethan > > > > Index: src/wxterminal/gp_cairo.c > =================================================================== > RCS file: /cvsroot/gnuplot/gnuplot/src/wxterminal/gp_cairo.c,v > retrieving revision 1.70 > diff -u -r1.70 gp_cairo.c > --- src/wxterminal/gp_cairo.c 22 Apr 2013 18:49:17 -0000 1.70 > +++ src/wxterminal/gp_cairo.c 25 Apr 2013 17:09:59 -0000 > @@ -1,5 +1,5 @@ > /* > - * $Id: gp_cairo.c,v 1.70 2013/04/22 18:49:17 sfeam Exp $ > + * $Id: gp_cairo.c,v 1.69 2013/03/07 22:06:33 sfeam Exp $ > */ > > /* GNUPLOT - gp_cairo.c */ > @@ -109,6 +109,9 @@ > static PangoAttrList *gp_cairo_enhanced_underprinted_AttrList = NULL; > /* converts text from symbol encoding to utf8 encoding */ > static gchar* gp_cairo_convert_symbol_to_unicode(plot_struct *plot, const char* string); > +/* copy font string to gp_cairo_enhanced_font > + * and replace ':' to spaces */ > +static void gp_cairo_set_font_enhanced(const char *name); > /* add standard attributes (fontsize,fontfamily, rise) to > * the specified characters in a PangoAttrList */ > static void gp_cairo_add_attr(plot_struct *plot, PangoAttrList * AttrList, int start, int end ); > @@ -296,36 +299,27 @@ > void gp_cairo_set_font(plot_struct *plot, const char *name, int fontsize) > { > char *c; > - char *fname; > > FPRINTF((stderr,"set_font \"%s\" %d\n", name,fontsize)); > > - /* Split out Bold and Italic attributes from font name */ > - fname = strdup(name); > - for (c=fname; *c; c++) { > - if (*c == '\\') { > - char *d = c; > - do { *d = *(d+1); } while (*d++); > - } else { > - if (*c == '-') *c = ' '; > - } > - } > - if ((c = strstr(fname, " Bold"))) { > - do { *c = *(c+5); } while (*c++); > - plot->fontweight = PANGO_WEIGHT_BOLD; > - } else > - plot->fontweight = 0; > - if ((c = strstr(fname, " Italic"))) { > - do { *c = *(c+7); } while (*c++); > - plot->fontstyle = PANGO_STYLE_ITALIC; > - } else > - plot->fontstyle = 0; > - > - strncpy( plot->fontname, fname, sizeof(plot->fontname) ); > - plot->fontsize = fontsize; > - free(fname); > + strncpy( plot->fontname, name, sizeof(plot->fontname) ); > + plot->fontsize = fontsize; > + for (c = plot->fontname; *c != '\0'; ++c) { > + if (*c == ':') > + *c = ' '; > + } > } > > +void gp_cairo_set_font_enhanced(const char *name) > +{ > + char *c; > + > + strncpy(gp_cairo_enhanced_font, name, sizeof(gp_cairo_enhanced_font) ); > + for (c = gp_cairo_enhanced_font; *c != '\0'; ++c) { > + if (*c == ':') > + *c = ' '; > + } > +} > > void gp_cairo_set_linewidth(plot_struct *plot, double linewidth) > { > @@ -789,8 +783,7 @@ > > pango_layout_set_text (layout, string_utf8, -1); > g_free(string_utf8); > - desc = pango_font_description_new (); > - pango_font_description_set_family (desc, (const char*) plot->fontname); > + desc = pango_font_description_from_string (plot->fontname); > #ifdef MAP_SYMBOL > /* restore the Symbol font setting */ > if (symbol_font_parsed) > @@ -798,10 +791,6 @@ > #endif /*MAP_SYMBOL*/ > pango_font_description_set_size (desc, (int) (plot->fontsize*PANGO_SCALE*plot->oversampling_scale) ); > > - pango_font_description_set_weight (desc, > - plot->fontweight ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL); > - pango_font_description_set_style (desc, > - plot->fontstyle ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL); > pango_layout_set_font_description (layout, desc); > pango_font_description_free (desc); > > @@ -1139,22 +1128,22 @@ > > void gp_cairo_add_attr(plot_struct *plot, PangoAttrList * AttrList, int start, int end ) > { > - PangoAttribute *p_attr_rise, *p_attr_size, *p_attr_family; > - > - p_attr_size = pango_attr_size_new ((int) (gp_cairo_enhanced_fontsize*PANGO_SCALE)); > - p_attr_size->start_index = start; > - p_attr_size->end_index = end; > - pango_attr_list_insert (AttrList, p_attr_size); > + PangoAttribute *p_attr_rise, *p_attr_desc; > + PangoFontDescription *desc; > > p_attr_rise = pango_attr_rise_new ((int) (gp_cairo_enhanced_base*PANGO_SCALE)); > p_attr_rise->start_index = start; > p_attr_rise->end_index = end; > pango_attr_list_insert (AttrList, p_attr_rise); > > - p_attr_family = pango_attr_family_new (gp_cairo_enhanced_get_fontname(plot)); > - p_attr_family->start_index = start; > - p_attr_family->end_index = end; > - pango_attr_list_insert (AttrList, p_attr_family); > + desc = pango_font_description_from_string (gp_cairo_enhanced_get_fontname(plot)); > + pango_font_description_set_size (desc, (int) (gp_cairo_enhanced_fontsize*PANGO_SCALE)); > + p_attr_desc = pango_attr_font_desc_new (desc); > + p_attr_desc->start_index = start; > + p_attr_desc->end_index = end; > + pango_attr_list_insert (AttrList, p_attr_desc); > + > + pango_font_description_free (desc); > } > > /* add a blank character to the text string with a custom shape */ > @@ -1289,13 +1278,9 @@ > /* Create a PangoLayout, set the font and text */ > current_layout = gp_cairo_create_layout (plot->cr); > pango_layout_set_text (current_layout, enhanced_text_utf8, -1); > - current_desc = pango_font_description_new (); > - pango_font_description_set_family (current_desc, gp_cairo_enhanced_get_fontname(plot)); > + current_desc = pango_font_description_from_string ( > + gp_cairo_enhanced_get_fontname(plot)); > pango_font_description_set_size(current_desc,(int) gp_cairo_enhanced_fontsize*PANGO_SCALE); > - pango_font_description_set_weight (current_desc, > - plot->fontweight ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL); > - pango_font_description_set_style (current_desc, > - plot->fontstyle ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL); > > pango_layout_set_font_description (current_layout, current_desc); > pango_font_description_free (current_desc); > @@ -1335,13 +1320,8 @@ > /* Create a PangoLayout, set the font and text */ > hide_layout = gp_cairo_create_layout (plot->cr); > pango_layout_set_text (hide_layout, enhanced_text_utf8, -1); > - hide_desc = pango_font_description_new (); > - pango_font_description_set_family (hide_desc, gp_cairo_enhanced_get_fontname(plot)); > + hide_desc = pango_font_description_from_string (gp_cairo_enhanced_get_fontname(plot)); > pango_font_description_set_size(hide_desc,(int) gp_cairo_enhanced_fontsize*PANGO_SCALE); > - pango_font_description_set_weight (hide_desc, > - plot->fontweight ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL); > - pango_font_description_set_style (hide_desc, > - plot->fontstyle ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL); > pango_layout_set_font_description (hide_layout, hide_desc); > pango_font_description_free (hide_desc); > > @@ -1366,13 +1346,8 @@ > /* Create a PangoLayout, set the font and text */ > zerowidth_layout = gp_cairo_create_layout (plot->cr); > pango_layout_set_text (zerowidth_layout, enhanced_text_utf8, -1); > - zerowidth_desc = pango_font_description_new (); > - pango_font_description_set_family (zerowidth_desc, gp_cairo_enhanced_get_fontname(plot)); > + zerowidth_desc = pango_font_description_from_string (gp_cairo_enhanced_get_fontname(plot)); > pango_font_description_set_size(zerowidth_desc,(int) gp_cairo_enhanced_fontsize*PANGO_SCALE); > - pango_font_description_set_weight (zerowidth_desc, > - plot->fontweight ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL); > - pango_font_description_set_style (zerowidth_desc, > - plot->fontstyle ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL); > pango_layout_set_font_description (zerowidth_layout, zerowidth_desc); > pango_font_description_free (zerowidth_desc); > pango_layout_get_extents(zerowidth_layout, NULL, &zerowidth_logical_rect); > @@ -1459,7 +1434,7 @@ > if (!gp_cairo_enhanced_opened_string) { > gp_cairo_enhanced_opened_string = TRUE; > gp_cairo_enhanced_char = gp_cairo_enhanced_string; > - strncpy(gp_cairo_enhanced_font, fontname, sizeof(gp_cairo_enhanced_font)); > + gp_cairo_set_font_enhanced (fontname); > gp_cairo_enhanced_fontsize = fontsize*plot->oversampling_scale; > gp_cairo_enhanced_base = base*plot->oversampling_scale; > gp_cairo_enhanced_showflag = showflag; > @@ -1724,13 +1699,8 @@ > /* Create a PangoLayout, set the font and text */ > layout = gp_cairo_create_layout (plot->cr); > pango_layout_set_text (layout, "0123456789", -1); > - desc = pango_font_description_new (); > - pango_font_description_set_family (desc, plot->fontname); > + desc = pango_font_description_from_string (plot->fontname); > pango_font_description_set_size(desc,(int) (plot->fontsize*PANGO_SCALE*plot->oversampling_scale)); > - pango_font_description_set_weight (desc, > - plot->fontweight ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL); > - pango_font_description_set_style (desc, > - plot->fontstyle ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL); > pango_layout_set_font_description (layout, desc); > pango_font_description_free (desc); > pango_layout_get_extents(layout, &ink_rect, &logical_rect); |