#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <libps/pslib.h>
#ifdef MEMORY_DEBUGGING
#include <libps/pslib-mp.h>
#endif

#define LEFT_BORDER 50

void errorhandler(PSDoc *p, int error, const char *str, void *data) {
	fprintf(stderr, "PSLib: %s\n", str);
}

size_t writeproc(PSDoc *p, void *data, size_t size) {
	return fwrite(data, 1, size, stdout);
}

void footer(PSDoc *p, const char *text) {
	int psfont;
	char buffer[100];
	psfont = PS_findfont(p, "Helvetica", "", 0);
	PS_setfont(p, psfont, 8.0);
	sprintf(buffer, "This file has been created with pslib %s", PS_get_parameter(p, "dottedversion", 0.0));
	PS_show_xy(p, buffer, LEFT_BORDER, 25);
}

int main() {
	PSDoc *ps;

	PS_boot();
#ifdef MEMORY_DEBUGGING
	PS_mp_init();
#endif

#ifdef MEMORY_DEBUGGING
	ps = PS_new2(errorhandler, PS_mp_malloc, PS_mp_realloc, PS_mp_free, NULL);
#else
	ps = PS_new();
#endif

	if (0 > PS_open_file(ps, "overprint.ps")) {
//	if (0 > PS_open_mem(ps, writeproc)) {
		printf("Cannot open PostScript file\n");
		exit(1);
	}

	PS_set_parameter(ps, "warning", "true");

	PS_set_info(ps, "Creator", __FILE__);
	PS_set_info(ps, "Author", "Uwe Steinmann");
	PS_set_info(ps, "Title", "Overprint demonstration");
	PS_set_info(ps, "Keywords", "Overprint");

	PS_begin_page(ps, 596, 842);
	footer(ps, "");

	PS_setcolor(ps, "fill", "cmyk", 1.0, 0.0, 0.0, 0.0);
	PS_rect(ps, 100, 100, 200, 200);
	PS_fill(ps);

	PS_setoverprintmode(ps, 1);

//	PS_setcolor(ps, "fill", "cmyk", 0.0, 1.0, 0.0, 0.0);
	PS_rect(ps, 120, 120, 100, 100);
	PS_fill(ps);

	PS_end_page(ps);

	PS_close(ps);
	PS_delete(ps);

#ifdef MEMORY_DEBUGGING 
	PS_mp_list_unfreed();
#endif  
	PS_shutdown();

	exit(0);
}
