/* txt2pdf.com
Converts text to pdf
*/
$ proc = f$enviornment("PROCEDURE")
$!
$ txt2pdf = f$edit("$" + f$parse(proc,,,"DEVICE") + f$parse(proc,,,"DIRECTORY") + f$parse(proc,,,"NAME") + ".EXE","LOWERCASE")
$!
$ ifil = f$search("''p1'")
$ if (ifil .eqs. "") then exit
$!
$ idev = f$edit(f$parse(ifil,,,"DEVICE"),"LOWERCASE")
$ idir = f$edit(f$parse(ifil,,,"DIRECTORY"),"LOWERCASE")
$ inam = f$edit(f$parse(ifil,,,"NAME"),"LOWERCASE")
$!
$ ofil = "''p2'"
$ if (ofil .eqs. "")
$ then
$ ofil = f$edit(idev + idir + inam + ".pdf","LOWERCASE")
$ else
$ ofil = f$edit(ofil,"COLLAPSE,LOWERCASE")
$ odev = f$edit(f$parse(ofil,,,"DEVICE"),"LOWERCASE")
$ odir = f$edit(f$parse(ofil,,,"DIRECTORY"),"LOWERCASE")
$ onam = f$edit(f$parse(ofil,,,"NAME"),"LOWERCASE")
$!
$ if (f$parse(ofil,,,"NAME") .eqs. "")
$ then
$ if (odev .nes. "")
$ then
$ ofil = odev + odir + inam + ".pdf"
$ else
$ ofil = idev + idir + inam + ".pdf"
$ endif
$ endif
$ if (f$parse(ofil,,,"TYPE") .eqs. ".") then ofil = ofil - "." + ".pdf"
$ endif
$!
$ type = "''p3'"
$ if (type .eqs. "")
$ then
$ ilrl = f$file_attributes(ifil,"LRL")
$ type = "0"
$ if (ilrl .gt. 80) then type = "1"
$ if (ilrl .gt. 100) then type = "2"
$ endif
$!
$ txt2pdf "''ifil'" "''ofil'" "''type'"
$!
$ purgeit = "''p4'"
$ if (purgeit .nes. "")
$ then
$ purge/nolog 'ofil'
$ rename/nolog 'ofil' 'ofil'
$ endif
$!
$ exit
$!
$!!!!!!!! C Code Follows !!!!!!
$!
/*
Copyright 1998
P. G. Womack, Diss, Norfolk, UK.
"BugBear"
Do what you like, but don't claim you wrote it.
*/
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "stdarg.h"
FILE *fpi;
FILE *fpo;
float page_width = 0.0;
float page_depth = 0.0;
float margin = 0.0;
float font_size = 0.0;
float lead_size = 0.0;
/* Default for portrait 80x66 */
float p80_page_width = 612.0;
float p80_page_depth = 792.0;
float p80_margin = 15.0;
float p80_font_size = 12.0;
float p80_lead_size = 11.5;
/* Default for landscape 100x66 */
float l100_page_depth = 612.0;
float l100_page_width = 792.0;
float l100_margin = 5.0;
float l100_font_size = 13.0;
float l100_lead_size = 10.5;
/* Default for landscape 132x66 */
float l132_page_depth = 612.0;
float l132_page_width = 792.0;
float l132_margin = 5.0;
float l132_font_size = 9.85;
float l132_lead_size = 9.0;
int object_id = 1;
int page_tree_id;
typedef struct _PageList {
struct _PageList *next;
int page_id;
} PageList;
int num_pages = 0;
PageList *pages = NULL;
PageList **insert_page = &pages;
store_page(int id)
{
PageList *n = (PageList *)malloc(sizeof(*n));
if(n == NULL)
{
fprintf(stderr,
"Unable to allocate array for page %d.", num_pages + 1);
exit(4);
}
n->next = NULL;
n->page_id = id;
*insert_page = n;
insert_page = &n->next;
num_pages++;
}
int num_xrefs = 0;
long *xrefs = NULL;
start_object(int id)
{
if(id >= num_xrefs)
{
long *new_xrefs;
int delta, new_num_xrefs;
delta = num_xrefs / 5;
if(delta < 1000)
delta += 1000;
new_num_xrefs = num_xrefs + delta;
new_xrefs = (long *)malloc(new_num_xrefs * sizeof(*new_xrefs));
if(new_xrefs == NULL)
{
fprintf(stderr,
"Unable to allocate array for object %d.", id);
exit(4);
}
memcpy(new_xrefs, xrefs, num_xrefs * sizeof(*xrefs));
free(xrefs);
xrefs = new_xrefs;
num_xrefs = new_num_xrefs;
}
xrefs[id] = ftell(fpo);
fprintf(fpo,"%d 0 objn", id);
}
int stream_id, stream_len_id;
long stream_start;
float ypos;
start_page()
{
stream_id = object_id++;
stream_len_id = object_id++;
start_object(stream_id);
fprintf(fpo,"<< /Length %d 0 R >>n", stream_len_id);
fprintf(fpo,"streamn");
stream_start = ftell(fpo);
fprintf(fpo,"BTn/F0 %g Tfn", font_size);
ypos = page_depth - margin;
fprintf(fpo,"%g %g Tdn", margin, ypos);
fprintf(fpo,"%g TLn", lead_size);
}
end_page()
{
long stream_len;
int page_id = object_id++;
store_page(page_id);
fprintf(fpo,"ETn");
stream_len = ftell(fpo) - stream_start;
fprintf(fpo,"endstreamnendobjn");
start_object(stream_len_id);
fprintf(fpo,"%ldnendobjn", stream_len);
start_object(page_id);
fprintf(fpo,"<>nendobjn", page_tree_id, stream_id);
}
do_text()
{
char buffer[8192];
start_page();
while(fgets(buffer,8192,fpi) != NULL)
{
if(ypos < margin)
{
end_page();
start_page();
}
if(strlen(buffer) == 0)
fprintf(fpo,"T*n");
else
{
if(buffer[0] == 'f')
{
end_page();
start_page();
}
else
{
int chok = 1;
char c, *s = buffer;
fputc('(',fpo);
while((c = *s++) != '�')
{
chok = 1;
switch(c)
{
case 'r':
case 'n':
chok = 0;
break;
case '(':
case ')':
case '':
fputc('',fpo);
}
if (chok) fputc(c,fpo);
}
fprintf(fpo,")'n");
}
}
ypos -= lead_size;
}
end_page();
}
int main(argc, argv)
int argc;
char **argv;
{
int i, catalog_id, font_id, pdf_type;
long start_xref;
int debug = 5;
char ifile[80] = "";
char ofile[80] = "";
if (argc < 4)
{
printf("Usage: input_file output_file type [0=p80x66,1=l100x51,2=l132x66]");
exit(0);
}
if (argc == debug) printf("Arguments %dn", argc);
for (i = 1; i < argc; i++)
{
if (argc == debug) printf("argument %d: %sn", i, argv);
if (i == 1) strcpy(ifile,argv);
if (i == 2) strcpy(ofile,argv);
};
if (argc == debug)
{
printf("IFile: %sn", ifile);
printf("OFile: %sn", ofile);
}
switch (argv[3][0]) {
case '0':
page_width = p80_page_width;
page_depth = p80_page_depth;
margin = p80_margin;
font_size = p80_font_size;
lead_size = p80_lead_size;
if (argc == debug) printf("p80n");
break;
case '1':
page_width = l100_page_width;
page_depth = l100_page_depth;
margin = l100_margin;
font_size = l100_font_size;
lead_size = l100_lead_size;
if (argc == debug) printf("l100n");
break;
case '2':
page_width = l132_page_width;
page_depth = l132_page_depth;
margin = l132_margin;
font_size = l132_font_size;
lead_size = l132_lead_size;
if (argc == debug) printf("p132n");
break;
default:
if (argc == debug) printf("nonen");
printf("Usage: input_file output_file type [0,1,2]");
exit(0);
break;
}
fpi = fopen(ifile, "r");
if (fpi == NULL) exit(1);
fpo = fopen(ofile, "w");
if (fpo == NULL) exit(1);
fprintf(fpo,"%%PDF-1.0n");
page_tree_id = object_id++;
do_text();
font_id = object_id++;
start_object(font_id);
fprintf(fpo,"<>nendobjn");
start_object(page_tree_id);
fprintf(fpo,"<page_id);
ptr = ptr->next;
}
fprintf(fpo,"]n");
}
fprintf(fpo,"/Resources<> >>n", font_id);
fprintf(fpo,"/MediaBox [ 0 0 %g %g ]n", page_width, page_depth);
fprintf(fpo,">>nendobjn");
catalog_id = object_id++;
start_object(catalog_id);
fprintf(fpo,"<>nendobjn", page_tree_id);
start_xref = ftell(fpo);
fprintf(fpo,"xrefn");
fprintf(fpo,"0 %dn", object_id);
fprintf(fpo,"0000000000 65535 f n");
for(i = 1; i < object_id; i++)
fprintf(fpo,"%010ld 00000 n n", xrefs);
fprintf(fpo,"trailern<>n", object_id, catalog_id);
fprintf(fpo,"startxrefn%ldn%%%%EOFn", start_xref);
fclose(fpi);
fclose(fpo);
if (argc == debug)
{
printf("After Open/Close IFile: %sn", ifile);
printf("After Open/Close OFile: %sn", ofile);
}
exit(0);
return 0;
}