From: ali@wildcat.mti.sgi.com (Ali Moayedian)
Newsgroups: soc.culture.iranian
Subject: Gerdsooz.c
Date: 17 Nov 92 18:15:15 GMT
Reply-To: ali@mti.sgi.com
Organization: Silicon Graphics, Inc.
NNTP-Posting-Host: wildcat.mti.sgi.com

----------- cut here -------------

/* $Header: Gerdsooz.c,v 1.1 92/11/13 15:23:09 ali Locked $ */
/* 

Program Gerdsooz - Release1.1

	Use Gerdsooz at your own risk.  We will not be responsible if 
	Gerdsooz runs out of oil, or if its fitileh burns out!

(c) Copyright 1992 - Ali Moayedian ( ali@mti.sgi.com )

                     .  |          . :  c | /      |  =                    
         ,  o_ >   q_7  |_o   q   ]_,_,_w |_)   .  | _)_ >                 
           ( :   / /          /                (_)  /  :                   
            .     .        |  =      |:         | /    |                   
         ! ]_   q_7_    .  | _)_ >   |_    o_   |_)_   |_o                 
              / /  .   (_)  /  :          ( : /    .                       

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	 | |
	 | |
	( ! )
	 \ /
	 ___
	|   |--0
	|___|
	 ! !	
	 ! !	
	 / \	    cheragh@alborz.mti.sgi.com (Mash Cheragh-Ali)
	~~~~~
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
*/

#include <stdio.h>

#define	EOL_char '\n'
#define	NOTEOL '\\'
#define BLANK ' '
#define TAB '	'
#define TABINDEX 98
#define EOLINDEX 99
#define FALSE 0
#define TRUE 1
#define MAXINDEX 400
#define MAXX	21
#define MAXY	28
#define LINEWIDTH 78
#define default_font	"mikhi-10"
#define mark1 "=begin="
#define mark2 "=end="

	FILE	*in_file, *out_file, *font_file;
	char	file_name[20];
	int	width[MAXINDEX];
	char	font[MAXX][MAXY][MAXINDEX];
	char    EOF_char;
	int	type[128];
	char	line_out[LINEWIDTH][MAXY];
	char	line_tmp[LINEWIDTH][MAXY];
	int	line_x, word_x, save_x;
	int 	height;
	int	full;
	int	echo;

main(int argc, char *argv[])

{
	EOF_char = EOF;
	if (open_files(argc, argv) == 1)
		return 1;
	read_font();
 	process_text();
	wrapup();

}

int open_files(int argc, char *argv[])
{
	int	in_flag, out_flag, font_flag;

	in_flag=FALSE;
	font_flag=FALSE;
	out_flag=FALSE;
	echo=TRUE;

	while (*++argv != NULL)
	{
		if ((strcmp(*argv, "-f")) == 0)
			if ((font_file = fopen(*++argv, "r")) == NULL) 
	 		{
		   		printf("<== Cannot Open Font File ==> \n"); 
				return 1;
			}
			else
				font_flag = TRUE;
		else if ((strcmp(*argv, "-i")) == 0)
			if ((in_file = fopen(*++argv, "r")) == NULL) 
	 		{
		   		printf("<== Cannot Open Input File ==> \n"); 
				return 1;
			}
			else
			{
				strcpy(file_name, *argv);
				in_flag = TRUE;
			}
		else if ((strcmp(*argv, "-o")) == 0)

		{
			out_file = fopen(*++argv, "w");
			out_flag=TRUE;
		}
		else if ((strcmp(*argv, "-n")) == 0)
			echo=FALSE;
		else 
		{
			printf("illegal option ==> %s\n", *argv); 
			return(1);
		}
	}

	if (!font_flag)
		if ((font_file = fopen(default_font, "r")) == NULL) 
		{
	   		printf("<== Cannot Open mikhi-10 Font File ==> \n"); 
			return 1;
		}
		
	if (!in_flag)
	{
		in_file = stdin;
		echo = FALSE;
	}
	if (!out_flag)
		out_file = stdout;
	
	return(0);
}

read_font()
{

#define NEWCHAR 'L'

	char	in_char;
	int	index;
	int	i, j, location;
	char	in_line[100];

	zeroout();
	while ((in_char = getc (font_file)) != 'h')
		if (in_char !=EOL_char )
			fgets (in_line, 20, font_file); /* go to next line */

	fscanf (font_file,"%d",&height);
	fgets (in_line, 20, font_file); /* next line */

	while ((in_char = getc (font_file)) != EOF_char)
	{
		if (in_char == NEWCHAR)
		{
			in_char = getc (font_file);
			if (in_char == TAB)
				index = TABINDEX;
			else
				index=in_char - 32;

			type[index]++;
			in_char=getc(font_file);	/* get location */
			location = in_char-48; 
			index = index + location*100;
			fgets (in_line, 20, font_file); /* next line  ??? */

			for (j=0; j<height; j++)
			{
				i=0;
				while ((in_char = getc (font_file)) != EOL_char)
				{	
					font[i][j][index] = in_char;
					i++;
				}
				if (i > width[index])
					width[index] = i;
			}

		}
	}
}	

