[comp.windows.x.motif] Multi-font text display - it can be done.

tbray@watsol.uwaterloo.ca (Tim Bray) (05/02/91)

Have noticed a lot of people, in this group and others, wishing for a
multi-style text capability, where style includes font, colour, reverse video,
and the like.  We have such a thing; either it or some of the pieces we used
to build it might be of help to people here.  What follows is a discussion of
how it's done and what it is.  Warning: we are a commercial outfit, and we
don't plan to give it away, so the body of this message has some aspects of an
advertisement.  On the other hand, some people might want to read it even if
they have no interest in a commercial product just to get a feeling for what's
involved in doing this.

Start of commercially-tainted material:


Lo these many years ago, somebody in the New Oxford English Dictionary
research group at the U of Waterloo wrote, under SunView AND X.10 AND NeWS, a
tool which could take text, with embedded markup, and display it in multiple
fonts and multiple columns, doing word wrap and fill as appropriate.  This
would be late '87 to '88.  Since that time, this tool has gotten a name
("Lector"), oozed forward into X11R4/Motif, and now it:

 1. Can do colour, font shifts, highlighting, and suppression, based on 
    markup in the text.  Also tabbing and indentation.
 2. Markup in the text is mapped to presentation using a user-written "display
    specification"; think of it as a style sheet.  These are pretty easy
    to write - you don't have to be a computer programmer.
 3. Can seek around in a file or process a stream of input text.
 4. Can use multiple style sheets to give different views of text, and
    switch between them quickly.
 5. Has a nice Motif wrapper, with pulldowns, buttons, on-line help, and
    the like.
 6. Is *fast* - can display text pretty well as fast as your X server can
    do XDrawText calls.  This means on a fast SPARC or DEC-MIPS or RS/6K,
    the screen paint is faster than my eye, at least, can follow.  All
    the text painting is done in a DrawingArea widget with nothing but raw
    Xlib calls.  The difference between R3 and R4 of X was dramatic...
 7. Allows navigation around and selection of pieces of text using the mouse.
 8. Does multi-column display - number & width reconfigurable at runtime.
 9. Maintains a map of what's where on the screen and will export pointers,
    so you can pipe a bunch of these together and do neat tricks.

What it, at the curent time, is NOT:

 1. Isn't a widget, it's a program.  Amount of work to make it into a widget:
    substantial.  (a) Find & kill non-reentrant stuff like globals 
    (b) Bow low and chant all the appropriate subclassing incantations.
 2. Doesn't provide selection into the Cut buffer or primary selection or
    Clipboard, even though there's an unused mouse button.  Amount of work
    to fix: trivial, should have been done years ago.
 3. Doesn't supprt editing; is an output-only system.

How it works:

Basically, there is a module that parses the display specification, then
spoon-feeds the main display loop, returning a chunk of text and instructions
for formatting it (in a C struct) each time around.  This separation of
display policy and mechanism is deliberate - at one time, we wrote a fairly-
complete text *editor* based on these same display specs; it has not yet
become a product; too much work, so little time...

There is about 25k of code that implements the display spec, then about
another 25k that talks to X, most of it concerned with the grotty business of
column filling and word wrap.  Doesn't sound like much code, but it took a
long time and a lot of work to get it right.  (Of course, after you factor in
Xlib and Motif, you get a 1.25Mb binary).

For Technical Info:

There is a technical report on Lector, written (very well) by its inventor,
Darrell Raymond, and this is available for a couple of bucks from the U of W
Centre for the New OED; contact Linda Jones there
(lmjones@watsol.waterloo.edu, (519)-885-1211, ext. 6200).  It goes into more
depth as to how it really works.

For Commercial Info:

Lector is now a product, being marketed by the New OED group spin-off company,
Open Text Systems (I work in both places).  We at Open Text mostly use it to
front-end our full-text search tool, Pat - the idea is you zero in on some text
in a big database, and Lector pops it up looking more or less like the page
from whence it came.  But it's been used for lots of other stuff.  We have 
binaries for just about every unix out there, and we've also licensed the
display spec parsing library as a separate product.  If there's any interest,
get in touch with me (tbray@watsol.waterloo.edu, (519)-746-8288).

Cheers, Tim Bray, 
University of Waterloo Centre for the New OED
 *and* 
  Open Text Systems, Inc.