File: g_alab_her.c

package info (click to toggle)
plotutils 2.4.1-15
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,072 kB
  • ctags: 6,952
  • sloc: ansic: 76,305; cpp: 12,402; sh: 8,475; yacc: 2,604; makefile: 894; lex: 144
file content (920 lines) | stat: -rw-r--r-- 27,368 bytes parent folder | download | duplicates (3)
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/* This file contains the internal method _alabel_hershey(), which plots a
   label using Hershey fonts.  Each character in a Hershey font is a
   sequence of pen motions, so this function calls _API_fmoverel() and
   _API_fcontrel() to `stroke' each character in the argument string.

   The width of the string in user units is returned.  The internal method
   _flabelwidth_hershey() is similar, but does not actually plot the label.  */

#include "sys-defines.h"
#include "extern.h"
#include "g_control.h"
#include "g_her_metr.h"

/* Shearing factor for oblique fonts, new_x = x + SHEAR * y  */

#define SHEAR (2.0/7.0)

/* Relative size of subscripts/superscripts (i.e. `indexical' size) */

#define SCRIPTSIZE (0.6)	

/* Positioning of subscripts/superscripts */

#define SUBSCRIPT_DX 0.0
#define SUBSCRIPT_DY (-0.25)
#define SUPERSCRIPT_DX 0.0
#define SUPERSCRIPT_DY 0.4

/* Positioning of accents (in Hershey units).  UP_SHIFT is amount by which
   accents are raised when placed over upper-case letters.  RIGHT_SHIFT is
   applied as well, if the upper-case letter is italic. */

#define ACCENT_UP_SHIFT 7.0
#define ACCENT_RIGHT_SHIFT 2.0

/* Relative size of small Japanese Kana */
#define SMALL_KANA_SIZE 0.725

/* Hershey glyph arrays */

#define OCCIDENTAL 0
#define ORIENTAL 1

/* Location of first Kana in the occidental glyph arrays.  (Kana, unlike
   Kanji, are placed in the occidental array, at the very end.) */
#define BEGINNING_OF_KANA 4195

/* forward references */
static bool _composite_char ____P((unsigned char *composite, unsigned char *character, unsigned char *accent));
static double _label_width_hershey ____P((const unsigned short *label));

/* An version of the alabel() method that is specific to the case when the
   current Plotter font is a Hershey font.  It handles escape sequences for
   subscripts, superscripts, shifts among Hershey fonts, etc.  */
double
#ifdef _HAVE_PROTOS
_alabel_hershey (R___(Plotter *_plotter) const unsigned char *s, int x_justify, int y_justify)
#else
_alabel_hershey (R___(_plotter) s, x_justify, y_justify)
     S___(Plotter *_plotter;)
     const unsigned char *s;
     int x_justify, y_justify;
