ftp.nice.ch/pub/next/unix/text/Wn2troff.s.tar.gz#/Wn2troff/roff.c

This is roff.c in view mode; [Download] [Up]

/***********************************************************************
* This program Copyright 1988 by Steven Dorner and the University of
* Illinois Board of Trustees
* 
* No warrantees expressed or implied.
* 
* This program may be redistributed, provided no charge is made for it.
***********************************************************************/
#include <stdio.h>
#include <ctype.h>
#include "wn.h"
FSSType NewFont;
FSSType OldFont;
RulerType OldRuler;
RulerType NewRuler;
extern char **ctrans;
int InALine = 0;
int InAPara = 0;
int ChangedFont = 1;
int ChangedRuler = 1;
int FooterHeight;
char *FontName();
A16BitInteger FontID();
#define HADJUST(x)	(x>72 ? x-72 : 0)
#define MAXLINE		64
typedef struct fontentry FontEntry;
struct fontentry
{
    int id;		/* the font id */
    char *name[4];	/* font names */
};

FontEntry FontTable[] =
{
    FontTimes,		"R",	"B",	"I",	"(BI",
    /*FontCourier,	"(PR",	"(PB",	"I",	"(BI",*/
    0,			0,	0,	0,	0
};


DoBegin()
{
    printf(".lg 0\n");		/* turn off troff ligatures */
    /* be sure old ruler and font are different from new ruler and font */
    OldRuler.leftMargin = 100000;
    OldFont.fontID = 255;
}

SetHFTraps(headHeight,footHeight)
int headHeight, footHeight;
{
    /* header */
    printf(".wh 0 hh\n");
    printf(".de hh\n.sp %dp\n..\n",headHeight);
    /* footer */
    printf(".wh %dp ff\n",-footHeight);
    printf(".de ff\n.bp\n..\n");
    FooterHeight = footHeight;
}

DoEnd()
{
}

DoFontChange(theChange)
FSSType *theChange;
{
    NewFont.fontID = FontID(theChange->fontID);
    NewFont.fontSize = theChange->fontSize;
    NewFont.fontStyle = theChange->fontStyle & CanDoStyleMask;
    ChangedFont = 1;
}

DoDataChar(theChar)
unsigned char theChar;
{
    switch (theChar)
    {
	case '\r':
	    if (InALine)
		puts("\n.br");
	    else
		puts(".sp");
	    if (ChangedRuler)
		ChangeTheRuler();
	    InAPara = InALine = 0;
	    break;
	default:
	    if (!InAPara && ChangedRuler)
		ChangeTheRuler();
	    if (ChangedFont)
		ChangeTheFont();

	    if (!InAPara && NewRuler.leftMargin != NewRuler.paraIndent)
	    {
		NewLine();
		printf("'ti %dp\n",NewRuler.paraIndent-NewRuler.leftMargin);
	    }

	    if (!InALine && isspace(theChar))
		fputs("\\&",stdout);
	    if (NewFont.fontStyle & StyleUline)
		printf("\\o'_%s'",ctrans[theChar]);
	    else
		fputs(ctrans[theChar],stdout);
	    InAPara = InALine = 1;
	    break;
    }
}

DoMarker(theMarker)
unsigned int theMarker;
{
    switch(theMarker)
    {
	case IDPNumMarker:
	    if (!InAPara && ChangedRuler)
		ChangeTheRuler();
	    if (ChangedFont)
		ChangeTheFont();
	    fputs("\\n%",stdout);
	    InALine = InAPara = 1;
	    break;
	case IDDateMarker:
	    if (!InAPara && ChangedRuler)
		ChangeTheRuler();
	    if (ChangedFont)
		ChangeTheFont();
	    fputs("\\n(mo/\\n(dy/\\n(yr",stdout);
	    InALine = InAPara = 1;
	    break;
	case IDTimeMarker:
	    fputs("Dropping time marker.\n",stderr);
	    break;
	case IDFNumMarker:
	    fputs("Dropping footnote marker.\n",stderr);
	    break;
    }
}

DoRulerChange(theRuler)
RulerType *theRuler;
{
    bcopy(theRuler,&NewRuler,sizeof(NewRuler));
    ChangedRuler = 1;
}

ChangeTheFont()
{
    ChangedFont = 0;		/* reset flag */

    if (!bcmp(&NewFont,&OldFont,sizeof(OldFont)))
	return;

    printf("\\f%s",FontName());	/* this will do plain, bold, italic as well */
    printf("\\s%d",(NewFont.fontSize*10)/12);
    bcopy(&NewFont,&OldFont,sizeof(OldFont));
    InALine = 1;
}

ChangeTheRuler()
{
    A16BitInteger *theTS;

    ChangedRuler = 0;
    if (!bcmp(&NewRuler,&OldRuler,sizeof(NewRuler)))
	return;

    NewLine();
    printf("'vs %d\n",NewRuler.lineSpacing);
    printf("'ad %s\n",NewRuler.justification==JRight ? "r" :
		       ((NewRuler.justification==JJust ? "b" :
		       (NewRuler.justification==JCenter ? "c" : "l"))));
    printf("'in %dp\n",HADJUST(NewRuler.leftMargin));
    printf("'ll %dp\n",HADJUST(NewRuler.rightMargin));
    if (*(theTS = NewRuler.tabStops) ||
	NewRuler.paraIndent<NewRuler.leftMargin)
    {
	printf("'ta");
	if (NewRuler.paraIndent < NewRuler.leftMargin)
	    printf(" %dp",NewRuler.leftMargin-NewRuler.paraIndent);
	for (;*theTS;theTS++)
	{
	    switch (*theTS & 0x3)
	    {
		case 1:
		    printf(" %dpC",((*theTS>>2) & 0x3fff) -
			(NewRuler.leftMargin));
		    break;
		case 2:
		    printf(" %dpR",((*theTS>>2) & 0x3fff) -
			(NewRuler.leftMargin));
		    break;
		default:
		    printf(" %dp",((*theTS>>2) & 0x3fff) -
			(NewRuler.leftMargin));
		    break;
	    }
	}
	putchar('\n');
    }
    bcopy(&NewRuler,&OldRuler,sizeof(RulerType));
}

DoNewPage()
{
    NewLine();
    puts(".ff");
}

char *
FontName()
{
    FontEntry *theEntry;

    for (theEntry=FontTable; theEntry->name[1] ; theEntry++)
	if (theEntry->id==NewFont.fontID)
	    return (theEntry->name[NewFont.fontStyle & 0x3]);
    return (FontTable[0].name[NewFont.fontStyle & 0x3]);
}

A16BitInteger
FontID(theID)
A16BitInteger theID;
{
    FontEntry *theEntry;

    for (theEntry=FontTable; theEntry->name[1] ; theEntry++)
	if (theEntry->id==theID)
	    return (theEntry->id);
    /*fprintf(stderr,"Font id %d not mapped.\n",theID,FontTable[0].id);*/
    return (FontTable[0].id);
}
	    
NewLine()
{
    if (InALine)
    {
	putchar('\n');
	InALine = 0;
    }
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.