Word Wrap

Authors

Publication

Pub Details

Date

Pages

See all articles from QL Hacker's Journal 20

Now that I have an HP Deskjet 520 inkjet printer, I’m starting to think about what type of output I could do on it. I’ve found the price of any word processors that support it to be a bit too steep. I have rigged up Quill to support one of the fonts built into the DJ520. I would like to use one of the proportional fonts, but Quill (and all text editors) are all monospace based.

I have written a short print filter that supports the DJ520. It supports dot commands (like ROFF or old WordStar) that do things like Bold, Italics, new page, etc. The next step is to add some word wrap facility. Below is the source code for a program that does just word wrapping. It takes in a file, wraps all the words based on the page width (in characters) and outputs the results to another file.

This program is really just a sort test program to focus on how to do word wrapping. By itself it would be rather limited (unless used in a piping environment like Unix). The page width should not be hard coded into the program, but loaded at runtime (either typed in or as a command line argument).

The program expects a few things about the input file. It expects a blank line between paragraphs. It expects the space, tab, or newline characters to divide words. Non-ASCII characters are not handled.

The next step in this program is to add the support of proportional fonts. As is the program treats every character as the same width. With proportional fonts, characters differ in width (an i is smaller than a w). Once adding proportional fonts is added, then different sized fonts can be added (12 point, 20 point, etc). Output needs to be based on the size of the output (in inches) and not based on the number of characters.

/* wrap_c
This program takes a file as input and reads in each
word and reformats the paragraphs based on WIDTH
to the ouput file.
This program expects a blank line between paragraphs.
*/

#include <stdio_h>
#define WIDTH 40

main() {
char file1[30], file2[30], str[30];
int fd1, fd2, temp, length, cur_len;

printf("Enter Input File Name : \n");
gets(file1);
fd1 = fopen(file1,"r");
if (fd1 == NULL) {
printf("Did not open file: %s",file1);
abort(1);
}

printf("Enter Output File Name: \n");
gets(file2);
fd2 = fopen(file2,"w");
if (fd2 == NULL) {
printf("Did not open file: %s",file2);
abort(1);
}
cur_len = 0;
while ( 1 == 1) {
temp = get_word(str,fd1);
if ( temp == -1)
{ fputc('\n',fd2);
fclose(fd1);
fclose(fd2);
abort(0); }
if ( temp == -2) {
fputc('\n',fd2);
fputc('\n',fd2);
cur_len = 0;
}
else {
length = strlen(str);
if ( (cur_len + length) > WIDTH) {
fputc('\n',fd2);
fputs(str,fd2);
cur_len = length;
}
else {
cur_len = cur_len + length + 1;
fputc(' ',fd2);
fputs(str,fd2);
}
}
} /* end while */
}
/* get_word ( string, file pointer )
gets the next word in the file. End of a word is
space, tab, or LF. A LF with no word means the end of a
paragraph.
Return values are:
0 - no error
-1 - End of File (EOF)
-2 - End of Line (EOL)
(meaning end of paragraph)
*/
get_word( str, fd)
char str[30]; int fd;
{
int count, c, lf;

str[0] = '\0';
lf = NO;
count = 0;
while ( 1 == 1) {
if ( ( c = getc(fd) ) == EOF ) return(-1);
if ( ( c > 32 ) && ( c < 127 ) ) {
str[count] = c;
count++;
}
/* Space Tab CR */
if ((c == 32) || (c == 9) || (c == 10)) {
if ( c == 10 ) lf = YES;
if ((count == 0) && (lf == YES)) return(-2);
if ( count > 0 ) {
str[count] = '\0';
return(0);
}
}
}
}

Products

 

Downloadable Media

 

Image Gallery

Scroll to Top