File: WcharDisp.c

package info (click to toggle)
kinput2 3.0-9
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,644 kB
  • ctags: 4,647
  • sloc: ansic: 45,508; makefile: 98; sh: 12
file content (853 lines) | stat: -rw-r--r-- 24,192 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
#ifndef lint
static char *rcsid = "$Id: WcharDisp.c,v 1.23 1994/10/27 08:29:05 ishisone Exp $";
#endif
/*
 * Copyright (c) 1990  Software Research Associates, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Software Research Associates not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  Software Research
 * Associates makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
 */

#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xmu/CharSet.h>
#include "CachedAtom.h"
#include "WcharDispP.h"
#include "LocalAlloc.h"

#define DEBUG_VAR debug_wcharDisplay
#include "DebugPrint.h"

/*
 * R5 servers implicitly changes font properties when an alias name is
 * used.  The character set properties (CHARSET_REGISTRY and
 * CHARSET_ENCODING) are taken from the XLFD fontname.  So if the
 * fontname is represented in lower-case letters, for example:
 *    -jis-fixed-medium-r-normal--16-110-100-100-c-160-jisx0208.1983-0
 *       (this example is taken from R5 fonts/misc/fonts.alias file)
 * then, the value of CHARSET_REGISTRY becomes "jisx0208.1983",
 * instead of the registered charset name "JISX0208.1983".
 * The following flag forces kinput2 to accept these invalid lower
 * case charset names as well.
 */
#define ALLOW_LOWERCASE_CHARSET_NAME

static FontMapping defaultMapping = { { False, False, False, False } };

static XtResource resources[] = {
#define offset(field) XtOffset(WcharDisplayObject, wcharDisplay.field)
    { XtNfontG0, XtCFont, XtRFontStruct, sizeof (XFontStruct *),
	offset(defaultfonts[0]), XtRString, XtDefaultFont },
    { XtNfontG1, XtCFont, XtRFontStruct, sizeof (XFontStruct *),
	offset(defaultfonts[1]), XtRImmediate, (XtPointer)NULL },
    { XtNfontG2, XtCFont, XtRFontStruct, sizeof (XFontStruct *),
	offset(defaultfonts[2]), XtRImmediate, (XtPointer)NULL },
    { XtNfontG3, XtCFont, XtRFontStruct, sizeof (XFontStruct *),
	offset(defaultfonts[3]), XtRImmediate, (XtPointer)NULL },
    { XtNfontMapping, XtCFontMapping, XtRFontMapping, sizeof (FontMapping),
	offset(defaultmapping), XtRFontMapping, (XtPointer)&defaultMapping },
#undef offset
};

static WDCharSet defCharSet[] = {
    { "ISO8859-1",		G0LCharSet },
    { "JISX0201.1976-0",	G0LCharSet },	/* alternative */
#ifdef ALLOW_LOWERCASE_CHARSET_NAME
    { "iso8859-1",		G0LCharSet },
    { "jisx0201.1976-0",	G0LCharSet },	/* alternative */
#endif
};

static void ClassInitialize();
static void StringToFontMapping();

static void Initialize();
static void Destroy();
static Boolean SetValues();

static void GetAtoms();
static void GetGC();
static void ChangeFont();

static int StringWidth();
static int LineHeight();
static void DrawString();
static int MaxChar();
static void SetFonts();

static int countControlChars();
static void expandControlChars();
static int charWidth();

