move text drawing into separate function. (create_text_pixmap): handle

2002-12-05  James Henstridge  <james@daa.com.au>

    * src/font-view.c (draw_text): move text drawing into separate
    function.
    (create_text_pixmap): handle bitmap fonts by drawing the test
    string at the correct size(s).
    (add_face_info): display some information about the font.
This commit is contained in:
James Henstridge 2002-12-05 12:51:45 +00:00 committed by James Henstridge
parent fad0b07622
commit d46f05cc6c
2 changed files with 120 additions and 24 deletions

View file

@ -1,5 +1,11 @@
2002-12-05 James Henstridge <james@daa.com.au> 2002-12-05 James Henstridge <james@daa.com.au>
* src/font-view.c (draw_text): move text drawing into separate
function.
(create_text_pixmap): handle bitmap fonts by drawing the test
string at the correct size(s).
(add_face_info): display some information about the font.
* src/font-view.c (main): change so that it renders the text at a * src/font-view.c (main): change so that it renders the text at a
number of different sizes, like fontcarton did. Need to show some number of different sizes, like fontcarton did. Need to show some
info about the font too ... info about the font too ...

View file

@ -4,6 +4,11 @@
#include <X11/Xft/Xft.h> #include <X11/Xft/Xft.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gdk/gdkx.h> #include <gdk/gdkx.h>
#include <libgnomevfs/gnome-vfs.h>
#ifndef _
# define _(s) (s)
#endif
FT_Error FT_New_Face_From_URI(FT_Library library, FT_Error FT_New_Face_From_URI(FT_Library library,
const gchar *uri, const gchar *uri,
@ -14,10 +19,33 @@ FT_Error FT_New_Face_From_URI(FT_Library library,
#define FONTAREA_WIDTH 500 #define FONTAREA_WIDTH 500
#define FONTAREA_HEIGHT 236 #define FONTAREA_HEIGHT 236
static void
draw_text(Display *xdisplay, XftDraw *draw, FT_Face face, gint pixel_size,
XftColor *colour, const gchar *text, gint textlen, gint *pos_y)
{
FcPattern *pattern;
XftFont *font;
pattern = FcPatternBuild(NULL,
FC_FT_FACE, FcTypeFTFace, face,
FC_PIXEL_SIZE, FcTypeDouble, (double)pixel_size,
NULL);
font = XftFontOpenPattern(xdisplay, pattern);
FcPatternDestroy(pattern);
if (!font) {
g_printerr("could not load Xft face\n");
goto end;
}
XftDrawString8(draw, colour, font, 5, *pos_y + font->ascent,
(gchar *)text, textlen);
XftFontClose(xdisplay, font);
end:
*pos_y += pixel_size;
}
static GdkPixmap * static GdkPixmap *
create_text_pixmap(GtkWidget *drawing_area, FT_Face face) create_text_pixmap(GtkWidget *drawing_area, FT_Face face)
{ {
static const gint sizes[] = { 8, 10, 12, 18, 24, 36, 48, 72 };
gint i, pos_y, textlen; gint i, pos_y, textlen;
GdkPixmap *pixmap; GdkPixmap *pixmap;
gchar *text; gchar *text;
@ -29,7 +57,7 @@ create_text_pixmap(GtkWidget *drawing_area, FT_Face face)
XftDraw *draw; XftDraw *draw;
XftColor colour; XftColor colour;
text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; text = _("ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789");
textlen = strlen(text); textlen = strlen(text);
/* create pixmap */ /* create pixmap */
@ -52,30 +80,80 @@ create_text_pixmap(GtkWidget *drawing_area, FT_Face face)
XftColorAllocName(xdisplay, xvisual, xcolormap, "black", &colour); XftColorAllocName(xdisplay, xvisual, xcolormap, "black", &colour);
pos_y = 4; pos_y = 4;
for (i = 0; i < G_N_ELEMENTS(sizes); i++) { /* bitmap fonts */
FcPattern *pattern; if (face->num_fixed_sizes > 0) {
XftFont *font; for (i = 0; i < face->num_fixed_sizes; i++) {
draw_text(xdisplay, draw, face, face->available_sizes[i].width,
pattern = FcPatternBuild(NULL, &colour, text, textlen, &pos_y);
FC_FT_FACE, FcTypeFTFace, face, }
FC_PIXEL_SIZE, FcTypeDouble, (double)sizes[i], } else {
NULL); static const gint sizes[] = { 8, 10, 12, 18, 24, 36, 48, 72 };
font = XftFontOpenPattern(xdisplay, pattern);
FcPatternDestroy(pattern); for (i = 0; i < G_N_ELEMENTS(sizes); i++) {
if (!font) { draw_text(xdisplay, draw, face, sizes[i],
g_printerr("could not load Xft face\n"); &colour, text, textlen, &pos_y);
goto endloop;
} }
XftDrawString8(draw, &colour, font, 5, pos_y + font->ascent,
text, textlen);
XftFontClose(xdisplay, font);
endloop:
pos_y += sizes[i];
} }
return pixmap; return pixmap;
} }
static void
add_row(GtkWidget *table, gint *row_p, const gchar *name, const gchar *value)
{
gchar *bold_name;
GtkWidget *name_w, *value_w;
bold_name = g_strconcat("<b>", name, "</b>", NULL);
name_w = gtk_label_new(bold_name);
g_free(bold_name);
gtk_misc_set_alignment(GTK_MISC(name_w), 1.0, 0.5);
gtk_label_set_use_markup(GTK_LABEL(name_w), TRUE);
value_w = gtk_label_new(value);
gtk_misc_set_alignment(GTK_MISC(value_w), 0.0, 0.5);
gtk_label_set_selectable(GTK_LABEL(value_w), TRUE);
gtk_table_attach(GTK_TABLE(table), name_w, 0, 1, *row_p, *row_p + 1,
GTK_FILL, GTK_FILL, 0, 0);
gtk_table_attach(GTK_TABLE(table), value_w, 1, 2, *row_p, *row_p + 1,
GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
(*row_p)++;
}
static void
add_face_info(GtkWidget *table, gint *row_p, const gchar *uri, FT_Face face)
{
gchar *filename;
GnomeVFSFileInfo *file_info;
add_row(table, row_p, _("Name:"), face->family_name);
if (face->style_name)
add_row(table, row_p, _("Style:"), face->style_name);
filename = gnome_vfs_get_local_path_from_uri(uri);
add_row(table, row_p, _("Location:"), filename ? filename : uri);
g_free(filename);
file_info = gnome_vfs_file_info_new();
if (gnome_vfs_get_file_info
(uri, file_info, GNOME_VFS_FILE_INFO_GET_MIME_TYPE) == GNOME_VFS_OK) {
if ((file_info->valid_fields&GNOME_VFS_FILE_INFO_FIELDS_MIME_TYPE) !=0)
add_row(table, row_p, _("Type:"), file_info->mime_type);
if ((file_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE) != 0) {
gchar *size;
size = gnome_vfs_format_file_size_for_display(file_info->size);
add_row(table, row_p, _("Size:"), size);
g_free(size);
}
}
gnome_vfs_file_info_unref(file_info);
}
static gboolean static gboolean
expose_event(GtkWidget *widget, GdkEventExpose *event, GdkPixmap *pixmap) expose_event(GtkWidget *widget, GdkEventExpose *event, GdkPixmap *pixmap)
{ {
@ -95,8 +173,8 @@ main(int argc, char **argv)
FT_Library library; FT_Library library;
FT_Face face; FT_Face face;
gchar *title; gchar *title;
GtkWidget *window; gint row;
GtkWidget *drawing_area; GtkWidget *window, *vbox, *table, *drawing_area;
GdkPixmap *pixmap; GdkPixmap *pixmap;
GdkColor white = { 0, 0xffff, 0xffff, 0xffff }; GdkColor white = { 0, 0xffff, 0xffff, 0xffff };
@ -137,10 +215,22 @@ main(int argc, char **argv)
g_free(title); g_free(title);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE); gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
table = gtk_table_new(1, 2, FALSE);
gtk_container_set_border_width(GTK_CONTAINER(table), 5);
gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
row = 0;
add_face_info(table, &row, argv[1], face);
gtk_table_set_col_spacings(GTK_TABLE(table), 4);
gtk_table_set_row_spacings(GTK_TABLE(table), 2);
drawing_area = gtk_drawing_area_new(); drawing_area = gtk_drawing_area_new();
gtk_widget_modify_bg(drawing_area, GTK_STATE_NORMAL, &white); gtk_widget_modify_bg(drawing_area, GTK_STATE_NORMAL, &white);
gtk_widget_set_double_buffered(drawing_area, FALSE); gtk_box_pack_start(GTK_BOX(vbox), drawing_area, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), drawing_area);
pixmap = create_text_pixmap(drawing_area, face); pixmap = create_text_pixmap(drawing_area, face);