wrapup()
{
	char	in_char;

	fclose(in_file);
	if (echo)
	{
		putc (EOL_char, out_file);
		fprintf (out_file, "%s\n", mark1);
		in_file = fopen(file_name, "r");
		while ((in_char = getc(in_file)) !=EOF_char)
			putc (in_char, out_file);
		fprintf (out_file, "%s\n", mark2);
	}
	fclose(out_file);
}

zeroout()
{
	int	i, j, k;

	for (i=0; i<128; i++)
		type[i]=0;

	for (k=0; k<MAXINDEX; k++)
	{
		width[k]=0;
		for (j=0; j<MAXY; j++)
			for (i=0; i<MAXX; i++)
				font[i][j][k] = BLANK;
	}
}

zero_line()
{
	int	i , j;

	for (j=0; j<height; j++)
		for (i=LINEWIDTH; i>0; i--)
			line_out[i-1][j] = BLANK;
	line_x=0;
	word_x=0;
}

write_line()
{
	int	i , j;

	for (j=0; j<height; j++)
	{
		for (i=LINEWIDTH; i>0; i--)
		{
			putc(line_out[i-1][j], out_file);
			line_out[i-1][j] = BLANK;
		}
		putc (EOL_char, out_file);
	}	
	putc (EOL_char, out_file);
	line_x=0;
	word_x=0;
	full = FALSE;
}

add_char(int index)
{
	int	i , j, w;

	w = width[index];

	for (j=0; j<height; j++)
		for (i=w; i>0; i--) 
		 	line_out[line_x+w-i][j] = font[i-1][j][index];
	line_x=line_x+w;
	full = TRUE;
}

save_chars()
{
	int	i , j;

	save_x=line_x-word_x;
	for (j=0; j<height; j++)
		for (i=0; i<save_x; i++) 
		{
			line_tmp[i][j] = line_out[i+word_x][j];
		 	line_out[i+word_x][j] = BLANK;
		}
}

restore_chars()
{
	int	i , j;

	line_x=save_x;
	for (j=0; j<height; j++)
		for (i=0; i<line_x; i++) 
			line_out[i][j] = line_tmp[i][j];
	word_x=0;
}

process_text()
{
	char	in_char;
	int	index, next_index, i, j;
	int 	attach, tmp;
	char	pres_char, next_char;

		zero_line();	
		full= FALSE;
		i=0;
		attach=FALSE;
		pres_char = getc (in_file);

		while (pres_char != EOF_char)
		{
			next_char = getc (in_file);

			if (pres_char == NOTEOL && next_char == EOL_char)	/* paragraph mark */
				next_char = BLANK;	/* change EOL to Blank */
			else if (pres_char == EOL_char)
			{
				if (full) 
					write_line();
				else
				{
					for (j=0; j<(height-1); j++)
						putc (EOL_char, out_file);
					i=0;
				}
			}
			else 
			{
				if (pres_char == TAB)
					index = TABINDEX;
				else
					index = pres_char - 32;

				if (next_char == TAB)
					next_index = TABINDEX;
				else if (next_char == EOL_char)
					next_index = EOLINDEX;
				else
					next_index = next_char - 32;

				if (type[index] == 0)
					printf("no fonts available for %c \n", pres_char);
				else 
					if (attach)		/* end or middle form */
						if(type[next_index]>1 && type[index]==4)
							index=index+300; /* middle form */
						else
						{
							index=index+100; /* end form */
							attach=FALSE;
						}
					else			/* begin, or separate form */
						if(type[index]==4 && type[next_index]>1)
						{
							index=index+200; /* begin form */
							attach=TRUE;
						}
							/* else separate form */

				tmp = line_x+width[index]+1;
				if (tmp > LINEWIDTH)
				{
					if (word_x > 0)
					{
						save_chars();
						write_line();
						restore_chars();
					}
					else
						write_line();
				}
				add_char(index);
				/* if (index == 32) */
				if (index == 0)
					word_x = line_x; /* index for last word */	
			}
			pres_char = next_char;
		}
		if (full)
			write_line();
}
			
Newsgroups: soc.culture.iranian,soc.culture.arabic,soc.culture.lebanon