From: <ai...@us...> - 2011-07-07 20:56:36
|
Revision: 11799 http://plplot.svn.sourceforge.net/plplot/?rev=11799&view=rev Author: airwin Date: 2011-07-07 20:56:29 +0000 (Thu, 07 Jul 2011) Log Message: ----------- Use plP_script_scale to calculate superscript/subscript font scale factors and vertical offsets. The results are identical with the previous version of this code that made these calculations independently of plP_script_scale. Note plP_script_scale arguments must be stored externally, and for the alt_unicode approach this storage is necessarily in aStream to preserve the values of these arguments between plP_script_scale calls. Modified Paths: -------------- trunk/drivers/cairo.c Modified: trunk/drivers/cairo.c =================================================================== --- trunk/drivers/cairo.c 2011-07-06 17:28:53 UTC (rev 11798) +++ trunk/drivers/cairo.c 2011-07-07 20:56:29 UTC (rev 11799) @@ -107,6 +107,16 @@ short upDown; float fontSize; short closed; + + // These are arguments for plP_script_scale which must be retained + // in aStream for the alt_unicode approach. level has an + // identical meaning to upDown above, but it is incremented and + // decremented in plP_script_scale as well as other places in the + // code so the only safe thing to do is to treat level separately + // from upDown. + PLFLT old_sscale, sscale, old_soffset, soffset; + PLINT level; + #if defined ( PLD_xcairo ) cairo_surface_t *cairoSurface_X; cairo_t *cairoContext_X; @@ -240,7 +250,7 @@ static char *ucs4_to_pango_markup_format( PLUNICODE *, int, float ); static void open_span_tag( char *, PLUNICODE, float, int ); static void close_span_tag( char *, int ); -static char *rise_span_tag( int, int, float ); +static char *rise_span_tag( int, float, float, float ); // Graphics @@ -650,6 +660,7 @@ aStream = (PLCairo *) pls->dev; aStream->upDown = 0; + aStream->level = 0; aStream->pangoMarkupString = (char *) malloc( sizeof ( char ) * MAX_MARKUP_LEN ); // Calculate the font size (in points since DPI = 72). aStream->fontSize = pls->chrht * DPI / 25.4; @@ -715,10 +726,15 @@ if ( aStream->upDown < 0 ) { strncat( aStream->pangoMarkupString, "</span>", MAX_MARKUP_LEN - 1 - strlen( aStream->pangoMarkupString ) ); + aStream->level++; } else { - strncat( aStream->pangoMarkupString, rise_span_tag( aStream->upDown, 1, aStream->fontSize ), MAX_MARKUP_LEN - 1 - strlen( aStream->pangoMarkupString ) ); + plP_script_scale( TRUE, &aStream->level, + &aStream->old_sscale, &aStream->sscale, &aStream->old_soffset, &aStream->soffset ); + strncat( aStream->pangoMarkupString, + rise_span_tag( TRUE, aStream->fontSize, aStream->sscale, aStream->soffset ), + MAX_MARKUP_LEN - 1 - strlen( aStream->pangoMarkupString ) ); } aStream->upDown++; break; @@ -726,10 +742,15 @@ if ( aStream->upDown > 0 ) { strncat( aStream->pangoMarkupString, "</span>", MAX_MARKUP_LEN - 1 - strlen( aStream->pangoMarkupString ) ); + aStream->level--; } else { - strncat( aStream->pangoMarkupString, rise_span_tag( aStream->upDown, -1, aStream->fontSize ), MAX_MARKUP_LEN - 1 - strlen( aStream->pangoMarkupString ) ); + plP_script_scale( FALSE, &aStream->level, + &aStream->old_sscale, &aStream->sscale, &aStream->old_soffset, &aStream->soffset ); + strncat( aStream->pangoMarkupString, + rise_span_tag( FALSE, aStream->fontSize, aStream->sscale, aStream->soffset ), + MAX_MARKUP_LEN - 1 - strlen( aStream->pangoMarkupString ) ); } aStream->upDown--; break; @@ -822,7 +843,7 @@ // printf("baseline %d %d\n", baseline, textYExtent); cairo_rel_move_to( aStream->cairoContext, (double) ( -1.0 * args->just * (double) textXExtent ), - (double) 0.5 * aStream->fontSize - baseline / 1024.0); + (double) 0.5 * aStream->fontSize - baseline / 1024.0 ); // Render the text pango_cairo_show_layout( aStream->cairoContext, layout ); @@ -937,8 +958,8 @@ // printf("baseline (ps) %d %d %f\n", baseline, textYExtent, aStream->fontSize); // Move to the text starting point cairo_rel_move_to( aStream->cairoContext, - (double) ( -1.0 * args->just * (double) textXExtent ), - (double) 0.5 * fontSize - baseline / 1024.0); + (double) ( -1.0 * args->just * (double) textXExtent ), + (double) 0.5 * fontSize - baseline / 1024.0 ); // Render the text pango_cairo_show_layout( aStream->cairoContext, layout ); @@ -968,6 +989,8 @@ PLUNICODE fci; char utf8[5]; char *pangoMarkupString; + PLFLT old_sscale, sscale, old_soffset, soffset; + PLINT level = 0.; // Will this be big enough? We might have lots of markup. pangoMarkupString = (char *) malloc( sizeof ( char ) * MAX_MARKUP_LEN ); @@ -1030,10 +1053,15 @@ if ( upDown < 0 ) { strncat( pangoMarkupString, "</span>", MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); + level++; } else { - strncat( pangoMarkupString, rise_span_tag( upDown, 1, fontSize ), MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); + plP_script_scale( TRUE, &level, + &old_sscale, &sscale, &old_soffset, &soffset ); + strncat( pangoMarkupString, + rise_span_tag( TRUE, fontSize, sscale, soffset ), + MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); } upDown++; } @@ -1042,10 +1070,15 @@ if ( upDown > 0 ) { strncat( pangoMarkupString, "</span>", MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); + level--; } else { - strncat( pangoMarkupString, rise_span_tag( upDown, -1, fontSize ), MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); + plP_script_scale( FALSE, &level, + &old_sscale, &sscale, &old_soffset, &soffset ); + strncat( pangoMarkupString, + rise_span_tag( FALSE, fontSize, sscale, soffset ), + MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); } upDown--; } @@ -1081,7 +1114,9 @@ { unsigned char fontFamily, fontStyle, fontWeight; char openTag[TAG_LEN]; - int level; + int upDown_level; + PLFLT old_sscale, sscale, old_soffset, soffset; + PLINT level = 0.; // Generate the font info for the open tag & concatenate this // onto the markup string. @@ -1101,13 +1136,21 @@ strncat( pangoMarkupString, openTag, MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); // Move to the right superscript/subscript level - for ( level = 0; level < upDown; level++ ) + for ( upDown_level = 0; upDown_level < upDown; upDown_level++ ) { - strncat( pangoMarkupString, rise_span_tag( level, 1, fontSize ), MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); + plP_script_scale( TRUE, &level, + &old_sscale, &sscale, &old_soffset, &soffset ); + strncat( pangoMarkupString, + rise_span_tag( TRUE, fontSize, sscale, soffset ), + MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); } - for ( level = 0; level > upDown; level-- ) + for ( upDown_level = 0; upDown_level > upDown; upDown_level-- ) { - strncat( pangoMarkupString, rise_span_tag( level, -1, fontSize ), MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); + plP_script_scale( FALSE, &level, + &old_sscale, &sscale, &old_soffset, &soffset ); + strncat( pangoMarkupString, + rise_span_tag( FALSE, fontSize, sscale, soffset ), + MAX_MARKUP_LEN - 1 - strlen( pangoMarkupString ) ); } } @@ -1150,23 +1193,18 @@ // rise_span_tag // // Create a rise span tag w/ appropriate font size & baseline offset +// fontSize is the baseline font size in points (1/72 of an inch), +// multiplier is a scaling factor for that font size for superscript +// or subscript, and rise is the vertical offset (in units of font +// size) for that superscript or subscript. + //-------------------------------------------------------------------------- -char *rise_span_tag( int level, int direction, float fontSize ) +char *rise_span_tag( int ifsuperscript, float fontSize, float multiplier, float rise ) { - int i; - float multiplier = 1.0; - float rise = 1.0; float offset; static char tag[100]; - for ( i = 0; i < abs( level ); i++ ) - { - multiplier = multiplier * 0.75; - rise += multiplier; - } - // Adjust multiplier to correspond to font scale for level. - multiplier = multiplier * 0.75; // http://developer.gnome.org/pango/unstable/PangoMarkupFormat.html says // rise should be in units of 10000 em's, but empricial evidence shows // it is in units of 1024th of a point. Therefore, since FontSize @@ -1179,7 +1217,7 @@ // 0.5*(fontSize - superscript/subscript fontSize). offset = 1024. * 0.5 * fontSize * ( 1.0 - multiplier ); - if ( direction > 0 ) + if ( ifsuperscript ) { sprintf( tag, "<span rise=\"%d\" size=\"%d\">", (int) ( rise + offset ), (int) ( fontSize * 1024. * multiplier ) ); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |