/*
 * Copyright 1990 by Baylor College of Medicine ALL RIGHTS RESERVED. 
 *
 * This program is subject to a license agreement between 
 * Baylor College of Medicine and MIT. Any use inconsistent with
 * said license and any use by persons other than the faculty, 
 * students and staff at MIT or any use on a computer not operated 
 * as part of the Athena Computing Environment (ACE) is expressly 
 * prohibited.
 */
#include <stdio.h>
#include <X11/Xlib.h>
#include <etrealloc.h>
#include <alloc_font.h>
#include <strings.h>

typedef struct
{
	XFontStruct *f ;
	int n_used ;
} Entry ;

static Entry *entries ;
static int n_entries ;

XFontStruct *
alloc_font(dpy,name)
	Display *dpy ;
	char *name ;
{
	if (name == NULL)
	{
		return NULL ;
	}
	else
	{
		XFontStruct *f = XLoadQueryFont(dpy,name) ;
		if (f == NULL)
		{
			return NULL ;
		}
		else
		{
			int i ;
			for(i = 0; i < n_entries; i++)
			{
				if (entries[i].f->fid == f->fid)
				{
					XFreeFontInfo((char **)NULL,f,1) ;
					entries[i].n_used++ ;
					return entries[i].f ;
				}
			}
			{
				entries = (Entry *)etrealloc((char *)entries,
					(unsigned)((n_entries + 1)* sizeof(Entry))) ;
				entries[n_entries].f = f ;
				entries[n_entries].n_used = 1 ;
				return f ;
			}
		}
	}
}

free_font(dpy,f)
	Display *dpy ;
	XFontStruct *f ;
{
	int i ;
	for(i = 0; i < n_entries; i++)
	{
		if (entries[i].f == f)
		{
			entries[i].n_used-- ;
			return ;
		}
	}
}