#endif
{
  unsigned short *codestring;
  char x_justify_c, y_justify_c;
  double label_width, label_height;
  double x_offset, y_offset;
  double x_displacement;
  double postdx, dx, dy;
  double theta;

  /* convert string to a codestring, including annotations */
  codestring = _controlify (R___(_plotter) s);

  /* dimensions of the string in user units */
  label_width = HERSHEY_UNITS_TO_USER_UNITS(_label_width_hershey (codestring));
  label_height = HERSHEY_UNITS_TO_USER_UNITS(HERSHEY_HEIGHT);
  
  x_justify_c = (char)x_justify;
  y_justify_c = (char)y_justify;  

  switch (x_justify_c)
    {
    case 'l': /* left justified */
    default:
      x_offset = 0.0;
      x_displacement = 1.0;
      break;

    case 'c': /* centered */
      x_offset = -0.5;
      x_displacement = 0.0;
      break;

    case 'r': /* right justified */
      x_offset = -1.0;
      x_displacement = -1.0;
      break;
    }

  switch (y_justify_c)
    {
    case 'b':			/* current point is at bottom */
      y_offset = (double)HERSHEY_DESCENT / (double)HERSHEY_HEIGHT;
      break;

    case 'x':			/* current point is on baseline */
    default:
      y_offset = 0.0;
      break;

    case 'c': 			/* current point midway between bottom, top */
      y_offset = 0.5 * ((double)HERSHEY_DESCENT - (double)HERSHEY_ASCENT) 
		   / (double)HERSHEY_HEIGHT;
      break;

    case 'C':			/* current point is on cap line */
      y_offset = - (double)HERSHEY_CAPHEIGHT / (double)HERSHEY_HEIGHT;
      break;

    case 't':			/* current point is at top */
      y_offset = - (double)HERSHEY_ASCENT / (double)HERSHEY_HEIGHT;
      break;
    }

  /* save relevant drawing attributes, and restore them later */
  {
    char *old_line_mode, *old_cap_mode, *old_join_mode;
    int old_fill_type;
    double oldposx, oldposy;
    bool old_dash_array_in_effect;

    old_line_mode = (char *)_plot_xmalloc (strlen (_plotter->drawstate->line_mode) + 1);
    old_cap_mode = (char *)_plot_xmalloc (strlen (_plotter->drawstate->cap_mode) + 1);
    old_join_mode = (char *)_plot_xmalloc (strlen (_plotter->drawstate->join_mode) + 1);
    oldposx = _plotter->drawstate->pos.x;
    oldposy = _plotter->drawstate->pos.y;    

    strcpy (old_line_mode, _plotter->drawstate->line_mode);
    strcpy (old_cap_mode, _plotter->drawstate->cap_mode);
    strcpy (old_join_mode, _plotter->drawstate->join_mode);
    old_fill_type = _plotter->drawstate->fill_type;
    old_dash_array_in_effect = _plotter->drawstate->dash_array_in_effect;
    
    /* Our choices for rendering: solid lines, rounded capitals and joins,
       a line width equal to slightly more than 1 Hershey unit, and no
       filling.  

       We don't set the pen type: we allow it to be 0, which will mean no
       stroking at all. */
    _API_linemod (R___(_plotter) "solid");
    _API_capmod (R___(_plotter) "round");
    _API_joinmod (R___(_plotter) "round");
    _API_filltype (R___(_plotter) 0);
    
    /* move to take horizontal and vertical justification into account */
    {
      double theta, deltax, deltay, dx_just, dy_just;

      theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  
      deltax = x_offset * label_width;
      deltay = y_offset * label_height;
      dx_just = cos(theta) * deltax - sin(theta) * deltay;
      dy_just = sin(theta) * deltax + cos(theta) * deltay;
      
      _API_fmoverel (R___(_plotter) dx_just, dy_just);
    }
    
    /* call stroker on the sequence of strokes obtained from each char (the
       stroker may manipulate the line width) */
    _draw_hershey_string (R___(_plotter) codestring);
    
    /* Restore original values of relevant drawing attributes, free
       storage.  endpath() will be invoked in here automatically, flushing
       the created polyline object comprising the stroked text. */
    _API_linemod (R___(_plotter) old_line_mode);
    _API_capmod (R___(_plotter) old_cap_mode);
    _API_joinmod (R___(_plotter) old_join_mode);
    _API_filltype (R___(_plotter) old_fill_type);
    _plotter->drawstate->dash_array_in_effect = old_dash_array_in_effect;
    
    free (old_line_mode);
    free (old_cap_mode);
    free (old_join_mode);

    /* return to original position */
    _API_fmove (R___(_plotter) oldposx, oldposy);
  }

  /* amount by which to shift after printing label (user units) */
  postdx = x_displacement * label_width;
  theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  dx = cos (theta) * postdx;
  dy = sin (theta) * postdx;

  _API_fmoverel (R___(_plotter) dx, dy);

  free (codestring);

  return label_width;		/* user units */
}

/* A version of the flabelwidth() method that is specific to the case when
   the current Plotter font is a Hershey font. */
double
#ifdef _HAVE_PROTOS
_flabelwidth_hershey (R___(Plotter *_plotter) const unsigned char *s)
#else
_flabelwidth_hershey (R___(_plotter) s)
     S___(Plotter *_plotter;)
     const unsigned char *s;
