summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvi2tty/dvi2tty-5.3.4-PATCHES/patch-03-uptex
blob: 7e50b25af423d3fb244f7c764fed460040774acd (plain)
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
diff -ur dvi2tty-5.3.4.orig/Changelog dvi2tty-5.3.4/Changelog
--- dvi2tty-5.3.4.orig/Changelog	2010-11-13 16:36:04.000000000 +0100
+++ dvi2tty-5.3.4/Changelog	2011-11-21 19:56:42.000000000 +0100
@@ -1,4 +1,9 @@
 /******************************************************************************
+ * Takuji Tanaka: 20111119 (KXD02663@nifty.ne.jp)
+ *               Used ptexenc library for Japanese TeX.
+ *               Enabled UTF8 output for Japanese TeX with option -Eu.
+ *               Added upTeX DVI support with option -U.
+ *               Added auto detected Japanese fonts in japanese-otf package.
  * Marcel Mol: 20101113  (marcel@mesa.nl)
  *               Version 5.3.4
  *               Fix VERSION code
diff -ur dvi2tty-5.3.4.orig/dvi2tty.1 dvi2tty-5.3.4/dvi2tty.1
--- dvi2tty-5.3.4.orig/dvi2tty.1	2010-11-13 16:30:56.000000000 +0100
+++ dvi2tty-5.3.4/dvi2tty.1	2011-11-21 21:26:24.000000000 +0100
@@ -129,7 +129,7 @@
 Note this may interfere with -u. Best not to use -u and -s together.
 .TP
 .B \-J
-Auto detect NTT JTeX and ASCII pTeX dvi format.
+Auto detect NTT JTeX, ASCII pTeX, and upTeX dvi format.
 .TP
 .B \-N
 Display NTT JTeX dvi.
@@ -137,6 +137,13 @@
 .B \-A
 Display ASCII pTeX dvi.
 .TP
+.B \-U
+Display upTeX dvi.
+.TP
+.B \-Eenc
+Set output Japanese encoding. The enc argument 'e', 's', 'j', and 'u'
+denotes EUC-JP, Shift_JIS, ISO-2022-JP, and UTF-8, respectively.
+.TP
 .B \-bdelim
 Print the name of fonts when switching to it (and ending it). The delim
 argument is used to delimit the fontname.
diff -ur dvi2tty-5.3.4.orig/dvi2tty.c dvi2tty-5.3.4/dvi2tty.c
--- dvi2tty-5.3.4.orig/dvi2tty.c	2010-11-13 16:33:50.000000000 +0100
+++ dvi2tty-5.3.4/dvi2tty.c	2011-11-21 22:30:24.000000000 +0100
@@ -55,11 +55,11 @@
 
 #define MAXLEN          100    /* size of char-arrays for strings            */
 #if defined(MSDOS) || defined(VMS) || defined(AMIGA)
-#define OPTSET      "haJwepPousltvbcAN" /* legal options                       */
-#define OPTWARG     "wepPovb"  /* options with argument                      */
+#define OPTSET      "haJweEpPousltvbcANU" /* legal options                   */
+#define OPTWARG     "weEpPovb"  /* options with argument                     */
 #else
-#define OPTSET      "haJwepPousqlfFtvbcAN"/* legal options                     */
-#define OPTWARG     "wepPoFvb"     /* options with argument                  */
+#define OPTSET      "haJweEpPousqlfFtvbcANU" /* legal options                */
+#define OPTWARG     "weEpPoFvb"     /* options with argument                 */
 #endif
 
 /*
@@ -75,10 +75,9 @@
 #define bdopt  7                /* bad option                        */
 #define onepp  8                /* only one page list allowed        */
 #define noarg  9                /* argument expected                 */
-#define confl  10               /* -N and -A conflict                */
-#define confj  11               /* -J conflicts -N or -A             */
+#define confl  10               /* -J, -N, -A, and -U conflict       */
 #if defined(THINK_C)