WcharDisplayClassRec wcharDisplayClassRec = {
  { /* object fields */
    /* superclass		*/	(WidgetClass)&convDisplayClassRec,
    /* class_name		*/	"WcharDisplay",
    /* widget_size		*/	sizeof(WcharDisplayRec),
    /* class_initialize		*/	ClassInitialize,
    /* class_part_initialize	*/	NULL,
    /* class_inited		*/	FALSE,
    /* initialize		*/	Initialize,
    /* initialize_hook		*/	NULL,
    /* obj1			*/	NULL,
    /* obj2			*/	NULL,
    /* obj3			*/	0,
    /* resources		*/	resources,
    /* num_resources		*/	XtNumber(resources),
    /* xrm_class		*/	NULLQUARK,
    /* obj4			*/	FALSE,
    /* obj5			*/	FALSE,
    /* obj6			*/	FALSE,
    /* obj7			*/	FALSE,
    /* destroy			*/	Destroy,
    /* obj8			*/	NULL,
    /* obj9			*/	NULL,
    /* set_values		*/	SetValues,
    /* set_values_hook		*/	NULL,
    /* obj10			*/	NULL,
    /* get_values_hook		*/	NULL,
    /* obj11			*/	NULL,
    /* version			*/	XtVersion,
    /* callback_private		*/	NULL,
    /* obj12			*/	NULL,
    /* obj13			*/	NULL,
    /* obj14			*/	NULL,
    /* extension		*/	NULL
  },
  { /* convDisplay fields */
    /* StringWidth		*/	StringWidth,
    /* LineHeight		*/	LineHeight,
    /* DrawString		*/	DrawString,
    /* MaxChar			*/	MaxChar,
    /* DrawCursor		*/	XtInheritDrawCursor,
    /* GetCursorBounds		*/	XtInheritGetCursorBounds,
    /* SetFonts			*/	SetFonts,
  },
  { /* wcharDisplay fields */
    /* charset_specs		*/	defCharSet,
    /* num_specs		*/	XtNumber(defCharSet),
  }
};

WidgetClass wcharDisplayObjectClass = (WidgetClass)&wcharDisplayClassRec;

/* ARGSUSED */
static void
ClassInitialize()
{
    /* add String -> FontMapping converter */
    XtAddConverter(XtRString, XtRFontMapping, StringToFontMapping,
		   (XtConvertArgList)NULL, (Cardinal)0);
}

/* ARGSUSED */
static void
StringToFontMapping(args, num_args, from, to)
XrmValue *args;
Cardinal *num_args;
XrmValue *from;
XrmValue *to;
{
    char *s = (char *)from->addr;
    char buf[128];
    static FontMapping fm;
    int c;
    int i;

    for (i = 0; i < 4; i++) fm.grmapping[i] = False;
    to->size = sizeof(FontMapping);
    to->addr = (caddr_t)&fm;

    if (strlen(s) + 1 > sizeof(buf)) return;

    XmuCopyISOLatin1Lowered(buf, s);
    s = buf;
    for (i = 0; i < 4; i++) {
	while ((c = *s) != '\0' && (c == ' ' || c == '\t' || c == '\n')) s++;
	if (c == '\0') break;
	if (c == '/' || c == ',') {
	    s++;
	    continue;
	}
	if (!strncmp(s, "gl", 2)) {
	    fm.grmapping[i] = False;
	} else if (!strncmp(s, "gr", 2)) {
	    fm.grmapping[i] = True;
	} else {
	    XtStringConversionWarning(s, XtRFontMapping);
	}
	s += 2;
	while ((c = *s) != '\0' && (c == ' ' || c == '\t' || c == '\n')) s++;
	if (c == '\0') break;
	if (c == '/' || c == ',') s++;
    }
}

