Category : C Source Code
Archive   : IMDSRC78.ZIP
Filename : TEXTUTIL.C

 
Output of file : TEXTUTIL.C contained in archive : IMDSRC78.ZIP
/*** Module: TEXTUTIL
General Purpose Graphics Text Routines

Includes text display and input.
***/

#define __MSC

/* * * * INCLUDE files * * * */

#include
#include
#include
#include
#include
#include "mshell.h"
#include "imdef.h"
#include "imdisp.h"
#include "imdutil.h"
#include "dispio.h"
#include "disputil.h"

/* * * * External functions * * * */


/* * * * Function declarations * * * */

int Font (int);
int DrawText ( char *, int, int, int, int, int);
int EraseText (int, int, int, int, int);
int LengthText (char *, int, int *);
int TypeText (char *);
int AcceptText (char *);
int WriteText (char *);
int StatusLine (int, char *);

/* * * * Global Variables * * * */


int TextLine, TextSample; /* line and sample where next text will go */
int TextHeight; /* Default character size */
int BigChars, SmallChars; /* multiples of character size */
int thefont; /* The number of the font */


struct FontDataType
{
unsigned char x, y, pen;
} FontData[MAXFONTSTROKES];
/* Contains the line strokes that define the characters for the
current font. Stored in byte format. The x and y coordinates
convert back to character coordinates with xc = (x-50)/100.
pen=2 is pen down, pen=3 is pen up. */

struct FontPtrType
{
int index;
unsigned char strokes, width;
} FontPointer[128];
/* Contains the info on each ascii character in the current font.
The index points to where the character starts in the FontData
array. The strokes is the number of line strokes for this
character. The width is the width of the character; stored
the same as the x and y above. */


/* The following is the internal default font.
It is in the exact same format as the font files. */