#endif
{
  double label_width;
  unsigned short *codestring;
  
  /* convert string to a codestring, including annotations */
  codestring = _controlify (R___(_plotter) s);

  label_width = HERSHEY_UNITS_TO_USER_UNITS(_label_width_hershey (codestring));
  free (codestring);
  
  return label_width;
}

/* _draw_hershey_stroke() draws a stroke, taking into account the
   transformation from Hershey units to user units, and also the angle in
   user space at which the label should be plotted. */

void
#ifdef _HAVE_PROTOS
_draw_hershey_stroke (R___(Plotter *_plotter) bool pendown, double deltax, double deltay)
#else
_draw_hershey_stroke (R___(_plotter) pendown, deltax, deltay)
     S___(Plotter *_plotter;)
     bool pendown;
     double deltax, deltay;
#endif
{
  double theta = M_PI * _plotter->drawstate->text_rotation / 180.0;
  double dx, dy;

  deltax = HERSHEY_UNITS_TO_USER_UNITS (deltax);
  deltay = HERSHEY_UNITS_TO_USER_UNITS (deltay);

  dx = cos(theta) * deltax - sin(theta) * deltay;
  dy = sin(theta) * deltax + cos(theta) * deltay;

  if (pendown)
    _API_fcontrel (R___(_plotter) dx, dy);
  else
    _API_fmoverel (R___(_plotter) dx, dy);
}

/* _label_width_hershey() computes the width (total delta x) of a
   controlified character string to be rendered in a vector font, in
   Hershey units.  Parsing must take into account the many control
   sequences which perform shifts, and initiate/terminate
   subscripts/superscripts. */

/* In addition to scaling the character sizes and the `width', we perform
   the following (dx, dy):
   
   enter subscript	(dx, dy) = (-1/9, -1/2) * width
   exit subscript	(dx, dy) = (+1/6, +1/2) * width
   
   enter superscript	(dx, dy) = (-1/9, +1/2) * width
   exit superscript	(dx, dy) = (+1/6, -1/2) * width
   
   For clarity here, `width' refers to the width _before_ it is
   multiplied by a factor 2/3.
   
   [N.B. In Bob Beach's original UGS character stroke generator,
   the +1/6's here were +2/9 instead.  Better?] */

static double
#ifdef _HAVE_PROTOS
_label_width_hershey (const unsigned short *label) 
#else
_label_width_hershey (label) 
     const unsigned short *label;