/* ARGSUSED */
static void
Initialize(req, new, args, num_args)
Widget req;
Widget new;
ArgList args;
Cardinal *num_args;
{
    WcharDisplayObjectClass class = (WcharDisplayObjectClass)XtClass(new);
    WcharDisplayObject obj = (WcharDisplayObject)new;
    int i;
    static char stipple_bits[] = {
	0x55, 0x55, 0x00, 0x00, 0xaa, 0xaa, 0x00, 0x00, 0x55, 0x55, 0x00, 0x00,
	0xaa, 0xaa, 0x00, 0x00, 0x55, 0x55, 0x00, 0x00, 0xaa, 0xaa, 0x00, 0x00,
	0x55, 0x55, 0x00, 0x00, 0xaa, 0xaa, 0x00, 0x00
    };

    /* create a Stipple Bitmap (for drawing CurrentSubSegment) */
    obj->wcharDisplay.stipple =
      XCreateBitmapFromData(XtDisplayOfObject((Widget)obj),
			    RootWindowOfScreen(XtScreenOfObject((Widget)obj)),
			    stipple_bits, 16, 16);

    for (i = 0; i < 4; i++) {
	obj->wcharDisplay.fonts[i] = obj->wcharDisplay.defaultfonts[i];
	obj->wcharDisplay.grmapping[i] =
		obj->wcharDisplay.defaultmapping.grmapping[i];
    }

    obj->wcharDisplay.num_specs = class->wcharDisplay_class.num_specs;
    obj->wcharDisplay.charset_specs = class->wcharDisplay_class.charset_specs;

    GetAtoms(obj);
    GetGC(obj);
}

static void
Destroy(w)
Widget w;
{
    WcharDisplayObject obj = (WcharDisplayObject)w;

    XtFree((char *)obj->wcharDisplay.fontspecs);
    XtWSReleaseGCSet(w, obj->wcharDisplay.gcset_normal);
    XtWSReleaseGCSet(w, obj->wcharDisplay.gcset_rev);
    XFreePixmap(XtDisplayOfObject((Widget)obj), obj->wcharDisplay.stipple);
}

/* ARGSUSED */
static Boolean
SetValues(cur, req, wid, args, num_args)
Widget cur;
Widget req;
Widget wid;
ArgList args;
Cardinal *num_args;
{
    WcharDisplayObject old = (WcharDisplayObject)cur;
    WcharDisplayObject new = (WcharDisplayObject)wid;
    Boolean redraw = False;
    int i;

#define wd wcharDisplay 
    for (i = 0; i < 4; i++) {
	if ((new->wd.defaultfonts[i] != old->wd.defaultfonts[i] ||
	     new->wd.defaultmapping.grmapping[i] !=
			new->wd.defaultmapping.grmapping[i]) &&
	    new->wd.fonts[i] == old->wd.defaultfonts[i]) {
	    redraw = True;
	    break;
	}
    }
    if (redraw ||
	new->convDisplay.foreground != old->convDisplay.foreground ||
	new->convDisplay.background != old->convDisplay.background) {
	XtWSReleaseGCSet(wid, new->wcharDisplay.gcset_normal);
	XtWSReleaseGCSet(wid, new->wcharDisplay.gcset_rev);
	GetGC(new);
	redraw = True;
    }
#undef wd

    return redraw;
}

static void
GetAtoms(obj)
WcharDisplayObject obj;
{
    Display *dpy = XtDisplayOfObject((Widget)obj);
    WDCharSet *csp;
    FontSpec *fsp;
    Cardinal nspecs;
    char buf[128];
    char *p, *q;
    int i;
    String params[1];
    Cardinal num_params;

    if ((nspecs = obj->wcharDisplay.num_specs) == 0) {
	params[0] = XtClass((Widget)obj)->core_class.class_name;
	num_params = 1;
	XtAppErrorMsg(XtWidgetToApplicationContext((Widget)obj),
		      "noEntry", "charSpec", "WidgetError",
		      "%s: has no character set spec.",
		      params, &num_params);
    }

    csp = obj->wcharDisplay.charset_specs;
    fsp = (FontSpec *)XtMalloc(sizeof(FontSpec) * obj->wcharDisplay.num_specs);
    obj->wcharDisplay.fontspecs = fsp;

    for (i = 0; i < nspecs; i++, csp++, fsp++) {
	p = csp->charset;
	q = buf;
	while (*p != '\0' && *p != '-') *q++ = *p++;
	if (*p++ == '\0' || *p == '\0') {
	    params[0] = XtClass((Widget)obj)->core_class.class_name;
	    num_params = 1;
	    XtAppErrorMsg(XtWidgetToApplicationContext((Widget)obj),
			  "invalidSpec", "charSetSpec", "WidgetError",
			  "%s: has invalid character set spec.",
			  params, &num_params);
	}
	*q = '\0';
	fsp->registry = CachedInternAtom(dpy, buf, False);
	fsp->encoding = CachedInternAtom(dpy, p, False);
    }
}

