#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <libps/pslib-mp.h>

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

void frame(PSDoc *psdoc, float width) {
	PS_rect(psdoc, 0, 0, width, 1000);
	PS_stroke(psdoc);
}

int main() {
	PSDoc *psdoc;
	int myfont;
	float boxwidth, boxheight, baseline, colsep, leftmargin;
	float fontsize;
	int boxed;
	int psimage;

	boxwidth = 100;
	boxheight = 630;
	baseline = 100;
	colsep = 20;
	leftmargin = 100;
	boxed = 0;
	fontsize = 10.0;

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

#ifdef MEMORY_DEBUGGING
	psdoc = PS_new2(errorhandler, PS_mp_malloc, PS_mp_realloc, PS_mp_free, NULL);
#else
	psdoc = PS_new();
#endif
	PS_open_file(psdoc, "logos.ps");
	PS_set_info(psdoc, "Creator", __FILE__);
	PS_set_info(psdoc, "Author", "Uwe Steinmann");
	PS_set_info(psdoc, "Title", "Logos");
	PS_set_info(psdoc, "Keywords", "Logos, CE");
	PS_set_info(psdoc, "BoundingBox", "0 0 596 842");

	myfont = PS_begin_font(psdoc, "test", 0, 0.001, 0.0, 0.0, 0.001, 0.0, 0.0, NULL);
	PS_begin_glyph(psdoc, "a", 1000.0, 0.0, 0.0, 1000.0, 1000.0);
	PS_rect(psdoc, 10, 10, 300, 300);
	PS_stroke(psdoc);
	PS_end_glyph(psdoc);

	PS_begin_glyph(psdoc, "b", 1000.0, 0.0, 0.0, 1000.0, 1000.0);
	PS_setlinewidth(psdoc, 90);
	PS_circle(psdoc, 500, 500, 450);
	PS_stroke(psdoc);
	/* F */
	PS_moveto(psdoc, 350, 250);
	PS_lineto(psdoc, 350, 700);
	PS_lineto(psdoc, 550, 700);
	PS_moveto(psdoc, 350, 500);
	PS_lineto(psdoc, 550, 500);
	/* I */
	PS_moveto(psdoc, 670, 250);
	PS_lineto(psdoc, 670, 740);
	PS_stroke(psdoc);
	PS_end_glyph(psdoc);

	PS_begin_glyph(psdoc, "c", 1700.0, 0.0, 0.0, 1000.0, 1700.0);
	frame(psdoc, 1700);
	PS_moveto(psdoc, 335, 25);
	PS_lineto(psdoc, 595, 25);
	PS_lineto(psdoc, 700, 370);
	PS_lineto(psdoc, 820, 370);
	PS_lineto(psdoc, 950, 25);
	PS_lineto(psdoc, 1620, 25);
	PS_arc(psdoc, 1570, 125, 100, 270, 390);
	PS_lineto(psdoc, 1370, 975);
	PS_lineto(psdoc, 1110, 975);
	PS_lineto(psdoc, 1375, 250);
	PS_lineto(psdoc, 1125, 250);
	PS_lineto(psdoc, 860, 975);
//	PS_lineto(psdoc, 170, 975);
	PS_arc(psdoc, 130, 875, 100, 90, 210);
//	PS_lineto(psdoc, 200, 425);
	PS_arc(psdoc, 280, 470, 100, 210, 270);
	PS_lineto(psdoc, 440, 370);
	PS_moveto(psdoc, 335, 760);
	PS_lineto(psdoc, 680, 760);
	PS_lineto(psdoc, 740, 600);
	PS_lineto(psdoc, 395, 600);
	PS_closepath(psdoc);
	PS_fill(psdoc);
	PS_end_glyph(psdoc);

	PS_begin_glyph(psdoc, "zero", 900.0, 0.0, 0.0, 900.0, 900.0);
	frame(psdoc, 900);
	PS_end_glyph(psdoc);

	PS_end_font(psdoc);

	PS_begin_page(psdoc, 596, 842);

	psimage = PS_open_image_file(psdoc, "gif", "FIMKO.gif", NULL, 0);
	PS_place_image(psdoc, psimage, 80, 700, 2.5);

	PS_setfont(psdoc, myfont, 100.0);
	PS_show_xy(psdoc, "b", 80, 700);

	psimage = PS_open_image_file(psdoc, "gif", "UL_UR.gif", NULL, 0);
	PS_place_image(psdoc, psimage, 80, 600, 1.37);

	PS_setfont(psdoc, myfont, 100.0);
	PS_show_xy(psdoc, "cb", 80, 600);

	PS_end_page(psdoc);

	PS_close(psdoc);
	PS_delete(psdoc);

#ifdef MEMORY_DEBUGGING 
	PS_mp_list_unfreed();
#endif  

	PS_shutdown();
	exit(0);
}


