示例#1
0
pointf textspan_size(GVC_t *gvc, textspan_t * span)
{
    char **fpp = NULL, *fontpath = NULL;
    textfont_t *font;

    assert(span->font);
    font = span->font;

    assert(font->name);

    /* only need to find alias once per font, since they are unique in dict */
    if (! font->postscript_alias)
        font->postscript_alias = translate_postscript_fontname(font->name);

    if (Verbose && emit_once(font->name))
        fpp = &fontpath;

    if (! gvtextlayout(gvc, span, fpp))
        estimate_textspan_size(span, fpp);

    if (fpp) {
        if (fontpath)
            fprintf(stderr, "fontname: \"%s\" resolved to: %s\n",
                    font->name, fontpath);
        else
            fprintf(stderr, "fontname: unable to resolve \"%s\"\n", font->name);
    }

    return span->size;
}
示例#2
0
/* estimate_textsize:
 * Estimate width of text, for given face and size, in points.
 * Value is stored textline->width.
 * NOTE: Tables are based on a font of size 1. Need to multiply by
 * fontsize to get appropriate value.
 */
static void
estimate_textsize(textline_t * textline, char *fontname, double fontsz,
		  char **fontpath)
{
    double *Fontwidth;
    char c, *p;

    textline->width = 0.0;
    textline->xshow = NULL;
#if !defined(DISABLE_CODEGENS) && !defined(HAVE_GD_FREETYPE) && !defined(ONLY_PLAIN_GEN) //maks
    if (Output_codegen == &GD_CodeGen) {
	int cwidth;
	double fsize = (fontsz * _dpi) / POINTS_PER_INCH;	/* in pixels */
	*fontpath = "[internal gd]";
	cwidth = builtinFontWd(fsize);
	if ((p = textline->str)) {
	    textline->width = strlen(p) * cwidth * POINTS_PER_INCH / _dpi;
	}
	return;
    } else if (!strncasecmp(fontname, "cour", 4)) {
#else
    if (!strncasecmp(fontname, "cour", 4)) {
#endif
	*fontpath = "[internal courier]";
	Fontwidth = courFontWidth;
    } else if (!strncasecmp(fontname, "arial", 5)
	       || !strncasecmp(fontname, "helvetica", 9)) {
	*fontpath = "[internal arial]";
	Fontwidth = arialFontWidth;
    } else {
	*fontpath = "[internal times]";
	Fontwidth = timesFontWidth;
    }
    if ((p = textline->str)) {
	while ((c = *p++))
	    textline->width += Fontwidth[(unsigned char) c];
	textline->width *= fontsz;
    }
}
#endif

double textwidth(textline_t * textline, char *fontname, double fontsize)
{
    char *fontpath = NULL;
    int freeFontpath = 0;

#ifdef CAIRO_HAS_FT_FONT
    cairo_t *cr;
    cairo_text_extents_t extents;

    cr = cairo_create();
    cairo_select_font(cr, fontname, 0, 0);
    cairo_scale_font(cr, fontsize);
    cairo_text_extents(cr, textline->str, &extents);
    cairo_destroy(cr);

    textline->width = extents.width;
    textline->xshow = NULL;
    fontpath = "[cairo]";
#else
    if (gd_textsize(textline, fontname, fontsize, &fontpath))
	estimate_textsize(textline, fontname, fontsize, &fontpath);
    else
	freeFontpath = 1; /* libgd mallocs space for fontpath */
#endif

    if (Verbose) {
	if (emit_once(fontname)) {
	    fprintf(stderr, "%s: fontname=%s fontpath=%s\n", CmdName,
		    fontname, fontpath);
	}
    }
    if (freeFontpath) free (fontpath);
    return textline->width;
}