/* melder_print.c */
/* Paul Boersma, August 2, 2001 */

#if defined (macintosh)
	#include "macport_on.h"
	#include <Memory.h>
	#include <Printing.h>
	#include "macport_off.h"
#elif defined (_WIN32)
	#include <windows.h>
#endif

#include "melder.h"

#ifdef macintosh
	static THPrint theMacPrinter;
#endif

#ifdef macintosh
static int sendFileToPrinterPort (TPPrPort port, MelderFile fs) {
	#define POSTSCRIPT_BEGIN  190
	#define POSTSCRIPT_END  191
	#define POSTSCRIPT_HANDLE  192
	FILE *f = Melder_fopen (fs, "r");
	Handle line = NewHandle (3000);
	PenState pen;
	if (! f) return 0;
	HLock (line);
	/*
	 * Flush GrafPort state.
	 */
	GetPenState (& pen);
	PenSize (0, 0);
	Line (0, 0);
	PenSize (pen. pnSize.h, pen. pnSize.v);
	/*
	 * Send direct PostScript commands.
	 */
	PicComment (POSTSCRIPT_BEGIN, 0, NULL);
	/*
	 * Save the driver's state.
	 */
	strcpy (*line, "/PraatPictureSaveObject save def\n");
	PicComment (POSTSCRIPT_HANDLE, strlen (*line), line);
	/*
	 * The LaserWriter driver puts the coordinates upside down.
	 * According to the PostScript Reference Manual,
	 * "There are few situations in which a PostScript language program
	 *  should execute initgraphics explicitly."
	 * This is one of them.
	 */
	strcpy (*line, "initmatrix initclip\n");
	PicComment (POSTSCRIPT_HANDLE, strlen (*line), line);
	/*
	 * Download the lines from the file. Ignore comments and the showpage operator.
	 */
	while (fgets (*line, 2000, f)) {
		int length = strlen (*line);
		Melder_assert (length < 2000);
		Melder_assert ((*line) [length - 1] == '\n');
		if ((*line) [0] != '%' && ! strnequ (*line, "showpage", 8))
			PicComment (POSTSCRIPT_HANDLE, strlen (*line), line);
	}
	/*
	 * Restore the driver's state.
	 */
	strcpy (*line, "PraatPictureSaveObject restore\n");
	PicComment (POSTSCRIPT_HANDLE, strlen (*line), line);
	PicComment (POSTSCRIPT_END, 0, NULL);
	DisposeHandle (line);
	Melder_fclose (fs, f);
	iferror return 0;
	return 1;
}
#endif

/* End of file melder_print.c */
