view tools/x11fonts.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
line wrap: on
line source
/*
    List font names in font style.

    Usage: x11fonts  [options]  name_pattern

    To enter name_patterns with wildcards, use forward quote,
    (eg. x11fonts 'vt*' finds all font names beginning with vt).

    Option -l      = list font names
    Default option = display font names (X11).

    The display format is:   "i/n  fontname"
      where the displayed name is the i'th out of n names which match the
      given name_pattern.
    The name is displayed at the current cursor position.

    The centre mouse button gets the next fontname in the list.
    The left button exits.
    The right button clears the display screen.
*/

#include <stdio.h>
#include <math.h>
#include "x11coord.h"

#define FONTNAME_SIZE   256

char printlist=0;
char printcount=0;
char printpath=0;

char **list = (char **)0 ;
char **pattern = (char **)0 ;
int    num_patterns ;

int  prompt_height ;
int  Y0 = 0 ;
int  Y = 0 ;

char prompt[FONTNAME_SIZE] ;

main(argc, argv)
int   argc ;
char *argv[] ;
{
    Window w;
    int    i, j, n;

    /* Search args list while args are single chars to prevent fontnames */
    /* which start with a `-' from being interpreted as args             */

    while ( --argc > 0 && **++argv == '-' && strlen( *argv ) < 3 )
	switch (*++*argv) {
	    case 'l': ++printlist;         break;
	    case 'n': ++printcount;        break;
	    case 'P': ++printpath;         break;
	    case 'h':
	    case 'H': help();
	}

    if ( argc == 0 ) num_patterns = 1    ;
    else             num_patterns = argc ;

    pattern = (char **)malloc( num_patterns * sizeof(char *) ) ;
    for ( i=0  ; i < num_patterns ; i++ )
	pattern[i] = (char *)malloc( FONTNAME_SIZE * sizeof(char) ) ;

    if ( argc == 0 )
	sprintf( pattern[0], "*\0" );
    else
	for ( i=0  ; i < num_patterns ; i++ )
	    sprintf( pattern[i], "%s\0", *argv++ ) ;


    set_window_parameters(BOXXORG,BOXYORG, BOXWIDTH,BOXHEIGHT);
    xopen();

    if ( printpath ) {
	list = XGetFontPath( theDisplay, &n ) ;
	for ( j=0  ; j < n ; j++ )
	    printf("%s\n", list[j]);
	XFreeFontPath( list ) ;
	exit( 0 ) ;
    }

    if ( printcount || printlist ) {
	for ( i=0  ; i < num_patterns ; i++ ) {
	    list = XListFonts(theDisplay, pattern[i], 1000, &n);
	    if (n>0) {
		if ( printcount )
		    printf("%d of %s\n", n, pattern[i]);
		else if ( printlist )
		    for ( j=0 ; j < n ; j++ )
			printf("%3d  %s\n", j+1, list[j]);
	    }
	    else
		fprintf(stderr, "no font names found to match pattern = %s\n", pattern[i] ) ;

	    XFreeFontNames(list);
	}
	exit( 0 ) ;
    }

    if ( setFont( theFontString ) == NULL ) {
	fprintf( stderr,"x11fonts: no prompts as can't load default font \"%s\"\n", theFontString ) ;
	prompt_height = 0 ;
    }
    else
	prompt_height = line(1) ;

    w = xcreate("fonts", ButtonPressMask | ExposureMask);
    xevent_monitor(w);

    if ( list != (char **)0 ) XFreeFontNames(list);
    XCloseDisplay( theDisplay ) ;
}


xevent_monitor(w)
Window w;
{
    XEvent event;
    int    n, i = 0, j = 0 ;

    for ( ; ; ) {
	XNextEvent(theDisplay,&event); switch (event.type) {
	    case ButtonPress:
		switch(event.xbutton.button) {
		    case Button1:               /* Left   */
			Y = Y0 ;
			if ( j < n ) {
			    XClearArea( theDisplay, w, 0, Y+2*prompt_height, BOXWIDTH, BOXHEIGHT-Y, 0 ) ;
			    print_alphabet(w, list, j++, n);
			}
			break ;
		    case Button2:               /* Middle */
			Y0 = Y ;
			if (j<n) print_alphabet(w, list, j++, n);
			break;
		    case Button3:               /* Right  */
			if ( ++i >= num_patterns ) return ;
			j = 0 ;
			if ( list != (char **)0 ) XFreeFontNames(list);
			list = XListFonts(theDisplay, pattern[i], 1000, &n);
			if ( prompt_height ) {
			    setFont( theFontString ) ;
			    XClearArea( theDisplay, w, 0, 0, BOXWIDTH, 2*prompt_height, 0 ) ;
			    if ( n > 0 ) {
				sprintf( prompt, "pattern  =  %s", pattern[i]);
				topline(w,prompt);
			    }
			    else {
				sprintf( prompt, "no font names found to match pattern  =  %s", pattern[i] ) ;
				topline(w,prompt);
			    }
			}
			break;
		    default:
			fprintf(stderr,"button %d not used\n", event.xbutton.button);
		}
		break;
	    case Expose:
		if ( event.xexpose.count == 0 ) {
		    list = XListFonts(theDisplay, pattern[i], 1000, &n);
		    if ( prompt_height ) {
			setFont( theFontString ) ;
			if (n>0) {
			    sprintf( prompt, "pattern  =  %s", pattern[i]);
			    topline(w,prompt);
			}
			else {
			    sprintf( prompt, "no font names found to match pattern  =  %s", pattern[i] ) ;
			    topline(w,prompt);
			}
		    }
		    if ( n > 0 ) {
			Y = Y0 ;
			print_alphabet(w, list, j++, n);
		    }
		}
		break;
	    default:
		fprintf(stderr,"event type %d not found\n", event.type);
	}
    }
}



char alphabet[] = "abcdefghijklmnopqrstuvwxyz\
ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

print_alphabet(w, fontlist, m, n)
Window w;
char **fontlist;
int    m,n;
{
    int  i,j,k, x,y;
    char str[128];

    if ( prompt_height ) {

	setFont( theFontString ) ;
	sprintf(str, "[%d/%d]        %s", m+1, n, fontlist[m]);

	Y += line(3) ;
	XDrawString(theDisplay,w,theGC,0,Y,str,strlen(str)) ;

	y = line(2);
    }
    else y = 0 ;

    setFont( fontlist[m] );
    if (y < line(2)) Y += line(2); /* set to start of larger font */
    else             Y += y ;

    y = Y ;
    for (i=0, k=0 ; i<nlines() && k<62; i++) {
	x = leftmargin();
	for (j=0 ; j<(ncols())-1 && k<62; j++) {
	    XDrawString(theDisplay, w, theGC, x,y, &alphabet[k++], 1);
	    x += colwidth();
	}
	y += lineheight();
    }
    Y = y ;


    if ( Y > BOXHEIGHT ) {
	Y = Y0 = 0 ;
	XClearArea( theDisplay, w, 0, 2*prompt_height, BOXWIDTH, BOXHEIGHT-prompt_height, 0 ) ;
	print_alphabet(w, fontlist, m, n) ;
    }
}


help()
{
    fprintf(stderr,"Usage: x11fonts  [options]  [name_patterns]\n");
    fprintf(stderr,"(name_patterns containing wild-cards must be quoted. Default = \"*\") \n");
    fprintf(stderr,"options: \n");
    fprintf(stderr,"      -n      = print count of matching font names found in search path\n");
    fprintf(stderr,"      -l      = list matching font names \n");
    fprintf(stderr,"      -P      = print the current font search path\n");
    exit(1);
}