/* text2pdf.c
 * Copyright (C) 1997-98 Thomas Merz. All rights reserved.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifdef POSIX
#include <unistd.h>
#endif

#ifdef DOS
#include <process.h>
#endif

#ifdef NeXT
#include <libc.h>	/* for getopt(), optind, optarg */
#endif

#include "pdf.h"

static void
usage(void)
{
    fprintf(stderr, "text2pdf - convert text files to pdf. (C) Thomas Merz 1997-98\n");
    fprintf(stderr, "usage: text2pdf [options] [textfile]\n");
    fprintf(stderr, "Available options:\n");
    fprintf(stderr, "-b            binary mode (default: ASCII)\n");
    fprintf(stderr, "-f fontname   name of font to use\n");
    fprintf(stderr, "-h height     page height in points\n");
    fprintf(stderr, "-m margin     margin size in points\n");
    fprintf(stderr, "-s size       font size\n");
    fprintf(stderr, "-o filename   PDF output file name\n");
    fprintf(stderr, "-w width      page width in points\n");
    fprintf(stderr, "-I path       path to AFM and font directory\n");

    exit(1);
}

#define BUFLEN 512

void
main(int argc, char *argv[])
{
    PDF_info	*info;
    char	buf[BUFLEN], *s;
    FILE	*pdffile = NULL, *textfile = stdin;
    PDF		*p;
    int		opt;
    char	*fontname;
    float	fontsize;
    float	x, y, width = a4.width, height = a4.height, margin = 20;
    
    info		= PDF_get_info();
    info->Title		= "stdin";
    info->Creator       = "text2pdf";
    info->binary_mode	= false;

    fontname		= "Courier";
    fontsize		= 12.0;

    while ((opt = getopt(argc, argv, "bf:h:m:o:s:w:I:")) != -1)
	switch (opt) {
	    case 'b':
		info->binary_mode = true;
		break;
		
	    case 'f':
		fontname = optarg;
		break;

	    case 'h':
		height = atoi(optarg);
		if (height < 0) {
		    fprintf(stderr, "Error: bad page height %f!\n", height);
		    usage();
		}
		break;

	    case 'm':
		margin = atoi(optarg);
		if (margin < 0) {
		    fprintf(stderr, "Error: bad margin %f!\n", margin);
		    usage();
		}
		break;

	    case 'o':
		pdffile = fopen(optarg, WRITEMODE);
		if (pdffile == NULL) {
		    fprintf(stderr, 
			    "Error: cannot open output file %s.\n", optarg);
		    usage();
		}
		break;

	    case 's':
		fontsize = atoi(optarg);
		if (fontsize < 0) {
		    fprintf(stderr, "Error: bad font size %f!\n", fontsize);
		    usage();
		}
		break;

	    case 'w':
		width = atoi(optarg);
		if (width < 0) {
		    fprintf(stderr, "Error: bad page width %f!\n", width);
		    usage();
		}
		break;

	    case 'I':
		info->fontpath = optarg;
		break;

	    case '?':
	    default:
		usage();
	}

    if (pdffile == (FILE *) NULL)
	usage();

    if (optind < argc) {
	info->Title	= argv[optind];
	if ((textfile = fopen(argv[optind], "r")) == NULL) {
	    fprintf(stderr, "Error: cannot open input file %s.\n",argv[optind]);
	    exit(2);
	}
    } else
	textfile = stdin;

    p = PDF_open(pdffile, info);

    x = margin;
    y = height - margin;

    while ((s = fgets(buf, BUFLEN, textfile)) != NULL) {
	if (s[0] == '\f') {
	    if (y == height - margin)
		PDF_begin_page(p, width, height);
	    PDF_end_page(p);
	    y = height - margin;
	    continue;
	}

	if (s[0] != '\0' && s[strlen(s) - 1] == '\n')
	    s[strlen(s) - 1] = '\0';	/* remove newline character */

	if (y < margin) {		/* page break necessary? */
	    y = height - margin;
	    PDF_end_page(p);
	}

	if (y == height - margin) {
	    PDF_begin_page(p, width, height);
	    PDF_set_font(p, fontname, fontsize, winansi);
	    PDF_set_text_pos(p, x, y);
	    y -= fontsize;
	}

	PDF_continue_text(p, s);
	y -= fontsize;

    }

    if (y != height - margin)
	PDF_end_page(p);

    PDF_close(p);

    exit(0);
}
