File: g_cntrlify.c

package info (click to toggle)
plotutils 2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 13,156 kB
  • ctags: 7,141
  • sloc: ansic: 68,670; sh: 20,082; cpp: 12,382; yacc: 2,588; makefile: 889; lex: 137
file content (816 lines) | stat: -rw-r--r-- 27,757 bytes parent folder | download | duplicates (7)
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
/* This file is part of the GNU plotutils package.  Copyright (C) 1995,
   1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.

   The GNU plotutils package is free software.  You may redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software foundation; either version 2, or (at your
   option) any later version.

   The GNU plotutils package is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with the GNU plotutils package; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
   Boston, MA 02110-1301, USA. */

/* _pl_g_controlify() converts a "label" (i.e. a character string), which may
   contain troff-like escape sequences, into a string of unsigned shorts.
   The possible troff-like escape sequences are listed in g_cntrlify.h.

   This conversion is to facilitate rendering.  _pl_g_controlify() is
   called by alabel(), and the converted label is rendered by
   _alabel_standard(), _alabel_stroke(), or _alabel_device(), depending on
   what sort of font is currently selected.  See g_alabel.c
   (_pl_g_controlify() is called by labelwidth() too).

   If the currently selected font is a font with ISO-Latin-1 encoding, the
   valid escape sequences include escape sequences for the non-ASCII
   ISO-Latin-1 characters.  Also allowed are such escape sequences as \f0,
   \f1, \f2, \f3, \f4, etc., which switch among the various fonts in the
   current typeface:

   	\f1	Switch to font #1, basic
	\f2	Switch to font #2, italic
	\f3	Switch to font #3, bold
	\f4	Switch to font #4, bold italic
	\f0	Switch to font #0, symbol (including Greek characters)
   
   \fP switches to the previously used font (there is a depth-1 stack,
   i.e. only one previous font is remembered, as in troff).

   All typefaces include at least two fonts: fonts #0 and #1.  Some may
   include more than the above five.  Each unsigned short in the converted
   string is really an annotated character: the low byte is the character,
   and the high byte is the font number.

   Besides font annotations, the controlified string may include control
   codes (unsigned shorts with particular bit patterns in their high
   bytes), which are produced by the escape sequences:

        \sp  start superscript
        \ep  end superscript

        \sb  start subscript
        \eb  end subscript

        \mk  mark location
        \rt  return to mark 
	     [useful e.g. for underlining, and filling square roots]

    There are also control codes for horizontal shifts.  \r1, \r2, \r4,
    \r6, \r8, \r^ will produce control codes that will shift right by 1 em,
    1/2 em, 1/4 em, 1/6 em, 1/8 em, 1/12 em, respectively.  \l1, \l2, \l4,
    \l6, \l8, \l^ are similar.

    The string of unsigned shorts, which is returned, is allocated with
    malloc and may be freed later. */

#include "sys-defines.h"
#include "extern.h"
#include "g_control.h"
#include "g_cntrlify.h"
#include "g_jis.h"

/* these two array lengths must agree with values in file g_her_glyph.c */
#define NUM_OCCIDENTAL_HERSHEY_GLYPHS 4400
#define NUM_ORIENTAL_HERSHEY_GLYPHS 5500