#endif
{ 
  const unsigned short *ptr = label;
  unsigned short c;
  double charsize = 1.0;	/* relative char size, 1.0 means full size */
  double saved_charsize = 1.0;
  double width = 0.0;		/* label width */
  double saved_width = 0.0;
  
  /* loop through unsigned shorts in label */
  while ((c = (*ptr)) != (unsigned short)'\0') 
    {
      int glyphnum;		/* glyph in Hershey array */
      const unsigned char *glyph;
      
      if (c & RAW_HERSHEY_GLYPH) 
	/* glyph was spec'd via an escape, not as a char in a font */
	{
	  glyphnum = c & GLYPH_SPEC;
	  glyph = (const unsigned char *)(_occidental_hershey_glyphs[glyphnum]);
	  
	  if (*glyph != '\0')	/* nonempty glyph */
	    /* 1st two chars are bounds */
	    width += charsize * ((int)glyph[1] - (int)glyph[0]);
	}
      else if (c & RAW_ORIENTAL_HERSHEY_GLYPH) 
	/* glyph was spec'd via an escape, not as a char in a font */
	{
	  glyphnum = c & GLYPH_SPEC;
	  glyph = (const unsigned char *)_oriental_hershey_glyphs[glyphnum];
	  
	  if (*glyph != '\0')	/* nonempty glyph */
	    /* 1st two chars are bounds */
	    width += charsize * ((int)glyph[1] - (int)glyph[0]);
	}
      else if (c & CONTROL_CODE)	/* parse control code */
	{
	  switch (c & ~CONTROL_CODE)
	    {
	    case C_BEGIN_SUBSCRIPT:
	    case C_BEGIN_SUPERSCRIPT :
	      charsize *= SCRIPTSIZE;
	      break;
	      
	    case C_END_SUBSCRIPT:
	    case C_END_SUPERSCRIPT:
	      charsize /= SCRIPTSIZE;
	      break;
	      
	    case C_PUSH_LOCATION:
	      saved_width = width;
	      saved_charsize = charsize;
	      break;
	      
	    case C_POP_LOCATION:
	      width = saved_width;
	      charsize = saved_charsize;
	      break;
	      
	    case C_RIGHT_ONE_EM:
	      width += charsize * HERSHEY_EM;
	      break;
	      
	    case C_RIGHT_HALF_EM:
	      width += charsize * HERSHEY_EM / 2.0;
	      break;
	      
	    case C_RIGHT_QUARTER_EM:
	      width += charsize * HERSHEY_EM / 4.0;
	      break;
	      
	    case C_RIGHT_SIXTH_EM:
	      width += charsize * HERSHEY_EM / 6.0;
	      break;
	      
	    case C_RIGHT_EIGHTH_EM:
	      width += charsize * HERSHEY_EM / 8.0;
	      break;
	      
	    case C_RIGHT_TWELFTH_EM:
	      width += charsize * HERSHEY_EM / 12.0;
	      break;
	      
	    case C_LEFT_ONE_EM:
	      width -= charsize * HERSHEY_EM;
	      break;
	      
	    case C_LEFT_HALF_EM:
	      width -= charsize * HERSHEY_EM / 2.0;
	      break;
	      
	    case C_LEFT_QUARTER_EM:
	      width -= charsize * HERSHEY_EM / 4.0;
	      break;
	      
	    case C_LEFT_SIXTH_EM:
	      width -= charsize * HERSHEY_EM / 6.0;
	      break;
	      
	    case C_LEFT_EIGHTH_EM:
	      width -= charsize * HERSHEY_EM / 8.0;
	      break;
	      
	    case C_LEFT_TWELFTH_EM:
	      width -= charsize * HERSHEY_EM / 12.0;
	      break;
	      
	      /* unrecognized control code */
	    default:
	      break;
	    }
	}
      else			/* yow, an actual character */
	{
	  int raw_fontnum;
	  
	  /* compute index of font, in table in g_fontdb.c */
	  raw_fontnum = (c >> FONT_SHIFT) & ONE_BYTE;
	  
	  c &= ~FONT_SPEC;	/* extract character proper */
	  glyphnum = (_hershey_font_info[raw_fontnum].chars)[c];

	  /* could be a pseudo glyph number, e.g. an indication that
	     character is composite */
	  if (glyphnum == ACC0 || glyphnum == ACC1 || glyphnum == ACC2)
	    {
	      unsigned char composite, character, accent;

	      /* if so, use 1st element of composite character */
	      composite = (unsigned char)c;
	      if (_composite_char (&composite, &character, &accent))
		glyphnum = (_hershey_font_info[raw_fontnum].chars)[character];
	      else
		glyphnum = UNDE; /* hope this won't happen */
	    }

	  /* could also be a glyph number displaced by KS, to indicate
	     that this is a small kana */
	  if (glyphnum & KS)
	    glyphnum -= KS;

	  glyph = (const unsigned char *)(_occidental_hershey_glyphs[glyphnum]);
	  if (*glyph != '\0')	/* nonempty glyph */
	    /* 1st two chars are bounds */
	    width += charsize * ((int)glyph[1] - (int)glyph[0]);
	}
      
      ptr++;			/* bump pointer in string */
    }

  return width;
}  

/* _draw_hershey_penup_stroke() draws a penup stroke, along a vector
   specified in Hershey units.  Size scaling and obliquing (true/false) are
   specified.  This is used for repositioning during rendering of
   composite (accented) characters. */
