Subject: libXg font handling improvement

From: Scott Schwartz <[email-masked]>
Date: Fri, 28 Nov 1997 17:52:38 -0500

Hi,
  As you know, libXg looks for fonts based on a filename, normally
given in your xdefaults file, containing a Plan 9 style font map.  That
works if and only if all your X clients have the same filesystem
namespace.  Normally, with local NFS, that's the case, but if you ever
log into some remote machine and try to run sam or 9term, the fonts
probably won't be where sam has been told to look.  So this patch
arranges to store the font map in the X resources database.  Combined
with using the x font server to hold the actual fonts, this makes
things work fairly nicely, without recourse to the filesystem(s).

In my xinitrc I run the following to set things up:

<unicode.9.font {
  printf '*p9font: '; sed -e 's/$/ \\n\\/'; printf '\n'
} | xrdb -merge

xrdb '+fp' tcp/fontserver:7100	# or 'fp='

(Using the font serever also fixes a JDK1.1 bug which gives the Java
AWT runtime system bus errors if you have the temerity to use a
non-Openwin X server with Sun's "run anywhere" technology.)

*** /tmp/T0a001XK	Fri Nov 28 15:05:48 1997
--- libXg/rdfontfile.c	Fri Nov 28 15:05:27 1997
***************
*** 13,27 ****
  	return s;
  }
  
! Font *
! rdfontfile(char *name, int ldepth)
  {
! 	Font *fnt;
! 	Cachesubf *c;
! 	int fd, i;
! 	char *buf, *s, *t;
  	struct stat sbuf;
! 	unsigned long min, max;
  
  	fd = open(name, O_RDONLY);
  	if (fd < 0)
--- 13,25 ----
  	return s;
  }
  
! char *
! file2string(char* name)
  {
! 	int fd;
  	struct stat sbuf;
! 	char *buf;
! 	int i;
  
  	fd = open(name, O_RDONLY);
  	if (fd < 0)
***************
*** 44,54 ****
  		free(buf);
  		return 0;
  	}
  
! 	s = buf;
  	fnt = (Font *)malloc(sizeof(Font));
! 	if (fnt == 0)
! 		goto Err1;
  	memset((void*)fnt, 0, sizeof(Font));
  	fnt->name = (char *)malloc(strlen(name)+1);
  	if (fnt->name==0)
--- 42,75 ----
  		free(buf);
  		return 0;
  	}
+ 	return buf;
+ }
  
! Font *
! rdfontfile(char *name, int ldepth)
! {
! 	return rdfontstring(name, file2string(name), ldepth);
! }
! 
! Font*
! rdfontstring(char *name, char *s, int ldepth)
! {
! 	Font *fnt;
! 	Cachesubf *c;
! 	int i;
! 	char *buf, *t;
! 	struct stat sbuf;
! 	unsigned long min, max;
! 
! 	buf = s;
! 	if (buf == 0)
! 		return 0;
  	fnt = (Font *)malloc(sizeof(Font));
! 	if (fnt == 0) {
!     Err1:
! 		free(buf);
! 		return 0;
! 	}
  	memset((void*)fnt, 0, sizeof(Font));
  	fnt->name = (char *)malloc(strlen(name)+1);
  	if (fnt->name==0)
*** /tmp/T0a001XK	Fri Nov 28 15:05:48 1997
--- libXg/xtbinit.c	Thu Nov 20 19:52:10 1997
***************
*** 181,187 ****
  	font = 0;
  	subfont = 0;
  	if (fontname) {
! 		font = rdfontfile(fontname, screen.ldepth);
  		if (!font || charwidth(font, (Rune) ' ') == 0) {
  			subfont = getsubfont(fontname);
  			if (!subfont)
--- 181,189 ----
  	font = 0;
  	subfont = 0;
  	if (fontname) {
! 		font = (fontname[0] == '.' || fontname[0] == '/') 
! 		     ? rdfontfile(fontname, screen.ldepth)
! 		     : rdfontstring("p9font", fontname, screen.ldepth);
  		if (!font || charwidth(font, (Rune) ' ') == 0) {
  			subfont = getsubfont(fontname);
  			if (!subfont)
*** /tmp/T0a001XK	Fri Nov 28 15:05:48 1997
--- include/libg.h	Thu Nov 20 19:43:54 1997
***************
*** 203,208 ****
--- 203,209 ----
  extern int	 bitbltclip(void*);
  extern Subfont*	 getsubfont(char*);
  extern Font	*rdfontfile(char*, int);
+ extern Font	*rdfontstring(char*, char*, int);
  extern void	 ffree(Font*);
  extern Font	*mkfont(Subfont*);
  extern void	 subffree(Subfont*);