unsigned char DefaultFont[2560] = {

114,0,7,150,3,100,100,2,100,133,2,67,133,2,67,67,2,133,67,2,133,133,2,100,133,
1,11,150,3,100,100,2,100,133,2,83,133,2,67,117,2,67,83,2,83,67,2,117,67,2,133,
83,2,133,117,2,117,133,2,100,133,2,5,150,3,100,100,2,100,133,2,67,67,2,133,67,
2,100,133,3,4,150,3,100,133,2,100,67,3,67,100,2,133,100,4,4,150,3,67,67,2,133,
133,3,67,133,2,133,67,5,6,150,3,100,100,2,100,133,2,67,100,2,100,67,2,133,100,
2,100,133,6,6,150,3,100,100,2,67,100,2,100,133,2,133,100,2,100,100,2,100,67,7,
4,150,3,67,67,2,133,133,2,67,133,2,133,67,8,6,150,3,67,133,2,133,133,2,67,67,2,
133,67,3,83,100,2,117,100,9,5,150,3,100,100,2,67,133,3,133,133,2,100,100,2,100,
67,10,13,150,3,100,100,2,133,133,3,117,117,2,83,117,2,67,133,3,83,117,2,83,83,
2,67,67,3,83,83,2,117,83,2,133,67,3,117,83,2,117,117,11,8,150,3,67,67,2,133,
133,3,67,133,2,133,67,3,100,67,2,100,133,3,67,100,2,133,100,12,6,150,3,100,100,
2,67,133,2,133,133,2,67,67,2,133,67,2,100,100,13,2,150,3,100,67,2,100,133,14,2,
150,3,67,100,2,133,100,15,6,150,3,67,100,2,133,100,3,67,133,2,133,133,3,67,67,
2,133,67,16,6,150,3,67,117,2,133,117,3,67,83,2,133,83,3,83,67,2,117,133,17,6,
150,3,100,100,2,100,133,3,83,117,2,117,117,3,83,83,2,117,83,32,0,150,33,9,150,
3,83,50,2,100,50,2,100,67,2,83,67,2,83,50,3,100,83,2,83,150,2,100,150,2,100,83,
34,4,150,3,83,117,2,83,150,3,117,150,2,117,117,35,8,150,3,83,50,2,83,150,3,117,
150,2,117,50,3,67,117,2,133,117,3,133,83,2,67,83,36,10,150,3,67,67,2,117,67,2,
133,83,2,117,100,2,83,100,2,67,117,2,83,133,2,133,133,3,100,150,2,100,50,37,12,
150,3,67,67,2,133,133,3,83,133,2,67,133,2,67,150,2,83,150,2,83,133,3,133,50,2,
117,50,2,117,67,2,133,67,2,133,50,38,11,150,3,133,83,2,100,50,2,83,50,2,67,67,
2,67,83,2,117,117,2,117,133,2,100,150,2,83,133,2,83,100,2,133,50,39,3,150,3,83,
117,2,100,133,2,100,150,40,4,150,3,117,150,2,83,117,2,83,83,2,117,50,41,4,150,
3,83,150,2,117,117,2,117,83,2,83,50,42,6,150,3,100,67,2,100,133,3,133,117,2,
67,83,3,67,117,2,133,83,43,4,150,3,100,67,2,100,133,3,67,100,2,133,100,44,3,
150,3,67,50,2,83,67,2,83,83,45,2,150,3,67,100,2,133,100,46,2,150,3,83,50,2,83,
50,47,2,150,3,67,50,2,133,150,48,10,150,3,67,67,2,67,133,2,83,150,2,117,150,2,
133,133,2,133,67,2,117,50,2,83,50,2,67,67,2,133,133,49,5,150,3,83,50,2,117,50,
3,100,50,2,100,150,2,83,133,50,8,150,3,67,133,2,83,150,2,117,150,2,133,133,2,
133,117,2,67,67,2,67,50,2,133,50,51,10,150,3,67,150,2,133,150,2,133,133,2,100,
100,2,117,100,2,133,83,2,133,67,2,117,50,2,83,50,2,67,67,52,5,150,3,117,50,2,
117,150,2,67,100,2,67,83,2,133,83,53,9,150,3,67,67,2,83,50,2,117,50,2,133,67,2,
133,100,2,117,117,2,67,117,2,67,150,2,133,150,54,10,150,3,67,100,2,117,100,2,
133,83,2,133,67,2,117,50,2,83,50,2,67,67,2,67,117,2,100,150,2,133,150,55,5,150,
3,67,150,2,133,150,2,133,133,2,83,83,2,83,50,56,17,150,3,83,50,2,117,50,2,133,
67,2,133,83,2,117,100,2,83,100,2,67,117,2,67,133,2,83,150,2,117,150,2,133,133,
2,133,117,2,117,100,3,83,100,2,67,83,2,67,67,2,83,50,57,10,150,3,67,50,2,100,
50,2,133,83,2,133,133,2,117,150,2,83,150,2,67,133,2,67,117,2,83,100,2,133,100,
58,4,150,3,83,50,2,83,50,3,83,83,2,83,83,59,5,150,3,83,50,2,100,67,2,100,83,3,
100,117,2,100,117,60,3,150,3,117,50,2,67,100,2,117,150,61,4,150,3,67,83,2,133,
83,3,133,117,2,67,117,62,3,150,3,83,50,2,133,100,2,83,150,63,8,150,3,100,50,2,
100,50,3,100,83,2,133,117,2,133,133,2,117,150,2,83,150,2,67,133,64,11,150,3,
117,50,2,83,50,2,67,67,2,67,133,2,83,150,2,117,150,2,133,133,2,133,83,2,100,83,
2,100,100,2,133,133,65,7,150,3,67,50,2,67,117,2,100,150,2,133,117,2,133,50,3,
67,83,2,133,83,66,12,150,3,67,50,2,67,150,2,117,150,2,133,133,2,133,117,2,117,
100,2,67,100,3,117,100,2,133,83,2,133,67,2,117,50,2,67,50,67,8,150,3,133,133,2,
117,150,2,83,150,2,67,133,2,67,67,2,83,50,2,117,50,2,133,67,68,8,150,3,67,50,2,
117,50,2,133,67,2,133,133,2,117,150,2,67,150,3,83,150,2,83,50,69,7,150,3,67,50,
2,67,150,2,133,150,3,117,100,2,67,100,3,67,50,2,133,50,70,5,150,3,67,50,2,67,
150,2,133,150,3,100,100,2,67,100,71,9,150,3,133,133,2,117,150,2,83,150,2,67,
133,2,67,67,2,83,50,2,133,50,2,133,83,2,117,83,72,6,150,3,67,50,2,67,150,3,67,
100,2,133,100,3,133,150,2,133,50,73,6,150,3,83,50,2,117,50,3,100,50,2,100,150,
3,83,150,2,117,150,74,5,150,3,67,67,2,83,50,2,117,50,2,133,67,2,133,150,75,5,
150,3,67,50,2,67,150,3,133,150,2,67,100,2,133,50,76,4,150,3,67,50,2,67,150,3,
67,50,2,133,50,77,5,150,3,67,50,2,67,150,2,100,100,2,133,150,2,133,50,78,6,150,
3,67,50,2,67,150,3,67,133,2,133,67,3,133,150,2,133,50,79,9,150,3,67,67,2,83,50,
2,117,50,2,133,67,2,133,133,2,117,150,2,83,150,2,67,133,2,67,67,80,7,150,3,67,
50,2,67,150,2,117,150,2,133,133,2,133,117,2,117,100,2,67,100,81,11,150,3,67,67,
2,83,50,2,117,50,2,133,67,2,133,133,2,117,150,2,83,150,2,67,133,2,67,67,3,100,
83,2,133,50,82,9,150,3,67,50,2,67,150,2,117,150,2,133,133,2,133,117,2,117,100,
2,67,100,3,100,100,2,133,50,83,12,150,3,67,67,2,83,50,2,117,50,2,133,67,2,133,
83,2,117,100,2,83,100,2,67,117,2,67,133,2,83,150,2,117,150,2,133,133,84,4,150,
3,67,150,2,133,150,3,100,150,2,100,50,85,6,150,3,67,150,2,67,67,2,83,50,2,117,
50,2,133,67,2,133,150,86,3,150,3,67,150,2,100,50,2,133,150,87,5,150,3,67,150,2,
83,50,2,100,100,2,117,50,2,133,150,88,8,150,3,67,50,2,67,67,2,133,133,2,133,
150,3,67,150,2,67,133,2,133,67,2,133,50,89,7,150,3,67,150,2,67,133,2,100,100,2,
100,50,3,133,150,2,133,133,2,100,100,90,6,150,3,67,150,2,133,150,2,133,133,2,
67,67,2,67,50,2,133,50,91,4,150,3,100,150,2,67,150,2,67,50,2,100,50,92,2,150,3,
133,50,2,67,150,93,4,150,3,67,50,2,100,50,2,100,150,2,67,150,94,5,150,3,67,100,
2,100,133,2,133,100,3,100,133,2,100,67,95,5,150,3,100,67,2,67,100,2,100,133,3,
67,100,2,133,100,96,2,150,3,67,150,2,117,100,97,10,150,3,67,67,2,67,100,2,83,
117,2,100,117,2,133,83,2,100,50,2,83,50,2,67,67,3,133,117,2,133,50,98,10,150,3,
67,50,2,67,150,3,67,83,2,100,117,2,117,117,2,133,100,2,133,67,2,117,50,2,100,
50,2,67,83,99,6,150,3,133,117,2,83,117,2,67,100,2,67,67,2,83,50,2,133,50,100,
10,150,3,133,150,2,133,50,3,133,83,2,100,117,2,83,117,2,67,100,2,67,67,2,83,50,
2,100,50,2,133,83,101,9,150,3,67,83,2,133,83,2,133,100,2,117,117,2,83,117,2,
67,100,2,67,67,2,83,50,2,117,50,102,5,150,3,100,50,2,100,133,2,117,150,3,83,
100,2,117,100,103,12,150,3,83,17,2,117,17,2,133,33,2,133,117,3,133,83,2,100,
117,2,83,117,2,67,100,2,67,67,2,83,50,2,100,50,2,133,83,104,6,150,3,67,50,2,67,
150,3,67,117,2,117,117,2,133,100,2,133,50,105,7,150,3,83,50,2,117,50,3,100,50,
2,100,100,2,83,100,3,100,133,2,100,133,106,6,150,3,83,50,2,100,50,2,117,67,2,
117,117,3,117,150,2,117,150,107,6,150,3,67,50,2,67,150,3,133,133,2,67,67,3,100,
100,2,133,50,108,5,150,3,83,50,2,117,50,3,100,50,2,100,150,2,83,150,109,10,150,
3,67,50,2,67,117,3,67,100,2,83,117,2,100,100,2,100,50,3,100,100,2,117,117,2,
133,100,2,133,50,110,7,150,3,67,50,2,67,117,3,67,100,2,83,117,2,117,117,2,133,
100,2,133,50,111,9,150,3,67,67,2,67,100,2,83,117,2,117,117,2,133,100,2,133,67,
2,117,50,2,83,50,2,67,67,112,10,150,3,67,17,2,67,117,3,67,83,2,100,117,2,117,
117,2,133,100,2,133,67,2,117,50,2,100,50,2,67,83,113,10,150,3,133,17,2,133,117,
3,133,83,2,100,117,2,83,117,2,67,100,2,67,67,2,83,50,2,100,50,2,133,83,114,5,
150,3,67,50,2,67,117,3,67,83,2,100,117,2,133,117,115,8,150,3,67,50,2,117,50,2,

133,67,2,117,83,2,83,83,2,67,100,2,83,117,2,133,117,116,5,150,3,83,117,2,117,
117,3,100,150,2,100,67,2,117,50,117,7,150,3,67,117,2,67,67,2,83,50,2,117,50,2,
133,67,3,133,117,2,133,50,118,3,150,3,67,117,2,100,50,2,133,117,119,5,150,3,67,
117,2,83,50,2,100,83,2,117,50,2,133,117,120,4,150,3,67,50,2,133,117,3,67,117,2,
133,50,121,8,150,3,83,17,2,117,17,2,133,33,2,133,117,3,67,117,2,67,67,2,83,50,
2,133,50,122,4,150,3,67,117,2,133,117,2,67,50,2,133,50,123,7,150,3,100,50,2,83,
67,2,83,83,2,67,100,2,83,117,2,83,133,2,100,150,124,4,150,3,100,50,2,100,83,3,
100,117,2,100,150,125,7,150,3,83,50,2,100,67,2,100,83,2,117,100,2,100,117,2,
100,133,2,83,150,126,4,150,3,67,83,2,83,100,2,117,100,2,133,117,127,8,150,3,67,
50,2,67,150,2,133,150,2,133,50,2,67,50,2,133,150,3,67,150,2,133,50,0,0,0,0,0,
0 };