-#define nored  12               /* if no input file, redirect stdin  */
+#define nored  11               /* if no input file, redirect stdin  */
 #endif
 
 
@@ -166,6 +165,7 @@
     Argc = argc;
     Argv = argv;
 
+    set_enc_string (NULL, "default");
     getargs();                              /* read command line arguments   */
 #if defined(THINK_C)
     if (inputfromfile) {
@@ -380,10 +380,35 @@
                        j = strlen(optarg);
                        break;
 #endif
-            /* case 'J' : japan   = TRUE; break; */
             case 'J' : jautodetect  = TRUE; break;
-            case 'A' : asciip  = TRUE; break; /* ASCII pTeX */
-            case 'N' : japan   = TRUE; break; /* NTT jTeX */
+            case 'U' : uptex   = TRUE; japan = TRUE;    /* upTeX */
+                       jautodetect = FALSE;
+                       enable_UPTEX(true);
+                       set_enc_string (NULL, UPTEX_INTERNAL_ENC);
+                       break;
+            case 'A' : asciip  = TRUE; japan = TRUE;    /* ASCII pTeX */
+                       jautodetect = FALSE;
+                       set_enc_string (NULL, PTEX_INTERNAL_ENC);
+                       break;
+            case 'N' : nttj    = TRUE; japan = TRUE;    /* NTT jTeX */
+                       jautodetect = FALSE;
+                       set_enc_string (NULL, JTEX_INTERNAL_ENC);
+                       break;
+            case 'E' : 
+                switch (optarg[0]) {
+                       case 'e' :
+                           set_enc_string ("euc", NULL);  break;
+                       case 's' :
+                           set_enc_string ("sjis", NULL); break;
+                       case 'j' :
+                           set_enc_string ("jis", NULL);  break;
+                       case 'u' :
+                           set_enc_string ("utf8", NULL); break;
+                       default :
+                           usage(noarg);
+                }
+                       j++;
+                       break;
             case 't' : ttfont  = TRUE; break;
 	    case 'l' : noffd   = TRUE; break;
 	    case 's' : scascii ^= 1; break;
@@ -436,14 +461,12 @@
     }
 
     /* Option conflict */
-    if (japan && asciip) {
+    if ((asciip && uptex) ||
+        (nttj && (asciip || uptex)) ||
+        (jautodetect && (nttj || asciip || uptex))) {
         usage(confl);
     }
 
-    if (jautodetect && (japan || asciip)) {
-        usage(confj);
-    }
-
     return;
 
 } /* setoption */