unsigned short *
_pl_g_controlify (R___(Plotter *_plotter) const unsigned char *src)
{     
  unsigned short *dest;
  unsigned char c, d;
  unsigned char esc[3];
  int j = 0;			/* index into destination string */
  int raw_fontnum, raw_symbol_fontnum;
  int previous_raw_fontnum;	/* implement depth-1 stack */
  unsigned short fontword, symbol_fontword;
  
  /* note: string length can grow by a factor of 6, because a single
     printable character can be mapped to a sequence of unsigned shorts, of
     length up to 6 (see comment below) */
  dest = (unsigned short *)_pl_xmalloc ((6 * strlen ((char *)src) + 1) * sizeof(unsigned short));
  
  /* Determine initial number of font, as index into low-level table in
     g_fontdb.c, and the initial value for the shifted `font word' which
     we'll OR with each character; also same, for associated symbol font.
     May be updated by \f0, \f1, etc. */
  switch (_plotter->drawstate->font_type)
    {
    case PL_F_POSTSCRIPT:
    default:
      raw_fontnum = _pl_g_ps_typeface_info[_plotter->drawstate->typeface_index].fonts[_plotter->drawstate->font_index];
      raw_symbol_fontnum = _pl_g_ps_typeface_info[_plotter->drawstate->typeface_index].fonts[0];
      break;
    case PL_F_PCL:
      raw_fontnum = _pl_g_pcl_typeface_info[_plotter->drawstate->typeface_index].fonts[_plotter->drawstate->font_index];
      raw_symbol_fontnum = _pl_g_pcl_typeface_info[_plotter->drawstate->typeface_index].fonts[0];
      break;
    case PL_F_STICK:
      raw_fontnum = _pl_g_stick_typeface_info[_plotter->drawstate->typeface_index].fonts[_plotter->drawstate->font_index];
      raw_symbol_fontnum = _pl_g_stick_typeface_info[_plotter->drawstate->typeface_index].fonts[0];
      break;
    case PL_F_HERSHEY:
      raw_fontnum = _pl_g_hershey_typeface_info[_plotter->drawstate->typeface_index].fonts[_plotter->drawstate->font_index];
      raw_symbol_fontnum = _pl_g_hershey_typeface_info[_plotter->drawstate->typeface_index].fonts[0];
      break;
    case PL_F_OTHER:
      /* no real font table in this case; by convention font #1 internally
         means the current device-specific font and #0 is an associated
         symbol font */
      raw_fontnum = 1;
      raw_symbol_fontnum = 0;
      break;
    }
  /* Of the following two words, `fontword' is updated whenever an escape
     sequence like \f0, \f1, \f2 etc. is seen, since raw_fontnum is itself
     updated.  But `symbol_fontword' is fixed */
  fontword = ((unsigned short)raw_fontnum) << FONT_SHIFT;
  symbol_fontword = ((unsigned short)raw_symbol_fontnum) << FONT_SHIFT;

  /* Implement depth-1 stack of fonts, for use by \fP macro */
  previous_raw_fontnum = raw_fontnum;

  while (*src != (unsigned char)'\0')
    {
      /* If EUC, check first for high bit and process two-byte characters
	 separately.  This approach is awkward (we duplicate a lot of code
	 here, which appears elsewhere below). */

      if ((raw_fontnum == PL_HERSHEY_EUC) 
	  && (*src & 0x80) && (*(src + 1) & 0x80))
	{
	  unsigned char jis_row = *src & ~(0x80);
	  unsigned char jis_col = *(src + 1) & ~(0x80);

	  if (GOOD_JIS_INDEX(jis_row, jis_col))
	    {
	      int jis_glyphindex = 256 * jis_row + jis_col;
	  
	      if (jis_glyphindex >= BEGINNING_OF_KANJI)
		/* in Kanji range, so check if we have it */
		{
#ifndef NO_KANJI
		  const struct kanjipair *kanji = _builtin_kanji_glyphs;
		  bool matched = false;
		  
		  while (kanji->jis != 0)
		    {
		      if (jis_glyphindex == kanji->jis)
			{
			  matched = true;
			  break;
			}
		      kanji++;
		    }
		  if (matched)
		    {
		      dest[j++] = RAW_ORIENTAL_HERSHEY_GLYPH | (kanji->nelson);
		      src += 2;
		      continue;	/* back to top of while loop */
		    }
		  else		/* a Kanji we don't have */
		    {
		      /* render as standard `undefined character' glyph */
		      dest[j++] = RAW_HERSHEY_GLYPH | UNDE;
		      src += 2;
		      continue;	/* back to top of while loop */
		    }
#endif /* not NO_KANJI */
		}
	      else
		/* not in Kanji range, so look for it in char table */
		{
		  const struct jis_entry *char_mapping = _builtin_jis_chars;
		  bool matched = false;
		  
		  while (char_mapping->jis != 0)
		    {
		      if (jis_glyphindex == char_mapping->jis)
			{
			  matched = true;
			  break;
			}
		      char_mapping++;
		    }
		  if (matched)
		    /* the entry in the character table maps the JIS
		       character to a character (in 0..255 range) in
		       one of the fonts in the master table in g_fontdb.c */
		    {
		      int fontnum = char_mapping->font;
		      unsigned short charnum = char_mapping->charnum;
		      
		      if (charnum & RAW_HERSHEY_GLYPH)
			/* a raw Hershey glyph, not in any font */
			dest[j++] = RAW_HERSHEY_GLYPH | charnum;
		      else
			/* a character in one of the fonts in g_fontdb.c */
			dest[j++] = (((unsigned short)fontnum) << FONT_SHIFT) | charnum;
		      src += 2;
		      continue; /* back to top of while loop */
		    }
		  else	/* a character we don't have */
		    {
		      /* render as standard `undefined character' glyph */
		      dest[j++] = RAW_HERSHEY_GLYPH | UNDE;
		      src += 2;
		      continue;	/* back to top of while loop */
		    }
		}
	    }
	  else
	    /* JIS index is OOB */
	    {
	      src += 2;
	      continue;		/* back to top of while loop */
	    }
	}

      /* if current font is Hershey, first try to match each ligature
         pattern (no ligatures supported in non-Hershey fonts) */
      if (_plotter->drawstate->font_type == PL_F_HERSHEY)
	{
	  int i;
	  bool matched = false;
	  
	  for (i = 0; i < NUM_LIGATURES; i++) 
	    if ((_ligature_tbl[i].font == raw_fontnum)
		&& (strncmp ((char *)src, _ligature_tbl[i].from, 
			     strlen (_ligature_tbl[i].from)) == 0))
	      {
		matched = true;
		break;
	      }
	  
	  if (matched)
	    {
	      dest[j++] = fontword | (unsigned short)_ligature_tbl[i].byte;
	      src += strlen (_ligature_tbl[i].from);
	      continue;		/* back to top of while loop */
	    }
	}

      c = *(src++);		/* no ligature, so get single new char */
      if (c != (unsigned char)'\\') /* ordinary char, may pass through */
	{
	  /* if current font is an ISO-Latin-1 Hershey font ... */
	  if (_plotter->drawstate->font_type == PL_F_HERSHEY
	      && _pl_g_hershey_font_info[raw_fontnum].iso8859_1)
	    {
	      int i;
	      bool matched = false;

	      /* check if this is a `raised' ISO-Latin-1 character */
	      for (i = 0; i < NUM_RAISED_CHARS; i++) 
		if (c == _raised_char_tbl[i].from)
		  {
		    matched = true;
		    break;
		  }
	      if (matched)	/* it's a raised character */
		{
		  /* map to string of unsigned shorts, length 3 or 6:
		     `begin superscript' control code, [`mark'
		     control code,] replacement char, [`return'
		     control code, underline,] `end superscript' */
		  dest[j++] = 
		    (unsigned short) (CONTROL_CODE | C_BEGIN_SUPERSCRIPT);
		  if (_raised_char_tbl[i].underscored) /* also underline */
		    {
		      dest[j++] = 
			(unsigned short) (CONTROL_CODE | C_PUSH_LOCATION);
		      dest[j++] = 
			fontword | (unsigned short)_raised_char_tbl[i].to;
		      dest[j++] = 
			(unsigned short) (CONTROL_CODE | C_POP_LOCATION);
		      /* select appropriate HersheySymbol font */
		      dest[j++] = 
			symbol_fontword | (unsigned short)VECTOR_SYMBOL_FONT_UNDERSCORE;
		    }
		  else	/* just print raised char, no underline */
		    dest[j++] = 
		      fontword | (unsigned short)_raised_char_tbl[i].to;
		  
		  dest[j++] = 
		    (unsigned short) (CONTROL_CODE | C_END_SUPERSCRIPT);
		  
		  continue; /* back to top of while loop */
		}

	      /* since current font is an ISO-Latin-1 Hershey font, also
                 check if this char should be deligatured */
	      for (i = 0; i < NUM_DELIGATURED_CHARS; i++) 
		if (c == _deligature_char_tbl[i].from)
		  {
		    matched = true;
		    break;
		  }
	      if (matched)
		{
		  if (_deligature_char_tbl[i].except_font != raw_fontnum)
		    {
		      dest[j++] = fontword 
			| (unsigned short)_deligature_char_tbl[i].to[0];
		      dest[j++] = fontword 
			| (unsigned short)_deligature_char_tbl[i].to[1];
		      continue;	/* back to top of while loop */
		    }
		}
	    }
	  
	  /* didn't do anything special, so just pass the character thru */
	  dest[j++] = fontword | (unsigned short)c; 
	  continue;		/* back to top of while loop */
	}
      else			/* character is a backslash */
	{
	  c = *(src++);		/* grab next character */
	  if (c == (unsigned char)'\0')	/* ASCII NUL ? */
	    {
	      dest[j++] = fontword | (unsigned short)'\\';
	      break;		/* string terminated with a backslash */
	    }
	  
 	  if (c == (unsigned char)'\\')
	    {
	      dest[j++] = fontword | (unsigned short)'\\';
	      dest[j++] = fontword | (unsigned short)'\\';
	      continue;		/* saw \\, leave as is */
	    }

	  d = *(src++);
 	  if (d == (unsigned char)'\0')
	    {
	      dest[j++] = fontword | (unsigned short)'\\';
	      dest[j++] = fontword | (unsigned short)c;
	      break;		/* string terminated with \c */
	    }

	  esc[0] = c;
	  esc[1] = d;
	  esc[2] = (unsigned char)'\0';	/* have an escape sequence */
	  
	  /* is this an escape seq. (e.g. \#H0001) for a raw Hershey glyph? */
	  if (_plotter->drawstate->font_type == PL_F_HERSHEY
	      && esc[0] == '#' && esc[1] == 'H'
	      && src[0] >= '0' && src[0] <= '9'
	      && src[1] >= '0' && src[1] <= '9'
	      && src[2] >= '0' && src[2] <= '9'
	      && src[3] >= '0' && src[3] <= '9')
	    {
	      int glyphindex;

	      glyphindex = (src[3] - '0') + 10 * (src[2] - '0')
		+ 100 * (src[1] - '0') + 1000 * (src[0] - '0');
	      if (glyphindex < NUM_OCCIDENTAL_HERSHEY_GLYPHS)
		{
		  dest[j++] = RAW_HERSHEY_GLYPH | glyphindex;
		  src += 4;
		  continue;	/* back to top of while loop */
		}
	    }

#ifndef NO_KANJI
	  /* is this an escape seq. (e.g. \#N0001) for a raw Japanese
             Hershey glyph (Kanji), as numbered in Nelson's dictionary? */
	  if (_plotter->drawstate->font_type == PL_F_HERSHEY
	      && esc[0] == '#' && esc[1] == 'N'
	      && src[0] >= '0' && src[0] <= '9'
	      && src[1] >= '0' && src[1] <= '9'
	      && src[2] >= '0' && src[2] <= '9'
	      && src[3] >= '0' && src[3] <= '9')
	    {
	      int glyphindex;

	      glyphindex = (src[3] - '0') + 10 * (src[2] - '0')
		+ 100 * (src[1] - '0') + 1000 * (src[0] - '0');
	      if (glyphindex < NUM_ORIENTAL_HERSHEY_GLYPHS)
		{
		  dest[j++] = RAW_ORIENTAL_HERSHEY_GLYPH | glyphindex;
		  src += 4;
		  continue;	/* back to top of while loop */
		}
	    }
#endif /* not NO_KANJI */

	  /* is this an escape seq. (e.g. \#J0001) for a raw Japanese
             Hershey glyph (JIS numbering, in hex)? */
	  if (_plotter->drawstate->font_type == PL_F_HERSHEY
	      && esc[0] == '#' && esc[1] == 'J'
	      && ((src[0] >= '0' && src[0] <= '9')
		  || (src[0] >= 'a' && src[0] <= 'f')
		  || (src[0] >= 'A' && src[0] <= 'F'))
	      && ((src[1] >= '0' && src[1] <= '9')
		  || (src[1] >= 'a' && src[1] <= 'f')
		  || (src[1] >= 'A' && src[1] <= 'F'))
	      && ((src[2] >= '0' && src[2] <= '9')
		  || (src[2] >= 'a' && src[2] <= 'f')
		  || (src[2] >= 'A' && src[2] <= 'F'))
	      && ((src[3] >= '0' && src[3] <= '9')
		  || (src[3] >= 'a' && src[3] <= 'f')
		  || (src[3] >= 'A' && src[3] <= 'F')))
	    {
	      int jis_glyphindex;
	      int i, hexnum[4];
	      int jis_row, jis_col;
	      
	      for (i = 0; i < 4; i++)
		if (src[i] >= 'a' && src[i] <= 'f')
		  hexnum[i] = 10 + src[i] - 'a';
		else if (src[i] >= 'A' && src[i] <= 'F')
		  hexnum[i] = 10 + src[i] - 'A';
		else /* a decimal digit */
		  hexnum[i] = src[i] - '0';

	      jis_glyphindex = (hexnum[3] + 16 * hexnum[2]
				+ 256 * hexnum[1] + 4096 * hexnum[0]);
	      jis_row = hexnum[1] + 16 * hexnum[0];
	      jis_col = hexnum[3] + 16 * hexnum[2];

	      if (GOOD_JIS_INDEX(jis_row, jis_col))
		{
		  if (jis_glyphindex >= BEGINNING_OF_KANJI)
		    /* in Kanji range, so check if we have it */
		    {
#ifndef NO_KANJI
		      const struct kanjipair *kanji = _builtin_kanji_glyphs;
		      bool matched = false;
		      
		      while (kanji->jis != 0)
			{
			  if (jis_glyphindex == kanji->jis)
			    {
			      matched = true;
			      break;
			    }
			  kanji++;
			}
		      if (matched)
			{
			  dest[j++] = RAW_ORIENTAL_HERSHEY_GLYPH | (kanji->nelson);
			  src += 4;
			  continue;	/* back to top of while loop */
			}
		      else		/* a Kanji we don't have */
			{
			  /* render as standard `undefined character' glyph */
			  dest[j++] = RAW_HERSHEY_GLYPH | UNDE;
			  src += 4;
			  continue;	/* back to top of while loop */
			}
#endif /* not NO_KANJI */
		    }
		  else
		    /* not in Kanji range, so look for it in char table */
		    {
		      const struct jis_entry *char_mapping = _builtin_jis_chars;
		      bool matched = false;
		      
		      while (char_mapping->jis != 0)
			{
			  if (jis_glyphindex == char_mapping->jis)
			    {
			      matched = true;
			      break;
			    }
			  char_mapping++;
			}
		      if (matched)
			/* the entry in the character table maps the JIS
			   character to a character (in 0..255 range) in
			   one of the fonts in the master table in g_fontdb.c*/
			{
			  int fontnum = char_mapping->font;
			  unsigned short charnum = char_mapping->charnum;
			  
			  if (charnum & RAW_HERSHEY_GLYPH)
			    /* a raw Hershey glyph, not in any font */
			    dest[j++] = RAW_HERSHEY_GLYPH | charnum;
			  else
			    /* a character in one of the fonts in g_fontdb.c */
			    dest[j++] = (((unsigned short)fontnum) << FONT_SHIFT) | charnum;
			  src += 4;
			  continue; /* back to top of while loop */
			}
		      else	/* a character we don't have */
			{
			  /* render as standard `undefined character' glyph */
			  dest[j++] = RAW_HERSHEY_GLYPH | UNDE;
			  src += 4;
			  continue;	/* back to top of while loop */
			}
		    }
		}
	    }

	  {
	    int i;
	    bool matched = false;

	    /* is this an escape seq. for a control code? */
	    for (i = 0; i < NUM_CONTROLS; i++) 
	      if (strcmp ((char *)esc, _control_tbl[i]) == 0)
		{
		  matched = true;
		  break;
		}
	    if (matched)		/* it's a control code */
	      {
		dest[j++] = CONTROL_CODE | i;
		continue;	/* back to top of while loop */
	      }
	  }

	  /* if current font is an ISO-Latin-1 Hershey font, is this an
	     escape sequence for an 8-bit (non-ASCII) char, which due to
	     nonexistence should be deligatured? */
	  if (_plotter->drawstate->font_type == PL_F_HERSHEY
	      && _pl_g_hershey_font_info[raw_fontnum].iso8859_1)
	    {
	      int i;
	      bool matched = false;
	     
	      for (i = 0; i < NUM_DELIGATURED_ESCAPES; i++)
		if (strcmp ((char *)esc, _deligature_escape_tbl[i].from) == 0)
		  {
		    matched = true;
		    break;
		  }
	      if (matched)
		{
		  if (_deligature_escape_tbl[i].except_font != raw_fontnum)
		    {
		      dest[j++] = fontword 
			| (unsigned short)_deligature_escape_tbl[i].to[0];
		      dest[j++] = fontword 
			| (unsigned short)_deligature_escape_tbl[i].to[1];

		      continue;	/* back to top of while loop */
		    }
		}
	    }

	  /* if the current font is an ISO-Latin-1 font (no matter whether
             font is a a Hershey font, a PS or PCL/Stick font, or a
             device-specific font for which we have no table entry), is
             this an escape seq. for an 8-bit (non-ASCII) ISO8859-1 char?  */
	  if ((_plotter->drawstate->font_type == PL_F_POSTSCRIPT
	       && _pl_g_ps_font_info[raw_fontnum].iso8859_1)
	      || (_plotter->drawstate->font_type == PL_F_HERSHEY
		  && _pl_g_hershey_font_info[raw_fontnum].iso8859_1)
	      || (_plotter->drawstate->font_type == PL_F_PCL
		  && _pl_g_pcl_font_info[raw_fontnum].iso8859_1)
	      || (_plotter->drawstate->font_type == PL_F_STICK
		  && _pl_g_stick_font_info[raw_fontnum].iso8859_1)
	      || (_plotter->drawstate->font_type == PL_F_OTHER
		  && _plotter->drawstate->font_is_iso8859_1
		  && raw_fontnum == 1))
	    {
	      int i;
	      bool matched = false;

	      for (i = 0; i < NUM_ISO_ESCAPES; i++) 
		if (strcmp ((char *)esc, _iso_escape_tbl[i].string) == 0)
		  {
		    matched = true;
		    break;
		  }
	      if (matched)	/* it's an 8-bit ISO8859-1 character */
		{
		  /* certain such characters are drawn in the Hershey fonts
                     as superscripts */
		  if (_plotter->drawstate->font_type == PL_F_HERSHEY)
		    {
		      int k;
		      bool matched2 = false;
		      
		      /* check if this is a `raised' ISO-Latin-1 character */
		      for (k = 0; k < NUM_RAISED_CHARS; k++) 
			if (_iso_escape_tbl[i].byte == _raised_char_tbl[k].from)
			  {
			    matched2 = true;
			    break;
			  }
		      if (matched2)	/* it's a raised character */
			{
			  /* map to string of unsigned shorts, length 3 or 6:
			     `begin superscript' control code, [`mark'
			     control code,] replacement char, [`return'
			     control code, underline,] `end superscript' */
			  dest[j++] = 
			    (unsigned short) (CONTROL_CODE | C_BEGIN_SUPERSCRIPT);
			  if (_raised_char_tbl[k].underscored) /* also underline */
			    {
			      dest[j++] = 
				(unsigned short) (CONTROL_CODE | C_PUSH_LOCATION);
			      dest[j++] = 
				fontword | (unsigned short)_raised_char_tbl[k].to;
			      dest[j++] = 
				(unsigned short) (CONTROL_CODE | C_POP_LOCATION);
			      /* select appropriate HersheySymbol font */
			      dest[j++] = 
				symbol_fontword | (unsigned short)VECTOR_SYMBOL_FONT_UNDERSCORE;
			    }
			  else	/* just print raised char, no underline */
			    {
			      dest[j++] = 
				fontword | (unsigned short)_raised_char_tbl[k].to;
			    }

			  dest[j++] = 
			    (unsigned short) (CONTROL_CODE | C_END_SUPERSCRIPT);

			  continue; /* back to top of while loop */
			}
		    }

		  /* won't draw this char as a superscript; just pass thru */
		  dest[j++] = fontword | (unsigned short)(_iso_escape_tbl[i].byte);
		  continue;	/* back to top of while loop */
		}
	    }

	  /* is this an escape seq. for a `special' (non-ISO, non-Symbol)
	     Hershey glyph?  Such glyphs include astronomical signs, and
	     `final s'. */
	  if (_plotter->drawstate->font_type == PL_F_HERSHEY)
	    {
	      int i;
	      bool matched = false;

	      for (i = 0; i < NUM_SPECIAL_ESCAPES; i++) 
		if (strcmp ((char *)esc, _special_escape_tbl[i].string) == 0)
		  {
		    matched = true;
		    break;
		  }
	      if (matched)	/* it's a special character */
		{
		  /* "\s-" is special; yields character in current font */
		  if (_special_escape_tbl[i].byte == FINAL_LOWERCASE_S)
		  dest[j++] = 
		    fontword | (unsigned short)(_special_escape_tbl[i].byte);
		  else
		  /* we select symbol font of typeface, in which we've
		     stored all other special characters */
		    dest[j++] = symbol_fontword | (unsigned short)(_special_escape_tbl[i].byte);
		  continue;	/* back to top of while loop */
		}
	    }

	  {
	    int i;
	    bool matched = false;
	    
	    /* Irrespective of font type, is this an escape seq. for a char
	       in the font's corresponding symbol font? */
	    for (i = 0; i < NUM_SYMBOL_ESCAPES; i++) 
	      if (strcmp (_symbol_escape_tbl[i].string, "NO_ABBREV") != 0
		  && strcmp ((char *)esc, _symbol_escape_tbl[i].string) == 0)
		{
		  matched = true;
		  break;
		}
	    if (matched)	/* it's a character in the symbol font */
	      {
		/* select symbol font by OR'ing in the symbol fontword */
		dest[j++] = symbol_fontword | (unsigned short)(_symbol_escape_tbl[i].byte);
		continue;	/* back to top of while loop */
	      }
	  }

	  /* Gross kludge.  In the non-Hershey fonts we handle the "\rn"
	     control sequence in a painful way.  For a PS font we map it
	     into (1) a left shift, (2) the `radicalex' character in the PS
	     Symbol font, and (3) a right shift.  Shift distances are taken
	     from the bbox of the radicalex char, and are slightly larger
	     than 0.5 em.  For a PCL font it's similar, but the shifts are
	     much smaller.  The reason it's different for PCL is that the
	     PCL radicalex character is different from the PS radicalex
	     character: the overbar is not displaced.  Possibly someone at
	     HP made a mistake while reimplementing the Adobe Symbol font
	     for PCL 5?

	     We don't implement \rn for Stick fonts, because they have
	     no associated symbol font. */
	  if (strcmp ((char *)esc, "rn") == 0)
	    {
	      if (_plotter->drawstate->font_type == PL_F_POSTSCRIPT
		  || _plotter->drawstate->font_type == PL_F_PCL)
		{
		  dest[j++] 
		    = (unsigned short)(CONTROL_CODE | C_LEFT_RADICAL_SHIFT);
		  /* take `radicalex' glyph from PS symbol font */
		  dest[j++] 
		    = symbol_fontword | (unsigned short)RADICALEX; 
		  dest[j++] 
		    = (unsigned short)(CONTROL_CODE | C_RIGHT_RADICAL_SHIFT);

		  continue;	/* back to top of while loop */
		}
	    }

	  /* Attempt to parse as a font-change command, i.e. as one of the
	     macros \f0, \f1, \f2, etc., or \fP.  \fR, \fI, \fB are the
	     same as \f1, \f2, \f3 for troff compatibility. */

	  if (esc[0] == 'f' && ((esc[1] >= '0' && esc[1] <= '9')
				|| esc[1] == 'P' || esc[1] == 'R'
				|| esc[1] == 'I' || esc[1] == 'B'))
	    {
	      /* If a user-specified, device-specific font [e.g. an X font,
		 for which we have no internal table listing the other
		 fonts in its family] is being used, we can't really do
		 font switching, except via \f0, \f1.  These switch to the
		 X symbol font and the current user-specified font,
		 respectively.  (\fP is also supported.) */
	      if (_plotter->drawstate->font_type == PL_F_OTHER
		  && ((esc[1] >= '2' && esc[1] <= '9')
		      || esc[1] == 'I' || esc[1] == 'B'))
		esc[1] = '1'; /* treat as \f1 */
	      
	      /* troff compatibility */
	      if (esc[1] == 'R')
		esc[1] = '1';
	      else if (esc[1] == 'I')
		esc[1] = '2';
	      else if (esc[1] == 'B')
		esc[1] = '3';

	      if (esc[1] == 'P') /* \fP seen, so go back to previous font */
		raw_fontnum = previous_raw_fontnum;
	      else		/* font specified as index into typeface */
		{
		  int new_font_index = esc[1] - '0';

		  /* switch to specified font (OOB tests here now obsolete?) */
		  previous_raw_fontnum = raw_fontnum;
		  switch (_plotter->drawstate->font_type)
		    {
		    case PL_F_HERSHEY:
		      if ((new_font_index >= _pl_g_hershey_typeface_info[_plotter->drawstate->typeface_index].numfonts)
			  || new_font_index < 0)
			new_font_index = 1; /* OOB -> use default font */
		      raw_fontnum = _pl_g_hershey_typeface_info[_plotter->drawstate->typeface_index].fonts[new_font_index];
		      break;
		    case PL_F_PCL:
		      if ((new_font_index >= _pl_g_pcl_typeface_info[_plotter->drawstate->typeface_index].numfonts)
			  || new_font_index < 0)
			new_font_index = 1; /* OOB -> use default font */
		      raw_fontnum = _pl_g_pcl_typeface_info[_plotter->drawstate->typeface_index].fonts[new_font_index];
		      break;
		    case PL_F_STICK:
		      if ((new_font_index >= _pl_g_stick_typeface_info[_plotter->drawstate->typeface_index].numfonts)
			  || new_font_index < 0)
			new_font_index = 1; /* OOB -> use default font */
		      raw_fontnum = _pl_g_stick_typeface_info[_plotter->drawstate->typeface_index].fonts[new_font_index];
		      break;
		    case PL_F_POSTSCRIPT:
		    default:
		      if ((new_font_index >= _pl_g_ps_typeface_info[_plotter->drawstate->typeface_index].numfonts)
			  || new_font_index < 0)
			new_font_index = 1; /* OOB -> use default font */
		      raw_fontnum = _pl_g_ps_typeface_info[_plotter->drawstate->typeface_index].fonts[new_font_index];
		      break;
		    case PL_F_OTHER:
		      if (new_font_index != 0 && new_font_index != 1)
			new_font_index = 1; /* OOB -> use default font */
		      raw_fontnum = new_font_index;
		      break;
		    }
		}

	      fontword = ((unsigned short)raw_fontnum) << FONT_SHIFT;

	      continue;		/* back to top of while loop */
	    }
	  
	  /* couldn't match; unknown escape seq., so pass through unchanged */
	  dest[j++] = fontword | (unsigned short)'\\';
	  dest[j++] = fontword | (unsigned short)c;
	  dest[j++] = fontword | (unsigned short)d;
	}
    }

  dest[j] = (unsigned short)'\0';   /* terminate string */

  return dest;
}

int 
_codestring_len (const unsigned short *codestring)
{
  int i = 0;

  while (*codestring)
    {
      i++;
      codestring++;
    }
  
  return i;
}