void
#ifdef _HAVE_PROTOS
_draw_hershey_penup_stroke(R___(Plotter *_plotter) double dx, double dy, double charsize, bool oblique)
#else
_draw_hershey_penup_stroke(R___(_plotter) dx, dy, charsize, oblique)
     S___(Plotter *_plotter;)
     double dx, dy;
     double charsize;
     bool oblique;
#endif
{
  double shear;

  shear = oblique ? (SHEAR) : 0.0;
  _draw_hershey_stroke (R___(_plotter)
			false,	/* pen up */
			charsize * (dx + shear * dy), 
			charsize * dy);
}

/* _draw_hershey_glyph() invokes move() and cont() to draw a raw Hershey
   glyph, specified by index in the occidental or oriental glyph arrays.
   Size scaling and obliquing (true/false) are specified. */
void
#ifdef _HAVE_PROTOS
_draw_hershey_glyph (R___(Plotter *_plotter) int glyphnum, double charsize, int type, bool oblique)
#else
_draw_hershey_glyph (R___(_plotter) glyphnum, charsize, type, oblique)
     S___(Plotter *_plotter;)
     int glyphnum;
     double charsize;
     int type;
     bool oblique;
#endif
{
  double xcurr, ycurr;
  double xfinal, yfinal;
  bool pendown = false;
  const unsigned char *glyph;
  double dx, dy;
  double shear;
  
  shear = oblique ? (SHEAR) : 0.0;
  switch (type)
    {
    case OCCIDENTAL:
    default:
      glyph = (const unsigned char *)(_occidental_hershey_glyphs[glyphnum]);
      break;
    case ORIENTAL:
      glyph = (const unsigned char *)(_oriental_hershey_glyphs[glyphnum]); 
      break;
    }

  if (*glyph != '\0')	/* nonempty glyph */
    {
      xcurr = charsize * (double)glyph[0];
      xfinal = charsize * (double)glyph[1];
      ycurr = yfinal = 0.0;
      glyph += 2;
      while (*glyph)
	{
	  int xnewint;
	  
	  xnewint = (int)glyph[0];
	  
	  if (xnewint == (int)' ')
	    pendown = false;
	  else
	    {
	      double xnew, ynew;

	      xnew = (double)charsize * xnewint;
	      ynew = (double)charsize 
		* ((int)'R' 
		   - ((int)glyph[1] + (double)HERSHEY_BASELINE));
	      dx = xnew - xcurr;
	      dy = ynew - ycurr;
	      _draw_hershey_stroke (R___(_plotter) 
				    pendown, dx + shear * dy, dy);
	      xcurr = xnew, ycurr = ynew;
	      pendown = true;
	    }
	  
	  glyph +=2;	/* on to next pair */
	}
      
      /* final penup stroke, to end where we should */
      dx = xfinal - xcurr;
      dy = yfinal - ycurr;
      _draw_hershey_stroke (R___(_plotter) false, dx + shear * dy, dy);
    }
}

/* _draw_hershey_string() strokes a string beginning at present location,
   which is taken to be on the string's baseline.  Besides invoking move()
   and cont(), it invokes linewidth(). */
void
#ifdef _HAVE_PROTOS
_draw_hershey_string (R___(Plotter *_plotter) const unsigned short *string)
#else
_draw_hershey_string (R___(_plotter) string)
     S___(Plotter *_plotter;)
     const unsigned short *string;
