Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tlug: Programming in Japanese on X



>>>>> "mc" == Michael Casinghino <michael@example.com> writes:

    mc> I have a programming question.  How can I figure out how to
    mc> display Japanese text in an X-Window?  I have just read the
    mc> following books (well, the i18n and font parts anyway) --

    mc> They all made vague references, and I tried a million things.
    mc> Nothing worked.

Um, the OReilly Xlib Programming Manual is quite specific.  As is the
Motif Programming Manual.  Did you try the example programs?  Are you
already an experienced X programmer?  If neither, I understand why you
think the references are "vague," but you really have to read the
_whole_ book up to the i18n chapter to understand that material.

What _did_ you try?  Send code when you have a programming question;
it's the only way we have hope of figuring out what you're thinking.

Basically it's a matter of setting the font and squirting strings at
the display using XDrawString and friends.  I just used straight JIS
(ie, 7 bit code with no escape sequences) which will look like random
ASCII (eg "#0#1#2#3#4#5#6#7#8#9" would display the digits in order in
zenkaku form; it turns out there is not a dependency on endianness,
look up the definition of struct XChar2b) after setting the font
appropriately.  Here's a patch to an OReilly example.  It's pretty
ugly; _all_ it does is output Japanese in a specific place.  (The
error routine has been tested, too ;-)

steve@example.com:basic$ diff -u basicwin.c japanese.c
--- basicwin.c  Wed Jun 10 06:22:18 1992
+++ japanese.c  Tue Sep 29 13:07:33 1998
@@ -2,12 +2,17 @@
  * Copyright 1989 O'Reilly and Associates, Inc.
  * See ../Copyright for complete rights and liability information.
  */
+#define JAPANESE 1
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
 #include <X11/Xos.h>
 #include <X11/Xatom.h>
 
 #include <stdio.h>
+#ifdef JAPANESE
+/* For sleep() */
+#include <unistd.h>
+#endif
 
 #include "../bitmaps/icon_bitmap"
 
@@ -175,7 +179,51 @@
                                TooSmall(win, gc, font_info);
                        else {
                                /* place text in window */
+#ifndef JAPANESE
                                draw_text(win, gc, font_info, width, height);
+#else
+/* Assuming you've already opened display, created and mapped
+   win, set up gc, entered the event loop, and received an Expose
+   event.  This code was tested by subtituting as is for the function
+   call to drawtext() in O'Reilly's basic/basicwin.c.  Available at 
+   ftp://ftp.uu.net/vendor/oreilly/xbook/xlib/xlibprgs3.tar.Z.
+   Compile with gcc -L /usr/X11R6/lib -lX11 -o japanese japanese.c 
+   (note rename). */
+{
+  /* that really should be a 14-hyphen XLFD, but I'm lazy */
+  /* most X servers have "k14", you can try that too      */
+  char *fontname = "-*-jisx0208.1983-0";
+  XFontStruct *jfont;
+
+  /* extra parens shut up compiler */
+  if ((jfont = XLoadQueryFont(display,fontname)))
+  {
+    XSetFont(display,gc,jfont->fid);
+    /* I think the 10 is correct (count characters), but it may actually
+       be 20 (count bytes). */
+    /* should use XTextExtents to insure this does not get clipped */
+    XDrawString16(display,win,gc,20,20,
+                  (XChar2b *) "#0#1#2#3#4#5#6#7#8#9", 10);
+  } else {
+    /* do the same display operation in the default font */
+    /* should check length of fontname and malloc this, also make sure
+       window boundaries don't clip the string */
+    char error[81];     /* +1 is belt-and-suspenders */
+    int nchars;
+    nchars = snprintf(error,80,"Can't find matching font: %s.",fontname);
+    XDrawString(display,win,gc,20,20,
+                error,nchars);
+    XDrawString(display,win,gc,20,40,
+                "If you're gonna display Japanese, you need fonts!", 49);
+    /* This is normally evil, but since we're exiting we're not unhappy
+       about preempting user interaction. Translation:  heavens, I'm lazy. */
+    XFlush(display);
+    sleep(5);
+    exit(1);
+  }
+}
+
+#endif
 
                                /* place graphics in window, */
                                draw_graphics(win, gc, width, height);



See also r5/i18n_output.c in xlibprgs3.tar.Z.

In Motif, you probably need to make a Compound Text string and use
XmDrawText (or some such function, I don't do Motif).

    mc> Where do I find out how X turns a euc string into Japanese

Use compound text, which is an 8-bit ISO-2022 encoding.  There are
convenience functions for making compound text out of strings in
Motif.  I believe that Motif does the conversion to the font's
encoding (JIS) internally.  Or maybe Xlib does.  Or you can do it; Ken 
Lunde's code is well documented, you can just borrow his functions
(check the license notice first, I forget if there are any special
restrictions, but he didn't just GPL it):

      ftp://ftp.uu.net/vendor/oreilly/nutshell/ujip/src/jconv.c

    mc> text?  Or perhaps more basically, how can I select a character
    mc> from a Japanese font set?  And what about input?

Japanese font sets are all encoded in JIS (that's why their longnames
end in jisx0208-0 etc).  Just set the font in the GC and start spewing 
JIS.

Input is a bitch.  Rather than try to handle input, you should use an
X Input Method like kinput2.  Unfortunately, both Xlib and Motif and
most versions of lesstif have bugs.  The XIM stuff is tedious but
straightforward; you set it up and then let the usual complement of
input functions do their work.  Mostly they won't crash on you,
although muriyari Netscape users may tell you differently :-P.

See the r5/i18n_input.c in xlibprgs3.tar.Z.

    mc> I have lesstif and Gtk.  I can read a little Japanese but...
    mc> If anyone can recommend a good book, document, or web site, I
    mc> will be eternally grateful.

Well, you listed my reading list already.  Gtk ought to have man pages
galore.  Look at the stuff in /usr/X11/man/man3/X{mb,wc}*.

Read programs (or perhaps better the Japanese-enabling patches).  I
can't suggest any, unfortunately, the programs I know (Mule, kinput2,
kterm) bury the Japanese handling deep in arcane general routines.

Maybe Jim Breen's xjdic (ftp://ftp.monash.edu.au/pub/nihongo/xjdic*),
but I think that only does input and assumes output to a kterm.

-- 
University of Tsukuba                Tennodai 1-1-1 Tsukuba 305-8573 JAPAN
Institute of Policy and Planning Sciences        Tel/fax: +1 (298) 53-5091
---------------------------------------------------------------
Next Meeting: 10 October, 12:30 Tokyo Station Yaesu central gate
Next Nomikai: 20 November, 19:30  Tengu TokyoEkiMae 03-3275-3691
---------------------------------------------------------------
Sponsor: PHT, makers of TurboLinux http://www.pht.co.jp


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links