@@ -707,7 +730,10 @@
 void usage(int uerr)
 {
 
-    fprintf(stderr, "%s", Copyright);
+    if (jautodetect || nttj || asciip || uptex)
+        fprintf(stderr, "%s (%s) %s", Progname, get_enc_string(), Copyright);
+    else
+        fprintf(stderr, "%s  %s", Progname, Copyright);
 
     if (uerr != ign) {
         fprintf(stderr,"\n%s: ", progname);
@@ -734,9 +760,7 @@
                             break;
             case   onepp  : fprintf(stderr, "only one pagelist allowed");
                             break;
-            case   confl  : fprintf(stderr, "-N and -A are conflicting");
-                            break;
-            case   confj  : fprintf(stderr, "-J conflicts -N or -A");
+            case   confl  : fprintf(stderr, "-J, -N, -A, and -U are mutually exclusive");
                             break;
 #if defined(THINK_C)
             case   nored  : fprintf(stderr, "\nIf no input file is given in\
@@ -794,12 +818,16 @@
     fprintf(stderr,
             " -u       Toggle latin1 support (default %s).\n", DEFLATIN1 ? "on" : "off");
     fprintf(stderr,
-            " -J       Enable auto detect for NTT JTeX and ASCII pTeX (japanese fonts).\n");
+            " -J       Enable auto detect for NTT JTeX, ASCII pTeX, and upTeX (japanese fonts).\n");
     fprintf(stderr,
             " -N       Support NTT JTeX dvi.\n");
     fprintf(stderr,
             " -A       Support ASCII pTeX dvi.\n");
     fprintf(stderr,
+            " -U       Support upTeX dvi.\n");
+    fprintf(stderr,
+            " -Eenc    Output Japanese encoding. e:EUC s:SJIS j:JIS, u:UTF8.\n");
+    fprintf(stderr,
             " -c       Override -a -u -s and print all characters 0-255.\n");
     fprintf(stderr,
             " -bdelim  Print font switch as text: delimcmr10miled\n");
diff -ur dvi2tty-5.3.4.orig/dvi2tty.h dvi2tty-5.3.4/dvi2tty.h
--- dvi2tty-5.3.4.orig/dvi2tty.h	2010-11-13 16:33:42.000000000 +0100
+++ dvi2tty-5.3.4/dvi2tty.h	2011-11-21 22:28:42.000000000 +0100
@@ -20,7 +20,8 @@
 
 #define VERSION "5.3.4"
 
-#define Copyright "dvi2tty  Copyright (C) 1984, 1985, 1986 Svante Lindahl.\n\
+#define Progname "dvi2tty"
+#define Copyright "Copyright (C) 1984, 1985, 1986 Svante Lindahl.\n\
 Copyright (C) 1989-2010 M.J.E. Mol, MESA Consulting B.V."
 
 
@@ -54,6 +55,18 @@
  */
 #define MAXTERMWIDTH	332
 
+#include <ptexenc/ptexenc.h>
+#include <ptexenc/unicode.h>
+
+/* internal encoding for NTT JTeX : "euc" */
+#define JTEX_INTERNAL_ENC  "euc"
+
+/* internal encoding for ASCII pTeX : "euc" or "sjis" */
+#define PTEX_INTERNAL_ENC  "euc"
+
+/* internal encoding for upTeX : "uptex" */
+#define UPTEX_INTERNAL_ENC  "uptex"
+
 /*
  * ERROR CODES , don't start with 0
  */
@@ -109,9 +122,11 @@
 extern bool   latin1;                  /* latin1 chars (dvistuff.c)          */
 extern bool   accent;                  /* Output accent stuff(dvistuff.c)    */
 extern bool   ttfont;                  /* tt font assumed   (dvistuff.c)     */
-extern bool   japan;                   /* NTT jTeX font support (dvistuff.c) */
+extern bool   jautodetect;             /* Autodetect NTT jTeX, ASCII pTeX and upTeX (dvistuff.c) */
+extern bool   nttj;                    /* NTT jTeX font support (dvistuff.c) */
 extern bool   asciip;                  /* ASCII pTeX font support (dvistuff.c) */
-extern bool   jautodetect;             /* Autodetect NTT jTex and ASCII pTeX (dvistuff.c) */
+extern bool   uptex;                   /* upTeX font support (dvistuff.c)    */
+extern bool   japan;                   /* japanized TeX font support (dvistuff.c) */
 extern bool   noffd;                   /* output ^L or formfeed (dvistuff.c) */
 extern bool   printfont;               /* include font switches (dvistuff.c) */
 extern bool   allchar;                 /* output all characters (dvistuff.c  */
diff -ur dvi2tty-5.3.4.orig/dvi2tty.tex dvi2tty-5.3.4/dvi2tty.tex
--- dvi2tty-5.3.4.orig/dvi2tty.tex	2010-10-30 12:13:29.000000000 +0200
+++ dvi2tty-5.3.4/dvi2tty.tex	2011-11-21 21:26:57.000000000 +0100
@@ -192,11 +192,16 @@
 terminals in Scandinavia are mapped to ``\{$|$\}[\bs ]''.
 Note this may interfere with -u. Best not to use -u and -s together.
 \item[{{\bf -J}}]
-Auto detect NTT JTeX and ASCII pTeX dvi format.
+Auto detect NTT JTeX, ASCII pTeX, and upTeX dvi format.
 \item[{{\bf -N}}]
 Display NTT JTeX dvi.
 \item[{{\bf -A}}]
 Display ASCII pTeX dvi.
+\item[{{\bf -U}}]
+Display upTeX dvi.
+\item[{{\bf -Eenc}}]
+Set output Japanese encoding. The enc argument 'e', 's', 'j', and 'u'
+denotes EUC-JP, Shift\_JIS, ISO-2022-JP, and UTF-8, respectively.
 \end{list}
 %
 %	NOTE --- No other files are used under VMS.
diff -ur dvi2tty-5.3.4.orig/dvistuff.c dvi2tty-5.3.4/dvistuff.c
--- dvi2tty-5.3.4.orig/dvistuff.c	2011-01-27 13:20:02.000000000 +0100
+++ dvi2tty-5.3.4/dvistuff.c	2011-11-21 22:41:54.000000000 +0100
@@ -87,13 +87,6 @@
 #define sget3()         snum(3)
 #define sget4()         snum(4)
 
-#if !defined(JIS)
-# define JIS    1       /* if 0 then EUC or SJIS */
-#endif
-#if !defined(SJIS)
-# define SJIS   0       /* if !JIS, 0 then EUC */
-#endif
-
 
 /*
  * Structure and variable definitions
@@ -122,7 +115,7 @@
     long    num;
     struct _font * next;
     char  * name;
-    char    flags; /* to store font types, to get rid of japan/asciip/ttfont/symbolfon/mifont vars */
+    char    flags; /* to store font types, to get rid of nttj/asciip/uptex/ttfont/symbolfon/mifont vars */
     int     fontnum; /* helper for japanese fonts */
 } font;
 
@@ -140,8 +133,10 @@
 bool        accent;             /* if true output accents etc: \'{e} etc.    */
 bool        ttfont = FALSE;     /* if true we assumed ttfonts, not cmr       */
 bool        symbolfont = FALSE; /* true if font is a symbol font             */
-bool        japan = FALSE;      /* switch to NTT japanese fonts ...          */
+bool        nttj = FALSE;       /* switch to NTT japanese fonts ...          */
 bool        asciip = FALSE;      /* switch to ASCII japanese fonts ...       */
+bool        uptex = FALSE;      /* switch to upTeX CJK fonts ...             */
+bool        japan = FALSE;      /* switch to NTT/ASCII/.. japanese fonts ... */
 bool        jautodetect = FALSE; /* switch if do auto detection of Japanese TeX */
 bool        jdetect = FALSE;     /* switch if Japanese TeX detection is done */
 bool        mifont = FALSE;      /* ASCII japanese font ??? */
@@ -177,14 +172,7 @@
 font * fonts = NULL;            /* List of fontnames defined                 */
 font * fnt = NULL;              /* Current font                              */
 
-#if JIS
-# define        SJIS    0
-int    inkanji = 0;    /* is in kanji sequence */
-# define KANJIIN  "\033$B"   /* was $@ in debian 5.1-13 patch.. */
-# define KANJIOUT "\033(J"
-#endif
-int    kanji1 = 0;     /* is first kanji character */
-int    kanji2 = 0;     /* is second kanji character */
+int    kanji1 = 0;     /* number of rest of trailer bytes in kanji character */
 
 
 
@@ -219,7 +207,7 @@
 void            setfont         (long);
 void            jischar         (unsigned long);
 int             compute_jis     (int, unsigned int, unsigned int *, unsigned int *);
-void            dokanji         (unsigned long);
+void            dokanji         (long);
 int             getjsubfont     (char *);
 
 #else
@@ -249,7 +237,7 @@
 void            setfont         (long fntnum);
 void            jischar         (unsigned long ch);
 void            compute_jis     (int f, unsigned int c, unsigned int * ku, unsigned int * ten);
-void            dokanji         (unsigned long ch);
+void            dokanji         (long ch);
 int             getjsubfont     (char * s);
 #if defined(VMS)
 long		vmsseek		();
@@ -448,9 +436,9 @@
             errorexit(illop);
         else
             switch (opcode) {
-                case SET1     : japan ? jischar(get1()) : setchar(get1());break;
-                case SET2     : asciip ? dokanji(get2()) : setchar(get2()); break;
-                case SET3     : setchar(get3()); break;
+                case SET1     : nttj ? jischar(get1()) : setchar(get1());break;
+                case SET2     : (asciip || uptex) ? dokanji(get2()) : setchar(get2()); break;
+                case SET3     : uptex ? dokanji(get3()) : setchar(get3()); break;
                 case SET4     : setchar(get4()); break;
                 case SET_RULE : { long height = sget4();
                                   rule(MOVE, sget4(), height); break;
@@ -617,8 +605,9 @@
 
 void printpage(void)
 {
-    register int  i, j;
+    register int  i, j, k;
     register unsigned char ch;
+    unsigned char buff[4];
 
     if (sptr != 0)
         fprintf(stderr, "dvi2tty: warning - stack not empty at eop.\n");
@@ -637,75 +626,59 @@
                    i++, j++) {
                 ch = currentline->text[i - leftmargin];
 
-                if (asciip) {
-                    if (kanji1) {
-#if !JIS && !SJIS
-                        ch |= 0x80;
-#endif
-                        kanji2 = 1;
-                        kanji1 = 0;
-                    }
-                    if (!kanji2 && ch > 127) {
-                        kanji1 = 1;
-#if JIS 
-                        if (!inkanji) {
-                            inkanji = 1;
-                            fprintf(output, KANJIIN);
-                        }
-                        ch &= 0x7f;
-#endif
-                    }
-#if JIS
-                    if (!kanji1 && !kanji2 && inkanji) {
-                        fprintf(output, KANJIOUT);
-                        inkanji = 0;
-                    }
-#endif  
-                    if (kanji1 && (j >= (int) foo) &&
-                        (currentline->charactercount > i+1)) {
-#if JIS
-                        if (inkanji)
-                            fprintf(output, KANJIOUT);
-#endif
-                        fprintf(output, "*\n");         /* if line to large */
-                        fprintf(output, " *");          /* mark output      */
-#if JIS
-                        if (inkanji)
-                            fprintf(output, KANJIIN);
-#endif
-                        j = 2;
-                    }
-                }
-
-                if (ch >= SPACE || allchar)
-                    putc(ch, output);
+		if (japan) {
+		  if (ch > 127) {
+		    for (k = 0; k < 4; k++) {
+		      if (i - leftmargin + k < LINELEN+1)
+			buff[k] = currentline->text[i - leftmargin + k];
+		      else buff[k] = '\0';
+		    }
+		    kanji1 = multistrlen(buff, 4, 0) - 1;
+		  }
+		  else kanji1 = 0;
+		  if (kanji1 && (j + kanji1 > (int) foo) &&
+		      (currentline->charactercount > i+1)) {
+		    putc2('*', output);
+		    putc2('\n', output);    /* if line to large */
+		    putc2(' ', output);
+		    putc2('*', output);     /* mark output      */
+		    j = 2;
+		  }
+		}
+
+                if (ch >= SPACE || allchar) {
+		  if (japan) {
+		    for (k = 0; k < kanji1; k++) {
+		      putc2(ch, output);
+		      i++; j++;
+		      ch = currentline->text[i - leftmargin];
+		    }
+		    putc2(ch, output);
+		  }
+		  else 
+		    putc(ch, output);
+		}
                 if ((j > (int) foo) && (currentline->charactercount > i+1)) {
-#if JIS
-                        if (asciip && inkanji)
-                                fprintf(output,KANJIOUT);
-#endif
-                        fprintf(output, "*\n");         /* if line to large */
-                        fprintf(output, " *");          /* mark output      */
-#if JIS
-                        if (asciip && inkanji)
-                            fprintf(output, KANJIIN);
-#endif
-                        j = 2;
-                 }
-                 if (asciip)
-                     kanji2 = 0;
+		  if (japan) {
+		    putc2('*', output);
+		    putc2('\n', output);    /* if line to large */
+		    putc2(' ', output);
+		    putc2('*', output);     /* mark output      */
+		  }
+		  else {
+		    fprintf(output, "*\n");         /* if line to large */
+		    fprintf(output, " *");          /* mark output      */
+		  }
+		  j = 2;
+                }
             } 
         }
-        putc('\n', output);
+        if (japan)
+          putc2('\n', output);
+        else 
+          putc('\n', output);
     } 
 