static void
GetGC(obj)
WcharDisplayObject obj;
{
    XtGCMask mask = GCFont|GCForeground|GCBackground;
    XGCValues values;
    int ascent, descent;
    Boolean *map = obj->wcharDisplay.grmapping;

    values.function = GXcopy;
    values.foreground = obj->convDisplay.foreground;
    values.background = obj->convDisplay.background;
    mask = GCFunction|GCForeground|GCBackground;
    obj->wcharDisplay.gc_normal = XtGetGC((Widget)obj, mask, &values);

    values.foreground = obj->convDisplay.background;
    values.fill_style = FillStippled;
    values.stipple = obj->wcharDisplay.stipple;
    mask = GCFunction|GCForeground|GCFillStyle|GCStipple;
    obj->wcharDisplay.gc_stipple = XtGetGC((Widget)obj, mask, &values);

    mask = GCFont|GCFunction|GCForeground|GCBackground;
    values.function = GXcopy;
    values.foreground = obj->convDisplay.foreground;
    values.background = obj->convDisplay.background;
    obj->wcharDisplay.gcset_normal = XtWSGetGCSet((Widget)obj, mask, &values,
						  obj->wcharDisplay.fonts[0],
						  obj->wcharDisplay.fonts[1],
						  obj->wcharDisplay.fonts[2],
						  obj->wcharDisplay.fonts[3]);
    values.foreground = obj->convDisplay.background;
    values.background = obj->convDisplay.foreground;
    obj->wcharDisplay.gcset_rev = XtWSGetGCSet((Widget)obj, mask, &values,
					       obj->wcharDisplay.fonts[0],
					       obj->wcharDisplay.fonts[1],
					       obj->wcharDisplay.fonts[2],
					       obj->wcharDisplay.fonts[3]);

    /* set font mapping */
    XWSSetMapping(obj->wcharDisplay.gcset_normal,
		  map[0], map[1], map[2], map[3]);
    XWSSetMapping(obj->wcharDisplay.gcset_rev,
		  map[0], map[1], map[2], map[3]);

    XWSFontHeight(obj->wcharDisplay.gcset_normal, NULL, 0, &ascent, &descent);
    obj->wcharDisplay.ascent = ascent;
    obj->wcharDisplay.fontheight = ascent + descent;
}

static void
ChangeFont(obj, fonts, mapping)
WcharDisplayObject obj;
XFontStruct **fonts;
Boolean *mapping;
{
    Boolean newgc = False;
    int i;

    for (i = 0; i < 4; i++) {
	if (fonts[i] != obj->wcharDisplay.fonts[i] ||
	    mapping[i] != obj->wcharDisplay.grmapping[i]) {
	    obj->wcharDisplay.fonts[i] = fonts[i];
	    obj->wcharDisplay.grmapping[i] = mapping[i];
	    newgc = True;
	}
    }
    if (newgc) {
	XtWSReleaseGCSet((Widget)obj, obj->wcharDisplay.gcset_normal);
	XtWSReleaseGCSet((Widget)obj, obj->wcharDisplay.gcset_rev);
	GetGC(obj);
    }
}

static int
StringWidth(w, seg, start, end)
Widget w;
ICString *seg;
int start;
int end;
{
    WcharDisplayObject obj = (WcharDisplayObject)w;
    wchar *wstr;
    int len = seg->nchars;
    int nctl;
    int width;

    if (end < 0 || len < end) end = len;
    if (start >= end || start >= len) return 0;

    wstr = (wchar *)seg->data + start;
    len = end - start;

    /*
     * searching for control characters -- if found, convert them
     * into '^?' format for readability.
     */
    if ((nctl = countControlChars(wstr, len)) == 0) {
	/* no control characters */
	width = XWSTextWidth(obj->wcharDisplay.gcset_normal, wstr, len);
    } else {
	wchar *s = (wchar *)LOCAL_ALLOC(sizeof(wchar) * (len + nctl));

	expandControlChars(wstr, len, s);
	width = XWSTextWidth(obj->wcharDisplay.gcset_normal, s, len + nctl);
	LOCAL_FREE(s);
    }
    return width;
}