int Font (int FontType)

/*** Font reads in the font file for type FontType and fills the
font arrays appropriately. If FontType = 0 the internal font file
is used instead. FontType = 5 reads in font file 005.FNT, etc.
The global variable thefont is set to the font number.

The format of a font file is a sequence of bytes. The first byte
is the number of characters defined in the font. For each
character there is the following: the ascii code, the number of
line strokes, the character width, and then the list of line strokes.
Each line stroke consists of three bytes: the pen motion, and the
x and y coordinate. The width, x, and y values are converted
back into character coordinates by subtracting 50 and dividing
by 100. The pen is 2 for pen down, and 3 for pen up.

***/

{
char FontName[16];
FILE *FontFile;
int i, j, ptr, bufptr, NumChar, ch;
unsigned char NumStrokes, Ascii;
unsigned char *TempBuf;

if (thefont == FontType)
return (0);
if (FontType == 0)
TempBuf = DefaultFont;
else
{
sprintf (FontName, "%d", 1000+FontType);
strcat (FontName, ".FNT");
if ((FontFile = fopen (&FontName[1], "rb")) == NULL)
return(0);

if ((TempBuf = malloc(9000)) == NULL)
{
FatalError( "Not enough memory for font.\n");
}

i = 0;
while ( ((ch = getc(FontFile)) != EOF) && (i < 9000) )
TempBuf[i++] = ch;
fclose (FontFile);
}

thefont = FontType;

for (Ascii = 0; Ascii <= 127; Ascii++)
{
FontPointer[Ascii].width = 50;
FontPointer[Ascii].strokes = 0;
}

bufptr = 0;
ptr = 1;
NumChar = TempBuf[bufptr++];
for (i = 1; i <= NumChar; i++)
{
Ascii = TempBuf[bufptr++];
NumStrokes = TempBuf[bufptr++];
FontPointer[Ascii].index = ptr;
FontPointer[Ascii].strokes = NumStrokes;
FontPointer[Ascii].width = TempBuf[bufptr++];
for (j = 1; j <= NumStrokes; j++)
{
FontData[ptr].pen = TempBuf[bufptr++];
FontData[ptr].x = TempBuf[bufptr++];
FontData[ptr].y = TempBuf[bufptr++];
ptr++;
}
}
if (FontType != 0)
free (TempBuf);
}



