Index: kit/CHANGELOG
diff -u kit/CHANGELOG:1.9 kit/CHANGELOG:removed
--- kit/CHANGELOG:1.9	Sat Sep  5 00:11:21 1998
+++ kit/CHANGELOG	Mon Jul  5 05:10:03 1999
@@ -1,51 +0,0 @@
-CHANGELOG for magicpoint
-$Id: CHANGELOG,v 1.9 1998/09/04 15:11:21 onoe Exp $
-
-Fri Sep  5 1998  onoe@sm.sony.co.jp
-	* handle key inputs from invoked terminal as pressed on Xserver,
-	  even if the Xserver is running on a remote host.
-
-Mon Aug 26 1998  itojun@iijlab.net
-	* new copyright. (BSDish copyright without clause 3)
-
-Mon Aug 25 1998  itojun@iijlab.net
-	* print.c: better x11/freetype/vflib -> postscript font mapping.
-
-Mon Aug 23 1998  kato@wide.ad.jp
-	* embedded image support (added mgpembed.pl and embed.c)
-
-Mon Aug 22 1998  itojun@iijlab.net
-	* added contrib/xmindpath, MindPath PocketPoint user-level driver.
-
-Fri Jul 10 11:57:59 JST 1998  itojun@iijlab.net
-	* cache gs-generated image file.
-	  Suggested by: luigi@FreeBSD.org
-
-Mon Jul  6 11:38:32 JST 1998  itojun@iijlab.net
-	* capable of handling GB2312 and KSC5601 encoding.
-	  they must be encoded by using iso-2022 like escape sequences.
-	  EUC-cn or EUC-kr does not work.
-	* -x option is added.
-
-Thu Jul  2 18:04:16 JST 1998  itojun@iijlab.net
-	* eliminate gsview.
-	* revamp "xfont" directive. (see SYNTAX for detail)
-
-Thu Jun 25 13:10:28 JST 1998  itojun@iijlab.net
-	* print.c: color postscript support by "mgp2ps -c".
-
-changes between 1.02a and 1.03a:
-	* To allow color name that has space inbetween ("dark blue"), 
-	  color name after directives SHOULD come with doublequote.
-	  Therefore,
-		%fore blue
-	  should be
-		%fore "blue"
-	  At this moment, doublequote can be ommitted for backward
-	  compatibility.  We may require to have doublequote in the future.
-	  Font names and other string parameter obeys the same rule.
-	* %image is now capable of rendering encapsulated postscript files
-	  (*.eps).
-		%image foo.eps
-	  should work fine.
-	  By using -X option, you can specify device name for ghostscript.
Index: kit/USAGE
diff -u kit/USAGE:1.25 kit/USAGE:1.26
--- kit/USAGE:1.25	Thu Jun 17 13:26:09 1999
+++ kit/USAGE	Mon Jun 28 11:04:22 1999
@@ -136,21 +136,8 @@
 Tgif*Foreground: white
 Tgif*WhereToPrint: EPS
 Tgif*InitialFontSize: 16
-Tgif*InitialKanjiFont: Gothic
 Tgif*FontSizes: 14 16 24 26
-Tgif*NihongoFonts: \
-	-*-fixed-medium-r-normal--%d-*-*-*-*-*-jisx0208.1983-0,, \
-	Ryumin-Light-EUC-H, Ryumin, \
-	H, false, 24, 26 \n\
-	-*-fixed-medium-r-normal--%d-*-*-*-*-*-jisx0208.1983-0,, \
-	GothicBBB-Medium-EUC-H, Gothic, \
-	H, false, 16, 18 \n\
-	-*-fixed-medium-r-normal--%d-*-*-*-*-*-jisx0208.1983-0,, \
-	Ryumin-Light-EUC-V, Ryumin-V, \
-	V, false, 24, 26 \n\
-	-*-fixed-medium-r-normal--%d-*-*-*-*-*-jisx0208.1983-0,, \
-	GothicBBB-Medium-EUC-V, Gothic-V, \
-	V, false, 16, 18
+Tgif.XpmToGif: xpmtoppm %s | ppmtogif -transparent black
 
 EPS files can be created just by typing:
 	tgif -print -eps *.obj
Index: kit/USAGE.jp
diff -u kit/USAGE.jp:1.18 kit/USAGE.jp:1.19
--- kit/USAGE.jp:1.18	Thu Jun 17 13:26:10 1999
+++ kit/USAGE.jp	Mon Jun 28 11:04:23 1999
@@ -144,6 +144,7 @@
 	-*-fixed-medium-r-normal--%d-*-*-*-*-*-jisx0208.1983-0,, \
 	GothicBBB-Medium-EUC-V, Gothic-V, \
 	V, false, 16, 18
+Tgif.XpmToGif: xpmtoppm %s | ppmtogif -transparent black
 
 EPS $B$O!"(B
 	tgif -print -eps *.obj
