diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /dviware/dvibook |
Initial commit
Diffstat (limited to 'dviware/dvibook')
76 files changed, 15315 insertions, 0 deletions
diff --git a/dviware/dvibook/Dvibook/Imakefile b/dviware/dvibook/Dvibook/Imakefile new file mode 100644 index 0000000000..c2ae432374 --- /dev/null +++ b/dviware/dvibook/Dvibook/Imakefile @@ -0,0 +1,28 @@ +OBJS =dvibook.o +SRCS =dvibook.c + + LIBTEXROOT= ../ + LIBTEXSRC = ../libtex + LIBTEX = $(LIBTEXSRC)/libtex.a + DEPLIBTEX = $(LIBTEX) + DEPLIBS = $(DEPLIBTEX) XawClientDepLibs +LOCAL_LIBRARIES = $(LIBTEX) + + DEFINES = -I$(LIBTEXROOT) + +FILES =Imakefile dvibook.man Makefile.raw $(SRCS) + +all: dvibook + +NormalProgramTarget(dvibook,$(OBJS),$(DEPLIBS),$(LOCAL_LIBRARIES),$(SYSLIBS)) +InstallProgram(dvibook, $(BINDIR)) +InstallManPage(dvibook,$(MANDIR)) +DependTarget() +TagsTarget() + +tar: + cd ../..;\ + for file in $(FILES); \ + do \ + tar rf $(TARFILE) $(PRODUCT)/Dvibook/$$file; \ + done diff --git a/dviware/dvibook/Dvibook/Makefile b/dviware/dvibook/Dvibook/Makefile new file mode 100644 index 0000000000..cea59ac94b --- /dev/null +++ b/dviware/dvibook/Dvibook/Makefile @@ -0,0 +1,356 @@ +# Makefile generated by imake - do not edit! +# $XConsortium: imake.c,v 1.65 91/07/25 17:50:17 rws Exp $ +# +# The cpp used on this machine replaces all newlines and multiple tabs and +# spaces in a macro expansion with a single space. Imake tries to compensate +# for this, but is not always successful. +# + +# ------------------------------------------------------------------------- +# Makefile generated from "Imake.tmpl" and <Imakefile> +# $XConsortium: Imake.tmpl,v 1.139 91/09/16 08:52:48 rws Exp $ +# +# Platform-specific parameters may be set in the appropriate <vendor>.cf +# configuration files. Site-specific parameters should be set in the file +# site.def. Full rebuilds are recommended if any parameters are changed. +# +# If your C preprocessor does not define any unique symbols, you will need +# to set BOOTSTRAPCFLAGS when rebuilding imake (usually when doing +# "make World" the first time). +# + +# ------------------------------------------------------------------------- +# site-specific configuration parameters that need to come before +# the platform-specific parameters - edit site.def to change + +# site: $XConsortium: site.def,v 1.2 91/07/30 20:26:44 rws Exp $ + +# ------------------------------------------------------------------------- +# platform-specific configuration parameters - edit sun.cf to change + +# platform: $XConsortium: sun.cf,v 1.69 91/09/09 18:23:59 rws Exp $ + +# operating system: SunOS 4.1.1 + +# $XConsortium: sunLib.rules,v 1.6 91/03/24 17:55:58 rws Exp $ + +# ------------------------------------------------------------------------- +# site-specific configuration parameters that go after +# the platform-specific parameters - edit site.def to change + +# site: $XConsortium: site.def,v 1.2 91/07/30 20:26:44 rws Exp $ + + SHELL = /bin/sh + + TOP = . + CURRENT_DIR = . + + AR = ar clq + BOOTSTRAPCFLAGS = + CC = cc + AS = as + + COMPRESS = compress + CPP = /lib/cpp $(STD_CPP_DEFINES) + PREPROCESSCMD = cc -E $(STD_CPP_DEFINES) + INSTALL = /usr/local/bin/install + LD = ld + LINT = lint + LINTLIBFLAG = -C + LINTOPTS = -axz + LN = ln -s + MAKE = make + MV = mv + CP = cp + + RANLIB = ranlib + RANLIBINSTFLAGS = + + RM = rm -f + TROFF = psroff + MSMACROS = -ms + TBL = tbl + EQN = eqn + STD_INCLUDES = + STD_CPP_DEFINES = + STD_DEFINES = + EXTRA_LOAD_FLAGS = + EXTRA_LIBRARIES = + TAGS = ctags + + SHAREDCODEDEF = -DSHAREDCODE + SHLIBDEF = -DSUNSHLIB + + PROTO_DEFINES = + + INSTPGMFLAGS = -s + + INSTBINFLAGS = -m 0755 + INSTUIDFLAGS = -m 4755 + INSTLIBFLAGS = -m 0644 + INSTINCFLAGS = -m 0444 + INSTMANFLAGS = -m 0444 + INSTDATFLAGS = -m 0444 + INSTKMEMFLAGS = -m 4755 + + TOP_INCLUDES = -I$(INCROOT) + + CDEBUGFLAGS = -O + CCOPTIONS = -pipe -temp=. + + ALLINCLUDES = $(INCLUDES) $(EXTRA_INCLUDES) $(TOP_INCLUDES) $(STD_INCLUDES) + ALLDEFINES = $(ALLINCLUDES) $(STD_DEFINES) $(EXTRA_DEFINES) $(PROTO_DEFINES) $(DEFINES) + CFLAGS = $(CDEBUGFLAGS) $(CCOPTIONS) $(ALLDEFINES) + LINTFLAGS = $(LINTOPTS) -DLINT $(ALLDEFINES) + + LDLIBS = $(SYS_LIBRARIES) $(EXTRA_LIBRARIES) + + LDOPTIONS = $(CDEBUGFLAGS) $(CCOPTIONS) $(LOCAL_LDFLAGS) -L$(USRLIBDIR) + + LDCOMBINEFLAGS = -X -r + DEPENDFLAGS = + + MACROFILE = sun.cf + RM_CMD = $(RM) *.CKP *.ln *.BAK *.bak *.o core errs ,* *~ *.a .emacs_* tags TAGS make.log MakeOut + + IMAKE_DEFINES = + + IRULESRC = $(CONFIGDIR) + IMAKE_CMD = $(IMAKE) -DUseInstalled -I$(IRULESRC) $(IMAKE_DEFINES) + + ICONFIGFILES = $(IRULESRC)/Imake.tmpl $(IRULESRC)/Imake.rules \ + $(IRULESRC)/Project.tmpl $(IRULESRC)/site.def \ + $(IRULESRC)/$(MACROFILE) $(EXTRA_ICONFIGFILES) + +# ------------------------------------------------------------------------- +# X Window System Build Parameters +# $XConsortium: Project.tmpl,v 1.138 91/09/10 09:02:12 rws Exp $ + +# ------------------------------------------------------------------------- +# X Window System make variables; this need to be coordinated with rules + + PATHSEP = / + USRLIBDIR = /usr/local/lib/X11R5 + BINDIR = /usr/local/bin/X11R5 + INCROOT = /usr/local/share/X11R5/include + BUILDINCROOT = $(TOP) + BUILDINCDIR = $(BUILDINCROOT)/X11 + BUILDINCTOP = .. + INCDIR = $(INCROOT)/X11 + ADMDIR = /usr/adm + LIBDIR = /usr/local/lib/X11R5 + CONFIGDIR = $(LIBDIR)/config + LINTLIBDIR = $(USRLIBDIR)/lint + + FONTDIR = $(LIBDIR)/fonts + XINITDIR = $(LIBDIR)/xinit + XDMDIR = $(LIBDIR)/xdm + TWMDIR = $(LIBDIR)/twm + MANPATH = /usr/local/share/man + MANSOURCEPATH = $(MANPATH)/man + MANSUFFIX = n + LIBMANSUFFIX = 3 + MANDIR = $(MANSOURCEPATH)$(MANSUFFIX) + LIBMANDIR = $(MANSOURCEPATH)$(LIBMANSUFFIX) + NLSDIR = $(LIBDIR)/nls + PEXAPIDIR = $(LIBDIR)/PEX + XAPPLOADDIR = $(LIBDIR)/app-defaults + FONTCFLAGS = -t + + INSTAPPFLAGS = $(INSTDATFLAGS) + + IMAKE = imake + DEPEND = makedepend + RGB = rgb + + FONTC = bdftopcf + + MKFONTDIR = mkfontdir + MKDIRHIER = /bin/sh $(BINDIR)/mkdirhier + + CONFIGSRC = $(TOP)/config + DOCUTILSRC = $(TOP)/doc/util + CLIENTSRC = $(TOP)/clients + DEMOSRC = $(TOP)/demos + LIBSRC = $(TOP)/lib + FONTSRC = $(TOP)/fonts + INCLUDESRC = $(TOP)/X11 + SERVERSRC = $(TOP)/server + UTILSRC = $(TOP)/util + SCRIPTSRC = $(UTILSRC)/scripts + EXAMPLESRC = $(TOP)/examples + CONTRIBSRC = $(TOP)/../contrib + DOCSRC = $(TOP)/doc + RGBSRC = $(TOP)/rgb + DEPENDSRC = $(UTILSRC)/makedepend + IMAKESRC = $(CONFIGSRC) + XAUTHSRC = $(LIBSRC)/Xau + XLIBSRC = $(LIBSRC)/X + XMUSRC = $(LIBSRC)/Xmu + TOOLKITSRC = $(LIBSRC)/Xt + AWIDGETSRC = $(LIBSRC)/Xaw + OLDXLIBSRC = $(LIBSRC)/oldX + XDMCPLIBSRC = $(LIBSRC)/Xdmcp + BDFTOSNFSRC = $(FONTSRC)/bdftosnf + BDFTOSNFSRC = $(FONTSRC)/clients/bdftosnf + BDFTOPCFSRC = $(FONTSRC)/clients/bdftopcf + MKFONTDIRSRC = $(FONTSRC)/clients/mkfontdir + FSLIBSRC = $(FONTSRC)/lib/fs + FONTSERVERSRC = $(FONTSRC)/server + EXTENSIONSRC = $(TOP)/extensions + XILIBSRC = $(EXTENSIONSRC)/lib/xinput + PHIGSLIBSRC = $(EXTENSIONSRC)/lib/PEX + +# $XConsortium: sunLib.tmpl,v 1.11 91/07/31 11:32:08 rws Exp $ + +SHLIBLDFLAGS = -assert pure-text +PICFLAGS = -pic + + DEPEXTENSIONLIB = + EXTENSIONLIB = -lXext + + DEPXLIB = $(DEPEXTENSIONLIB) + XLIB = $(EXTENSIONLIB) -lX11 + + DEPXMULIB = $(USRLIBDIR)/libXmu.sa.$(SOXMUREV) + XMULIB = -lXmu + + DEPOLDXLIB = + OLDXLIB = -loldX + + DEPXTOOLLIB = $(USRLIBDIR)/libXt.sa.$(SOXTREV) + XTOOLLIB = -lXt + + DEPXAWLIB = $(USRLIBDIR)/libXaw.sa.$(SOXAWREV) + XAWLIB = -lXaw + + DEPXILIB = + XILIB = -lXi + + SOXLIBREV = 4.10 + SOXTREV = 4.10 + SOXAWREV = 5.0 + SOOLDXREV = 4.10 + SOXMUREV = 4.10 + SOXEXTREV = 4.10 + SOXINPUTREV = 4.10 + + DEPXAUTHLIB = $(USRLIBDIR)/libXau.a + XAUTHLIB = -lXau + DEPXDMCPLIB = $(USRLIBDIR)/libXdmcp.a + XDMCPLIB = -lXdmcp + + DEPPHIGSLIB = $(USRLIBDIR)/libphigs.a + PHIGSLIB = -lphigs + + DEPXBSDLIB = $(USRLIBDIR)/libXbsd.a + XBSDLIB = -lXbsd + + LINTEXTENSIONLIB = $(LINTLIBDIR)/llib-lXext.ln + LINTXLIB = $(LINTLIBDIR)/llib-lX11.ln + LINTXMU = $(LINTLIBDIR)/llib-lXmu.ln + LINTXTOOL = $(LINTLIBDIR)/llib-lXt.ln + LINTXAW = $(LINTLIBDIR)/llib-lXaw.ln + LINTXI = $(LINTLIBDIR)/llib-lXi.ln + LINTPHIGS = $(LINTLIBDIR)/llib-lphigs.ln + + DEPLIBS = $(DEPXAWLIB) $(DEPXMULIB) $(DEPXTOOLLIB) $(DEPXLIB) + + DEPLIBS1 = $(DEPLIBS) + DEPLIBS2 = $(DEPLIBS) + DEPLIBS3 = $(DEPLIBS) + +# ------------------------------------------------------------------------- +# Imake rules for building libraries, programs, scripts, and data files +# rules: $XConsortium: Imake.rules,v 1.123 91/09/16 20:12:16 rws Exp $ + +# ------------------------------------------------------------------------- +# start of Imakefile + +OBJS =dvibook.o +SRCS =dvibook.c + + LIBTEXROOT= ../ + LIBTEXSRC = ../libtex + LIBTEX = $(LIBTEXSRC)/libtex.a + DEPLIBTEX = $(LIBTEX) + DEPLIBS = $(DEPLIBTEX) $(DEPXAWLIB) $(DEPXMULIB) $(DEPXTOOLLIB) $(DEPXLIB) +LOCAL_LIBRARIES = $(LIBTEX) + + DEFINES = -I$(LIBTEXROOT) + +FILES =Imakefile dvibook.man Makefile.raw $(SRCS) + +all: dvibook + +dvibook: $(OBJS) $(DEPLIBS) + $(RM) $@ + $(CC) -o $@ $(OBJS) $(LDOPTIONS) $(LOCAL_LIBRARIES) $(LDLIBS) $(SYSLIBS) $(EXTRA_LOAD_FLAGS) + +clean:: + $(RM) dvibook + +install:: dvibook + @if [ -d $(DESTDIR) $(BINDIR) ]; then set +x; \ + else (set -x; $(MKDIRHIER) $(DESTDIR) $(BINDIR)); fi + $(INSTALL) -c $(INSTPGMFLAGS) dvibook $(DESTDIR) $(BINDIR) + +install.man:: dvibook.man + @if [ -d $(DESTDIR)$(MANDIR) ]; then set +x; \ + else (set -x; $(MKDIRHIER) $(DESTDIR)$(MANDIR)); fi + $(INSTALL) -c $(INSTMANFLAGS) dvibook.man $(DESTDIR)$(MANDIR)/dvibook.$(MANSUFFIX) + +depend:: + $(DEPEND) $(DEPENDFLAGS) -s "# DO NOT DELETE" -- $(ALLDEFINES) -- $(SRCS) + +tags:: + $(TAGS) -w *.[ch] + $(TAGS) -xw *.[ch] > TAGS + +tar: + cd ../..;\ + for file in $(FILES); \ + do \ + tar rf $(TARFILE) $(PRODUCT)/Dvibook/$$file; \ + done + +# ------------------------------------------------------------------------- +# common rules for all Makefiles - do not edit + +emptyrule:: + +clean:: + $(RM_CMD) "#"* + +Makefile:: + -@if [ -f Makefile ]; then set -x; \ + $(RM) Makefile.bak; $(MV) Makefile Makefile.bak; \ + else exit 0; fi + $(IMAKE_CMD) -DTOPDIR=$(TOP) -DCURDIR=$(CURRENT_DIR) + +tags:: + $(TAGS) -w *.[ch] + $(TAGS) -xw *.[ch] > TAGS + +saber: + # load $(ALLDEFINES) $(SRCS) + +osaber: + # load $(ALLDEFINES) $(OBJS) + +# ------------------------------------------------------------------------- +# empty rules for directories that do not have SUBDIRS - do not edit + +install:: + @echo "install in $(CURRENT_DIR) done" + +install.man:: + @echo "install.man in $(CURRENT_DIR) done" + +Makefiles:: + +includes:: + +# ------------------------------------------------------------------------- +# dependencies generated by makedepend + diff --git a/dviware/dvibook/Dvibook/dvibook.c b/dviware/dvibook/Dvibook/dvibook.c new file mode 100644 index 0000000000..3ee3266e6e --- /dev/null +++ b/dviware/dvibook/Dvibook/dvibook.c @@ -0,0 +1,871 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/dvi/RCS/dviselect.c,v 3.1 89/08/22 17:16:13 chris Exp $"; +#endif + +/* + * DVI page rearrangement program + * + * Reads DVI version 2 files and rearranges pages into signatures, + * writing a new DVI file. + */ + +#include "libtex/types.h" +#include "libtex/dviclass.h" +#include "libtex/dvicodes.h" +#include "libtex/error.h" +#include "libtex/fio.h" +#include "libtex/gripes.h" +#include "libtex/search.h" +#include <stdio.h> +#include <ctype.h> +#include "libtex/seek.h" + +#define white(x) ((x) == ' ' || (x) == '\t' || (x) == ',') + +#define MAXDVIPAGES 1000 /* max (absolute) pages in DVI file */ + +char *ProgName; +extern char *optarg; +extern int optind; + +/* Globals */ +char serrbuf[BUFSIZ]; /* buffer for stderr */ + +/* + * We will try to keep output lines shorter than MAXCOL characters. + */ +#define MAXCOL 75 + +/* + * We use the following structure to keep track of fonts we have seen. + * The final DVI file lists only the fonts it uses. + */ +struct fontinfo { + i32 fi_newindex; /* font number in output file */ + int fi_reallyused; /* true => used on a page we copied */ + i32 fi_checksum; /* the checksum */ + i32 fi_mag; /* the magnification */ + i32 fi_designsize; /* the design size */ + short fi_n1; /* the name header length */ + short fi_n2; /* the name body length */ + char *fi_name; /* the name itself */ +}; + +int Signature; /* #pages per signature (multiple of 4) */ + +int SFlag; /* true => -s, silent operation */ + +struct search *FontFinder; /* maps from input indicies to fontinfo */ +i32 NextOutputFontIndex; /* generates output indicies */ +i32 CurrentFontIndex; /* current (old) index in input */ +i32 OutputFontIndex; /* current (new) index in ouput */ + +char *DVIFileName; /* name of input DVI file */ +FILE *inf; /* the input file itself */ +FILE *outf; /* the output DVI file */ + +long StartOfPage[MAXDVIPAGES]; /* The file positions of the + input pages */ + +long StartOfLastPage; /* The file position just before we + started the last page */ +long CurrentPosition; /* The current position of the file */ + +int UseThisPage; /* true => current page is selected */ + +i32 InputPageNumber; /* current absolute page in old DVI file */ +int NumberOfOutputPages; /* number of pages in new DVI file */ + +i32 Numerator; /* numerator from DVI file */ +i32 Denominator; /* denominator from DVI file */ +i32 DVIMag; /* magnification from DVI file */ + +i32 Count[10]; /* the 10 \count variables */ + +/* save some string space: we use this a lot */ +char writeerr[] = "error writing DVI file"; + +char *malloc(), *realloc(); +/* + * You may get lint warnings about sprintf's return value. + * Older versions of 4BSD have `char *sprintf()'. ANSI and + * SysV use `int sprintf()'; so ignore the warnings. + */ + +/* + * Lint gets somewhat confused over putc. + */ +#ifdef lint +#undef putc +#ifdef ultrix /* grr */ +#define putc(c, f) fputc((char)(c), f) +#else +#define putc(c, f) fputc((int)(c), f) +#endif +#endif + +/* + * Print a message to stderr, with an optional leading space, and handling + * long line wraps. + */ +message(space, str, len) + int space; + register char *str; + register int len; +{ + static int beenhere; + static int col; + + if (!beenhere) + space = 0, beenhere++; + if (len == 0) + len = strlen(str); + col += len; + if (space) { + if (col >= MAXCOL) + (void) putc('\n', stderr), col = len; + else + (void) putc(' ', stderr), col++; + } + while (--len >= 0) + (void) putc(*str++, stderr); + (void) fflush(stderr); +} + +/* + * Start a page (process a DVI_BOP). + */ +BeginPage() +{ + register i32 *i; + + OutputFontIndex = -1; /* new page requires respecifying font */ + for (i = Count; i < &Count[10]; i++) + fGetLong(inf, *i); + (void) GetLong(inf); /* previous page pointer */ + + if (!UseThisPage) + return; + + putbyte(outf, DVI_BOP); + for (i = Count; i < &Count[10]; i++) + PutLong(outf, *i); + PutLong(outf, StartOfLastPage); + if (ferror(outf)) + error(1, -1, writeerr); + + StartOfLastPage = CurrentPosition; + CurrentPosition += 45; /* we just wrote this much */ + + if (!SFlag) { /* write nice page usage messages */ + register int z = 0; + register int mlen = 0; + char msg[80]; + + (void) sprintf(msg, "[%ld", (long)Count[0]); + mlen = strlen(msg); + for (i = &Count[1]; i < &Count[10]; i++) { + if (*i == 0) { + z++; + continue; + } + while (--z >= 0) + msg[mlen++] = '.', msg[mlen++] = '0'; + z = 0; + (void) sprintf(msg + mlen, ".%ld", (long)*i); + mlen += strlen(msg + mlen); + } + message(1, msg, mlen); + } +} + +/* + * End a page (process a DVI_EOP). + */ +EndPage() +{ + + if (!UseThisPage) + return; + if (!SFlag) + message(0, "]", 1); + putbyte(outf, DVI_EOP); + if (ferror(outf)) + error(1, -1, writeerr); + CurrentPosition++; + NumberOfOutputPages++; +} + +/* + * For each of the fonts used in the new DVI file, write out a definition. + */ +/* ARGSUSED */ +void +PostAmbleFontEnumerator(addr, key) + char *addr; + i32 key; +{ + + if (((struct fontinfo *)addr)->fi_reallyused) + WriteFont((struct fontinfo *)addr); +} + +HandlePostAmble() +{ + register i32 c; + + (void) GetLong(inf); /* previous page pointer */ + if (GetLong(inf) != Numerator) + GripeMismatchedValue("numerator"); + if (GetLong(inf) != Denominator) + GripeMismatchedValue("denominator"); + if (GetLong(inf) != DVIMag) + GripeMismatchedValue("\\magnification"); + + putbyte(outf, DVI_POST); + PutLong(outf, StartOfLastPage); + PutLong(outf, Numerator); + PutLong(outf, Denominator); + PutLong(outf, DVIMag); + c = GetLong(inf); + PutLong(outf, c); /* tallest page height */ + c = GetLong(inf); + PutLong(outf, c); /* widest page width */ + c = GetWord(inf); + PutWord(outf, c); /* DVI stack size */ + PutWord(outf, NumberOfOutputPages); + StartOfLastPage = CurrentPosition; /* point at post */ + CurrentPosition += 29; /* count all those `put's */ +#ifdef notdef + (void) GetWord(inf); /* skip original number of pages */ +#endif + + /* + * just ignore all the incoming font definitions; we are done with + * input file + */ + + /* + * run through the FontFinder table and dump definitions for the + * fonts we have used. + */ + SEnumerate(FontFinder, PostAmbleFontEnumerator); + + putbyte(outf, DVI_POSTPOST); + PutLong(outf, StartOfLastPage); /* actually start of postamble */ + putbyte(outf, DVI_VERSION); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + CurrentPosition += 10; + while (CurrentPosition & 3) { + putbyte(outf, DVI_FILLER); + CurrentPosition++; + } + if (ferror(outf)) + error(1, -1, writeerr); +} + +/* + * Write a font definition to the output file + */ +WriteFont(fi) + register struct fontinfo *fi; +{ + register int l; + register char *s; + + if (fi->fi_newindex < 256) { + putbyte(outf, DVI_FNTDEF1); + putbyte(outf, fi->fi_newindex); + CurrentPosition += 2; + } else if (fi->fi_newindex < 65536) { + putbyte(outf, DVI_FNTDEF2); + PutWord(outf, fi->fi_newindex); + CurrentPosition += 3; + } else if (fi->fi_newindex < 16777216) { + putbyte(outf, DVI_FNTDEF3); + Put3Byte(outf, fi->fi_newindex); + CurrentPosition += 4; + } else { + putbyte(outf, DVI_FNTDEF4); + PutLong(outf, fi->fi_newindex); + CurrentPosition += 5; + } + PutLong(outf, fi->fi_checksum); + PutLong(outf, fi->fi_mag); + PutLong(outf, fi->fi_designsize); + putbyte(outf, fi->fi_n1); + putbyte(outf, fi->fi_n2); + l = fi->fi_n1 + fi->fi_n2; + CurrentPosition += 14 + l; + s = fi->fi_name; + while (--l >= 0) + putbyte(outf, *s++); +} + +/* + * Handle the preamble. Someday we should update the comment field. + */ +HandlePreAmble() +{ + register int n, c; + + c = getc(inf); + if (c == EOF) + GripeUnexpectedDVIEOF(); + if (c != DVI_PRE) + GripeMissingOp("PRE"); + if (getc(inf) != DVI_VERSION) + error(1, 0, "%s is not a DVI version %d file", + DVIFileName, DVI_VERSION); + Numerator = GetLong(inf); + Denominator = GetLong(inf); + DVIMag = GetLong(inf); + putbyte(outf, DVI_PRE); + putbyte(outf, DVI_VERSION); + PutLong(outf, Numerator); + PutLong(outf, Denominator); + PutLong(outf, DVIMag); + + n = UnSign8(GetByte(inf)); + CurrentPosition = 15 + n; /* well, almost */ + putbyte(outf, n); + while (--n >= 0) { + c = GetByte(inf); + putbyte(outf, c); + } +} + +main(argc, argv) + int argc; + register char **argv; +{ + register int c; + register char *s; + char *outname = NULL; + + Signature = 0; + + ProgName = *argv; + setbuf(stderr, serrbuf); + + while ((c = getopt(argc, argv, "i:o:s:q")) != EOF) { + switch (c) { + + case 'q': /* silent */ + SFlag++; + break; + + case 'i': + if (DVIFileName != NULL) + goto usage; + DVIFileName = optarg; + break; + + case 'o': + if (outname != NULL) + goto usage; + outname = optarg; + break; + + case 's': + if (Signature != 0) + goto usage; + Signature = atoi(optarg); + if (Signature <= 0) + error(1, -1, "-s parameter must be > 0"); + if (Signature % 4 != 0) + error(1, -1, + "-s parameter must be a multiple of 4"); + break; + + case '?': +usage: + (void) fprintf(stderr, "\ +Usage: %s [-s signature] [-q] [-i infile] [-o outfile] [infile [outfile]]\n", + ProgName); + (void) fflush(stderr); + exit(1); + } + } + + while (optind < argc) { + s = argv[optind++]; + c = *s; + if (DVIFileName == NULL) + DVIFileName = s; + else if (outname == NULL) + outname = s; + else + goto usage; + } + if (DVIFileName == NULL) { + DVIFileName = "`stdin'"; + inf = stdin; + } else if ((inf = fopen(DVIFileName, "r")) == 0) + error(1, -1, "cannot read %s", DVIFileName); + if (outname == NULL) + outf = stdout; + else if ((outf = fopen(outname, "w")) == 0) + error(1, -1, "cannot write %s", outname); + + if ((FontFinder = SCreate(sizeof(struct fontinfo))) == 0) + error(1, 0, "cannot create font finder (out of memory?)"); + + /* copy inf to TEMP file if not seekable */ + if ((inf = SeekFile(inf)) == NULL) { + error(1, 0, "can't seek file"); + } + InputPageNumber = 0; + StartOfLastPage = -1; + HandlePreAmble(); + ScanDVIFile(); + HandleDVIFile(); + HandlePostAmble(); + if (!SFlag) + (void) fprintf(stderr, "\nWrote %d page%s, %ld bytes\n", + NumberOfOutputPages, NumberOfOutputPages == 1 ? "" : "s", + (long)CurrentPosition); + exit(0); + /* NOTREACHED */ +} + +/* + * Handle a font definition. + */ +HandleFontDef(index) + i32 index; +{ + register struct fontinfo *fi; + register int i; + register char *s; + int def = S_CREATE | S_EXCL; + + if (!UseThisPage) { + if ((fi = (struct fontinfo *)SSearch(FontFinder, index, &def)) == 0) + if (def & S_COLL) + error(1, 0, "font %ld already defined", (long)index); + else + error(1, 0, "cannot stash font %ld (out of memory?)", + (long)index); + fi->fi_reallyused = 0; + fi->fi_checksum = GetLong(inf); + fi->fi_mag = GetLong(inf); + fi->fi_designsize = GetLong(inf); + fi->fi_n1 = UnSign8(GetByte(inf)); + fi->fi_n2 = UnSign8(GetByte(inf)); + i = fi->fi_n1 + fi->fi_n2; + if ((s = malloc((unsigned)i)) == 0) + GripeOutOfMemory(i, "font name"); + fi->fi_name = s; + while (--i >= 0) + *s++ = GetByte(inf); + } else { + (void) GetLong(inf); + (void) GetLong(inf); + (void) GetLong(inf); + i = UnSign8(GetByte(inf)); + i += UnSign8(GetByte(inf)); + while (--i >= 0) + (void) GetByte(inf); + } +} + +/* + * Handle a \special. + */ +HandleSpecial(c, l, p) + int c; + register int l; + register i32 p; +{ + register int i; + + if (UseThisPage) { + putbyte(outf, c); + switch (l) { + + case DPL_UNS1: + putbyte(outf, p); + CurrentPosition += 2; + break; + + case DPL_UNS2: + PutWord(outf, p); + CurrentPosition += 3; + break; + + case DPL_UNS3: + Put3Byte(outf, p); + CurrentPosition += 4; + break; + + case DPL_SGN4: + PutLong(outf, p); + CurrentPosition += 5; + break; + + default: + panic("HandleSpecial l=%d", l); + /* NOTREACHED */ + } + CurrentPosition += p; + while (--p >= 0) { + i = getc(inf); + putbyte(outf, i); + } + if (feof(inf)) + GripeUnexpectedDVIEOF(); + if (ferror(outf)) + error(1, -1, writeerr); + } else + while (--p >= 0) + (void) getc(inf); +} + +ReallyUseFont() +{ + register struct fontinfo *fi; + int look = S_LOOKUP; + + fi = (struct fontinfo *)SSearch(FontFinder, CurrentFontIndex, &look); + if (fi == NULL) + error(1, 0, "DVI file requested font %ld without defining it", + (long)CurrentFontIndex); + if (fi->fi_reallyused == 0) { + fi->fi_reallyused++; + fi->fi_newindex = NextOutputFontIndex++; + WriteFont(fi); + } + if (fi->fi_newindex != OutputFontIndex) { + PutFontSelector(fi->fi_newindex); + OutputFontIndex = fi->fi_newindex; + } +} + +/* + * Write a font selection command to the output file + */ +PutFontSelector(index) + i32 index; +{ + + if (index < 64) { + putbyte(outf, index + DVI_FNTNUM0); + CurrentPosition++; + } else if (index < 256) { + putbyte(outf, DVI_FNT1); + putbyte(outf, index); + CurrentPosition += 2; + } else if (index < 65536) { + putbyte(outf, DVI_FNT2); + PutWord(outf, index); + CurrentPosition += 3; + } else if (index < 16777216) { + putbyte(outf, DVI_FNT3); + Put3Byte(outf, index); + CurrentPosition += 4; + } else { + putbyte(outf, DVI_FNT4); + PutLong(outf, index); + CurrentPosition += 5; + } +} + +/* + * The following table describes the length (in bytes) of each of the DVI + * commands that we can simply copy, starting with DVI_SET1 (128). + */ +char oplen[128] = { + 0, 0, 0, 0, /* DVI_SET1 .. DVI_SET4 */ + 9, /* DVI_SETRULE */ + 0, 0, 0, 0, /* DVI_PUT1 .. DVI_PUT4 */ + 9, /* DVI_PUTRULE */ + 1, /* DVI_NOP */ + 0, /* DVI_BOP */ + 0, /* DVI_EOP */ + 1, /* DVI_PUSH */ + 1, /* DVI_POP */ + 2, 3, 4, 5, /* DVI_RIGHT1 .. DVI_RIGHT4 */ + 1, /* DVI_W0 */ + 2, 3, 4, 5, /* DVI_W1 .. DVI_W4 */ + 1, /* DVI_X0 */ + 2, 3, 4, 5, /* DVI_X1 .. DVI_X4 */ + 2, 3, 4, 5, /* DVI_DOWN1 .. DVI_DOWN4 */ + 1, /* DVI_Y0 */ + 2, 3, 4, 5, /* DVI_Y1 .. DVI_Y4 */ + 1, /* DVI_Z0 */ + 2, 3, 4, 5, /* DVI_Z1 .. DVI_Z4 */ + 0, /* DVI_FNTNUM0 (171) */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 172 .. 179 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 180 .. 187 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 188 .. 195 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 196 .. 203 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 204 .. 211 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 212 .. 219 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 220 .. 227 */ + 0, 0, 0, 0, 0, 0, 0, /* 228 .. 234 */ + 0, 0, 0, 0, /* DVI_FNT1 .. DVI_FNT4 */ + 0, 0, 0, 0, /* DVI_XXX1 .. DVI_XXX4 */ + 0, 0, 0, 0, /* DVI_FNTDEF1 .. DVI_FNTDEF4 */ + 0, /* DVI_PRE */ + 0, /* DVI_POST */ + 0, /* DVI_POSTPOST */ + 0, 0, 0, 0, 0, 0, /* 250 .. 255 */ +}; + +/* + * Here we scan the input DVI file and record pointers to the pages. + */ +ScanDVIFile() +{ + UseThisPage = 0; + + StartOfPage[InputPageNumber] = ftell(inf); + while (HandlePage()) { /* scan DVI file */ + StartOfPage[++InputPageNumber] = ftell(inf); + } +} + +/* + * Here we read the input DVI file and write relevant pages to the + * output DVI file. We also keep track of font changes, handle font + * definitions, and perform some other housekeeping. + */ +HandleDVIFile() +{ + int CurrentPage, ActualPage, MaxPage; + + UseThisPage = 1; + + MaxPage = InputPageNumber + (4-InputPageNumber%4)%4; + + if (!Signature) + Signature = MaxPage; + for (CurrentPage = 0; CurrentPage < MaxPage; CurrentPage++) { + ActualPage = CurrentPage - CurrentPage%Signature; + switch (CurrentPage%4) { + case 0: + case 3: + ActualPage += Signature-1-(CurrentPage%Signature)/2; + break; + case 1: + case 2: + ActualPage += (CurrentPage%Signature)/2; + break; + } + if (ActualPage < InputPageNumber) { + if (fseek(inf, StartOfPage[ActualPage], 0) == -1) + error(1, -1, + "can't seek page %d", ActualPage+1); + HandlePage(); + } else + PutEmptyPage(); + } + if (fseek(inf, StartOfPage[InputPageNumber]+1, 0) == -1) + error(1, -1, "can't seek last page"); +} + +int +HandlePage() +{ + register int c, l; + register i32 p; + register int CurrentFontOK = 0; + int doingpage = 0; + + /* Only way out is via "return" statement */ + for (;;) { + c = getc(inf); /* getc() returns unsigned values */ + if (DVI_IsChar(c)) { + /* + * Copy chars, note font usage, but ignore if + * page is not interesting. + */ + if (!UseThisPage) + continue; + if (!CurrentFontOK) { + ReallyUseFont(); + CurrentFontOK++; + } + putbyte(outf, c); + CurrentPosition++; + continue; + } + if (DVI_IsFont(c)) { /* note font change */ + CurrentFontIndex = c - DVI_FNTNUM0; + CurrentFontOK = 0; + continue; + } + if (c == EOF) + GripeUnexpectedDVIEOF(); + if ((l = (oplen - 128)[c]) != 0) { /* simple copy */ + if (!UseThisPage) { + while (--l > 0) + (void) getc(inf); + continue; + } + CurrentPosition += l; + putbyte(outf, c); + while (--l > 0) { + c = getc(inf); + putbyte(outf, c); + } + if (ferror(outf)) + error(1, -1, writeerr); + continue; + } + if ((l = DVI_OpLen(c)) != 0) { + /* + * Handle other generics. + * N.B.: there should only be unsigned parameters + * here (save SGN4), for commands with negative + * parameters have been taken care of above. + */ + switch (l) { + + case DPL_UNS1: + p = getc(inf); + break; + + case DPL_UNS2: + fGetWord(inf, p); + break; + + case DPL_UNS3: + fGet3Byte(inf, p); + break; + + case DPL_SGN4: + fGetLong(inf, p); + break; + + default: + panic("HandleDVIFile l=%d", l); + } + + /* + * Now that we have the parameter, perform the + * command. + */ + switch (DVI_DT(c)) { + + case DT_SET: + case DT_PUT: + if (!UseThisPage) + continue; + if (!CurrentFontOK) { + ReallyUseFont(); + CurrentFontOK++; + } + putbyte(outf, c); + switch (l) { + + case DPL_UNS1: + putbyte(outf, p); + CurrentPosition += 2; + continue; + + case DPL_UNS2: + PutWord(outf, p); + CurrentPosition += 3; + continue; + + case DPL_UNS3: + Put3Byte(outf, p); + CurrentPosition += 4; + continue; + + case DPL_SGN4: + PutLong(outf, p); + CurrentPosition += 5; + continue; + } + + case DT_FNT: + CurrentFontIndex = p; + CurrentFontOK = 0; + continue; + + case DT_XXX: + HandleSpecial(c, l, p); + continue; + + case DT_FNTDEF: + HandleFontDef(p); + continue; + + default: + panic("HandleDVIFile DVI_DT(%d)=%d", + c, DVI_DT(c)); + } + continue; + } + + switch (c) { /* handle the few remaining cases */ + + case DVI_BOP: + if (doingpage) + GripeUnexpectedOp("BOP (during page)"); + BeginPage(); + doingpage = 1; + break; + + case DVI_EOP: + if (!doingpage) + GripeUnexpectedOp("EOP (outside page)"); + EndPage(); + doingpage = 0; + return(1); + + case DVI_PRE: + GripeUnexpectedOp("PRE"); + /* NOTREACHED */ + + case DVI_POST: + if (doingpage) + GripeUnexpectedOp("POST (inside page)"); + return(0); + + case DVI_POSTPOST: + GripeUnexpectedOp("POSTPOST"); + /* NOTREACHED */ + + default: + GripeUndefinedOp(c); + /* NOTREACHED */ + } + } +} + +/* write an empty page to fill out space */ +PutEmptyPage() +{ + int i; + + putbyte(outf, DVI_BOP); + PutLong(outf, -1L); + for (i = 1; i < 10; i++) /* set all sub counts to 0 */ + PutLong(outf, 0L); + PutLong(outf, StartOfLastPage); + putbyte(outf, DVI_EOP); + if (!SFlag) { /* write nice page usage messages */ + char *msg = "[*]"; + message(1, msg, strlen(msg)); + } + if (ferror(outf)) + error(1, -1, writeerr); + + StartOfLastPage = CurrentPosition; + CurrentPosition += 46; /* we just wrote this much */ + NumberOfOutputPages++; +} diff --git a/dviware/dvibook/Dvibook/dvibook.man b/dviware/dvibook/Dvibook/dvibook.man new file mode 100644 index 0000000000..00a9df0101 --- /dev/null +++ b/dviware/dvibook/Dvibook/dvibook.man @@ -0,0 +1,52 @@ +.TH DVIBOOK 1 +.SH NAME +dvibook \- rearrange pages in DVI file into signatures +.SH SYNOPSIS +.B dvibook +[ +.B \-q +] [ +.B \-s\fI<num> +] [ +.B \-i +.I infile +] [ +.B \-o +.I outfile +] +[ +.I infile +[ +.I outfile +] ] +.SH DESCRIPTION +.I Dvibook +rearranges pages from a DVI file produced by TeX into ``signatures'' for +printing books or booklets, creating a new DVI file +usable by any of the TeX conversion programs. +.PP +The +.I \-s +option selects the size of signature which will be used. The signature size is +the number of sides which will be folded and bound together; the number given +should be a multiple of four. The default is to use one signature for the +whole file. Extra blank sides will be added if the file does not contain a +multiple of four pages. +.PP +Dvibook normally prints the page numbers of the pages rearranged; the +.I \-q +option suppresses this. +.SH AUTHOR +Angus Duggan, from dviselect(1), by +Chris Torek, University of Maryland +.SH "SEE ALSO" +dviconcat(1), dviselect(1), latex(1), tex(1) +.br +.I "MC-TeX User's Guide" +.br +.I "The TeXbook" +.I Dvibook +does not adjust the parameters in the postamble; however, since these +values are normally used only to size certain structures in the output +conversion programs, and the parameters never need to be adjusted upward, +this has not proven to be a problem. diff --git a/dviware/dvibook/Dviconcat/Imakefile b/dviware/dvibook/Dviconcat/Imakefile new file mode 100644 index 0000000000..05ca4e87c5 --- /dev/null +++ b/dviware/dvibook/Dviconcat/Imakefile @@ -0,0 +1,20 @@ +#include "../Imake.Config" + + BINDIR = $(TEX_BINDIR) + MANDIR = $(TEX_MANDIR) + + FILES = Imakefile dviconcat.man $(SRCS) + + INCLUDES = -I$(LIBTEXROOT) +LOCAL_LIBRARIES = $(LIBTEX) + DEPLIBS = $(DEPLIBTEX) \ + $(DEPXAWLIB) $(DEPXMULIB) $(DEPXTOOLLIB) $(DEPXLIB) + +SimpleProgramTarget(dviconcat) + +tar: + cd ../..;\ + for file in $(FILES); \ + do \ + tar rf $(TARFILE) $(PRODUCT)/Dviconcat/$$file; \ + done diff --git a/dviware/dvibook/Dviconcat/dviconcat.c b/dviware/dvibook/Dviconcat/dviconcat.c new file mode 100644 index 0000000000..5f4629286d --- /dev/null +++ b/dviware/dvibook/Dviconcat/dviconcat.c @@ -0,0 +1,756 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/dvi/RCS/dviconcat.c,v 3.1 89/08/22 17:23:15 chris Exp $"; +#endif + +/* + * DVI page concatenation program. + */ + +#include "libtex/types.h" +#include "libtex/dviclass.h" +#include "libtex/dvicodes.h" +#include "libtex/error.h" +#include "libtex/fio.h" +#include "libtex/gripes.h" +#include "libtex/search.h" +#include <stdio.h> +#include <ctype.h> + +char *ProgName; +extern char *optarg; +extern int optind; + +/* + * We use the following structure to keep track of fonts we have seen. + * The final DVI file lists all the fonts used in each of the input + * files. + */ +struct fontinfo { + struct fontinfo *fi_next;/* next in list */ + i32 fi_index; /* font number in output file */ + i32 fi_checksum; /* the checksum */ + i32 fi_mag; /* the magnification */ + i32 fi_designsize; /* the design size */ + short fi_n1; /* the name header length */ + short fi_n2; /* the name body length */ + char *fi_name; /* the name itself */ +} *fonts; + +struct search *FontFinder; /* input indicies to ptr-to-fontinfo */ +i32 NextOutputFontIndex; /* generates output indicies */ +i32 OutputFontIndex; /* current (new) index in ouput */ + +char *DVIFileName; /* the current input file name */ +FILE *inf; /* the current input DVI file */ +FILE *outf; /* the output DVI file */ + +int errs; /* counts non-fatal errors */ + +long StartOfLastPage; /* The file position just before we started + the last page (this is later written to + the output file as the previous page + pointer). */ +long CurrentPosition; /* The current position of the file */ + +int NumberOfOutputPages; /* number of pages in new DVI file */ + +i32 Numerator; /* numerator from current DVI file */ +i32 Denominator; /* denominator from current DVI file */ +i32 DVIMag; /* magnification from current DVI file */ + +i32 OutputNumerator; /* numerator from first DVI file */ +i32 OutputDenominator; /* denominator from first DVI file */ +i32 OutputMag; /* magnification from first DVI file or arg */ + +i32 TallestPageHeight; /* max of all tallest-page-height values */ +i32 WidestPageWidth; /* max of all widest-page-widths */ +i16 DVIStackSize; /* max of all stack sizes */ + +/* save some string space: we use this a lot */ +char writeerr[] = "error writing DVI file"; + +char *malloc(), *realloc(); +/* + * You may get lint warnings about sprintf's return value. + * Older versions of 4BSD have `char *sprintf()'. ANSI and + * SysV use `int sprintf()'; so ignore the warnings. + */ + +/* + * Lint gets somewhat confused over putc. + */ +#ifdef lint +#define putc(c, f) fputc((int) c, f) +#endif + +/* + * Start a page (process a DVI_BOP). + */ +void +BeginPage() +{ + register i32 t; + register int i; + + OutputFontIndex = -1; /* new page requires respecifying font */ + putbyte(outf, DVI_BOP); + /* copy the count registers */ + for (i = 10; --i >= 0;) { + fGetLong(inf, t); + PutLong(outf, t); + } + (void) GetLong(inf); /* previous page pointer */ + PutLong(outf, StartOfLastPage); + if (ferror(outf)) + error(1, -1, writeerr); + + StartOfLastPage = CurrentPosition; + CurrentPosition += 45; /* we just wrote this much */ +} + +/* + * End a page (process a DVI_EOP). + */ +void +EndPage() +{ + + putbyte(outf, DVI_EOP); + if (ferror(outf)) + error(1, -1, writeerr); + CurrentPosition++; + NumberOfOutputPages++; +} + +/* + * Write a font definition to the output file. + */ +void +WriteFont(fi) + register struct fontinfo *fi; +{ + register int l; + register char *s; + + if (fi->fi_index < 256) { + putbyte(outf, DVI_FNTDEF1); + putbyte(outf, fi->fi_index); + CurrentPosition += 2; + } else if (fi->fi_index < 65536) { + putbyte(outf, DVI_FNTDEF2); + PutWord(outf, fi->fi_index); + CurrentPosition += 3; + } else if (fi->fi_index < 16777216) { + putbyte(outf, DVI_FNTDEF3); + Put3Byte(outf, fi->fi_index); + CurrentPosition += 4; + } else { + putbyte(outf, DVI_FNTDEF4); + PutLong(outf, fi->fi_index); + CurrentPosition += 5; + } + PutLong(outf, fi->fi_checksum); + PutLong(outf, fi->fi_mag); + PutLong(outf, fi->fi_designsize); + putbyte(outf, fi->fi_n1); + putbyte(outf, fi->fi_n2); + l = fi->fi_n1 + fi->fi_n2; + CurrentPosition += 14 + l; + s = fi->fi_name; + while (--l >= 0) + putbyte(outf, *s++); +} + +/* + * Write the postamble for the concatenation of all the input files. + */ +void +WritePostAmble() +{ + register struct fontinfo *fi; + register i32 postpos = CurrentPosition; /* remember for later */ + + /* POST p n d m h w s pages */ + putbyte(outf, DVI_POST); + PutLong(outf, StartOfLastPage); + PutLong(outf, OutputNumerator); + PutLong(outf, OutputDenominator); + PutLong(outf, OutputMag); + PutLong(outf, TallestPageHeight); + PutLong(outf, WidestPageWidth); + PutWord(outf, DVIStackSize); + PutWord(outf, NumberOfOutputPages); + CurrentPosition += 29; /* count all those `put's */ + + for (fi = fonts; fi != NULL; fi = fi->fi_next) + WriteFont(fi); + + putbyte(outf, DVI_POSTPOST); + PutLong(outf, postpos); + putbyte(outf, DVI_VERSION); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + CurrentPosition += 10; + while (CurrentPosition & 3) { + putbyte(outf, DVI_FILLER); + CurrentPosition++; + } + if (ferror(outf)) + error(1, -1, writeerr); +} + +/* + * Read the information we need from the postamble for the current DVI file. + */ +void +HandlePostAmble() +{ + register i32 t; + register i16 w; + + (void) GetLong(inf); /* previous page pointer */ + if (GetLong(inf) != Numerator) { + error(0, 0, + "%s: postamble's numerator does not match preamble's", + DVIFileName); + errs++; + } + if (GetLong(inf) != Denominator) { + error(0, 0, + "%s: postamble's denominator does not match preamble's", + DVIFileName); + errs++; + } + if (GetLong(inf) != DVIMag) { + error(0, 0, + "%s: postamble's magnification does not match preamble's", + DVIFileName); + errs++; + } + + /* + * Find maximum of tallest page height, widest page width, and + * stack size. + */ + t = GetLong(inf); + if (t > TallestPageHeight) + TallestPageHeight = t; + t = GetLong(inf); + if (t > WidestPageWidth) + WidestPageWidth = t; + w = GetWord(inf); + if (w > DVIStackSize) + DVIStackSize = w; + + /* + * The remainder of the file---number of pages, list of fonts, + * postpost, pointer, version, and filler---we simply ignore. + */ +} + +/* + * Handle a preamble. + * `firstone' is true if this is the first input file. + * Return true iff something is wrong with the file. + */ +int +HandlePreAmble(firstone) + int firstone; +{ + register int n, c; + static char warn1[] = "%s: Warning: preamble %s of %ld"; + static char warn2[] = "does not match first file's value of %ld"; + + if (getc(inf) != DVI_PRE) { + error(0, 0, "%s does not begin with a preamble", DVIFileName); + error(0, 0, "(are you sure it is a DVI file?)"); + errs++; + return (1); + } + if (getc(inf) != DVI_VERSION) { + error(0, 0, "%s is not a DVI version %d file", DVIFileName, + DVI_VERSION); + errs++; + return (1); + } + + /* committed to DVI file: now most errors are fatal */ + if (firstone) { + OutputNumerator = Numerator = GetLong(inf); + OutputDenominator = Denominator = GetLong(inf); + } else { + Numerator = GetLong(inf); + if (Numerator != OutputNumerator) { + error(0, 0, warn1, DVIFileName, "numerator", + (long)Numerator); + error(0, 0, warn2, (long)OutputNumerator); + errs++; + } + Denominator = GetLong(inf); + if (Denominator != OutputDenominator) { + error(0, 0, warn1, DVIFileName, "denominator", + (long)Denominator); + error(0, 0, warn2, (long)OutputDenominator); + errs++; + } + } + DVIMag = GetLong(inf); + if (OutputMag == 0) + OutputMag = DVIMag; + else if (DVIMag != OutputMag) { + error(0, 0, + "%s: Warning: magnification of %ld changed to %ld", + DVIFileName, (long)DVIMag, (long)OutputMag); + errs++; + } + n = UnSign8(GetByte(inf)); /* comment length */ + if (firstone) { + putbyte(outf, DVI_PRE); + putbyte(outf, DVI_VERSION); + PutLong(outf, Numerator); + PutLong(outf, Denominator); + PutLong(outf, OutputMag); + + CurrentPosition = 15 + n; /* well, almost */ + putbyte(outf, n); + while (--n >= 0) { + c = GetByte(inf); + putbyte(outf, c); + } + } else { + while (--n >= 0) + (void) GetByte(inf); + } + return (0); +} + +/* + * Read one DVI file, concatenating it with the previous one (if any) + * or starting up the output file (otherwise). + */ +void +doit(name, fp) + char *name; + FILE *fp; +{ + static int started; + + DVIFileName = name; + inf = fp; + if (HandlePreAmble(started ? 0 : 1)) + return; + SClear(FontFinder); + HandleDVIFile(); + HandlePostAmble(); + started = 1; +} + +main(argc, argv) + int argc; + register char **argv; +{ + register int c; + register char *s; + FILE *f; + + ProgName = *argv; + + /* + * Handle arguments. + */ + while ((c = getopt(argc, argv, "m:o:")) != EOF) { + switch (c) { + + case 'm': + if (OutputMag) + goto usage; + OutputMag = atoi(optarg); + break; + + case 'o': + if (outf != NULL) + goto usage; + if ((outf = fopen(optarg, "w")) == NULL) + error(1, -1, "cannot write %s", optarg); + break; + + case '?': +usage: + (void) fprintf(stderr, + "Usage: %s [-m mag] [-o outfile] [files]\n", + ProgName); + (void) fflush(stderr); + exit(1); + } + } + + /* setup */ + if (outf == NULL) + outf = stdout; + if ((FontFinder = SCreate(sizeof(struct fontinfo *))) == 0) + error(1, 0, "cannot create font finder (out of memory?)"); + StartOfLastPage = -1; + + /* + * Concatenate the named input file(s). + * We write a preamble based on the first input file. + */ + if (optind >= argc) + doit("`stdin'", stdin); + else { + for (c = optind; c < argc; c++) { + s = argv[c]; + if (*s == '-' && s[1] == 0) + doit("`stdin'", stdin); + else if ((f = fopen(s, "r")) == NULL) { + error(0, -1, "cannot read %s", s); + errs++; + } else { + doit(s, f); + (void) fclose(f); + } + } + } + if (CurrentPosition) + WritePostAmble(); + + (void) fprintf(stderr, "Wrote %d page%s, %ld bytes\n", + NumberOfOutputPages, NumberOfOutputPages == 1 ? "" : "s", + (long)CurrentPosition); + exit(errs ? 2 : 0); + /* NOTREACHED */ +} + +/* + * Handle a font definition. + */ +HandleFontDef(index) + i32 index; +{ + register struct fontinfo *fi; + register int i; + register char *s; + register i32 checksum, mag, designsize; + register int n1, n2; + struct fontinfo **p; + char *name; + int d = S_CREATE | S_EXCL; + + if ((p = (struct fontinfo **)SSearch(FontFinder, index, &d)) == NULL) + if (d & S_COLL) + error(1, 0, "font %ld already defined", (long)index); + else + error(1, 0, "cannot stash font %ld (out of memory?)", + (long)index); + + /* collect the font information */ + checksum = GetLong(inf); + mag = GetLong(inf); + designsize = GetLong(inf); + n1 = UnSign8(GetByte(inf)); + n2 = UnSign8(GetByte(inf)); + i = n1 + n2; + if ((s = malloc((unsigned)i)) == NULL) + GripeOutOfMemory(i, "font name"); + for (name = s; --i >= 0;) + *s++ = GetByte(inf); + s = name; + + /* + * We have not seen the font before in this input file, + * but we may have seen it in a previous file, so we + * must search. + */ + i = n1 + n2; + for (fi = fonts; fi != NULL; fi = fi->fi_next) { + if (fi->fi_designsize == designsize && + fi->fi_mag == mag && + fi->fi_n1 == n1 && fi->fi_n2 == n2 && + bcmp(fi->fi_name, s, i) == 0) { + if (fi->fi_checksum == 0) + fi->fi_checksum = checksum; + else if (checksum && fi->fi_checksum != checksum) { + error(0, 0, "\ +%s: Warning: font checksum mismatch for %.*s detected", + DVIFileName, i, s); + errs++; + } + *p = fi; + return; + } + } + /* it is really new; add it to the list */ + if ((fi = (struct fontinfo *)malloc(sizeof *fi)) == NULL) + GripeOutOfMemory(sizeof *fi, "font information"); + fi->fi_next = fonts; + fi->fi_index = NextOutputFontIndex++; + fi->fi_checksum = checksum; + fi->fi_mag = mag; + fi->fi_designsize = designsize; + fi->fi_n1 = n1; + fi->fi_n2 = n2; + fi->fi_name = s; + fonts = fi; + WriteFont(fi); + *p = fi; +} + +/* + * Handle a \special. + */ +HandleSpecial(c, l, p) + int c; + register int l; + register i32 p; +{ + register int i; + + putbyte(outf, c); + switch (l) { + + case DPL_UNS1: + putbyte(outf, p); + CurrentPosition += 2; + break; + + case DPL_UNS2: + PutWord(outf, p); + CurrentPosition += 3; + break; + + case DPL_UNS3: + Put3Byte(outf, p); + CurrentPosition += 4; + break; + + case DPL_SGN4: + PutLong(outf, p); + CurrentPosition += 5; + break; + + default: + panic("HandleSpecial l=%d", l); + /* NOTREACHED */ + } + CurrentPosition += p; + while (--p >= 0) { + i = getc(inf); + putbyte(outf, i); + } + if (feof(inf)) + GripeUnexpectedDVIEOF(); + if (ferror(outf)) + error(1, -1, writeerr); +} + +UseFont(index) + register i32 index; +{ + struct fontinfo *fi, **p; + int look = S_LOOKUP; + + p = (struct fontinfo **)SSearch(FontFinder, index, &look); + if (p == NULL) + error(1, 0, "%s requested font %ld without defining it", + (long)index); + if ((fi = *p) == NULL) + panic("null entry in FontFinder for %ld", (long)index); + index = fi->fi_index; + if (index < 64) { + putbyte(outf, index + DVI_FNTNUM0); + CurrentPosition++; + } else if (index < 256) { + putbyte(outf, DVI_FNT1); + putbyte(outf, index); + CurrentPosition += 2; + } else if (index < 65536) { + putbyte(outf, DVI_FNT2); + PutWord(outf, index); + CurrentPosition += 3; + } else if (index < 16777216) { + putbyte(outf, DVI_FNT3); + Put3Byte(outf, index); + CurrentPosition += 4; + } else { + putbyte(outf, DVI_FNT4); + PutLong(outf, index); + CurrentPosition += 5; + } +} + +/* + * The following table describes the length (in bytes) of each of the DVI + * commands that we can simply copy, starting with DVI_SET1 (128). + */ +char oplen[128] = { + 2, 3, 4, 5, /* DVI_SET1 .. DVI_SET4 */ + 9, /* DVI_SETRULE */ + 2, 3, 4, 5, /* DVI_PUT1 .. DVI_PUT4 */ + 9, /* DVI_PUTRULE */ + 1, /* DVI_NOP */ + 0, /* DVI_BOP */ + 0, /* DVI_EOP */ + 1, /* DVI_PUSH */ + 1, /* DVI_POP */ + 2, 3, 4, 5, /* DVI_RIGHT1 .. DVI_RIGHT4 */ + 1, /* DVI_W0 */ + 2, 3, 4, 5, /* DVI_W1 .. DVI_W4 */ + 1, /* DVI_X0 */ + 2, 3, 4, 5, /* DVI_X1 .. DVI_X4 */ + 2, 3, 4, 5, /* DVI_DOWN1 .. DVI_DOWN4 */ + 1, /* DVI_Y0 */ + 2, 3, 4, 5, /* DVI_Y1 .. DVI_Y4 */ + 1, /* DVI_Z0 */ + 2, 3, 4, 5, /* DVI_Z1 .. DVI_Z4 */ + 0, /* DVI_FNTNUM0 (171) */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 172 .. 179 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 180 .. 187 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 188 .. 195 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 196 .. 203 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 204 .. 211 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 212 .. 219 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 220 .. 227 */ + 0, 0, 0, 0, 0, 0, 0, /* 228 .. 234 */ + 0, 0, 0, 0, /* DVI_FNT1 .. DVI_FNT4 */ + 0, 0, 0, 0, /* DVI_XXX1 .. DVI_XXX4 */ + 0, 0, 0, 0, /* DVI_FNTDEF1 .. DVI_FNTDEF4 */ + 0, /* DVI_PRE */ + 0, /* DVI_POST */ + 0, /* DVI_POSTPOST */ + 0, 0, 0, 0, 0, 0, /* 250 .. 255 */ +}; + +/* + * Here we read the input DVI file and copy the pages to the output + * DVI file, renumbering fonts. We also keep track of font changes, + * handle font definitions, and perform some other housekeeping. + */ +HandleDVIFile() +{ + register int c, l; + register i32 p; + int doingpage = 0; + + /* Only way out is via "return" statement */ + for (;;) { + c = getc(inf); /* getc() returns unsigned values */ + if (DVI_IsChar(c)) { + putbyte(outf, c); + CurrentPosition++; + continue; + } + if (DVI_IsFont(c)) { /* note font change */ + UseFont((i32)(c - DVI_FNTNUM0)); + continue; + } + if (c == EOF) + GripeUnexpectedDVIEOF(); + if ((l = (oplen - 128)[c]) != 0) { /* simple copy */ + CurrentPosition += l; + putbyte(outf, c); + while (--l > 0) { + c = getc(inf); + putbyte(outf, c); + } + if (ferror(outf)) + error(1, -1, writeerr); + continue; + } + if ((l = DVI_OpLen(c)) != 0) { + /* + * Handle other generics. + * N.B.: there should only be unsigned parameters + * here (save SGN4), for commands with negative + * parameters have been taken care of above. + */ + switch (l) { + + case DPL_UNS1: + p = getc(inf); + break; + + case DPL_UNS2: + fGetWord(inf, p); + break; + + case DPL_UNS3: + fGet3Byte(inf, p); + break; + + case DPL_SGN4: + fGetLong(inf, p); + break; + + default: + panic("HandleDVIFile l=%d", l); + } + + /* + * Now that we have the parameter, perform the + * command. + */ + switch (DVI_DT(c)) { + + case DT_FNT: + UseFont(p); + continue; + + case DT_XXX: + HandleSpecial(c, l, p); + continue; + + case DT_FNTDEF: + HandleFontDef(p); + continue; + + default: + panic("HandleDVIFile DVI_DT(%d)=%d", + c, DVI_DT(c)); + } + continue; + } + + switch (c) { /* handle the few remaining cases */ + + case DVI_BOP: + if (doingpage) + GripeUnexpectedOp("BOP (during page)"); + BeginPage(); + doingpage = 1; + break; + + case DVI_EOP: + if (!doingpage) + GripeUnexpectedOp("EOP (outside page)"); + EndPage(); + doingpage = 0; + break; + + case DVI_PRE: + GripeUnexpectedOp("PRE"); + /* NOTREACHED */ + + case DVI_POST: + if (doingpage) + GripeUnexpectedOp("POST (inside page)"); + return; + + case DVI_POSTPOST: + GripeUnexpectedOp("POSTPOST"); + /* NOTREACHED */ + + default: + GripeUndefinedOp(c); + /* NOTREACHED */ + } + } +} diff --git a/dviware/dvibook/Dviconcat/dviconcat.man b/dviware/dvibook/Dviconcat/dviconcat.man new file mode 100644 index 0000000000..9267446095 --- /dev/null +++ b/dviware/dvibook/Dviconcat/dviconcat.man @@ -0,0 +1,46 @@ +.TH DVICONCAT 1 +.SH NAME +dviconcat \- concatenate DVI files +.SH SYNOPSIS +.B dviconcat +[ +.B \-m +.I magnification +] [ +.B \-o +.I outfile +] +.I "list of files" +.SH DESCRIPTION +.I Dviconcat +concatenates the named +.IR file s +(or standard input, if no file names are given) +onto the standard output +or the given +.IR outfile , +creating a new DVI file +usable by any of the TeX conversion programs, +or even by +.I dviconcat +itself. +.PP +The magnification of the output file +is the same as that of the first input file +unless the +.B \-m +option is given. +When +.I dviconcat +changes the magnification of any input file, +it prints a warning to this effect. +.SH AUTHOR +Chris Torek, University of Maryland +.SH "SEE ALSO" +dviselect(1), latex(1), tex(1) +.br +.I "MC-TeX User's Guide" +.SH BUGS +The program should be called +.IR dvicat , +but that name was taken (for the driver for the C/A/T typesetter). diff --git a/dviware/dvibook/Dviconcat/dviconcat.pdf b/dviware/dvibook/Dviconcat/dviconcat.pdf Binary files differnew file mode 100644 index 0000000000..be3dcd2a8d --- /dev/null +++ b/dviware/dvibook/Dviconcat/dviconcat.pdf diff --git a/dviware/dvibook/Dviselect/Imakefile b/dviware/dvibook/Dviselect/Imakefile new file mode 100644 index 0000000000..a82397690f --- /dev/null +++ b/dviware/dvibook/Dviselect/Imakefile @@ -0,0 +1,20 @@ +#include "../Imake.Config" + + BINDIR = $(TEX_BINDIR) + MANDIR = $(TEX_MANDIR) + + FILES = Imakefile dviselect.man $(SRCS) + + INCLUDES = -I$(LIBTEXROOT) +LOCAL_LIBRARIES = $(LIBTEX) + DEPLIBS = $(DEPLIBTEX) + SYS_LIBRARIES = -lm + +SimpleProgramTarget(dviselect) + +tar: + cd ../..;\ + for file in $(FILES); \ + do \ + tar rf $(TARFILE) $(PRODUCT)/Dviselect/$$file; \ + done diff --git a/dviware/dvibook/Dviselect/dviselect.c b/dviware/dvibook/Dviselect/dviselect.c new file mode 100644 index 0000000000..b3714b7e40 --- /dev/null +++ b/dviware/dvibook/Dviselect/dviselect.c @@ -0,0 +1,1054 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/dvi/RCS/dviselect.c,v 3.1 89/08/22 17:16:13 chris Exp $"; +#endif + +/* + * DVI page selection program + * + * Reads DVI version 2 files and selects pages, writing a new DVI + * file. The new DVI file is technically correct, though we do not + * adjust the tallest and widest page values, nor the DVI stack size. + * This is all right since the values will never become too small, + * but it might be nice to fix them up. Perhaps someday . . . . + */ + +#include "libtex/types.h" +#include "libtex/dviclass.h" +#include "libtex/dvicodes.h" +#include "libtex/error.h" +#include "libtex/fio.h" +#include "libtex/gripes.h" +#include "libtex/search.h" +#include <stdio.h> +#include <ctype.h> + +#define white(x) ((x) == ' ' || (x) == '\t' || (x) == ',') + +char *ProgName; +extern char *optarg; +extern int optind; + +/* Globals */ +char serrbuf[BUFSIZ]; /* buffer for stderr */ + +/* + * We will try to keep output lines shorter than MAXCOL characters. + */ +#define MAXCOL 75 + +/* + * We use the following structure to keep track of fonts we have seen. + * The final DVI file lists only the fonts it uses. + */ +struct fontinfo { + i32 fi_newindex; /* font number in output file */ + int fi_reallyused; /* true => used on a page we copied */ + i32 fi_checksum; /* the checksum */ + i32 fi_mag; /* the magnification */ + i32 fi_designsize; /* the design size */ + short fi_n1; /* the name header length */ + short fi_n2; /* the name body length */ + char *fi_name; /* the name itself */ +}; + +/* + * We need to remember which pages the user would like. We build a linked + * list that allows us to decide (for any given page) whether it should + * be included in the output file. Each page has ten \count variables + * associated with it. We put a bound on the values allowed for each, and + * keep a linked list of alternatives should any be outside the allowed + * range. For example, `dviselect *.3,10-15' would generate a two-element + * page list, with the first allowing any value for \count0 (and \counts 2 to + * 9) but \count1 restricted to the range 3-3, and the second restricting + * \count0 to the range 10-15 but leaving \counts 1 to 9 unrestrained. + * + * We remember whether a bound is specified, so that we need not fix + * some `large' number as a maximum value. + * + * We also allow `absolute' page references, where the first page is + * page 1, the second 2, and so forth. These are specified with an + * equal sign: `dviselect =4:10' picks up the fourth through tenth + * sequential pages, irrespective of \count values. + * + * There is also an even/odd switch (which can be applied to absolute + * or relative pages): dviselect =even, even.3, etc. + */ +enum pstype { + pt_lu, /* have both lower and upper bounds */ + pt_l, /* lower bound only */ + pt_u, /* upper bound only */ + pt_unbounded, /* any value at all */ + pt_even, /* even values only */ + pt_odd /* odd values only */ +}; +struct pagesel { + enum pstype ps_type; /* type of selector */ + i32 ps_low; /* lower bound */ + i32 ps_high; /* upper bound */ +}; +struct pagelist { + struct pagelist *pl_alt; /* next in a series of alternates */ + int pl_len; /* number of ranges to check */ + int pl_abs; /* true iff absolute page ref */ + struct pagesel pl_pages[10]; /* one for each \count variable */ +}; + +int SFlag; /* true => -s, silent operation */ + +struct search *FontFinder; /* maps from input indicies to fontinfo */ +i32 NextOutputFontIndex; /* generates output indicies */ +i32 CurrentFontIndex; /* current (old) index in input */ +i32 OutputFontIndex; /* current (new) index in ouput */ + +struct pagelist *PageList; /* the list of allowed pages */ + +char *DVIFileName; /* name of input DVI file */ +FILE *inf; /* the input file itself */ +FILE *outf; /* the output DVI file */ + +long StartOfLastPage; /* The file position just before we started + the last page (this is later written to + the output file as the previous page + pointer). */ +long CurrentPosition; /* The current position of the file */ + +int UseThisPage; /* true => current page is selected */ + +i32 InputPageNumber; /* current absolute page in old DVI file */ +int NumberOfOutputPages; /* number of pages in new DVI file */ + +i32 Numerator; /* numerator from DVI file */ +i32 Denominator; /* denominator from DVI file */ +i32 DVIMag; /* magnification from DVI file */ + +i32 Count[10]; /* the 10 \count variables */ + +/* save some string space: we use this a lot */ +char writeerr[] = "error writing DVI file"; + +char *malloc(), *realloc(); +/* + * You may get lint warnings about sprintf's return value. + * Older versions of 4BSD have `char *sprintf()'. ANSI and + * SysV use `int sprintf()'; so ignore the warnings. + */ + +/* + * Lint gets somewhat confused over putc. + */ +#ifdef lint +#undef putc +#ifdef ultrix /* grr */ +#define putc(c, f) fputc((char)(c), f) +#else +#define putc(c, f) fputc((int)(c), f) +#endif +#endif + +/* + * Return true iff the 10 \counts are one of the desired output pages. + */ +DesiredPageP() +{ + register struct pagelist *pl; + + for (pl = PageList; pl != NULL; pl = pl->pl_alt) { + register struct pagesel *ps = pl->pl_pages; + register int i; + register i32 *pagep; + + pagep = pl->pl_abs ? &InputPageNumber : &Count[0]; + for (i = 0; i < pl->pl_len; i++, ps++, pagep++) { + switch (ps->ps_type) { + + case pt_lu: /* check upper, fall through */ + if (*pagep > ps->ps_high) + goto no_good; + /* FALLTHROUGH */ + + case pt_l: /* only if not too low */ + if (*pagep < ps->ps_low) + goto no_good; + break; + + case pt_u: /* only if not too high */ + if (*pagep > ps->ps_high) + goto no_good; + break; + + case pt_unbounded: + break; + + case pt_even: /* only if even */ + if (*pagep & 1) + goto no_good; + break; + + case pt_odd: /* only if odd */ + if ((*pagep & 1) == 0) + goto no_good; + break; + } + } + return (1); /* success */ + no_good: + continue; + } + return (0); +} + +/* + * Print a message to stderr, with an optional leading space, and handling + * long line wraps. + */ +message(space, str, len) + int space; + register char *str; + register int len; +{ + static int beenhere; + static int col; + + if (!beenhere) + space = 0, beenhere++; + if (len == 0) + len = strlen(str); + col += len; + if (space) { + if (col >= MAXCOL) + (void) putc('\n', stderr), col = len; + else + (void) putc(' ', stderr), col++; + } + while (--len >= 0) + (void) putc(*str++, stderr); + (void) fflush(stderr); +} + +/* + * Start a page (process a DVI_BOP). + */ +BeginPage() +{ + register i32 *i; + + OutputFontIndex = -1; /* new page requires respecifying font */ + InputPageNumber++; /* count it */ + for (i = Count; i < &Count[10]; i++) + fGetLong(inf, *i); + (void) GetLong(inf); /* previous page pointer */ + + if ((UseThisPage = DesiredPageP()) == 0) + return; + + putbyte(outf, DVI_BOP); + for (i = Count; i < &Count[10]; i++) + PutLong(outf, *i); + PutLong(outf, StartOfLastPage); + if (ferror(outf)) + error(1, -1, writeerr); + + StartOfLastPage = CurrentPosition; + CurrentPosition += 45; /* we just wrote this much */ + + if (!SFlag) { /* write nice page usage messages */ + register int z = 0; + register int mlen = 0; + char msg[80]; + + (void) sprintf(msg, "[%ld", (long)Count[0]); + mlen = strlen(msg); + for (i = &Count[1]; i < &Count[10]; i++) { + if (*i == 0) { + z++; + continue; + } + while (--z >= 0) + msg[mlen++] = '.', msg[mlen++] = '0'; + z = 0; + (void) sprintf(msg + mlen, ".%ld", (long)*i); + mlen += strlen(msg + mlen); + } + message(1, msg, mlen); + } +} + +/* + * End a page (process a DVI_EOP). + */ +EndPage() +{ + + if (!UseThisPage) + return; + if (!SFlag) + message(0, "]", 1); + putbyte(outf, DVI_EOP); + if (ferror(outf)) + error(1, -1, writeerr); + CurrentPosition++; + NumberOfOutputPages++; +} + +/* + * For each of the fonts used in the new DVI file, write out a definition. + */ +/* ARGSUSED */ +void +PostAmbleFontEnumerator(addr, key) + char *addr; + i32 key; +{ + + if (((struct fontinfo *)addr)->fi_reallyused) + WriteFont((struct fontinfo *)addr); +} + +HandlePostAmble() +{ + register i32 c; + + (void) GetLong(inf); /* previous page pointer */ + if (GetLong(inf) != Numerator) + GripeMismatchedValue("numerator"); + if (GetLong(inf) != Denominator) + GripeMismatchedValue("denominator"); + if (GetLong(inf) != DVIMag) + GripeMismatchedValue("\\magnification"); + + putbyte(outf, DVI_POST); + PutLong(outf, StartOfLastPage); + PutLong(outf, Numerator); + PutLong(outf, Denominator); + PutLong(outf, DVIMag); + c = GetLong(inf); + PutLong(outf, c); /* tallest page height */ + c = GetLong(inf); + PutLong(outf, c); /* widest page width */ + c = GetWord(inf); + PutWord(outf, c); /* DVI stack size */ + PutWord(outf, NumberOfOutputPages); + StartOfLastPage = CurrentPosition; /* point at post */ + CurrentPosition += 29; /* count all those `put's */ +#ifdef notdef + (void) GetWord(inf); /* skip original number of pages */ +#endif + + /* + * just ignore all the incoming font definitions; we are done with + * input file + */ + + /* + * run through the FontFinder table and dump definitions for the + * fonts we have used. + */ + SEnumerate(FontFinder, PostAmbleFontEnumerator); + + putbyte(outf, DVI_POSTPOST); + PutLong(outf, StartOfLastPage); /* actually start of postamble */ + putbyte(outf, DVI_VERSION); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + CurrentPosition += 10; + while (CurrentPosition & 3) { + putbyte(outf, DVI_FILLER); + CurrentPosition++; + } + if (ferror(outf)) + error(1, -1, writeerr); +} + +/* + * Write a font definition to the output file + */ +WriteFont(fi) + register struct fontinfo *fi; +{ + register int l; + register char *s; + + if (fi->fi_newindex < 256) { + putbyte(outf, DVI_FNTDEF1); + putbyte(outf, fi->fi_newindex); + CurrentPosition += 2; + } else if (fi->fi_newindex < 65536) { + putbyte(outf, DVI_FNTDEF2); + PutWord(outf, fi->fi_newindex); + CurrentPosition += 3; + } else if (fi->fi_newindex < 16777216) { + putbyte(outf, DVI_FNTDEF3); + Put3Byte(outf, fi->fi_newindex); + CurrentPosition += 4; + } else { + putbyte(outf, DVI_FNTDEF4); + PutLong(outf, fi->fi_newindex); + CurrentPosition += 5; + } + PutLong(outf, fi->fi_checksum); + PutLong(outf, fi->fi_mag); + PutLong(outf, fi->fi_designsize); + putbyte(outf, fi->fi_n1); + putbyte(outf, fi->fi_n2); + l = fi->fi_n1 + fi->fi_n2; + CurrentPosition += 14 + l; + s = fi->fi_name; + while (--l >= 0) + putbyte(outf, *s++); +} + +/* + * Handle the preamble. Someday we should update the comment field. + */ +HandlePreAmble() +{ + register int n, c; + + c = getc(inf); + if (c == EOF) + GripeUnexpectedDVIEOF(); + if (c != DVI_PRE) + GripeMissingOp("PRE"); + if (getc(inf) != DVI_VERSION) + error(1, 0, "%s is not a DVI version %d file", + DVIFileName, DVI_VERSION); + Numerator = GetLong(inf); + Denominator = GetLong(inf); + DVIMag = GetLong(inf); + putbyte(outf, DVI_PRE); + putbyte(outf, DVI_VERSION); + PutLong(outf, Numerator); + PutLong(outf, Denominator); + PutLong(outf, DVIMag); + + n = UnSign8(GetByte(inf)); + CurrentPosition = 15 + n; /* well, almost */ + putbyte(outf, n); + while (--n >= 0) { + c = GetByte(inf); + putbyte(outf, c); + } +} + +main(argc, argv) + int argc; + register char **argv; +{ + register int c; + register char *s; + char *outname = NULL; + + ProgName = *argv; + setbuf(stderr, serrbuf); + + while ((c = getopt(argc, argv, "i:o:s")) != EOF) { + switch (c) { + + case 's': /* silent */ + SFlag++; + break; + + case 'i': + if (DVIFileName != NULL) + goto usage; + DVIFileName = optarg; + break; + + case 'o': + if (outname != NULL) + goto usage; + outname = optarg; + break; + + case '?': +usage: + (void) fprintf(stderr, "\ +Usage: %s [-s] [-i infile] [-o outfile] pages [...] [infile [outfile]]\n", + ProgName); + (void) fflush(stderr); + exit(1); + } + } + + while (optind < argc) { + s = argv[optind++]; + c = *s; + if (!isalpha(c) && c != '/' || + (c == 'e' || c == 'o') && evenodd(s)) { + if (ParsePages(s)) + goto usage; + } else if (DVIFileName == NULL) + DVIFileName = s; + else if (outname == NULL) + outname = s; + else + goto usage; + } + if (PageList == NULL) + goto usage; + if (DVIFileName == NULL) { + DVIFileName = "`stdin'"; + inf = stdin; + } else if ((inf = fopen(DVIFileName, "r")) == 0) + error(1, -1, "cannot read %s", DVIFileName); + if (outname == NULL) + outf = stdout; + else if ((outf = fopen(outname, "w")) == 0) + error(1, -1, "cannot write %s", outname); + + if ((FontFinder = SCreate(sizeof(struct fontinfo))) == 0) + error(1, 0, "cannot create font finder (out of memory?)"); + + StartOfLastPage = -1; + HandlePreAmble(); + HandleDVIFile(); + HandlePostAmble(); + if (!SFlag) + (void) fprintf(stderr, "\nWrote %d page%s, %ld bytes\n", + NumberOfOutputPages, NumberOfOutputPages == 1 ? "" : "s", + (long)CurrentPosition); + exit(0); + /* NOTREACHED */ +} + +struct pagelist * +InstallPL(ps, n, absolute) + register struct pagesel *ps; + register int n; + int absolute; +{ + register struct pagelist *pl; + + pl = (struct pagelist *)malloc(sizeof *pl); + if (pl == NULL) + GripeOutOfMemory(sizeof *pl, "page list"); + pl->pl_alt = PageList; + PageList = pl; + pl->pl_len = n; + while (--n >= 0) + pl->pl_pages[n] = ps[n]; + pl->pl_abs = absolute; +} + +/* + * Return true iff the string `s' is the word `even' or `odd', + * followed by `.' or `white' characters. + */ +int +evenodd(s) + char *s; +{ + register int c; + + if (strncmp(s, "even", 4) == 0) + c = s[4]; + else if (strncmp(s, "odd", 3) == 0) + c = s[3]; + else + return (0); + if (c == 0 || c == '.' || white(c)) + return (1); + return (0); +} + +/* + * Parse a string representing a list of pages. Return 0 iff ok. As a + * side effect, the page selection(s) is (are) prepended to PageList. + */ +ParsePages(s) + register char *s; +{ + register struct pagesel *ps; + register int c; /* current character */ + register i32 n; /* current numeric value */ + register int innumber; /* true => gathering a number */ + int i; /* next index in page select list */ + int range; /* true => saw a range indicator */ + int negative; /* true => number being built is negative */ + int absolute; /* true => absolute, not \count */ + int lb, ub, even, odd; /* flags for lower,upper,even,odd */ + struct pagesel pagesel[10]; + + range = 0; + innumber = 0; + absolute = 0; + i = 0; + ps = pagesel; + lb = ub = even = odd = 0; + /* + * Talk about ad hoc! (Not to mention convoluted.) + */ + for (;;) { + c = *s++; + if (i == 0 && !innumber && !range) { + /* nothing special going on */ + if (c == 0) + return 0; + if (white(c)) + continue; + } + if ((c == 'e' || c == 'o') && evenodd(s - 1)) { + if (innumber || range) + return (-1); + if (c == 'e') { + s += 3; + even = 1; + } else { + s += 2; + odd = 1; + } + c = *s++; + goto finishnum; + } + if (c == '_') { + /* kludge: should be '-' for negatives */ + if (innumber || absolute) + return (-1); + innumber = 1; + negative = 1; + n = 0; + continue; + } + if (c == '=') { + /* absolute page */ + if (innumber || range || i > 0) + return (-1); + absolute = 1; + negative = 0; + n = 0; + continue; + } + if (isdigit(c)) { + /* accumulate numeric value */ + if (!innumber) { + innumber = 1; + negative = 0; + n = c - '0'; + continue; + } + n *= 10; + n += negative ? '0' - c : c - '0'; + continue; + } + if (c == '-' || c == ':') { + /* here is a range */ + if (range) + return (-1); + if (innumber) { /* have a lower bound */ + ps->ps_low = n; + lb = 1; + } + range = 1; + innumber = 0; + continue; + } + if (c == '*') { + /* no lower bound, no upper bound */ + c = *s++; + if (innumber || range || (c && c != '.' && !white(c))) + return (-1); + goto finishnum; + } + if (c == 0 || c == '.' || white(c)) { + /* end of this range */ + if (innumber) { /* have an upper bound */ + ps->ps_high = n; + ub = 1; + if (!range) { + /* no range => lower bound == upper */ + ps->ps_low = ps->ps_high; + lb = 1; + } + } +finishnum: + if (even) + ps->ps_type = pt_even; + else if (odd) + ps->ps_type = pt_odd; + else if (lb) + ps->ps_type = ub ? pt_lu : pt_l; + else + ps->ps_type = ub ? pt_u : pt_unbounded; + if (c == '.') { + if (absolute) + return (-1); + if (++i >= 10) /* too many specifiers */ + return (-1); + ps++; + } else { + InstallPL(pagesel, i + 1, absolute); + ps = pagesel; + i = 0; + absolute = 0; + } + if (c == 0) + return (0); + range = 0; + innumber = 0; + lb = ub = even = odd = 0; + continue; + } + /* illegal character */ + return (-1); + } +} + +/* + * Handle a font definition. + */ +HandleFontDef(index) + i32 index; +{ + register struct fontinfo *fi; + register int i; + register char *s; + int def = S_CREATE | S_EXCL; + + if ((fi = (struct fontinfo *)SSearch(FontFinder, index, &def)) == 0) + if (def & S_COLL) + error(1, 0, "font %ld already defined", (long)index); + else + error(1, 0, "cannot stash font %ld (out of memory?)", + (long)index); + fi->fi_reallyused = 0; + fi->fi_checksum = GetLong(inf); + fi->fi_mag = GetLong(inf); + fi->fi_designsize = GetLong(inf); + fi->fi_n1 = UnSign8(GetByte(inf)); + fi->fi_n2 = UnSign8(GetByte(inf)); + i = fi->fi_n1 + fi->fi_n2; + if ((s = malloc((unsigned)i)) == 0) + GripeOutOfMemory(i, "font name"); + fi->fi_name = s; + while (--i >= 0) + *s++ = GetByte(inf); +} + +/* + * Handle a \special. + */ +HandleSpecial(c, l, p) + int c; + register int l; + register i32 p; +{ + register int i; + + if (UseThisPage) { + putbyte(outf, c); + switch (l) { + + case DPL_UNS1: + putbyte(outf, p); + CurrentPosition += 2; + break; + + case DPL_UNS2: + PutWord(outf, p); + CurrentPosition += 3; + break; + + case DPL_UNS3: + Put3Byte(outf, p); + CurrentPosition += 4; + break; + + case DPL_SGN4: + PutLong(outf, p); + CurrentPosition += 5; + break; + + default: + panic("HandleSpecial l=%d", l); + /* NOTREACHED */ + } + CurrentPosition += p; + while (--p >= 0) { + i = getc(inf); + putbyte(outf, i); + } + if (feof(inf)) + GripeUnexpectedDVIEOF(); + if (ferror(outf)) + error(1, -1, writeerr); + } else + while (--p >= 0) + (void) getc(inf); +} + +ReallyUseFont() +{ + register struct fontinfo *fi; + int look = S_LOOKUP; + + fi = (struct fontinfo *)SSearch(FontFinder, CurrentFontIndex, &look); + if (fi == NULL) + error(1, 0, "DVI file requested font %ld without defining it", + (long)CurrentFontIndex); + if (fi->fi_reallyused == 0) { + fi->fi_reallyused++; + fi->fi_newindex = NextOutputFontIndex++; + WriteFont(fi); + } + if (fi->fi_newindex != OutputFontIndex) { + PutFontSelector(fi->fi_newindex); + OutputFontIndex = fi->fi_newindex; + } +} + +/* + * Write a font selection command to the output file + */ +PutFontSelector(index) + i32 index; +{ + + if (index < 64) { + putbyte(outf, index + DVI_FNTNUM0); + CurrentPosition++; + } else if (index < 256) { + putbyte(outf, DVI_FNT1); + putbyte(outf, index); + CurrentPosition += 2; + } else if (index < 65536) { + putbyte(outf, DVI_FNT2); + PutWord(outf, index); + CurrentPosition += 3; + } else if (index < 16777216) { + putbyte(outf, DVI_FNT3); + Put3Byte(outf, index); + CurrentPosition += 4; + } else { + putbyte(outf, DVI_FNT4); + PutLong(outf, index); + CurrentPosition += 5; + } +} + +/* + * The following table describes the length (in bytes) of each of the DVI + * commands that we can simply copy, starting with DVI_SET1 (128). + */ +char oplen[128] = { + 0, 0, 0, 0, /* DVI_SET1 .. DVI_SET4 */ + 9, /* DVI_SETRULE */ + 0, 0, 0, 0, /* DVI_PUT1 .. DVI_PUT4 */ + 9, /* DVI_PUTRULE */ + 1, /* DVI_NOP */ + 0, /* DVI_BOP */ + 0, /* DVI_EOP */ + 1, /* DVI_PUSH */ + 1, /* DVI_POP */ + 2, 3, 4, 5, /* DVI_RIGHT1 .. DVI_RIGHT4 */ + 1, /* DVI_W0 */ + 2, 3, 4, 5, /* DVI_W1 .. DVI_W4 */ + 1, /* DVI_X0 */ + 2, 3, 4, 5, /* DVI_X1 .. DVI_X4 */ + 2, 3, 4, 5, /* DVI_DOWN1 .. DVI_DOWN4 */ + 1, /* DVI_Y0 */ + 2, 3, 4, 5, /* DVI_Y1 .. DVI_Y4 */ + 1, /* DVI_Z0 */ + 2, 3, 4, 5, /* DVI_Z1 .. DVI_Z4 */ + 0, /* DVI_FNTNUM0 (171) */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 172 .. 179 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 180 .. 187 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 188 .. 195 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 196 .. 203 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 204 .. 211 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 212 .. 219 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 220 .. 227 */ + 0, 0, 0, 0, 0, 0, 0, /* 228 .. 234 */ + 0, 0, 0, 0, /* DVI_FNT1 .. DVI_FNT4 */ + 0, 0, 0, 0, /* DVI_XXX1 .. DVI_XXX4 */ + 0, 0, 0, 0, /* DVI_FNTDEF1 .. DVI_FNTDEF4 */ + 0, /* DVI_PRE */ + 0, /* DVI_POST */ + 0, /* DVI_POSTPOST */ + 0, 0, 0, 0, 0, 0, /* 250 .. 255 */ +}; + +/* + * Here we read the input DVI file and write relevant pages to the + * output DVI file. We also keep track of font changes, handle font + * definitions, and perform some other housekeeping. + */ +HandleDVIFile() +{ + register int c, l; + register i32 p; + register int CurrentFontOK = 0; + int doingpage = 0; + + /* Only way out is via "return" statement */ + for (;;) { + c = getc(inf); /* getc() returns unsigned values */ + if (DVI_IsChar(c)) { + /* + * Copy chars, note font usage, but ignore if + * page is not interesting. + */ + if (!UseThisPage) + continue; + if (!CurrentFontOK) { + ReallyUseFont(); + CurrentFontOK++; + } + putbyte(outf, c); + CurrentPosition++; + continue; + } + if (DVI_IsFont(c)) { /* note font change */ + CurrentFontIndex = c - DVI_FNTNUM0; + CurrentFontOK = 0; + continue; + } + if (c == EOF) + GripeUnexpectedDVIEOF(); + if ((l = (oplen - 128)[c]) != 0) { /* simple copy */ + if (!UseThisPage) { + while (--l > 0) + (void) getc(inf); + continue; + } + CurrentPosition += l; + putbyte(outf, c); + while (--l > 0) { + c = getc(inf); + putbyte(outf, c); + } + if (ferror(outf)) + error(1, -1, writeerr); + continue; + } + if ((l = DVI_OpLen(c)) != 0) { + /* + * Handle other generics. + * N.B.: there should only be unsigned parameters + * here (save SGN4), for commands with negative + * parameters have been taken care of above. + */ + switch (l) { + + case DPL_UNS1: + p = getc(inf); + break; + + case DPL_UNS2: + fGetWord(inf, p); + break; + + case DPL_UNS3: + fGet3Byte(inf, p); + break; + + case DPL_SGN4: + fGetLong(inf, p); + break; + + default: + panic("HandleDVIFile l=%d", l); + } + + /* + * Now that we have the parameter, perform the + * command. + */ + switch (DVI_DT(c)) { + + case DT_SET: + case DT_PUT: + if (!UseThisPage) + continue; + if (!CurrentFontOK) { + ReallyUseFont(); + CurrentFontOK++; + } + putbyte(outf, c); + switch (l) { + + case DPL_UNS1: + putbyte(outf, p); + CurrentPosition += 2; + continue; + + case DPL_UNS2: + PutWord(outf, p); + CurrentPosition += 3; + continue; + + case DPL_UNS3: + Put3Byte(outf, p); + CurrentPosition += 4; + continue; + + case DPL_SGN4: + PutLong(outf, p); + CurrentPosition += 5; + continue; + } + + case DT_FNT: + CurrentFontIndex = p; + CurrentFontOK = 0; + continue; + + case DT_XXX: + HandleSpecial(c, l, p); + continue; + + case DT_FNTDEF: + HandleFontDef(p); + continue; + + default: + panic("HandleDVIFile DVI_DT(%d)=%d", + c, DVI_DT(c)); + } + continue; + } + + switch (c) { /* handle the few remaining cases */ + + case DVI_BOP: + if (doingpage) + GripeUnexpectedOp("BOP (during page)"); + BeginPage(); + doingpage = 1; + CurrentFontOK = 0; + break; + + case DVI_EOP: + if (!doingpage) + GripeUnexpectedOp("EOP (outside page)"); + EndPage(); + doingpage = 0; + break; + + case DVI_PRE: + GripeUnexpectedOp("PRE"); + /* NOTREACHED */ + + case DVI_POST: + if (doingpage) + GripeUnexpectedOp("POST (inside page)"); + return; + + case DVI_POSTPOST: + GripeUnexpectedOp("POSTPOST"); + /* NOTREACHED */ + + default: + GripeUndefinedOp(c); + /* NOTREACHED */ + } + } +} diff --git a/dviware/dvibook/Dviselect/dviselect.man b/dviware/dvibook/Dviselect/dviselect.man new file mode 100644 index 0000000000..814f102308 --- /dev/null +++ b/dviware/dvibook/Dviselect/dviselect.man @@ -0,0 +1,129 @@ +.TH DVISELECT 1 +.SH NAME +dviselect \- extract pages from DVI files +.SH SYNOPSIS +.B dviselect +[ +.B \-s +] [ +.B \-i +.I infile +] [ +.B \-o +.I outfile +] +.I "list of pages" +[ +.I infile +[ +.I outfile +] ] +.SH DESCRIPTION +.I Dviselect +selects pages from a DVI file produced by TeX, +creating a new DVI file +usable by any of the TeX conversion programs, +or even by +.I dviselect +itself. +.PP +A +.I range +is a string of the form +.IR even , +.IR odd , +or +.I first:last +where both +.I first +and +.I last +are optional numeric strings, with negative numbers indicated by +a leading underscore character ``_''. If both +.I first +and +.I last +are omitted, the colon may also be omitted, or may be replaced +with an asterisk ``*''. A +.I page range +is a list of ranges separated by periods. A +.I "list of pages" +is described by a set of page ranges separated by commas and/or white space. +.PP +.I Dviselect +actually looks at the ten +.I count +variables that TeX writes; the first of these (\ecount0) is the +page number, with \ecount1 through \ecount9 having varied uses +depending on which macro packages are in use. (Typically \ecount1 +might be a chapter or section number.) A page is included in +.IR dviselect 's +output if all its \ecount values match any one of the ranges +listed on the command line. For example, the command +``dviselect *.1,35:'' +might select everything in chapter 1, as well as pages 35 and up. +``dviselect 10:30'' would select pages 10 through 30 (inclusive). +``:43'' means everything up to and including page 43 (including +negative-numbered pages). +To get all even-numbered pages, use ``even''; +to get all odd-numbered pages, use ``odd''. +If a Table of Contents has negative page numbers, ``:_1'' will select it. +Note that ``*'' must be quoted from the shell; +the empty string is more convenient to use, if harder to read. +.PP +Instead of \ecount values, +.I dviselect +can also select by ``absolute page number'', where the first page +is page 1, the second page 2, and so forth. Absolute page numbers +are indicated by a leading equal sign ``=''. Ranges of absolute +pages are also allowed: ``dviselect =3:7'' will extract the third +through seventh pages. Dot separators are not legal in absolute +ranges, and there are no negative absolute page numbers. +Even/odd specifiers, however, are legal; +``dviselect =even'' selects every other page, +starting with the second. +.PP +More precisely, an asterisk or an empty string implies no limit; +an equal sign means absolute page number rather than \ecounts; +a leading colon means everything up to and including the given page; a +trailing colon means everything from the given page on; +the word ``even'' means only even values shall be accepted; +the word ``odd'' means only odd values shall be accepted; and +a period indicates that the next \ecount should be examined. +If fewer than 10 ranges are specified, the remaining \ecounts +are left unrestricted (that is, ``1:5'' and ``1:5.*'' are equivalent). +A single number \fIn\fP is treated as if it were the range \fIn:n\fP. +An arbitrary number of page selectors may be given, separated by commas +or whitespace; a page is selected if any of the selectors matches +its \ecounts or absolute page number. +.PP +Dviselect normally prints the page numbers of the pages selected; the +.I \-s +option suppresses this. +.SH AUTHOR +Chris Torek, University of Maryland +.SH "SEE ALSO" +dviconcat(1), latex(1), tex(1) +.br +.I "MC-TeX User's Guide" +.br +.I "The TeXbook" +.SH BUGS +A leading ``-'' ought to be allowed for negative numbers, but it +is currently used as a synonym for ``:'', for backwards compatibility. +.PP +Section or subsection selection will sometimes fail, for the DVI +file lists only the \ecount values that were active when the page +ended. Clever macro packages can alleviate this by making use of +other ``free'' \ecount registers. Chapters normally begin on new +pages, and do not suffer from this particular problem. +.PP +The heuristic that decides which arguments are page selectors +and which are file names is often wrong. +Using shell redirection or the \-i and \-o options is safest. +.PP +.I Dviselect +does not adjust the parameters in the postamble; however, since these +values are normally used only to size certain structures in the output +conversion programs, and the parameters never need to be adjusted upward, +this has not proven to be a problem. diff --git a/dviware/dvibook/Dvitodvi/Imakefile b/dviware/dvibook/Dvitodvi/Imakefile new file mode 100644 index 0000000000..b75807a7ff --- /dev/null +++ b/dviware/dvibook/Dvitodvi/Imakefile @@ -0,0 +1,28 @@ +OBJS =dvitodvi.o +SRCS =dvitodvi.c + + LIBTEXROOT= ../ + LIBTEXSRC = ../libtex + LIBTEX = $(LIBTEXSRC)/libtex.a + DEPLIBTEX = $(LIBTEX) + DEPLIBS = $(DEPLIBTEX) XawClientDepLibs +LOCAL_LIBRARIES = $(LIBTEX) + + DEFINES = -I$(LIBTEXROOT) + +FILES =Imakefile dvitodvi.man Makefile.raw $(SRCS) + +all: dvitodvi + +NormalProgramTarget(dvitodvi,$(OBJS),$(DEPLIBS),$(LOCAL_LIBRARIES),$(SYSLIBS)) +InstallProgram(dvitodvi, $(BINDIR)) +InstallManPage(dvitodvi,$(MANDIR)) +DependTarget() +TagsTarget() + +tar: + cd ../..;\ + for file in $(FILES); \ + do \ + tar rf $(TARFILE) $(PRODUCT)/Dvitodvi/$$file; \ + done diff --git a/dviware/dvibook/Dvitodvi/Makefile b/dviware/dvibook/Dvitodvi/Makefile new file mode 100644 index 0000000000..4753afc223 --- /dev/null +++ b/dviware/dvibook/Dvitodvi/Makefile @@ -0,0 +1,356 @@ +# Makefile generated by imake - do not edit! +# $XConsortium: imake.c,v 1.65 91/07/25 17:50:17 rws Exp $ +# +# The cpp used on this machine replaces all newlines and multiple tabs and +# spaces in a macro expansion with a single space. Imake tries to compensate +# for this, but is not always successful. +# + +# ------------------------------------------------------------------------- +# Makefile generated from "Imake.tmpl" and <Imakefile> +# $XConsortium: Imake.tmpl,v 1.139 91/09/16 08:52:48 rws Exp $ +# +# Platform-specific parameters may be set in the appropriate <vendor>.cf +# configuration files. Site-specific parameters should be set in the file +# site.def. Full rebuilds are recommended if any parameters are changed. +# +# If your C preprocessor does not define any unique symbols, you will need +# to set BOOTSTRAPCFLAGS when rebuilding imake (usually when doing +# "make World" the first time). +# + +# ------------------------------------------------------------------------- +# site-specific configuration parameters that need to come before +# the platform-specific parameters - edit site.def to change + +# site: $XConsortium: site.def,v 1.2 91/07/30 20:26:44 rws Exp $ + +# ------------------------------------------------------------------------- +# platform-specific configuration parameters - edit sun.cf to change + +# platform: $XConsortium: sun.cf,v 1.69 91/09/09 18:23:59 rws Exp $ + +# operating system: SunOS 4.1.1 + +# $XConsortium: sunLib.rules,v 1.6 91/03/24 17:55:58 rws Exp $ + +# ------------------------------------------------------------------------- +# site-specific configuration parameters that go after +# the platform-specific parameters - edit site.def to change + +# site: $XConsortium: site.def,v 1.2 91/07/30 20:26:44 rws Exp $ + + SHELL = /bin/sh + + TOP = . + CURRENT_DIR = . + + AR = ar clq + BOOTSTRAPCFLAGS = + CC = cc + AS = as + + COMPRESS = compress + CPP = /lib/cpp $(STD_CPP_DEFINES) + PREPROCESSCMD = cc -E $(STD_CPP_DEFINES) + INSTALL = /usr/local/bin/install + LD = ld + LINT = lint + LINTLIBFLAG = -C + LINTOPTS = -axz + LN = ln -s + MAKE = make + MV = mv + CP = cp + + RANLIB = ranlib + RANLIBINSTFLAGS = + + RM = rm -f + TROFF = psroff + MSMACROS = -ms + TBL = tbl + EQN = eqn + STD_INCLUDES = + STD_CPP_DEFINES = + STD_DEFINES = + EXTRA_LOAD_FLAGS = + EXTRA_LIBRARIES = + TAGS = ctags + + SHAREDCODEDEF = -DSHAREDCODE + SHLIBDEF = -DSUNSHLIB + + PROTO_DEFINES = + + INSTPGMFLAGS = -s + + INSTBINFLAGS = -m 0755 + INSTUIDFLAGS = -m 4755 + INSTLIBFLAGS = -m 0644 + INSTINCFLAGS = -m 0444 + INSTMANFLAGS = -m 0444 + INSTDATFLAGS = -m 0444 + INSTKMEMFLAGS = -m 4755 + + TOP_INCLUDES = -I$(INCROOT) + + CDEBUGFLAGS = -O + CCOPTIONS = -pipe -temp=. + + ALLINCLUDES = $(INCLUDES) $(EXTRA_INCLUDES) $(TOP_INCLUDES) $(STD_INCLUDES) + ALLDEFINES = $(ALLINCLUDES) $(STD_DEFINES) $(EXTRA_DEFINES) $(PROTO_DEFINES) $(DEFINES) + CFLAGS = $(CDEBUGFLAGS) $(CCOPTIONS) $(ALLDEFINES) + LINTFLAGS = $(LINTOPTS) -DLINT $(ALLDEFINES) + + LDLIBS = $(SYS_LIBRARIES) $(EXTRA_LIBRARIES) + + LDOPTIONS = $(CDEBUGFLAGS) $(CCOPTIONS) $(LOCAL_LDFLAGS) -L$(USRLIBDIR) + + LDCOMBINEFLAGS = -X -r + DEPENDFLAGS = + + MACROFILE = sun.cf + RM_CMD = $(RM) *.CKP *.ln *.BAK *.bak *.o core errs ,* *~ *.a .emacs_* tags TAGS make.log MakeOut + + IMAKE_DEFINES = + + IRULESRC = $(CONFIGDIR) + IMAKE_CMD = $(IMAKE) -DUseInstalled -I$(IRULESRC) $(IMAKE_DEFINES) + + ICONFIGFILES = $(IRULESRC)/Imake.tmpl $(IRULESRC)/Imake.rules \ + $(IRULESRC)/Project.tmpl $(IRULESRC)/site.def \ + $(IRULESRC)/$(MACROFILE) $(EXTRA_ICONFIGFILES) + +# ------------------------------------------------------------------------- +# X Window System Build Parameters +# $XConsortium: Project.tmpl,v 1.138 91/09/10 09:02:12 rws Exp $ + +# ------------------------------------------------------------------------- +# X Window System make variables; this need to be coordinated with rules + + PATHSEP = / + USRLIBDIR = /usr/local/lib/X11R5 + BINDIR = /usr/local/bin/X11R5 + INCROOT = /usr/local/share/X11R5/include + BUILDINCROOT = $(TOP) + BUILDINCDIR = $(BUILDINCROOT)/X11 + BUILDINCTOP = .. + INCDIR = $(INCROOT)/X11 + ADMDIR = /usr/adm + LIBDIR = /usr/local/lib/X11R5 + CONFIGDIR = $(LIBDIR)/config + LINTLIBDIR = $(USRLIBDIR)/lint + + FONTDIR = $(LIBDIR)/fonts + XINITDIR = $(LIBDIR)/xinit + XDMDIR = $(LIBDIR)/xdm + TWMDIR = $(LIBDIR)/twm + MANPATH = /usr/local/share/man + MANSOURCEPATH = $(MANPATH)/man + MANSUFFIX = n + LIBMANSUFFIX = 3 + MANDIR = $(MANSOURCEPATH)$(MANSUFFIX) + LIBMANDIR = $(MANSOURCEPATH)$(LIBMANSUFFIX) + NLSDIR = $(LIBDIR)/nls + PEXAPIDIR = $(LIBDIR)/PEX + XAPPLOADDIR = $(LIBDIR)/app-defaults + FONTCFLAGS = -t + + INSTAPPFLAGS = $(INSTDATFLAGS) + + IMAKE = imake + DEPEND = makedepend + RGB = rgb + + FONTC = bdftopcf + + MKFONTDIR = mkfontdir + MKDIRHIER = /bin/sh $(BINDIR)/mkdirhier + + CONFIGSRC = $(TOP)/config + DOCUTILSRC = $(TOP)/doc/util + CLIENTSRC = $(TOP)/clients + DEMOSRC = $(TOP)/demos + LIBSRC = $(TOP)/lib + FONTSRC = $(TOP)/fonts + INCLUDESRC = $(TOP)/X11 + SERVERSRC = $(TOP)/server + UTILSRC = $(TOP)/util + SCRIPTSRC = $(UTILSRC)/scripts + EXAMPLESRC = $(TOP)/examples + CONTRIBSRC = $(TOP)/../contrib + DOCSRC = $(TOP)/doc + RGBSRC = $(TOP)/rgb + DEPENDSRC = $(UTILSRC)/makedepend + IMAKESRC = $(CONFIGSRC) + XAUTHSRC = $(LIBSRC)/Xau + XLIBSRC = $(LIBSRC)/X + XMUSRC = $(LIBSRC)/Xmu + TOOLKITSRC = $(LIBSRC)/Xt + AWIDGETSRC = $(LIBSRC)/Xaw + OLDXLIBSRC = $(LIBSRC)/oldX + XDMCPLIBSRC = $(LIBSRC)/Xdmcp + BDFTOSNFSRC = $(FONTSRC)/bdftosnf + BDFTOSNFSRC = $(FONTSRC)/clients/bdftosnf + BDFTOPCFSRC = $(FONTSRC)/clients/bdftopcf + MKFONTDIRSRC = $(FONTSRC)/clients/mkfontdir + FSLIBSRC = $(FONTSRC)/lib/fs + FONTSERVERSRC = $(FONTSRC)/server + EXTENSIONSRC = $(TOP)/extensions + XILIBSRC = $(EXTENSIONSRC)/lib/xinput + PHIGSLIBSRC = $(EXTENSIONSRC)/lib/PEX + +# $XConsortium: sunLib.tmpl,v 1.11 91/07/31 11:32:08 rws Exp $ + +SHLIBLDFLAGS = -assert pure-text +PICFLAGS = -pic + + DEPEXTENSIONLIB = + EXTENSIONLIB = -lXext + + DEPXLIB = $(DEPEXTENSIONLIB) + XLIB = $(EXTENSIONLIB) -lX11 + + DEPXMULIB = $(USRLIBDIR)/libXmu.sa.$(SOXMUREV) + XMULIB = -lXmu + + DEPOLDXLIB = + OLDXLIB = -loldX + + DEPXTOOLLIB = $(USRLIBDIR)/libXt.sa.$(SOXTREV) + XTOOLLIB = -lXt + + DEPXAWLIB = $(USRLIBDIR)/libXaw.sa.$(SOXAWREV) + XAWLIB = -lXaw + + DEPXILIB = + XILIB = -lXi + + SOXLIBREV = 4.10 + SOXTREV = 4.10 + SOXAWREV = 5.0 + SOOLDXREV = 4.10 + SOXMUREV = 4.10 + SOXEXTREV = 4.10 + SOXINPUTREV = 4.10 + + DEPXAUTHLIB = $(USRLIBDIR)/libXau.a + XAUTHLIB = -lXau + DEPXDMCPLIB = $(USRLIBDIR)/libXdmcp.a + XDMCPLIB = -lXdmcp + + DEPPHIGSLIB = $(USRLIBDIR)/libphigs.a + PHIGSLIB = -lphigs + + DEPXBSDLIB = $(USRLIBDIR)/libXbsd.a + XBSDLIB = -lXbsd + + LINTEXTENSIONLIB = $(LINTLIBDIR)/llib-lXext.ln + LINTXLIB = $(LINTLIBDIR)/llib-lX11.ln + LINTXMU = $(LINTLIBDIR)/llib-lXmu.ln + LINTXTOOL = $(LINTLIBDIR)/llib-lXt.ln + LINTXAW = $(LINTLIBDIR)/llib-lXaw.ln + LINTXI = $(LINTLIBDIR)/llib-lXi.ln + LINTPHIGS = $(LINTLIBDIR)/llib-lphigs.ln + + DEPLIBS = $(DEPXAWLIB) $(DEPXMULIB) $(DEPXTOOLLIB) $(DEPXLIB) + + DEPLIBS1 = $(DEPLIBS) + DEPLIBS2 = $(DEPLIBS) + DEPLIBS3 = $(DEPLIBS) + +# ------------------------------------------------------------------------- +# Imake rules for building libraries, programs, scripts, and data files +# rules: $XConsortium: Imake.rules,v 1.123 91/09/16 20:12:16 rws Exp $ + +# ------------------------------------------------------------------------- +# start of Imakefile + +OBJS =dvitodvi.o +SRCS =dvitodvi.c + + LIBTEXROOT= ../ + LIBTEXSRC = ../libtex + LIBTEX = $(LIBTEXSRC)/libtex.a + DEPLIBTEX = $(LIBTEX) + DEPLIBS = $(DEPLIBTEX) $(DEPXAWLIB) $(DEPXMULIB) $(DEPXTOOLLIB) $(DEPXLIB) +LOCAL_LIBRARIES = $(LIBTEX) + + DEFINES = -I$(LIBTEXROOT) + +FILES =Imakefile dvitodvi.man Makefile.raw $(SRCS) + +all: dvitodvi + +dvitodvi: $(OBJS) $(DEPLIBS) + $(RM) $@ + $(CC) -o $@ $(OBJS) $(LDOPTIONS) $(LOCAL_LIBRARIES) $(LDLIBS) $(SYSLIBS) $(EXTRA_LOAD_FLAGS) + +clean:: + $(RM) dvitodvi + +install:: dvitodvi + @if [ -d $(DESTDIR) $(BINDIR) ]; then set +x; \ + else (set -x; $(MKDIRHIER) $(DESTDIR) $(BINDIR)); fi + $(INSTALL) -c $(INSTPGMFLAGS) dvitodvi $(DESTDIR) $(BINDIR) + +install.man:: dvitodvi.man + @if [ -d $(DESTDIR)$(MANDIR) ]; then set +x; \ + else (set -x; $(MKDIRHIER) $(DESTDIR)$(MANDIR)); fi + $(INSTALL) -c $(INSTMANFLAGS) dvitodvi.man $(DESTDIR)$(MANDIR)/dvitodvi.$(MANSUFFIX) + +depend:: + $(DEPEND) $(DEPENDFLAGS) -s "# DO NOT DELETE" -- $(ALLDEFINES) -- $(SRCS) + +tags:: + $(TAGS) -w *.[ch] + $(TAGS) -xw *.[ch] > TAGS + +tar: + cd ../..;\ + for file in $(FILES); \ + do \ + tar rf $(TARFILE) $(PRODUCT)/Dvitodvi/$$file; \ + done + +# ------------------------------------------------------------------------- +# common rules for all Makefiles - do not edit + +emptyrule:: + +clean:: + $(RM_CMD) "#"* + +Makefile:: + -@if [ -f Makefile ]; then set -x; \ + $(RM) Makefile.bak; $(MV) Makefile Makefile.bak; \ + else exit 0; fi + $(IMAKE_CMD) -DTOPDIR=$(TOP) -DCURDIR=$(CURRENT_DIR) + +tags:: + $(TAGS) -w *.[ch] + $(TAGS) -xw *.[ch] > TAGS + +saber: + # load $(ALLDEFINES) $(SRCS) + +osaber: + # load $(ALLDEFINES) $(OBJS) + +# ------------------------------------------------------------------------- +# empty rules for directories that do not have SUBDIRS - do not edit + +install:: + @echo "install in $(CURRENT_DIR) done" + +install.man:: + @echo "install.man in $(CURRENT_DIR) done" + +Makefiles:: + +includes:: + +# ------------------------------------------------------------------------- +# dependencies generated by makedepend + diff --git a/dviware/dvibook/Dvitodvi/dvitodvi.c b/dviware/dvibook/Dvitodvi/dvitodvi.c new file mode 100644 index 0000000000..c2bbadeeeb --- /dev/null +++ b/dviware/dvibook/Dvitodvi/dvitodvi.c @@ -0,0 +1,1141 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/dvi/RCS/dviselect.c,v 3.1 89/08/22 17:16:13 chris Exp $"; +#endif + +/* + * DVI page rearrangement program + * + * Reads DVI version 2 files and rearranges pages, + * writing a new DVI file. + */ + +#include "libtex/types.h" +#include "libtex/dviclass.h" +#include "libtex/dvicodes.h" +#include "libtex/error.h" +#include "libtex/fio.h" +#include "libtex/gripes.h" +#include "libtex/search.h" +#include <stdio.h> +#include <ctype.h> +#include "libtex/seek.h" + +#define white(x) ((x) == ' ' || (x) == '\t' || (x) == ',') + +#define MAXDVIPAGES 1000 /* max (absolute) pages in DVI file */ + +char *ProgName; +extern char *optarg; +extern int optind; + +/* Globals */ +char serrbuf[BUFSIZ]; /* buffer for stderr */ + +/* + * We will try to keep output lines shorter than MAXCOL characters. + */ +#define MAXCOL 75 + +/* + * We use the following structure to keep track of fonts we have seen. + * The final DVI file lists only the fonts it uses. + */ +struct fontinfo { + i32 fi_newindex; /* font number in output file */ + int fi_reallyused; /* true => used on a page we copied */ + i32 fi_checksum; /* the checksum */ + i32 fi_mag; /* the magnification */ + i32 fi_designsize; /* the design size */ + short fi_n1; /* the name header length */ + short fi_n2; /* the name body length */ + char *fi_name; /* the name itself */ +}; + + +i32 Width; /* width of page */ +i32 Height; /* height of page */ +i32 Magnification; /* Magnification of pages */ +int Modulo; /* page spec modulo */ +struct pagespec *PageSpecs; /* page specification list */ + +int SFlag; /* true => -s, silent operation */ + +struct search *FontFinder; /* maps from input indicies to fontinfo */ +i32 NextOutputFontIndex; /* generates output indicies */ +i32 CurrentFontIndex; /* current (old) index in input */ +i32 OutputFontIndex; /* current (new) index in ouput */ + +char *DVIFileName; /* name of input DVI file */ +FILE *inf; /* the input file itself */ +FILE *outf; /* the output DVI file */ + +long StartOfPage[MAXDVIPAGES]; /* The file positions of the + input pages */ + +long StartOfLastPage; /* The file position just before we + started the last page */ +long CurrentPosition; /* The current position of the file */ + +int UseThisPage; /* true => current page is selected */ + +i32 InputPageNumber; /* current absolute page in old DVI file */ +int NumberOfOutputPages; /* number of pages in new DVI file */ + +i32 Numerator; /* numerator from DVI file */ +i32 Denominator; /* denominator from DVI file */ +i32 DVIMag; /* magnification from DVI file */ + +i32 Count[10]; /* the 10 \count variables */ + +/* save some string space: we use this a lot */ +char writeerr[] = "error writing DVI file"; + +char *malloc(), *realloc(); +/* + * You may get lint warnings about sprintf's return value. + * Older versions of 4BSD have `char *sprintf()'. ANSI and + * SysV use `int sprintf()'; so ignore the warnings. + */ + +/* + * Lint gets somewhat confused over putc. + */ +#ifdef lint +#undef putc +#ifdef ultrix /* grr */ +#define putc(c, f) fputc((char)(c), f) +#else +#define putc(c, f) fputc((int)(c), f) +#endif +#endif + +void specusage() +{ + error(1, -1, "page specification error:\n\ + <pagespecs> = [modulo:][mag@]<spec>\n\ + <spec> = [-]pageno[(xoff,yoff)][,spec|+spec]\n\ + modulo>=1, 0<=pageno<modulo"); +} + +/* + * This function calculates approximately (whole + num/den) * sf. + * No need for real extreme accuracy; one ten thousandth of an + * inch should be sufficient. + * + * Assumptions: + * + * 0 <= num < den <= 10000 + * 0 <= whole + */ +i32 defaultscale = 4736286 ; +i32 scale(whole, num, den, sf) + i32 whole, sf; + int num, den; +{ + i32 v ; + + if (!sf) + sf = defaultscale ; + v = whole * sf + num * (sf / den) ; + if (v / sf != whole || v < 0 || v > 0x40000000L) + error(1, -1, "arithmetic overflow in dimension") ; + sf = sf % den ; + v += (sf * num * 2 + den) / (2 * den) ; + return (v) ; +} + +struct pagespec { + int reversed, pageno, add; + i32 xoff, yoff; + struct pagespec *next; +}; + +struct pagespec *newspec() +{ + struct pagespec *temp = (struct pagespec *)malloc(sizeof(struct pagespec)); + temp->reversed = temp->pageno = temp->add = 0; + temp->xoff = temp->yoff = 0; + temp->next = NULL; + return (temp); +} + +int parseint(sp) + char **sp; +{ + char *s = *sp; + int n = 0, neg = 0; + + if (*s == '-') { + neg = 1; + s++; + } + for (; isdigit(*s); s++) + n = n*10 + (*s-'0'); + if (*sp == s) specusage(); + *sp = s; + return (neg ? -n : n); +} + +i32 parsedimen(sp) + char **sp; +{ + i32 whole = 0; + int num = 0, den = 1, neg = 0; + i32 fac = 0L; + char *s = *sp; + + if (*s == '-') { + neg = 1; + *sp = ++s; + } + for (; isdigit(*s); s++) + whole = whole*10 + (*s-'0'); + + if (*s == '.') { + *sp = ++s; + for (; isdigit(*s); s++) { + if (den < 10000) { /* limit precision for scale to work */ + num = num*10 + (*s-'0'); + den *= 10; + } + } + } + if (*sp == s) specusage(); + *sp = s; + + /* + * Allowed units are `in', `cm', `mm', `pt', `sp', `cc', `dd', and `pc'; + * must be in lower case. + */ + if (*s == 'c' && s[1] == 'm') { + /* centimeters need to be scaled by 72.27 * 216 / 2.54, or 1 864 680 */ + fac = 1864680L ; + s += 2; + } else if (*s == 'p' && s[1] == 't') { + /* real points need to be scaled by 65536 */ + fac = 65536L ; + s += 2; + } else if (*s == 'p' && s[1] == 'c') { + /* picas need to be scaled by 65536 * 12, or 786 432 */ + fac = 786432L ; + s += 2; + } else if (*s == 'm' && s[1] == 'm') { + /* millimeters need to be scaled by 72.27 * 216 / 25.4, or 186 468 */ + fac = 186468L ; + s += 2; + } else if (*s == 's' && s[1] == 'p') { + /* scaled points are already taken care of; simply round */ + fac = 1L ; + s += 2; + } else if (*s == 'b' && s[1] == 'p') { + /* big points need to be scaled by 72.27 * 65536 / 72, or 65782 */ + fac = 65782L ; + s += 2; + } else if (*s == 'd' && s[1] == 'd') { + /* didot points need to be scaled by 65536 * 1238 / 1157, or 70124 */ + fac = 70124L ; + s += 2; + } else if (*s == 'c' && s[1] == 'c') { + /* cicero need to be scaled by 65536 * 1238 / 1157 * 12, or 841 489 */ + fac = 841489L ; + s += 2; + } else if (*s == 'i' && s[1] == 'n') { + /* inches need to be scaled by 72.27 * 65536, or 4 736 286 */ + fac = 4736286L ; + s += 2; + } else if (*s == 'w') { + fac = Width; + s++; + } else if (*s == 'h') { + fac = Height; + s++; + } + whole = scale(whole, num, den, fac) ; + *sp = s; + return (neg ? -whole : whole); +} + +struct pagespec *ParseSpecs(str, make) + char *str; + int make; +{ + char *t; + struct pagespec *head, *tail; + int other = 0; + int num = -1; + struct pagespec spare; + + if (make) + head = tail = newspec(); + else + head = tail = &spare; + while (*str) { + if (isdigit(*str)) { + num = parseint(&str); + } else { + switch (*str++) { + case ':': + if (other || head != tail || num < 1) specusage(); + Modulo = num; + num = -1; + break; + case '@': + if (other || head != tail || num < 1) specusage(); + Magnification = num; + num = -1; + break; + case '-': + tail->reversed = !tail->reversed; + other = 1; + break; + case '(': + tail->xoff += parsedimen(&str); + if (*str++ != ',') specusage(); + tail->yoff += parsedimen(&str); + if (*str++ != ')') specusage(); + other = 1; + break; + case '+': + tail->add = 1; + case ',': + if (num < 0 || num >= Modulo) specusage(); + tail->pageno = num; + if (make) { + tail->next = newspec(); + tail = tail->next; + } + num = -1; + other = 1; + break; + default: + specusage(); + } + } + } + if (num >= Modulo) + specusage(); + else if (num >= 0) + tail->pageno = num; + return (head); +} + +i32 singledimen(str) + char *str; +{ + i32 num = parsedimen(&str); + if (*str) return (0); + return (num); +} + +/* + * Print a message to stderr, with an optional leading space, and handling + * long line wraps. + */ +message(space, str, len) + int space; + register char *str; + register int len; +{ + static int beenhere; + static int col; + + if (!beenhere) + space = 0, beenhere++; + if (len == 0) + len = strlen(str); + col += len; + if (space) { + if (col >= MAXCOL) + (void) putc('\n', stderr), col = len; + else + (void) putc(' ', stderr), col++; + } + while (--len >= 0) + (void) putc(*str++, stderr); + (void) fflush(stderr); +} + +/* + * Start a page (process a DVI_BOP). + */ +BeginPage(really) + int really; +{ + register i32 *i; + + OutputFontIndex = -1; /* new page requires respecifying font */ + for (i = Count; i < &Count[10]; i++) + fGetLong(inf, *i); + (void) GetLong(inf); /* previous page pointer */ + + if (!UseThisPage || !really) + return; + + putbyte(outf, DVI_BOP); + for (i = Count; i < &Count[10]; i++) + PutLong(outf, *i); + PutLong(outf, StartOfLastPage); + if (ferror(outf)) + error(1, -1, writeerr); + + StartOfLastPage = CurrentPosition; + CurrentPosition += 45; /* we just wrote this much */ + + if (!SFlag) { /* write nice page usage messages */ + register int z = 0; + register int mlen = 0; + char msg[80]; + + (void) sprintf(msg, "[%ld", (long)Count[0]); + mlen = strlen(msg); + for (i = &Count[1]; i < &Count[10]; i++) { + if (*i == 0) { + z++; + continue; + } + while (--z >= 0) + msg[mlen++] = '.', msg[mlen++] = '0'; + z = 0; + (void) sprintf(msg + mlen, ".%ld", (long)*i); + mlen += strlen(msg + mlen); + } + message(1, msg, mlen); + } +} + +/* + * End a page (process a DVI_EOP). + */ +EndPage(really) + int really; +{ + + if (!UseThisPage || !really) + return; + if (!SFlag) + message(0, "]", 1); + putbyte(outf, DVI_EOP); + if (ferror(outf)) + error(1, -1, writeerr); + CurrentPosition++; + NumberOfOutputPages++; +} + +/* + * For each of the fonts used in the new DVI file, write out a definition. + */ +/* ARGSUSED */ +void +PostAmbleFontEnumerator(addr, key) + char *addr; + i32 key; +{ + + if (((struct fontinfo *)addr)->fi_reallyused) + WriteFont((struct fontinfo *)addr); +} + +HandlePostAmble() +{ + register i32 c; + + (void) GetLong(inf); /* previous page pointer */ + if (GetLong(inf) != Numerator) + GripeMismatchedValue("numerator"); + if (GetLong(inf) != Denominator) + GripeMismatchedValue("denominator"); + if (GetLong(inf) * Magnification / 1000 != DVIMag) + GripeMismatchedValue("\\magnification"); + + putbyte(outf, DVI_POST); + PutLong(outf, StartOfLastPage); + PutLong(outf, Numerator); + PutLong(outf, Denominator); + PutLong(outf, DVIMag); + c = GetLong(inf); + PutLong(outf, c); /* tallest page height */ + c = GetLong(inf); + PutLong(outf, c); /* widest page width */ + c = GetWord(inf)+1; + PutWord(outf, c); /* DVI stack size */ + PutWord(outf, NumberOfOutputPages); + StartOfLastPage = CurrentPosition; /* point at post */ + CurrentPosition += 29; /* count all those `put's */ +#ifdef notdef + (void) GetWord(inf); /* skip original number of pages */ +#endif + + /* + * just ignore all the incoming font definitions; we are done with + * input file + */ + + /* + * run through the FontFinder table and dump definitions for the + * fonts we have used. + */ + SEnumerate(FontFinder, PostAmbleFontEnumerator); + + putbyte(outf, DVI_POSTPOST); + PutLong(outf, StartOfLastPage); /* actually start of postamble */ + putbyte(outf, DVI_VERSION); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + putbyte(outf, DVI_FILLER); + CurrentPosition += 10; + while (CurrentPosition & 3) { + putbyte(outf, DVI_FILLER); + CurrentPosition++; + } + if (ferror(outf)) + error(1, -1, writeerr); +} + +/* + * Write a font definition to the output file + */ +WriteFont(fi) + register struct fontinfo *fi; +{ + register int l; + register char *s; + + if (fi->fi_newindex < 256) { + putbyte(outf, DVI_FNTDEF1); + putbyte(outf, fi->fi_newindex); + CurrentPosition += 2; + } else if (fi->fi_newindex < 65536) { + putbyte(outf, DVI_FNTDEF2); + PutWord(outf, fi->fi_newindex); + CurrentPosition += 3; + } else if (fi->fi_newindex < 16777216) { + putbyte(outf, DVI_FNTDEF3); + Put3Byte(outf, fi->fi_newindex); + CurrentPosition += 4; + } else { + putbyte(outf, DVI_FNTDEF4); + PutLong(outf, fi->fi_newindex); + CurrentPosition += 5; + } + PutLong(outf, fi->fi_checksum); + PutLong(outf, fi->fi_mag); + PutLong(outf, fi->fi_designsize); + putbyte(outf, fi->fi_n1); + putbyte(outf, fi->fi_n2); + l = fi->fi_n1 + fi->fi_n2; + CurrentPosition += 14 + l; + s = fi->fi_name; + while (--l >= 0) + putbyte(outf, *s++); +} + +/* + * Handle the preamble. Someday we should update the comment field. + */ +HandlePreAmble() +{ + register int n, c; + + c = getc(inf); + if (c == EOF) + GripeUnexpectedDVIEOF(); + if (c != DVI_PRE) + GripeMissingOp("PRE"); + if (getc(inf) != DVI_VERSION) + error(1, 0, "%s is not a DVI version %d file", + DVIFileName, DVI_VERSION); + Numerator = GetLong(inf); + Denominator = GetLong(inf); + DVIMag = GetLong(inf) * Magnification / 1000; + putbyte(outf, DVI_PRE); + putbyte(outf, DVI_VERSION); + PutLong(outf, Numerator); + PutLong(outf, Denominator); + PutLong(outf, DVIMag); + + n = UnSign8(GetByte(inf)); + CurrentPosition = 15 + n; /* well, almost */ + putbyte(outf, n); + while (--n >= 0) { + c = GetByte(inf); + putbyte(outf, c); + } +} + +main(argc, argv) + int argc; + register char **argv; +{ + register int c; + register char *s; + char *outname = NULL; + char *specstring = NULL; + + Width = 0; + Height = 0; + Magnification = 1000; + Modulo = 1; + + ProgName = *argv; + setbuf(stderr, serrbuf); + + while ((c = getopt(argc, argv, "i:o:w:h:q")) != EOF) { + switch (c) { + + case 'q': /* silent */ + SFlag++; + break; + + case 'i': + if (DVIFileName != NULL) + goto usage; + DVIFileName = optarg; + break; + + case 'o': + if (outname != NULL) + goto usage; + outname = optarg; + break; + + case 'w': + if (Width != 0) + goto usage; + Width = singledimen(optarg); + if (Width <= 0) + error(1, -1, "-w parameter must be > 0"); + break; + + case 'h': + if (Height != 0) + goto usage; + Height = singledimen(optarg); + if (Height <= 0) + error(1, -1, "-h parameter must be > 0"); + break; + + case '?': +usage: + (void) fprintf(stderr, "\ +Usage: %s [-q] [-i infile] [-o outfile] [-w width] [-h height] <pagespecs> [infile [outfile]]\n", + ProgName); + (void) fflush(stderr); + exit(1); + } + } + + while (optind < argc) { + s = argv[optind++]; + c = *s; + if (specstring == NULL) + (void) ParseSpecs((specstring = s), 0); + else if (DVIFileName == NULL) + DVIFileName = s; + else if (outname == NULL) + outname = s; + else + goto usage; + } + + if (specstring == NULL) + goto usage; + + if (DVIFileName == NULL) { + DVIFileName = "`stdin'"; + inf = stdin; + } else if ((inf = fopen(DVIFileName, "r")) == 0) + error(1, -1, "cannot read %s", DVIFileName); + if (outname == NULL) + outf = stdout; + else if ((outf = fopen(outname, "w")) == 0) + error(1, -1, "cannot write %s", outname); + + if ((FontFinder = SCreate(sizeof(struct fontinfo))) == 0) + error(1, 0, "cannot create font finder (out of memory?)"); + + /* copy inf to TEMP file if not seekable */ + if ((inf = SeekFile(inf)) == NULL) { + error(1, 0, "can't seek file"); + } + + InputPageNumber = 0; + StartOfLastPage = -1; + HandlePreAmble(); + ScanDVIFile(); + if (fseek(inf, 16L, 1) == -1) + error(1, -1, "can't seek postamble"); + if (Height == 0) /* get height from postamble */ + Height = GetLong(inf); + else + (void) GetLong(inf); /* ignore height */ + if (Width == 0) /* get width from postamble */ + Width = GetLong(inf); + PageSpecs = ParseSpecs(specstring, 1); + + HandleDVIFile(); + HandlePostAmble(); + if (!SFlag) + (void) fprintf(stderr, "\nWrote %d page%s, %ld bytes\n", + NumberOfOutputPages, NumberOfOutputPages == 1 ? "" : "s", + (long)CurrentPosition); + exit(0); + /* NOTREACHED */ +} + +/* + * Handle a font definition. + */ +HandleFontDef(index) + i32 index; +{ + register struct fontinfo *fi; + register int i; + register char *s; + int def = S_CREATE | S_EXCL; + + if (!UseThisPage) { + if ((fi = (struct fontinfo *)SSearch(FontFinder, index, &def)) == 0) + if (def & S_COLL) + error(1, 0, "font %ld already defined", (long)index); + else + error(1, 0, "cannot stash font %ld (out of memory?)", + (long)index); + fi->fi_reallyused = 0; + fi->fi_checksum = GetLong(inf); + fi->fi_mag = GetLong(inf); + fi->fi_designsize = GetLong(inf); + fi->fi_n1 = UnSign8(GetByte(inf)); + fi->fi_n2 = UnSign8(GetByte(inf)); + i = fi->fi_n1 + fi->fi_n2; + if ((s = malloc((unsigned)i)) == 0) + GripeOutOfMemory(i, "font name"); + fi->fi_name = s; + while (--i >= 0) + *s++ = GetByte(inf); + } else { + (void) GetLong(inf); + (void) GetLong(inf); + (void) GetLong(inf); + i = UnSign8(GetByte(inf)); + i += UnSign8(GetByte(inf)); + while (--i >= 0) + (void) GetByte(inf); + } +} + +/* + * Handle a \special. + */ +HandleSpecial(c, l, p) + int c; + register int l; + register i32 p; +{ + register int i; + + if (UseThisPage) { + putbyte(outf, c); + switch (l) { + + case DPL_UNS1: + putbyte(outf, p); + CurrentPosition += 2; + break; + + case DPL_UNS2: + PutWord(outf, p); + CurrentPosition += 3; + break; + + case DPL_UNS3: + Put3Byte(outf, p); + CurrentPosition += 4; + break; + + case DPL_SGN4: + PutLong(outf, p); + CurrentPosition += 5; + break; + + default: + panic("HandleSpecial l=%d", l); + /* NOTREACHED */ + } + CurrentPosition += p; + while (--p >= 0) { + i = getc(inf); + putbyte(outf, i); + } + if (feof(inf)) + GripeUnexpectedDVIEOF(); + if (ferror(outf)) + error(1, -1, writeerr); + } else + while (--p >= 0) + (void) getc(inf); +} + +ReallyUseFont() +{ + register struct fontinfo *fi; + int look = S_LOOKUP; + + fi = (struct fontinfo *)SSearch(FontFinder, CurrentFontIndex, &look); + if (fi == NULL) + error(1, 0, "DVI file requested font %ld without defining it", + (long)CurrentFontIndex); + if (fi->fi_reallyused == 0) { + fi->fi_reallyused++; + fi->fi_newindex = NextOutputFontIndex++; + WriteFont(fi); + } + if (fi->fi_newindex != OutputFontIndex) { + PutFontSelector(fi->fi_newindex); + OutputFontIndex = fi->fi_newindex; + } +} + +/* + * Write a font selection command to the output file + */ +PutFontSelector(index) + i32 index; +{ + + if (index < 64) { + putbyte(outf, index + DVI_FNTNUM0); + CurrentPosition++; + } else if (index < 256) { + putbyte(outf, DVI_FNT1); + putbyte(outf, index); + CurrentPosition += 2; + } else if (index < 65536) { + putbyte(outf, DVI_FNT2); + PutWord(outf, index); + CurrentPosition += 3; + } else if (index < 16777216) { + putbyte(outf, DVI_FNT3); + Put3Byte(outf, index); + CurrentPosition += 4; + } else { + putbyte(outf, DVI_FNT4); + PutLong(outf, index); + CurrentPosition += 5; + } +} + +/* + * The following table describes the length (in bytes) of each of the DVI + * commands that we can simply copy, starting with DVI_SET1 (128). + */ +char oplen[128] = { + 0, 0, 0, 0, /* DVI_SET1 .. DVI_SET4 */ + 9, /* DVI_SETRULE */ + 0, 0, 0, 0, /* DVI_PUT1 .. DVI_PUT4 */ + 9, /* DVI_PUTRULE */ + 1, /* DVI_NOP */ + 0, /* DVI_BOP */ + 0, /* DVI_EOP */ + 1, /* DVI_PUSH */ + 1, /* DVI_POP */ + 2, 3, 4, 5, /* DVI_RIGHT1 .. DVI_RIGHT4 */ + 1, /* DVI_W0 */ + 2, 3, 4, 5, /* DVI_W1 .. DVI_W4 */ + 1, /* DVI_X0 */ + 2, 3, 4, 5, /* DVI_X1 .. DVI_X4 */ + 2, 3, 4, 5, /* DVI_DOWN1 .. DVI_DOWN4 */ + 1, /* DVI_Y0 */ + 2, 3, 4, 5, /* DVI_Y1 .. DVI_Y4 */ + 1, /* DVI_Z0 */ + 2, 3, 4, 5, /* DVI_Z1 .. DVI_Z4 */ + 0, /* DVI_FNTNUM0 (171) */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 172 .. 179 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 180 .. 187 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 188 .. 195 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 196 .. 203 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 204 .. 211 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 212 .. 219 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 220 .. 227 */ + 0, 0, 0, 0, 0, 0, 0, /* 228 .. 234 */ + 0, 0, 0, 0, /* DVI_FNT1 .. DVI_FNT4 */ + 0, 0, 0, 0, /* DVI_XXX1 .. DVI_XXX4 */ + 0, 0, 0, 0, /* DVI_FNTDEF1 .. DVI_FNTDEF4 */ + 0, /* DVI_PRE */ + 0, /* DVI_POST */ + 0, /* DVI_POSTPOST */ + 0, 0, 0, 0, 0, 0, /* 250 .. 255 */ +}; + +int +HandlePage(first, last, hoffset, voffset) + int first, last; + i32 hoffset, voffset; +{ + register int c, l; + register i32 p; + register int CurrentFontOK = 0; + int doingpage = 0; + + /* Only way out is via "return" statement */ + for (;;) { + c = getc(inf); /* getc() returns unsigned values */ + if (DVI_IsChar(c)) { + /* + * Copy chars, note font usage, but ignore if + * page is not interesting. + */ + if (!UseThisPage) + continue; + if (!CurrentFontOK) { + ReallyUseFont(); + CurrentFontOK++; + } + putbyte(outf, c); + CurrentPosition++; + continue; + } + if (DVI_IsFont(c)) { /* note font change */ + CurrentFontIndex = c - DVI_FNTNUM0; + CurrentFontOK = 0; + continue; + } + if (c == EOF) + GripeUnexpectedDVIEOF(); + if ((l = (oplen - 128)[c]) != 0) { /* simple copy */ + if (!UseThisPage) { + while (--l > 0) + (void) getc(inf); + continue; + } + CurrentPosition += l; + putbyte(outf, c); + while (--l > 0) { + c = getc(inf); + putbyte(outf, c); + } + if (ferror(outf)) + error(1, -1, writeerr); + continue; + } + if ((l = DVI_OpLen(c)) != 0) { + /* + * Handle other generics. + * N.B.: there should only be unsigned parameters + * here (save SGN4), for commands with negative + * parameters have been taken care of above. + */ + switch (l) { + + case DPL_UNS1: + p = getc(inf); + break; + + case DPL_UNS2: + fGetWord(inf, p); + break; + + case DPL_UNS3: + fGet3Byte(inf, p); + break; + + case DPL_SGN4: + fGetLong(inf, p); + break; + + default: + panic("HandleDVIFile l=%d", l); + } + + /* + * Now that we have the parameter, perform the + * command. + */ + switch (DVI_DT(c)) { + + case DT_SET: + case DT_PUT: + if (!UseThisPage) + continue; + if (!CurrentFontOK) { + ReallyUseFont(); + CurrentFontOK++; + } + putbyte(outf, c); + switch (l) { + + case DPL_UNS1: + putbyte(outf, p); + CurrentPosition += 2; + continue; + + case DPL_UNS2: + PutWord(outf, p); + CurrentPosition += 3; + continue; + + case DPL_UNS3: + Put3Byte(outf, p); + CurrentPosition += 4; + continue; + + case DPL_SGN4: + PutLong(outf, p); + CurrentPosition += 5; + continue; + } + + case DT_FNT: + CurrentFontIndex = p; + CurrentFontOK = 0; + continue; + + case DT_XXX: + HandleSpecial(c, l, p); + continue; + + case DT_FNTDEF: + HandleFontDef(p); + continue; + + default: + panic("HandleDVIFile DVI_DT(%d)=%d", + c, DVI_DT(c)); + } + continue; + } + + switch (c) { /* handle the few remaining cases */ + + case DVI_BOP: + if (doingpage) + GripeUnexpectedOp("BOP (during page)"); + BeginPage(first); + if (UseThisPage) { + if (!last) { + putbyte(outf, DVI_PUSH); + CurrentPosition++; + } + if (hoffset != 0) { + putbyte(outf, DVI_RIGHT4) ; + PutLong(outf, hoffset) ; + CurrentPosition += 5; + } + if (voffset != 0) { + putbyte(outf, DVI_DOWN4) ; + PutLong(outf, voffset) ; + CurrentPosition += 5; + } + } + doingpage = 1; + break; + + case DVI_EOP: + if (!doingpage) + GripeUnexpectedOp("EOP (outside page)"); + if (!last && UseThisPage) { + putbyte(outf, DVI_POP); + CurrentPosition++; + } + EndPage(last); + doingpage = 0; + return(1); + + case DVI_PRE: + GripeUnexpectedOp("PRE"); + /* NOTREACHED */ + + case DVI_POST: + if (doingpage) + GripeUnexpectedOp("POST (inside page)"); + return(0); + + case DVI_POSTPOST: + GripeUnexpectedOp("POSTPOST"); + /* NOTREACHED */ + + default: + GripeUndefinedOp(c); + /* NOTREACHED */ + } + } +} + +/* write an empty page to fill out space */ +PutEmptyPage() +{ + int i; + + putbyte(outf, DVI_BOP); + PutLong(outf, -1L); + for (i = 1; i < 10; i++) /* set all sub counts to 0 */ + PutLong(outf, 0L); + PutLong(outf, StartOfLastPage); + putbyte(outf, DVI_EOP); + if (!SFlag) { /* write nice page usage messages */ + char *msg = "[*]"; + message(1, msg, strlen(msg)); + } + if (ferror(outf)) + error(1, -1, writeerr); + + StartOfLastPage = CurrentPosition; + CurrentPosition += 46; /* we just wrote this much */ + NumberOfOutputPages++; +} + +/* + * Here we scan the input DVI file and record pointers to the pages. + */ +ScanDVIFile() +{ + UseThisPage = 0; + + StartOfPage[InputPageNumber] = ftell(inf); + while (HandlePage(0, 0, 0, 0)) { /* scan DVI file */ + StartOfPage[++InputPageNumber] = ftell(inf); + } +} + +/* + * Here we read the input DVI file and write relevant pages to the + * output DVI file. We also keep track of font changes, handle font + * definitions, and perform some other housekeeping. + */ +HandleDVIFile() +{ + int CurrentPage, ActualPage, MaxPage; + + UseThisPage = 1; + + MaxPage = ((InputPageNumber+Modulo-1)/Modulo)*Modulo; + + for (CurrentPage = 0; CurrentPage < MaxPage; CurrentPage += Modulo) { + int add_last = 0; + struct pagespec *ps; + for (ps = PageSpecs; ps != NULL; ps = ps->next) { + int add_next = ps->add; + if (ps->reversed) + ActualPage = MaxPage-CurrentPage-Modulo+ps->pageno; + else + ActualPage = CurrentPage+ps->pageno; + if (ActualPage < InputPageNumber) { + if (fseek(inf, StartOfPage[ActualPage], 0) == -1) + error(1, -1, + "can't seek page %d", ActualPage+1); + HandlePage(!add_last, !add_next, ps->xoff, ps->yoff); + } else if (!add_last && !add_next) + PutEmptyPage(); + add_last = add_next; + } + } + if (fseek(inf, StartOfPage[InputPageNumber]+1, 0) == -1) + error(1, -1, "can't seek last page"); +} diff --git a/dviware/dvibook/Dvitodvi/dvitodvi.man b/dviware/dvibook/Dvitodvi/dvitodvi.man new file mode 100644 index 0000000000..313fc34fd1 --- /dev/null +++ b/dviware/dvibook/Dvitodvi/dvitodvi.man @@ -0,0 +1,137 @@ +.TH DVITODVI 1 +.SH NAME +dvitodvi \- rearrange pages in a DVI file +.SH SYNOPSIS +.B dvitodvi +[ +.B \-q +] [ +.B \-i +.I infile +] [ +.B \-o +.I outfile +] +[ +.B \-w\fIwidth\fR +] +[ +.B \-h\fIheight\fR +] +.I pagespecs +[ +.I infile +[ +.I outfile +] ] +.SH DESCRIPTION +.I Dvitodvi +rearranges pages from a DVI file, creating a new DVI file. +.I Dvitodvi +can be used to perform a large number of arbitrary re-arrangements of +Documents, including arranging for printing 2-up, 4-up, booklets, reversing, +selecting front or back sides of documents, scaling, etc. +.PP +.I pagespecs +follow the syntax: +.RS +.TP 12 +.I pagespecs +.I = [modulo:][mag@]specs +.TP +.I specs +.I = spec[+specs][,specs] +.TP +.I spec +.I = [-]pageno[(xoff,yoff)] +.RE +.sp +.I modulo +is the number of pages in each block. The value of +.I modulo +should be greater than 0; the default value is 1. +The optional +.I mag +parameter adjusts the file's magnification by the magnification specified. The +magnification and modulo can be specified in reverse order if desired. +.I specs +are the page specifications for the pages in each block. The value of the +.I pageno +in each +.I spec +should be between 0 (for the first page in the block) and \fImodulo\fR-1 +(for the last page in each block) inclusive. +The optional dimensions +.I xoff +and +.I yoff +shift the page by the specified (positive) amount. +.I xoff +and +.I yoff +are in PostScript's points, but may be followed by the units +.B "cm" +or +.B "in" +to convert to centimetres or inches, or the flag +.B "w" +or +.B "h" +to specify as a multiple of the width or height. +If the optional minus sign is specified, the page is relative to the end of +the document, instead of the start. + +If page \fIspec\fRs are separated by +.B \+ +the pages will be merged into one page; if they are separated by +.B \, +they will be on separate pages. +If there is only one page specification, with +.I pageno +zero, the \fIpageno\fR may be omitted. +.PP +The +.I \-w +option gives the width which is used by the +.B "w" +dimension specifier, and the +.I \-h +option gives the height which is used by the +.B "h" +dimension specifier. These dimensions are also used (after scaling) to set the +clipping path for each page. +.PP +Dvitodvi normally prints the page numbers of the pages re-arranged; the +.I \-q +option suppresses this. +.SH EXAMPLES +This section contains some sample re-arrangements. To put two pages on one +sheet (of landscaped A3 paper), the pagespec to use is: +.sp +.ce +2:0+1(21cm,0) +.sp +To reduce an A3 page to A4 size, the pagespec to use is: +.sp +.ce +700@(-.3in,-.3in) +.sp +To select all of the odd pages in reverse order, use: +.sp +.ce +2:-0 +.sp +.SH AUTHOR +Angus Duggan, from dviselect(1), by +Chris Torek, University of Maryland +.SH "SEE ALSO" +dviconcat(1), dvibook(1), dviselect(1), latex(1), tex(1) +.br +.I "MC-TeX User's Guide" +.br +.I "The TeXbook" +.I Dvitodvi +does not adjust some of the parameters in the postamble. The may be a problem +if these +values are used to size certain structures in the output +conversion programs. diff --git a/dviware/dvibook/Imake.Config b/dviware/dvibook/Imake.Config new file mode 100644 index 0000000000..dd708b5dc3 --- /dev/null +++ b/dviware/dvibook/Imake.Config @@ -0,0 +1,205 @@ +/* * Install directories for programs not related to X */ + +TEX_BINDIR = /srl/TeX/bin +TEX_MANDIR = /srl/TeX/man/man1 +TEX_INPUTS = /srl/TeX/lib/tex/inputs + +/* + * Set `FONTDESC' to the location of your fontdesc file, unquoted. + * This is passed to the library routines so they can find it. + */ + +FontDesc= /srl/TeX/lib/tex/fontdesc + +IprBin=/usr/local/bin + +/*********************************************************************** + * Config options for Xtex: + */ + +/* + * Options in the Xtex.ad file enabled by #ifdef's. + * + * Possibilities include: + * -DCOLOR_BUTTONS + */ +XTEX_IFDEF_OPTIONS= + +/* + * XTEX_TFM_PATH is a colon seperated list of directories that + * might contains TFM files; these are used by xtex to determine + * true font widths. + */ + +XTEX_TFM_PATH=.:/srl/TeX/lib/tex/fonts:/usr/local/lib/tex82/fonts:/usr/local/lib/tex/fonts:/usr/lib/tex82/fonts:/usr/lib/tex/fonts:/usr/local/lib/tex/tfm:/usr/local/Tex/lib/tex/tfm + +/* XtexFontPath is a comma/colon seperated list of directories that + * commonly contain X fonts for XTeX. If you specify your directory(-ies) + * here when you install XTeX, you avoid having to explain to users + * what needs to be done. You may need to #ifdef-configure this + * based on architecture. I don't use this, so it's commented out. + */ + +XTEX_FONT_PATH=/srl/TeX.xtex +/*********************************************************************** + * Automatic font creation: Morry Katz (katz@cs.stanford.edu) added this. + * + * If a font is not found, a shell script (TeXtoXfont) is executed. + * This will attempt to build the font from scratch, using Metafont. + * The font will be installed in a certain directory, after which the + * server should be able to find it. + * + * Comand to build fonts: + * %n will be replaced by the font name + * %d will be replaced by the dpi + * %b will be replaced by the base dpi of the display device + * %m will be replaced by the magnification + * For a server and client on the same machine the following is recommended: + * Xtex*.makeFont:TeXtoXfont %n %d %b %m MODE FONTCACHEDIR + * MODE should be replaced by the metafont mode_def for the server's + * display + * FONTCACHEDIR should be replaced by the directory in which the .bdf + * and/or .snf font files which are built should be stored (this + * directory must be in the server's font search path for correct + * operation). + * For execution when the client is on a different machine than the server + * the following is recommended (note that TeXtoXfont and mftodbf must exist + * on the server): + * Xtex*.makeFont:rsh SERVER -l USER TeXtoXfont %n %d %b %m MODE FONTCACHEDIR + * SERVER should be replaced by the name of the X server. + * USER should be replaced by the username of the user whose account + * should be used to run the script. (The clinet must have rsh access + * to the server for this to work.) + * In those rare cases in which the server and client are different and the + * client knows how to build fonts for the server which the server cannot + * build on its own the following is recommended: + * Xtex*.makeFont:TeXtoXfont.remote %n %d %b %m MODE FONTCACHEDIR SERVER USER + * + ***********************************************************************/ + +XTEX_MAKE_FONT=TeXtoXfont %n %d %b %m sun $(XTEX_FONT_PATH) + +/* + * X_FONT_SUFFIXES Font file suffix for your server + * X_FONT_COMPILER Font compiler for your server + * X_FONT_MAKE_DIR The way you build a 'fonts.dir' or equivilent + * X_FONT_COMPRESS YES/NO - do you want fonts compressed? + * X_FONT_RESET Program to reset your font path. + */ + +#if defined(UltrixArchitecture) + X_FONT_SUFFIX=pcf + X_FONT_COMPILER=/usr/bin/dxfc $$BDFNAME > $$SNFNAME + X_FONT_MAKE_DIR=/usr/bin/dxmkfontdir + X_FONT_COMPRESS=YES + X_FONT_PREPEND=xset +fp + X_FONT_RESET=xset fp rehash +#else +# if !defined(SunArchitecture) + X_FONT_SUFFIX=snf + X_FONT_COMPILER=bdftosnf $$BDFNAME > $$SNFNAME + X_FONT_MAKE_DIR=mkfontdir + X_FONT_COMPRESS=YES + X_FONT_PREPEND=true #xset +fp + X_FONT_RESET=xset fp rehash +# else + X_FONT_SUFFIX=fb + X_FONT_COMPILER=convertfont -o $$NAME.$$DPI $$BDFNAME + X_FONT_MAKE_DIR=bldfamily >/dev/null -f 64 -d + X_FONT_COMPRESS=NO + X_FONT_PREPEND=true # xset +fp + X_FONT_RESET=xset fp rehash +# endif +#endif + +/* + * + * DISPLAY_POSTSCRIPT + * Set this if the client-side provides display postscript. + * If the server does not support DPS, you just won't see + * anything, but if you specify this & the client doesn't + * have it, you'll get compile errors. + * + */ + +/* +#define DISPLAY_POSTSCRIPT +*/ + +/* + * DISPLAY_GHOSTSCRIPT + * Set this if you are using Ghostscript to display embedded + * PostScript. + * + */ + +#define DISPLAY_GHOSTSCRIPT + +#ifdef DISPLAY_GHOSTSCRIPT +/* + * GHOST_SEARCH_PATH + * Should be a colon separated list of directories. + * This is pre-pended to ghostscripts default file search path. + * Use this to specify the location of prlog files (e.g., mac.pro). + */ +GHOST_SEARCH_PATH=/srl/TeX/lib/tex82/ps +#endif + +/* + * NEWS SUPPORT -- If you have NeWS, then define NEWS + */ + +#if defined(SunArchitecture) && OSMajorVersion >= 4 +/* + * I don't think that NEWS/OpenWindows should be the default -- if + * you want NeWS/OpenWindows, just comment in the followingl. + */ + +/* # define NEWS */ +#endif + +/* + * SELFILE + * Set this if you want to use Erik M. van der Poel's file selection + * widget. This just adds a new `Browse' button; files can still be + * be specified using the `Set Directory' and `Open' buttons. + * + */ +#define SELFILE + +/* + * you can change this to transientShellWidgetClass if you like; however, + * the mouse cursor won't be positioned under the 'confirm' button + * automatically on the goto & print popups. + */ +#if 1 + POPUP_WINDOW_TYPE =overrideShellWidgetClass +#else + POPUP_WINDOW_TYPE =transientShellWidgetClass +#endif + +/* + * common Make macros + */ + LIBTEXROOT = ../ + LIBTEXSRC = ../libtex + LIBTEX = $(LIBTEXSRC)/libtex.a + DEPLIBTEX = $(LIBTEX) + +/* + * Undefine GETOP if your loader complains about multiple ref's. + */ +#if 1 + GETOPT=getopt.o +#else + GETOPT= +#endif + +/* + * Define STRTOL if your system does not provide strtol + */ +#if 0 + STRTOL=strtol.o +#else + STRTOL= +#endif diff --git a/dviware/dvibook/Imake.cpp b/dviware/dvibook/Imake.cpp new file mode 100644 index 0000000000..6e3ef838b2 --- /dev/null +++ b/dviware/dvibook/Imake.cpp @@ -0,0 +1,33 @@ +/* + * You will have to change these + */ +HOME =/hyperion/a/grads/shankar +BINDIR =${HOME}/terra/bin +MANDIR =${HOME}/man +RESOURCES =${HOME}/lib/X11/app-defaults + +/* + * Where do X includes and libraries live? + */ +INCROOT =/usr/local/X11R5/include +XLIBDIR =-L${XHOME}/lib + +CDEBUGFLAGS=-O + +/***********************************************************************/ + + +#define XawClientDepLibs +#define XawClientLibs $(XLIBDIR) -lXaw -lXt -lXmu -lXext -lX11 + +SYS_LIBRARIES = $(XLIBDIR) -lXaw -lXt -lXmu -lXext -lX11 -lm +SYSLIBS = $(SYS_LIBRARIES) + +CFLAGS=$(DEFINES) $(CDEBUGFLAGS) $(INCLUDES) $(SYS_INCLUDES) + +#define YES 1 +#define NO 0 + +#if defined(mips) || defined(ultrix) +# define UltrixArchitecture +#endif diff --git a/dviware/dvibook/Imake.m4 b/dviware/dvibook/Imake.m4 new file mode 100644 index 0000000000..791703db52 --- /dev/null +++ b/dviware/dvibook/Imake.m4 @@ -0,0 +1,89 @@ +undefine(`include') + +define(MakeSubdirs, +all:: + for dir in $1 ; do (echo Build in $$dir; cd $$dir ; make ) ; done +clean:: + for dir in $1 ; do (echo Clean in $$dir; cd $$dir ; make clean) ; done +install:: + for dir in $1; do (echo Install in $$dir; cd $$dir; make install); done +) + +define(DependSubdirs, +depend:: + for dir in $1 ; do (cd $$dir ; make depend) ; done +) + +define(NormalLibraryObjectRule,) + +define(NormalLibraryTarget, +lib$1.a: $2 + rm -f lib$1.a + ar r lib$1.a $2 + ranlib lib$1.a +clean:: + rm -f lib$1.a $2 +) + +define(LintLibraryTarget,) + +define(CleanTarget,) + +define(TagsTarget, +tags:: + etags *.c *.h +) + +define(NormalLintTarget,) + +define(NormalProgramTarget, +$1: $2 + ${CC} ${CFLAGS} -o $1 $2 $4 $5 +clean:: + rm -f $1 $2 +) + +define(SingleProgramTarget, +$1: $(OBJS) + ${CC} ${CFLAGS} -o $1 $(OBJS) $(LOCAL_LIBRARIES) $(SYSLIBS) +clean:: + rm -f $1 $2 +install:: + install -c -s $1 $(BININDIR) +) + +define(ComplexProgramTarget, +$1: $(OBJS) + ${CC} ${CFLAGS} -o $1 $(OBJS) $(LOCAL_LIBRARIES) $(SYSLIBS) +clean:: + rm -f $1 $2 +install:: + install -c -s $1 $(BININDIR) +) + +define(InstallProgram, +install:: $1 + install -c -s $1 $2 +) + +define(InstallNonExec, +install:: $1 + install -c $1 $2 +) + +define(InstallScript, +install:: $1 + install -c -m 555 $1.script $2/$1 +) + +define(InstallManPage, +install:: $1.man + install -c $1.man $2/$1.1 +) + +define(InstallAppDefaults, +install:: $1.ad + install -c $1.ad $RESOURCES/$1 +) + +define(DependTarget,) diff --git a/dviware/dvibook/Imakefile b/dviware/dvibook/Imakefile new file mode 100644 index 0000000000..2ab8de24a7 --- /dev/null +++ b/dviware/dvibook/Imakefile @@ -0,0 +1,121 @@ +/* + * Copyright 1989 Dirk Grunwald + * + * Permission to use, copy, modify, distribute, and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and that + * both that copyright notice and this permission notice appear in + * supporting documentation, and that the name of Dirk Grunwald or M.I.T. + * not be used in advertising or publicity pertaining to distribution of + * the software without specific, written prior permission. Dirk + * Grunwald and M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + * DIRK GRUNWALD AND M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT + * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE + * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE + * OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: + * Dr. Dirk Grunwald + * Dept. of Computer Science + * Campus Box 430 + * Univ. of Colorado, Boulder + * Boulder, CO 80309 + * + * grunwald@colorado.edu + * + */ + +#define IHaveSubdirs + +/* +COMMON_SUBDIRS = libtex Xtex Mftobdf Dviselect Dviconcat Texx Iptex +*/ +COMMON_SUBDIRS = libtex Dviselect Dviconcat Dvitodvi Dvibook + +#include "./Imake.Config" + +#ifdef SunArchitecture + SUBDIRS = $(COMMON_SUBDIRS) Texsun +#endif + +#ifdef unixpc + SUBDIRS = libtex Tex3b1 Dviselect +#endif + +#if !defined(SunArchitecture) && !defined(unixpc) + SUBDIRS = $(COMMON_SUBDIRS) +#endif + +TARSUBDIRS = libtex Doc Xtex Mftobdf Dviselect Dviconcat \ + Texx Iptex Fonts Texsun Tex3b1 + +#define PassCDebugFlags 'CDEBUGFLAGS=$(CDEBUGFLAGS)' + +MakeSubdirs($(SUBDIRS)) +DependSubdirs($(SUBDIRS)) + + +FILES = Imakefile Imake.Config fontdesc-example README \ + Imake.cpp Imake.m4 NoImake + +VERSION =2.18.5 +FTP =/srl/ftp +tar: + for file in $(TARSUBDIRS);\ + do\ + (cd $$file ; $(MAKE) Makefile ) ;\ + done + cd ..;\ + rm -f SeeTeX.tar ; \ + tar cf SeeTeX.tar SeeTeX/Imakefile ; \ + for file in $(FILES);\ + do\ + tar rf SeeTeX.tar SeeTeX/$$file; \ + done; \ + cd SeeTeX; \ + for file in $(TARSUBDIRS);\ + do\ + (cd $$file ; $(MAKE) TARFILE=SeeTeX.tar PRODUCT=SeeTeX tar);\ + done ; \ + cd .. ; \ + mv SeeTeX.tar /usr/tmp ; \ + rm -rf /usr/tmp/SeeTeX ; \ + (cd /usr/tmp;\ + tar xvf SeeTeX.tar;\ + rm -f SeeTeX.tar $(FTP)/pub/SeeTeX/SeeTeX*.tar*;\ + tar crf $(FTP)/pub/SeeTeX/SeeTeX-$(VERSION).tar SeeTeX;\ + rm -rf SeeTeX ;\ + compress $(FTP)/pub/SeeTeX/SeeTeX-$(VERSION).tar) + (cd Xtex; \ + $(MAKE) xtex-$(VERSION).tar.Z;\ + mv xtex-$(VERSION).tar.Z $(FTP)/pub/SeeTeX) + + +libtex/lib.a: + cd libtex; $(MAKE) + +xtex: libtex/lib.a + cd Xtex; $(MAKE) + +mftobdf XeT/mftobdf: + cd Mftobdf ; $(MAKE) mftobdf + +dviselect Dviselect/dviselect: + cd Dviselect ; $(MAKE) dviselect + +texsun Texsun/texsun: + cd Texsun ; $(MAKE) texsun + +texx Texx/texx: + cd Texx ; $(MAKE) texx + +iptex Iptex/iptex: + cd Iptex ; $(MAKE) iptex + + diff --git a/dviware/dvibook/NoImake b/dviware/dvibook/NoImake new file mode 100644 index 0000000000..61139c6e1a --- /dev/null +++ b/dviware/dvibook/NoImake @@ -0,0 +1,17 @@ +#!/bin/sh + +cat Imake.cpp Imakefile | /lib/cpp > Makefile.tmp +m4 Imake.m4 Makefile.tmp > Makefile +rm -f Makefile.tmp + +COMMON_SUBDIRS="libtex Dviselect Dviconcat Dvitodvi Dvibook" + +for dir in ${COMMON_SUBDIRS} ; +do + echo Build makefile in $dir ; + (cd $dir; + cat ../Imake.cpp Imakefile | /lib/cpp > Makefile.tmp ; + m4 ../Imake.m4 Makefile.tmp > Makefile ; + rm -f Makefile.tmp ; + ) +done diff --git a/dviware/dvibook/README b/dviware/dvibook/README new file mode 100644 index 0000000000..aee9135ae9 --- /dev/null +++ b/dviware/dvibook/README @@ -0,0 +1,21 @@ +These utilities re-arrange TeX DVI files, creating DVI files as output. + +Dvibook rearranges pages so that the file can be printed as a booklet on a +duplex printer. + +Dvitodvi rearranges pages according to a complex page specification. It can +be used to rearrange for booklet printing, extract odd or even pages, overlay +pages, and many other functions. + +These programs require the Chris Torek's DVI library, which is included in +the SeeTeX distribution, available from a good archive near you. + +AJCD 5/3/92 + + +The relevant parts of the SeeTeX distribution are now included in the tar file +version of this distribution. The full SeeTeX distribution is still available +from ftp.dcs.ed.ac.uk:pub/tex/SeeTeX, among other places. Coming soon; Dvisep, +a colour separator for DVI files. + +AJCD 14/3/94 diff --git a/dviware/dvibook/libtex/Imakefile b/dviware/dvibook/libtex/Imakefile new file mode 100644 index 0000000000..b6891ab872 --- /dev/null +++ b/dviware/dvibook/libtex/Imakefile @@ -0,0 +1,44 @@ +#include "../Imake.Config" + + DEFINES =-DFONTDESC=\"$(FontDesc)\" + +SRCS=\ + bcopy.c error.c gffont.c rstfont.c split.c\ + bzero.c findpost.c gripes0.c scaletfm.c strsave.c\ + conv.c fio.c gripes1.c scanpost.c tempfile.c\ + dvi_draw.c font.c magfactor.c sdecode.c tfm.c\ + dviclass.c font_subr.c pkfont.c search.c tfmfont.c\ + dvistate.c getopt.c pxlfont.c seek.c\ + dvistuff.c gfclass.c rotate.c skip.c strtol.c + +OBJS=\ + bcopy.o error.o gffont.o rstfont.o split.o \ + bzero.o findpost.o gripes0.o scaletfm.o strsave.o \ + conv.o fio.o gripes1.o scanpost.o tempfile.o \ + dvi_draw.o font.o magfactor.o sdecode.o tfm.o \ + dviclass.o font_subr.o pkfont.o search.o tfmfont.o \ + dvistate.o pxlfont.o seek.o \ + dvistuff.o gfclass.o rotate.o skip.o $(GETOPT) $(STRTOL) + +HDRS =\ + conv.h dvistate.h font.h num.h seek.h \ + convstruct.h dvistuff.h gfclass.h postamble.h tfm.h\ + dviclass.h error.h gfcodes.h sdecode.h types.h \ + dvicodes.h fio.h gripes.h search.h + +FILES =$(SRCS) $(HDRS) Imakefile Makefile + +NormalLibraryObjectRule() +NormalLibraryTarget(tex,$(OBJS)) +LintLibraryTarget(tex,$(SRCS)) +DependTarget() +NormalLintTarget($(SRCS)) + +tar: + cd ../..;\ + for file in $(FILES); \ + do \ + tar rf $(TARFILE) $(PRODUCT)/libtex/$$file; \ + done + +font.o: ../Imake.Config diff --git a/dviware/dvibook/libtex/Makefile b/dviware/dvibook/libtex/Makefile new file mode 100644 index 0000000000..8c8a7b8f1d --- /dev/null +++ b/dviware/dvibook/libtex/Makefile @@ -0,0 +1,388 @@ +# Makefile generated by imake - do not edit! +# $XConsortium: imake.c,v 1.65 91/07/25 17:50:17 rws Exp $ + +# ------------------------------------------------------------------------- +# Makefile generated from "Imake.tmpl" and <Imakefile> +# $XConsortium: Imake.tmpl,v 1.138 91/07/30 14:59:20 rws Exp $ +# +# Platform-specific parameters may be set in the appropriate <vendor>.cf +# configuration files. Site-specific parameters should be set in the file +# site.def. Full rebuilds are recommended if any parameters are changed. +# +# If your C preprocessor does not define any unique symbols, you will need +# to set BOOTSTRAPCFLAGS when rebuilding imake (usually when doing +# "make World" the first time). +# + +# ------------------------------------------------------------------------- +# site-specific configuration parameters that need to come before +# the platform-specific parameters - edit site.def to change + +# site: $XConsortium: site.def,v 1.2 91/07/30 20:26:44 rws Exp $ + +# ------------------------------------------------------------------------- +# platform-specific configuration parameters - edit ultrix.cf to change + +# platform: $XConsortium: ultrix.cf,v 1.45 91/07/30 12:12:11 rws Exp $ + +# operating system: Ultrix-32 4.2 + +# ------------------------------------------------------------------------- +# site-specific configuration parameters that go after +# the platform-specific parameters - edit site.def to change + +# site: $XConsortium: site.def,v 1.2 91/07/30 20:26:44 rws Exp $ + + SHELL = /bin/sh + + TOP = ../. + CURRENT_DIR = ./libtex + + AR = ar clq + BOOTSTRAPCFLAGS = + CC = cc + AS = as + + COMPRESS = compress + CPP = /lib/cpp $(STD_CPP_DEFINES) + PREPROCESSCMD = cc -E $(STD_CPP_DEFINES) + INSTALL = install + LD = ld + LINT = lint + LINTLIBFLAG = -o + LINTOPTS = -ax + LN = ln -s + MAKE = make + MV = mv + CP = cp + + RANLIB = ranlib + RANLIBINSTFLAGS = + + RM = rm -f + TROFF = psroff + MSMACROS = -ms + TBL = tbl + EQN = eqn + STD_INCLUDES = + STD_CPP_DEFINES = + STD_DEFINES = + EXTRA_LOAD_FLAGS = + EXTRA_LIBRARIES = + TAGS = ctags + + PROTO_DEFINES = -DFUNCPROTO + + INSTPGMFLAGS = -s -m 755 + + INSTBINFLAGS = -m 0755 + INSTUIDFLAGS = -m 4755 + INSTLIBFLAGS = -m 0644 + INSTINCFLAGS = -m 0444 + INSTMANFLAGS = -m 0444 + INSTDATFLAGS = -m 0444 + INSTKMEMFLAGS = -m 4755 + + PROJECTROOT = /usr/local/X.V11R5 + + TOP_INCLUDES = -I$(INCROOT) + + CDEBUGFLAGS = -O + CCOPTIONS = + + ALLINCLUDES = $(INCLUDES) $(EXTRA_INCLUDES) $(TOP_INCLUDES) $(STD_INCLUDES) + ALLDEFINES = $(ALLINCLUDES) $(STD_DEFINES) $(EXTRA_DEFINES) $(PROTO_DEFINES) $(DEFINES) + CFLAGS = $(CDEBUGFLAGS) $(CCOPTIONS) $(ALLDEFINES) + LINTFLAGS = $(LINTOPTS) -DLINT $(ALLDEFINES) + + LDLIBS = $(SYS_LIBRARIES) $(EXTRA_LIBRARIES) + + LDOPTIONS = $(CDEBUGFLAGS) $(CCOPTIONS) $(LOCAL_LDFLAGS) -L$(USRLIBDIR) + + LDCOMBINEFLAGS = -r + DEPENDFLAGS = + + MACROFILE = ultrix.cf + RM_CMD = $(RM) *.CKP *.ln *.BAK *.bak *.o core errs ,* *~ *.a .emacs_* tags TAGS make.log MakeOut + + IMAKE_DEFINES = + + IRULESRC = $(CONFIGDIR) + IMAKE_CMD = $(IMAKE) -DUseInstalled -I$(IRULESRC) $(IMAKE_DEFINES) + + ICONFIGFILES = $(IRULESRC)/Imake.tmpl $(IRULESRC)/Imake.rules \ + $(IRULESRC)/Project.tmpl $(IRULESRC)/site.def \ + $(IRULESRC)/$(MACROFILE) $(EXTRA_ICONFIGFILES) + +# ------------------------------------------------------------------------- +# X Window System Build Parameters +# $XConsortium: Project.tmpl,v 1.138 91/09/10 09:02:12 rws Exp $ + +# ------------------------------------------------------------------------- +# X Window System make variables; this need to be coordinated with rules + + PATHSEP = / + USRLIBDIR = /usr/local/X.V11R5/lib + BINDIR = /usr/local/X.V11R5/bin + INCROOT = /usr/local/X.V11R5/include + BUILDINCROOT = $(TOP) + BUILDINCDIR = $(BUILDINCROOT)/X11 + BUILDINCTOP = .. + INCDIR = $(INCROOT)/X11 + ADMDIR = /usr/adm + LIBDIR = $(USRLIBDIR) + CONFIGDIR = $(LIBDIR)/config + LINTLIBDIR = $(USRLIBDIR)/lint + + FONTDIR = $(LIBDIR)/fonts + XINITDIR = $(LIBDIR)/xinit + XDMDIR = $(LIBDIR)/xdm + TWMDIR = $(LIBDIR)/twm + MANPATH = /usr/local/X.V11R5/man + MANSOURCEPATH = $(MANPATH)/man + MANSUFFIX = 1 + LIBMANSUFFIX = 3 + MANDIR = $(MANSOURCEPATH)$(MANSUFFIX) + LIBMANDIR = $(MANSOURCEPATH)$(LIBMANSUFFIX) + NLSDIR = $(LIBDIR)/nls + PEXAPIDIR = $(LIBDIR)/PEX + XAPPLOADDIR = $(LIBDIR)/app-defaults + FONTCFLAGS = -t + + INSTAPPFLAGS = $(INSTDATFLAGS) + + IMAKE = imake + DEPEND = makedepend + RGB = rgb + + FONTC = bdftopcf + + MKFONTDIR = mkfontdir + MKDIRHIER = /bin/sh $(BINDIR)/mkdirhier + + CONFIGSRC = $(TOP)/config + DOCUTILSRC = $(TOP)/doc/util + CLIENTSRC = $(TOP)/clients + DEMOSRC = $(TOP)/demos + LIBSRC = $(TOP)/lib + FONTSRC = $(TOP)/fonts + INCLUDESRC = $(TOP)/X11 + SERVERSRC = $(TOP)/server + UTILSRC = $(TOP)/util + SCRIPTSRC = $(UTILSRC)/scripts + EXAMPLESRC = $(TOP)/examples + CONTRIBSRC = $(TOP)/../contrib + DOCSRC = $(TOP)/doc + RGBSRC = $(TOP)/rgb + DEPENDSRC = $(UTILSRC)/makedepend + IMAKESRC = $(CONFIGSRC) + XAUTHSRC = $(LIBSRC)/Xau + XLIBSRC = $(LIBSRC)/X + XMUSRC = $(LIBSRC)/Xmu + TOOLKITSRC = $(LIBSRC)/Xt + AWIDGETSRC = $(LIBSRC)/Xaw + OLDXLIBSRC = $(LIBSRC)/oldX + XDMCPLIBSRC = $(LIBSRC)/Xdmcp + BDFTOSNFSRC = $(FONTSRC)/bdftosnf + BDFTOSNFSRC = $(FONTSRC)/clients/bdftosnf + BDFTOPCFSRC = $(FONTSRC)/clients/bdftopcf + MKFONTDIRSRC = $(FONTSRC)/clients/mkfontdir + FSLIBSRC = $(FONTSRC)/lib/fs + FONTSERVERSRC = $(FONTSRC)/server + EXTENSIONSRC = $(TOP)/extensions + XILIBSRC = $(EXTENSIONSRC)/lib/xinput + PHIGSLIBSRC = $(EXTENSIONSRC)/lib/PEX + + DEPEXTENSIONLIB = $(USRLIBDIR)/libXext.a + EXTENSIONLIB = -lXext + + DEPXLIB = $(DEPEXTENSIONLIB) $(USRLIBDIR)/libX11.a + XLIB = $(EXTENSIONLIB) -lX11 + + DEPXAUTHLIB = $(USRLIBDIR)/libXau.a + XAUTHLIB = -lXau + DEPXDMCPLIB = $(USRLIBDIR)/libXdmcp.a + XDMCPLIB = -lXdmcp + + DEPXMULIB = $(USRLIBDIR)/libXmu.a + XMULIB = -lXmu + + DEPOLDXLIB = $(USRLIBDIR)/liboldX.a + OLDXLIB = -loldX + + DEPXTOOLLIB = $(USRLIBDIR)/libXt.a + XTOOLLIB = -lXt + + DEPXAWLIB = $(USRLIBDIR)/libXaw.a + XAWLIB = -lXaw + + DEPXILIB = $(USRLIBDIR)/libXi.a + XILIB = -lXi + + DEPPHIGSLIB = $(USRLIBDIR)/libphigs.a + PHIGSLIB = -lphigs + + DEPXBSDLIB = $(USRLIBDIR)/libXbsd.a + XBSDLIB = -lXbsd + + LINTEXTENSIONLIB = $(LINTLIBDIR)/llib-lXext.ln + LINTXLIB = $(LINTLIBDIR)/llib-lX11.ln + LINTXMU = $(LINTLIBDIR)/llib-lXmu.ln + LINTXTOOL = $(LINTLIBDIR)/llib-lXt.ln + LINTXAW = $(LINTLIBDIR)/llib-lXaw.ln + LINTXI = $(LINTLIBDIR)/llib-lXi.ln + LINTPHIGS = $(LINTLIBDIR)/llib-lphigs.ln + + DEPLIBS = $(DEPXAWLIB) $(DEPXMULIB) $(DEPXTOOLLIB) $(DEPXLIB) + + DEPLIBS1 = $(DEPLIBS) + DEPLIBS2 = $(DEPLIBS) + DEPLIBS3 = $(DEPLIBS) + +# ------------------------------------------------------------------------- +# Imake rules for building libraries, programs, scripts, and data files +# rules: $XConsortium: Imake.rules,v 1.121 91/08/22 16:32:50 rws Exp $ + +# ------------------------------------------------------------------------- +# start of Imakefile + +TEX_BINDIR = /srl/TeX/bin +TEX_MANDIR = /srl/TeX/man/man1 +TEX_INPUTS = /srl/TeX/lib/tex/inputs + +FontDesc= /srl/TeX/lib/tex/fontdesc + +IprBin=/usr/local/bin + +XTEX_IFDEF_OPTIONS= + +XTEX_TFM_PATH=.:/srl/TeX/lib/tex/fonts:/usr/local/lib/tex82/fonts:/usr/local/lib/tex/fonts:/usr/lib/tex82/fonts:/usr/lib/tex/fonts:/usr/local/lib/tex/tfm:/usr/local/Tex/lib/tex/tfm + +XTEX_FONT_PATH=/srl/TeX.xtex + +XTEX_MAKE_FONT=TeXtoXfont %n %d %b %m sun $(XTEX_FONT_PATH) + + X_FONT_SUFFIX=pcf + X_FONT_COMPILER=/usr/bin/dxfc $$BDFNAME > $$SNFNAME + X_FONT_MAKE_DIR=/usr/bin/dxmkfontdir + X_FONT_COMPRESS=1 + X_FONT_PREPEND=xset +fp + X_FONT_RESET=xset fp rehash + +GHOST_SEARCH_PATH=/srl/TeX/lib/tex82/ps + + POPUP_WINDOW_TYPE =overrideShellWidgetClass + + LIBTEXROOT = ../ + LIBTEXSRC = ../libtex + LIBTEX = $(LIBTEXSRC)/libtex.a + DEPLIBTEX = $(LIBTEX) + + GETOPT=getopt.o + + STRTOL= + + DEFINES =-DFONTDESC=\"$(FontDesc)\" + +SRCS=\ + bcopy.c error.c gffont.c rstfont.c split.c\ + bzero.c findpost.c gripes0.c scaletfm.c strsave.c\ + conv.c fio.c gripes1.c scanpost.c tempfile.c\ + dvi_draw.c font.c magfactor.c sdecode.c tfm.c\ + dviclass.c font_subr.c pkfont.c search.c tfmfont.c\ + dvistate.c getopt.c pxlfont.c seek.c\ + dvistuff.c gfclass.c rotate.c skip.c strtol.c + +OBJS=\ + bcopy.o error.o gffont.o rstfont.o split.o \ + bzero.o findpost.o gripes0.o scaletfm.o strsave.o \ + conv.o fio.o gripes1.o scanpost.o tempfile.o \ + dvi_draw.o font.o magfactor.o sdecode.o tfm.o \ + dviclass.o font_subr.o pkfont.o search.o tfmfont.o \ + dvistate.o pxlfont.o seek.o \ + dvistuff.o gfclass.o rotate.o skip.o $(GETOPT) $(STRTOL) + +HDRS =\ + conv.h dvistate.h font.h num.h seek.h \ + convstruct.h dvistuff.h gfclass.h postamble.h tfm.h\ + dviclass.h error.h gfcodes.h sdecode.h types.h \ + dvicodes.h fio.h gripes.h search.h + +FILES =$(SRCS) $(HDRS) Imakefile Makefile + +.c.o: + $(RM) $@ $*.os \ + && $(CC) -c $(_NOOP_) $(CFLAGS) $*.c \ + && $(LD) -x -r $@ -o $*.os \ + && $(MV) $*.os $@ + +all:: libtex.a + + libtex.a: $(OBJS) + $(RM) $@ + $(AR) $@ $(OBJS) + $(RANLIB) $@ + +lintlib:: llib-ltex.ln + + llib-ltex.ln: $(SRCS) + $(RM) $@ + $(LINT) $(LINTLIBFLAG)tex $(LINTFLAGS) $(SRCS) + +depend:: + $(DEPEND) $(DEPENDFLAGS) -s "# DO NOT DELETE" -- $(ALLDEFINES) -- $(SRCS) + +lint: + $(LINT) $(LINTFLAGS) $(SRCS) $(LINTLIBS) +lint1: + $(LINT) $(LINTFLAGS) $(FILE) $(LINTLIBS) + +tar: + cd ../..;\ + for file in $(FILES); \ + do \ + tar rf $(TARFILE) $(PRODUCT)/libtex/$$file; \ + done + +font.o: ../Imake.Config + +# ------------------------------------------------------------------------- +# common rules for all Makefiles - do not edit + +emptyrule:: + +clean:: + $(RM_CMD) "#"* + +Makefile:: + -@if [ -f Makefile ]; then set -x; \ + $(RM) Makefile.bak; $(MV) Makefile Makefile.bak; \ + else exit 0; fi + $(IMAKE_CMD) -DTOPDIR=$(TOP) -DCURDIR=$(CURRENT_DIR) + +tags:: + $(TAGS) -w *.[ch] + $(TAGS) -xw *.[ch] > TAGS + +saber: + # load $(ALLDEFINES) $(SRCS) + +osaber: + # load $(ALLDEFINES) $(OBJS) + +# ------------------------------------------------------------------------- +# empty rules for directories that do not have SUBDIRS - do not edit + +install:: + @echo "install in $(CURRENT_DIR) done" + +install.man:: + @echo "install.man in $(CURRENT_DIR) done" + +Makefiles:: + +includes:: + +# ------------------------------------------------------------------------- +# dependencies generated by makedepend + diff --git a/dviware/dvibook/libtex/bcopy.c b/dviware/dvibook/libtex/bcopy.c new file mode 100644 index 0000000000..70c308b0a5 --- /dev/null +++ b/dviware/dvibook/libtex/bcopy.c @@ -0,0 +1,26 @@ +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/bcopy.c,v 3.1 89/08/22 21:42:07 chris Exp $"; +#endif + +/* + * Sample bcopy() routine. + * This should be rewritten to be as fast as possible for your + * machine. + */ +bcopy(from, to, count) + register char *from, *to; + register int count; +{ + + if (from == to) + return; + if (from > to) { + while (--count >= 0) + *to++ = *from++; + } else { + from += count; + to += count; + while (--count >= 0) + *--to = *--from; + } +} diff --git a/dviware/dvibook/libtex/bzero.c b/dviware/dvibook/libtex/bzero.c new file mode 100644 index 0000000000..cf1dd41645 --- /dev/null +++ b/dviware/dvibook/libtex/bzero.c @@ -0,0 +1,17 @@ +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/bzero.c,v 3.1 89/08/22 21:42:12 chris Exp $"; +#endif + +/* + * Sample bzero() routine. + * This should be rewritten to be as fast as possible for your + * machine. + */ +bzero(addr, count) + register char *addr; + register int count; +{ + + while (--count >= 0) + *addr++ = 0; +} diff --git a/dviware/dvibook/libtex/conv.c b/dviware/dvibook/libtex/conv.c new file mode 100644 index 0000000000..f2b096e8dd --- /dev/null +++ b/dviware/dvibook/libtex/conv.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/conv.c,v 3.1 89/08/22 21:48:01 chris Exp $"; +#endif + +/* + * Conversions. + */ + +#include "types.h" +#include "conv.h" + +double DMagFactor(); + +Conv Conversion; /* the global conversion */ + +/* + * Set a conversion (possibly the global conversion). + */ +void +CSetConversion(c, dpi, usermag, num, denom, dvimag) + register struct conversion *c; + int dpi, usermag; + i32 num, denom, dvimag; +{ + double ddpi = dpi; + + c->c_mag = DMagFactor((int) dvimag) * DMagFactor(usermag); + c->c_dpi = ddpi; + + /* + * The conversion facture is figured as follows: there are exactly + * num/denom DVI units per decimicron, and 254000 decimicrons per + * inch, and dpi pixels per inch. Then we have to adjust this by + * the stated magnification. + */ + c->c_fromsp = (num / 254000.0) * (ddpi / denom) * c->c_mag; + + /* + * c->c_tosp is 1/c->c_fromsp, but we will invert the expression + * above in the hopes of some extra accuracy. + * + * IS THIS ANY GOOD? I NEED A NUMERICAL ANALYST! + */ + c->c_tosp = (254000.0 / num) * (denom / ddpi) * (1.0 / c->c_mag); +} diff --git a/dviware/dvibook/libtex/conv.h b/dviware/dvibook/libtex/conv.h new file mode 100644 index 0000000000..3350ff5a90 --- /dev/null +++ b/dviware/dvibook/libtex/conv.h @@ -0,0 +1,89 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * Conversions. Conversion factors convert between values in scaled + * points and values in device-dependent units. The results of all + * conversions are rounded to the nearest integral value, of type (i32). + */ + +/* + * This is now done using `double' values, but may be changed to + * fixed-point or some other `fast' method, as long as the results + * are consistent and reasonably accurate. The structure `conversion' + * holds the conversion-method-dependent quantities; the macros + * fromSP and toSP apply the conversion to a value. (Note that + * fromSP and toSP need not be macros, but should be fast.) + * + * SetConversion sets the (single, global) conversion factor. + * If a driver needs special conversions, there is another routine, + * CSetConversion that sets a specific conversion, and cfromSP and + * ctoSP to apply these. + * + * IS USING DOTS PER INCH SUFFICIENT? (Pixels per point might be better.) + * + * Note that it is necessary to set the global conversion factor before + * using any fonts. + */ +typedef struct conversion { + double c_fromsp; /* multiplier to convert from scaled points */ + double c_tosp; /* multiplier to convert to scaled points: + could divide by c_fromsp, but this should + be faster and more accurate */ + double c_mag; /* the magnification this conversion + represents; mainly for GetFont() */ + double c_dpi; /* dpi (should be pixels per point?) */ +} Conv; + +extern Conv Conversion; /* the global conversion factor */ + +/* + * In order to do this, we need to round properly. The compilers I + * have tend to generate very poor code for this. The following is + * intended to help them out. Smarter compilers can do better, but + * if they are smart enough, they will realise that the variables + * here are not used anywhere else, and discard them. (For a compiler + * to do this given separate compliation, `static' is a must.) + */ +#ifdef lint /* or a smart compiler */ + +#define ROUND(f) ((i32) ((f) < 0.0 ? (f) - 0.5 : (f) + 0.5)) +#define CEIL(f) ((double) (i32) (f) < (f) ? (i32) (f) + 1 : (i32) (f)) + +#else /* lint */ + +static double _half = 0.5; +static double _d; +#define ROUND(f) ((i32) (_d = (f), _d < 0.0 ? _d - _half : _d + _half)) + +#ifdef NEGATIVE_FLOAT_ROUNDS_TO_NEGATIVE_INFINITY +#define CEIL(f) (-(i32) -(f)) +#else /* we will assume that floating to integer truncates */ +static i32 _i; +#define CEIL(f) (_i = _d = (f), _i < _d ? _i + 1 : _i) +#endif /* round towards negative infinity */ + +#endif /* lint */ + +#define SetConversion(dpi, usermag, num, denom, dvimag) \ + CSetConversion(&Conversion, dpi, usermag, num, denom, dvimag) + +#define cfromSP(c, v) ROUND((c)->c_fromsp * (v)) +#define ctoSP(c, v) ROUND((c)->c_tosp * (v)) + +#define fromSP(v) cfromSP(&Conversion, v) +#define toSP(v) ctoSP(&Conversion, v) + +/* + * Conversions for rules are a bit different: we must round up, rather + * than off. ConvRule applies the global conversion value for a rule + * value (height or width); CConvRule applies a specific conversion. + */ +#define CConvRule(c, v) CEIL((c)->c_fromsp * (v)) +#define ConvRule(v) CConvRule(&Conversion, v) + +void CSetConversion(); diff --git a/dviware/dvibook/libtex/convstruct.h b/dviware/dvibook/libtex/convstruct.h new file mode 100644 index 0000000000..b0f97088d8 --- /dev/null +++ b/dviware/dvibook/libtex/convstruct.h @@ -0,0 +1,38 @@ +/* + * Copyright 1989 Chris Torek + * + * Permission to use, copy, modify, distribute, and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and that + * both that copyright notice and this permission notice appear in + * supporting documentation, and that the name of Chris Torek or M.I.T. + * not be used in advertising or publicity pertaining to distribution of + * the software without specific, written prior permission. Chris + * Torek and M.I.T. make no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + * CHRIS TOREK AND M.I.T. DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS. IN NO EVENT SHALL CHRIS TOREK OR M.I.T. BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Original Author: + * Chris Torek + * Dept. of Computer Science + * Univ. of Maryland + * chris@cs.umd.edu + */ + +typedef struct conversion { + double c_fromsp; /* multiplier to convert from scaled points */ + double c_tosp; /* multiplier to convert to scaled points: + could divide by c_fromsp, but this should + be faster and more accurate */ + double c_mag; /* the magnification this conversion + represents; mainly for GetFont() */ + double c_dpi; /* dpi (should be pixels per point?) */ +} Conv; diff --git a/dviware/dvibook/libtex/dvi_draw.c b/dviware/dvibook/libtex/dvi_draw.c new file mode 100644 index 0000000000..e6259c993b --- /dev/null +++ b/dviware/dvibook/libtex/dvi_draw.c @@ -0,0 +1,390 @@ +/* + * Support drawing routines for TeXsun and TeX + * + * Copyright, (C) 1987, 1988 Tim Morgan, UC Irvine + * Copyright 1989 Dirk Grunwald (extensions & fixes) + * + * Permission to use, copy, modify, distribute, and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and that + * both that copyright notice and this permission notice appear in + * supporting documentation, and that the name of Tim Morgan, Dirk + * Grunwald or M.I.T. not be used in advertising or publicity + * pertaining to distribution of the software without specific, + * written prior permission. Tim Morgan, Dirk Grunwald and M.I.T. + * makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + * TIM MORGAN, + * DIRK GRUNWALD AND M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT + * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE + * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE + * OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: + * Tim Morgan + * Dept. of Computer Science + * Univ. of Calf. at Irvine + * + * Dirk Grunwald + * Dept. of Computer Science + * + * + * At the time these routines are called, the values of hh and vv should + * have been updated to the upper left corner of the graph (the position + * the \special appears at in the dvi file). Then the coordinates in the + * graphics commands are in terms of a virtual page with axes oriented the + * same as the Imagen and the SUN normally have: + * + * 0,0 + * +-----------> +x + * | + * | + * | + * \ / + * +y + * + * Angles are measured in the conventional way, from +x towards +y. + * Unfortunately, that reverses the meaning of "counterclockwise" + * from what it's normally thought of. + * + * A lot of floating point arithmetic has been converted to integer + * arithmetic for speed. In some places, this is kind-of kludgy, but + * it's worth it. + */ + +static char *rcsid="$Header: /usr/local/src/Uiuctex/libtex/RCS/dvi_draw.c,v 1.3 88/02/19 17:00:34 grunwald Exp Locker: grunwald $" ; + +#include <math.h> +#include <stdio.h> +#include <ctype.h> + +#define MAXPOINTS 300 /* Max points in a path */ +#define TWOPI (3.14159265359*2.0) +#define MAX_PEN_SIZE 7 /* Max pixels of pen width */ + + +static int xx[MAXPOINTS], yy[MAXPOINTS]; /* Path in milli-inches */ +static int path_len = 0; /* # points in current path */ +int pen_size = 1; /* Pixel width of lines drawn */ +#define FALSE 0 +#define TRUE 1 +int whiten = FALSE, shade = FALSE, blacken = FALSE; + +extern void dot_at(), line_btw(), do_attribute_path(); + +#define shrink_factor shrinkFactor[currentShrink] + +/* Unfortunately, these values also appear in dvisun.c */ +#define xRESOLUTION (dviDPI/shrink_factor) +#define yRESOLUTION (dviDPI/shrink_factor) + +extern int dviDPI, currentShrink, shrinkFactor[]; + + +/* + * Issue warning/error messages + */ +void Fatal(msg) +char *msg; +{ + fprintf(stderr, "%s\n", msg); + exit(1); +} + +static void Warning(fmt, msg) +char *fmt, *msg; +{ + fprintf(stderr, fmt, msg); + fputc ('\n', stderr); +} + + +/* + * Set the size of the virtual pen used to draw in milli-inches + */ +/* ARGSUSED */ +void set_pen_size(cp) +char *cp; +{ + int ps; + + if (sscanf(cp, " %d ", &ps) != 1) { + Warning("illegal .ps command format: %s", cp); + return; + } + pen_size = (ps*(xRESOLUTION+yRESOLUTION) + 1000) / 2000; + if (pen_size < 1) pen_size = 1; + else if (pen_size > MAX_PEN_SIZE) pen_size = MAX_PEN_SIZE; +} + + +/* + * Print the line defined by previous path commands + */ +void flush_path() +{ + register int i; + int last_min_x, last_max_x, last_min_y, last_max_y; + + last_min_x = 30000; last_min_y = 30000; + last_max_x = -30000; last_max_y = -30000; + for (i=1; i<path_len; i++) { + if (xx[i] > last_max_x) last_max_x = xx[i]; + if (xx[i] < last_min_x) last_min_x = xx[i]; + if (yy[i] > last_max_y) last_max_y = yy[i]; + if (yy[i] < last_min_y) last_min_y = yy[i]; + line_btw(xx[i], yy[i], xx[i+1], yy[i+1]); + } + if (xx[path_len] > last_max_x) last_max_x = xx[path_len]; + if (xx[path_len] < last_min_x) last_min_x = xx[path_len]; + if (yy[path_len] > last_max_y) last_max_y = yy[path_len]; + if (yy[path_len] < last_min_y) last_min_y = yy[path_len]; + path_len = 0; + do_attribute_path(last_min_x, last_max_x, last_min_y, last_max_y); +} + + +/* + * Print a dashed line along the previously defined path, with + * the dashes/inch defined. + */ +void flush_dashed(cp, dotted) +char *cp; +int dotted; +{ + int i, numdots, x0, y0, x1, y1; + int cx0, cy0, cx1, cy1; + float inchesperdash; + double d, spacesize, a, b, dx, dy, milliperdash; + + if (sscanf(cp, " %f ", &inchesperdash) != 1) { + Warning("illegal format for dotted/dashed line: %s", cp); + return; + } + if (path_len <= 1 || inchesperdash <= 0.0) { + Warning("illegal conditions for dotted/dashed line"); + return; + } + milliperdash = inchesperdash * 1000.0; + x0 = xx[1]; y0 = yy[1]; + x1 = xx[2]; y1 = yy[2]; + dx = x1 - x0; + dy = y1 - y0; + if (dotted) { + numdots = sqrt(dx*dx + dy*dy) / milliperdash + 0.5; + if ( numdots == 0 ) { /* always draw at least one dot */ + numdots = 1; + } + for (i=0; i <= numdots; i++) { + a = (float) i / (float) numdots; + cx0 = x0 + a*dx + 0.5; + cy0 = y0 + a*dy + 0.5; + dot_at(cx0, cy0); + } + } + else { + d = sqrt(dx*dx + dy*dy); + if (d <= 2.0*milliperdash) + line_btw(x0, y0, x1, y1); + else { + numdots = d / (2.0*milliperdash) + 1.0; + spacesize = (d - numdots * milliperdash) / (numdots - 1); + for (i=0; i<numdots-1; i++) { + a = i * (milliperdash + spacesize) / d; + b = a + milliperdash / d; + cx0 = x0 + a*dx + 0.5; + cy0 = y0 + a*dy + 0.5; + cx1 = x0 + b*dx + 0.5; + cy1 = y0 + b*dy + 0.5; + line_btw(cx0, cy0, cx1, cy1); + b += spacesize / d; + } + cx0 = x0 + b*dx + 0.5; + cy0 = y0 + b*dy + 0.5; + line_btw(cx0, cy0, x1, y1); + } + } + path_len = 0; +} + + +/* + * Add a point to the current path + */ +void add_path(cp) +char *cp; +{ + int pathx, pathy; + + if (++path_len >= MAXPOINTS) Fatal("Too many points"); + if (sscanf(cp, " %d %d ", &pathx, &pathy) != 2) + Fatal("Malformed path command"); + xx[path_len] = pathx; + yy[path_len] = pathy; +} + + +/* + * Draw to a floating point position + */ +static void im_fdraw(x, y) +double x,y; +{ + if (++path_len >= MAXPOINTS) Fatal("Too many arc points"); + xx[path_len] = x + 0.5; + yy[path_len] = y + 0.5; +} + + +/* + * Draw an arc + */ +void arc(cp) +char *cp; +{ + int xc, yc, xrad, yrad, n; + float start_angle, end_angle, angle, theta, r; + double xradius, yradius, xcenter, ycenter; + + if (sscanf(cp, " %d %d %d %d %f %f ", &xc, &yc, &xrad, &yrad, &start_angle, + &end_angle) != 6) { + Warning("illegal arc specification: %s", cp); + return; + } + /* We have a specialized fast way to draw closed circles/ellipses */ + if (start_angle <= 0.0 && end_angle >= 6.282) { + draw_ellipse(xc, yc, xrad, yrad); + return; + } + xcenter = xc; + ycenter = yc; + xradius = xrad; + yradius = yrad; + r = (xradius + yradius) / 2.0; + theta = sqrt(1.0 / r); + n = 0.3 * TWOPI / theta + 0.5; + if (n < 12) n = 12; + else if (n > 80) n = 80; + n /= 2; + theta = TWOPI / n; + flush_path(); + im_fdraw( xcenter + xradius*cos(start_angle), + ycenter + yradius*sin(start_angle) ); + angle = start_angle + theta; + while (angle < end_angle) { + im_fdraw(xcenter + xradius*cos(angle), + ycenter + yradius*sin(angle) ); + angle += theta; + } + im_fdraw(xcenter + xradius*cos(end_angle), + ycenter + yradius*sin(end_angle) ); + flush_path(); +} + + +/* + * APPROXIMATE integer distance between two points + */ +#define dist(x0, y0, x1, y1) (abs(x0-x1)+abs(y0-y1)) + + +/* + * Draw a spline along the previously defined path + */ +void flush_spline() +{ + int xp, yp, N, lastx=(-1), lasty; + int t1, t2, t3, steps, j; + register int i, w; + +#ifdef lint + lasty = -1; +#endif + N = path_len + 1; + xx[0] = xx[1]; yy[0] = yy[1]; + xx[N] = xx[N-1]; yy[N] = yy[N-1]; + for (i=0; i<N-1; i++) { /* interval */ + steps = (dist(xx[i], yy[i], xx[i+1], yy[i+1]) + + dist(xx[i+1], yy[i+1], xx[i+2], yy[i+2])) / 80; + for (j=0; j<steps; j++) { /* points within */ + w = (j*1000 + 500) / steps; + t1 = w * w / 20; + w -= 500; + t2 = (750000 - w * w) / 10; + w -= 500; + t3 = w * w / 20; + xp = (t1*xx[i+2] + t2*xx[i+1] + t3*xx[i] + 50000) / 100000; + yp = (t1*yy[i+2] + t2*yy[i+1] + t3*yy[i] + 50000) / 100000; + if (lastx > -1) line_btw(lastx, lasty, xp, yp); + lastx = xp; + lasty = yp; + } + } + path_len = 0; +} + + +/* + * Shade the last box, circle, or ellipse + */ +void shade_last() +{ + blacken = whiten = FALSE; + shade = TRUE; +} + + +/* + * Make the last box, circle, or ellipse, white inside (shade with white) + */ +void whiten_last() +{ + whiten = TRUE; + blacken = shade = FALSE; +} + + +/* + * Make last box, etc, black inside + */ +void blacken_last() +{ + blacken = TRUE; + whiten = shade = FALSE; +} + + +/* + * Draw an ellipse with the indicated center and radices. + */ +draw_ellipse(xc, yc, xr, yr) +int xc, yc, xr, yr; +{ + double angle, theta; + int n, px0, py0, px1, py1; + + angle = (xr + yr) / 2.0; + theta = sqrt(1.0 / angle); + n = TWOPI / theta + 0.5; + if (n < 12) n = 12; + else if (n > 80) n = 80; + n /= 2; + theta = TWOPI / n; + + angle = 0.0; + px0 = xc + xr; /* cos(0) = 1 */ + py0 = yc; /* Sin(0) = 0 */ + while ((angle += theta) <= TWOPI) { + px1 = xc + xr*cos(angle) + 0.5; + py1 = yc + yr*sin(angle) + 0.5; + line_btw(px0, py0, px1, py1); + px0 = px1; + py0 = py1; + } + line_btw(px0, py0, xc + xr, yc); +} diff --git a/dviware/dvibook/libtex/dviclass.c b/dviware/dvibook/libtex/dviclass.c new file mode 100644 index 0000000000..2897a00b7c --- /dev/null +++ b/dviware/dvibook/libtex/dviclass.c @@ -0,0 +1,97 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/dviclass.c,v 3.1 89/08/22 21:48:35 chris Exp $"; +#endif + +/* + * dviclass - DVI code classification tables. + */ + +#include "dviclass.h" + +/* shorthand---in lowercase for contrast (read on!) */ +#define four(x) x, x, x, x +#define six(x) four(x), x, x +#define sixteen(x) four(x), four(x), four(x), four(x) +#define sixty_four(x) sixteen(x), sixteen(x), sixteen(x), sixteen(x) +#define one_twenty_eight(x) sixty_four(x), sixty_four(x) + +/* + * This table contains the byte length of the single operand, or DPL_NONE + * if no operand, or if it cannot be decoded this way. + * + * The sequences UNS1, UNS2, UNS3, SGN4 (`SEQ_U') and SGN1, SGN2, SGN3, + * SGN4 (`SEQ_S') are rather common, and so we define macros for these. + */ +#define SEQ_U DPL_UNS1, DPL_UNS2, DPL_UNS3, DPL_SGN4 +#define SEQ_S DPL_SGN1, DPL_SGN2, DPL_SGN3, DPL_SGN4 + +char dvi_oplen[256] = { + one_twenty_eight(DPL_NONE), + /* characters 0 through 127 */ + SEQ_U, /* DVI_SET1 through DVI_SET4 */ + DPL_NONE, /* DVI_SETRULE */ + SEQ_U, /* DVI_PUT1 through DVI_PUT4 */ + DPL_NONE, /* DVI_PUTRULE */ + DPL_NONE, /* DVI_NOP */ + DPL_NONE, /* DVI_BOP */ + DPL_NONE, /* DVI_EOP */ + DPL_NONE, /* DVI_PUSH */ + DPL_NONE, /* DVI_POP */ + SEQ_S, /* DVI_RIGHT1 through DVI_RIGHT4 */ + DPL_NONE, /* DVI_W0 */ + SEQ_S, /* DVI_W1 through DVI_W4 */ + DPL_NONE, /* DVI_X0 */ + SEQ_S, /* DVI_X1 through DVI_X4 */ + SEQ_S, /* DVI_DOWN1 through DVI_DOWN4 */ + DPL_NONE, /* DVI_Y0 */ + SEQ_S, /* DVI_Y1 through DVI_Y4 */ + DPL_NONE, /* DVI_Z0 */ + SEQ_S, /* DVI_Z1 through DVI_Z4 */ + sixty_four(DPL_NONE), /* DVI_FNTNUM0 through DVI_FNTNUM63 */ + SEQ_U, /* DVI_FNT1 through DVI_FNT4 */ + SEQ_U, /* DVI_XXX1 through DVI_XXX4 */ + SEQ_U, /* DVI_FNTDEF1 through DVI_FNTDEF4 */ + DPL_NONE, /* DVI_PRE */ + DPL_NONE, /* DVI_POST */ + DPL_NONE, /* DVI_POSTPOST */ + six(DPL_NONE) /* 250 through 255 */ +}; + +char dvi_dt[256] = { + one_twenty_eight(DT_CHAR), + /* characters 0 through 127 */ + four(DT_SET), /* DVI_SET1 through DVI_SET4 */ + DT_SETRULE, /* DVI_SETRULE */ + four(DT_PUT), /* DVI_PUT1 through DVI_PUT4 */ + DT_PUTRULE, /* DVI_PUTRULE */ + DT_NOP, /* DVI_NOP */ + DT_BOP, /* DVI_BOP */ + DT_EOP, /* DVI_EOP */ + DT_PUSH, /* DVI_PUSH */ + DT_POP, /* DVI_POP */ + four(DT_RIGHT), /* DVI_RIGHT1 through DVI_RIGHT4 */ + DT_W0, /* DVI_W0 */ + four(DT_W), /* DVI_W1 through DVI_W4 */ + DT_X0, /* DVI_X0 */ + four(DT_X), /* DVI_X1 through DVI_X4 */ + four(DT_DOWN), /* DVI_DOWN1 through DVI_DOWN4 */ + DT_Y0, /* DVI_Y0 */ + four(DT_Y), /* DVI_Y1 through DVI_Y4 */ + DT_Z0, /* DVI_Z0 */ + four(DT_Z), /* DVI_Z1 through DVI_Z4 */ + sixty_four(DT_FNTNUM), /* DVI_FNTNUM0 through DVI_FNTNUM63 */ + four(DT_FNT), /* DVI_FNT1 through DVI_FNT4 */ + four(DT_XXX), /* DVI_XXX1 through DVI_XXX4 */ + four(DT_FNTDEF), /* DVI_FNTDEF1 through DVI_FNTDEF4 */ + DT_PRE, /* DVI_PRE */ + DT_POST, /* DVI_POST */ + DT_POSTPOST, /* DVI_POSTPOST */ + six(DT_UNDEF) /* 250 through 255 */ +}; diff --git a/dviware/dvibook/libtex/dviclass.h b/dviware/dvibook/libtex/dviclass.h new file mode 100644 index 0000000000..32b8ce0be6 --- /dev/null +++ b/dviware/dvibook/libtex/dviclass.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * Macros to convert DVI opcodes to (hopefully) simpler values. + */ + +/* + * Large range types---code may be any value (including EOF). + */ +#define DVI_IsChar(code) ((ui32)(code) < 128) +#define DVI_IsFont(code) ((code) >= 171 && (code) < 235) + +/* + * Symbolic names for generic types (for things with parameters). + * These are obtained via the macro DVI_DT(int c), where 0 <= c <= 255. + */ +#define DT_CHAR 0 +#define DT_SET 1 +#define DT_SETRULE 2 +#define DT_PUT 3 +#define DT_PUTRULE 4 +#define DT_NOP 5 +#define DT_BOP 6 +#define DT_EOP 7 +#define DT_PUSH 8 +#define DT_POP 9 +#define DT_RIGHT 10 +#define DT_W0 11 +#define DT_W 12 +#define DT_X0 13 +#define DT_X 14 +#define DT_DOWN 15 +#define DT_Y0 16 +#define DT_Y 17 +#define DT_Z0 18 +#define DT_Z 19 +#define DT_FNTNUM 20 +#define DT_FNT 21 +#define DT_XXX 22 +#define DT_FNTDEF 23 +#define DT_PRE 24 +#define DT_POST 25 +#define DT_POSTPOST 26 +#define DT_UNDEF 27 + +/* + * Symbolic names for parameter lengths, obtained via the macro + * DVL_OpLen(int c). + * + * N.B.: older drivers may assume that 0 => none, 1-4 => 1-4 bytes + * and 5-7 => unsigned version of 1-4---so DO NOT change these values! + */ +#define DPL_NONE 0 +#define DPL_SGN1 1 +#define DPL_SGN2 2 +#define DPL_SGN3 3 +#define DPL_SGN4 4 +#define DPL_UNS1 5 +#define DPL_UNS2 6 +#define DPL_UNS3 7 +/* there are no unsigned four byte parameters */ + +#define DVI_OpLen(code) (dvi_oplen[code]) +#define DVI_DT(code) (dvi_dt[code]) +extern char dvi_oplen[]; +extern char dvi_dt[]; diff --git a/dviware/dvibook/libtex/dvicodes.h b/dviware/dvibook/libtex/dvicodes.h new file mode 100644 index 0000000000..4f93c35c89 --- /dev/null +++ b/dviware/dvibook/libtex/dvicodes.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* DVI opcodes */ + +#define DVI_VERSION 2 /* version number that should appear in + pre- and post-ambles */ + +#define DVI_SET1 128 /* set character, 1 byte param */ +#define DVI_SET2 129 /* set character, 2 byte param */ +#define DVI_SET3 130 /* set character, 3 byte param */ +#define DVI_SET4 131 /* set character, 4 byte param */ +#define DVI_SETRULE 132 /* set a rule */ +#define DVI_PUT1 133 /* put char, don't move right */ +#define DVI_PUT2 134 /* put char, 2 byte */ +#define DVI_PUT3 135 /* etc */ +#define DVI_PUT4 136 +#define DVI_PUTRULE 137 /* put rule, don't move right */ +#define DVI_NOP 138 /* no-op */ +#define DVI_BOP 139 /* begin page */ +#define DVI_EOP 140 /* end page */ +#define DVI_PUSH 141 /* push h,v,w,x,y,z */ +#define DVI_POP 142 /* pop h,v,w,x,y,z */ +#define DVI_RIGHT1 143 /* move right, 1 byte signed param */ +#define DVI_RIGHT2 144 /* move right, 2 byte signed param */ +#define DVI_RIGHT3 145 /* etc */ +#define DVI_RIGHT4 146 +#define DVI_W0 147 /* h += w */ +#define DVI_W1 148 /* w = 1 byte signed param, h += w */ +#define DVI_W2 149 /* w = 2 byte etc, h += w */ +#define DVI_W3 150 +#define DVI_W4 151 +#define DVI_X0 152 /* like DVI_W0 but for x */ +#define DVI_X1 153 /* etc */ +#define DVI_X2 154 +#define DVI_X3 155 +#define DVI_X4 156 +#define DVI_DOWN1 157 /* v += 1 byte signed param */ +#define DVI_DOWN2 158 /* v += 2 byte signed param */ +#define DVI_DOWN3 159 /* etc */ +#define DVI_DOWN4 160 +#define DVI_Y0 161 /* y = 1 byte signed param, v += y */ +#define DVI_Y1 162 /* etc */ +#define DVI_Y2 163 +#define DVI_Y3 164 +#define DVI_Y4 165 +#define DVI_Z0 166 /* z = 1 byte signed param, v += z */ +#define DVI_Z1 167 /* etc */ +#define DVI_Z2 168 +#define DVI_Z3 169 +#define DVI_Z4 170 +#define DVI_FNTNUM0 171 + +#define DVI_FNT1 235 /* select font, 1 byte param */ +#define DVI_FNT2 236 /* etc */ +#define DVI_FNT3 237 +#define DVI_FNT4 238 +#define DVI_XXX1 239 /* for \special: if length < 256 */ +#define DVI_XXX2 240 /* etc */ +#define DVI_XXX3 241 +#define DVI_XXX4 242 +#define DVI_FNTDEF1 243 /* Define font, 1 byte param (0 to 63) */ +#define DVI_FNTDEF2 244 /* etc */ +#define DVI_FNTDEF3 245 +#define DVI_FNTDEF4 246 +#define DVI_PRE 247 /* preamble */ +#define DVI_POST 248 /* postamble */ +#define DVI_POSTPOST 249 /* end of postamble */ +#define DVI_FILLER 223 /* filler bytes at end of dvi file */ diff --git a/dviware/dvibook/libtex/dvistate.c b/dviware/dvibook/libtex/dvistate.c new file mode 100644 index 0000000000..b11f3f10cc --- /dev/null +++ b/dviware/dvibook/libtex/dvistate.c @@ -0,0 +1,243 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/dvistate.c,v 1.1 89/08/22 21:49:03 chris Exp $"; +#endif + +/* + * dvistate - basic DVI machine interpreter + */ + +#include <stdio.h> +#include "types.h" +#include "conv.h" +#include "dvicodes.h" +#include "error.h" +#include "fio.h" +#include "font.h" +#include "gripes.h" +#include "postamble.h" +#include "search.h" +#include "seek.h" +#include "dvistate.h" + +struct dvi_state ds; + +/* yech, statics! */ +static struct font *(*s_deffnt)(); +static int s_dpi; +static int s_errs; + +static void pre(), post(), fntdef(); + +char *malloc(); + +/* + * Read through the postamble and the preamble and set up the + * global DVI state. Each font is passed to (*fontfn)(). + * Upon return, the input file (fp) is positioned at the beginning + * of the first page. + * + * dpi is the resolution of the printer in pixels (dots) per inch, + * and xoffset and yoffset are the values from the -X and -Y flags + * (default zero). We add 1000 to each offset (since the offsets + * are in 1/1000ths of an inch), and then multiply by dpi and divide + * by 1000, to get the actual offset. + * + * If a device introduces a constant offset, e.g., of .25 inch + * horizontally and .31 inch vertically, the caller can compensate + * by passing (xflag - 2500) and (yflag - 3100). + */ +void +DVISetState(fp, fontfn, dpi, xoffset, yoffset) + FILE *fp; + struct font *(*fontfn)(); + int dpi, xoffset, yoffset; +{ + + /* make sure fseek() will work */ + if ((ds.ds_fp = SeekFile(fp)) == NULL) + error(1, -1, "unable to copy input to temp file (disk full?)"); + + /* h and v must be deferred until we have a conversion */ + ds.ds_fresh.hh = ((1000 + (i32)xoffset) * dpi) / 1000; + ds.ds_fresh.vv = ((1000 + (i32)yoffset) * dpi) / 1000; + + ds.ds_fonts = SCreate(sizeof(struct font *)); + if (ds.ds_fonts == NULL) + error(1, 0, "cannot create font table (out of memory?)"); + s_errs = 0; + s_dpi = dpi; + s_deffnt = fontfn; + ScanPostAmble(ds.ds_fp, post, fntdef); + if (s_errs) + GripeMissingFontsPreventOutput(s_errs); + pre(); +} + +/* + * Read the preamble and apply sanity checks. + * The presence of a preamble is virtually certain, + * since we have already found and read a postamble. + */ +static void +pre() +{ + register int n; + register FILE *fp = ds.ds_fp; + + rewind(fp); + if (GetByte(fp) != Sign8(DVI_PRE)) + GripeMissingOp("PRE"); + if (GetByte(fp) != Sign8(DVI_VERSION)) + GripeMismatchedValue("version number"); + if (GetLong(fp) != ds.ds_num) + GripeMismatchedValue("numerator"); + if (GetLong(fp) != ds.ds_denom) + GripeMismatchedValue("denominator"); + if (GetLong(fp) != ds.ds_dvimag) + GripeMismatchedValue("\\magnification"); + n = UnSign8(GetByte(fp)); + while (--n >= 0) + (void) GetByte(fp); +} + +/* + * Store the relevant information from the DVI postamble. + */ +static void +post(p) + register struct PostAmbleInfo *p; +{ + register int n; + + ds.ds_prevpage = p->pai_PrevPagePointer; + ds.ds_num = p->pai_Numerator; + ds.ds_denom = p->pai_Denominator; + ds.ds_dvimag = p->pai_DVIMag; + ds.ds_maxheight = p->pai_TallestPageHeight; + ds.ds_maxwidth = p->pai_WidestPageWidth; + ds.ds_npages = p->pai_NumberOfPages; + + /* set the global conversion factor */ + SetConversion(s_dpi, + ds.ds_usermag, ds.ds_num, ds.ds_denom, ds.ds_dvimag); + + ds.ds_fresh.h = toSP(ds.ds_fresh.hh); + ds.ds_fresh.v = toSP(ds.ds_fresh.vv); +#ifdef unneeded /* counting on init-to-zero ... */ + ds.ds_fresh.w = 0; + ds.ds_fresh.x = 0; + ds.ds_fresh.y = 0; + ds.ds_fresh.z = 0; +#endif + /* make the stack */ + n = p->pai_DVIStackSize * sizeof(DviStack); + if ((ds.ds_stack = (struct dvi_stack *)malloc((unsigned)n)) == NULL) + GripeOutOfMemory(n, "DVI stack"); + ds.ds_sp = ds.ds_stack; +} + +/* + * Handle a font definition from the postamble. + */ +static void +fntdef(p) + register struct PostAmbleFont *p; +{ + register struct font *f, **fp; + int def = S_CREATE | S_EXCL; + + fp = (struct font **)SSearch(ds.ds_fonts, p->paf_DVIFontIndex, &def); + if (fp == NULL) { + if (def & S_COLL) + GripeFontAlreadyDefined(p->paf_DVIFontIndex); + else + error(1, 0, "cannot stash font %ld (out of memory?)", + (long)p->paf_DVIFontIndex); + /* NOTREACHED */ + } + + /* apply driver function to get font */ + f = (*s_deffnt)(p->paf_name, p->paf_DVIMag, p->paf_DVIDesignSize); + if (f == NULL) { + s_errs++; + return; + } + *fp = f; + + /* match checksums, unless one is (or both are) zero */ + if (p->paf_DVIChecksum && f->f_checksum && + p->paf_DVIChecksum != f->f_checksum) + GripeDifferentChecksums(f->f_path, p->paf_DVIChecksum, + f->f_checksum); +} + +/* + * Given a font index, return the corresponding pointer. + */ +struct font * +DVIFindFont(n) + i32 n; +{ + int x = S_LOOKUP; + char *p; + + if ((p = SSearch(ds.ds_fonts, n, &x)) == NULL) { + GripeNoSuchFont(n); + /* NOTREACHED */ + } + return (*(struct font **)p); +} + +/* + * Handle a rule: read width and height, then call the given function. + * If `advance', move dvi position afterwards. + */ +void +DVIRule(fn, advance) + void (*fn)(); + int advance; +{ + register i32 h, w, ww; + register FILE *fp = ds.ds_fp; + + fGetLong(fp, h); + fGetLong(fp, ww); + h = ConvRule(h); + w = ConvRule(ww); + (*fn)(h, w); + if (advance) { + dvi_hh += w; + dvi_h += ww; + w = fromSP(dvi_h); + FIXDRIFT(dvi_hh, w); + } +} + +/* + * Handle a BOP: read the ten \count values and the previous page pointer, + * reset DVI position, and call the given function. + */ +void +DVIBeginPage(fn) + void (*fn)(); +{ + register int i; + i32 count[10]; + + for (i = 0; i < 10; i++) + count[i] = GetLong(ds.ds_fp); + ds.ds_prevpage = GetLong(ds.ds_fp); + + /* reset DVI page position and stack pointer, then call driver */ + ds.ds_cur = ds.ds_fresh; + ds.ds_sp = ds.ds_stack; + + (*fn)(count); +} diff --git a/dviware/dvibook/libtex/dvistate.h b/dviware/dvibook/libtex/dvistate.h new file mode 100644 index 0000000000..c0b3b7b00c --- /dev/null +++ b/dviware/dvibook/libtex/dvistate.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* DVI file information (as used by dvi-to-device programs) */ + +/* + * Units of distance are stored in scaled points, but we can convert to + * units of 10^-7 meters by multiplying by the numbers in the preamble. + * The values hh and vv are kept in device coordinates. + */ + +/* the structure of the stack used to hold the values (h,v,hh,vv,w,x,y,z) */ +typedef struct dvi_stack { + i32 h; /* the saved h */ + i32 v; /* the saved v */ + i32 hh; /* the saved hh */ + i32 vv; /* the saved vv */ + i32 w; /* etc */ + i32 x; + i32 y; + i32 z; +} DviStack; + +/* + * DVI state machine interpreter encapsulation (global state). + */ +struct dvi_state { + /* + * Variables that must be set by user before calling SetDVIState(). + */ + int ds_usermag; /* user-supplied (additional) magnification */ + int ds_maxdrift; /* maximum drift, typically 3 */ + + /* the rest are set by DVISetState() */ + + /* + * Values based on the (postamble of the) DVI file, + * or used by the interpreter. + */ + i32 ds_num; /* numerator from DVI file */ + i32 ds_denom; /* denominator from DVI file */ + i32 ds_dvimag; /* magnification from DVI file */ + i32 ds_maxheight; /* height of tallest page */ + i32 ds_maxwidth; /* width of widest page */ + i32 ds_prevpage; /* previous page of DVI file */ + int ds_npages; /* number of DVI pages */ + DviStack *ds_stack; /* base of stack of DVI values */ + DviStack *ds_sp; /* current place in stack */ + DviStack ds_cur; /* current values */ + DviStack ds_fresh; /* fresh values for new pages: not all + zero unless there are no margins. + This works because all DVI motion + is relative. */ + /* + * Although this is not strictly within the realm of DVI + * state, we need a place to store the fonts we allocate + * while reading through the postamble. Each entry in + * the ds_fonts search table is a pointer to a font, + * so that *(struct font **)SSearch(...) is the pointer. + */ + struct search *ds_fonts;/* font table */ + + /* + * A few routines want a private copy of the DVI file pointer. + */ + FILE *ds_fp; /* input DVI file */ +}; + +extern struct dvi_state ds; + +/* shorthand */ +#define dvi_h ds.ds_cur.h +#define dvi_v ds.ds_cur.v +#define dvi_hh ds.ds_cur.hh +#define dvi_vv ds.ds_cur.vv +#define dvi_w ds.ds_cur.w +#define dvi_x ds.ds_cur.x +#define dvi_y ds.ds_cur.y +#define dvi_z ds.ds_cur.z + +/* + * Set up the state for the interpreter loop. + */ +void DVISetState(); + +/* + * Service routines (called from main loop). + */ +struct font *DVIFindFont(); +void DVIRule(), DVIBeginPage(); + +/* + * Drift fixing. Note that the first argument must be an lvalue. + * It is the desired position in pixels, and is maintained within + * ds.ds_maxdrift pixels of the second argument, which is the + * `absolute' DVI position rounded to the nearest pixel. + */ +#define FIXDRIFT(xx, x) \ + if ((x) >= (xx)) \ + if ((x) - (xx) <= ds.ds_maxdrift) \ + /* position is in bounds */; \ + else \ + (xx) = (x) - ds.ds_maxdrift; \ + else \ + if ((xx) - (x) <= ds.ds_maxdrift) \ + /* position is in bounds */; \ + else \ + (xx) = (x) + ds.ds_maxdrift diff --git a/dviware/dvibook/libtex/dvistuff.c b/dviware/dvibook/libtex/dvistuff.c new file mode 100644 index 0000000000..82549e66fd --- /dev/null +++ b/dviware/dvibook/libtex/dvistuff.c @@ -0,0 +1,1188 @@ +/* + * Copyright 1989 Dirk Grunwald + * + * Permission to use, copy, modify, distribute, and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and that + * both that copyright notice and this permission notice appear in + * supporting documentation, and that the name of Dirk Grunwald or M.I.T. + * not be used in advertising or publicity pertaining to distribution of + * the software without specific, written prior permission. Dirk + * Grunwald and M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + * DIRK GRUNWALD AND M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT + * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE + * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE + * OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: + * Dr. Dirk Grunwald + * Dept. of Computer Science + * Campus Box 430 + * Univ. of Colorado, Boulder + * Boulder, CO 80309 + * + * grunwald@colorado.edu + * + */ + +#include <stdio.h> +#include <ctype.h> + +#include "dvistuff.h" /* includes types & fonts */ + +static char *rcsid="$Header: /home/reed/grunwald/Iptex/drivers/RCS/dvistuff.c,v 1.17 88/07/07 23:25:32 grunwald Exp Locker: grunwald $" ; + +#include "dviclass.h" +#include "dvicodes.h" +#include "postamble.h" +#include "search.h" +#include "fio.h" + +int dviHH = -1; /* current horizontal position, in DEVs */ +int dviVV = -1; /* current vertical position, in DEVs */ +long *dviCount[DVI_COUNT]; /* values of count */ +DviStack dvi_current; +int dviHHMargin = -1; /* horizontal margin (in DEVs) */ +int dviVVMargin = -1; /* vertical margin (in DEVs) */ +int dviTallestPage = -1; +int dviWidestPage = -1 ; +int dviCurrentPage = -1; +int dviTotalPages = -1; +int dviDPI = -1; +int dviMaxDrift = -1; +int dviUserMag = -1; /* user-specified magnification */ +int dviBlackness = -1; +int dviFontRotation = -1; +char *DVIFileName; +static char *dviTmpFileName = 0; +static char dviTmpFileNameBuffer[256]; +FILE *dviFile; /* users file */ +char *dviPrintEngine = "canon"; + +char *ProgName; + +/* + * Similar to dvi_stack, but includes `dviHH and `dviVV, which are usually + * but not always the same as fromSP(h) and fromSP(v): + */ +struct localstack { + int stack_hh; + int stack_vv; + struct dvi_stack stack_dvi; +}; + +static struct localstack *dvi_stack = 0;/* base of stack */ +static struct localstack *dvi_stackp; /* current place in stack */ + +/* + * DVI preamble and postamble information. + */ + +static long Magnification; +static long Numerator; /* numerator from DVI file */ +static long Denominator; /* denominator from DVI file */ + +/* + * Font related things. + * We keep track of the current font, use the fontfiner + * and record errors in the dvi postable in FontErros. + * the NoFont is a used when we cant find a font, + * to avoid errors. + */ + +struct fontinfo *dviCurrentFont = 0; + +/* + * imagen1-special uses the NextFamilyNumber. *sigh* + */ + +int NextFamilyNumber = 0; +static int MaxFontFamily = MAX_FONTFAMILY; + +static struct search *FontFinder = 0; +int FontErrors = 0; +static struct fontinfo NoFont; +/* + * We keep a list of pointers to the beginning of pages. + * Each page is headed by a DVI_BOP + 44 bytes of information. + */ + +static long *pageOffset = 0; + +#undef FIXDRIFT +/* + * Correct devpos (the actual device position) to be within MaxDrift pixels + * of dvipos (the virtual DVI position). + */ +#define FIXDRIFT(devpos, dvipos) \ + if (ABS((devpos) - (dvipos)) <= dviMaxDrift) \ + /* void */; \ + else \ + if ((devpos) < (dvipos)) \ + (devpos) = (dvipos) - dviMaxDrift; \ + else \ + (devpos) = (dvipos) + dviMaxDrift + +#define ABS(X) ( (X) < 0 ? -(X) : (X) ) + + +/* + * Store the relevant information from the DVI postamble, and set up + * various internal things. + */ +static void +PostAmbleHeader(p) + register struct PostAmbleInfo *p; +{ + int i; + int page; + int pageSize; + int stackSize; + long prevPagePointer; + long wuzAt; + + Numerator = p->pai_Numerator; + Denominator = p->pai_Denominator; + Magnification = p->pai_DVIMag; + +/* + * Set the conversion factor. This must be done before using + * any fonts or the fromSP routine. + */ + + SetConversion(dviDPI, dviUserMag, Numerator, Denominator, Magnification); + + dviTotalPages = p -> pai_NumberOfPages; + dviWidestPage = fromSP(p -> pai_WidestPageWidth); + dviTallestPage = fromSP(p -> pai_TallestPageHeight); + +/* + * Set up the DVI stack + */ + stackSize = p -> pai_DVIStackSize * sizeof(*dvi_stack); + if (dvi_stack != 0) { + free(dvi_stack); + } + dvi_stack = (struct localstack *) malloc((unsigned) stackSize); + +/* + * Set of the table of pointers to pages and room for the + * count variables. + */ + + pageSize = dviTotalPages * sizeof(long); + if (pageOffset != 0) { + free(pageOffset); + } + pageOffset = (long *) malloc(pageSize); + + if (pageOffset == NULL) { + fprintf(stderr,"xdvi: Can not allocate page directory (%d pages)", + dviTotalPages); + exit(1); + } + + for (i = 0; i < DVI_COUNT; i++) { + if (dviCount[i] != 0) { + free(dviCount[i]); + } + dviCount[i] = (long*) malloc(pageSize); + if (dviCount[i] == NULL) { + fprintf(stderr,"xdvi: Can not allocate count directory (%d pages)", + dviTotalPages); + exit(1); + } + } + + +/* + * Follow back pointers through pages in the DVI file, + * storing the offsets in the pageOffset table. + */ + + prevPagePointer = p->pai_PrevPagePointer; + wuzAt = (long) ftell(dviFile); + + for (page = dviTotalPages - 1; page >= 0 ; page--) { + +/* + * Skip to previous page, but skip over BOP marker. + */ + pageOffset[page] = prevPagePointer; + if( fseek(dviFile, prevPagePointer+1, 0) < 0) { + perror("fseek"); + fprintf(stderr,"[postamble] improper seek looking up pages\n"); + fprintf(stderr,"prevPagePointer = %lx\n", + prevPagePointer); + exit(1); + } + +/* + * Read the other counters at the beginning of the page + */ + + for (i = 0; i < DVI_COUNT; i++) { + long value; + fGetLong(dviFile, value); + dviCount[i][page] = value; + } + fGetLong(dviFile, prevPagePointer); + } + fseek(dviFile, wuzAt, 0); +} + +static void +PostAmbleFontDef(p) +register struct PostAmbleFont *p; +{ + register struct fontinfo *fi; + register struct font *f; + + char *fname; + int def = S_CREATE | S_EXCL; + + fi = (struct fontinfo *) SSearch(FontFinder, p->paf_DVIFontIndex, + &def); + if (fi == NULL) { +/* + * If the font already exists, lets just check that its really the + * same thing & return + */ + + if ((def & S_FOUND) | (def & S_COLL)) { + + def = S_LOOKUP; + if ((fi = (struct fontinfo *) SSearch(FontFinder, + p -> paf_DVIFontIndex, + &def)) == 0){ + GripeNoSuchFont(p -> paf_DVIFontIndex); + exit(1); + } + + if (fi -> f == NULL) { + GripeCannotGetFont(p->paf_name, p->paf_DVIMag, + p->paf_DVIDesignSize, + dviPrintEngine, fname); + } + f = fi -> f; + if (p->paf_DVIChecksum && f->f_checksum && + p->paf_DVIChecksum != f->f_checksum) { + GripeDifferentChecksums(fname, p->paf_DVIChecksum, + f->f_checksum); + exit(1); + } + return; /* font already defined */ + } + else { + fprintf(stderr, "Can not stash font %ld (out of memory?)\n", + p -> paf_DVIFontIndex); + exit(1); + } + } + + if (NextFamilyNumber == MaxFontFamily) { + fprintf(stderr,"Out of font family!\n"); + exit(1); + } + + fi->family = NextFamilyNumber; + NextFamilyNumber++; + + f = GetFont(p->paf_name, p->paf_DVIMag, p->paf_DVIDesignSize, + dviPrintEngine, &fname); + +/* + * Give the application a chance to bang on this if they want to. + */ + + f = applicationNewFont(f, fi -> family); + + if ((fi->f = f) == NULL) { + GripeCannotGetFont(p->paf_name, p->paf_DVIMag, + p->paf_DVIDesignSize, dviPrintEngine, fname); + FontErrors++; + return; + } + + /* match checksums, if not zero */ + if (p->paf_DVIChecksum && f->f_checksum && + p->paf_DVIChecksum != f->f_checksum) + GripeDifferentChecksums(fname, p->paf_DVIChecksum, + f->f_checksum); + + fi->pspace = p->paf_DVIMag / 6; /* a three-unit "thin space" */ + fi->nspace = -4 * fi->pspace; + fi->vspace = 5 * fi->pspace; +} + +static void +ReadPostAmble() +{ + + if (FontFinder == 0) { + if ((FontFinder = SCreate(sizeof(struct fontinfo))) == 0) { + fprintf(stderr, "can not create FontFinder (out of memory?)"); + exit(1); + } + } + + ScanPostAmble(dviFile, PostAmbleHeader, PostAmbleFontDef); + + if (FontErrors) + GripeMissingFontsPreventOutput(FontErrors); +} + +/* + * Read the preamble and do a few sanity checks + */ + +static void +ReadPreAmble() +{ + register int n; + + rewind(dviFile); + if (GetByte(dviFile) != Sign8(DVI_PRE)) + GripeMissingOp("PRE"); + + if (GetByte(dviFile) != Sign8(DVI_VERSION)) + GripeMismatchedValue("version numbers"); + + if (GetLong(dviFile) != Numerator) + GripeMismatchedValue("numerator"); + + if (GetLong(dviFile) != Denominator) + GripeMismatchedValue("denominator"); + + if (GetLong(dviFile) != Magnification) + GripeMismatchedValue("\\magfactor"); + + n = UnSign8(GetByte(dviFile)); + while (--n >= 0) + (void) GetByte(dviFile); +} + +int +dviInit() +{ + extern char *ProgName; + FILE *tmpFile; + char *mktemp(); + + dviFini(); /* clean up the old files */ + + if (DVIFileName == 0) { + dviFile = stdin; + DVIFileName = "<stdin>"; + }else if ((dviFile = fopen(DVIFileName, "r")) == NULL) { + int n = strlen(DVIFileName); + char *dvi_name; + + if (strcmp(DVIFileName + n - sizeof(".dvi") + 1, ".dvi") == 0) { + perror(DVIFileName); + return(1); + } + + dvi_name = (char *) malloc((unsigned) n + sizeof(".dvi")); + sprintf(dvi_name, "%s.dvi", DVIFileName); + + if ((dviFile = fopen(dvi_name, "r")) == NULL) { + perror(dvi_name); + return(1); + } + DVIFileName = dvi_name; + } + +/* + * node, copy the file to a temporary location. This lets the person + * peruse the file while theyre re-texing it + */ + strcpy(dviTmpFileNameBuffer,"/tmp/dvistuff.XXXXXX"); + dviTmpFileName = mktemp(dviTmpFileNameBuffer); + + if (!(tmpFile = fopen(dviTmpFileName,"w+"))) { + perror("fopen"); + fprintf(stderr,"Unable to create temporary file"); + dviTmpFileName = 0; + } else { + char buffer[BUFSIZ]; + int b; + + rewind(dviFile); + do { + b = fread(buffer, 1, BUFSIZ, dviFile); + fwrite(buffer, 1, b, tmpFile); + } while (! feof(dviFile)); + + fclose(dviFile); + dviFile = tmpFile; + rewind(dviFile); + } +/* + * Unlink the temporary file. This keeps tmp files from cluddering + * up /tmp and it does it in a very application-independent way. + * You can't reopen the tmp file, but we don't really allow that + * anyway (the tmp file is hidden from the user). + */ + if (dviTmpFileName != 0 && + strncmp(dviTmpFileName,"/tmp/",5) == 0) { + unlink(dviTmpFileName); + } + + if (dviUserMag == -1) { + dviUserMag = 1000; + } + + if (dviMaxDrift == -1) { + dviMaxDrift = DEFAULT_MAX_DRIFT; + } + + if (dviDPI == -1) { + dviDPI = DEFAULT_DPI; + } + + if (dviBlackness == -1) { + dviBlackness = DEFAULT_BLACKNESS; + } + + if (dviFontRotation == -1) { + dviFontRotation = ROT_NORM; + } + + ReadPostAmble(); + ReadPreAmble(); + + /* Margins -- needs work! */ + + if (dviHHMargin == -1) { + dviHHMargin = DEFAULT_HHMARGIN; + } + + if (dviVVMargin == -1) { + dviVVMargin = DEFAULT_VVMARGIN; + } + + dviCurrentFont = &NoFont; /* fake font, all zeros */ + + return(0); +} + +void +dviFini() +{ + if (dviFile != 0) { + fclose(dviFile); + } + + if (dviTmpFileName != 0 && + strncmp(dviTmpFileName,"/tmp/",5) == 0) { + unlink(dviTmpFileName); + } +} + + +static void +SelectFont(n) +i32 n; +{ + int x = S_LOOKUP; + + if ((dviCurrentFont = (struct fontinfo *)SSearch(FontFinder, n, &x)) == 0) + GripeNoSuchFont(n); +} + +/* Read the postamble. */ + +static void +doDviChar(c, advance) +int c; +int advance; +{ + register struct glyph *g; + register struct font *f; + int p; + + f = dviCurrentFont->f; + + if (c < f -> f_lowch || c > f -> f_highch) { + fprintf(stderr,"Character out of range: %d\n", + c); + return; + } + + g = GLYPH(f, c); + if (!GVALID(g)) { + fprintf(stderr, "there is no character %d in %s", + c, f->f_path); + exit(1); + } else { + + if (HASRASTER(g)) { /* workaround for Imagen bug */ + applicationPutChar(dviHH,dviVV,c); + } + + if (advance) { + dviHH += g->g_pixwidth; + dvi_h += g->g_tfmwidth; + p = fromSP(dvi_h); + FIXDRIFT(dviHH, p); + } + } +} + +/* + * The next two routines are used to reset all the glyphs. + * the dviResetFont routine is called for each font in the font table. + * It de-allocates the rasters & glyphs for the shrunken fonts, leaving + * the original glyph data untouched. + */ + + +static void +dviResetFont(fi, key) +struct fontinfo *fi; +int key; +{ + if (fi != 0) { + applicationResetFont(fi, fi -> family); + } +} + +void +dviResetAll() +{ + SEnumerate(FontFinder, dviResetFont); +} + +/* + * Process the DVI commands for page #page. If the page number is + * out of range, it is cropped to the end. + * + * Updates dviCurrentPage + */ +void +dviPreparePage(page) +int page; +{ + register int c; + register i32 p; + int advance; + + if (page < 0) { + page = 0; + } + + if (page >= dviTotalPages) { + page = dviTotalPages - 1; + } + + dviCurrentPage = page; + + fseek(dviFile, (long) pageOffset[page], 0); + + dviHH = dviHHMargin; + dviVV = dviVVMargin; + + dvi_h = toSP(dviHH); + dvi_v = toSP(dviVV); + + dvi_w = 0; + dvi_x = 0; + dvi_y = 0; + dvi_z = 0; + + dvi_stackp = dvi_stack; + + for(;;) { +/* + * Get the DVI byte, and switch on its parameter length and type. + * Note that getchar() returns unsigned values. + */ + + if (feof(dviFile)) { + fprintf(stderr,"Unexpected end-of-file\n"); + exit(1); + } + + c = getc(dviFile); + + if (DVI_IsChar(c)) { + advance = 1; + doDviChar(c, advance); + } else { + + switch (DVI_OpLen(c)) { + + case DPL_NONE: + break; + + case DPL_SGN1: + p = getc(dviFile); + p = Sign8(p); + break; + + case DPL_SGN2: + fGetWord(dviFile, p); + p = Sign16(p); + break; + + case DPL_SGN3: + fGet3Byte(dviFile, p); + p = Sign24(p); + break; + + case DPL_SGN4: + fGetLong(dviFile, p); + break; + + case DPL_UNS1: + p = UnSign8(getc(dviFile)); + break; + + case DPL_UNS2: + fGetWord(dviFile, p); + p = UnSign16(p); + break; + + case DPL_UNS3: + fGet3Byte(dviFile, p); + p = UnSign24(p); + break; + + default: + panic("DVI_OpLen(%d) = %d", c, DVI_OpLen(c)); + /* NOTREACHED */ + } + + switch (DVI_DT(c)) { + + case DT_SET: + c = p; + doDviChar(c, 1); + break; + + case DT_PUT: + c = p; + doDviChar(c, 0); + break; + + case DT_SETRULE: + case DT_PUTRULE: + + { + i32 h, w, rw; + + fGetLong(dviFile, h); + fGetLong(dviFile, rw); + + h = ConvRule(h); + w = ConvRule(rw); + + applicationSetRule(dviHH, dviVV, h,w); + + if (DVI_DT(c) == DT_SETRULE) { + dviHH += w; + dvi_h += rw; + w = fromSP(dvi_h); + FIXDRIFT(dviHH, w); + } + } + break; + + case DT_NOP: + break; + + case DT_BOP: +/* + * Each beginning of page has 11 4-byte words telling us things + * about the page. We ignore them. + */ + { + fseek(dviFile, (long) (11 * 4), 1); + } + + break; + + case DT_EOP: + return; + + case DT_PUSH: + dvi_stackp->stack_hh = dviHH; + dvi_stackp->stack_vv = dviVV; + dvi_stackp->stack_dvi = dvi_current; + dvi_stackp++; + break; + + case DT_POP: + dvi_stackp--; + dviHH = dvi_stackp->stack_hh; + dviVV = dvi_stackp->stack_vv; + dvi_current = dvi_stackp->stack_dvi; + break; + + case DT_W0: /* there should be a way to make these pretty */ + p = dvi_w; + goto move_right; + + case DT_W: + dvi_w = p; + goto move_right; + + case DT_X0: + p = dvi_x; + goto move_right; + + case DT_X: + dvi_x = p; + goto move_right; + + case DT_RIGHT: + move_right: + dvi_h += p; +/* + * DVItype tells us that we must round motions in this way: + * `When the horizontal motion is small, like a kern, hh + * changes by rounding the kern; but when the motion is + * large, hh changes by rounding the true position so that + * accumulated rounding errors disappear. + */ + if (p >= dviCurrentFont->pspace || p <= dviCurrentFont->nspace) + dviHH = fromSP(dvi_h); + else { + dviHH += fromSP(p); + p = fromSP(dvi_h); + FIXDRIFT(dviHH, p); + } + break; + + case DT_Y0: + p = dvi_y; + goto move_down; + + case DT_Y: + dvi_y = p; + goto move_down; + + case DT_Z0: + p = dvi_z; + goto move_down; + + case DT_Z: + dvi_z = p; + goto move_down; + + case DT_DOWN: + move_down: + dvi_v += p; +/* + * `Vertical motion is done similarly, but with the threshold + * between ``small and ``large increased by a factor of + * 5. The idea is to make fractions like $1\over2$ round + * consistently, but to absorb accumulated rounding errors in + * the baseline-skip moves. + */ + if (ABS(p) >= dviCurrentFont->vspace) + dviVV = fromSP(dvi_v); + else { + dviVV += fromSP(p); + p = fromSP(dvi_v); + FIXDRIFT(dviVV, p); + } + break; + + case DT_FNTNUM: + SelectFont((i32) (c - DVI_FNTNUM0)); + break; + + case DT_FNT: + SelectFont(p); + break; + + case DT_XXX: + { + char specialBuffer [2048]; + register char *cp; + int sweetp = 0; + + if (p > 2047) { + sweetp = p - 2047; + p = 2047; + } + + for (cp = specialBuffer ; p > 0; p--) { + *cp = getc(dviFile); + cp++; + } + *(cp) = 0; + + while(sweetp > 0) { + getc(dviFile); + } + + applicationDoSpecial(specialBuffer); + } + break; + + case DT_FNTDEF: + SkipFontDef(dviFile); + break; + + case DT_PRE: + GripeUnexpectedOp("PRE"); + /* NOTREACHED */ + + case DT_POST: + GripeUnexpectedOp("POST"); + /* NOTREACHED */ + + case DT_POSTPOST: + GripeUnexpectedOp("POSTPOST"); + /* NOTREACHED */ + + case DT_UNDEF: + GripeUndefinedOp(c); + /* NOTREACHED */ + + default: + panic("DVI_DT(%d) = %d", c, DVI_DT(c)); + /* NOTREACHED */ + } + } + } +} + +/* Transpose bytes in each pair of bytes (0123 -> 1032) */ +/* Reverse bits in a byte (01234567 -> 76543210) */ +/* needed for sun386i */ +/* Gerhard Fleischanderl & Wolfgang Nejdl */ + +#ifdef sun386 +static unsigned char pow2[]={1,2,4,8,16,32,64,128}; + +unsigned char reverseByte(oldByte) + unsigned char oldByte; +{ + unsigned short i; + unsigned char newByte=0; + + /* build newByte with bits mirrored */ + + for(i=0;i<8;i++) + if (oldByte & pow2[i]) + newByte |= pow2[7-i]; + + return newByte; +} + +#ifdef UNDEF +#define transposeBytesMirrorBits(pairPtr) \ +/* B16 *pairPtr; */ \ +/* B16 comprises 2 bytes */ \ +{ \ + unsigned char c0,c1, *bP = (unsigned char *) pairPtr; \ + \ + /* transpose neighbouring bytes and \ + reverse bits in each byte */ \ + \ + c0 = *bP; \ + c1 = *(bP+1); \ + *bP = ReverseByteTable[c1]; \ + *(bP+1) = ReverseByteTable[c0]; \ +} + +#else +#define transposeBytesMirrorBits(ptr) {*ptr = ReverseByteTable[ (*ptr)&0xff ];} +#endif /* undef */ + +#endif + + +/* + * The following routine is used to shrink a glyph by some + * shrink factor (in the width & height dimension). + * + * These shrunken glyphs are more appropriate for previewers. + * + * To do this, we simple scan the original raster, counting the + * number of pixels within a given area. If the number of on pixels is + * at least twice the total number of pixels, we turn the pixel in + * the shrunken glyph ON, else OFF. + * + * We use a lookup table to hasten the process of counting pixels. + * + * The method used should be independent of byte-order (I think). + * + * You need to define two types. One should be 32-bits long, and + * the other 16 bits long. + */ + +typedef unsigned char B8; +typedef unsigned short B16; +typedef unsigned long B32; + +#define LOOKUP_BYTES 256 +#define LOOKUP_BITS 8 +#define LOOKUP_MASK 0xff + +static char dviLookUpTable[LOOKUP_BYTES]; +static char tableNotInitialized = 1; + +#ifdef sun386 +static unsigned char ReverseByteTable[LOOKUP_BYTES]; +#endif + +struct glyph * +dviShrinkGlyph(gly, shrinkH, shrinkW) +DviGlyph *gly; +int shrinkH; +int shrinkW; +{ + int shrunkHeight; + int shrunkWidth; + int glyphWide; + int glyphHigh; + int glyphAdvance; + int shrunkAdvance; + + int bytesWide; + int shrunkBytesWide; + + struct glyph *ngly; + + B16 *shrunkRaster; + int rasterSize; + + int x,y; + B8 *cp; + B8 *ptr; + + B32 shrinkMask; + + int sampleSize; + + if (gly == 0 || !HASRASTER(gly)) { + return(0); + } + + if (gly -> g_raster == 0) { + gly-> g_raster = RASTER(gly, dviCurrentFont -> f, dviFontRotation); + } + +/* + * Initialize the lookup table of the number of bits in a given byte + */ + + if (tableNotInitialized) { + register int i; + register int j; + register int k; + register int acc; + for (i = 0; i < LOOKUP_BYTES; i++) { + j = i; + acc = 0; + for (k = 0; j != 0 && k < LOOKUP_BITS ; k++) { + acc += (j & 1); + j >>= 1; + } + dviLookUpTable[i] = acc; + } + +#ifdef sun386 + for (i = 0; i < LOOKUP_BYTES; i++) { + ReverseByteTable[i] = reverseByte(i); + } +#endif + + tableNotInitialized = 0; + } + +/* + * When shrinking the fonts, we convert them from byte alligned + * to 16 bit-aligned fonts. I think that this is needed by X, + * and also SunWindows. This is done rather than changing the + * library routines because some device drivers, i.e. imagen1, + * depend on the byte-alligned fonts. + */ + glyphHigh = gly -> g_height; + glyphWide = gly -> g_width; + + gly -> g_pixwidth = fromSP(gly -> g_tfmwidth); + glyphAdvance = gly -> g_pixwidth; + + shrunkHeight = (glyphHigh + shrinkH - 1) / shrinkH; + shrunkWidth = (glyphWide + shrinkW - 1) / shrinkW; + shrunkAdvance = (glyphAdvance + shrinkW - 1) / shrinkW; + + bytesWide = (gly -> g_width + 7) >> 3; + + shrunkBytesWide = ((shrunkWidth + 15) >> 4) * 2; + + rasterSize = (shrunkHeight + 1) * shrunkBytesWide; + shrunkRaster = (B16 *) malloc(rasterSize); + bzero(shrunkRaster, rasterSize); + ptr = (B8 *) shrunkRaster; + + if (shrunkRaster == NULL) { + fprintf(stderr, "Out of memory!\n"); + exit(1); + } + + for (y = 0; y < glyphHigh; y+= shrinkH) { + cp = (B8 *) ptr; + shrinkMask = 0x80; + for (x = 0; x < glyphWide; x += shrinkW) { + int i; + int samples; + B8 *baseP; + int upper; + register int thisShrinkW; + + baseP = (B8 *) gly -> g_raster + (y * bytesWide); + +/* + * Set the upper limit on the height iteration so we dont count + * off the end of the raster + */ + + upper = y + shrinkH; + if (upper > glyphHigh) { + upper = glyphHigh; + } + + if (x + shrinkW > glyphWide) { + thisShrinkW = glyphWide - x; + } else { + thisShrinkW = shrinkW; + } + + samples = 0; + sampleSize = thisShrinkW * (upper - y); + + for (i = y; i < upper; i++) { + register int acc; + register B8 *p; + register B8 *ep; +/* + * Determine how many bytes our shrink window crosses (we might + * overlap on byte-edges) + */ + + p = baseP + (x >> 3); + ep = baseP + ( (x + thisShrinkW - 1) >> 3); + baseP += bytesWide; + +/* + * stuff everything in the accumulator + */ + + acc = 0; + while (p <= ep) { + acc = ((acc << 8) & ~0xff) | *p; + p++; + } + +/* + * clean off the right hand-side of extra bits, then clean off + * the left hand side of extra bits, and then count them. + */ + + acc = acc >> ( 7 - ((x + thisShrinkW - 1) & 0x7)); + acc &= ~(-1 << thisShrinkW); + while (acc != 0) { + samples += dviLookUpTable[ acc & LOOKUP_MASK ]; + acc >>= LOOKUP_BITS; + } + } +/* + * If at least 1/blackness of the bits are on, treat this entire sample as + * being on. + */ + + if ((samples * dviBlackness) >= sampleSize) { + *ptr |= shrinkMask; + } else { + *ptr &= ~ shrinkMask; + } + shrinkMask >>= 1; + if (shrinkMask == 0) { + shrinkMask = 0x80; +#ifdef sun386 + transposeBytesMirrorBits(ptr) +#endif + ptr ++; + } + } +#ifdef sun386 + transposeBytesMirrorBits(ptr) +#endif + ptr = (B8 *) (cp + shrunkBytesWide); + } + +/* + * Build a new glyph from the shrunken raster + */ + +#ifdef UNDEF + if ( shrunkBytesWide != 2 ) + { + printf("Old glyph:\n"); + seeGlyph(gly -> g_raster, glyphHigh, bytesWide); + printf("%d ", shrunkBytesWide); + printf("New glyph:\n"); + seeGlyph(shrunkRaster, shrunkHeight, shrunkBytesWide); + } +#endif UNDEF + + ngly = (struct glyph *) malloc(sizeof(struct glyph)); + bzero(ngly, sizeof(struct glyph)); + + ngly -> g_raster = (char * ) shrunkRaster; + ngly -> g_width = shrunkWidth; + ngly -> g_pixwidth = shrunkAdvance; + ngly -> g_height = shrunkHeight; + + ngly -> g_xorigin = gly -> g_xorigin / shrinkH; + ngly -> g_yorigin = gly -> g_yorigin / shrinkW; + + ngly -> g_flags |= GF_SHRUNK; /* yes, its been shrunk */ + + return(ngly); +} + +#ifdef UNDEF + +seeGlyph(c, h, w) +char *c; +int h; +int w; +{ + int i,j; + + for (i = 0; i < h; i++ ) { + for (j = 0; j < w; j++) { + int k; + register int ch; + register int m; + char str[9]; + + ch = *(c++); + m = 0x80; + for (k = 0; k < 8; k++) { + str[k] = '"' + ( (ch & m) ? 1 : 0 ); + m >>= 1; + } + str[8] = 0; + printf("%s", str); + } + printf("\n"); + } +} + + +#endif UNDEF diff --git a/dviware/dvibook/libtex/dvistuff.h b/dviware/dvibook/libtex/dvistuff.h new file mode 100644 index 0000000000..040e02bdac --- /dev/null +++ b/dviware/dvibook/libtex/dvistuff.h @@ -0,0 +1,145 @@ +/* + * Copyright 1989 Dirk Grunwald + * + * Permission to use, copy, modify, distribute, and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and that + * both that copyright notice and this permission notice appear in + * supporting documentation, and that the name of Dirk Grunwald or M.I.T. + * not be used in advertising or publicity pertaining to distribution of + * the software without specific, written prior permission. Dirk + * Grunwald and M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" without express + * or implied warranty. + * + * DIRK GRUNWALD AND M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT + * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE + * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE + * OR PERFORMANCE OF THIS SOFTWARE. + * + * Author: + * Dr. Dirk Grunwald + * Dept. of Computer Science + * Campus Box 430 + * Univ. of Colorado, Boulder + * Boulder, CO 80309 + * + * grunwald@colorado.edu + * + */ + +static char *dvistuff_header_rcsid="$Header: /home/reed/grunwald/Iptex/drivers/RCS/dvistuff.h,v 1.11 89/02/15 16:31:56 grunwald Exp $" ; + +/* + * Include the following from the CTex distribution + */ + +#include <stdio.h> + +#include "types.h" +#include "font.h" +#include "conv.h" + +/* + * We use the following flags to keep track of shrunk fonts + */ + +#define GF_SHRUNK GF_USR0 + +#define GLYPH_SHRUNK(g) ((g) -> g_flags & GF_SHRUNK) + +#define SHRUNK_GLYPH_BYTES_WIDE(g) ((((g) -> g_width + 15) >> 4) * 2) + +typedef struct font DviFont; +typedef struct glyph DviGlyph; + +#define MAX_GLYPH 128 +#define MAX_FONTFAMILY 128 + +#define DEFAULT_DPI 300 +#define DEFAULT_HHMARGIN DEFAULT_DPI +#define DEFAULT_VVMARGIN DEFAULT_DPI +#define DEFAULT_MAX_DRIFT 3 + +#define DEFAULT_BLACKNESS 3 + + +struct fontinfo { + struct font *f; /* the font */ + i32 pspace; /* boundary between `small & `large spaces + (for positive horizontal motion) */ + i32 nspace; /* -4 * pspace, for negative motion */ + i32 vspace; /* 5 * pspace, for vertical motion */ + int family; /* Imagen family number (we pick one) */ +}; + +typedef struct fontinfo DviFontInfo; + +/* DVI file info */ + +/* + * Units of distance are stored in scaled points, but we can convert to + * units of 10^-7 meters by multiplying by the numbers in the preamble. + */ + +/* the structure of the stack used to hold the values (h,v,w,x,y,z) */ + +typedef struct dvi_stack { + i32 h; /* the saved h */ + i32 v; /* the saved v */ + i32 w; /* etc */ + i32 x; + i32 y; + i32 z; +} DviStack; + +extern DviStack dvi_current; /* the current values of h, v, etc */ +extern int dvi_f; /* the current font */ + +#define dvi_h dvi_current.h +#define dvi_v dvi_current.v +#define dvi_w dvi_current.w +#define dvi_x dvi_current.x +#define dvi_y dvi_current.y +#define dvi_z dvi_current.z + + + +extern int dviHH; /* current horizontal position, in DEVs */ +extern int dviVV; /* current vertical position, in DEVs */ +#define DVI_COUNT 10 /* count0 .. count9 */ +extern long *dviCount[DVI_COUNT]; /* values of count */ + +extern Conv Conversion; + +extern int dviHHMargin; +extern int dviVVMargin; +extern int dviDPI; +extern int dviTallestPage; +extern int dviWidestPage; +extern int dviTotalPages; +extern int dviCurrentPage; +extern int dviMaxDrift; +extern char *dviPrintEngine; +extern int dviBlackness; +extern int dviFontRotation; +extern int dviUserMag; + +extern DviFontInfo *dviCurrentFont; +extern char *DVIFileName; +extern FILE *dviFile; /* user's file */ +extern char *ProgName; + +extern int dviInit(); +extern void dviFini(); +extern void dviPreparePage( /* int */ ); +extern struct glyph *dviShrinkGlyph(); + +void applicationPutChar( /* hh, vv, charCode */ ); +DviFont *applicationNewFont( /* font structure */); +void applicationResetFont(/* font info, key */); +void applicationSetRule( /* hh, vv, height, width : long*/ ); +void applicationDoSpecial(); diff --git a/dviware/dvibook/libtex/error.c b/dviware/dvibook/libtex/error.c new file mode 100644 index 0000000000..7d745108b8 --- /dev/null +++ b/dviware/dvibook/libtex/error.c @@ -0,0 +1,197 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/error.c,v 2.8 89/08/22 21:49:12 chris Exp $"; +#endif + +/* + * Print an error message with an optional system error number, and + * optionally quit. + * + * THIS CODE IS SYSTEM DEPENDENT UNLESS varargs WORKS WITH vprintf + * OR _doprnt. It should work properly under System V using vprintf. + * (If you have vprintf, define HAVE_VPRINTF in ../h/types.h.) + */ + +#include "types.h" /* for HAVE_VPRINTF */ +#include "error.h" +#include <stdio.h> +#include <varargs.h> + +#if defined(lint) && !defined(LINT_ANYWAY) + +/* ARGSUSED */ +void SetErrorTrap(fn) void (*fn)(); {;} + +/* VARARGS3 ARGSUSED */ +void error(quit, e, fmt) int quit, e; char *fmt; {;} + +/* VARARGS1 ARGSUSED */ +void panic(fmt) char *fmt; { exit(1); /* NOTREACHED */ } + +#else lint + +extern char *ProgName; /* program name from argv[0] */ +extern int errno; /* Unix system-call error */ +extern char *sys_errlist[]; /* table of error number => string */ +extern int sys_nerr; /* size of table */ + +static FILE *trap_file; /* error diversion file, if any */ +static void (*trap_fn)(); /* trap function */ +static char *trap_buf; /* buffer for trapped error strings */ +static int trap_size; /* size of trap_buf */ + +extern char *malloc(), *realloc(); + +/* + * Enable error trapping: register the function fn as the trapper. + * If fn is NULL, stop trapping. + */ +void +SetErrorTrap(fn) + void (*fn)(); +{ + int tempfd; + char fname[BUFSIZ]; + + /* shut down any existing error trap */ + if (trap_file) { + (void) fclose(trap_file); + trap_file = NULL; + } + if ((trap_fn = fn) == NULL) + return; + /* begin trapping */ + if ((tempfd = MakeRWTempFile(fname)) < 0) + error(1, -1, "cannot create temporary file %s", fname); + if (trap_size == 0) { + trap_buf = malloc((unsigned)(trap_size = 1000)); + if (trap_buf == 0) + error(1, -1, + "cannot get space for error buffer"); + } + if ((trap_file = fdopen(tempfd, "r+")) == NULL) + error(1, -1, "cannot get stdio file for error trap"); +} + +/* + * Read a trapped error into trap_buf. + * Return a pointer to the (NUL-terminated) text. + * If something goes wrong, return something else printable. + */ +static char * +readback() +{ + int nbytes = ftell(trap_file) + 1; + + if (nbytes > trap_size) { + if (trap_buf == NULL) + trap_buf = malloc((unsigned)nbytes); + else + trap_buf = realloc(trap_buf, (unsigned)nbytes); + if (trap_buf == NULL) { + trap_size = 0; + return ("Ouch! Lost error text: out of memory?"); + } + } + rewind(trap_file); /* now can read */ + nbytes = fread(trap_buf, 1, nbytes, trap_file); + if (nbytes < 0) + return ("Ouch! Trouble reading error text!"); + trap_buf[nbytes] = 0; + return (trap_buf); +} + +/* + * Print an error message to the error output (either stderr, or + * if trapping errors, to the error trap file). We precede this + * with the program's name and an optional string (a0), then use + * the format and variable argument list, then append an optional + * Unix error string. Finally, if errors are being trapped, we + * pass the error text and the quit flag to the trap function. + * + * In the interest of `look and feel', if errors are being trapped, + * the program name is omitted. + */ +static void +verror(quit, a0, fmt, l, e) + char *a0, *fmt; + va_list l; + int e; +{ + register FILE *fp = trap_file; + + /* print to the trap file, if any, else stderr */ + if ((fp = trap_file) != NULL) + rewind(fp); /* now can write */ + else { + fp = stderr; + (void) fflush(fp); + } + if (trap_file == NULL) + (void) fprintf(fp, "%s: ", ProgName); + if (a0) + (void) fprintf(fp, "%s", a0); +#ifdef HAVE_VPRINTF + (void) vfprintf(fp, fmt, l); +#else + (void) _doprnt(fmt, l, fp); +#endif + if (e) { + if (e < sys_nerr) + (void) fprintf(fp, ": %s", sys_errlist[e]); + else + (void) fprintf(fp, ": Unknown error code %d", e); + } + (void) putc('\n', fp); + (void) fflush(fp); + if (trap_file != NULL) + (*trap_fn)(quit, readback()); + if (quit) + exit(quit); +} + +/* + * Print an error message and optionally quit. + */ +void +error(va_alist) + va_dcl +{ + va_list l; + int quit, e; + char *fmt; + + va_start(l); + quit = va_arg(l, int); + if ((e = va_arg(l, int)) < 0) + e = errno; + fmt = va_arg(l, char *); + verror(quit, (char *)NULL, fmt, l, e); + va_end(l); +} + +/* + * Panic (print to stderr and abort). + */ +void +panic(va_alist) + va_dcl +{ + va_list l; + char *fmt; + + SetErrorTrap((void (*)())NULL); /* shut down any trap */ + va_start(l); + fmt = va_arg(l, char *); + verror(0, "panic: ", fmt, l, 0); + va_end(l); + abort(); +} + +#endif /* lint */ diff --git a/dviware/dvibook/libtex/error.h b/dviware/dvibook/libtex/error.h new file mode 100644 index 0000000000..195382257a --- /dev/null +++ b/dviware/dvibook/libtex/error.h @@ -0,0 +1,14 @@ +/* + * Copyright (c) 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * Errors. + */ +void error(/* int quit, int e, char *fmt, ... */); +void panic(/* char *fmt, ... */); + +void SetErrorTrap(/* void (*fn)(int quit, char *text) */); diff --git a/dviware/dvibook/libtex/findpost.c b/dviware/dvibook/libtex/findpost.c new file mode 100644 index 0000000000..4f0839a4e1 --- /dev/null +++ b/dviware/dvibook/libtex/findpost.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/findpost.c,v 2.3 89/08/22 21:50:11 chris Exp $"; +#endif + +/* + * FindPostAmble - Find the postamble of a DVI file. + * + * N.B.: This routine assumes that ftell() returns byte offsets, + * not magic cookies. + */ + +#include <stdio.h> +#include "types.h" +#include "dvicodes.h" +#include "fio.h" +#include "num.h" + +/* + * The end of the file consists of a four-byte postamble pointer, + * followed by the DVI_VERSION byte, followed by at least four + * bytes of DVI_FILLER, and at least enough bytes to make the file + * size a multiple of four. Normally, this would be between 4 and 7 + * inclusive, but some versions of TeX append huge amounts of padding. + */ +#define POSTSIZE 512 /* how many bytes to read at a time */ + +long ftell(); /* should be declared in stdio.h */ + +FindPostAmble(f) + register FILE *f; +{ + register long offset; + register char *p; + register int i; + register i32 n; + char postbuf[POSTSIZE]; + + /* + * Working backwards from end-of-file, read POSTSIZE bytes. + * Keep looking until we find something or run out of filler. + */ + fseek(f, 0L, 2); + offset = ftell(f); + do { + if ((offset -= POSTSIZE) < 0L) + offset = 0L; /* no negative fseeks please */ + fseek(f, offset, 0); + if ((i = fread(postbuf, sizeof(char), POSTSIZE, f)) <= 0) + break; + for (p = postbuf + i; --i >= 0;) + if (UnSign8(*--p) != DVI_FILLER) + goto found_something; + } while (offset); + return (-1); /* ran out of filler: not a DVI file */ + + /* + * We found something other than a filler byte at *p (which is + * the same as postbuf[i]). It had better be a version byte. + * If so, we want to start at the byte four bytes before that, + * and get one long; that will tell us where the postamble + * begins. + */ +found_something: + if (UnSign8(*p) != DVI_VERSION) + return (-1); /* not a DVI file */ + if ((i -= 4) >= 0) { + /* it is in the buffer, so just fish it out */ + p -= 4; + pGetLong(p, n); + } else { + if ((offset += i) < 0L) + return (-1); /* tsk */ + fseek(f, offset, 0); + fGetLong(f, n); + } + offset = n; + fseek(f, offset, 0); + return (0); /* success */ +} diff --git a/dviware/dvibook/libtex/fio.c b/dviware/dvibook/libtex/fio.c new file mode 100644 index 0000000000..b4ccf8974c --- /dev/null +++ b/dviware/dvibook/libtex/fio.c @@ -0,0 +1,44 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/fio.c,v 2.4 89/08/22 21:50:31 chris Exp $"; +#endif + +/* + * File I/O subroutines for getting bytes, words, 3bytes, and longwords. + * N.B.: these believe they are working on a DVI file. + */ + +#include <stdio.h> +#include "types.h" +#include "error.h" +#include "fio.h" + +static char eofmsg[] = "unexpected EOF, help"; + +/* for symmetry: */ +#define fGetByte(fp, r) ((r) = getc(fp)) +#define Sign32(i) (i) + +#define make(name, func, signextend) \ +i32 \ +name(fp) \ + register FILE *fp; \ +{ \ + register i32 n; \ + \ + func(fp, n); \ + if (feof(fp)) \ + error(1, 0, eofmsg); \ + return (signextend(n)); \ +} + +make(GetByte, fGetByte, Sign8) +make(GetWord, fGetWord, Sign16) +make(Get3Byte, fGet3Byte, Sign24) +make(GetLong, fGetLong, Sign32) diff --git a/dviware/dvibook/libtex/fio.h b/dviware/dvibook/libtex/fio.h new file mode 100644 index 0000000000..53f2ef8ca8 --- /dev/null +++ b/dviware/dvibook/libtex/fio.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * File I/O: numbers. + * + * We deal in fixed format numbers and (FILE *)s here. + * For pointer I/O, see pio.h. + * + * N.B.: These do the `wrong thing' at EOF. It is imperative + * that the caller add appropriate `if (feof(fp))' statements. + */ + +/* + * Get one unsigned byte. Note that this is a proper expression. + * The reset have more limited contexts, and are therefore OddLy + * CapItaliseD. + */ +#define fgetbyte(fp) getc(fp) + +/* + * Get a two-byte unsigned integer, a three-byte unsigned integer, + * or a four-byte signed integer. + */ +#define fGetWord(fp, r) ((r) = getc(fp) << 8, (r) |= getc(fp)) +#define fGet3Byte(fp,r) ((r) = getc(fp) << 16, (r) |= getc(fp) << 8, \ + (r) |= getc(fp)) +#define fGetLong(fp, r) ((r) = getc(fp) << 24, (r) |= getc(fp) << 16, \ + (r) |= getc(fp) << 8, (r) |= getc(fp)) + +/* + * Fast I/O write (and regular write) macros. + */ +#define putbyte(fp, r) ((void) putc((r), fp)) + +#define PutWord(fp, r) ((void) putc((r) >> 8, fp), \ + (void) putc((r), fp)) +#define Put3Byte(fp, r) ((void) putc((r) >> 16, fp), \ + (void) putc((r) >> 8, fp), \ + (void) putc((r), fp)) +#define PutLong(fp, r) ((void) putc((r) >> 24, fp), \ + (void) putc((r) >> 16, fp), \ + (void) putc((r) >> 8, fp), \ + (void) putc((r), fp)) + +/* + * Function types + */ +i32 GetByte(), GetWord(), Get3Byte(), GetLong(); diff --git a/dviware/dvibook/libtex/font.c b/dviware/dvibook/libtex/font.c new file mode 100644 index 0000000000..267e7dc5b4 --- /dev/null +++ b/dviware/dvibook/libtex/font.c @@ -0,0 +1,537 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/font.c,v 2.12 89/11/06 15:00:47 chris Exp $"; +#endif + +/* + * Routines for working with fonts. In particular, the configuration + * dependent code is here. + * + * Specific fonts (GF, PXL, etc.) have functions in separate files. + */ + +#include <stdio.h> +#include <errno.h> +#include <ctype.h> +#include "types.h" +#include "conv.h" +#include "error.h" +#include "font.h" + +/* + * Define the default configuration file. + * Also define the maximum path name length. + */ +#ifndef FONTDESC +/* #define FONTDESC "/usr/local/lib/tex/fontdesc" */ + you must define FONTDESC in the Makefile +#endif + +#define PATHLEN 1024 + +/* + * A font configuration. The font list is ordered. + * + * A specifier is typically a particular print engine, since + * different engines need slightly different fonts. + */ +struct fontconf { + struct fontconf *fc_next; + struct fontops *fc_ops; + char *fc_path; /* path, with metacharacters */ + char *fc_spec; /* specifier */ + int fc_slop; /* slop value */ +}; + +/* + * EQ is a fast way to check for string equivalence. + */ +#define EQ(a, b) (*(a) == *(b) && strcmp(a, b) == 0) + +/* + * Private variables and functions. + */ +static int didinit; /* true => initialised already */ +static char *cfname; /* config file name, for errors */ +static int cfline; /* config file line, likewise */ +static struct fontops *fontops;/* font operations code: list head */ +static struct fontconf *fonts; /* font list */ +static struct fontconf **nextfc;/* used during initialisation */ +static char spec_any[] = "*"; /* the `anything' specifier */ + +static void readconf(), badcf(), setfenv(), setfont(); +static struct font *getafont(); + +/* + * Imports. + */ +extern int errno; +char *getenv(), *malloc(), *strncpy(), *strsave(); + +/* + * You may get warnings from lint about sprintf(), which is of type + * `char *' in early 4BSD releases. Ignore the warning. + */ + +/* + * Here, alas, we know about all the kinds of fonts. + * This also means that every DVI interpreter pulls in + * the full set of font manipulation routines. + * + * PERHAPS THIS SHOULD BE CONFIGURABLE. + */ +#define ADDFONT(x) { \ + extern struct fontops x; \ + x.fo_next = fontops; \ + fontops = &x; \ +} + +void +fontinit(file) + char *file; +{ + + if (didinit) { + /* + * Could free the old configuration and fire up + * a new one, but for now . . . + */ + error(1, 0, "attempt to reinit fonts"); + /* NOTREACHED */ + } + didinit++; + ADDFONT(invisops); + ADDFONT(tfmops); + ADDFONT(blankops); + ADDFONT(boxops); + ADDFONT(pxlops); + ADDFONT(gfops); + ADDFONT(pkops); + nextfc = &fonts; + if (file == NULL) + if ((file = getenv(CONFENV)) == NULL) + file = FONTDESC; + readconf(file); +} + +/* + * Find a font's operations, given its name. + */ +static struct fontops * +findops(name) + register char *name; +{ + register struct fontops *fo; + + for (fo = fontops; fo != NULL; fo = fo->fo_next) + if (EQ(fo->fo_name, name)) + return (fo); + return (NULL); +} + +/* + * Read the named configuration file. The file is split into + * lines, and lines are split into words; if the first word is + * "font", this is a fontconf, and we read the remainder of the + * words and make a fontconf entry. + */ +static void +readconf(name) + char *name; +{ + register FILE *f; /* config file */ + register int c; /* char and word counter */ + int isenv; /* true => doing `fontenv', not `font' */ + char *env; /* value from getenv() */ + struct fontconf proto; /* prototype fontconf */ + char *v[20]; /* word vector */ + char line[BUFSIZ]; /* input line */ + + if ((f = fopen(name, "r")) == NULL) + error(1, -1, "cannot read font configuration file \"%s\"", + name); + cfname = name; + cfline = 0; + while (fgets(line, sizeof (line), f) != NULL) { + cfline++; + if ((c = strlen(line)) > 0) { + if (line[--c] != '\n') + badcf("line too long"); + line[c] = 0; + } + if ((c = split(line, v, sizeof v / sizeof *v)) < 0) + badcf("too many words"); + /* skip things that are not fonts */ + if (c == 0) + continue; + if (EQ(v[0], "font")) + isenv = 0; + else if (EQ(v[0], "fontenv")) + isenv = 1; + else + continue; + switch (c) { + case 1: + badcf("missing font typename"); + /* NOTREACHED */ + case 2: + badcf("missing font spec (engine)"); + /* NOTREACHED */ + case 3: + badcf("missing slop value"); + /* NOTREACHED */ + case 4: + badcf(isenv ? "need environment variable" : + "need pathname"); + /* NOTREACHED */ + case 5: + if (isenv) + badcf("need suffix"); + break; + case 6: + if (isenv) + break; + default: + error(0, 0, + "%s, line %d: warning: %d extra word(s) ignored", + cfname, cfline, c - 5); + break; + } + if ((proto.fc_ops = findops(v[1])) == NULL) { + error(0, 0, + "\"%s\", line %d: unknown font type \"%s\" ignored", + cfname, cfline, v[1]); + continue; + } + + /* + * For fontenv, if the environment variable is not actually + * set in the environment, just skip over this line. + */ + if (isenv && (env = getenv(v[4])) == NULL) + continue; + proto.fc_next = NULL; + proto.fc_spec = EQ(v[2], spec_any) ? NULL : strsave(v[2]); + proto.fc_slop = atoi(v[3]); + if (proto.fc_slop < 0) /* quietly enforce proper slops */ + proto.fc_slop = 0; + if (isenv) + setfenv(&proto, env, v[5]); + else + setfont(&proto, strsave(v[4])); + } +} + +/* + * Handle fontenv lines by appending as many configuration entries + * as there are components in the given environment variable. + */ +static void +setfenv(pfc, env, suf) + struct fontconf *pfc; + char *env, *suf; +{ + register char *s, *t; + register int len, suflen = strlen(suf); + register char *mem, *slash; + char *munch = strsave(env); /* a copy we can write on */ + struct fontops *fo; + + /* + * Loop over the path. Turn `foo' into `foo/<suf1><suf2>'; + * turn an empty string into just <suf1><suf2>. Put these + * names in fresh memory and set them up as places to search. + */ + for (s = env; (t = s) != NULL; s = t) { + for (;;) { + if (*t == ':') { + *t++ = 0; + len = t - s; /* includes trailing NUL */ + break; + } + if (*t++ == 0) { + len = t - s; /* includes trailing NUL */ + t = NULL; + break; + } + } + if (len != 0) { + slash = "/"; + len++; + } else + slash = ""; + if ((mem = malloc(len + suflen)) == NULL) + error(1, -1, "out of memory while saving $%s", env); + (void) sprintf(mem, "%s%s%s", s, slash, suf); + setfont(pfc, mem); + } + free(munch); +} + +/* + * Turn a prototype fontconf into a real one. + */ +static void +setfont(pfc, path) + struct fontconf *pfc; + char *path; +{ + register struct fontconf *fc; + + if ((fc = (struct fontconf *)malloc(sizeof(*fc))) == NULL) + error(1, -1, "out of memory for font configuration (sorry)"); + *fc = *pfc; + fc->fc_path = path; + *nextfc = fc; + nextfc = &fc->fc_next; +} + +/* + * Complain about a problem in the configuration file. + */ +static void +badcf(why) + char *why; +{ + + error(1, 0, "\"%s\", line %d: %s", cfname, cfline, why); + /* NOTREACHED */ +} + +/* + * Copy string s into buffer buf, dropping off any numerical suffix. + * E.g., cmr7 and cmr10 become cmr. (This is the `%b' operator.) + */ +static void +basify(s, buf, size) + char *s, *buf; + int size; +{ + register char *p, *endp; + register int n; + + for (p = s; *p; p++) + if (!isdigit(*p)) + endp = p; + if ((n = endp + 1 - s) >= size) + error(1, 0, "font name `%s' too long for %%b (sorry)", s); + (void) strncpy(buf, s, n); + buf[n] = 0; +} + +/* + * Turn a prototype path, name, and magnification into a full + * path (expand %f, %m, %b). + */ +static void +pave(result, proto, name, mag) + char *result, *proto, *name; + int mag; +{ + register int c; + register char *s, *d, *p; + char num[30], fontbase[256]; + + d = result; + p = proto; + s = NULL; + num[0] = 0; /* will need changing for other bases */ + fontbase[0] = 0; + + while (p != NULL) { + /* + * If sourcing from s, take its next character, and + * insert it directly. Otherwise take the next path + * character and interpret it. + */ + if (s != NULL) { + if ((c = *s++) == 0) { + s = NULL; + continue; + } + goto put; + } + if ((c = *p++) == 0) + p = NULL; + if (c != '%') + goto put; + + switch (c = *p++) { + + case 'b': + if (fontbase[0] == 0) + basify(name, fontbase, sizeof(fontbase)); + s = fontbase; + continue; + + case 'f': + case 'n': + case 's': + s = name; + continue; + + case 'd': + case 'm': + if (num[0] == 0) + (void) sprintf(num, "%d", mag); + s = num; + continue; + + case 0: + c = '%'; + p--; + /* FALLTHROUGH */ + } +put: + if (d - result >= PATHLEN) + error(1, 0, "font path `%s' too long (sorry)", proto); + *d++ = c; + } +} + +/* + * Given a font name and size, return the first font that fits, along + * with its name (via fname). If we cannot find such a font, we set + * *fname to point to a `canonical' example font name, unless there are + * are no fonts for the device, in which case we set *fname to NULL. + */ +struct font * +GetFont(nm, dvimag, dvidsz, dev, fname) + char *nm; + i32 dvimag, dvidsz; + char *dev, **fname; +{ + + return (getafont(nm, dvimag, dvidsz, dev, fname, 1)); +} + +/* + * Same as GetFont, but caller promises never to ask for rasters. + */ +struct font * +GetRasterlessFont(nm, dvimag, dvidsz, dev, fname) + char *nm; + i32 dvimag, dvidsz; + char *dev, **fname; +{ + + return (getafont(nm, dvimag, dvidsz, dev, fname, 0)); +} + +/* + * NEED TO THINK ABOUT gf NAMING CONVENTIONS HERE: ARE THEY LIKE pxl? + * WHAT ABOUT OTHERS? + */ +static struct font * +getafont(nm, dvimag, dvidsz, dev, fname, wantrast) + char *nm; + i32 dvimag, dvidsz; + char *dev, **fname; + int wantrast; +{ + register int slop, fmag; + register struct font *f; + register struct fontconf *fc; + register struct fontops *fo; + register char *path; + int fd, scaled; + double mag; + static char firstpath[PATHLEN], laterpath[PATHLEN]; + + if (!didinit) + fontinit((char *)NULL); + + /* + * The equation below means, approximately, `the font is + * magnified by the ratio of the actual size dvimag to the + * design size dvidsz, and then further scaled by the + * global magnification.' We multiply this by the printer's + * resolution in dots per inch, then use the per-font + * conversion factor to convert a dots-per-inch value to + * a font name `%m' magnification (extension). + */ + mag = (double) dvimag / (double) dvidsz; + scaled = mag * 1000.0 + 0.5; + mag *= Conversion.c_mag * Conversion.c_dpi; + + path = firstpath; /* no canonical font name yet */ + for (fc = fonts; fc != NULL; fc = fc->fc_next) { + if (dev != NULL && fc->fc_spec != NULL && + !EQ(dev, fc->fc_spec)) + continue; + /* + * Fake fonts are ignored unless there is already + * `canonical' real font. The first non-fake font + * is the `canonical' one. + */ + fo = fc->fc_ops; + if (path == firstpath && fo->fo_fakefont) + continue; + fmag = mag * fo->fo_dpitomag + 0.5; + for (slop = 0; slop <= fc->fc_slop; slop++) { + pave(path, fc->fc_path, nm, fmag + slop); + if ((fd = open(path, 0)) >= 0) + goto found; + path = laterpath; /* tried at least one now */ + if (slop) { + pave(path, fc->fc_path, nm, fmag - slop); + if ((fd = open(path, 0)) >= 0) + goto found; + } + } + } + + /* not found */ + if (path == firstpath) { + *fname = NULL; + errno = ENXIO; + } else { + *fname = firstpath; + errno = ENOENT; + } + return (NULL); + +found: + *fname = path; + + /* allocate space for the per-font info, and read it in */ + f = (struct font *)malloc(sizeof *f); + if (f == NULL) { + errno = ENOMEM; + return (NULL); + } + f->f_flags = wantrast ? FF_RASTERS : 0; + f->f_ops = fc->fc_ops; + f->f_path = strsave(path); + f->f_font = strsave(nm); + f->f_dvimag = dvimag; + f->f_dvidsz = dvidsz; + f->f_scaled = scaled; + f->f_pspace = f->f_dvimag / 6; /* a three-unit "thin space" */ + f->f_nspace = -4 * f->f_pspace; + f->f_vspace = 5 * f->f_pspace; + errno = 0; + if ((*f->f_ops->fo_read)(f, fd)) { + int e = errno; /* paranoid */ + + free(f->f_path); + free(f->f_font); + free((char *)f); + errno = e; + return (NULL); + } + if (f->f_ops->fo_fakefont) { + error(0, 0, "Warning: no font for %s", Font_TeXName(f)); + if (firstpath[0]) + error(0, 0, "(wanted, e.g., \"%s\")", firstpath); + error(0, 0, "I will substitute %s for the missing glyphs", + f->f_ops->fo_fakefont == 1 ? "boxes" : "white space"); + } + return (f); +} diff --git a/dviware/dvibook/libtex/font.h b/dviware/dvibook/libtex/font.h new file mode 100644 index 0000000000..2998bcf50b --- /dev/null +++ b/dviware/dvibook/libtex/font.h @@ -0,0 +1,301 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * Font file information, readers, etc. + */ + +#ifndef _MCTEX_TYPES_ +#include "types.h" +#endif + +/* + * First, font independent information: per glyph info, and per font + * info. + */ +struct glyph { + short g_flags; /* see below */ + /* + * The following cannot be used with GetRasterlessFont + */ + short g_rotation; /* see below */ + char *g_raster; /* raster, if known */ + /* + * These, however, do come with rasterless fonts, + * even though they relate only to the raster. + */ + i32 g_height; /* height of bounding box */ + i32 g_width; /* width of bounding box */ + i32 g_yorigin; /* y origin (>= 0 -> within box) */ + i32 g_xorigin; /* x origin (>= 0 -> within box) */ + /* + * These of course come with every font. + */ + i32 g_rawtfmwidth; /* raw TFM width (in FIXes) */ + i32 g_tfmwidth; /* TFM width in scaled points */ + /* + * The X and Y escapements come from PK and GF fonts. + * They are in scaled pixels (65536 scaled pixels = 1 pixel). + * If they are not defined by the font, these are set to + * NO_ESCAPEMENT. + */ + i32 g_xescapement; /* x escapement (`chardx') */ + i32 g_yescapement; /* y escapement (`chardy') */ +#define NO_ESCAPEMENT ((i32)0x89abcdef) + /* + * This is provided purely for DVI to device conversion programs. + */ + int g_pixwidth; /* width in pixels */ + /* + * Mainly for internal use, index is the glyph index within the + * font. That is, f->f_gly[i]->g_index == i. + */ + int g_index; /* character index */ + /* + * g_details and g_integer are purely for the font reading + * subroutines to use however they will. g_next makes lists + * of glyphs while the glyphs are free. + */ + union { /* various options */ + char *g_details; /* details: arbitrary */ + i32 g_integer; /* 32 bit integer */ + struct glyph *g_next; /* linked list */ + } g_un; +}; + +/* + * Glyph flags. + */ +#define GF_VALID 0x0001 /* glyph is `real' */ +#define GF_SEEN 0x0002 /* glyph has been encountered/loaded */ + +#define GF_USR0 0x0100 /* reserved to user code */ +#define GF_USR1 0x0200 /* reserved to user code */ +#define GF_USR2 0x0400 /* reserved to user code */ +#define GF_USR3 0x0800 /* reserved to user code */ + +/* + * Rotations are in quarter-pi steps, counterclockwise of course. + * This order must be maintained; see rotate.c. + */ +#define ROT_NORM 0 /* no rotation: `normal' position */ +#define ROT_LEFT 1 /* 1/4 turn counterclockwise */ +#define ROT_DOWN 2 /* 1/2 turn, i.e., upside-down */ +#define ROT_RIGHT 3 /* 3/4 turn ccw, or 1/4 turn cw */ + +struct font { + int f_flags; /* see below */ + struct fontops *f_ops; /* operations */ + /* + * f_un is provided for DVI drivers, which typically need + * some way to translate DVI font numbers into local values. + * Most seem to need integers. + */ + union { + int f_int; /* value if int */ + i32 f_i32; /* value if i32 */ + char *f_ptr; /* value if pointer */ + } f_un; +#ifdef notdef + /* for LRU cacheing; not yet implemented */ + i32 f_lastuse; +#endif + /* + * f_details is provided for font reading subroutines. + * It is intended to be cast to a pointer to a structure + * that is allocated by those routines, and used to keep + * track of whatever information those routines need to + * determine glyph boxes and (if asked for) rasters. + */ + char *f_details; /* type dependent stuff */ + /* + * f_path is the full pathname to the font file, filled in + * by GetFont and GetRasterlessFont. Note that since we + * hold on to the path until the font is freed, it should be + * possible to cache glyph accesses on memory-poor machines. + */ + char *f_path; /* font file pathname */ + /* + * f_dvimag and f_dvidsz are the magnification and design size + * values from the DVI file. f_font and f_scaled correspond to + * TeX's idea of the proper name for the font (e.g., `cmr10', + * `cmbx10 scaled 1440'). (Note that f_scaled is just the + * ratio of f_dvimag and f_dvidsz; you could save a bit of memory + * by eliminating it and altering the routine Font_TeXName()). + * f_design_size is the font's design size, or 0 for fake (TFM) + * fonts. f_checksum should be set by the font reading routines + * to the font checksum. If the value is nonzero, it will be + * compared to the checksum in the DVI file. + */ + i32 f_dvimag; /* magnification from DVI file */ + i32 f_dvidsz; /* design size from DVI file */ + char *f_font; /* TeX's name for the font */ + int f_scaled; /* the ratio of dvimag to dvidsz, x1000 */ + i32 f_design_size; /* font design size */ + i32 f_checksum; /* font checksum, or 0 */ + /* + * The three values f_pspace, f_nspace, and f_vspace correspond + * to the amount of positive, negative, and vertical space that + * limits `kerning' (if horizontal) or `in-line motion' (vertical). + * f_pspace is equal to 1/6 of f_dvimag, and f_nspace and f_vspace + * are -4 and 5 times this value respectively. DVItype puts it + * this way (slightly edited): + * + * Rounding to the nearest pixel is best done in [this] + * manner ... so as to be inoffensive to the eye: When [a] + * horizontal motion is small, like a kern, [the device + * position] changes by rounding the kern; but when the + * motion is large, [the device position] changes by rounding + * the [DVI position] so that accumulated rounding errors + * disappear. We allow a larger space in the negative + * direction than in the positive one, because TeX makes + * comparatively large backspaces when it positions accents. + * + * Vertical motion is done similarly, but with the threshold + * between ``small'' and ``large'' increased by a factor of + * five. The idea is to make fractions like ``$1\over2$'' + * round consistently, but to absorb accumulated rounding + * errors in the baseline-skip moves. + * + * There is, however, an exception: + * + * A sequence of consecutive rules, or consecutive characters + * in a fixed-width font whose width is not an integer number + * of pixels, can cause [the device position] to drift far + * away from a correctly rounded value. + * + * The drift correction is applied after every horizontal or + * vertical motion, including any resulting from printing a + * character. + */ + i32 f_pspace; /* limit on positive kerns */ + i32 f_nspace; /* limit on negative kerns */ + i32 f_vspace; /* limit on vertical `kerns' */ + + /* + * If set, f_hppp and f_vppp tell how many pixels there are + * per scaled point in the font. For instance, a 300 dpi font + * has 300pixels/1inch * 1inch/72.27pt * 65536pt/1sp ~= + * 272046 pixels/sp. + * + * If they are zero, the font does not tell us. + */ + i32 f_hppp; /* horizontal pixels per point */ + i32 f_vppp; /* vertical pixels per point */ + /* + * f_lowch and f_highch bound the region in which f_gly + * indicies are valid. Specificially, f_gly[i] may be + * read or written if and only if i is in the half-open + * interval [f_lowch..f_highch). f_gly is an array of + * pointers to glyph structures. The structures themselves + * are not allocated until requested. + * + * f_glybase is the actual return from malloc(), since it + * is theoretically possible for f_gly-f_lowch to become + * NULL. + */ + int f_lowch; /* first character */ + int f_highch; /* last character, plus 1 */ + struct glyph **f_gly; /* glyphs */ + struct glyph **f_glybase; +}; + +/* + * Macros for rounding DVI and device motions per DVItype rules. + */ +#define F_SMALLH(f, m) ((m) < (f)->f_pspace && (m) > (f)->f_nspace) +#define F_SMALLV(f, m) ((m) < (f)->f_vspace && -(m) < (f)->f_vspace) + +/* + * Font flags. + */ +#define FF_RASTERS 0x0001 /* font has rasters */ +#define FF_USR0 0x0100 /* reserved to user code */ +#define FF_USR1 0x0200 /* reserved to user code */ +#define FF_USR2 0x0400 /* reserved to user code */ +#define FF_USR3 0x0800 /* reserved to user code */ + +/* + * Operations on fonts. + * + * The `fo_dpitomag' field is used as a multiplier for a desired + * resolution in dots per inch. The result of the multiplication + * is converted to a font name by multipying by 1000.0 and rounding. + * The idea is that PXL files will have a multiplier of 5.0, and + * others will have a multiplier of 1.0. This suffices for the + * present, at any rate; in the future, this field may be replaced + * with something more general. + * + * N.B.: more operations may be added as they are discovered to be + * useful. + */ +struct fontops { + char *fo_name; /* name, e.g., "gf" */ + int fo_fakefont; /* 1="box", 2="blank"; else 0 */ + double fo_dpitomag; /* multiplier */ + int (*fo_read)(); /* open and read the font itself */ + int (*fo_getgly)(); /* obtain specified glyphs (range) */ +#ifdef notdef + void (*fo_freegly)(); /* release specified glyphs */ +#endif + int (*fo_rasterise)(); /* rasterise specified glyphs */ + void (*fo_freefont)(); /* discard font (free details) */ + struct fontops *fo_next; /* purely for font.c */ +}; + +/* + * Return a pointer to the glyph information for character `c' in + * font `f'. + */ +#define GLYPH(f, c) \ + ((c) < (f)->f_lowch || (c) >= (f)->f_highch ? (struct glyph *)0 : \ + ((f)->f_gly[c] ? (f)->f_gly[c] : GetGlyph(f, (int)(c)))) + +/* + * True iff glyph `g' is valid. Useful for checking return values + * from GLYPH(). + */ +#define GVALID(g) ((g) && ((g)->g_flags & GF_VALID)) + +/* + * True iff glyph g has a raster. + */ +#define HASRASTER(g) ((g)->g_height && (g)->g_width) + +/* + * Return a pointer to the raster information for glyph `g' in font + * `f' at rotation `r'. + */ +#define RASTER(g, f, r) ((g)->g_rotation == (r) && (g)->g_raster ? \ + (g)->g_raster : GetRaster(g, f, r)) + +/* + * Function types. + */ +struct font *GetFont(), *GetRasterlessFont(); +struct glyph *GetGlyph(); +char *GetRaster(); +void FreeFont(); +void FreeGlyph(); +void FreeRaster(); +char *Font_TeXName(); +double DMagFactor(); /* from magfactor.c */ +void fontinit(); + +/* + * Normally from stdio.h + */ +#ifndef NULL +#define NULL 0 +#endif + +/* + * The following environment variable overrides the default font + * configuration file. That default is used when fontinit() is not + * called, or is passed a null pointer. + */ +#define CONFENV "TEXFONTDESC" diff --git a/dviware/dvibook/libtex/font_subr.c b/dviware/dvibook/libtex/font_subr.c new file mode 100644 index 0000000000..97e82fc8c1 --- /dev/null +++ b/dviware/dvibook/libtex/font_subr.c @@ -0,0 +1,251 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/font_subr.c,v 2.8 89/10/30 04:08:52 chris Exp $"; +#endif + +/* + * Subroutines common to all fonts. + */ + +#include "types.h" +#include "error.h" +#include "font.h" + +static struct glyph *freeglyphs; + +char *malloc(); +extern errno; + +/* + * Set up the font structures to note that a font has glyphs in + * the half-open interval [low, high). + * + * SHOULD I ALLOW ADDITIONS TO THE RANGE VIA SUBSEQUENT CALLS TO + * FontHasGlyphs? + */ +FontHasGlyphs(f, low, high) + register struct font *f; + register int low, high; +{ + register struct glyph **gp; + + /* record bounds */ + f->f_lowch = low; + f->f_highch = high; + + /* + * Allocate space for all the glyph pointers, and set + * them all to NULL. + */ + if (low >= high) /* no glyphs */ + gp = NULL; + else { + gp = (struct glyph **) malloc((unsigned) (high - low) * + sizeof (*gp)); + if (gp == NULL) + return (-1); + } + f->f_glybase = gp; + f->f_gly = gp - low; + while (++low <= high) + *gp++ = NULL; + return (0); +} + +/* + * AllocGlyph allocates a new glyph. ReleaseGlyph puts one onto the free + * list. We maintain a local list of free glyphs. + */ +#define ReleaseGlyph(g) \ + ((g)->g_un.g_next = freeglyphs, freeglyphs = (g)) + +static struct glyph * +AllocGlyph(n) + int n; +{ + register struct glyph *g; + register int i; + + if ((g = freeglyphs) == NULL) { + g = (struct glyph *) malloc((unsigned) (128 * sizeof (*g))); + if (g == NULL) + error(1, errno, "out of glyph memory"); + g += (i = 128); + while (--i >= 0) { + g--; + ReleaseGlyph(g); + } + } + freeglyphs = g->g_un.g_next; + g->g_flags = 0; + g->g_raster = NULL; + g->g_index = n; + g->g_xescapement = NO_ESCAPEMENT;/* default, if not set by font */ + g->g_yescapement = NO_ESCAPEMENT; + return (g); +} + +/* + * Free one glyph. + */ +void +FreeGlyph(f, g) + struct font *f; + register struct glyph *g; +{ + int n = g->g_index; + +#ifdef notdef + (*f->f_ops->fo_freegly)(f, n, n); +#endif + if (g->g_raster != NULL) + free(g->g_raster); + ReleaseGlyph(g); + f->f_gly[n] = NULL; +} + +/* + * Free a font. + */ +void +FreeFont(f) + register struct font *f; +{ + register struct glyph *g; + register int i; + +#ifdef notdef + (*f->f_ops->fo_freegly)(f, f->f_lowch, f->f_highch); +#endif + for (i = f->f_lowch; i < f->f_highch; i++) { + if ((g = f->f_gly[i]) == NULL) + continue; + if (g->g_raster != NULL) + free(g->g_raster); + ReleaseGlyph(g); + } + if (f->f_glybase != NULL) + free((char *)f->f_glybase); + (*f->f_ops->fo_freefont)(f); + free(f->f_path); + free(f->f_font); + free((char *)f); +} + +/* + * Get glyph `c' in font `f'. We pull in a few adjacent glyphs here + * under the (perhaps naive) assumption that things will go faster + * that way. + * + * TODO: + * try different adjacency values + * make adjacency a font attribute? (or an op) + */ +#define ADJ 4 /* must be a power of 2 */ +#define GET_ADJ(c, l, h) ((h) = ADJ + ((l) = (c) & ~(ADJ - 1))) + +struct glyph * +GetGlyph(f, c) + register struct font *f; + int c; +{ + register int i, h, l; + + GET_ADJ(c, l, h); + if (l < f->f_lowch) + l = f->f_lowch; + if (h > f->f_highch) + h = f->f_highch; + if (l >= h) + return (NULL); + for (i = l; i < h; i++) + if (f->f_gly[i] == NULL) + f->f_gly[i] = AllocGlyph(i); + + if ((*f->f_ops->fo_getgly)(f, l, h)) { + /* + * I do not know what to do about this just yet, so: + */ + panic("getgly fails and I am confused ... help!"); + } + + /* + * Apply the appropriate scale factor to the TFM widths. + * This makes them come out in scaled points, instead of FIXes. + */ + ScaleGlyphs(f, l, h); /* ??? */ + + return (f->f_gly[c]); +} + +/* + * Get the raster for glyph g in font f at rotation r. + */ +char * +GetRaster(g, f, r) + register struct glyph *g; + register struct font *f; + int r; +{ + int l, h; + + /* abort if caller did not ask for rasters in advance */ + if ((f->f_flags & FF_RASTERS) == 0) + panic("GetRaster(%s)", f->f_path); + + /* + * If there is no raster, get one. Blank characters, + * however, never have rasters. + */ + if (g->g_raster == NULL) { + if (!HASRASTER(g)) + return (NULL); + /* + * THE FOLLOWING DEPENDS ON THE ADJACENCY MATCHING THAT IN + * GetGlyph() ABOVE. + */ + GET_ADJ(g->g_index, l, h); + if (l < f->f_lowch) + l = f->f_lowch; + if (h > f->f_highch) + h = f->f_highch; + if ((*f->f_ops->fo_rasterise)(f, l, h)) + error(1, 0, "rasterise fails (out of memory?)"); + } + if (g->g_rotation != r) + SetRotation(g, r); + return (g->g_raster); +} + +void +FreeRaster(g) + struct glyph *g; +{ + + if (g->g_raster) { + free((g)->g_raster); + g->g_raster = NULL; + } +} + +/* + * Return a TeX-style font name, including scale factor. + * SHOULD I BOTHER WITH \magstep STYLE NAMES? + */ +char * +Font_TeXName(f) + register struct font *f; +{ + static char result[200]; + + if (f->f_scaled == 1000) + return (f->f_font); + (void) sprintf(result, "%s scaled %d", f->f_font, f->f_scaled); + return (result); +} diff --git a/dviware/dvibook/libtex/getopt.c b/dviware/dvibook/libtex/getopt.c new file mode 100644 index 0000000000..e3b296ca50 --- /dev/null +++ b/dviware/dvibook/libtex/getopt.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * getopt - get option letter from argv + * (From Henry Spencer @ U of Toronto Zoology, slightly edited) + */ + +#include <stdio.h> + +char *optarg; /* Global argument pointer. */ +int optind; /* Global argv index. */ + +static char *scan; /* Private scan pointer. */ + +extern char *index(); + +int +getopt(argc, argv, optstring) + register int argc; + register char **argv; + char *optstring; +{ + register int c; + register char *place; + + optarg = NULL; + if (scan == NULL || *scan == 0) { + if (optind == 0) + optind++; + if (optind >= argc || argv[optind][0] != '-' || + argv[optind][1] == 0) + return (EOF); + if (strcmp(argv[optind], "--") == 0) { + optind++; + return (EOF); + } + scan = argv[optind] + 1; + optind++; + } + c = *scan++; + place = index(optstring, c); + + if (place == NULL || c == ':') { + fprintf(stderr, "%s: unknown option -%c\n", argv[0], c); + return ('?'); + } + place++; + if (*place == ':') { + if (*scan != '\0') { + optarg = scan; + scan = NULL; + } else { + if (optind >= argc) { + fprintf(stderr, + "%s: missing argument after -%c\n", + argv[0], c); + return ('?'); + } + optarg = argv[optind]; + optind++; + } + } + return (c); +} diff --git a/dviware/dvibook/libtex/gfclass.c b/dviware/dvibook/libtex/gfclass.c new file mode 100644 index 0000000000..207b58ef7e --- /dev/null +++ b/dviware/dvibook/libtex/gfclass.c @@ -0,0 +1,81 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/gfclass.c,v 2.3 89/08/22 21:52:30 chris Exp $"; +#endif + +/* + * gfclass - GF code classification tables. + */ + +#include "gfclass.h" + +/* shorthand---in lowercase for contrast (read on!) */ +#define three(x) x, x, x +#define four(x) x, x, x, x +#define five(x) four(x), x +#define six(x) four(x), x, x +#define eight(x) four(x), four(x) +#define sixteen(x) eight(x), eight(x) +#define thirty_two(x) sixteen(x), sixteen(x) +#define sixty_four(x) thirty_two(x), thirty_two(x) +#define one_twenty_eight(x) sixty_four(x), sixty_four(x) +#define one_sixty_five(x) one_twenty_eight(x), thirty_two(x), five(x) + +/* + * Length of the single (or first) operand, if any. + */ +char gf_oplen[256] = { + sixty_four(GPL_NONE), /* GF_PAINT_0 through GF_PAINT_63 */ + GPL_UNS1, /* GF_PAINT1 */ + GPL_UNS2, /* GF_PAINT2 */ + GPL_UNS3, /* GF_PAINT3 */ + GPL_NONE, /* GF_BOC */ + GPL_NONE, /* GF_BOC1 */ + GPL_NONE, /* GF_EOC */ + GPL_NONE, /* GF_SKIP0 */ + GPL_UNS1, /* GF_SKIP1 */ + GPL_UNS2, /* GF_SKIP2 */ + GPL_UNS3, /* GF_SKIP3 */ + one_sixty_five(GPL_NONE),/* GF_NEW_ROW_0 through GF_NEW_ROW_164 */ + GPL_UNS1, /* GF_XXX1 */ + GPL_UNS2, /* GF_XXX2 */ + GPL_UNS3, /* GF_XXX3 */ + GPL_SGN4, /* GF_XXX4 */ + GPL_SGN4, /* GF_YYY */ + GPL_NONE, /* GF_NOP */ + GPL_NONE, /* GF_CHAR_LOC */ + GPL_NONE, /* GF_CHAR_LOC0 */ + GPL_NONE, /* GF_PRE */ + GPL_NONE, /* GF_POST */ + GPL_NONE, /* GF_POSTPOST */ + six(GPL_NONE) /* 250 through 255 */ +}; + +/* + * Types of the various opcodes. + */ +char gf_gt[256] = { + sixty_four(GT_PAINT0), /* GF_PAINT_0 through GF_PAINT_63 */ + three(GT_PAINT), /* GF_PAINT1 through GF_PAINT3 */ + GT_BOC, /* GF_BOC */ + GT_BOC1, /* GF_BOC1 */ + GT_EOC, /* GF_EOC */ + GT_SKIP0, /* GF_SKIP0 */ + three(GT_SKIP), /* GF_SKIP1 through GF_SKIP3 */ + one_sixty_five(GT_NEW_ROW),/* GF_NEW_ROW_0 throgh GF_NEW_ROW_164 */ + four(GT_XXX), /* GF_XXX1 through GF_XXX4 */ + GT_YYY, /* GF_YYY */ + GT_NOP, /* GF_NOP */ + GT_CHAR_LOC, /* GF_CHAR_LOC */ + GT_CHAR_LOC0, /* GF_CHAR_LOC0 */ + GT_PRE, /* GF_PRE */ + GT_POST, /* GF_POST */ + GT_POSTPOST, /* GF_POSTPOST */ + six(GT_UNDEF) /* 250 through 255 */ +}; diff --git a/dviware/dvibook/libtex/gfclass.h b/dviware/dvibook/libtex/gfclass.h new file mode 100644 index 0000000000..dd43f8254a --- /dev/null +++ b/dviware/dvibook/libtex/gfclass.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * GF classification codes + */ + +/* + * Predicate for simple paint commands. This is presumably the most + * common GF operation; it may be profitable to check for this before + * switching out on the command type. + */ +#define GF_IsPaint(c) ((c) < 64) + +/* + * Symbolic names for command `types', as returned by the macro + * GT_TYPE(int c). + */ +#define GT_PAINT0 0 /* paint without parameter */ +#define GT_PAINT 1 /* paint with parameter */ +#define GT_BOC 2 /* long BOC */ +#define GT_BOC1 3 /* short BOC */ +#define GT_EOC 4 /* EOC */ +#define GT_SKIP0 5 /* parameterless SKIP */ +#define GT_SKIP 6 /* parmeterised SKIP */ +#define GT_NEW_ROW 7 /* NEW_ROW_n */ +#define GT_XXX 8 /* XXXn */ +#define GT_YYY 9 /* YYY */ +#define GT_NOP 10 /* no op */ +#define GT_CHAR_LOC 11 /* CHAR_LOC */ +#define GT_CHAR_LOC0 12 /* abbreviated CHAR_LOC */ +#define GT_PRE 13 +#define GT_POST 14 +#define GT_POSTPOST 15 +#define GT_UNDEF 16 + +/* + * Symbolic names for parameter lengths, obtained via the macro + * GT_OpLen(int c). + */ +#define GPL_NONE 0 /* no parameter, or too complex */ +#define GPL_UNS1 1 /* one one-byte parameter in 0..255 */ +#define GPL_UNS2 2 /* one two-byte parameter in 0..65535 */ +#define GPL_UNS3 3 /* one three-byte parameter in 0..16777215 */ +#define GPL_SGN4 4 /* one four-byte signed parameter */ +/* + * there are no unsigned four byte parameters, and no shorter signed + * parameters + */ + +#define GF_OpLen(code) (gf_oplen[code]) +#define GF_TYPE(code) (gf_gt[code]) +extern char gf_oplen[]; +extern char gf_gt[]; diff --git a/dviware/dvibook/libtex/gfcodes.h b/dviware/dvibook/libtex/gfcodes.h new file mode 100644 index 0000000000..beab1c54ea --- /dev/null +++ b/dviware/dvibook/libtex/gfcodes.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * GF (generic font) opcodes. + */ + +#define GF_PAINT_0 0 /* paint with `d' = 0 */ + /* ... through PAINT_63, d = 63 */ +#define GF_PAINT1 64 /* paint, with a one-byte parameter */ +#define GF_PAINT2 65 /* paint, with a two-byte parameter */ +#define GF_PAINT3 66 /* paint, with a three-byte parameter */ +#define GF_BOC 67 /* beginning of character */ +#define GF_BOC1 68 /* compressed form of BOC */ +#define GF_EOC 69 /* end of character */ +#define GF_SKIP0 70 /* finish this row, begin next with white */ +#define GF_SKIP1 71 /* finish row, one byte parameter */ +#define GF_SKIP2 72 +#define GF_SKIP3 73 +#define GF_NEW_ROW_0 74 /* begin a new row, ready to blacken */ +#define GF_NEW_ROW_1 75 /* begin a new row, but one col. from left */ + /* through GF_NEW_ROW_164 */ +#define GF_XXX1 239 /* special (interpreter dependent) command */ +#define GF_XXX2 240 /* like XXX1, but two byte length parameter */ +#define GF_XXX3 241 +#define GF_XXX4 242 +#define GF_YYY 243 /* takes four byte parameter, otherwise nop */ + /* (generated by METAFONT for numspecial) */ +#define GF_NOP 244 /* no op */ +#define GF_CHAR_LOC 245 /* character locator */ +#define GF_CHAR_LOC0 246 /* abbreviated form of CHAR_LOC */ +#define GF_PRE 247 /* introduces preamble */ +#define GF_POST 248 /* introduces postamble */ +#define GF_POSTPOST 249 /* marks end of postamble */ + + /* codes 250 through 255 are undefined */ + +#define GF_ID 131 /* identifies this revision of GF */ + +#define GF_FILLER 223 /* filler bytes at end of GF file */ diff --git a/dviware/dvibook/libtex/gffont.c b/dviware/dvibook/libtex/gffont.c new file mode 100644 index 0000000000..78ab10a3d2 --- /dev/null +++ b/dviware/dvibook/libtex/gffont.c @@ -0,0 +1,943 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/gffont.c,v 2.9 89/09/02 17:16:17 chris Exp $"; +#endif + +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include "types.h" +#include "error.h" +#include "font.h" +#include "gfcodes.h" +#include "gfclass.h" +#include "num.h" + +/* + * GF font operations. + * + * GF files may be compact, but this code surely is not! + * + * TODO: + * think about fonts with characters outside [0..255] + * find some way to free gf body when done + */ +static int gf_read(), gf_getgly(), gf_rasterise(); +static void gf_freefont(); + +struct fontops gfops = + { "gf", 0, 1.0, gf_read, gf_getgly, gf_rasterise, gf_freefont }; + +/* + * Local info. + */ + +/* + * A bounding box. The names follow those in the GF documentation. + */ +struct bounds { + i32 min_m, max_m; /* min and max `m' (colunm) values */ + i32 min_n, max_n; /* min and max `n' (row) values */ +}; + +/* + * char_loc is one `character locator' from a GF file, save for the + * `character residue', which we need not. + */ +struct char_loc { + i32 cl_dx; /* x escapement (scaled pixels) */ + i32 cl_dy; /* y escapement (scaled pixels) */ + i32 cl_w; /* TFM width */ + i32 cl_p; /* pointer to BOC (or specials) */ +}; + +/* + * GF details include: + * -> the main body of the GF file (all bytes save pre- and post-amble); + * -> global box boundaries; + * and character locators, addressed by `character residue'. Empty + * slots are indicated by a -1 `pointer'. + */ +struct gf_details { + char *gd_body; /* GF body */ + char *gd_base; /* == gd_body - preamble_size */ + struct bounds gd_gb; /* global boundaries */ + struct char_loc gd_cl[256]; /* character locators */ +}; + +/* + * Get the gf_details from font f. + */ +#define ftogd(f) ((struct gf_details *)(f)->f_details) + +extern int errno; +char *malloc(), *copyit(); + +/* + * This code mimicks findpost.c. + */ +#define POSTSIZE 512 + +/* + * Find the GF postamble. Store the offsets of the POST and POSTPOST + * opcodes through postp and postpostp. + */ +static int +findGFpostamble(fd, postp, postpostp) + int fd; + long *postp, *postpostp; + +{ + register long offset; + register char *p; + register int i; + register i32 n; + char postbuf[POSTSIZE]; + + offset = lseek(fd, 0L, 2); + do { + + if ((offset -= POSTSIZE) < 0L) + offset = 0L; + (void) lseek(fd, offset, 0); + i = read(fd, postbuf, POSTSIZE); + if (i <= 0) + break; + for (p = postbuf + i; --i >= 0;) + if (UnSign8(*--p) != GF_FILLER) + goto found_something; + } while (offset); + return (-1); /* ran out of filler */ + +found_something: + if (UnSign8(*p) != GF_ID) + return (-1); /* not a GF file */ + offset += i; /* this is the offset of *p */ + *postpostp = offset - 5;/* POSTPOST should be here */ + /* we need 4 bytes for the pointer */ + if (i >= 4) + p -= 4; + else { + offset -= 4; + if (offset < 0L) + return (-1); /* tsk */ + (void) lseek(fd, offset, 0); + if (read(fd, p = postbuf, 4) != 4) + return (-1); + } + /* + * p now points to the postamble pointer. Get it and + * seek to the postamble, also saving the offset. + */ + pGetLong(p, n); + *postp = offset = n; + (void) lseek(fd, offset, 0); + return (0); /* made it */ +} + +/* + * Read a GF file. + */ +static int +gf_read(f, fd) + register struct font *f; + int fd; +{ + register struct gf_details *gd; + register char *p; + register struct char_loc *cl; + register int i; + int presize, postsize, bodysize, firstc, lastc; + char *postamble; + long postaddr, postpostaddr; + i32 lasteoc; + char *problem = NULL; + struct stat st; + char b[4]; + int saverr; + + gd = NULL; /* prepare for failure */ + postamble = NULL; + + /* + * The file had best be at least 50 bytes long. A + * `completely empty' GF file might consist of a PRE, a GF_ID, + * no comment (one zero byte), then: POST, pointer to last + * EOC, design size, checksum, hppp, vppp, min_m, max_m, + * min_n, max_n, POSTPOST, pointer to POST, GF_ID, and four + * FILLERs. + */ + (void) fstat(fd, &st); + if (st.st_size < 50) { /* too small to be a GF file */ + problem = "file is too short"; + goto fail; + } + + /* + * Read the very beginning and pick up the preamble size. + */ + if (read(fd, b, 4) != 4) + goto fail; + if (UnSign8(b[0]) != GF_PRE) { + problem = "file does not begin with PRE"; + goto fail; + } + i = UnSign8(b[1]); + if (i != GF_ID) + error(0, 0, "Warning: strange GF id (%d) in \"%s\"", i, + f->f_path); + presize = 3 + UnSign8(b[2]); + + /* + * Find the postamble, allocate space, and read it in. + */ + if (findGFpostamble(fd, &postaddr, &postpostaddr)) { + problem = "cannot find postamble"; + goto fail; + } + postsize = postpostaddr - postaddr + 1; + if ((p = malloc(postsize)) == NULL) + goto fail; + if (read(fd, p, postsize) != postsize) + goto fail; + postamble = p; + + /* + * Make sure we found it. + */ + if (pgetbyte(p) != GF_POST) { + problem = "no GF_POST at postamble"; + goto fail; + } + if (UnSign8(postamble[postsize - 1]) != GF_POSTPOST) { + problem = "no GF_POSTPOST after postamble"; + goto fail; + } + + /* + * Looks okay. Allocate detail space and poke through the postamble. + */ + if ((gd = (struct gf_details *) malloc(sizeof (*gd))) == NULL) + goto fail; + gd->gd_body = NULL; + + pGetLong(p, lasteoc); /* actually one past last EOC */ + pGetLong(p, f->f_design_size); + pGetLong(p, f->f_checksum); + pGetLong(p, f->f_hppp); + pGetLong(p, f->f_vppp); + + pGetLong(p, gd->gd_gb.min_m); + pGetLong(p, gd->gd_gb.max_m); + pGetLong(p, gd->gd_gb.min_n); + pGetLong(p, gd->gd_gb.max_n); + + /* + * Zap all the character locators, then read those that are + * defined in the postamble. Remember the first and last + * characters so that we know which glyphs are defined. Lastc + * is actually the last-plus-one'th character. + */ + for (cl = gd->gd_cl, i = 256; --i >= 0; cl++) + cl->cl_p = -1; + firstc = 256; + lastc = 0; + for (;;) { + i32 dx, dy; + + switch (pgetbyte(p)) { + + case GF_CHAR_LOC: + i = pgetbyte(p); + pGetLong(p, dx); + pGetLong(p, dy); + goto merge; + + case GF_CHAR_LOC0: + i = pgetbyte(p); + dx = ((i32) pgetbyte(p)) << 16; + dy = 0; +merge: + if (i < firstc) + firstc = i; + if (i >= lastc) + lastc = i + 1; + cl = &gd->gd_cl[i]; + cl->cl_dx = dx; + cl->cl_dy = dy; + pGetLong(p, cl->cl_w); + pGetLong(p, cl->cl_p); + break; + + case GF_POSTPOST: + goto done; + + default: + error(0, 0, "I do not understand %d here", + UnSign8(p[-1])); + problem = "unexpected opcode in postamble"; + goto fail; + } + } +done: + free(postamble); + postamble = NULL; /* all done with it */ + + /* + * Alas, we need the instructions whether or not we need + * the rasters, since the raster bounding box information + * can only be properly determined by converting the rasters. + * Compute the size of the main body of the GF file, then + * read it in. + */ + bodysize = lasteoc - presize; + if ((gd->gd_body = malloc(bodysize + 1)) == NULL) + goto fail; + (void) lseek(fd, (long) presize, 0); + if (read(fd, gd->gd_body, bodysize) != bodysize) + goto fail; + /* + * The next byte might be a special, so we just + * arbitrarily stuff in a POST. + */ + gd->gd_body[bodysize] = GF_POST; + gd->gd_base = gd->gd_body - presize; + + f->f_details = (char *)gd; + if (FontHasGlyphs(f, firstc, lastc)) + goto fail2; + (void) close(fd); + return (0); + +fail: + if (problem == NULL) + error(0, errno, "trouble reading \"%s\"", f->f_path); + else { + error(0, 0, "%s", problem); + error(0, 0, "(are you sure \"%s\" is a GF file?)", f->f_path); + } + errno = 0; +fail2: + saverr = errno; + if (postamble != NULL) + free(postamble); + if (gd != NULL) { + if (gd->gd_body != NULL) + free(gd->gd_body); + free((char *) gd); + } + (void) close(fd); + errno = saverr; + return (-1); +} + +/* + * Some global variables, used while building rasters. (These are + * referenced also in copyit(), so must be global. Fortunately, no + * one yet requires the font routines to be re-entrant.) + */ +static char *buildraster; /* raster being built */ +static int buildsize; /* size of buildraster (bytes) */ + +static struct bounds tempb; /* bounds used during buildraster */ +static struct bounds ob; /* observed bounds */ + +/* + * Bit tables: `left' and `right' bits. lbits[b] has all the bits + * that are to the left of bit b set; rbits[b] has all the bits + * that are to the right of bit b set, as well as bit b itself. + */ +static char lbits[] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe }; +static char rbits[] = { 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01 }; + +/* + * The magic address `nullraster' is known in getgly() as a valid + * but empty raster, and changed there to NULL. A NULL return from + * drawchar indicates failure; we need something to distinguish the + * empty raster. + */ +static char nullraster[1]; + +/* + * `Inline functions': + * -> convert a bit number to a byte number (round down); + * -> convert a number of bits to a number of bytes (round up); + * and convert a bit to a bit index. + */ +#define btoby(b) ((b) >> 3) +#define btonb(b) (((b) + 7) >> 3) +#define btobi(b) ((b) & 7) + +/* + * Helper function for getgly: build the raster, and compute its + * minimal bounding box. Called with `p' pointing past the backpointer + * field of the BOC command (i.e., at min_m or del_m). `abbrev' is true + * iff this was a BOC1. `globalb' are the global bounds from the GF file, + * whose name is pointed to by gfname. + */ +static char * +drawchar(p, abbrev, globalb, gfname) + register char *p; + int abbrev; + struct bounds globalb; + char *gfname; +{ + register i32 m; /* m register (column) */ + register char *colp; /* pointer to byte corresponding to m */ + register int c; /* temporary */ + register i32 i; /* temporary */ + register int black; /* true when paint_switch==black */ + register i32 n; /* n register (row) */ + int stride; /* multiplier to convert linear to 2d array */ + int wrotethisrow; /* true iff we wrote in the current row */ + char *virtrast; /* virtual origin version of buildraster */ + int mustcopy; /* true if we must copy the built raster */ + struct bounds gb; /* bounds from the GF file */ + + /* get the bounds */ + if (abbrev) { + c = pgetbyte(p);/* del_m */ + gb.min_m = (gb.max_m = pgetbyte(p)) - c; + c = pgetbyte(p);/* del_n */ + gb.min_n = (gb.max_n = pgetbyte(p)) - c; + } else { + pGetLong(p, gb.min_m); + pGetLong(p, gb.max_m); + pGetLong(p, gb.min_n); + pGetLong(p, gb.max_n); + } + + /* + * Trim the GF bounds according to the global bounds. We + * use the trimmed values to allocate the build space. + */ + tempb = gb; +#define GB_ADJ(field, cmp) \ + if (tempb.field cmp globalb.field) \ + tempb.field = globalb.field + GB_ADJ(min_m, <); + GB_ADJ(max_m, >); + GB_ADJ(min_n, <); + GB_ADJ(max_n, >); +#undef GB_ADJ + + /* + * First check for completely empty characters---that is, + * those where min > max. The space in cmtex10, for instance, + * could be a completely empty character, but for some reason + * METAFONT declares cmtex10 space as [0..0][0..0]. + * Nonetheless, we will not assume that [0..-1][0..0] or + * the like are impossible. + */ + if (tempb.min_m > tempb.max_m || tempb.min_n > tempb.max_n) + return (nullraster); + + /* + * Compute the distance between rows (the number of bytes per + * column), then make sure there is room in the build space + * for [min_n..max_n] of these, possibly by allocating a new raster. + */ + stride = btonb(tempb.max_m - tempb.min_m + 1); + c = stride * (tempb.max_n - tempb.min_n + 1); + if (c > buildsize) { + if (buildraster != NULL) + free(buildraster); + if ((buildraster = malloc(c)) == NULL) { + buildsize = 0; + return (NULL); + } + buildsize = c; + } + + /* + * If we are using an old raster that is too big, remember to + * scrunch it down later. + */ + mustcopy = buildsize > c; + + /* clear the raster to white */ + bzero(buildraster, c); + + /* + * Make a virtual origin raster pointer. The virtual origin is + * where raster[0][0] is, whether or not there is a raster[0][0]. + * Normally, this would be + * &buildraster[-gb.min_n * stride - btoby(gb.min_m)], + * but it is complicated by two things. Firstly, we would like + * n==max_n to be the bottommost point in the raster (low + * addresses), and n==min_n to be the topmost (high addresses). + * In other words, we need to reflect the n (Y) values about + * the X axis: negate them. Secondly, the raster we create + * must be `flush left'. That is, somewhere along its rows, + * bit 0x80 must be set at the left edge of one of its columns. + * We need to subtract away the minimum bit index before + * calculating bit values. This cannot really be done in + * advance, since we cannot address bits directly. + */ + virtrast = &buildraster[gb.max_n * stride]; + + /* + * Set up the bounds-trimming variables. We could do this by + * simply setting the observed min point to a very large number, + * and the observed max point to a very small number, but this + * way we need not come up with extreme numbers (which might not + * be portable). + * + * If at least one pixel is blackened, the observed bounds will + * become the correct bounds in the code below, and we will have + * min<=max; but if no pixels are blackened, we arrange to have + * min>max, so we have to check for that again later. + * + * This would be `min = max + 1' and `max = min - 1', but for + * efficiency, the observed m bounds are kept offset by gb.min_m + * until we finish drawing the character. + */ + ob.min_m = tempb.max_m + 1 - gb.min_m; + ob.max_m = tempb.min_m - 1 - gb.min_m; + ob.min_n = tempb.max_n + 1; + ob.max_n = tempb.min_n - 1; + wrotethisrow = 0; + +#define FIX_N_BOUNDS() { \ + if (wrotethisrow) { \ + c = -n; /* recall that n is reflected about X axis */ \ + if (c < ob.min_n) \ + ob.min_n = c; \ + if (c > ob.max_n) \ + ob.max_n = c; \ + wrotethisrow = 0; \ + } \ +} + + /* + * Initialise state variables: m = min_m, n = max_n, + * paint_switch = white. + */ + m = 0; /* gb.min_m - gb.min_m */ + n = -gb.max_n; /* reflected */ + colp = &virtrast[n * stride]; +if (colp != buildraster) +panic("gffont drawchar colp != buildraster"); + black = 0; + + /* + * Now interpret the character. + * `for (;;)' pushes everything off the right edge, alas. + */ +more: + c = pgetbyte(p); + if (GF_IsPaint(c)) /* faster? */ + goto paint; + switch (GF_OpLen(c)) { + + case GPL_NONE: + break; + + case GPL_UNS1: + i = pgetbyte(p); + break; + + case GPL_UNS2: + pGetWord(p, i); + break; + + case GPL_UNS3: + pGet3Byte(p, i); + break; + + case GPL_SGN4: + pGetLong(p, i); + break; + + default: + panic("gffont drawchar GF_OpLen(%d) = %d", c, GF_OpLen(c)); + /* NOTREACHED */ + } + + switch (GF_TYPE(c)) { + + case GT_PAINT0: +paint: + i = c - GF_PAINT_0; + /* FALLTHROUGH */ + + case GT_PAINT: + /* + * Paint `i' bits in the current row at columns [m..m+i). + */ + if (i && black) { + /* remember to adjust n bounds later */ + wrotethisrow = 1; + /* adjust minimum m bound */ + if (m < ob.min_m) + ob.min_m = m; + + /* + * Finish the partial byte at colp. There are 8-k + * bits to set to finish it, where k is the bit + * index value from m. If we need to set fewer + * than 8-k bits, set them now and skip the rest + * of this. + */ + c = 8 - btobi(m); + if (i < c) { /* cannot finish it off */ + *colp |= UnSign8(lbits[i]) >> btobi(m); + m += i; + } else { /* finish it off */ + *colp++ |= rbits[btobi(m)]; + i -= c; + m += c; + + /* + * Update m to reflect having written the + * remaining i bits, then write them. + * First write all the full bytes, then + * start a partial byte with whatever + * is left over, if anything. + */ + m += i; + i >>= 3; + while (--i >= 0) + *colp++ = 0xff; + *colp |= lbits[btobi(m)]; + } + + /* adjust maximum m bound */ + if (m > ob.max_m) + ob.max_m = m; + } else { + /* + * Add the bit index so that we round up whenever + * this fills the partial byte at colp. + */ + colp += (i + btobi(m)) >> 3; + m += i; + } + black = !black; + break; + + case GT_EOC: /* all done */ + FIX_N_BOUNDS(); + goto done; + + case GT_SKIP0: /* skip no rows */ + i = 0; + /* FALLTHROUGH */ + + case GT_SKIP: /* skip some rows, draw white */ + m = 0; + black = 0; + goto skip_or_new_row; + + case GT_NEW_ROW: /* next row near left col, draw black */ + m = c - GF_NEW_ROW_0; + black = 1; + i = 0; /* n offset is 1: skip no rows */ +skip_or_new_row: + FIX_N_BOUNDS(); + n += i + 1; /* += because reflected */ + colp = &virtrast[n * stride + btoby(m)]; + break; + + case GT_XXX: /* special */ + p += i; + break; + + case GT_YYY: /* numspecial */ + break; + + case GT_NOP: /* dull */ + break; + + case GT_BOC: /* should not appear */ + case GT_BOC1: + case GT_CHAR_LOC: + case GT_CHAR_LOC0: + case GT_PRE: + case GT_POST: + case GT_POSTPOST: + case GT_UNDEF: + error(0, 0, "unexpected GF opcode %d", c); + error(1, 0, "bad GF file \"%s\"", gfname); + /* NOTREACHED */ + + default: + panic("gffont drawchar GF_TYPE(%d) = %d", c, GF_TYPE(c)); + /* NOTREACHED */ + } + goto more; + +done: + /* + * The observed bounds `m' values are both off by gb.min_m, so + * fix them now. + */ + ob.min_m += gb.min_m; + ob.max_m += gb.min_m; + + /* as noted above, we have to make sure there were some points */ + if (ob.min_m > ob.max_m) + return (nullraster); +if (ob.min_n > ob.max_n) +panic("gffont min>max: n but not m"); + + /* + * If we used too much memory for the raster, copy it now. + */ + if (mustcopy || tempb.min_n != ob.min_n || tempb.max_n != ob.max_n || + btonb(ob.max_m - ob.min_m + 1) != stride) + return (copyit()); + + /* + * If the left column bounds match, just move the raster in place. + */ + if (tempb.min_m == ob.min_m) { + p = buildraster; + buildraster = NULL; + buildsize = 0; + return (p); + } + + /* + * The raster must be copied, but only because it is not + * `left justified'. + * + * CONSIDER DEFERRING THIS PHASE UNTIL THE RASTER IS USED + */ + return (copyit()); +} + +/* + * Copy the built raster to newly allocated space. + * We may need to shift all the bits left as well. + */ +char * +copyit() +{ + register char *o, *p; + register int i, oldoff, newoff; + char *n; + + /* + * Compute the observed minimum stride. + */ + newoff = btonb(ob.max_m - ob.min_m + 1); + n = malloc((unsigned)(newoff * (ob.max_n - ob.min_n + 1))); + if ((p = n) == NULL) + return (NULL); + + /* + * Compute the old stride. + */ + oldoff = btonb(tempb.max_m - tempb.min_m + 1); +if (oldoff < newoff) +panic("gffont copyit oldoff < newoff"); + + /* + * Point at the old raster, then add the offset to the first + * written row, and then the offset to the first written column. + */ + o = buildraster; + o += (ob.max_n - tempb.max_n) * oldoff; + i = ob.min_m - tempb.min_m; + o += btoby(i); + + /* + * Now copy each row, doing shifting if required. + */ + if ((i = btobi(i)) != 0) { /* must shift, alas */ + register int r = 8 - i, j, k; + + oldoff -= newoff; + for (k = ob.max_n; k >= ob.min_n; k--) { + for (j = newoff; --j > 0;) { + *p = *o++ << i; + *p++ |= UnSign8(*o) >> r; + } + /* last byte is special: no rightward bits */ + *p++ = *o++ << i; + o += oldoff; + } + } else if (oldoff > newoff) { /* compressing columns */ + for (i = ob.max_n; i >= ob.min_n; i--) { + bcopy(o, p, newoff); + o += oldoff; + p += newoff; + } + } else /* just squeezing out extra rows */ + bcopy(o, p, newoff * (ob.max_n - ob.min_n + 1)); + + /* finally, return the copy */ + return (n); +} + +/* + * Obtain the specified range of glyphs. + */ +static int +gf_getgly(f, l, h) + register struct font *f; + int l, h; +{ + register struct glyph *g; + register struct char_loc *cl; + register char *p; + register i32 c; + register int i; + register i32 thisboc; + int abbrev; + struct gf_details *gd = ftogd(f); + + /* + * For each glyph, make sure there exists an instance of that + * character residue. Go find the actual glyph (which may be + * arbitrarily far down a chain of pointers) and get its info. + */ + for (cl = &gd->gd_cl[i = l]; i < h; i++, cl++) { + g = f->f_gly[i]; + thisboc = cl->cl_p; + + /* + * Screw around locating the character for real. + */ + while (thisboc != -1) { + p = gd->gd_base + thisboc; +skip: + c = pgetbyte(p); + switch (GF_TYPE(c)) { + + case GT_XXX: + switch (GF_OpLen(c)) { + + case GPL_UNS1: + c = pgetbyte(p); + break; + + case GPL_UNS2: + pGetWord(p, c); + break; + + case GPL_UNS3: + pGet3Byte(p, c); + break; + + case GPL_SGN4: + pGetLong(p, c); + break; + + default: + panic("gf_getgly GF_OpLen(%d) = %d", + c, GF_OpLen(c)); + /* NOTREACHED */ + } + p += c; + goto skip; + + case GT_YYY: + p += 4; + goto skip; + + case GT_BOC: + abbrev = 0; + pGetLong(p, c); + break; + + case GT_BOC1: + abbrev = 1; + c = pgetbyte(p); + break; + + default: + error(0, 0, "GF code %d; I expected BOC", c); + error(1, 0, "bad GF file \"%s\"", f->f_path); + /* NOTREACHED */ + } + /* + * Found a BOC. If it is the right character, + * go handle it. + */ + if (c == i) + goto handleit; + if ((c & 255) != i) { + error(0, 0, "%d != %d mod 256", c, i); + error(1, 0, "Bad GF file \"%s\"", f->f_path); + } + /* + * Follow the backpointer. + */ + if (abbrev) + thisboc = -1; + else + pGetLong(p, thisboc); + } + + /* + * If we get here, the glyph is not valid after all. + */ + continue; + + /* + * The glyph is okay. Set it up. + */ +handleit: + g->g_flags = GF_VALID; + g->g_xescapement = cl->cl_dx; + g->g_yescapement = cl->cl_dy; + g->g_rawtfmwidth = cl->cl_w; + if (!abbrev) + p += 4; /* skip backpointer */ + if ((p = drawchar(p, abbrev, gd->gd_gb, f->f_path)) == NULL) + return (-1); /* ??? */ + if (p == nullraster) { + /* min > max, hence max-min+1 <= 0; we want 0 */ + g->g_height = 0; + g->g_width = 0; + g->g_yorigin = 0; + g->g_xorigin = 0; + g->g_raster = NULL; + } else { + /* set height &c based on observed bounds */ + g->g_height = ob.max_n - ob.min_n + 1; + g->g_width = ob.max_m - ob.min_m + 1; + g->g_yorigin = ob.max_n; + g->g_xorigin = -ob.min_m; + g->g_raster = p; + } + g->g_rotation = ROT_NORM; + } + return (0); +} + +/* + * Obtain rasters for the specified glyphs. We did this above, while + * adjusting the bounding boxes, so this routine should never get called. + */ +static int +gf_rasterise(f, l, h) + struct font *f; + int l, h; +{ + + panic("gf_rasterise(%s, %d, %d)", f->f_path, l, h); +} + +/* + * Discard the font details. + */ +static void +gf_freefont(f) + struct font *f; +{ + struct gf_details *gd; + + if ((gd = ftogd(f)) != NULL) { + free(gd->gd_body); + free((char *) gd); + } +} diff --git a/dviware/dvibook/libtex/gripes.h b/dviware/dvibook/libtex/gripes.h new file mode 100644 index 0000000000..87ebbb18b9 --- /dev/null +++ b/dviware/dvibook/libtex/gripes.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * Declarations for the various gripes. + */ + +void GripeOutOfMemory(), GripeCannotGetFont(), GripeDifferentChecksums(); +void GripeMissingFontsPreventOutput(), GripeNoSuchFont(); +void GripeFontAlreadyDefined(), GripeUnexpectedDVIEOF(), GripeUnexpectedOp(); +void GripeMissingOp(), GripeCannotFindPostamble(), GripeMismatchedValue(); +void GripeUndefinedOp(), GripeBadGlyph(); diff --git a/dviware/dvibook/libtex/gripes0.c b/dviware/dvibook/libtex/gripes0.c new file mode 100644 index 0000000000..69a33354e7 --- /dev/null +++ b/dviware/dvibook/libtex/gripes0.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/gripes0.c,v 2.5 89/09/01 13:52:38 chris Exp $"; +#endif + +/* + * Common errors (`gripes'). + */ + +#include <stdio.h> +#include "types.h" +#include "error.h" +#include "gripes.h" + +extern errno; + +/* + * Cannot allocate memory. + */ +void +GripeOutOfMemory(n, why) + int n; + char *why; +{ + + error(1, -1, "ran out of memory allocating %d bytes for %s", n, why); + /* NOTREACHED */ +} + +/* + * Cannot get a font. + * RETURNS TO CALLER + */ +void +GripeCannotGetFont(name, mag, dsz, dev, fullname) + char *name; + i32 mag, dsz; + char *dev, *fullname; +{ + int e = errno; + char scale[40]; + + if (mag == dsz) /* no scaling */ + scale[0] = 0; + else + (void) sprintf(scale, " scaled %d", + (int) ((double) mag / (double) dsz * 1000.0 + .5)); + + error(0, e, "no font for %s%s", name, scale); + if (fullname) + error(0, 0, "(wanted, e.g., \"%s\")", fullname); + else { + if (dev) + error(1, 0, "(there are no fonts for the %s engine!)", + dev); + else + error(1, 0, "(I cannot find any fonts!)"); + /* NOTREACHED */ + } +} + +/* + * Font checksums do not match. + * RETURNS TO CALLER + */ +void +GripeDifferentChecksums(font, tfmsum, fontsum) + char *font; + i32 tfmsum, fontsum; +{ + + error(0, 0, "\ +WARNING: TeX and I have different checksums for font\n\ +\t\"%s\"\n\ +\tPlease notify your TeX maintainer\n\ +\t(TFM checksum = 0%lo, my checksum = 0%lo)", + font, (long)tfmsum, (long)fontsum); +} + +/* + * A font, or several fonts, are missing, so no output. + */ +void +GripeMissingFontsPreventOutput(n) + int n; +{ + static char s[2] = {'s', 0}; + + error(1, 0, "%d missing font%s prevent%s output (sorry)", n, + n > 1 ? s : &s[1], n == 1 ? s : &s[1]); + /* NOTREACHED */ +} diff --git a/dviware/dvibook/libtex/gripes1.c b/dviware/dvibook/libtex/gripes1.c new file mode 100644 index 0000000000..cd31a2f2b5 --- /dev/null +++ b/dviware/dvibook/libtex/gripes1.c @@ -0,0 +1,161 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/gripes1.c,v 1.1 89/08/22 21:54:19 chris Exp $"; +#endif + +/* + * Gripes having to do with DVI files. + */ + +#include <stdio.h> +#include "types.h" +#include "error.h" +#include "font.h" +#include "gripes.h" + +extern char *DVIFileName; + +static char * +dfn() +{ + + return (DVIFileName ? DVIFileName : "the input"); +} + +/* + * Save string space by declaring these here. + */ +#if __STDC__ >= 1 +static const char dfl[] = "DVI file"; +static const char areyousure[] = "(are you sure %s is a %s?)"; +#else +static char dfl[] = "DVI file"; +static char areyousure[] = "(are you sure %s is a %s?)"; +#endif + +/* + * DVI file requests a font it never defined. + */ +void +GripeNoSuchFont(n) + i32 n; +{ + + error(0, 0, "%s wants font %ld, which it never defined", dfl, (long)n); + error(1, 0, areyousure, dfn(), dfl); + /* NOTREACHED */ +} + +/* + * DVI file redefines a font. + */ +void +GripeFontAlreadyDefined(n) + i32 n; +{ + + error(0, 0, "%s redefines font %ld", dfl, n); + error(1, 0, areyousure, dfn(), dfl); + /* NOTREACHED */ +} + +/* + * Unexpected end of DVI file. + */ +void +GripeUnexpectedDVIEOF() +{ + + GripeUnexpectedOp("end of file"); + /* NOTREACHED */ +} + +/* + * Unexpected DVI opcode. + */ +void +GripeUnexpectedOp(s) + char *s; +{ + + error(0, 0, "unexpected %s in %s", s, dfl); + error(1, 0, areyousure, dfn(), dfl); + /* NOTREACHED */ +} + +/* + * Missing DVI opcode. + */ +void +GripeMissingOp(s) + char *s; +{ + + error(0, 0, "missing %s in %s", s, dfl); + error(1, 0, areyousure, dfn(), dfl); + /* NOTREACHED */ +} + +/* + * Cannot find DVI postamble. + */ +void +GripeCannotFindPostamble() +{ + + error(0, 0, "cannot find postamble"); + error(1, 0, areyousure, dfn(), dfl); + /* NOTREACHED */ +} + +/* + * Inconsistent DVI value. + */ +void +GripeMismatchedValue(s) + char *s; +{ + + error(0, 0, "mismatched %s in %s", s, dfl); + error(1, 0, areyousure, dfn(), dfl); + /* NOTREACHED */ +} + +/* + * Undefined DVI opcode. + */ +void +GripeUndefinedOp(n) + int n; +{ + + error(0, 0, "undefined DVI opcode %d", n); + error(1, 0, areyousure, dfn(), dfl); + /* NOTREACHED */ +} + +/* + * DVI file requests glyph that is not in some font, or + * when no font is set for this page. + * + * RETURNS TO CALLER + */ +void +GripeBadGlyph(c, f) + i32 c; + struct font *f; +{ + + if (f->f_path == NULL) { + error(0, 0, "bad %s: char without setfont", dfl); + error(1, 0, "(try checking %s with dvitype)", dfn()); + /* NOTREACHED */ + } + error(0, 0, "there is no character %ld in %s!", (long)c, f->f_path); +} diff --git a/dviware/dvibook/libtex/magfactor.c b/dviware/dvibook/libtex/magfactor.c new file mode 100644 index 0000000000..f0419d4aa8 --- /dev/null +++ b/dviware/dvibook/libtex/magfactor.c @@ -0,0 +1,40 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * Convert a magnification factor to floating point. This is used in + * conjunction with the FONT_SLOP stuff to try to get the file names + * right, and may also be used by DVI reading programs to get slightly + * more accurate values for (mag/1000.0). + */ +double +DMagFactor(mag) + int mag; +{ + + switch (mag) { + + case 1095: /* stephalf */ + return (1.095445); + + case 1315: /* stepihalf */ + return (1.314534); + + case 2074: /* stepiv */ + return (2.0736); + + case 2488: /* stepv */ + return (2.48832); + + case 2986: /* stepiv */ + return (2.985984); + + default: /* remaining mags have been ok */ + return ((double) mag / 1000.); + } + /* NOTREACHED */ +} diff --git a/dviware/dvibook/libtex/num.h b/dviware/dvibook/libtex/num.h new file mode 100644 index 0000000000..80e03188df --- /dev/null +++ b/dviware/dvibook/libtex/num.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * Pointer I/O: numbers. + * + * We deal in fixed format numbers and pointers here. + * For file I/O, see fio.h. + */ + +/* + * Get one unsigned byte. Note that this is a proper expression. + * The rest have more limited contexts, and are therefore OddLy + * CapItaliseD. + */ +#define pgetbyte(p) UnSign8(*(p)++) + +/* + * Get a two-byte unsigned integer, a three-byte unsigned integer, + * or a four-byte signed integer. + */ +#define pGetWord(p, r) ((r) = UnSign8(*(p)++) << 8, \ + (r) |= UnSign8(*(p)++)) +#define pGet3Byte(p,r) ((r) = UnSign8(*(p)++) << 16, \ + (r) |= UnSign8(*(p)++) << 8, \ + (r) |= UnSign8(*(p)++)) +#define pGetLong(p, r) ((r) = UnSign8(*(p)++) << 24, \ + (r) |= UnSign8(*(p)++) << 16, \ + (r) |= UnSign8(*(p)++) << 8, \ + (r) |= UnSign8(*(p)++)) + +/* + * ADD pputbyte, pPutWord, pPut3Byte, pPutLong HERE IF THEY PROVE + * USEFUL. (But then must consider changing PutWord &c in fio.h.) + */ diff --git a/dviware/dvibook/libtex/pkfont.c b/dviware/dvibook/libtex/pkfont.c new file mode 100644 index 0000000000..da65190b3d --- /dev/null +++ b/dviware/dvibook/libtex/pkfont.c @@ -0,0 +1,729 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/pkfont.c,v 2.10 89/08/22 21:54:49 chris Exp $"; +#endif + +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include "types.h" +#include "error.h" +#include "font.h" +#include "num.h" + +/* + * PK font operations. + * + * The spelling `nybble' is a concession to the authors of the PK format. + */ +static int pk_read(), pk_getgly(), pk_rasterise(); +static void pk_freefont(); + +struct fontops pkops = + { "pk", 0, 1.0, pk_read, pk_getgly, pk_rasterise, pk_freefont }; + +/* + * Local info. + */ + +/* + * Commands. + */ +#define PK_XXX1 240 /* 1 byte special */ +#define PK_XXX2 241 /* 2 byte special */ +#define PK_XXX3 242 /* 3 byte special */ +#define PK_XXX4 243 /* 4 byte special */ +#define PK_YYY 244 /* METAFONT numspecial */ +#define PK_POST 245 /* marks postamble */ +#define PK_NO_OP 246 /* do nothing */ +#define PK_PRE 247 /* marks preamble */ + /* 248..255 undefined */ +#define PK_IsUndef(c) ((c) > PK_PRE) + +#define PK_ID 89 /* marks this brand of PK file */ + +/* + * Information about a character packet. + */ +struct cp { + char *cp_packet; /* the beginning of the packet */ + int cp_type; /* decoded pre type, see below */ +}; + +#define CP_SHORT 0 /* short preamble */ +#define CP_EXT_SHORT 1 /* extended short preamble */ +#define CP_LONG 2 /* long preamble */ + +/* + * The PK details include: + * -> a pointer to the next byte to fetch; + * -> the most recent byte fetched (when we are using nextnyb()); + * -> a flag indicating that we have used nybble 0 (bits 4..7) and + * should use nybble 1 next; + * -> the base address of the memory allocated for the PK file; + * -> the value of dyn_f (during character translation); + * -> the repeat count (during rasterisation); + * -> the lowest glyph number that is legal; + * -> the highest glyph number that is legal; + * -> glyph instructions for the standard glyphs; + * -> glyph instructions for more (nonstandard) glyphs; + * and the number of glyphs left unrasterised. + */ +struct pk_details { + char *pk_ptr; /* next byte to fetch */ + int pk_c; /* most recent byte fetched, if nybbling */ + int pk_1nyb; /* true => nybble 1 is next (bits 0..3) */ + char *pk_base; /* base of allocated memory */ + int pk_dyn_f; /* the dyn_f value */ + int pk_repeat; /* the repeat count */ + int pk_minc; /* minimum character value */ + int pk_maxc; /* maximum character value */ +#define MAXSTD 256 /* maximum `standard' character value */ + int pk_gleft; /* number of valid glyphs left uninterpreted */ + struct cp pk_cpack[MAXSTD]; /* for characters in [0..MAXSTD) */ + struct cp *pk_morec; /* for characters in [MAXSTD..maxc] */ +}; + +/* + * Fetch the next byte from the PK file. + */ +#define nextbyte(pk) pgetbyte((pk)->pk_ptr) + +/* + * PK packed number encoding. Nybbles in [1..dyn_f] represent themselves. + * Values in (dyn_f..13] are two-nybble values, and represent values + * dyn_f+1 through (13-dyn_f+1)*16+15. Zero marks a long number; 14 and + * 15 specify repeat counts instead (which are another packed number). + * Note that we cannot represent the number zero as a packed number. + */ +#define PK_LONGNUM 0 /* a `long number' */ +#define PK_REPEAT 14 /* set repeat count */ +#define PK_REPEAT1 15 /* set repeat to 1 */ + +/* + * Get the next nybble. This is an expression rendition of + * if (--pk->pk_1nyb < 0) { + * pk->pk_1nyb = 1; + * pk->pk_c = nextbyte(pk); + * return (pk->pk_c >> 4); + * } else + * return (pk->pk_c & 0xf); + */ +#define nextnyb(pk) \ + (--(pk)->pk_1nyb < 0 ? \ + ((pk)->pk_1nyb = 1, ((pk)->pk_c = nextbyte(pk)) >> 4) : \ + (pk)->pk_c & 0xf) + +/* + * Get the pk_details from font f. + */ +#define ftopk(f) ((struct pk_details *)(f)->f_details) + +static int scan_characters(); + +extern int errno; +char *malloc(); + +/* + * PK subroutines. + */ + +/* + * Unpack a packed number. + */ +static int +pk_unpack(pk) + register struct pk_details *pk; +{ + register int i, j; + +top: + if ((i = nextnyb(pk)) == PK_LONGNUM) { +#if PK_LONGNUM != 0 /* this may be silly, but . . . */ + i = 0; +#endif + /* + * Expand a long number. There are one fewer leading + * zeros than there are nonzero digits to obtain, so + * count up the leading zeros, add one, and get that + * many digits. (The `digits' are hexadecimal values.) + */ + do { + i++; + } while ((j = nextnyb(pk)) == 0); + while (--i >= 0) { + j <<= 4; + j += nextnyb(pk); + } + return (j - 15 + (13 - pk->pk_dyn_f) * 16 + pk->pk_dyn_f); + } + if (i <= pk->pk_dyn_f) + return (i); + if (i < PK_REPEAT) + return ((i - pk->pk_dyn_f - 1) * 16 + nextnyb(pk) + + pk->pk_dyn_f + 1); + + /* + * There is a repeat count, either one or a packed number. + * Get it first, then start over. (tail recursion) + */ + if (i == PK_REPEAT) + pk->pk_repeat = pk_unpack(pk); + else + pk->pk_repeat = 1; + goto top; +} + +/* + * Skip over special commands (PK_XXX?, PK_YYY). + */ +static void +skip_specials(f) + struct font *f; +{ + struct pk_details *pk = ftopk(f); + register char *p = pk->pk_ptr; + register i32 i; + + for (;;) { + switch (UnSign8(*p++)) { + + case PK_XXX1: + i = UnSign8(*p++); + p += i; + break; + + case PK_XXX2: + pGetWord(p, i); + p += i; + break; + + case PK_XXX3: + pGet3Byte(p, i); + p += i; + break; + + case PK_XXX4: + pGetLong(p, i); + p += i; + break; + + case PK_YYY: + p += 4; + break; + + case PK_NO_OP: + break; + + case PK_PRE: + error(1, 0, "unexpected PK_PRE in \"%s\"", f->f_path); + break; + + default: + p--; + if (PK_IsUndef(UnSign8(*p))) + error(1, 0, "invalid opcode %d in \"%s\"", + f->f_path); + pk->pk_ptr = p; + return; + } + } +} + +/* + * Read a PK file. + */ +static int +pk_read(f, fd) + register struct font *f; + int fd; +{ + register struct pk_details *pk; + register char *p; + int i, minc; + struct stat st; + char *reason; + + pk = NULL; /* prepare for failure */ + reason = NULL; + (void) fstat(fd, &st); + if (st.st_size < 4) { /* ??? */ + reason = "file is too small"; + goto fail; + } + if ((pk = (struct pk_details *)malloc(sizeof (*pk))) == NULL) + goto fail; + pk->pk_morec = NULL; + if ((pk->pk_base = malloc(st.st_size)) == NULL) + goto fail; + if (read(fd, pk->pk_base, st.st_size) != st.st_size) + goto fail; + pk->pk_ptr = pk->pk_base; + if (nextbyte(pk) != PK_PRE) { + reason = "file does not begin with PK_PRE"; + goto fail; + } + if (nextbyte(pk) != PK_ID) { + reason = "bad PK_ID"; + goto fail; + } + i = nextbyte(pk); + p = pk->pk_ptr + i; /* skip comment */ + pGetLong(p, f->f_design_size); + pGetLong(p, f->f_checksum); + pGetLong(p, f->f_hppp); + pGetLong(p, f->f_vppp); + pk->pk_ptr = p; + + f->f_details = (char *)pk; + + /* scan the characters, fail if necessary */ + if (scan_characters(f, &reason)) + goto fail; + + /* ignore the postamble */ + + /* COMPRESS pk->pk_base DATA? */ + + if (FontHasGlyphs(f, pk->pk_minc, pk->pk_maxc + 1)) + goto fail; + (void) close(fd); + return (0); + +fail: + if (reason) { + error(0, 0, "%s", reason); + error(0, 0, "(are you sure %s is a PK file?)", f->f_path); + errno = 0; + } + if (pk != NULL) { + if (pk->pk_base != NULL) + free(pk->pk_base); + if (pk->pk_morec != NULL) + free((char *)pk->pk_morec); + free((char *)pk); + } + (void) close(fd); + return (-1); +} + +/* + * Scan through the characters in the PK file, and set the offsets + * and preamble types for each of the character packets. + */ +static int +scan_characters(f, reason) + struct font *f; + char **reason; +{ + register struct pk_details *pk = ftopk(f); + register i32 c, pl; + register char *p; + register struct cp *cp; + int type; + +#ifdef lint + /* reason will be used someday ... I think */ + reason = reason; +#endif + + /* set up the minimisers and the glyph count */ + pk->pk_minc = 1; + pk->pk_maxc = 0; + pk->pk_gleft = 0; + + /* mark all character packets as untouched */ + for (cp = pk->pk_cpack, c = MAXSTD; --c >= 0; cp++) + cp->cp_packet = NULL; + + /* + * Loop through the packets until we reach a POST, skipping + * the glyph instructions themselves after each definition, + * and specials (if any) before each. + */ + for (;; pk->pk_ptr = p + pl) { + skip_specials(f); + p = pk->pk_ptr; + if ((c = pgetbyte(p)) == PK_POST) + break; /* whoops, done after all */ + + /* + * Look at the low three bits to decide preamble size. + * A value of 7 is a `long preamble'; 4, 5, and 6 are + * `extended short preambles'; and 0, 1, 2, and 3 are + * `short preambles'. + * + * We ignore most of the preamble, reading only the + * `packet length' and the character code at this time. + */ + switch (c & 7) { + + case 7: /* long */ + type = CP_LONG; + pGetLong(p, pl); + pGetLong(p, c); + break; + + case 6: + case 5: + case 4: /* extended short */ + type = CP_EXT_SHORT; + pGetWord(p, pl); + pl += (c & 3) << 16; + c = pgetbyte(p); + break; + + default: /* short */ + type = CP_SHORT; + pl = ((c & 3) << 8) + pgetbyte(p); + c = pgetbyte(p); + break; + } + + if (c >= MAXSTD) { + /* + * BEGIN XXX - should alloc pk_morec, but is hard + * and not now useful + */ + error(0, 0, "ignoring character %d in \"%s\"", + f->f_path); + error(0, 0, "because some darn programmer was lazy!"); + continue; + /* END XXX */ + } else + cp = &pk->pk_cpack[c]; + + cp->cp_packet = pk->pk_ptr; + cp->cp_type = type; + + /* adjust range */ + if (c < pk->pk_minc) + pk->pk_minc = c; + if (c > pk->pk_maxc) + pk->pk_maxc = c; + + pk->pk_gleft++; /* and count the glyph */ + } + return (0); /* no problems */ +} + +/* + * Obtain the specified range of glyphs. + */ +static int +pk_getgly(f, l, h) + register struct font *f; + int l, h; +{ + register struct pk_details *pk = ftopk(f); + register char *p; + register struct glyph *g; + register int i; + register struct cp *cp; + + if (pk == NULL) + panic("pk_getgly(%s)", f->f_path); + for (i = l; i < h; i++) { + if (i < MAXSTD) + cp = &pk->pk_cpack[i]; + else { + if (i > pk->pk_maxc) + panic("pk_getgly(%s, %d)", f->f_path, i); + cp = &pk->pk_morec[i - MAXSTD]; + } + p = cp->cp_packet; + if (p == NULL) /* glyph is not valid */ + continue; + g = f->f_gly[i]; + p++; /* skip flag */ + switch (cp->cp_type) { + + case CP_LONG: + p += 8; /* skip packet len, character code */ + pGetLong(p, g->g_rawtfmwidth); + pGetLong(p, g->g_xescapement); + pGetLong(p, g->g_yescapement); + pGetLong(p, g->g_width); + pGetLong(p, g->g_height); + pGetLong(p, g->g_xorigin); + pGetLong(p, g->g_yorigin); + break; + + case CP_EXT_SHORT: { + i32 dm; + + p += 3; /* skip packet len, character code */ + pGet3Byte(p, g->g_rawtfmwidth); + pGetWord(p, dm); + g->g_xescapement = dm << 16; + g->g_yescapement = 0; + pGetWord(p, g->g_width); + pGetWord(p, g->g_height); + pGetWord(p, g->g_xorigin); + g->g_xorigin = Sign16(g->g_xorigin); + pGetWord(p, g->g_yorigin); + g->g_yorigin = Sign16(g->g_yorigin); + break; + } + + case CP_SHORT: + p += 2; /* skip packet len, character code */ + pGet3Byte(p, g->g_rawtfmwidth); + g->g_xescapement = pgetbyte(p) << 16; + g->g_yescapement = 0; + g->g_width = pgetbyte(p); + g->g_height = pgetbyte(p); + g->g_xorigin = pgetbyte(p); + g->g_xorigin = Sign8(g->g_xorigin); + g->g_yorigin = pgetbyte(p); + g->g_yorigin = Sign8(g->g_yorigin); + break; + } + g->g_flags = GF_VALID; + g->g_un.g_details = p; + } + return (0); +} + +/* + * Bit masks for pk_rasterise(). + */ +static char bmask[8] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe}; +static char rbits[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}; + +/* + * Obtain rasters for the specified glyphs. + */ +static int +pk_rasterise(f, l, h) + struct font *f; + int l, h; +{ + struct pk_details *pk0; + struct glyph *g0; + char *p0, *rp0; + int flag, ii; + + if ((pk0 = ftopk(f)) == NULL) + panic("pk_rasterise(%s)", f->f_path); + for (ii = l; ii < h; ii++) { + { + register struct glyph *g; + register char *p; + register int i; + + g = f->f_gly[i = ii]; + if ((g->g_flags & GF_VALID) == 0) + continue; /* no glyph */ + if (!HASRASTER(g)) /* empty raster */ + goto done; + + /* + * Allocate a raster. + */ + rp0 = malloc(((g->g_width + 7) >> 3) * g->g_height); + if ((g->g_raster = rp0) == NULL) + return (-1);/* ??? */ + g->g_rotation = ROT_NORM; + + /* + * Pick up the flag byte, then start at the real + * packet, which we saved in g_details. + */ + if (i < MAXSTD) + p = pk0->pk_cpack[i].cp_packet; + else + p = pk0->pk_morec[i - MAXSTD].cp_packet; + flag = UnSign8(*p); + p0 = g->g_un.g_details; + g0 = g; + } + if ((pk0->pk_dyn_f = flag >> 4) == 14) { + register char *p = p0, *rp = rp0; + register int j, ls, rs, i, w; + + /* + * Expand a bit-packed representation. + * If we get lucky, it is byte packed and + * we can just copy it over. + */ + i = g0->g_height; + j = g0->g_width; + if ((j & 7) == 0) { + bcopy(p, rp, i * (j >> 3)); + goto done; + } + + /* + * No such luck. + */ + w = j; + ls = 0; + while (--i >= 0) { + rs = 8 - ls; + /* know j always != 8 */ + for (j = w; j > 8; j -= 8) { + *rp = *p++ << ls; + *rp++ |= UnSign8(*p) >> rs; + } + + /* + * We need j more bits; there are rs + * bits available at *p. Ask for j, + * which gets min(j, rs). + */ + *rp++ = (*p << ls) & bmask[j]; + /* account for j bits */ + ls += j; ls &= 7; + /* then adjust j based on rs */ + j -= rs; + /* still need j more bits */ + if (j < 0) /* got them all */ + continue; + p++; + if (j == 0) /* there were just enough */ + continue; + /* take j more bits */ + rp[-1] |= UnSign8(*p & bmask[j]) >> rs; + } + } else { + register struct pk_details *pk = pk0; + register int on = flag & 8 ? 0xff : 0; + register char *rowp; /* pointer into this row */ + register int j; /* trimmed run count */ + register int k; /* misc */ + register int b; /* bit index in current col */ + register int i; /* run count */ + register int colsleft; /* columns left this row */ + register int rowsleft; /* rows left */ + static char *row; /* a one-row buffer */ + static int rowsize; /* and its size in bytes */ + int wb; /* row width in bytes */ + + wb = (g0->g_width + 7) >> 3; + if (rowsize < wb) { /* get more row space */ + if (row) + free(row); + /* keep a slop byte */ + row = malloc((unsigned) (wb + 1)); + if (row == NULL) + return (-1); /* ??? */ + rowsize = wb; + } + bzero(row, wb); + rowsleft = g0->g_height; + colsleft = g0->g_width; + pk->pk_repeat = 0; + pk->pk_ptr = p0; + pk->pk_1nyb = 0; /* start on nybble 0 */ + rowp = row; + b = 0; + while (rowsleft > 0) { /* do all rows */ + /* EXPAND IN LINE? */ + i = pk_unpack(pk); + /* + * Work until the run count is exhausted + * (or at least pretty tired). + * + * (Okay, so the joke is stolen!) + */ + while ((j = i) > 0) { + /* + * If the count is more than the + * rest of this row, trim it down. + */ + if (j > colsleft) + j = colsleft; + i -= j; /* call them done */ + /* + * We need k=8-b bits to finish + * up the current byte. If we + * can finish it, do so; the proper + * bits to set are in rbits[k]. + */ + if (j >= (k = 8 - b)) { + j -= k; + colsleft -= k; + *rowp++ |= on & rbits[k]; + b = 0; + } + /* + * Set any full bytes. + */ + while (j >= 8) { + *rowp++ = on; + j -= 8; + colsleft -= 8; + } + /* + * Finally, begin a new byte, or + * add to the current byte, with + * j more bits. We know j <= 8-b. + * (If j==0, we may be writing on + * our slop byte, which is why we + * keep one around....) + */ +if (j > 8-b) panic("pk_rasterise j>8-b"); + *rowp |= (on & bmask[j]) >> b; + colsleft -= j; + b += j; b &= 7; + if (colsleft == 0) { + pk->pk_repeat++; + rowsleft -= pk->pk_repeat; + while (--pk->pk_repeat >= 0) { + bcopy(row, rp0, wb); + rp0 += wb; + } +if (rowsleft == 0 && i) panic("pk_rasterise leftover bits"); + pk->pk_repeat = 0; + rowp = row; + colsleft = g0->g_width; + bzero(row, wb); + b = 0; + } + } + on = 0xff - on; + } + } + +done: + /* + * Successfully converted another glyph. + */ + pk0->pk_gleft--; + } + +if (pk0->pk_gleft < 0) +panic("%s: gleft==%d", f->f_path, pk0->pk_gleft); + if (pk0->pk_gleft == 0) { + free(pk0->pk_base); + if (pk0->pk_morec != NULL) + free((char *) pk0->pk_morec); + free((char *) pk0); + f->f_details = NULL; + } + return (0); +} + +/* + * Discard the font details. + */ +static void +pk_freefont(f) + struct font *f; +{ + register struct pk_details *pk = ftopk(f); + + if (pk != NULL) { + free(pk->pk_base); + if (pk->pk_morec != NULL) + free((char *) pk->pk_morec); + free((char *) pk); + } +} diff --git a/dviware/dvibook/libtex/postamble.h b/dviware/dvibook/libtex/postamble.h new file mode 100644 index 0000000000..6931b46d40 --- /dev/null +++ b/dviware/dvibook/libtex/postamble.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* Definitions for ScanPostAmble */ + +/* + * ScanPostAmble reads the postamble of a DVI file from the (stdio) + * file specified in its first argument. It is handed two pointers to + * functions. The first (ScanPostAmble's second argument) is called + * after the header information has been read, and given a pointer to a + * PostAmbleInfo structure. It is the job of this function to extract the + * required information from this structure (which is deallocated when + * ScanPostAmble returns). + * + * The second function is called once for each font definition occurring in + * the postamble, and is given a pointer to a PostAmbleFont structure. This + * function should do whatever the device needs to read the actual font. + * + * If the DVI file appears malformed, ScanPostAmble will print an error + * message and exit. (Drastic, perhaps, but effective.) + */ + +struct PostAmbleInfo { + i32 pai_PrevPagePointer; /* previous page pointer */ + i32 pai_Numerator; /* numerator from dvi file */ + i32 pai_Denominator; /* denominator from dvi file*/ + i32 pai_DVIMag; /* \magnification */ + i32 pai_TallestPageHeight; /* height of tallest page */ + i32 pai_WidestPageWidth; /* width of widest page */ + int pai_DVIStackSize; /* DVI stack size required */ + int pai_NumberOfPages; /* total number of pages in DVI file */ +}; + +struct PostAmbleFont { + char *paf_name; /* name of font (null terminated) */ + int paf_n1; /* length of first part of name */ + int paf_n2; /* length of second part of name */ + i32 paf_DVIFontIndex; /* font index number */ + i32 paf_DVIChecksum; /* checksum from DVI file */ + i32 paf_DVIMag; /* "at size" */ + i32 paf_DVIDesignSize; /* design size of font */ +}; + +int ScanPostAmble(); diff --git a/dviware/dvibook/libtex/pxlfont.c b/dviware/dvibook/libtex/pxlfont.c new file mode 100644 index 0000000000..a210bf60e3 --- /dev/null +++ b/dviware/dvibook/libtex/pxlfont.c @@ -0,0 +1,304 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/pxlfont.c,v 2.5 89/08/22 21:55:34 chris Exp $"; +#endif + +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <errno.h> +#include "types.h" +#include "error.h" +#include "font.h" +#include "fio.h" + +/* + * PXL font operations. + */ +static int pxl_read(), pxl_getgly(), pxl_rasterise(); +static void pxl_freefont(); + +struct fontops pxlops = + { "pxl", 0, 5.0, pxl_read, pxl_getgly, pxl_rasterise, pxl_freefont }; + +/* + * Local info. + */ +#define PXLID 1001 /* ID denoting PXL files */ +#define TAILSIZE (517 * 4) /* size of pxl tail info */ + +/* + * pc describes one PXL character information block. + */ +struct pc { + i16 pc_width; /* character width (pixels) */ + i16 pc_height; /* character height (pixels) */ + i16 pc_xoffset; /* X offset of reference point */ + i16 pc_yoffset; /* Y offset of reference point */ + i32 pc_rastoff; /* raster offset */ + i32 pc_TFMwidth; /* TFM width (FIXes) */ +}; + +/* + * pxl_details are the PXL-specific font details. + * + * We keep track of the number of characters converted to internal + * glyph form, and when all have been done, we discard the now- + * useless details. + */ +struct pxl_details { + int pd_nconv; /* number of characters converted */ + char *pd_ras; /* raster space, until converted */ + struct pc pd_pc[128]; /* `font directory' */ +}; + +/* + * Get the pxl_details from font f. + */ +#define ftopd(f) ((struct pxl_details *) (f)->f_details) + +extern errno; +char *malloc(); + +/* + * Read a PXL file. + */ +static int +pxl_read(f, fd) + struct font *f; + int fd; +{ + register struct pxl_details *pd; + register FILE *fp; + register struct pc *pc; + register int i; + int saverr; + i32 pxlid; + struct stat st; + + fp = NULL; + if ((pd = (struct pxl_details *)malloc(sizeof (*pd))) == 0) + goto fail; + pd->pd_ras = 0; + + /* + * There should be 4n bytes, with an absolute minimum of TAILSIZE+4 + * (+4 for the initial PXLID). + */ + (void) fstat(fd, &st); + if ((st.st_size & 3) != 0 || st.st_size < (TAILSIZE + 4)) { + errno = EINVAL; + goto fail; + } + + /* + * Set up the raster pointer (if we need rasters). + */ + if (f->f_flags & FF_RASTERS) { + i = st.st_size - (TAILSIZE + 4); + if (i != 0) { + if ((pd->pd_ras = malloc((unsigned) i)) == 0) + goto fail; + (void) lseek(fd, 4L, 0); + if (read(fd, pd->pd_ras, i) != i) + goto fail; + } + } + + /* + * Read the glyph information. + */ + errno = 0; /* try to make errno meaningful */ + if ((fp = fdopen(fd, "r")) == NULL) { + if (errno == 0) + errno = EMFILE; + goto fail; + } + (void) fseek(fp, (long) -TAILSIZE, 2); + for (i = 128, pc = pd->pd_pc; --i >= 0; pc++) { + fGetWord(fp, pc->pc_width); + fGetWord(fp, pc->pc_height); + fGetWord(fp, pc->pc_xoffset); + fGetWord(fp, pc->pc_yoffset); + fGetLong(fp, pc->pc_rastoff); + fGetLong(fp, pc->pc_TFMwidth); + } + f->f_checksum = GetLong(fp); + (void) GetLong(fp); /* mag */ + f->f_design_size = GetLong(fp); /* designsize */ + f->f_hppp = 0; /* ??? maybe 200? */ + f->f_vppp = 0; + (void) GetLong(fp); /* dirpointer */ + pxlid = GetLong(fp); + if (pxlid != PXLID) + error(0, 0, "Warning: strange PXL id (%d) in \"%s\"", + pxlid, f->f_path); + + f->f_details = (char *)pd; + if (FontHasGlyphs(f, 0, 128)) + goto fail; + (void) fclose(fp); + pd->pd_nconv = 0; + return (0); + +fail: + saverr = errno; + if (pd) { + if (pd->pd_ras) + free(pd->pd_ras); + free((char *)pd); + } + if (fp) + (void) fclose(fp); + else + (void) close(fd); + errno = saverr; + return (-1); +} + +/* + * Obtain the specified range of glyphs. + */ +static int +pxl_getgly(f, l, h) + register struct font *f; + int l; + register int h; +{ + register struct glyph *g; + register struct pc *pc; + register int i; + struct pxl_details *pd; + + if ((pd = ftopd(f)) == NULL) + error(1, 0, "pxl_getgly details==0: cannot happen"); + /* + * By definition, all 128 glyphs are valid. Just copy the + * PXL information. + */ + for (pc = &pd->pd_pc[i = l]; i < h; i++, pc++) { + g = f->f_gly[i]; + g->g_flags = GF_VALID; + g->g_height = pc->pc_height; + g->g_width = pc->pc_width; + g->g_yorigin = pc->pc_yoffset; + g->g_xorigin = pc->pc_xoffset; + g->g_rawtfmwidth = pc->pc_TFMwidth; + } + return (0); +} + +/* + * Helper function for rasterise: return a pointer to a converted + * (malloc()ed and minimised) raster. + */ +static char * +makeraster(h, w, rp) + register int h, w; + register char *rp; +{ + register char *cp; + register int i, o; + char *rv; + + /* + * The height and width values are in bits. Convert width to + * bytes, rounding up. The raw raster (at rp) is almost what + * we want, but not quite: it has `extra' bytes at the end of + * each row, to pad out to a multiple of four bytes. + */ + w = (w + 7) >> 3; + o = (4 - w) & 3; /* offset (number of `extra' bytes) */ + if ((cp = malloc((unsigned) (h * w))) == NULL) + return (NULL); + if (o == 0) { + /* + * The raster fits exactly; just copy it to the allocated + * memory space. (We must copy anyway, so that glyphs + * can be freed, e.g., after rotation.) + */ + bcopy(rp, cp, h * w); + return (cp); + } + rv = cp; + while (--h >= 0) { + /* + * Copy each row, then skip over the extra stuff. + */ + for (i = w; --i >= 0;) + *cp++ = *rp++; + rp += o; + } + return (rv); +} + +/* + * Obtain rasters for the specified glyphs. + */ +static int +pxl_rasterise(f, l, h) + register struct font *f; + int l; + register int h; +{ + register struct glyph *g; + register struct pc *pc; + register int i; + register struct pxl_details *pd; + + if ((pd = ftopd(f)) == NULL) + error(1, 0, "pxl_rasterise details==0: cannot happen"); + if (pd->pd_ras == NULL) + error(1, 0, "pxl_rasterise pd_ras==NULL: cannot happen"); + for (pc = &pd->pd_pc[i = l]; i < h; i++, pc++) { + g = f->f_gly[i]; + if (pc->pc_rastoff == 0) { + /* + * g should not claim a raster, since it has none. + */ + if (HASRASTER(g)) + error(1, 0, "bad PXL glyph %d in \"%s\"", + g->g_index, f->f_path); + } else { + g->g_raster = makeraster(pc->pc_height, pc->pc_width, + pd->pd_ras + ((pc->pc_rastoff - 1) << 2)); + if (g->g_raster == NULL) + return (-1); /* ??? */ + g->g_rotation = ROT_NORM; + } + } + + /* + * If we have converted all the characters, dump the + * pre-conversion rasters. In fact, dump everything. + */ + pd->pd_nconv += h - l; + if (pd->pd_nconv == 128) { + free(pd->pd_ras); + free((char *)pd); + f->f_details = NULL; + } + return (0); +} + +/* + * Discard the font details. + */ +static void +pxl_freefont(f) + struct font *f; +{ + struct pxl_details *pd; + + if ((pd = ftopd(f)) != NULL) { + if (pd->pd_ras != NULL) + free(pd->pd_ras); + free((char *)pd); + } +} diff --git a/dviware/dvibook/libtex/rotate.c b/dviware/dvibook/libtex/rotate.c new file mode 100644 index 0000000000..5895e30d9a --- /dev/null +++ b/dviware/dvibook/libtex/rotate.c @@ -0,0 +1,154 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/rotate.c,v 2.4 89/08/22 21:56:05 chris Exp $"; +#endif + +/* + * Routines to generate rotated bitmaps given unrotated inputs. + * + * The rotated bitmap is indistinguishable from the unrotated one (aside + * from being rotated of course!). + */ + +#include "types.h" +#include "error.h" +#include "font.h" + +static void RotateClockwise(); + +extern int errno; +char *malloc(); + +/* + * Rounding, but by powers of two only. + */ +#define ROUND(n,r) (((n) + ((r) - 1)) & ~((r) - 1)) + +/* + * Set the rotation of glyph g to r. + */ +SetRotation(g, r) + register struct glyph *g; + int r; +{ + + if (r < ROT_NORM || r > ROT_RIGHT) + error(1, 0, "bad rotation value %d", r); + + /* + * The null glyph is trivial to rotate by any amount. + * + * Note that this assumes that any raster has been obtained + * BEFORE calling SetRotation()! + */ + if (g->g_raster == NULL) { + g->g_rotation = r; + return; + } + + /* + * This is hardly efficient, but it *is* expedient.... + */ + while (g->g_rotation != r) { + RotateClockwise(g); + g->g_rotation = (g->g_rotation - 1) & 3; + } +} + +/* + * Rotation by 1/4 turn clockwise (from ROT_NORM to ROT_RIGHT, e.g.). + */ +static void +RotateClockwise(glyph) + struct glyph *glyph; +{ + register char *nrast; /* new raster */ + register char *orast; /* old raster */ + register int oheight; /* old raster height, new raster width */ + register int owidth; /* old raster width, new raster height */ + unsigned int size; /* size of new raster (in bytes) */ + int nplus; /* offset between rows in nrast */ + + /* + * First, get a new raster. + */ + { + register struct glyph *g = glyph; + register int t; + + oheight = g->g_height; + owidth = g->g_width; + + /* + * Offset is (new width) rounded to bytes. + */ + nplus = ROUND(oheight, 8) >> 3; + + /* + * Size of new raster is (new height) * (new rounded width, + * in bytes). + */ + size = nplus * owidth; + if ((nrast = malloc(size)) == NULL) + error(1, errno, "out of memory"); + bzero(nrast, size); + + /* + * New y origin is old x origin; new x origin is old height + * minus old y origin - 1. + */ + t = g->g_yorigin; + g->g_yorigin = g->g_xorigin; + g->g_xorigin = oheight - t - 1; + + /* While we are at it, exchange height & width... */ + g->g_height = owidth; + g->g_width = oheight; + + /* and grab a pointer to the old raster. */ + orast = g->g_raster; + } + + /* + * Now copy bits from the old raster to the new one. The mapping + * function is + * + * for i in [0..height) + * for j in [0..width) + * new[j, height-i-1] = old[i, j] + * + * Thus i maps to height-i-1 and (since we have to do our own 2 + * dimensional array indexing) j to nplus*j. We call the mapped + * variables mapi and mapj, and, since we scan sequentially through + * the old raster, can discard the original i and j. + */ + { + register int mapj, c, k, mapi; + + mapi = oheight; + owidth *= nplus; + while (--mapi >= 0) { + k = 7; + for (mapj = 0; mapj < owidth; mapj += nplus) { + if (++k == 8) /* get another byte */ + c = *orast++, k = 0; + if (c & 0x80) /* old[i,j] was set */ + nrast[mapj + (mapi >> 3)] |= + 1 << (7 - (mapi & 7)); + c <<= 1; + } + } + } + + /* + * Finally, free the storage associated with the original raster. + */ + free(glyph->g_raster); + glyph->g_raster = nrast; +} diff --git a/dviware/dvibook/libtex/rstfont.c b/dviware/dvibook/libtex/rstfont.c new file mode 100644 index 0000000000..693d876263 --- /dev/null +++ b/dviware/dvibook/libtex/rstfont.c @@ -0,0 +1,299 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + * + * The code in this file was partially derived from another version by + * Tim Morgan at UCI. + * + * TODO: + * test + */ + +#ifndef lint +static char rcsid[] = "$Header$"; +#endif + +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <errno.h> +#include "types.h" +#include "error.h" +#include "font.h" +#include "fio.h" + +/* + * RST (Imagen's font format) font operations. + */ +static int rst_read(), rst_getgly(), rst_rasterise(); +static void rst_freefont(); + +#ifdef UCI +#define D 1.0 /* I think */ +#else +#define D 150.0 +#endif + +struct fontops rstops = + { "rst", 0, D, rst_read, rst_getgly, rst_rasterise, rst_freefont }; + +#undef D + +/* + * Local info. + */ +#define FILEMARK_SIZE 8 /* header `file mark' bytes to skip */ + +/* + * rc describes one RST character information block. + */ +struct rc { + i16 rc_height; /* character height (pixels) */ + i16 rc_width; /* character width (pixels) */ + i16 rc_yoffset; /* Y offset of reference point */ + i16 rc_xoffset; /* X offset of reference point */ + i32 rc_rastoff; /* raster offset */ + i32 rc_TFMwidth; /* TFM width */ +}; + +/* + * rst_details are the RST-specific font details. + * + * We keep track of the number of characters converted to internal + * glyph form, and when all have been done, we discard the now- + * useless details. + */ +struct rst_details { + int rd_unconv; /* number of characters unconverted */ + struct rc *rd_rc; /* `font directory' */ + char *rd_ras; /* raster space, until converted */ + long rd_rastbase; /* adjust rc_rastoff by rastbase */ +}; + +/* + * Get the rst_details from font f. + */ +#define ftord(f) ((struct rst_details *)(f)->f_details) + +extern errno; +char *malloc(); +long ftell(); + +/* + * Read an RST file. + */ +static int +rst_read(f, fd) + struct font *f; + int fd; +{ + register struct rst_details *rd; + register FILE *fp; + register struct rc *rc; + register int i; + int saverr; + long presize, diroff; + i32 firstgly, lastgly; + + fp = NULL; + if ((rd = (struct rst_details *)malloc(sizeof *rd)) == 0) + goto fail; + rd->rd_ras = 0; + rd->rd_rc = 0; + + /* + * Read the header. + */ + errno = 0; /* try to make errno meaningful */ + if ((fp = fdopen(fd, "r")) == NULL) { + if (errno == 0) + errno = EMFILE; + goto fail; + } + (void) fseek(fp, (long)FILEMARK_SIZE, 0); + fGetWord(fp, presize); /* bytes in preamble (from FILEMARK_SIZE+2) */ + presize += 2 + FILEMARK_SIZE; + i = getc(fp); /* version */ + if (i != 0) { + error(0, 0, "Warning: strange RST version (%d) in \"%s\"", i, + f->f_path); + errno = EINVAL; + goto fail; + } + fGet3Byte(fp, diroff); /* offset to glyph directory */ + fGetWord(fp, firstgly); /* first glyph */ + fGetWord(fp, lastgly); /* last glyph (inclusive) */ + lastgly++; /* make it exclusive */ + + (void) GetLong(fp); /* mag */ + fGetLong(fp, f->f_design_size); + /* interline, interword, rotation, charadv, lineadv */ + (void) fseek(fp, (long)(4 + 4 + 2 + 1 + 1), 1); + fGetLong(fp, f->f_checksum); + f->f_hppp = 0; /* ??? */ + f->f_vppp = 0; + + /* + * Read the glyph directory. + */ + i = lastgly - firstgly; + rd->rd_rc = (struct rc *)malloc((unsigned) (i * sizeof(*rc))); + if (rd->rd_rc == 0) + goto fail; + (void) fseek(fp, presize, 0); + for (rc = rd->rd_rc; --i >= 0; rc++) { + fGetWord(fp, rc->rc_height); + fGetWord(fp, rc->rc_width); + fGetWord(fp, rc->rc_yoffset); + fGetWord(fp, rc->rc_xoffset); + fGetLong(fp, rc->rc_TFMwidth); + fGet3Byte(fp, rc->rc_rastoff); + } + + /* + * Set up the raster pointer (if we need rasters). + */ + if (f->f_flags & FF_RASTERS) { + struct stat st; + + rd->rd_rastbase = ftell(fp); + /* back to unbuffered I/O */ + (void) fstat(fd, &st); + i = st.st_size - presize - 15 * (lastgly - firstgly); + if (i < 0) + error(1, 0, "calculated raster size < 0 for \"%s\"", + f->f_path); + if (i != 0) { + if ((rd->rd_ras = malloc((unsigned) i)) == 0) + goto fail; + (void) lseek(fd, rd->rd_rastbase, 0); + if (read(fd, rd->rd_ras, i) != i) + goto fail; + } + } + + f->f_details = (char *) rd; + if (FontHasGlyphs(f, firstgly, lastgly)) + goto fail; + + (void) fclose(fp); + rd->rd_unconv = lastgly - firstgly; + return (0); + +fail: + saverr = errno; + if (rd) { + if (rd->rd_rc) + free((char *)rd->rd_rc); + if (rd->rd_ras) + free(rd->rd_ras); + free((char *) rd); + } + if (fp) + (void) fclose(fp); + else + (void) close(fd); + errno = saverr; + return (-1); +} + +/* + * Obtain the specified range of glyphs. + */ +static int +rst_getgly(f, l, h) + register struct font *f; + int l; + register int h; +{ + register struct glyph *g; + register struct rc *rc; + register int i; + struct rst_details *rd; + + if ((rd = ftord(f)) == NULL) + panic("rst_getgly"); + + /* + * By definition, all glyphs are valid. Just copy the + * RST information. + */ + for (rc = &rd->rd_rc[(i = l) - f->f_lowch]; i < h; i++, rc++) { + g = f->f_gly[i]; + g->g_flags = GF_VALID; + g->g_height = rc->rc_height; + g->g_width = rc->rc_width; + g->g_yorigin = rc->rc_yoffset; + g->g_xorigin = rc->rc_xoffset; + g->g_rawtfmwidth = rc->rc_TFMwidth; + } + return (0); +} + +/* + * Obtain rasters for the specified glyphs. + */ +static int +rst_rasterise(f, l, h) + register struct font *f; + int l; + register int h; +{ + register struct glyph *g; + register struct rc *rc; + register struct rst_details *rd; + register int i; + unsigned sz; + + if ((rd = ftord(f)) == NULL) + panic("rst_rasterise details==0"); + if (rd->rd_ras == NULL) + panic("rst_rasterise rd_ras==NULL"); + for (rc = &rd->rd_rc[(i = l) - f->f_lowch]; i < h; i++, rc++) { + g = f->f_gly[i]; + if (!HASRASTER(g)) + continue; + if (rc->rc_rastoff == 0) { + error(1, 0, "bad RST glyph %d in \"%s\"", + g->g_index, f->f_path); + } else { + sz = ((g->g_width + 7) >> 3) * g->g_height; + if ((g->g_raster = malloc(sz)) == NULL) + return (-1); /* ??? */ + g->g_rotation = ROT_NORM; + bcopy(rd->rd_ras + rc->rc_rastoff - rd->rd_rastbase, + g->g_raster, sz); + } + } + + /* + * If we have converted all the characters, dump the + * pre-conversion rasters. In fact, dump everything. + */ + rd->rd_unconv -= h - l; + if (rd->rd_unconv == 0) { + free((char *)rd->rd_rc); + free(rd->rd_ras); + free((char *)rd); + f->f_details = NULL; + } + return (0); +} + +/* + * Discard the font details. + */ +static void +rst_freefont(f) + struct font *f; +{ + struct rst_details *rd; + + if ((rd = ftord(f)) != NULL) { + free((char *)rd->rd_rc); + free(rd->rd_ras); + free((char *)rd); + } +} diff --git a/dviware/dvibook/libtex/scaletfm.c b/dviware/dvibook/libtex/scaletfm.c new file mode 100644 index 0000000000..f6444d5d36 --- /dev/null +++ b/dviware/dvibook/libtex/scaletfm.c @@ -0,0 +1,120 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/scaletfm.c,v 3.2 89/11/23 16:34:43 chris Exp $"; +#endif + +#include "types.h" +#include "error.h" +#include "font.h" + +/* + * From DVITYPE.WEB: + * + * ``The most important part of in_TFM is the width computation, which + * involvles multiplying the relative widths in the TFM file by the scaling + * factor in the DVI file. This fixed-point multiplication must be done with + * precisely the same accuracy by all DVI-reading programs, in order to + * validate the assumptions made by DVI-writing programs like \TeX 82. + * + * Let us therefore summarize what needs to be done. Each width in a TFM + * file appears as a four-byte quantity called a fix_word. A fix_word whose + * respective bytes are (a,b,c,d) represents the number + * + * {{ b * 2^{-4} + c * 2^{-12} + d * 2^{-20}, if a = 0; + * x = {{ + * {{ -16 + b * 2^{-4} + c * 2^{-12} + d * 2^{-20}, if a = 255. + * + * (No other choices of a are allowed, since the magnitude of a TFM dimension + * must be less than 16.) We want to multiply this quantity by the integer + * z, which is known to be less than 2^{27}. Let \alpha = 16z. If z < + * 2^{23}, the individual multiplications b * z, c * z, d * z cannot + * overflow; otherwise we will divide z by 2, 4, 8, or 16, to obtain a + * multiplier less than 2^{23}, and we can compensate for this later. If z + * has thereby been replaced by z' = z/2^e, let \beta = 2^{4-e}; we shall + * compute + * + * \lfloor (b + c * 2^{-8} + d * 2^{-16})z' / \beta \rfloor + * + * if a = 0, or the same quantity minus \alpha if a = 255. This calculation + * must be done exactly, for the reasons stated above; the following program + * does the job in a system-independent way, assuming that arithmetic is + * exact on numbers less than 2^{31} in magnitude.'' + */ +#define ZLIM ((i32)1 << 23) /* z must be < zlim */ + +/* + * Scale the single TFM width t by z. + */ +i32 +ScaleOneWidth(t, z) + register i32 t, z; +{ + register i32 alpha, log2beta; + register int a0; + + /* First compute \alpha, \beta, and z': */ + alpha = 16 * z; + log2beta = 4; + while (z >= ZLIM) { + z >>= 1; + log2beta--; + } + + /* The four values 'a', 'b', 'c', and 'd' are fields within t: */ +#define a (UnSign8(t >> 24)) +#define b (UnSign8(t >> 16)) +#define c (UnSign8(t >> 8)) +#define d (UnSign8(t)) + if (t) { + a0 = a; + t = (((((d * z) >> 8) + c * z) >> 8) + b * z) >> log2beta; + if (a0) { + if (a0 != 255) + error(0, 0, "bad TFM width! [ScaleOneWidth]"); + t -= alpha; + } + } + return (t); +} + +/* + * Scale a set of glyphs [l..h) in font f according to f->f_dvimag + * (that is, set g_tfmwidth from g_rawtfmwidth). + */ +ScaleGlyphs(f, l, h) + register struct font *f; + int l, h; +{ + register int i, a0; + register i32 t, z, alpha, log2beta; + + z = f->f_dvimag; + alpha = 16 * z; + log2beta = 4; + while (z >= ZLIM) { + z >>= 1; + log2beta--; + } + + for (i = l; i < h; i++) { + if ((t = f->f_gly[i]->g_rawtfmwidth) != 0) { + a0 = a; + t = (((((d * z) >> 8) + c * z) >> 8) + b * z) >> + log2beta; + if (a0) { + if (a0 != 255) + error(0, 0, + "\"%s\", glyph %d: bad TFM width", + f->f_path, i); + t -= alpha; + } + } + f->f_gly[i]->g_tfmwidth = t; + } +} diff --git a/dviware/dvibook/libtex/scanpost.c b/dviware/dvibook/libtex/scanpost.c new file mode 100644 index 0000000000..f71b33c9b5 --- /dev/null +++ b/dviware/dvibook/libtex/scanpost.c @@ -0,0 +1,111 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/local/src/SeeTeX/libtex/RCS/scanpost.c,v 1.2 90/07/10 14:33:23 grunwald Exp Locker: grunwald $"; +#endif + +/* + * ScanPostAmble - read a DVI postamble. + */ + +#include <stdio.h> +#include "types.h" +#include "dvicodes.h" +#include "fio.h" +#include "gripes.h" +#include "postamble.h" + +ScanPostAmble(f, headerfunc, fontfunc) + register FILE *f; + void (*headerfunc)(); + register void (*fontfunc)(); +{ + register int n; + register char *s; + char name[512]; + + if (FindPostAmble(f)) { + GripeCannotFindPostamble(); + return(1); + } + if (GetByte(f) != Sign8(DVI_POST)) { + GripeMissingOp("POST"); + return(1); + } + + /* Read the postamble info stuff. */ + { + struct PostAmbleInfo pai; + register struct PostAmbleInfo *p = &pai; + + p->pai_PrevPagePointer = GetLong(f); + p->pai_Numerator = GetLong(f); + p->pai_Denominator = GetLong(f); + p->pai_DVIMag = GetLong(f); + p->pai_TallestPageHeight = GetLong(f); + p->pai_WidestPageWidth = GetLong(f); + p->pai_DVIStackSize = GetWord(f); + p->pai_NumberOfPages = GetWord(f); + + (*headerfunc)(p); + } + + /* Now read all the font definitions. */ + { + struct PostAmbleFont paf; + register struct PostAmbleFont *p = &paf; + int c; + + for (;;) { + if ((c = getc(f)) == EOF) + GripeUnexpectedDVIEOF(); + switch (c) { + + case DVI_NOP: + break; + + case DVI_FNTDEF1: + p->paf_DVIFontIndex = UnSign8(getc(f)); + break; + + case DVI_FNTDEF2: + p->paf_DVIFontIndex = UnSign16(GetWord(f)); + break; + + case DVI_FNTDEF3: + p->paf_DVIFontIndex = UnSign24(Get3Byte(f)); + break; + + case DVI_FNTDEF4: + p->paf_DVIFontIndex = GetLong(f); + break; + + case DVI_POSTPOST: + return(0); + + default: + GripeMissingOp("POSTPOST"); + return(1); + /*NOTREACHED*/ + } + p->paf_DVIChecksum = GetLong(f); + p->paf_DVIMag = GetLong(f); + p->paf_DVIDesignSize = GetLong(f); + p->paf_n1 = UnSign8(getc(f)); + p->paf_n2 = UnSign8(getc(f)); + p->paf_name = name; /* never trust people not to + clobber it */ + n = p->paf_n1 + p->paf_n2; + s = name; + while (--n >= 0) + *s++ = GetByte(f); + *s = 0; + (*fontfunc)(p); + } + } +} diff --git a/dviware/dvibook/libtex/sdecode.c b/dviware/dvibook/libtex/sdecode.c new file mode 100644 index 0000000000..942b63f9c4 --- /dev/null +++ b/dviware/dvibook/libtex/sdecode.c @@ -0,0 +1,642 @@ +/* + * Copyright (c) 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/sdecode.c,v 3.4 89/11/06 15:01:07 chris Exp $"; +#endif + +#include <stdio.h> +#include <varargs.h> +#include "types.h" +#include "sdecode.h" + +static char cclass[256]; /* XXX assumes 8-bit char */ +#define CCL_SPACE 0x01 /* white space */ +#define CCL_SEMI 0x02 /* `semicolon' (statement separator) */ +#define isstopc(c) (cclass[c] != 0) /* both space and semi char */ +static int sdset; /* flag says whether cclass[] set up */ + +static void args(), badarg(); +static int scan(), scan_i(), scan_d(); + +extern char *strsave(); + +extern char *DVIFileName; + +#ifndef BLOCK_COPY +/* + * Copy the text at `src' `downward' in memory for `len' bytes. + * This is like strcpy or memcpy, but handles one direction of + * overlap (when dst < src). + */ +static void +movedown(src, dst, len) + register char *src, *dst; + register int len; +{ + + while (--len >= 0) + *dst++ = *src++; +} +#endif + +/* + * We have a number of states to do word interpretation. Outside words, + * we skip blanks and `semicolons'. The first non-blank, non-`semi' enters + * WORD state; in WORD state, double quote characters switch to + * QUOTE state, after which another double quote switches back to + * WORD state, and blanks and `semi' end WORD state, switching to DONE. + * While in WORD or QUOTE states, `c produces c for all characters c; + * to do this, we use two more states. + */ +#define S_DULL 0 /* not in a word */ +#define S_WORD 1 /* in a word */ +#define S_QUOTE 2 /* inside "" in a word */ +#define S_BKQT1 3 /* not in "", but just read one ` */ +#define S_BKQT2 4 /* in "", but just read one ` */ +#define S_DONE 5 /* found end of word */ + +/* + * Structure used to communicate between the various functions. + */ +struct decode_info { + /* data used to read from input file */ + int di_nch; /* number of characters remaining in buffer */ + char *di_cp; /* next character to be read from buffer */ + long di_nfilech; /* number of characters remaining in file */ + FILE *di_file; /* the file itself */ + + /* information used and/or returned by the canon routine */ + int di_lexstate; /* lexical state */ + int di_call; /* set when we reach terminating `semicolon' */ + + /* information used more globally */ + char *di_word; /* last word from word() */ + char *di_kw; /* the keyword */ + int di_bkw; /* true iff keyword is in static buffer */ +}; + +/* + * `Edit' the text in the buffer in-place. + * Return the count of characters resulting from the edit (e.g., + * from `foo`"bar' the count would be 7). + * Set di_call if we reach state S_DONE by encountering a `semicolon'. + * The edited text begins wherever di_cp points. + * We begin the edit in the state given by di_lexstate. + */ +static int +canon(di) + register struct decode_info *di; +{ + register int state, c, n; + register char *cp, *outp, *s; + + state = di->di_lexstate; + n = di->di_nch; + cp = outp = di->di_cp; + while (--n >= 0) { + c = *cp++; + switch (state) { + + case S_DULL: + if (isstopc(c)) + continue; + state = S_WORD; + /* FALLTHROUGH */ + + case S_WORD: + if (c == '"') { + state = S_QUOTE; + continue; + } + if (c == '`') { + state = S_BKQT1; + continue; + } + /* look for `semicolon' (call) characters */ + if (cclass[c] & CCL_SEMI) + di->di_call = 1; + if (isstopc(c)) { + state = S_DONE; + goto done; + } + break; + + case S_QUOTE: + if (c == '`') { + state = S_BKQT2; + continue; + } + if (c == '"') { + state = S_WORD; + continue; + } + break; + + case S_BKQT1: + state = S_WORD; + break; + + case S_BKQT2: + state = S_QUOTE; + break; + + default: + panic("sdecode canon"); + /* NOTREACHED */ + } + *outp++ = c; + } + n = 0; +done: + c = outp - di->di_cp; /* wrote this many chars */ + di->di_lexstate = state;/* save new state */ + di->di_nch = n; /* and new counts and pointers */ + di->di_cp = cp; + return (c); +} + +#define WDMAX BUFSIZ /* when testing, make this small */ + +/* + * Read a word from the input file. + * Returns NULL (and sets di_call) at end of \special. + * + * We gather up to WDMAX + k characters at a time into an input + * buffer, and lex them using `canon' above. The result is at most + * as long as the original text. Since a single legal word might + * be up to WDMAX bytes long, k must be at least 1 (for the trailing + * NUL). If the input word is longer, we flush the trailing part, + * using the k `extra' bytes (and again k must be at least 1). + * + * If there is a current keyword and it is in the current buffer + * (di_bkw != 0), and we have to move existing buffer text around + * or write over it with new file text, we save the keyword and + * clear di_bkw. + */ +static char * +word(di, quietly) + register struct decode_info *di; + int quietly; +{ + register int n; + register char *p; + register int len, trunc = 0; + static char inbuf[WDMAX + 20]; /* k=20 */ + + len = 0; /* no word bytes yet */ + p = di->di_cp; /* where canon() writes (nil if first time) */ + di->di_lexstate = S_DULL; + /* + * Loop invariants: + * len is in [0..WDMAX) + * di_lexstate != S_DONE + */ + do { + /* if the buffer is empty, refill it */ + if (di->di_nch == 0) { + /* compute remaining buffer space */ + n = sizeof inbuf - len; + /* read n bytes or rest of string, whichever smaller */ + if (di->di_nfilech < n && (n = di->di_nfilech) == 0) { + /* `eof': fake up a call-terminator */ + di->di_call = 1; + if (di->di_lexstate == S_DULL) + return (di->di_word = NULL); + break; /* take partial word */ + } + /* save keyword if this will clobber it */ + if (di->di_bkw) { + di->di_kw = strsave(di->di_kw); + di->di_bkw = 0; + } + /* if there is some canon text, move it to front */ + if (len && p > inbuf) { +#ifdef BLOCK_COPY + bcopy(p, inbuf, len); +#else + movedown(p, inbuf, len); +#endif + } + /* canon text is (or will be) at front now */ + p = inbuf; + di->di_cp = p + len; + if (fread(di->di_cp, 1, n, di->di_file) != n) { + if (ferror(di->di_file)) + error(1, -1, "error reading %s", + DVIFileName); + GripeUnexpectedDVIEOF(); + } + di->di_nfilech -= n; + di->di_nch = n; + } + n = canon(di); + if ((len += n) > WDMAX) { + len = WDMAX; + trunc = 1; + } + } while (di->di_lexstate != S_DONE); + p[len] = 0; + + /* + * If the word was too long (and hence truncated), complain + * (unless we are quietly skipping over the remains of a + * broken \special). + */ + if (trunc && !quietly) { + /* word was too long; complain */ + error(0, 0, +#if WDMAX > 20 + "over-long word in \\special: `%.20s...'", +#else + "over-long word in \\special: `%s...'", +#endif + p); + error(0, 0, "(truncated to %d characters)", WDMAX); + } + return (di->di_word = p); +} + +/* + * Initialise the character class table. + */ +void +SDsetclass(spaces, semis) + register char *spaces; + register char *semis; +{ + register int c; + + if (spaces == NULL) + spaces = "\b\t\n\f\r "; + if (semis == NULL) + semis = ";\n"; + + for (c = 0; c < sizeof cclass / sizeof *cclass; c++) + cclass[c] = 0; + while ((c = *spaces++) != 0) + cclass[c] |= CCL_SPACE; + while ((c = *semis++) != 0) + cclass[c] |= CCL_SEMI; + sdset = 1; +} + +/* + * Perform a binary search for the given string in the given table. + */ +static struct sdecode * +lookup(str, p, max) + register char *str; + register struct sdecode *p; + register int max; +{ + register struct sdecode *m; + register int dir; + + while (max > 0) { + /* + * Look at the median; pick the right hand one if + * there are two such entries (when `max' is even). + * Set dir positive if str is `greater than' m->sd_name, + * negative if it is `less'. If not equal, move + * left or right accordingly. When max is even and + * we move right, we have to use (max-1)/2 since we + * broke the tie towards the right. + */ + m = p + (max >> 1); + if ((dir = *str - *m->sd_name) == 0) + dir = strcmp(str, m->sd_name); + if (dir == 0) /* found */ + return (m); + if (dir > 0) { /* str > mid: move right */ + p = m + 1; + max = (max - 1) >> 1; + } else /* str < mid: move left */ + max >>= 1; + } + return (NULL); +} + +/* + * Decode a series of \special keywords. + * + * The table points to a sorted list of keywords to match. + * On a match, the appropriate function is called with appropriate + * arguments. + * + * Characters in `semi' (if not NULL) act like `statement separators'. + * For instance, decoding with semi=>";\n" means that + * + * \special{foo 1; bar 2 4^^Jbaz 3} + * + * tries to call foo(1), bar(2,4), and baz(3) (in that order). + */ +void +SDecode(fp, len, table, tsize) + FILE *fp; + register i32 len; + struct sdecode *table; + int tsize; +{ + register char *cp; + register struct sdecode *tp; + register int h; + struct decode_info di; + + /* set defaults, if necessary */ + if (!sdset) + SDsetclass((char *)NULL, (char *)NULL); + + di.di_nch = 0; + di.di_cp = NULL; + di.di_nfilech = len; + di.di_file = fp; + di.di_bkw = 0; + for (;;) { + di.di_call = 0; + if ((cp = word(&di, 0)) == NULL) + return; /* (all done) */ + tp = lookup(cp, table, tsize); + if (tp == NULL) { + error(0, 0, + "Warning: unrecognised \\special keyword `%s' ignored", cp); + /* di_call is not set, so fall through */ + } else { + di.di_kw = cp; + di.di_bkw = 1; + args(tp, &di); + if (!di.di_call) + error(0, 0, + "Warning: extra arguments to \\special `%s' ignored", di.di_kw); + if (!di.di_bkw) + free(di.di_kw); + di.di_bkw = 0; + } + /* eat extra arguments, or arguments to an unknown function */ + while (!di.di_call) + (void) word(&di, 1); + } +} + +/* + * Gather up the arguments to the given function, then (if all goes well) + * call that function with those arguments. On return, if di_call is not + * set, there were extra arguments left over. + */ +static void +args(tp, di) + register struct sdecode *tp; + register struct decode_info *di; +{ + char *fmt; + int n, room, f1; + i32 *ip, i[4]; + double d[2]; + + switch (tp->sd_args) { + + case sda_none: + (*tp->sd_fn)(tp->sd_name); + return; + + case sda_s: + fmt = "s"; + if (scan(di, &fmt)) + break; + (*tp->sd_fn)(tp->sd_name, di->di_word); + return; + + case sda_d: + fmt = "d"; + if (scan(di, &fmt, &i[0])) + break; + (*tp->sd_fn)(tp->sd_name, i[0]); + return; + + case sda_f: + fmt = "f"; + if (scan(di, &fmt, &d[0])) + break; + (*tp->sd_fn)(tp->sd_name, d[0]); + return; + + case sda_dd: + fmt = "dd"; + if (scan(di, &fmt, &i[0], &i[1])) + break; + (*tp->sd_fn)(tp->sd_name, i[0], i[1]); + return; + + case sda_ff: + fmt = "ff"; + if (scan(di, &fmt, &d[0], &d[1])) + break; + (*tp->sd_fn)(tp->sd_name, d[0], d[1]); + return; + + case sda_ddddff: + fmt = "ddddff"; + if (scan(di, &fmt, &i[0], &i[1], &i[2], &i[3], &d[0], &d[1])) + break; + (*tp->sd_fn)(tp->sd_name, i[0], i[1], i[2], i[3], d[0], d[1]); + return; + + case sda_nd: + f1 = 'd'; + goto array; + + case sda_nx: + f1 = 'x'; + goto array; + + case sda_rest: + (*tp->sd_fn)(tp->sd_name, + (i32)di->di_nch, di->di_cp, (i32)di->di_nfilech); + di->di_call = 1; + di->di_nch = 0; + di->di_nfilech = 0; + return; + + default: + panic("sdecode args(): sd_argtype"); + /* NOTREACHED */ + } + + /* + * If we get here, there is a missing or incorrect argument. + */ + badarg(*fmt, di); + return; + +array: + ip = (i32 *)malloc((unsigned)(room = 10) * sizeof(i32)); + if (ip == NULL) + goto nomem; + n = 0; + while (!di->di_call && word(di, 0) != NULL) { + if (scan_i(di->di_word, f1, &i[0])) { + badarg(f1, di); + free((char *)ip); + return; + } + if (n == room) { + room += 10; + ip = (i32 *)realloc((char *)ip, + (unsigned)room * sizeof(i32)); + if (ip == NULL) + goto nomem; + } + ip[n++] = i[0]; + } + (*tp->sd_fn)(tp->sd_name, n, ip); + free((char *)ip); + return; +nomem: + /* + * might as well stop: if malloc() failed, we will probably get + * nowhere fast anyway. + */ + error(1, -1, + "ran out of memory allocating %d bytes for \\special `%s'", + n * sizeof(i32), di->di_kw); + /* NOTREACHED */ + +} + +/* + * Complain about an argument. Scan() has set di_word to NULL if + * it was missing; otherwise it is incorrect. + */ +static void +badarg(c, di) + int c; + struct decode_info *di; +{ + char *s; + + switch (c) { + + case 'd': + s = "decimal"; + break; + + case 'f': + s = "floating"; + break; + + case 's': + s = "string"; /* must be `missing', not `bad' */ + break; + + case 'x': + s = "hexadecimal"; + break; + + default: + panic("sdecode badarg(%c)", c); + /* NOTREACHED */ + } + if (di->di_word == NULL) + error(0, 0, "missing %s argument for \\special `%s'", + s, di->di_kw); + else { + error(0, 0, "bad %s argument to \\special `%s': `%s'", + s, di->di_kw, di->di_word); + error(0, 0, "(this part of the \\special will be ignored)"); + while (!di->di_call) + (void) word(di, 1); + } +} + +static int +scan(va_alist) + va_dcl +{ + register struct decode_info *di; + register char *fmt; + register int c; + char **fmtp; + va_list ap; + + va_start(ap); + di = va_arg(ap, struct decode_info *); + fmtp = va_arg(ap, char **); + fmt = *fmtp; + while ((c = *fmt++) != 0) { + if (di->di_call || word(di, 0) == NULL) { + di->di_word = NULL; + goto out; + } + switch (c) { + + /* BEWARE, can only handle one string */ + case 's': /* always accepted */ + break; + + case 'd': + case 'x': + if (scan_i(di->di_word, c, va_arg(ap, i32 *))) + goto out; + break; + + case 'f': + if (scan_d(di->di_word, va_arg(ap, double *))) + goto out; + break; + + default: + panic("sdecode scan() *fmt=%c", c); + /* NOTREACHED */ + } + } + c = 0; +out: + va_end(ap); + *fmtp = fmt - 1; + return (c); +} + +static int +scan_i(p, c, result) + char *p; + int c; + i32 *result; +{ +#ifdef HAVE_STRTOL + char *end; + long strtol(); + + *result = strtol(p, &end, c == 'x' ? 16 : 10); + return (*end); +#else + long l; + char junk; + + if (sscanf(p, c == 'x' ? "%lx%c" : "%ld%c", &l, &junk) != 1) + return (1); + *result = l; + return (0); +#endif +} + +static int +scan_d(p, result) + char *p; + double *result; +{ +#ifdef HAVE_STRTOD + char *end; + double strtod(); + + *result = strtod(p, &end); + return (*end); +#else + char junk; + + return (sscanf(p, "%lf%c", result, &junk) != 1); +#endif +} diff --git a/dviware/dvibook/libtex/sdecode.h b/dviware/dvibook/libtex/sdecode.h new file mode 100644 index 0000000000..b0ef12ef8e --- /dev/null +++ b/dviware/dvibook/libtex/sdecode.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * Specials consist of keyword/value groupings, with the simple syntax + * suggested by the BNF + * + * list ::= kv | list ',' kv + * kv ::= word opt_eq words + * opt_eq ::= '=' | <empty> + * words ::= word | words ' ' word + * + * `Words' include any character that is not a space, comma, or quote, + * and can include these by using quoting. Inside and outside quotes, + * `\"' produces `"'; `\\' produces `\'; and `\,' produces `,'. + * + * No single word can exceed WDMAX characters (after quotes are interpreted). + * WDMAX is defined in sdecode.c, and is typically large (>= 1024). + * + * Each special is typed according to its arguments, which are collected + * and decoded, and passed to the special function as ordinary parameters. + * All possible argument-types must be known to the special decoder. + * The function also receives its own name (from the table) as its + * first argument. + */ + +/* value argument types */ +enum sd_args { + sda_none, /* no arguments, just a flag */ + sda_s, /* one string argument (`foo abc') */ + sda_d, /* one decimal (i32) argument (`foo 2') */ + sda_f, /* one floating (double) argument (`foo 3.14') */ + sda_dd, /* two decimal arguments (`foo 3 4') */ + sda_ff, /* two floating arguments (`foo .3 .4') */ + sda_ddddff, /* four decimal args, then 2 floating args */ + sda_nd, /* array n of decimal arguments */ + sda_nx, /* array n of hex arguments (`x 1a00 d02f ...') */ + sda_rest /* fn(len1, ptr, len2); uses up rest of input */ +}; + +struct sdecode { + char *sd_name; /* keyword to match */ + enum sd_args sd_args; /* arguments to collect */ + void (*sd_fn)(); /* actual arguments depend on sd_args */ +}; + +void SDecode(/* FILE *, i32, struct sdecode *, size_t */); +void SDsetclass(/* char *spaces, char *semis */); + +#define SDsize(tab) ((int)(sizeof(tab) / sizeof(*tab))) diff --git a/dviware/dvibook/libtex/search.c b/dviware/dvibook/libtex/search.c new file mode 100644 index 0000000000..3ca80c10b8 --- /dev/null +++ b/dviware/dvibook/libtex/search.c @@ -0,0 +1,180 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/search.c,v 3.1 89/08/22 21:58:14 chris Exp $"; +#endif + +/* + * Key search routines (for a 32 bit key) + * + * SCreate initializes the search control area. + * + * SSearch returns the address of the data area (if found or created) + * or a null pointer (if not). The last argument controls the disposition + * in various cases, and is a ``value-result'' parameter. + * + * SEnumerate calls the given function on each data object within the + * search table. Note that no ordering is specified (though currently + * it runs in increasing-key-value sequence). + */ + +#include "types.h" +#include "search.h" + +#if vax || mc68000 || ns32000 || pyr +#define HARD_ALIGNMENT 4 +#else +#define HARD_ALIGNMENT 16 /* should suffice for most everyone */ +#endif + +static int DOffset; /* part of alignment code */ + +char *malloc(), *realloc(); + +struct search * +SCreate(dsize) + register unsigned int dsize; +{ + register struct search *s; + + if ((s = (struct search *) malloc(sizeof *s)) == 0) + return (0); + + if (DOffset == 0) { +#ifndef HARD_ALIGNMENT + DOffset = sizeof(i32); +#else + DOffset = (sizeof(i32) + HARD_ALIGNMENT - 1) & + ~(HARD_ALIGNMENT - 1); +#endif + } + dsize += DOffset; /* tack on space for keys */ + +#ifdef HARD_ALIGNMENT + /* + * For machines with strict alignment constraints, it may be + * necessary to align the data at a multiple of some positive power + * of two. In general, it would suffice to make dsize a power of + * two, but this could be very space-wasteful, so instead we align it + * to HARD_ALIGNMENT. 64 bit machines might ``#define HARD_ALIGNMENT + * 8'', for example. N.B.: we assume that HARD_ALIGNMENT is a power + * of two. + */ + + dsize = (dsize + HARD_ALIGNMENT - 1) & ~(HARD_ALIGNMENT - 1); +#endif + + s->s_dsize = dsize; /* save data object size */ + s->s_space = 10; /* initially, room for 10 objects */ + s->s_n = 0; /* and none in the table */ + if ((s->s_data = malloc(s->s_space * dsize)) == 0) { + free((char *)s); + return (0); + } + return (s); +} + +/* + * We actually use a binary search right now - this may change. + */ +char * +SSearch(s, key, disp) + register struct search *s; + register i32 key; + int *disp; +{ + register char *keyaddr; + int itemstomove; + + *disp &= S_CREATE | S_EXCL; /* clear return codes */ + if (s->s_n) { /* look for the key */ + register int h, l, m; + + h = s->s_n - 1; + l = 0; + while (l <= h) { + m = (l + h) >> 1; + keyaddr = s->s_data + m * s->s_dsize; + if (*(i32 *) keyaddr > key) + h = m - 1; + else if (*(i32 *) keyaddr < key) + l = m + 1; + else { /* found it, now what? */ + if (*disp & S_EXCL) { + *disp |= S_COLL; + return (0); /* collision */ + } + *disp |= S_FOUND; + return (keyaddr + DOffset); + } + } + keyaddr = s->s_data + l * s->s_dsize; + } else + keyaddr = s->s_data; + + /* keyaddr is now where the key should have been found, if anywhere */ + if ((*disp & S_CREATE) == 0) + return (0); /* not found */ + + /* avoid using realloc so as to retain old data if out of memory */ + if (s->s_space <= 0) { /* must expand; double it */ + register char *new; + + if ((new = malloc((s->s_n << 1) * s->s_dsize)) == 0) { + *disp |= S_ERROR; /* no space */ + return (0); + } + keyaddr = (keyaddr - s->s_data) + new; /* relocate */ + bcopy(s->s_data, new, s->s_n * s->s_dsize); + free(s->s_data); + s->s_data = new; + s->s_space = s->s_n; + } + /* now move any keyed data that is beyond keyaddr down */ + itemstomove = s->s_n - (keyaddr - s->s_data) / s->s_dsize; + if (itemstomove) { +#ifndef BLOCK_COPY + register char *from, *to; + + from = s->s_data + s->s_n * s->s_dsize; + to = from + s->s_dsize; + while (from > keyaddr) + *--to = *--from; +#else + BLOCK_COPY(keyaddr, keyaddr + s->s_dsize, + itemstomove * s->s_dsize); +#endif + } + *disp |= S_NEW; + s->s_n++; + s->s_space--; + *(i32 *) keyaddr = key; + keyaddr += DOffset; /* now actually dataaddr */ + /* the bzero is just a frill... */ + bzero(keyaddr, s->s_dsize - DOffset); + return (keyaddr); +} + +/* + * Call function `f' for each element in the search table `s'. + */ +void +SEnumerate(s, f) + register struct search *s; + register void (*f)(); +{ + register int n; + register char *p; + + n = s->s_n; + p = s->s_data; + while (--n >= 0) { + (*f)(p + DOffset, *(i32 *)p); + p += s->s_dsize; + } +} diff --git a/dviware/dvibook/libtex/search.h b/dviware/dvibook/libtex/search.h new file mode 100644 index 0000000000..864649c8df --- /dev/null +++ b/dviware/dvibook/libtex/search.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* search structures and routines for 32-bit key, arbitrary data */ + +struct search { + unsigned s_dsize; /* data object size (includes key size) */ + unsigned s_space; /* space left (in terms of objects) */ + unsigned s_n; /* number of objects in the table */ + char *s_data; /* data area */ +}; + +/* returns a pointer to the search table (for future search/installs) */ +struct search *SCreate(); /* create a search table */ + +/* returns a pointer to the data object found or created */ +char *SSearch(); /* search for a data object */ + +/* clears a search table (wipes out all entries) */ +#define SClear(s) ((s)->s_space += (s)->s_n, (s)->s_n = 0) + +/* the third argument to SSearch controls operation as follows: */ +#define S_LOOKUP 0x00 /* pseudo flag */ +#define S_CREATE 0x01 /* create object if not found */ +#define S_EXCL 0x02 /* complain if already exists */ + +/* in addition, it is modified before return to hold status: */ +#define S_COLL 0x04 /* collision (occurs iff S_EXCL set) */ +#define S_FOUND 0x08 /* found (occurs iff existed already) */ +#define S_NEW 0x10 /* created (occurs iff S_CREATE && !S_EXCL) */ +#define S_ERROR 0x20 /* problem creating (out of memory) */ + +void SEnumerate(); diff --git a/dviware/dvibook/libtex/seek.c b/dviware/dvibook/libtex/seek.c new file mode 100644 index 0000000000..1d9915516b --- /dev/null +++ b/dviware/dvibook/libtex/seek.c @@ -0,0 +1,147 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/seek.c,v 3.1 89/08/22 21:59:04 chris Exp $"; +#endif + +/* + * SeekFile copies an input stdio file, if necessary, so that + * it produces a stdio file on which fseek() works properly. + * It returns NULL if this cannot be done; in that case, the + * input file is closed (or otherwise rendered worthless). + * + * CopyFile copies an input file unconditionally. (On non-Unix + * machines, it might `accidentally' not copy it.) + * + * On Unix machines, this means `if the input is a pipe or tty, + * copy it to a temporary file'. On other systems, all stdio files + * might happen to be seekable. + */ + +#include <stdio.h> +#include "types.h" /* for BSD_FILE_SYSTEM */ +#include "seek.h" + +#ifdef EASY_BUT_NOT_GOOD + +FILE *CopyFile(f) FILE *f; { return (f); } +FILE *SeekFile(f) FILE *f; { return (f); } + +#else + +#include <errno.h> +#ifdef BSD_FILE_SYSTEM +#include <sys/param.h> /* want MAXBSIZE */ +#else +#include <sys/types.h> +#endif +#include <sys/stat.h> + +long lseek(); +char *malloc(); + +extern int errno; + +/* + * Make and return a version of `f' on which fseek works (unconditionally). + * This code is somewhat Unix-specific. + */ +FILE * +CopyFile(f) + FILE *f; +{ + register int tf, n, ifd, w; + register char *p, *buf; + register int blksize; + struct stat st; + int e; +#ifdef MAXBSIZE +#define BSIZE MAXBSIZE +#else +#define BSIZE BUFSIZ +#endif + char stackbuf[BSIZE]; + + /* get a read/write temp file which will vanish when closed */ + if ((tf = MakeRWTempFile(stackbuf)) < 0) { + e = errno; + (void) fclose(f); + errno = e; + return (NULL); + } + + /* compute buffer size and choose buffer */ + ifd = fileno(f); + buf = stackbuf; + blksize = sizeof stackbuf; +#ifdef BSD_FILE_SYSTEM + if (fstat(tf, &st) == 0 && st.st_blksize > blksize) { + /* + * The output block size is the important one, + * but the input block size is not irrelevant. + * + * This should actually compute the lcm, but we + * will rely on block sizes being powers of two + * (so that the larger is the lcm). + */ + blksize = st.st_blksize; + if (fstat(ifd, &st) == 0 && st.st_blksize > blksize) + blksize = st.st_blksize; + if ((buf = malloc((unsigned)blksize)) == NULL) { + buf = stackbuf; + blksize = sizeof stackbuf; + } + } +#endif + /* copy from input file to temp file */ + (void) lseek(ifd, 0L, 0); /* paranoia */ + while ((n = read(ifd, p = buf, blksize)) > 0) { + do { + if ((w = write(tf, p, n)) < 0) { + (void) close(tf); + (void) fclose(f); + return (NULL); + } + p += w; + } while ((n -= w) > 0); + } + e = errno; /* in case n < 0 */ + if (buf != stackbuf) + free(buf); + if (n < 0) { + (void) close(tf); + (void) fclose(f); + errno = e; + return (NULL); + } + + /* discard the input file, and rewind and open the temporary */ + (void) fclose(f); + (void) lseek(tf, 0L, 0); + errno = 0; + if ((f = fdopen(tf, "r")) == NULL) { + if (errno == 0) + e = EMFILE; + (void) close(tf); + errno = e; + } + return (f); +} + +/* + * Copy an input file, but only if necessary. + */ +FILE *SeekFile(f) + FILE *f; +{ + int fd = fileno(f); + + return (lseek(fd, 0L, 1) >= 0 && !isatty(fd) ? f : CopyFile(f)); +} + +#endif /* EASY_BUT_NOT_GOOD */ diff --git a/dviware/dvibook/libtex/seek.h b/dviware/dvibook/libtex/seek.h new file mode 100644 index 0000000000..7ab66cbdb6 --- /dev/null +++ b/dviware/dvibook/libtex/seek.h @@ -0,0 +1,14 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * Declarations for seek.c: SeekFile(FILE *fp) returns a + * version of `fp' on which `fseek' functions properly; + * CopyFile(FILE *fp) returns a temporary copy of fp. + */ + +FILE *SeekFile(/* FILE *fp */), *CopyFile(/* FILE *fp */); diff --git a/dviware/dvibook/libtex/skip.c b/dviware/dvibook/libtex/skip.c new file mode 100644 index 0000000000..54366b331a --- /dev/null +++ b/dviware/dvibook/libtex/skip.c @@ -0,0 +1,30 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/skip.c,v 3.1 89/08/22 21:45:13 chris Exp $"; +#endif + +#include <stdio.h> +#include "types.h" +#include "fio.h" + +/* + * Skip a font definition. The font number has already been consumed. + */ +SkipFontDef(fp) + register FILE *fp; +{ + register int i; + + (void) GetLong(fp); /* checksum */ + (void) GetLong(fp); /* mag */ + (void) GetLong(fp); /* designsize */ + i = UnSign8(GetByte(fp)) + UnSign8(GetByte(fp)); + while (--i >= 0) + (void) GetByte(fp); +} diff --git a/dviware/dvibook/libtex/split.c b/dviware/dvibook/libtex/split.c new file mode 100644 index 0000000000..21d0d64832 --- /dev/null +++ b/dviware/dvibook/libtex/split.c @@ -0,0 +1,226 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/split.c,v 3.1 89/08/22 21:59:36 chris Exp $"; +#endif + +#include <ctype.h> + +/* + * Split a line into an array of words. This is destructive of + * the original line; the word pointers point to places within + * that line. + * + * The pound-sign character `#', at the beginning of the line or + * after white space, marks the end of the line. + * + * Return the number of words made, or -1 for overflow. + */ + +/* + * The lexical states are much like `sh's, except that we also do + * C-style backslash-escapes. + */ +enum lexstate { + S_BLANK, /* outside a word */ + S_WORD, /* inside a word, no quoting */ + S_SQUOTE, /* inside a single quote */ + S_DQUOTE, /* inside a double quote */ + S_BKSL0, /* last char was \ */ + S_BKSL1, /* last chars were \, [0-7] */ + S_BKSL2 /* last chars were \, [0-7][0-7] */ +}; + +int +split(s, w, nw) + register char *s, **w; + int nw; +{ + register int c; + register char *canon = s; + register int wleft = nw; + enum lexstate state, prebkstate; + + /* + * Start out in the `blank' state (outside a word). Handle + * quotes and things. Backslashes are handled by saving the + * `pre-backslash' state, doing the backslash, and restoring + * that state at the end of the backslash sequence. + */ + state = S_BLANK; + while ((c = *s++) != 0) { +reswitch: + switch (state) { + + /* + * Blanks: spaces stay in blank state; '#' ends the line; + * anything else starts a word. However, quotes may put + * us into quote states, rather than word states. + */ + case S_BLANK: + if (isspace(c)) + continue; + if (c == '#') + goto stopped_by_comment; + if (--wleft < 0) + return (-1); + *w++ = canon; + state = S_WORD; + /* FALLTHROUGH */ + + /* + * In a word. Spaces take us out (and end the + * current word). Quotes, however, put us into + * quote states. + */ + case S_WORD: + if (isspace(c)) { + *canon++ = 0; + state = S_BLANK; + break; + } + if (c == '\'') { + state = S_SQUOTE; + break; + } + if (c == '"') { + state = S_DQUOTE; + break; + } + if (c == '\\') { + prebkstate = S_WORD; + state = S_BKSL0; + break; + } + *canon++ = c; + break; + + /* + * Inside a single quote, the only special character + * is another single quote. This matches the Bourne + * shell quoting convention exactly. + */ + case S_SQUOTE: + if (c == '\'') + state = S_WORD; + else + *canon++ = c; + break; + + /* + * Inside a double quote, double quotes get us out, + * but backslashes must be interpreted. + */ + case S_DQUOTE: + if (c == '\\') { + prebkstate = S_DQUOTE; + state = S_BKSL0; + } else if (c == '"') + state = S_WORD; + else + *canon++ = c; + break; + + /* + * If we are handling a backslash, we will either + * restore the state, or go to BKSL1 state. In + * the latter case, do not advance the canonicalisation + * pointer, since we might have more octal digits + * to insert. + */ + case S_BKSL0: + state = prebkstate; /* probably */ + switch (c) { + + case 'b': + *canon++ = '\b'; + break; + + case 'f': + *canon++ = '\f'; + break; + + case 'n': + *canon++ = '\n'; + break; + + case 'r': + *canon++ = '\r'; + break; + + case 't': + *canon++ = '\t'; + break; + + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + *canon = c - '0'; + state = S_BKSL1; + break; + + default: + *canon++ = c; + break; + } + break; + + + /* + * In BKSL1, we have seen backslash and one octal + * digit. There may be more (in which case just + * count them on in), or there might be something + * that requires we restore the state and try again. + */ + case S_BKSL1: + switch (c) { + + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + *canon <<= 3; + *canon |= c - '0'; + state = S_BKSL2; + break; + + default: + canon++; + state = prebkstate; + goto reswitch; + } + break; + + /* + * BKSL2 is like BKSL1, except that it cannot + * help but restore the original state, since + * there are no four-character octal sequences. + */ + case S_BKSL2: + state = prebkstate; /* assuredly */ + switch (c) { + + case '0': case '1': case '2': case '3': + case '4': case '5': case '6': case '7': + *canon <<= 3; + *canon++ |= c - '0'; + break; + + default: + canon++; + goto reswitch; + } + break; + } + } +stopped_by_comment: +#ifdef notdef + if (state != S_WORD && state != S_BLANK) + error(0, 0, "warning: unclosed quote"); +#endif + if (state != S_BLANK) + *canon = 0; + return (nw - wleft); +} diff --git a/dviware/dvibook/libtex/strsave.c b/dviware/dvibook/libtex/strsave.c new file mode 100644 index 0000000000..998ff8003e --- /dev/null +++ b/dviware/dvibook/libtex/strsave.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * Save a string in managed memory. + */ + +char *malloc(), *realloc(); +extern int errno; + +#include "types.h" /* for bcopy */ +#include "error.h" + +char * +strsave(s) + register char *s; +{ + register int l = strlen(s) + 1; + register char *p = malloc((unsigned) l); + + if (p == 0) + error(1, errno, "no room for %d bytes of string", l); + bcopy(s, p, l); + return (p); +} diff --git a/dviware/dvibook/libtex/strtol.c b/dviware/dvibook/libtex/strtol.c new file mode 100644 index 0000000000..5cb6662fbb --- /dev/null +++ b/dviware/dvibook/libtex/strtol.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <ctype.h> + +/* + * This is for people who don't have strtol defined on their systems. + * -- Compliments of Daniel.Stodolsky@cs.cmu.edu + */ + +long strtol(nptr,base,eptr) +char *nptr; +int base; +char **eptr; +{ + long l; + + if (base==10) + { + if (sscanf(nptr,"%ld",&l)!=1) + { + *eptr = nptr; + return 0; + } + else + { + while(!isdigit(*nptr)) + nptr++; + while (isdigit(*nptr)) + nptr++; + *eptr = nptr; + return l; + } + } + else + if (base==16) + { + if (sscanf(nptr,"%lx",&l)!=1) + { + *eptr = nptr; + return 0; + } + else + { + while(!isxdigit(*nptr)) + nptr++; + while (isxdigit(*nptr)) + nptr++; + *eptr = nptr; + return l; + } + } + else + { /* a base we don't understand. Puke */ + *eptr = nptr; + return 0; + } +} diff --git a/dviware/dvibook/libtex/tempfile.c b/dviware/dvibook/libtex/tempfile.c new file mode 100644 index 0000000000..acd410cefc --- /dev/null +++ b/dviware/dvibook/libtex/tempfile.c @@ -0,0 +1,77 @@ +/* + * Copyright (c) 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/tempfile.c,v 3.1 89/08/22 21:45:20 chris Exp $"; +#endif + +#include <stdio.h> +#include <sys/types.h> +#include <sys/file.h> + +char *getenv(); + +/* + * Create an open but unlinked (i.e., remove-on-exit-or-file-close) + * file. Return the file descriptor, or -1 if unable to create such + * a file. The buffer `name' is used to hold the file name (so that + * if this returns -1, it is useful for error messages). + * + * We use the despicable trick of unlinking an open temporary file. + * The alternatives are too painful. If it becomes necessary to + * do this another way, however, here is a method suggested by Fred + * Blonder: fork, and have the parent wait for the child to exit. + * (The parent must avoid being killed during this phase.) When + * the child exits, the parent should then remove the temporary file, + * then exit with the same status, or send itself the same signal. + */ +int +MakeRWTempFile(name) + register char *name; +{ + register int tf, n; + int mypid, tries; + char *tmpdir; + + if ((tmpdir = getenv("TMPDIR")) == NULL) + tmpdir = "/tmp"; + + /* compose a suitable temporary file name, and get an r/w descriptor */ + mypid = getpid(); + n = 0; + tries = 0; + do { + (void) sprintf(name, "%s/#%d.%d", tmpdir, mypid, n++); + (void) unlink(name); +#ifdef O_CREAT /* have three-argument open syscall */ + tries++; + tf = open(name, O_RDWR | O_CREAT | O_EXCL, 0666); +#else + if (access(name, 0) == 0) { + /* + * Skip existing files. Note that tf might + * not be set yet. + */ + tf = -1; + continue; + } + tries++; + tf = creat(name, 0666); + if (tf >= 0) { + (void) close(tf); + tf = open(name, 2); + if (tf < 0) + (void) unlink(name); + } +#endif + } while (tf < 0 && tries < 20); + if (tf < 0) /* probably unrecoverable user error */ + return (-1); + + (void) unlink(name); + return (tf); +} diff --git a/dviware/dvibook/libtex/tfm.c b/dviware/dvibook/libtex/tfm.c new file mode 100644 index 0000000000..adbd7c624d --- /dev/null +++ b/dviware/dvibook/libtex/tfm.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/tfm.c,v 3.1 89/08/22 22:00:12 chris Exp $"; +#endif + +/* + * TFM file reading routines. + * + * TODO: + * finish + */ + +#include <stdio.h> +#include "types.h" +#include "fio.h" +#include "tfm.h" + +static int trd_header(), trd_ci(), trd_fix(); + +char *malloc(); + +#define ALLOC(n, type) ((type *)malloc((unsigned)((n) * sizeof(type)))) + +int +readtfmfile(f, t, stopafterwidth) + register FILE *f; + register struct tfmdata *t; + int stopafterwidth; /* ??? */ +{ + i32 nc; + + if (trd_header(f, &t->t_hdr)) + return (-1); + nc = t->t_hdr.th_ec - t->t_hdr.th_bc + 1; + + t->t_ci = NULL; + t->t_width = NULL; + t->t_height = NULL; + t->t_depth = NULL; + + (void) fseek(f, t->t_hdr.th_lh * 4L, 1); /* XXX */ + + if ((t->t_ci = ALLOC(nc, struct char_info_word)) == NULL || + trd_ci(f, nc, t->t_ci) || + (t->t_width = ALLOC(t->t_hdr.th_nw, i32)) == NULL || + trd_fix(f, t->t_hdr.th_nw, t->t_width)) + goto bad; + if (stopafterwidth) + return (0); + if ((t->t_height = ALLOC(t->t_hdr.th_nh, i32)) == NULL || + trd_fix(f, t->t_hdr.th_nh, t->t_height) || + (t->t_depth = ALLOC(t->t_hdr.th_nd, i32)) == NULL || + trd_fix(f, t->t_hdr.th_nd, t->t_depth)) + goto bad; + return (0); + +bad: + if (t->t_ci != NULL) + free((char *) t->t_ci); + if (t->t_width != NULL) + free((char *) t->t_width); + if (t->t_height != NULL) + free((char *) t->t_height); + if (t->t_depth != NULL) + free((char *) t->t_depth); + return (-1); +} + +static int +trd_header(f, th) + register FILE *f; + register struct tfmheader *th; +{ + register i32 *p; + + for (p = &th->th_lf; p <= &th->th_np; p++) + fGetWord(f, *p); + if (feof(f)) + return (-1); + return (0); +} + +static int +trd_ci(f, nc, ci) + register FILE *f; + register int nc; + register struct char_info_word *ci; +{ + + while (--nc >= 0) { + ci->ci_width = fgetbyte(f); + ci->ci_h_d = fgetbyte(f); + ci->ci_i_t = fgetbyte(f); + ci->ci_remainder = fgetbyte(f); + ci++; + } + if (feof(f)) + return (-1); + return (0); +} + +static int +trd_fix(f, nf, p) + register FILE *f; + register int nf; + register i32 *p; +{ + + while (--nf >= 0) { + fGetLong(f, *p); + p++; + } + if (feof(f)) + return (-1); + return (0); +} diff --git a/dviware/dvibook/libtex/tfm.h b/dviware/dvibook/libtex/tfm.h new file mode 100644 index 0000000000..9a50657e87 --- /dev/null +++ b/dviware/dvibook/libtex/tfm.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * TFM file information. + */ + +/* + * TFM files start with a series of unsigned 16 bit integers. We + * read this into the structure `tfm_header'. These are type i32 + * so that they may be used as integer quantities without concern + * as to sign extension. + */ +struct tfmheader { + i32 th_lf; /* length of the file (16 bit words) */ + i32 th_lh; /* length of the header data (words) */ + i32 th_bc; /* beginning character */ + i32 th_ec; /* ending character (inclusive) */ + i32 th_nw; /* number of words in width table */ + i32 th_nh; /* number of words in height table */ + i32 th_nd; /* number of words in depth table */ + i32 th_ni; /* words in italic correction table */ + i32 th_nl; /* words in ligature/kern table */ + i32 th_nk; /* words in kern table */ + i32 th_ne; /* words in extensible character table */ + i32 th_np; /* number of font parameter words */ +}; + +/* + * The remainder of the TFM file comprises the following information, + * all of which are 32 bit quantities: + * + * header: array [0..lh-1] of stuff + * char_info: array [bc..ec] of char_info_word + * width: array [0..nw-1] of fix_word + * height: array [0..nh-1] of fix_word + * depth: array [0..nd-1] of fix_word + * italic: array [0..ni-1] of fix_word + * lig_kern: array [0..nl-1] of lig_kern_command + * kern: array [0..ne-1] of extensible_recipie + * param: array [0..np-1] of fix_word + */ + +/* + * A char_info_word is built of four unsigned eight-bit quantities. The first + * is an index into the width table (this saves 24 bits for every + * character that has the same width as another character). The + * second is a composite height and depth index. The third is a + * composite italic index and tag, and the fourth is a remainder. + * + * XXX needs explaining + */ +struct char_info_word { + char ci_width; /* width index */ + char ci_h_d; /* height and depth index */ + char ci_i_t; /* italic index and tag */ + char ci_remainder; /* ??? */ +}; + +/* + * These macros split up h_and_d and i_and_t values. + */ +#define T_CI_H(ci) (((ci)->ci_h_d >> 4) & 0xf) +#define T_CI_D(ci) ((ci)->ci_h_d & 0xf) +#define T_CI_I(ci) (((ci)->ci_i_t >> 2) & 0x3f) +#define T_CI_T(ci) ((ci)->ci_i_t & 3) + +/* + * This structure contains everything one might need to know about + * a TFM file at run-time. + * + * XXX incomplete, or wrong, as yet + */ +struct tfmdata { + struct tfmheader t_hdr; /* full header */ + struct char_info_word *t_ci; /* char info */ + i32 *t_width; /* widths table */ + i32 *t_height; /* heights */ + i32 *t_depth; /* depths */ +}; diff --git a/dviware/dvibook/libtex/tfmfont.c b/dviware/dvibook/libtex/tfmfont.c new file mode 100644 index 0000000000..8831a8700f --- /dev/null +++ b/dviware/dvibook/libtex/tfmfont.c @@ -0,0 +1,260 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +#ifndef lint +static char rcsid[] = "$Header: /usr/src/local/tex/local/mctex/lib/RCS/tfmfont.c,v 3.4 89/11/29 02:29:58 chris Exp $"; +#endif + +#include <stdio.h> +#include <sys/types.h> +#include <sys/stat.h> +#include "types.h" +#include "conv.h" +#include "font.h" +#include "tfm.h" + +/* + * TFM font operations. This defines three fonts: + * + * box - prints as little square boxes, outlining what TeX + * thinks is the character; carries a warning. + * blank - prints as entirely blank; carries a warning. + * tfm - prints as entirely blank; can be used for built-in fonts. + * invis - an alias for tfm. + * + * The first two also complain that no font is available in the + * requested size; these are intended to be used as a last resort + * so that users can always print DVI files. You should configure + * in exactly one of box or blank. + * + * TODO: + * base box edge widths on Conversion.c_dpi + */ +static int box_read(), blank_read(), tfm_read(); +static int tfm_getgly(), tfm_rasterise(); +static void tfm_freefont(); + + /* magnifications are unused in tfm fonts */ +struct fontops boxops = /* `boxtops'? Is this a cereal driver? */ + { "box", 1, 0.0, box_read, tfm_getgly, tfm_rasterise, tfm_freefont }; +struct fontops blankops = + { "blank", 2, 0.0, blank_read, tfm_getgly, tfm_rasterise, tfm_freefont }; +struct fontops tfmops = + { "tfm", 0, 0.0, tfm_read, tfm_getgly, tfm_rasterise, tfm_freefont }; +struct fontops invisops = + { "invis", 0, 0.0, tfm_read, tfm_getgly, tfm_rasterise, tfm_freefont }; + +/* + * Local info. + */ +struct tfm_details { + int tfm_edge; /* box edge widths, in pixels */ + struct tfmdata tfm_data; /* the TFM file data */ +}; + +/* + * Get the tfm_details from font f. + */ +#define ftotfm(f) ((struct tfm_details *)(f)->f_details) + +static int do_read(); + +extern int errno; +char *malloc(); + +/* + * Read a Box font. + */ +static int +box_read(f, fd) + struct font *f; + int fd; +{ + + return (do_read(f, fd, 0)); +} + +/* + * Read a Blank font. + */ +static int +blank_read(f, fd) + struct font *f; + int fd; +{ + + return (do_read(f, fd, 1)); +} + +/* + * Read a TFM font. + */ +static int +tfm_read(f, fd) + struct font *f; + int fd; +{ + + return (do_read(f, fd, 1)); +} + +/* + * Read a TFM font. It is blank if `blank'. + */ +static int +do_read(f, fd, blank) + register struct font *f; + int fd, blank; +{ + register struct tfm_details *tfm; + FILE *fp; + + if ((fp = fdopen(fd, "r")) == NULL) { + (void) close(fd); + return (-1); + } + if ((tfm = (struct tfm_details *)malloc(sizeof *tfm)) == NULL) + goto fail; + if (readtfmfile(fp, &tfm->tfm_data, blank)) + goto fail; + if (blank) + tfm->tfm_edge = 0; + else + tfm->tfm_edge = 2; /* XXX should be based on dpi */ + if (FontHasGlyphs(f, tfm->tfm_data.t_hdr.th_bc, + tfm->tfm_data.t_hdr.th_ec + 1)) + goto fail; + f->f_checksum = 0; /* ??? */ + f->f_design_size = 0; /* ??? */ + f->f_hppp = 0; + f->f_vppp = 0; + f->f_details = (char *)tfm; + (void) fclose(fp); + return (0); + +fail: + (void) fclose(fp); + if (tfm != NULL) + free((char *)tfm); + return (-1); +} + +/* + * Obtain the specified range of glyphs. + */ +static int +tfm_getgly(f, l, h) + register struct font *f; + int l; + register int h; +{ + register struct tfm_details *tfm = ftotfm(f); + register struct glyph *g; + register int i; + register struct char_info_word *ci; +#define t (&tfm->tfm_data) + i32 ScaleOneWidth(); +#define ftop(fix) fromSP(ScaleOneWidth(fix, f->f_dvimag)) + + for (i = l; i < h; i++) { + ci = &t->t_ci[i - t->t_hdr.th_bc]; + /* zero widths mark invalid characters */ + if (ci->ci_width == 0) + continue; + g = f->f_gly[i]; + g->g_flags = GF_VALID; + g->g_rawtfmwidth = t->t_width[UnSign8(ci->ci_width)]; + g->g_xorigin = 0; + if (tfm->tfm_edge != 0) { + g->g_yorigin = ftop(t->t_height[T_CI_H(ci)]); + g->g_width = ftop(g->g_rawtfmwidth); + g->g_height = g->g_yorigin + + ftop(t->t_depth[T_CI_D(ci)]); + } else { + g->g_yorigin = 0; + g->g_width = 0; + g->g_height = 0; + } + } + return (0); +#undef t +} + +/* + * Obtain rasters for the specified glyphs. + * + * IGNORES tfm->tfm_edge: 2 HARDCODED FOR NOW + */ +static int +tfm_rasterise(f, l, h) + struct font *f; + int l, h; +{ + register struct glyph *g; + register char *p; + register int w, j, i; + struct tfm_details *tfm = ftotfm(f); +#define EDGE 2 + + if (tfm->tfm_edge == 0) + return (0); +if (tfm->tfm_edge != 2) panic("tfm_rasterise"); + for (i = l; i < h; i++) { + g = f->f_gly[i]; + if ((g->g_flags & GF_VALID) == 0 || !HASRASTER(g)) + continue; + w = (g->g_width + 7) >> 3; + p = malloc((unsigned) (g->g_height * w)); + if (p == NULL) + return (-1); + g->g_raster = p; + g->g_rotation = ROT_NORM; + if (g->g_width < 2 * EDGE) { + w = 2 * EDGE - g->g_width; + for (j = g->g_height; --j >= 0;) + *p++ = 0xf0 << w; + } else { + bzero(p, g->g_height * w); + for (j = 0; j < g->g_height;) { + if (j < EDGE || j >= g->g_height - EDGE) { + register int k = w; + + while (--k > 0) + *p++ = 0xff; + *p++ = 0xff << ((8 - g->g_width) & 7); + j++; + continue; + } + /* the following depends on EDGE==2 */ + *p = 0xc0; + p += w - ((g->g_width & 7) == 1 ? 2 : 1); + *p++ |= 0xc0 >> ((g->g_width - EDGE) & 7); + if ((g->g_width & 7) == 1) + *p++ = 0x80; + /* end dependencies */ + if (++j == EDGE && g->g_height >= 2 * EDGE) { + register int n = g->g_height - EDGE; + + p += (n - j) * w; + j = n; + } + } + } + } + return (0); +} + +/* + * Discard the font details. + */ +static void +tfm_freefont(f) + struct font *f; +{ + + free(f->f_details); +} diff --git a/dviware/dvibook/libtex/types.h b/dviware/dvibook/libtex/types.h new file mode 100644 index 0000000000..c3c780814a --- /dev/null +++ b/dviware/dvibook/libtex/types.h @@ -0,0 +1,107 @@ +/* + * Copyright (c) 1987, 1989 University of Maryland + * Department of Computer Science. All rights reserved. + * Permission to copy for any purpose is hereby granted + * so long as this copyright notice remains intact. + */ + +/* + * MC-TeX types and macros (system dependent). + * Check to make sure they are correct for your system. + */ + +#ifndef _MCTEX_TYPES_ +#define _MCTEX_TYPES_ + +/* + * Define void as int if your compiler does not support void, + * or if there are bugs in its support (e.g., 4.1BSD). + */ +/* #define void int */ + +/* + * Define the following if and only if vfprintf is in your C library. + * If not, lib/error.c will make assumptions about the implementation + * of stdio. If neither works, you may have to come up with something + * yourself. + */ +#define HAVE_VPRINTF + +/* + * Define BSD_FILE_SYSTEM if you have the BSD file system `stat' + * structure (with the st_blksize field). Otherwise, MakeSeekable() + * will be slower than necessary. + */ +#define BSD_FILE_SYSTEM + +/* + * Define this as the name of a routine that handles overlapping block + * copies, if there is such a routine. Usually it will be called memmove() + * but on 4.3BSD it is called bcopy(). Note that the 4.2BSD bcopy() does + * not handle overlap, and must not be used here. If there is no routine, + * or if its overlap handling is uncertain, leave BLOCK_COPY undefined. + * + * (The bcopy provided in lib/bcopy.c does handle overlap.) + */ +/* #define BLOCK_COPY(from, to, len) memmove(to, from, len) */ +#define BLOCK_COPY(from, to, len) bcopy(from, to, len) + +/* + * If you have memcpy/memmove, but not bcopy, use the definition below. + * If you have neither, try the bcopy provided in lib/bcopy.c. + */ +/* #define bcopy(from, to, len) memcpy(to, from, len) */ + +/* + * If you have memcmp, but not bcmp, use the definition below. + * If you have neither, try the bcmp provided in lib/bcmp.c. + */ +/* #define bcmp(s1, s2, len) memcmp(s1, s2, len) */ + +/* + * Define the following types and macros as required by your system. + */ + +typedef short i16; /* a 16 bit integer (signed) */ + +typedef long i32; /* a 32 bit integer (signed) */ +typedef unsigned long ui32; /* a 32 bit integer (unsigned) */ + +/* macros to sign extend quantities that are less than 32 bits long */ + +/* these compilers mishandle (int)(char)(constant), but a subterfuge works */ +#if defined(sun) || defined(hp300) +#define Sign8(n) ((i32)(char)(int)(n)) +#endif + +/* these have signed characters and (int)(char)(constant) works */ +#if defined(vax) || defined(mips) +#define Sign8(n) ((i32)(char)(n)) +#endif + +/* this works everywhere, but may be slow */ +#ifndef Sign8 +#define Sign8(n) ((n) & 0x80 ? ((n) | ~0xff) : (n)) +#endif + +/* this should work everywhere */ +#ifndef Sign16 +#define Sign16(n) ((i32)(i16)(n)) +#endif + +/* this works where int is 32 bits, and >> sign extents and is fast */ +#if defined(vax) || defined(mips) +#define Sign24(n) (((n) << 8) >> 8) +#endif + +/* this works everywhere, but may be slow */ +#ifndef Sign24 +#define Sign24(n) ((n) & 0x800000 ? ((n) | ~0xffffff) : (n)) +#endif + +/* macros to truncate quantites that may be signed */ +#define UnSign8(n) ((i32)(n) & 0xff) +#define UnSign16(n) ((i32)(n) & 0xffff) +#define UnSign24(n) ((i32)(n) & 0xffffff) + +#endif /* _MCTEX_TYPES_ */ |