int DrawText (char * text, int line, int sample, int height, int angle, int DNvalue)

/*** DrawText draws a text string on the display using the
current font.

Parameter Type Description
text char ptr text string to display
line int line coordinate of lower left of first char
sample int sample coordinate of lower left of first char
height int height of characters in pixels
angle int angle in degrees ccw from positive sample direction
DNvalue int the DN value to draw the pixels in with
***/

{
int i, j, k, ascii, numstrokes;
int linep, sampp, secth, cscth, dist;
int chl, chs, pixl, pixs, oldl, olds;


linep = line; sampp = sample;
secth = 100.0/(cos(0.0174533*angle)+0.01) + 0.5;
cscth = 100.0/(sin(0.0174533*angle)+0.01) + 0.5;

for (i = 0; i < strlen(text); i++)
{
ascii = text[i];
j = FontPointer[ascii].index;
numstrokes = FontPointer[ascii].strokes;
for (k = 1; k <= numstrokes; k++)
{
chs = height*(FontData[j].x-50);
chl = -height*(FontData[j].y-50);
pixs = (chs / secth) + (chl / cscth) + sampp;
pixl = -(chs / cscth) + (chl / secth) + linep;
if (FontData[j].pen == 2)
{
if (ascii==95) /* patch to fix underscore font char */
{
if (k==2)
DrawLine (oldl, olds-5, oldl, olds+5, DNvalue);
}
else
DrawLine (oldl, olds, pixl, pixs, DNvalue);
oldl = pixl; olds = pixs;
}
else
{
oldl = pixl; olds = pixs;
}
j++;
}
dist = height*(FontPointer[ascii].width-50);
linep -= dist/cscth;
sampp += dist/secth;
if (sampp >= dispns) break;
}
}