Index: kit/ctlwords.pl
diff -u kit/ctlwords.pl:1.2 kit/ctlwords.pl:removed
--- kit/ctlwords.pl:1.2	Sat Sep  5 06:05:08 1998
+++ kit/ctlwords.pl	Mon Jul  5 05:10:07 1999
@@ -1,12 +0,0 @@
-#! /usr/bin/perl
-$counter = 0;	# 0 origin
-print "/* generated by ctlwords.awk. do not edit by hand. */\n";
-
-while (<>) {
-	next if (!/^\/\*CTL\*\//);
-	next if ($_ !~ /(CTL_[A-Z0-9]+)/);
-
-	$word = $1;
-	print "#define $word\t$counter\n";
-	$counter++;
-}
Index: kit/draw.c
diff -u kit/draw.c:1.151 kit/draw.c:1.152
--- kit/draw.c:1.151	Tue Jun  8 11:43:58 1999
+++ kit/draw.c	Sun Jul  4 00:52:39 1999
@@ -26,16 +26,14 @@
  * SUCH DAMAGE.
  */
 /*
- * $Id: draw.c,v 1.151 1999/06/08 02:43:58 nishida Exp $
+ * $Id: draw.c,v 1.152 1999/07/03 15:52:39 nishida Exp $
  */
 
 #include "mgp.h"
 
 /* state associated with the window - how should we treat this? */
-static struct ctrl *bg_image_ctl[2] = {NULL, NULL};
-static struct ctrl *bg_image_prev[2] = {NULL, NULL};
-static Image *bg_image[2] = {NULL, NULL};
-static XImageInfo *bg_ximageinfo[2] = {NULL, NULL};
+static struct ctrl *bg_ctl, *bg_ctl_last;
+static int bgindex = 0;
 struct render_state cache_state;
 
 static u_short kinsokutable[] = {
@@ -56,10 +54,12 @@
 } pcache;
 
 #define	COMPLEX_BGIMAGE \
-    (bg_image_prev[caching]				\
-     && ((bg_image_prev[caching]->ct_op == CTL_BIMAGE)	\
-       || bg_image_prev[caching]->ct_op == CTL_BGRAD))
+    (bg_ctl		\
+     && ((bg_ctl->ct_op == CTL_BIMAGE)	\
+       || bg_ctl->ct_op == CTL_BGRAD))
 
+#define	COMPLEX_BGIMAGE2 (0)
+
 #define	POSY(size)	(-(int)((size)/2))
 
 static void process_direc __P((struct render_state *, int *));
@@ -126,6 +126,10 @@
 static void cache_effect2 __P((void));
 static void set_from_cache __P((struct render_state *));
 static void pcache_process __P((int));
+static void regist_bgcache __P((XImageInfo *, Image *));
+static int check_bgcache __P((void));
+static int regist_bgpixmap __P((void));
+static void set_bgwindow __P((struct ctrl *));
 
 #define CHECK_CACHE {if (caching){caching = -1; return;}}
 
@@ -171,6 +175,7 @@
 state_next(state)
 	struct render_state *state;
 {
+
 	switch (state->phase) {
 	case P_NONE:
 		fprintf(stderr, "internal error\n");
@@ -491,7 +496,7 @@
 		break;
 
 	case CTL_BACK:
-		bg_image_ctl[caching] = cp;	/*update later*/
+		bg_ctl = cp;	/*update later*/
 		break;
 
 	case CTL_CCOLOR:
@@ -565,13 +570,13 @@
 	case CTL_BIMAGE:
 		if (mgp_flag & FL_BIMAGE)
 			break;
-		bg_image_ctl[caching] = cp;	/*update later*/
+		bg_ctl = cp;	/*update later*/
 		break;
 
 	case CTL_BGRAD:
 		if (mgp_flag & FL_BIMAGE)
 			break;
-		bg_image_ctl[caching] = cp;	/*update later*/
+		bg_ctl = cp;	/*update later*/
 		break;
 
 	case CTL_LCUTIN:
@@ -620,29 +625,29 @@
 
 	case CTL_LINESTART:
 		if (state->line == 0) {
-			if (bg_image_ctl[caching]) {
-				if (bg_image_prev[caching]
-				 && ctlcmp(bg_image_ctl[caching], 
-					bg_image_prev[caching]) == 0) {
+			if (bg_ctl) {
+				if (check_bgcache()){
+					if (!caching)  set_bgwindow(bg_ctl);
 					; /* same as the last time */
-				} else if (bg_image_ctl[caching]->ct_op == CTL_BIMAGE) {
-				    image_load(state, bg_image_ctl[caching]->ctm_fname,
-					bg_image_ctl[caching]->ctm_numcolor,
-					bg_image_ctl[caching]->ctm_ximagesize,
-					bg_image_ctl[caching]->ctm_yimagesize, 1,
-					bg_image_ctl[caching]->ctm_zoomflag, 0);
-				} else if (bg_image_ctl[caching]->ct_op == CTL_BGRAD) {
+				} else if (bg_ctl->ct_op == CTL_BIMAGE) {
+				    image_load(state, bg_ctl->ctm_fname,
+					bg_ctl->ctm_numcolor,
+					bg_ctl->ctm_ximagesize,
+					bg_ctl->ctm_yimagesize, 1,
+					bg_ctl->ctm_zoomflag, 0);
+				} else if (bg_ctl->ct_op == CTL_BGRAD) {
 					back_gradation(state,
-						&bg_image_ctl[caching]->ct_val.ctrl_grad);
-				} else if (bg_image_ctl[caching]->ct_op == CTL_BACK) {
-					if (!caching)
+						&bg_ctl->ct_val.ctrl_grad);
+				} else if (bg_ctl->ct_op == CTL_BACK) {
+					if (!caching){
+						bg_ctl_last = bg_ctl;
 						XSetWindowBackground(display,
-							state->target, bg_image_ctl[caching]->ctl_value);
-					else {
+							state->target, bg_ctl->ctl_value);
+					} else {
 						XSetPixmapBackground(display,
-							state->target, bg_image_ctl[caching]->ctl_value);
+							state->target, bg_ctl->ctl_value);
 					}
-					back_color[caching] = bg_image_ctl[caching]->ctl_value;
+					back_color[caching] = bg_ctl->ctl_value;
 				} else {
 					fprintf(stderr, "internal error: "
 						"bogus background\n");
@@ -650,14 +655,12 @@
 				}
 			}
 
-			if (!caching) 
+			if (!caching) { 
 				XClearWindow(display, state->target);
-			else 
+				bg_ctl_last = bg_ctl;
+			} else 
 				XClearPixmap(display, state->target);
 
-			bg_image_prev[caching] = bg_image_ctl[caching];
-			bg_image_ctl[caching] = NULL;
-
 			if (t_fin)
 				timebar(state);
 		}
@@ -1329,7 +1332,8 @@
 		freeImage(obj->data.image.image);
 		break;
 	case O_XFONT:
-		/* do nothing */
+		free(obj->data.xfont.xfont);
+		free(obj->data.xfont.registry);
 		break;
 	case O_ICON:
 		if (obj->data.icon.xpoint)
@@ -1408,7 +1412,7 @@
 	obj->y = y;
 	obj->fore = fore_color[caching];
 	obj->type = O_XFONT;
-	obj->data.xfont.xfont = x_findseed(state, registry);
+	obj->data.xfont.xfont = strdup(x_findseed(state, registry));
 	obj->data.xfont.csize = size;
 	obj->data.xfont.code = code;
 	obj->data.xfont.registry = registry;
@@ -1603,8 +1607,8 @@
 		bw = bh = 0;	/* for lint */
 		goto end;
 	}
-	bw = bg_image[caching]->width;
-	bh = bg_image[caching]->height;
+	bh = bgcache[bgindex].image->height;
+	bw = bgcache[bgindex].image->width;
 	j = 0;
 	if (image->type == IBITMAP) {
   expand:
@@ -1617,7 +1621,7 @@
 	}
 	pl = image->pixlen;
 	p = image->data + image->width * j * pl;
-	bpl = bg_image[caching]->pixlen;
+	bpl = bgcache[bgindex].image->pixlen;
 	pd = -1;
 	n = 0;	/* for lint */
 	trans = image->trans;
@@ -1625,19 +1629,19 @@
 	for ( ; j < image->height; j++) {
 		by = (y + j) % bh;
 		bx = x % bw;
-		b = bg_image[caching]->data + (bg_image[caching]->width * by + bx) * bpl;
+		b = bgcache[bgindex].image->data + (bgcache[bgindex].image->width * by + bx) * bpl;
 		for (i = 0; i < image->width; i++, p += pl, b += bpl, bx++) {
 			if (bx == bw) {
 				bx = 0;
-				b = bg_image[caching]->data + 
-					bg_image[caching]->width * by * bpl;
+				b = bgcache[bgindex].image->data + 
+					bgcache[bgindex].image->width * by * bpl;
 			}
 			if (memToVal(p, pl) != trans)
 				continue;
 			d = memToVal(b, bpl);
 			if (d != pd) {
 				pd = d;
-				n = obj_image_color(image, bg_image[caching], d, &inithist);
+				n = obj_image_color(image, bgcache[bgindex].image, d, &inithist);
 				if (n == -1)
 					goto expand;
 			}
@@ -1659,7 +1663,7 @@
 		fprintf(stderr, "  image=%dx%d+%d+%d",
 			image->width, image->height, x, y);
 		if (COMPLEX_BGIMAGE)
-			fprintf(stderr, "  bg_image[caching]=%dx%d", bw, bh);
+			fprintf(stderr, "  bgcache[bgindex].image=%dx%d", bw, bh);
 		fprintf(stderr, "\n");
 	}
 	image->trans = -1;	/* XXX: need recalculation to redraw? */
@@ -1714,11 +1718,28 @@
 {
 	struct render_object *obj;
 	int x = 0, y = 0;
-	int width, height;
+	int width, height, xwidth, xheight;
 	u_long fore;
 	u_int code;
 	char *registry;
 	XChar2b kch[2];
+#define MAXDRAWAREA 1024
+	struct {
+		int x, y, width, height;
+	} drawarea[MAXDRAWAREA];
+	int areaindex = 0;
+#define addarea(X) \
+{\
+	if (areaindex == MAXDRAWAREA){\
+		fprintf(stderr, "too many drawarea (increase MAXDRAWAREA)\n");\
+			exit(1);\
+	}\
+	drawarea[areaindex].x = x;\
+	drawarea[areaindex].y = y - obj->data.X->ascent;\
+	drawarea[areaindex].width = obj->data.X->width+1;\
+	drawarea[areaindex].height = obj->data.X->height+1;\
+	areaindex ++;\
+}
 #if 0
 	char ch[2];
 #endif
@@ -1746,12 +1767,18 @@
 	bcolor = back_color[caching];
 	for (obj = state->obj; obj; obj = obj->next) {
 #ifdef VFLIB
-		if (obj->type == O_VFONT)
+		if (obj->type == O_VFONT){
+			xwidth = obj->data.vfc->width;
+			xheight = obj->data.vfc->height;
 			break;
+		}
 #endif /* VFLIB */
 #ifdef FREETYPE
-		if (obj->type == O_TFONT)
+		if (obj->type == O_TFONT){
+			xwidth = obj->data.tfc->width;
+			xheight = obj->data.tfc->height;
 			break;
+		}
 #endif /* FREETYPE */
 	}
 	if (obj != NULL) {	/* VFONT exist */
@@ -1767,7 +1794,7 @@
 			int bpl;
 			XColor col;
 
-			bim = bg_ximageinfo[caching]->ximage;
+			bim = bgcache[bgindex].ximageinfo->ximage;
 			bw = bim->width;
 			bh = bim->height;
 			ox = xpos;
@@ -1776,22 +1803,22 @@
 			by = oy % bh;
 			if (bw == 1) {
 				r = g = b = 0;
-				bpl = bg_image[caching]->pixlen;
-				bp = bg_image[caching]->data + by * bpl;
+				bpl = bgcache[bgindex].image->pixlen;
+				bp = bgcache[bgindex].image->data + by * bpl;
 				for (y = 0;
 				     y < height;
 				     y++, by++, bp += bpl) {
 					if (by == bh)
 						by = 0;
 					p = memToVal(bp, bpl);
-					if (TRUEP(bg_image[caching])) {
+					if (TRUEP(bgcache[bgindex].image)) {
 						r += TRUE_RED(p) << 8;
 						g += TRUE_GREEN(p) << 8;
 						b += TRUE_BLUE(p) << 8;
 					} else {
-						r += bg_image[caching]->rgb.red[p];
-						g += bg_image[caching]->rgb.green[p];
-						b += bg_image[caching]->rgb.blue[p];
+						r += bgcache[bgindex].image->rgb.red[p];
+						g += bgcache[bgindex].image->rgb.green[p];
+						b += bgcache[bgindex].image->rgb.blue[p];
 					}
 					p = XGetPixel(bim, 0, by);
 					for (x = 0; x < width; x++)
@@ -1810,7 +1837,7 @@
 #if 0
 			fprintf(stderr, "bim=%dx%d, r=%x, g=%x, b=%x, "
 				"bcolor=%x\n",
-				bg_image[caching]->width, bg_image[caching]->height,
+				bgcache[bgindex].image->width, bgcache[bgindex].image->height,
 				col.red, col.green, col.blue, bcolor);
 #endif
 			} else {
@@ -1851,17 +1878,27 @@
 			if (obj->type == O_VFONT) {
 				(void)vfc_image(obj->data.vfc,
 					obj->fore, bcolor, xim, x, y);
+				addarea(vfc);
 			}
 #endif /* VFLIB */
 #ifdef FREETYPE
 			if (obj->type == O_TFONT) {
 				(void)tfc_image(obj->data.tfc,
 					obj->fore, bcolor, xim, x, y);
+				addarea(tfc);
 			}
 #endif /* FREETYPE */
 		}
+#if 0
 		XPutImage(display, target, gcfore, xim, 0, 0,
 			xpos, ypos, width, height);
+#else
+		for (i = 0; i < areaindex; i ++)
+			XPutImage(display, target, gcfore, xim,
+				drawarea[i].x, drawarea[i].y,
+				drawarea[i].x + xpos, ypos + drawarea[i].y,
+				drawarea[i].width, drawarea[i].height);
+#endif
 		XDestroyImage(xim);
 		if (mgp_flag & FL_GLYPHEDGE) {
 			XDrawLine(display, target, gcfore, 0, ypos,
@@ -1877,6 +1914,7 @@
 				0, ypos + height,
 				state->width - 1, ypos + height);
 		}
+
 	}
 #endif /* RASTERLIB */
 	for (obj = state->obj; obj; obj = obj->next) {
@@ -2568,22 +2606,18 @@
 	}
 
 	/* draw background */
-	if (!caching) 
+	if (!caching){ 
+		bg_ctl_last = bg_ctl;
 		XSetWindowBackgroundPixmap(display, state->target, mypixmap);
-	else  
-		XSetPixmapBackgroundPixmap(display, state->target, 
-			mypixmap, ximageinfo->ximage->width, ximageinfo->ximage->height);
+	}
+
+	XSetPixmapBackgroundPixmap(display, state->target, 
+		mypixmap, ximageinfo->ximage->width, ximageinfo->ximage->height);
 
 	/* finish */
 	XFreePixmap(display, mypixmap);
+	regist_bgcache(ximageinfo, myimage);
 
-	if (bg_image[caching]) {
-		freeXImage(bg_image[caching], bg_ximageinfo[caching]);
-		freeImage(bg_image[caching]);
-	}
-	bg_ximageinfo[caching] = ximageinfo;
-	bg_image[caching] = myimage;
-
 	XUndefineCursor(display, window);
 	XFlush(display);
 }
@@ -2704,13 +2738,14 @@
 			cleanup(-1);
 		}
 
-		if (!caching)
+		if (!caching){
+			bg_ctl_last = bg_ctl;
 			XSetWindowBackgroundPixmap(display, state->target, mypixmap);
-		else  {
-			XSetPixmapBackgroundPixmap(display, state->target, mypixmap,
-				ximageinfo->ximage->width, ximageinfo->ximage->height);
-		}
+		} 
 
+		XSetPixmapBackgroundPixmap(display, state->target, mypixmap,
+			ximageinfo->ximage->width, ximageinfo->ximage->height);
+
 		XFreePixmap(display, mypixmap);
 		strcpy(backfile, filename);
 		backnumcolor = numcolor;
@@ -2718,12 +2753,7 @@
 		backx = ximagesize;
 		backy = yimagesize;
 
-		if (bg_image[caching]) {
-			freeXImage(bg_image[caching], bg_ximageinfo[caching]);
-			freeImage(bg_image[caching]);
-		}
-		bg_ximageinfo[caching] = ximageinfo;
-		bg_image[caching] = myimage;
+		regist_bgcache(ximageinfo, myimage);
 
 		goto end;
 	}
@@ -3369,7 +3399,8 @@
 	struct render_state *state;
 {
 	/* invalidate the background image cache */
-	bg_image_prev[0] = NULL;
+
+	bg_ctl_last = NULL;
 	x_registerseed(state, NULL, NULL);
 }
 
@@ -3638,20 +3669,20 @@
 			Pixel d;
 
 			/* XXX: use background color of center position */
-			x = (obj->x + image->width/2) % bg_image[caching]->width;
-			y = (state->ypos + image->height/2) % bg_image[caching]->height;
-			bpl = bg_image[caching]->pixlen;
-			p = bg_image[caching]->data 
-				+ (bg_image[caching]->width * y + x) * bpl;
+			x = (obj->x + image->width/2) % bgcache[bgindex].image->width;
+			y = (state->ypos + image->height/2) % bgcache[bgindex].image->height;
+			bpl = bgcache[bgindex].image->pixlen;
+			p = bgcache[bgindex].image->data 
+				+ (bgcache[bgindex].image->width * y + x) * bpl;
 			d = memToVal(p, bpl);
-			if (bg_image[caching]->type == ITRUE) {
+			if (bgcache[bgindex].image->type == ITRUE) {
 				back.red   = TRUE_RED(d) << 8;
 				back.green = TRUE_GREEN(d) << 8;
 				back.blue  = TRUE_BLUE(d) << 8;
 			} else {
-				back.red   = bg_image[caching]->rgb.red  [d];
-				back.green = bg_image[caching]->rgb.green[d];
-				back.blue  = bg_image[caching]->rgb.blue [d];
+				back.red   = bgcache[bgindex].image->rgb.red  [d];
+				back.green = bgcache[bgindex].image->rgb.green[d];
+				back.blue  = bgcache[bgindex].image->rgb.blue [d];
 			}
 		}
 		for (i = 0; i < image->rgb.used; i++) {
@@ -3829,8 +3860,12 @@
 	Drawable target;
 	u_long	bgcolor;
 {
+	int	i;
+
+	if ((i = regist_bgpixmap()) < 0) return;
+
 	XSetForeground(display, gc_cache, bgcolor);
-	XFillRectangle(display, cacheback,
+	XFillRectangle(display, bgpixmap[i].pixmap,
 		gc_cache, 0, 0, window_width, window_height);
 }
 
@@ -3841,22 +3876,87 @@
 	Pixmap	pixmap;
 	int	width, height;
 {
-	int	x, y;
+	int	x, y, i;
 
+	if ((i = regist_bgpixmap()) < 0) return;
+
 	for (y = 0; y < window_height; y += height)
 		for (x = 0; x < window_width; x += width){
-			XCopyArea(display, pixmap, cacheback, gc_cache, 0, 0,
+			XCopyArea(display, pixmap, bgpixmap[i].pixmap, gc_cache, 0, 0,
 				width, height, x, y);
 		}
 }
 
+static int
+regist_bgpixmap()
+{
+	int	i, j;
+
+	for (i = 0; i < MAXBGPIXMAP; i ++){
+		if (bgpixmap[i].ctl && ctlcmp(bg_ctl, bgpixmap[i].ctl) == 0) 
+			return -1;
+	}
+
+	/* search empty slot */	
+	for (i = 0; i < MAXBGPIXMAP; i ++){
+		if (bgpixmap[i].ctl == NULL)
+			break;
+	}
+	if (i == MAXBGPIXMAP){
+		for (j = 0; j < MAXBGPIXMAP -1; j ++){	
+			bgpixmap[j].ctl = bgpixmap[j+1].ctl;	
+			bgpixmap[j].pixmap = bgpixmap[j+1].pixmap;	
+		}
+		i = 0;
+	}
+	bgpixmap[i].ctl = bg_ctl;
+
+	return i;
+}
+
+static void
+set_bgwindow(ctl)
+	struct ctrl *ctl;
+{
+	int	i;
+
+	if (!ctlcmp(bg_ctl_last, ctl)) return; 	
+
+	bg_ctl_last = ctl;
+	if (ctl->ct_op == CTL_BIMAGE || ctl->ct_op == CTL_BGRAD) {
+		for (i = 0; i < MAXBGPIXMAP; i ++){
+			if (bgpixmap[i].ctl && ctlcmp(bg_ctl, bgpixmap[i].ctl) == 0) {
+				XSetWindowBackgroundPixmap(display, window, bgpixmap[i].pixmap);
+				return;
+			}
+		}
+	} else {
+		if (ctl->ct_op == CTL_BACK) 
+			XSetWindowBackground(display, window, ctl->ctl_value);
+		else {
+			fprintf(stderr, "internal error: set_bgwindow()\n"); 
+			cleanup(-1);
+		}
+	}
+}
+
 static void 
 XClearPixmap(display, target)
 	Display *display;
 	Drawable target;
 {
-	XCopyArea(display, cacheback, cachewin, gc_cache, 
-			0, 0, window_width, window_height, 0, 0);
+	int	i;
+
+	for (i = 0; i < MAXBGPIXMAP; i ++){
+		if (bgpixmap[i].ctl && ctlcmp(bg_ctl, bgpixmap[i].ctl) == 0){ 
+			XCopyArea(display, bgpixmap[i].pixmap, cachewin, gc_cache, 
+				0, 0, window_width, window_height, 0, 0);
+			return;
+		}
+	}
+
+	fprintf(stderr, "internal error: XClearPixmap()\n"); 
+	cleanup(-1);
 }
 
 /* cache specified page */
@@ -3892,6 +3992,8 @@
 set_from_cache(state)
 	struct render_state *state;
 {
+	int	i;
+
 	char_size[0] = char_size[1];
 	horiz_gap[0] = horiz_gap[1];
 	vert_gap[0] = vert_gap[1];
@@ -3900,24 +4002,12 @@
 	ctrl_color[0] = ctrl_color[1];
 	b_quality[0] = b_quality[1];
 
-	bg_image_ctl[0] = bg_image_ctl[1];
-	bg_image_prev[0] = bg_image_prev[1];
-	bg_image[0] = bg_image[1];
-	bg_ximageinfo[0] = bg_ximageinfo[1];
-
-	bg_image_ctl[1] = NULL;
-	bg_image_prev[1] = NULL;
-	bg_image[1] = NULL;
-	bg_ximageinfo[1] = NULL;
-
 	memcpy(state, &cache_state, sizeof(struct render_state));	
 	state->target = window;
 
 	XSetForeground(display, gcfore, fore_color[0]);
 	XSetBackground(display, gcfore, back_color[0]);
-	XCopyArea(display, cacheback, cachetmpback, gc_cache,
-                  0, 0,  window_width, window_height, 0, 0);
-	XSetWindowBackgroundPixmap(display, state->target, cachetmpback);
+	set_bgwindow(bgcache[bgindex].ctl);
 
 	switch(cache_effect){
 		case 1:
@@ -3936,11 +4026,57 @@
 
 void
 reset_cache_bgimage()
+{
+	int	i = 0;
+
+	bg_ctl_last = NULL;
+
+	for (i = 0; i < MAXBGCACHE; i ++) {
+		if (bgcache[i].image){
+			freeXImage(bgcache[i].image, bgcache[i].ximageinfo);
+			freeImage(bgcache[0].image);
+		}
+		bgcache[i].ctl = NULL;
+		bgcache[i].image = NULL;
+		bgcache[i].ximageinfo = NULL;
+	}
+}
+
+static void
+regist_bgcache(ximageinfo, image)
+XImageInfo	*ximageinfo;
+Image	*image;
 {
-	bg_image_ctl[1] = NULL;
-	bg_image_prev[1] = NULL;
-	bg_image[1] = NULL;
-	bg_ximageinfo[1] = NULL;
+	int	i;
+	/* search empty slot */	
+	for (i = 0; i < MAXBGCACHE; i ++){
+		if (bgcache[i].ctl == NULL)
+			break;
+	}
+	if (i == MAXBGCACHE){
+		/* no empty slot, we need to make one  */
+		freeXImage(bgcache[0].image, bgcache[0].ximageinfo);
+		freeImage(bgcache[0].image);
+		i = 0;
+	}
+
+	bgcache[i].ctl = bg_ctl;
+	bgcache[i].image = image;
+	bgcache[i].ximageinfo = ximageinfo;
+	bgindex = i;
+}
+
+static int
+check_bgcache()
+{
+	int	i;
+	for (i = 0; i < MAXBGCACHE; i ++){
+		if (bgcache[i].ctl && ctlcmp(bg_ctl, bgcache[i].ctl) == 0) { 
+			bgindex = i;
+			return 1;
+		}
+	}
+	return 0;
 }
 
 static void
Index: kit/font.c
diff -u kit/font.c:1.23 kit/font.c:1.24
--- kit/font.c:1.23	Sun May 16 00:50:26 1999
+++ kit/font.c	Sun Jul  4 00:52:41 1999
@@ -26,7 +26,7 @@
  * SUCH DAMAGE.
  */
 /*
- * $Id: font.c,v 1.23 1999/05/15 15:50:26 nishida Exp $
+ * $Id: font.c,v 1.24 1999/07/03 15:52:41 nishida Exp $
  */
 
 #include "mgp.h"
@@ -156,7 +156,6 @@
 {
 	VFC_DELHASH(vfc);
 	VFC_DELLRU(vfc);
-	XDestroyImage(vfc->image);
 	free(vfc->dbitmap);
 	free(vfc);
 	vfcachecnt--;
@@ -307,6 +306,7 @@
 	if (code >= 0x2121 && code <= 0x2125 && max_x < width / 2)
 		max_x += width / 2;
 
+#if 0  /* remove soon */
 	xim = XCreateImage(display, visual, 1,
 		XYBitmap, 0, NULL, width, height, 8, 0);
 	if (xim == NULL) {
@@ -322,6 +322,7 @@
 	}
 	xim->data = (char *)bitmap;
 	vfc->image = xim;
+#endif
 	/* horizontal gap of 1 dot is included by default */
 	char_len = max_x - min_x + 2;
 	vfc->xoff = min_x;
Index: kit/globals.c
diff -u kit/globals.c:1.37 kit/globals.c:1.38
--- kit/globals.c:1.37	Mon Jun  7 02:55:13 1999
+++ kit/globals.c	Sun Jul  4 00:52:43 1999
@@ -26,7 +26,7 @@
  * SUCH DAMAGE.
  */
 /*
- * $Id: globals.c,v 1.37 1999/06/06 17:55:13 nishida Exp $
+ * $Id: globals.c,v 1.38 1999/07/03 15:52:43 nishida Exp $
  */
 
 #include "mgp.h"
@@ -67,8 +67,9 @@
 int cache_value = 60;
 Pixmap pixmap;
 Pixmap cachewin;
-Pixmap cacheback;
-Pixmap cachetmpback;
+Pixmap cachetmp;
+struct bgpixmap bgpixmap[MAXBGPIXMAP];
+struct bgcache bgcache[MAXBGCACHE];
 Colormap colormap;
 int free_clr_num;
 u_long *free_clr = NULL;
Index: kit/mgp.c
diff -u kit/mgp.c:1.102 kit/mgp.c:1.103
--- kit/mgp.c:1.102	Thu Jun 17 14:21:25 1999
+++ kit/mgp.c	Sun Jul  4 01:15:06 1999
@@ -26,7 +26,7 @@
  * SUCH DAMAGE.
  */
 /*
- * $Id: mgp.c,v 1.102 1999/06/17 05:21:25 kazu Exp $
+ * $Id: mgp.c,v 1.103 1999/07/03 16:15:06 nishida Exp $
  */
 
 static char *mgp_version = "1.05a (990617)";
@@ -932,6 +932,8 @@
 				if (control) {
 reload:
 					pl_off(&state);
+					cached_page = 0;
+					reset_cache_bgimage();
 					draw_reinit(&state);
 					cleanup_file();
 					load_file(mgp_fname);
Index: kit/mgp.h
diff -u kit/mgp.h:1.108 kit/mgp.h:1.109
--- kit/mgp.h:1.108	Mon Jun  7 02:55:18 1999
+++ kit/mgp.h	Sun Jul  4 00:52:44 1999
@@ -26,7 +26,7 @@
  * SUCH DAMAGE.
  */
 /*
- * $Id: mgp.h,v 1.108 1999/06/06 17:55:18 nishida Exp $
+ * $Id: mgp.h,v 1.109 1999/07/03 15:52:44 nishida Exp $
  */
 
 #include <stdio.h>
@@ -132,6 +132,8 @@
 #define MAXTAB		32
 #define MAXSTYLE	100
 #define MAXFONTDEF	100
+#define MAXBGCACHE	2
+#define MAXBGPIXMAP	2
 
 #define SP_NONE		0
 #define SP_SHRINK	1
@@ -451,7 +453,6 @@
 	int descent;
 	u_int xoff;
 	char *fontname;
-	XImage *image;
 	u_char *dbitmap;
 	u_int ref;
 };
@@ -473,7 +474,6 @@
 	int descent;		/* (origin y axis) - (bottom of bitmap) */
 	int xoff;		/* (left of bitmap) - (origin x axis) */
 	char *fontname;
-	XImage *image;
 	u_char *dbitmap;
 	u_int ref;
 };
@@ -484,6 +484,17 @@
 	long	*colors;
 };
 
+struct bgcache {
+	struct ctrl *ctl;
+	Image *image;
+	XImageInfo *ximageinfo;
+};
+
+struct bgpixmap {
+	struct ctrl *ctl;
+	Pixmap	pixmap;
+};
+
 /*
  * The following variables are defined in global.c, and therefore
  * they are available in both "mgp" and "mgp2ps" binary.
@@ -513,8 +524,9 @@
 extern int window_height;
 extern Pixmap pixmap;
 extern Pixmap cachewin;
-extern Pixmap cacheback;
-extern Pixmap cachetmpback;
+extern Pixmap cachetmp;
+extern struct bgpixmap bgpixmap[MAXBGPIXMAP];
+extern struct bgcache bgcache[MAXBGCACHE];
 extern Colormap colormap;
 extern struct alloc_color image_clr;
 extern struct alloc_color back_clr;
Index: kit/tfont.c
diff -u kit/tfont.c:1.30 kit/tfont.c:1.31
--- kit/tfont.c:1.30	Sun May 16 00:58:08 1999
+++ kit/tfont.c	Sun Jul  4 00:52:45 1999
@@ -26,7 +26,7 @@
  * SUCH DAMAGE.
  */
 /*
- * $Id: tfont.c,v 1.30 1999/05/15 15:58:08 nishida Exp $
+ * $Id: tfont.c,v 1.31 1999/07/03 15:52:45 nishida Exp $
  */
 
 #include "mgp.h"
@@ -123,7 +123,6 @@
 {
 	TFC_DELHASH(tfc);
 	TFC_DELLRU(tfc);
-	XDestroyImage(tfc->image);
 	free(tfc->fontname);
 	free(tfc->dbitmap);
 	free(tfc);
@@ -490,11 +489,10 @@
 		TTCEIL(metrics.bbox.yMax - metrics.bbox.yMin - metrics.bearingY) >> 6;
 	tfc->ascent = bitmap.rows - tfc->descent;
 	tfc->xoff = metrics.bearingX >> 6;
-	tfc->fontname = strdup(tf_curname);
+	tfc->fontname = strdup(fontname);
 	tfc->charlen = metrics.advance >> 6;
 	tfc->ref = 0;
 	tfc->dbitmap = bitmap.bitmap;
-	tfc->image = xim;
 
 	TFC_INSHASH(tfc);
 	TFC_INSLRU(tfc);
Index: kit/x11.c
diff -u kit/x11.c:1.24 kit/x11.c:1.25
--- kit/x11.c:1.24	Thu May 27 12:22:53 1999
+++ kit/x11.c	Sun Jul  4 00:52:47 1999
@@ -26,7 +26,7 @@
  * SUCH DAMAGE.
  */
 /*
- * $Id: x11.c,v 1.24 1999/05/27 03:22:53 nishida Exp $
+ * $Id: x11.c,v 1.25 1999/07/03 15:52:47 nishida Exp $
  */
 
 #include "mgp.h"
@@ -214,6 +214,7 @@
 	struct gcconf *pc;
 	u_long color;
 	XClassHint res = { "MagicPoint", "MagicPoint" };
+	int	i;
 
 	xsa.border_pixel = fore_color[0];
 	xsa.background_pixel = back_color[0];
@@ -236,8 +237,10 @@
 	pixmap = XCreatePixmap(display, window, xa.width, xa.height, depth);
 	maskpix = XCreatePixmap(display, window, xa.width, xa.height, depth);
 	cachewin = XCreatePixmap(display, window, xa.width, xa.height, depth);
-	cacheback = XCreatePixmap(display, window, xa.width, xa.height, depth);
-	cachetmpback = XCreatePixmap(display, window, xa.width, xa.height, depth);
+	cachetmp = XCreatePixmap(display, window, xa.width, xa.height, depth);
+	for (i = 0; i < MAXBGCACHE; i ++)
+		bgpixmap[i].pixmap = 
+			XCreatePixmap(display, window, xa.width, xa.height, depth);
 	gc_cache = XCreateGC(display, window, 0, 0);
 
 	for (pc = &gcconf[0]; pc->gc; pc++) {
@@ -410,11 +413,16 @@
 void
 reset_cache_pixmap()
 {
+	int	i;
+
 	XFreePixmap(display, cachewin);
-	XFreePixmap(display, cacheback);
-	XFreePixmap(display, cachetmpback);
+	XFreePixmap(display, cachetmp);
+	for (i = 0; i < MAXBGCACHE; i ++)
+		XFreePixmap(display, bgpixmap[i].pixmap);
 
 	cachewin = XCreatePixmap(display, window, xa.width, xa.height, depth);
-	cacheback = XCreatePixmap(display, window, xa.width, xa.height, depth);
-	cachetmpback = XCreatePixmap(display, window, xa.width, xa.height, depth);
+	cachetmp = XCreatePixmap(display, window, xa.width, xa.height, depth);
+	for (i = 0; i < MAXBGCACHE; i ++)
+	bgpixmap[i].pixmap = 
+		XCreatePixmap(display, window, xa.width, xa.height, depth);
 }