-#if JIS
-    if (asciip && !kanji1 && !kanji2 && inkanji) {
-       fprintf(output, KANJIOUT);
-       inkanji = 0;
-    }
-#endif
-
     currentline = firstline;
     while (currentline->next != nil) {
         currentline = currentline->next;
@@ -988,23 +961,19 @@
  * DOKANJI -- Process a kanji character opcode.
  */
  
-void dokanji(unsigned long ch)
+void dokanji(long ch)
 {
-#if SJIS
-    register int c1, c2;
+    long i;
 
-    c1 = (ch >> 8) & 0xff;
-    c2 = ch & 0xff;
+    i = toBUFF(fromDVI(ch));
+    kanji1 = 3;
+    if (BYTE1(i) != 0) outchar(BYTE1(i));
+    kanji1 = 2;
+    if (BYTE2(i) != 0) outchar(BYTE2(i));
     kanji1 = 1;
-    outchar((((c1 - 1) >> 1) + ((c1 <= 0x5e) ? 0x71 : 0xb1)));
+    /* always */       outchar(BYTE3(i));
     kanji1 = 0;
-    outchar((c2 + ((c1 & 1) ? ((c2 < 0x60) ? 0x1f : 0x20) : 0x7e)));
-#else
-    kanji1 = 1;
-    outchar(0x80 | ( ch >> 8 ));
-    kanji1 = 0;
-    outchar(ch & 0xff);
-#endif
+    /* always */       outchar(BYTE4(i));
 
     return;
 
@@ -1019,7 +988,7 @@
 void dochar(unsigned char ch)
 {
 
-    if (japan && fnt->fontnum)
+    if (nttj && fnt->fontnum)
         jischar((long) ch);
     else if (symbolfont == TRUE)
         symchar(ch);
@@ -1048,8 +1017,8 @@
        case  14: ch = 'O'; break;
        case  15: ch = 'o'; break;
        case  24: ch = '~'; break;
-       case  32: ch = japan ? '<' : 32; break; /* really only for japan? */
-       case  33: ch = japan ? '>' : 33; break; /* really only for japan? */
+       case  32: ch = nttj ? '<' : 32; break; /* really only for japan? */
+       case  33: ch = nttj ? '>' : 33; break; /* really only for japan? */
        case 102: ch = '{'; break;
        case 103: ch = '}'; break;
        case 104: ch = '<'; break;
@@ -1284,6 +1253,14 @@
     /*----------------- end of 'latin1 / Scandinavian code' ----------------*/
 
     if (foo == leftmargin-1) {
+      if (japan) {
+        while (((currentline->text[j - leftmargin] != SPACE) ||
+	        (kanji1 && (currentline->text[j+kanji1 - leftmargin] != SPACE)))
+                && (j < rightmargin)) {
+	  j++;
+	  h += charwidth;
+        }
+      } else {
         while (j < rightmargin &&
                ( (currentline->text[j - leftmargin] != SPACE) ||
                  (kanji1 && (currentline->text[j+1 - leftmargin] != SPACE))
@@ -1291,6 +1268,7 @@
             j++;
             h += charwidth;
         }
+      }
     }
     if ( allchar || ((ch >= SPACE) && (ch != DEL)) ||
          ((latin1 || scascii) && (ch == 23)) ) {
@@ -1321,7 +1299,7 @@
     register long saveh;
 
     saveh = h;
-    if (japan)
+    if (nttj)
         dochar((unsigned char) charnr);
     else if (allchar || ((charnr >= 0) && (charnr <= LASTCHAR)))
         outchar((unsigned char) charnr);
@@ -1351,6 +1329,43 @@
 } /* setchar */
 
 
+static const char *ptex_fontchk[] = {
+    "min", "goth", "jis",
+    "hmin", "hgoth", "hmgoth",               /* japanese-otf package */
+    "nmlmin", "nmlgoth", "nmlmgoth", 
+    "hiramin", "hirakaku", "hiramaru",
+    NULL /* end */
+};
+
+static const char *uptex_fontchk[] = {
+    "umin", "ugoth", "ujis",
+    "upjis", "upjpn", "upsch", "uptch", "upkor",
+    "uphmin", "uphgoth", "uphmgoth",         /* japanese-otf package */
+    "upnmlmin", "upnmlgoth", "upnmlmgoth", 
+    "uphiramin", "uphirakaku", "uphiramaru",
+    NULL /* end */
+};
+
+static const char *jtex_fontchk[] = {
+    "dmj", "dgj",
+    NULL /* end */
+};
+
+static int checkjfont(const char **jfontlist, const char *name)
+{
+    int i, len;
+    const char *tmpfont;
+
+    i=0;
+    while ( (tmpfont=jfontlist[i]) != NULL ) {
+        len=strlen(tmpfont);
+        if ( !strncmp(tmpfont, name, len) ) return 1;
+        i++;
+    }
+    return 0;
+} /* checkjfont */
+
+
 
 /*
  * FONTDEF -- Process a font definition.
@@ -1402,22 +1417,33 @@
      */
     fonts->flags = 0;
 
-    if (jautodetect && asciip == FALSE && japan == FALSE && !jdetect) {
-        if (!strncmp("min", name, 3) || !strncmp("got", name, 3)) {
+    if ((asciip == FALSE && nttj == FALSE && uptex == FALSE)
+        && (!jdetect) && jautodetect) {
+        if ( checkjfont(ptex_fontchk, name) ) {
             /* Detect as ASCII TeX */
             asciip = TRUE;
-            japan = FALSE;
-            jdetect = TRUE;
+            nttj = uptex = FALSE;
+            japan = jdetect = TRUE;
+            fonts->flags |= MIFONT;
+            set_enc_string (NULL, PTEX_INTERNAL_ENC);
+        } else if ( checkjfont(uptex_fontchk, name) ) {
+            /* Detect as upTeX */
+            uptex = TRUE;
+            nttj = asciip = FALSE;
+            japan = jdetect = TRUE;
             fonts->flags |= MIFONT;
-        } else if (!strncmp("dmj", name, 3) || !strncmp("dgj", name, 3)) {
+            enable_UPTEX(true);
+            set_enc_string (NULL, UPTEX_INTERNAL_ENC);
+        } else if ( checkjfont(jtex_fontchk, name) ) {
             /* Detect as NTT JTeX */
-            asciip = FALSE;
-            japan = TRUE;
-            jdetect = TRUE;
+            nttj = TRUE;
+            asciip = uptex = FALSE;
+            japan = jdetect = TRUE;
             fonts->flags |= JAPFONT;
+            set_enc_string (NULL, JTEX_INTERNAL_ENC);
         }
     }
-    if (japan)
+    if (nttj)
         fonts->fontnum = getjsubfont(name);
     else
         fonts->fontnum = 0;
@@ -1511,7 +1537,9 @@
     unsigned int Ku, Ten;
 
     compute_jis(fnt->fontnum, (unsigned int) ch, &Ku, &Ten);
+    kanji1 = 1;
     outchar(Ku+128);
+    kanji1 = 0;
     outchar(Ten+128);
 
     return;