static int
LineHeight(w, ascentp)
Widget w;
Position *ascentp;
{
    WcharDisplayObject obj = (WcharDisplayObject)w;

    if (ascentp != NULL) *ascentp = obj->wcharDisplay.ascent;
    return obj->wcharDisplay.fontheight;
}

static void
DrawString(w, canvas, seg, start, end, x, y)
Widget w;
Widget canvas;
ICString *seg;
int start;
int end;
int x;
int y;
{
    WcharDisplayObject obj = (WcharDisplayObject)w;
    wchar *wstr;
    XWSGC gcset;
    int len = seg->nchars;
    int nctl;
    Display *dpy = XtDisplay(canvas);
    Window win = XtWindow(canvas);
    int width;
    int attr;

    if (end < 0 || len < end) end = len;
    if (start >= end || start >= len) return;

    wstr = (wchar *)seg->data + start;
    len = end - start;

#define STIPPLED	1
#define UNDERLINED	2

    if (seg->attr == ICAttrNormalString) {
	gcset = obj->wcharDisplay.gcset_normal;
	attr = 0;
    } else if (seg->attr & ICAttrConverted) {
	/* converted segment */
	if (seg->attr & ICAttrCurrentSegment) {
	    gcset = obj->wcharDisplay.gcset_rev;
	    attr = 0;
	} else if (seg->attr & ICAttrCurrentSubSegment) {
	    gcset = obj->wcharDisplay.gcset_rev;
	    attr = STIPPLED;
	} else {
	    gcset = obj->wcharDisplay.gcset_normal;
	    attr = 0;
	}
    } else {	/* ICAttrNotConverted */
	gcset = obj->wcharDisplay.gcset_normal;
	attr = UNDERLINED;
    }

    if ((nctl = countControlChars(wstr, len)) == 0) {
	width = XWSDrawImageString(dpy, win, gcset,
				   x, y + obj->wcharDisplay.ascent,
				   wstr, len);
    } else {
	wchar *s = (wchar *)LOCAL_ALLOC((len + nctl) * sizeof(wchar));
	expandControlChars(wstr, len, s);
	width = XWSDrawImageString(dpy, win, gcset,
				   x, y + obj->wcharDisplay.ascent,
				   s, len + nctl);
	LOCAL_FREE(s);
    }

    if (attr == UNDERLINED) {
	int uloffset = 1;
	int descent = obj->wcharDisplay.fontheight - obj->wcharDisplay.ascent;

	if (descent <= 1) {
	    /* font descent is 0 or 1 */
	    uloffset = descent - 1;
	}
	XDrawLine(dpy, win, obj->wcharDisplay.gc_normal,
		  x, y + obj->wcharDisplay.ascent + uloffset,
		  x + width - 1, y + obj->wcharDisplay.ascent + uloffset);
    } else if (attr == STIPPLED) {
	XFillRectangle(dpy, win, obj->wcharDisplay.gc_stipple, x, y,
		       (unsigned int)width,
		       (unsigned int)obj->wcharDisplay.fontheight);
    }
}

static int
MaxChar(w, seg, start, width)
Widget w;
ICString *seg;
int start;
int width;
{
    WcharDisplayObject obj = (WcharDisplayObject)w;
    XWSGC gcset = obj->wcharDisplay.gcset_normal;
    wchar *sp = (wchar *)seg->data + start;
    wchar *ep = (wchar *)seg->data + seg->nchars;
    int cwidth;
    int chars;

    chars = 0;
    while (sp < ep) {
	cwidth = charWidth(*sp++, gcset);
	if (width < cwidth) break;
	chars++;
	if ((width -= cwidth) == 0) break;
    }
    return chars;
}

