JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_cairoSetFont 
   (JNIEnv *env, jobject obj, jobject font)
{
  struct graphics2d *gr = NULL;
  struct peerfont *pfont = NULL;
  cairo_font_t *ft = NULL;
  FT_Face face = NULL;

  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);

  pfont = (struct peerfont *)NSA_GET_FONT_PTR (env, font);
  g_assert (pfont != NULL);

  gdk_threads_enter ();

  face = pango_ft2_font_get_face (pfont->font);
  g_assert (face != NULL);

  ft = cairo_ft_font_create_for_ft_face (face);
  g_assert (ft != NULL);

  if (gr->debug) printf ("cairo_set_font '%s'\n", face->family_name);
  
  cairo_set_font (gr->cr, ft);

  cairo_scale_font (gr->cr, 
		    pango_font_description_get_size (pfont->desc) / 
		    (double)PANGO_SCALE);

  cairo_font_destroy (ft);

  gdk_threads_leave ();
}
Exemple #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;
}