/* pdfclock.c
 * Copyright (C) 1997-98 Thomas Merz. All rights reserved.
 *
 * A little PDFlib application to draw an analog clock.
 */

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

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

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

#include "pdf.h"

static void
usage(void)
{
    fprintf(stderr, 
	"pdfclock - generate clock in PDF format. (C) Thomas Merz 1997-98\n");
    fprintf(stderr, "usage: pdfclock [-c pagecount] -o outfile\n");

    exit(1);
}

#define RADIUS		200.0
#define MARGIN		20.0

void
main(int argc, char *argv[])
{
    PDF_info	*info;
    FILE	*pdffile = NULL;
    PDF		*p;
    int		opt;
    float	alpha;
    time_t	timer;
    struct tm	ltime;
    int		pagecount = 1;
    
    info		= PDF_get_info();
    info->Title		= "PDF Clock";
    info->Creator       = "pdfclock";

    while ((opt = getopt(argc, argv, "c:o:")) != -1)
	switch (opt) {
	    case 'c':
		pagecount = atoi(optarg);
		if (pagecount < 0)
		    pagecount = 1;
		break;

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

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

    p = PDF_open(pdffile, info);

    while (pagecount-- > 0) {
	PDF_begin_page(p, 2 * (RADIUS + MARGIN), 2 * (RADIUS + MARGIN));
	
	PDF_set_transition(p, trans_wipe);
	PDF_set_duration(p, 0.5);

	PDF_translate(p, RADIUS + MARGIN, RADIUS + MARGIN);
	PDF_setrgbcolor(p, 0.0, 0.0, 1.0);
	PDF_save(p);

	/* minute strokes */
	PDF_setlinewidth(p, 2.0);
	for (alpha = 0; alpha < 360; alpha += 6)
	{
	    PDF_rotate(p, 6.0);
	    PDF_moveto(p, RADIUS, 0.0);
	    PDF_lineto(p, RADIUS-MARGIN/3, 0.0);
	    PDF_stroke(p);
	}

	PDF_restore(p);
	PDF_save(p);

	/* 5 minute strokes */
	PDF_setlinewidth(p, 3.0);
	for (alpha = 0; alpha < 360; alpha += 30)
	{
	    PDF_rotate(p, 30.0);
	    PDF_moveto(p, RADIUS, 0.0);
	    PDF_lineto(p, RADIUS-MARGIN, 0.0);
	    PDF_stroke(p);
	}

	time(&timer);
	ltime = *localtime(&timer);

	/* draw hour hand */
	PDF_save(p);
	PDF_rotate(p, 
		(float)(-((ltime.tm_min/60.0) + ltime.tm_hour - 3.0) * 30.0));
	PDF_moveto(p, -RADIUS/10, -RADIUS/20);
	PDF_lineto(p, RADIUS/2, 0.0);
	PDF_lineto(p, -RADIUS/10, RADIUS/20);
	PDF_closepath(p);
	PDF_fill(p);
	PDF_restore(p);

	/* draw minute hand */
	PDF_save(p);
	PDF_rotate(p,
		(float) (-((ltime.tm_sec/60.0) + ltime.tm_min - 15.0) * 6.0));
	PDF_moveto(p, -RADIUS/10, -RADIUS/20);
	PDF_lineto(p, RADIUS * 0.8, 0.0);
	PDF_lineto(p, -RADIUS/10, RADIUS/20);
	PDF_closepath(p);
	PDF_fill(p);
	PDF_restore(p);

	/* draw second hand */
	PDF_setrgbcolor(p, 1.0, 0.0, 0.0);
	PDF_setlinewidth(p, 2);
	PDF_save(p);
	PDF_rotate(p, (float) -((ltime.tm_sec - 15.0) * 6.0));
	PDF_moveto(p, -RADIUS/5, 0.0);
	PDF_lineto(p, RADIUS, 0.0);
	PDF_stroke(p);
	PDF_restore(p);

	/* draw little circle at center */
	PDF_circle(p, 0, 0, RADIUS/30);
	PDF_fill(p);

	PDF_restore(p);

	PDF_end_page(p);

	sleep(1);		/* we want to see the clock hand move */
    }

    PDF_close(p);

    exit(0);
}
