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

void * my_malloc(PSDoc *p, size_t size, const char *caller) {
	void *a;
	a = (void *) malloc(size);
//	printf("Allocating %d bytes at 0x%X (%s)\n", size, a, caller);
	return(a);
}   

void * my_realloc(PSDoc *p, void *mem, size_t size, const char *caller) {
	return((void *) realloc(mem, size));
} 
  
void my_free(PSDoc *p, void *mem) {
//	printf("Freeing memory at 0x%X\n", mem);
	free(mem);
}


int main() {
	PSDoc *psdoc;
	int fraktur, antiqua;
	float boxwidth, boxheight, baseline, colsep, leftmargin, x;
	float textwidth, fontsize;
	int rest, boxed;
	char text[12000], *str;
	FILE *fp;

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

#ifdef MEMORY_DEBUGGING
	PS_mp_init();
#endif
	PS_boot();
#ifdef MEMORY_DEBUGGING
	psdoc = PS_new2(NULL, PS_mp_malloc, PS_mp_realloc, PS_mp_free, NULL);
#else
	psdoc = PS_new();
#endif
	PS_open_file(psdoc, "einstein.ps");
	PS_set_info(psdoc, "Creator", __FILE__);
	PS_set_info(psdoc, "Author", "Uwe Steinmann");
	PS_set_info(psdoc, "Title", "Long Text Examples");
	PS_set_info(psdoc, "Keywords", "boxed text, Einstein");
	PS_set_info(psdoc, "BoundingBox", "0 0 596 842");

	fraktur = PS_findfont(psdoc, "AlteSchwabacher", "AlteSchwabacher.enc", 1);
	antiqua = PS_findfont(psdoc, "Helvetica", "", 0);
	PS_begin_page(psdoc, 596, 842);

	PS_setfont(psdoc, fraktur, 15.0);
	PS_set_value(psdoc, "charspacing", 2);
	strcpy(text, "Was ist Relativitäts-Theorie?");
	textwidth = PS_stringwidth(psdoc, text, fraktur, 15.0);
	PS_show_xy2(psdoc, text, strlen(text), leftmargin+boxwidth+colsep/2-textwidth/2, baseline+boxheight+20);
	PS_set_value(psdoc, "charspacing", 0.0);

	PS_setfont(psdoc, fraktur, fontsize);
	PS_set_value(psdoc, "leading", 14);

	fp = fopen("einstein.txt", "r");
	str = text;
	while(!feof(fp)) {
		fread(str++, 1, 1, fp);
	}
	str--;
	*str = '\0';
	fclose(fp);

	PS_set_parameter(psdoc, "hyphenation", "true");
	PS_set_parameter(psdoc, "hyphendict", "hyph_de.dic");
	PS_set_value(psdoc, "hyphenminchars", 2);
	PS_set_value(psdoc, "parindent", fontsize*1.2);
	PS_set_value(psdoc, "parskip", 14 /* fontsize*1.2 */);
	PS_set_value(psdoc, "parindentskip", 0);
//	PS_set_value(psdoc, "numindentlines", 1);
	x = leftmargin;
	if(boxed) {
		PS_rect(psdoc, x, baseline, boxwidth, boxheight);
		PS_stroke(psdoc);
	}
	str = text;
	rest = PS_show_boxed(psdoc, str, x, baseline, boxwidth, boxheight, "fulljustify", "");
	PS_set_value(psdoc, "parindentskip", 1);
	if(rest) {
		str += strlen(str)-rest;
		x = leftmargin+boxwidth+colsep;
		if(boxed) {
			PS_rect(psdoc, x, baseline, boxwidth, boxheight);
			PS_stroke(psdoc);
		}
		rest = PS_show_boxed(psdoc, str, x, baseline, boxwidth, boxheight, "fulljustify", "");
	}
	PS_setfont(psdoc, antiqua, 8.0);
	PS_show_boxed(psdoc, "Albert Einstein, Was ist Relativitäts-Theorie? The collected Papers of Albert Einstein. Volume7, Seiten 206­­211, Princeton University Press, 2002", leftmargin, baseline-30, colsep+2*boxwidth, 20, "left", "");
	PS_setfont(psdoc, fraktur, fontsize);
	PS_end_page(psdoc);

	while(rest) {
		str += strlen(str)-rest;
		PS_begin_page(psdoc, 596, 842);
		PS_setfont(psdoc, fraktur, fontsize);
		PS_set_value(psdoc, "leading", 14);
		x = leftmargin;
		if(boxed) {
			PS_rect(psdoc, x, baseline, boxwidth, boxheight);
			PS_stroke(psdoc);
		}
		rest = PS_show_boxed(psdoc, str, x, baseline, boxwidth, boxheight, "fulljustify", "");
		if(rest) {
			x = leftmargin+boxwidth+colsep;
			str += strlen(str)-rest;
			if(boxed) {
				PS_rect(psdoc, x, baseline, boxwidth, boxheight);
				PS_stroke(psdoc);
			}
			rest = PS_show_boxed(psdoc, str, x, baseline, boxwidth, boxheight, "fulljustify", "");
		}
		if(!rest) {
			PS_set_value(psdoc, "textx", x+boxwidth-PS_stringwidth(psdoc, "Albert Einstein", fraktur, fontsize));
			PS_continue_text(psdoc, "Albert Einstein");
		}
		PS_end_page(psdoc);
	}
	PS_deletefont(psdoc, fraktur);
	PS_deletefont(psdoc, antiqua);
	PS_close(psdoc);
	PS_delete(psdoc);
#ifdef MEMORY_DEBUGGING 
	PS_mp_list_unfreed();
#endif  
	PS_shutdown();
	exit(0);
}