static void
SetFonts(w, fonts, num_fonts)
Widget w;
XFontStruct  **fonts;
Cardinal num_fonts;
{
    WcharDisplayObject obj = (WcharDisplayObject)w;
    WDCharSet *csp = obj->wcharDisplay.charset_specs;
    FontSpec *fsp = obj->wcharDisplay.fontspecs;
    Cardinal nspecs = obj->wcharDisplay.num_specs;
    Cardinal i, j;
    XFontStruct *pickedfonts[4];
    Boolean mapping[4];
    static int csetmask[4] = {
	G0LCharSet|G0RCharSet,
	G1LCharSet|G1RCharSet,
	G2LCharSet|G2RCharSet,
	G3LCharSet|G3RCharSet,
    };

    if (num_fonts == 0) {
	ChangeFont(obj, obj->wcharDisplay.defaultfonts,
		   obj->wcharDisplay.defaultmapping.grmapping);
	return;
    }

    for (i = 0; i < 4; i++) pickedfonts[i] = NULL;

    (void)_CDPickupFonts(w, fsp, nspecs, fonts, num_fonts);

#define GRMAP	(G0RCharSet|G1RCharSet|G2RCharSet|G3RCharSet)
    for (j = 0; j < nspecs; j++, fsp++, csp++) {
	if (fsp->font == NULL) continue;
	for (i = 0; i < 4; i++) {
	    if (pickedfonts[i] == NULL && (csp->flag & csetmask[i])) {
		pickedfonts[i] = fsp->font;
		mapping[i] = (csp->flag & csetmask[i] & GRMAP) ? True : False;
	    }
	}
    }
#undef GRMAP
    for (i = 0; i < 4; i++) {
	if (pickedfonts[i] == NULL) {
	    pickedfonts[i] = obj->wcharDisplay.defaultfonts[i];
	    mapping[i] = obj->wcharDisplay.defaultmapping.grmapping[i];
	}
    }

    ChangeFont(obj, pickedfonts, mapping);
}

/* countControlChars -- count number of control characters in a string */
static int
countControlChars(wstr, len)
register wchar *wstr;
int len;
{
    register wchar *end = wstr + len;
    register int n = 0;

    while (wstr < end) {
	if (*wstr < 0x20 || *wstr == 0x7f) n++;
	wstr++;
    }
    return n;  
}

/* expandControlChars -- convert control characters into '^?' format */
static void
expandControlChars(org, orglen, res)
wchar *org;
int orglen;
wchar *res;
{
    wchar *end;

    for (end = org + orglen; org < end; org++) {
	if (*org < 0x20 || *org == 0x7f) {
	    *res++ = '^';
	    *res++ = *org ^ 0x40;
	} else {
	    *res++ = *org;
	}
    }
}

#define WITHIN_RANGE_2D(row, col, fs) \
    ((fs)->min_byte1 <= (row) && (row) <= (fs)->max_byte1 && \
     (fs)->min_char_or_byte2 <= (col) && (col) <= (fs)->max_char_or_byte2)

#define WITHIN_RANGE(c, fs) \
    ((fs)->min_char_or_byte2 <= (c) && (c) <= (fs)->max_char_or_byte2)

#define CHAR_INFO_2D(row, col, fs) \
    ((fs)->per_char + ((row) - (fs)->min_byte1) * \
     ((fs)->max_char_or_byte2 - (fs)->min_char_or_byte2 + 1) + \
     ((col) - (fs)->min_char_or_byte2))

#define CHAR_INFO(c, fs) \
    ((fs)->per_char + ((c) - (fs)->min_char_or_byte2))

#define CHAR_EXIST(csp) \
    ((csp)->width != 0 || ((csp)->rbearing != 0) || ((csp)->lbearing != 0))