int EraseText (int line, int samp, int height, int len, int color)

/*** EraseText erases a string of text from the screen by writing over
it using blocks of the background color. Calls DrawBox to do the
actual erasing.

Parameter Type Description
line int starting line of text string
samp int starting sample of text string
height int size of text characters
len int length of text string
color int background color to use
***/

{
int e_line, e_samp, e_height, e_len;

e_line = line - height - 1;
e_samp = samp;
e_height = height + 7;
e_len = len;
if (dispns < e_len+e_samp)
e_len = dispns-e_samp-1;
DrawBox( e_line, e_samp, e_height, e_len, color);
}

int LengthText (char * text, int height, int * p_TextLength)

/*** LengthText finds the length in pixels of a given text string.

Parameter Type Description
text char ptr text string to display
height int height of characters in pixels
p_TextLength int ptr where to return length of string

***/

{
int i, ascii;

if (thefont == -1)
Font (0);

*p_TextLength = 0;
for (i = 0; i < strlen(text); i++)
{
ascii = text[i];
*p_TextLength += (height*(FontPointer[ascii].width-50)) / 100;
}
}



int TypeText (char * TypeString)

/*** TypeText displays a line of text on the screen at the current
text location with a height of 15 pixels. The area is first erased.
The text position is advanced to the next position after the last
character.

Parameter Type Description
TypeString char ptr text string to display
***/

{
int TextLength, n_chars;

LengthText (TypeString, TextHeight, &TextLength);
EraseText (TextLine, TextSample, TextHeight, TextLength, 0);

DrawText (TypeString, TextLine, TextSample, TextHeight, 0, numDN-1);
TextSample += TextLength;
}

int WriteText (char * text)