#endif
{
  unsigned short c;
  const unsigned short *ptr = string;
  double charsize = 1.0;
  double saved_charsize = 1.0;
  double saved_position_x = _plotter->drawstate->pos.x;
  double saved_position_y = _plotter->drawstate->pos.y;
  double old_line_width;
  int line_width_type = 0;	/* 0,1,2 = unset,occidental,oriental */

  /* save line width (will restore at end) */
  old_line_width = _plotter->drawstate->line_width;

  while ((c = (*ptr++)) != '\0')
    {
      /* Check for the four possibilities: (1) a Hershey glyph specified by
	 glyph number, (2) an oriental Hershey glyph specified by glyph
	 number, (3) a control code, and (4) an ordinary font character,
	 which will be mapped to a Hershey glyph by one of the tables in
	 g_fontdb.c. */

      if (c & RAW_HERSHEY_GLYPH)
	{
	  if (line_width_type != 1)
	    {
	      _API_flinewidth (R___(_plotter)
				    HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_STROKE_WIDTH));
	      line_width_type = 1;
	    }
	  _draw_hershey_glyph (R___(_plotter)
			       c & GLYPH_SPEC, charsize, OCCIDENTAL, false);
	}

      else if (c & RAW_ORIENTAL_HERSHEY_GLYPH)
	{
	  if (line_width_type != 2)
	    {
	      _API_flinewidth (R___(_plotter)
				    HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_ORIENTAL_STROKE_WIDTH));
	      line_width_type = 2;
	    }
	  _draw_hershey_glyph (R___(_plotter)
			       c & GLYPH_SPEC, charsize, ORIENTAL, false);
	}

      else if (c & CONTROL_CODE)	
	switch (c & ~CONTROL_CODE) /* parse control codes */
	  {
	  case C_BEGIN_SUPERSCRIPT :
	    _draw_hershey_stroke (R___(_plotter)
				  false, 
				  SUPERSCRIPT_DX * charsize * HERSHEY_EM,
				  SUPERSCRIPT_DY * charsize * HERSHEY_EM);
	    charsize *= SCRIPTSIZE;
	    break;
		
	  case C_END_SUPERSCRIPT:
	    charsize /= SCRIPTSIZE;
	    _draw_hershey_stroke (R___(_plotter)
				  false, 
				  - SUPERSCRIPT_DX * charsize * HERSHEY_EM,
				  - SUPERSCRIPT_DY * charsize * HERSHEY_EM);
	    break;
		
	  case C_BEGIN_SUBSCRIPT:
	    _draw_hershey_stroke (R___(_plotter)
				  false, 
				  SUBSCRIPT_DX * charsize * HERSHEY_EM,
				  SUBSCRIPT_DY * charsize * HERSHEY_EM);
	    charsize *= SCRIPTSIZE;
	    break;
		
	  case C_END_SUBSCRIPT:
	    charsize /= SCRIPTSIZE;
	    _draw_hershey_stroke (R___(_plotter)
				  false, 
				  - SUBSCRIPT_DX * charsize * HERSHEY_EM,
				  - SUBSCRIPT_DY * charsize * HERSHEY_EM);
	    break;
		
	  case C_PUSH_LOCATION:
	    saved_charsize = charsize;
	    saved_position_x = _plotter->drawstate->pos.x;
	    saved_position_y = _plotter->drawstate->pos.y;
	    break;
		
	  case C_POP_LOCATION:
	    charsize = saved_charsize;
	    _API_fmove (R___(_plotter)
			     saved_position_x, saved_position_y);
	    break;
		
	  case C_RIGHT_ONE_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, charsize * HERSHEY_EM, 0.0);
	    break;
		
	  case C_RIGHT_HALF_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, charsize * HERSHEY_EM / 2.0, 0.0);
	    break;
		
	  case C_RIGHT_QUARTER_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, charsize * HERSHEY_EM / 4.0, 0.0);
	    break;
		
	  case C_RIGHT_SIXTH_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, charsize * HERSHEY_EM / 6.0, 0.0);
	    break;
		
	  case C_RIGHT_EIGHTH_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, charsize * HERSHEY_EM / 8.0, 0.0);
	    break;
		
	  case C_RIGHT_TWELFTH_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, charsize * HERSHEY_EM / 12.0, 0.0);
	    break;
		
	  case C_LEFT_ONE_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, - charsize * HERSHEY_EM, 0.0);
	    break;
		
	  case C_LEFT_HALF_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, - charsize * HERSHEY_EM / 2.0, 0.0);
	    break;
		
	  case C_LEFT_QUARTER_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, - charsize * HERSHEY_EM / 4.0, 0.0);
	    break;
		
	  case C_LEFT_SIXTH_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, - charsize * HERSHEY_EM / 6.0, 0.0);
	    break;
		
	  case C_LEFT_EIGHTH_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, - charsize * HERSHEY_EM / 8.0, 0.0);
	    break;

	  case C_LEFT_TWELFTH_EM:
	    _draw_hershey_stroke (R___(_plotter)
				  false, - charsize * HERSHEY_EM / 12.0, 0.0);
	    break;
		
	    /* unrecognized control code, punt */
	  default:
	    break;
	  }
      
      else
	/* yow, an actual font character!  Several possibilities: could be
	   a composite (accented) character, could be a small Kana, or
	   could be a garden-variety character. */
	{
	  int raw_fontnum;
	  int glyphnum;		/* glyph in Hershey array */
	  int char_glyphnum, accent_glyphnum; /* for composite chars */
	  int char_width, accent_width; /* for composite chars */
	  const unsigned char *char_glyph, *accent_glyph;
	  unsigned char composite, character, accent;
	  bool oblique, small_kana = false;
	  
	  /* compute index of font, in font table in g_fontdb.c */
	  raw_fontnum = (c >> FONT_SHIFT) & ONE_BYTE;
	  /* shear font?  (for HersheySans-Oblique, etc.) */
	  oblique = _hershey_font_info[raw_fontnum].obliquing;
	  
	  c &= ~FONT_SPEC;	/* extract character proper */
	  glyphnum = (_hershey_font_info[raw_fontnum].chars)[c];
	  
	  if (glyphnum & KS) /* a small kana? */
	    {
	      glyphnum -= KS;
	      small_kana = true;
	    }
	  
	  switch (glyphnum)
	    {
	      /* special case: this is a composite (accented) character;
		 search font table in g_fontdb.c for it */
	    case ACC0:
	    case ACC1:
	    case ACC2:
	      composite = (unsigned char)c;
	      if (_composite_char (&composite, &character, &accent))
		{
		  char_glyphnum = 
		    (_hershey_font_info[raw_fontnum].chars)[character];
		  accent_glyphnum = 
		    (_hershey_font_info[raw_fontnum].chars)[accent];
		}
	      else
		{		/* hope this won't happen */
		  char_glyphnum = UNDE; 
		  accent_glyphnum = 0;
		}
	      char_glyph = 
		(const unsigned char *)_occidental_hershey_glyphs[char_glyphnum];
	      accent_glyph = 
		(const unsigned char *)_occidental_hershey_glyphs[accent_glyphnum];
	  
	      if (*char_glyph != '\0') /* nonempty glyph */
		/* 1st two chars are bounds, in Hershey units */
		char_width = (int)char_glyph[1] - (int)char_glyph[0];
	      else
		char_width = 0;

	      if (*accent_glyph != '\0') /* nonempty glyph */
		/* 1st two chars are bounds, in Hershey units */
		accent_width = (int)accent_glyph[1] - (int)accent_glyph[0];
	      else
		accent_width = 0;

	      /* draw the character */
	      if (line_width_type != 1)
		{
		  _API_flinewidth (R___(_plotter)
					HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_STROKE_WIDTH));
		  line_width_type = 1;
		}
	      _draw_hershey_glyph (R___(_plotter)
				   char_glyphnum, charsize, 
				   OCCIDENTAL, oblique);
	      /* back up to draw accent */
	      _draw_hershey_penup_stroke (R___(_plotter)
					  -0.5 * (double)char_width
					  -0.5 * (double)accent_width,
					  0.0, charsize, oblique);

	      /* repositioning for uppercase and uppercase italic */
	      if (glyphnum == ACC1)
		_draw_hershey_penup_stroke (R___(_plotter)
					    0.0, 
					    (double)(ACCENT_UP_SHIFT),
					    charsize, oblique);
	      else if (glyphnum == ACC2)
		_draw_hershey_penup_stroke (R___(_plotter)
					    (double)(ACCENT_RIGHT_SHIFT),
					    (double)(ACCENT_UP_SHIFT),
					    charsize, oblique);

	      /* draw the accent */
	      _draw_hershey_glyph (R___(_plotter)
				   accent_glyphnum, charsize, 
				   OCCIDENTAL, oblique);

	      /* undo special repositioning if any */
	      if (glyphnum == ACC1)
		_draw_hershey_penup_stroke (R___(_plotter)
					    0.0, 
					    -(double)(ACCENT_UP_SHIFT),
					    charsize, oblique);
	      else if (glyphnum == ACC2)
		_draw_hershey_penup_stroke (R___(_plotter)
					    -(double)(ACCENT_RIGHT_SHIFT),
					    -(double)(ACCENT_UP_SHIFT),
					    charsize, oblique);

	      /* move forward, to end composite char where we should */
	      _draw_hershey_penup_stroke (R___(_plotter)
					  0.5 * (double)char_width
					  -0.5 * (double)accent_width,
					  0.0, charsize, oblique);
	      break;

	      /* not a composite (accented) character; just an ordinary
		 glyph from occidental+Kana array (could be a Kana, in
		 particular, could be a small Kana) */
	    default:
	      if (small_kana)
		{
		  int kana_width;
		  const unsigned char *kana_glyph;
		  double shift = 0.5 * (1.0 - (SMALL_KANA_SIZE));

		  kana_glyph = 
		    (const unsigned char *)_occidental_hershey_glyphs[glyphnum];
		  kana_width = (int)kana_glyph[1] - (int)kana_glyph[0];

		  /* draw small Kana, preceded and followed by a penup
		     stroke in order to traverse the full width of an
		     ordinary Kana */
		  _draw_hershey_penup_stroke (R___(_plotter)
					      shift * (double)kana_width,
					      0.0, charsize, oblique);
		  if (line_width_type != 2)
		    {
		      _API_flinewidth (R___(_plotter)
					    HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_ORIENTAL_STROKE_WIDTH));
		      line_width_type = 2;
		    }
		  _draw_hershey_glyph (R___(_plotter)
				       glyphnum, 
				       (SMALL_KANA_SIZE) * charsize,
				       OCCIDENTAL, oblique);
		  _draw_hershey_penup_stroke (R___(_plotter)
					      shift * (double)kana_width,
					      0.0, charsize, oblique);
		}
	      else
		/* whew! just an ordinary glyph from the occidental array
		   (could be a Kana however, since they're confusingly
		   placed in that array, at the end) */
		{
		  if (glyphnum >= BEGINNING_OF_KANA)
		    {
		      if (line_width_type != 2)
			{
			  _API_flinewidth (R___(_plotter)  
						HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_ORIENTAL_STROKE_WIDTH));
			  line_width_type = 2;
			}
		    }
		  else
		      if (line_width_type != 1)
			{
			  _API_flinewidth (R___(_plotter)
						HERSHEY_UNITS_TO_USER_UNITS (HERSHEY_STROKE_WIDTH));
			  line_width_type = 1;
			}
		_draw_hershey_glyph (R___(_plotter)
				     glyphnum, charsize, 
				     OCCIDENTAL, oblique);
		}
	      break;
	    } /* end of case statement that switches based on glyphnum */

	} /* end of font character case */

    } /* end of loop through unsigned shorts in the codestring */
  
  if (line_width_type != 0)
    /* must restore old line width */
    _API_flinewidth (R___(_plotter) old_line_width);
  
  return;
}

/* retrieve the two elements of a composite character from the table in
   g_fontdb.c */
static bool
#ifdef _HAVE_PROTOS
_composite_char (unsigned char *composite, unsigned char *character, unsigned char *accent)
#else
_composite_char (composite, character, accent)
     unsigned char *composite;
     unsigned char *character, *accent;
#endif
{
  const struct plHersheyAccentedCharInfoStruct *compchar = _hershey_accented_char_info;
  bool found = false;
  unsigned char given = *composite;
  
  while (compchar->composite)
    {
      if (compchar->composite == given)
	{
	  found = true;
	  /* return char and accent via pointers */
	  *character = compchar->character;
	  *accent = compchar->accent;
	}
      compchar++;
    }

  return found;
}