Re: fonts in gtk-2.0/pango



On Mon, 5 Aug 2002, Owen Taylor wrote:

> Allin Cottrell <cottrell wfu edu> writes:
>
> > Thank you for putting me onto the right track.  It was a freetype
> > problem for me too, but in my case the problem was that the freetype
> > version was too recent (version 2.1.2) rather than too old.  After
> > installing 2.0.9 I get the correct font metrics.  It would be nice if
> > pango's configure script gave an error message when one tries to build
> > against freetype 2.1.2.
>
> Why would it do that? To my knowledge it works more or less
> perfectly with 2.1.2. It's certainly not incompatible with 2.1.2.

I spoke too soon, changing the freetype version did not actually fix
my metrics problem.  I have done some more checking and the problem
seems to be in the pangox backend.  The calculation is going on in
get_font_metrics_from_subfonts() in pangox.c.  I wrote a little test
program (below) and ran it with an input of "fixed medium 10": the
function that gets the character width by making a layout gives the
correct pixel width of 6; the function that uses
pango_font_metrics_get_approximate_char_width() gives an answer of
6890, or 7 after applying the PANGO_PIXELS() macro.

Along the way, the pangox code gets an avg_width of 60 via
get_int_prop (avg_width_atom, fs, &avg_width), which I guess is right,
but the further transformations of this number produce a wrong answer.
I got pangox to print out the fontmap->resolution and it's reporting
913.017.  I'm running XFree 4.2.0 at 1152x864.

Allin Cottrell

test program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

static gint char_width_from_layout (PangoFontDescription *pfd,
				    const gchar *s)
{
    GtkWidget *w;
    PangoContext *pc;
    PangoLayout *pl;
    gint width;

    w = gtk_label_new(NULL);
    pc = gtk_widget_get_pango_context(w);
    pango_context_set_font_description(pc, pfd);

    pl = pango_layout_new(pc);
    pango_layout_set_text(pl, s, 1);
    pango_layout_get_pixel_size(pl, &width, NULL);

    g_object_unref(G_OBJECT(pl));
    g_object_unref(G_OBJECT(pc));
    gtk_widget_destroy(w);

    return width;
}

static gint char_width_from_metrics (PangoFontDescription *pfd)
{
    GtkWidget *w;
    PangoContext *pc;
    PangoFontMetrics *metrics;
    PangoLanguage *lang;
    gint width;

    w = gtk_label_new(NULL);
    pc = gtk_widget_get_pango_context(w);
    lang = pango_context_get_language(pc);
    metrics = pango_context_get_metrics(pc, pfd, lang);
    width = pango_font_metrics_get_approximate_char_width(metrics);
    pango_font_metrics_unref(metrics);
    gtk_widget_destroy(w);

    return width;
}

int main (int argc, char *argv[])
{
    gint i, width;
    gchar fontname[128];
    PangoFontDescription *pfd;

    if (argc < 2) {
	fprintf(stderr, "%s: give me a fontname < 128 chars\n",
		argv[0]);
	exit(EXIT_FAILURE);
    }

    *fontname = 0;
    for (i=1; i<argc; i++) {
	strcat(fontname, argv[i]);
	if (i < argc - 1) strcat(fontname, " ");
    }

    printf("Got fontname '%s'\n", fontname);

    gtk_init(&argc, &argv);

    pfd = pango_font_description_from_string(fontname);

    if (pfd == NULL) {
	fprintf(stderr, "%s: couldn't get a font description\n",
		fontname);
	exit(EXIT_FAILURE);
    }

    printf("Character widths from layout:\n");
    width = char_width_from_layout(pfd, "i");
    printf("Got char width for 'i' of %d\n", width);
    width = char_width_from_layout(pfd, "W");
    printf("Got char width for 'W' of %d\n", width);

    printf("'Approximate' character width from metrics:\n");
    width = char_width_from_metrics(pfd);
    printf("Got width of %d (%d)\n", width, PANGO_PIXELS(width));

    return 0;
}

./pangotest "fixed medium 10"
Got fontname 'fixed medium 10'
Character widths from layout:
Got char width for 'i' of 6
Got char width for 'W' of 6
'Approximate' character width from metrics:
Got width of 6890 (7)






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]