#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

struct spotcolor {
	int id;
	char *name;
	char *colorspace;
	float c1;
	float c2;
	float c3;
	float c4;
};

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);
}

int main() {
	PSDoc *ps;
	int psfont, psfontbold;
	int psimage;
	int i, w, x;
	struct spotcolor spotcolors[5] = {
		{0, "PANTONE Violet C", "cmyk", 0.75, 0.94, 0.0, 0.0},
		{0, "PANTONE 114 C", "cmyk", 0.0, 0.11, 0.69, 0.0},
		{0, "PANTONE 5565 C", "cmyk", 0.37, 0.0, 0.34, 0.34},
		{0, "RGB Blue", "rgb", 0.0, 0.0, 1.0, 0.0},
		{0, "Gray Black", "gray", 0.0, 0.0, 0.0, 0.0}};

	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, "poster.ps")) {
		printf("Cannot open PostScript file\n");
		exit(1);
	}

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

	PS_set_info(ps, "Creator", __FILE__);
	PS_set_info(ps, "Author", "Uwe Steinmann");
	PS_set_info(ps, "Title", "Poster Example");
	PS_set_info(ps, "Keywords", "Poster");

	for(i=0; i<5; i++) {
		PS_setcolor(ps, "fill", spotcolors[i].colorspace, spotcolors[i].c1, spotcolors[i].c2, spotcolors[i].c3, spotcolors[i].c4);
		spotcolors[i].id = PS_makespotcolor(ps, spotcolors[i].name, 0);
	}

	PS_begin_page(ps, 596, 842);
	psfont = PS_findfont(ps, "Helvetica", "", 0);
	psfontbold = PS_findfont(ps, "Helvetica-Bold", "", 0);
	PS_setfont(ps, psfont, 8.0);

	PS_setcolor(ps, "fill", "spot", (float) spotcolors[2].id, 0.1, 0.0, 0.0);
	PS_rect(ps, 0, 767, 410, 65);
	PS_fill(ps);
	PS_rect(ps, 410, 782, 15, 50);
	PS_fill(ps);
	PS_circle(ps, 410, 782, 15);
	PS_fill(ps);
	psimage = PS_open_image_file(ps, "eps", "MMK.eps", NULL, 0);
	PS_place_image(ps, psimage, 370, 785, 1.3);
	PS_rect(ps, 0, 0, 596, 20);
	PS_fill(ps);
	
	PS_setfont(ps, psfontbold, 24.0);
	x = (596-PS_stringwidth(ps, "Lernen mit frei verfügbaren Komponenten", psfontbold, 0))/2;
	PS_show_xy(ps, "Lernen mit frei verfügbaren Komponenten", x, 720);

	PS_setfont(ps, psfont, 14.0);
	PS_show_xy(ps, "Frei verfügbare Komponenten für Lernplattformen bieten", 240, 650);
	PS_show_xy(ps, "Flexibilität bei der Zusammenstellen", 250, 630);
	PS_show_xy(ps, "Anpassung an die eigenen Bedürfnisse", 250, 610);
	PS_show_xy(ps, "Uneingeschränkte Nutzung", 250, 590);
	PS_show_xy(ps, "offene Schnittstellen", 250, 570);

	psimage = PS_open_image_file(ps, "png", "lvm_screenshot.png", NULL, 0);
	PS_place_image(ps, psimage, 10, 500, 0.25);

	psimage = PS_open_image_file(ps, "png", "gits.png", NULL, 0);
	PS_place_image(ps, psimage, 300, 270, 0.25);

	psimage = PS_open_image_file(ps, "png", "evita.png", NULL, 0);
	PS_place_image(ps, psimage, 10, 30, 0.3);

	PS_end_page(ps);

	PS_close(ps);
	PS_delete(ps);

#ifdef MEMORY_DEBUGGING 
	PS_mp_list_unfreed();
#endif  

	PS_shutdown();
	exit(0);
}