static int
defaultCharWidth(font)
XFontStruct *font;
{
    int defchar = font->default_char;

    if (font->min_byte1 || font->max_byte1) {
	int row = defchar >> 8;
	int col = defchar & 0xff;
	if (WITHIN_RANGE_2D(row, col, font)) {
	    if (font->per_char == NULL) {
		return font->min_bounds.width;
	    } else {
		XCharStruct *csp = CHAR_INFO_2D(row, col, font);
		return CHAR_EXIST(csp) ? csp->width : 0;
	    }
	} else {
	    return 0;
	}
    } else {
	if (WITHIN_RANGE(defchar, font)) {
	    if (font->per_char == NULL) {
		return font->min_bounds.width;
	    } else {
		XCharStruct *csp = CHAR_INFO(defchar, font);
		return CHAR_EXIST(csp) ? csp->width : 0;
	    }
	} else {
	    return 0;
	}
    }
}

/* charWidth -- returns width of the specified character */
static int
charWidth(c, gcset)
register int c;
XWSGC gcset;
{
    register XFontStruct *font;
    int width;
    int gset;
    int nonPrinting = (c < 0x20 || c == 0x7f);    

    if (nonPrinting) c ^= 0x40;

    switch (c & 0x8080) {
    case 0x0000: gset = 0; break;
    case 0x8080: gset = 1; break;
    case 0x0080: gset = 2; break;
    case 0x8000: gset = 3; break;
    }

    if ((font = gcset->fe[gset].font) == NULL) return 0;

    if (gcset->fe[gset].flag & GRMAPPING) {
	c |= 0x8080;
    } else {
	c &= 0x7f7f;
    }

    if (gcset->fe[gset].flag & TWOB) {
	register int row = (c >> 8) & 0xff;
	register int col = c & 0xff;
	if (WITHIN_RANGE_2D(row, col, font)) {
	    if (font->per_char == NULL) {
		width = font->min_bounds.width;
	    } else {
		XCharStruct *csp = CHAR_INFO_2D(row, col, font);

		width = CHAR_EXIST(csp) ? csp->width : defaultCharWidth(font);
	    }
	} else {
	    width = defaultCharWidth(font);
	}
    } else {
	c &= 0xff;
	if (WITHIN_RANGE(c, font)) {
	    if (font->per_char == NULL) {
		width = font->min_bounds.width;
	    } else {
		XCharStruct *csp = CHAR_INFO(c, font);

		width = CHAR_EXIST(csp) ? csp->width : defaultCharWidth(font);
	    }
	} else {
	    width = defaultCharWidth(font);
	}
    }
    if (nonPrinting) width += charWidth('^', gcset);

    return width;
}


/*
 * jpWcharDisplay definition
 *
 *	character set assignment for Japanese wchar:
 *	    G0: ascii (ISO8859/1 left-half)
 *	    G1: kanji (JISX0208)
 *	    G2: half-width kana (JISX0201 right-half)
 *	    G3: unused
 */

static FontMapping jpDefaultMapping = { { False, False, True, False } };

#define JPFONT_ASCII	"-Misc-Fixed-Medium-R-*--14-*-*-*-C-*-ISO8859-1"
#define JPFONT_KANJI	"-Misc-Fixed-Medium-R-*--14-*-*-*-C-*-JISX0208.1983-0"
#define JPFONT_KANA	"-Misc-Fixed-Medium-R-*--14-*-*-*-C-*-JISX0201.1976-0"