/*** WriteText writes out a line of text to the user. If there
is only one screen then the text is written on the image display.
Otherwise it is written to the terminal.
Parameter Type Description
text char ptr text string to display
***/

{
if (OneScreen)
{
if (TextLine >= dispnl)
{
ClearDisplay (0);
TextLine = TextHeight+2; TextSample = 1;
}
TypeText (text);
TextLine += TextHeight+5;
TextSample = 1;
}
else
printf ("%s", text);
}


int AcceptText (char * AcceptString)

/*** AcceptString gets a string from the keyboard. The characters
are echoed on the display screen using DrawText. The string is
terminated with a return (ascii 13). Backspace deleting is supported.
The text position is left at the beginning of the next line.

Parameter Type Description
AcceptString char ptr returned text string from user
***/

{
unsigned char ch[2];
int len, last, n_chars;
int old_textsample;
int ptr;
int i;

strcpy (AcceptString, "");
ch[0] = 0; ch[1] = 0;
old_textsample = TextSample;
ptr = CommandPtr;
while (ch[0] != 13)
{
EraseText (TextLine, TextSample, TextHeight, 15, 0);
if ((ch[0] = getch()) == 0)
ch[0] = 0x80 | getch();

if ( (ch[0] == 8) && (strlen(AcceptString) > 0) )
{
last = strlen(AcceptString)-1;
LengthText (&AcceptString[last], TextHeight, &len);
TextSample -= len;
AcceptString[last] = 0;
}
else if (CommandFlag && ch[0] == UP_ARROW)
{
ptr--;
if (ptr < 0) ptr = MAXRECALL - 1;
TextSample = old_textsample;
EraseText (TextLine, TextSample, TextHeight, 900, 0);
strcpy (AcceptString, CommandList[ptr]);
DrawText (AcceptString, TextLine, TextSample, TextHeight, 0,
numDN-1);
LengthText (AcceptString, TextHeight, &len);
TextSample += len;
}
else if (CommandFlag && ch[0] == DOWN_ARROW)
{
ptr++;
if (ptr == MAXRECALL) ptr = 0;
TextSample = old_textsample;
EraseText (TextLine, TextSample, TextHeight, 900, 0);
strcpy (AcceptString, CommandList[ptr]);
DrawText (AcceptString, TextLine, TextSample, TextHeight, 0,
numDN-1);
LengthText (AcceptString, TextHeight, &len);
TextSample += len;
}
else if ((ch[0] >= ' ') && (ch[0] < 200))
{
DrawText (ch, TextLine, TextSample, TextHeight, 0, numDN-1);
LengthText (ch, TextHeight, &len);
TextSample += len;
strcat (AcceptString, ch);
}

}
if (CommandFlag && strlen(AcceptString) > 0)
{
strcpy(CommandList[CommandPtr], AcceptString);
CommandPtr++;
if (CommandPtr == MAXRECALL) CommandPtr = 0;
}
TextSample = 1;
}

int StatusLine (int WhichLine, char * TextString)

/*** StatusText puts a line of text on the screen at the top, (error
reporting), or one of two lines on the bottom of the screen (file
name and command line). It erases the previous line before writing
the new one.

Parameter Type Description
WhichLine int which line to erase and display
TextString char ptr returned text string from user
***/

{
static int Last0=0, Last1=0, Last2=0;
int TextLength, n_chars;

{
if (WhichLine == 0)
{
TextLine = TextHeight+5; TextSample = 1;
TextLength = Last0;
}
if (WhichLine == 1)
{
TextLine = dispnl-TextHeight-12; TextSample = 1;
TextLength = Last1;
}
if (WhichLine == 2)
{
TextLine = dispnl-5; TextSample = 1;
TextLength = Last2;
}
if (TextLength > 0)
{
EraseText (TextLine, TextSample, TextHeight, TextLength, 0);
}

LengthText (TextString, TextHeight, &TextLength);
if (WhichLine == 0) Last0 = TextLength;
if (WhichLine == 1) Last1 = TextLength;
if (WhichLine == 2) Last2 = TextLength;
TypeText (TextString);
}
}



  3 Responses to “Category : C Source Code
Archive   : IMDSRC78.ZIP
Filename : TEXTUTIL.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/