static XtResource jpresources[] = {
    /* only override superclass's default */
#define offset(field) XtOffset(JpWcharDisplayObject, wcharDisplay.field)
    { XtNfont, XtCFont, XtRFontStruct, sizeof (XFontStruct *),
	offset(defaultfonts[0]), XtRString, JPFONT_ASCII },
    { XtNkanjiFont, XtCKanjiFont, XtRFontStruct, sizeof (XFontStruct *),
	offset(defaultfonts[1]), XtRString, JPFONT_KANJI },
    { XtNkanaFont, XtCKanaFont, XtRFontStruct, sizeof (XFontStruct *),
	offset(defaultfonts[2]), XtRString, JPFONT_KANA },
    { XtNfontG3, "Not.used", XtRFontStruct, sizeof (XFontStruct *),
	offset(defaultfonts[3]), XtRImmediate, (XtPointer)NULL },
    { XtNfontMapping, XtCFontMapping, XtRFontMapping, sizeof (FontMapping),
	offset(defaultmapping), XtRFontMapping, (XtPointer)&jpDefaultMapping },
#undef offset
};

static WDCharSet jpCharSet[] = {
    { "ISO8859-1",		G0LCharSet },		/* my preference */
    { "JISX0201.1976-0",	G0LCharSet | G2RCharSet },
    { "JISX0208.1990-0",	G1LCharSet },
    { "JISX0208.1983-0",	G1LCharSet },
    { "JISX0208.1978-0",	G1LCharSet },
    { "JISX0208.1983-1",	G1RCharSet },
    { "JISX0208.1978-1",	G1RCharSet },
#ifdef ALLOW_LOWERCASE_CHARSET_NAME
    { "iso8859-1",		G0LCharSet },		/* my preference */
    { "jisx0201.1976-0",	G0LCharSet | G2RCharSet },
    { "jisx0208.1990-0",	G1LCharSet },
    { "jisx0208.1983-0",	G1LCharSet },
    { "jisx0208.1978-0",	G1LCharSet },
    { "jisx0208.1983-1",	G1RCharSet },
    { "jisx0208.1978-1",	G1RCharSet },
#endif
};

JpWcharDisplayClassRec jpWcharDisplayClassRec = {
  { /* object fields */
    /* superclass		*/	(WidgetClass)&wcharDisplayClassRec,
    /* class_name		*/	"JpWcharDisplay",
    /* widget_size		*/	sizeof(JpWcharDisplayRec),
    /* class_initialize		*/	NULL,
    /* class_part_initialize	*/	NULL,
    /* class_inited		*/	FALSE,
    /* initialize		*/	NULL,
    /* initialize_hook		*/	NULL,
    /* obj1			*/	NULL,
    /* obj2			*/	NULL,
    /* obj3			*/	0,
    /* resources		*/	jpresources,
    /* num_resources		*/	XtNumber(jpresources),
    /* xrm_class		*/	NULLQUARK,
    /* obj4			*/	FALSE,
    /* obj5			*/	FALSE,
    /* obj6			*/	FALSE,
    /* obj7			*/	FALSE,
    /* destroy			*/	NULL,
    /* obj8			*/	NULL,
    /* obj9			*/	NULL,
    /* set_values		*/	NULL,
    /* set_values_hook		*/	NULL,
    /* obj10			*/	NULL,
    /* get_values_hook		*/	NULL,
    /* obj11			*/	NULL,
    /* version			*/	XtVersion,
    /* callback_private		*/	NULL,
    /* obj12			*/	NULL,
    /* obj13			*/	NULL,
    /* obj14			*/	NULL,
    /* extension		*/	NULL
  },
  { /* convDisplay fields */
    /* StringWidth		*/	XtInheritStringWidth,
    /* LineHeight		*/	XtInheritLineHeight,
    /* DrawString		*/	XtInheritDrawString,
    /* MaxChar			*/	XtInheritMaxChar,
    /* DrawCursor		*/	XtInheritDrawCursor,
    /* GetCursorBounds		*/	XtInheritGetCursorBounds,
    /* SetFonts			*/	XtInheritSetFonts,
  },
  { /* wcharDisplay fields */
    /* charset_specs		*/	jpCharSet,
    /* num_specs		*/	XtNumber(jpCharSet),
  },
  { /* jpWcharDisplay fields */
    /* empty			*/	0,
  },
};

WidgetClass jpWcharDisplayObjectClass = (WidgetClass)&jpWcharDisplayClassRec;