/*************************************************************/
/* Includes */
/*************************************************************/

/* MUI */
#include <libraries/mui.h>

/* System */
#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/memory.h>
#include <exec/execbase.h>
#include <dos.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/exall.h>
#include <graphics/gfxmacros.h>
#include <workbench/workbench.h>
#include <diskfont/diskfont.h>	// TetiSoft: for MAXFONTNAME
#include <diskfont/oterrors.h>	// TetiSoft
#include <diskfont/diskfonttag.h>
#include <diskfont/glyph.h>
#include <libraries/gadtools.h>	// TetiSoft: for struct NewMenu
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/icon.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <proto/asl.h>
#include <proto/diskfont.h>	// TetiSoft
#include <proto/utility.h>	// TetiSoft
#include <proto/locale.h>	// TetiSoft
#include <clib/muimaster_protos.h>
#include <clib/alib_protos.h>	// TetiSoft: for DoMethod()
#include <pragmas/muimaster_pragmas.h>

/* ANSI C */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>	// TetiSoft: for tolower()

/* Local */
#include "type1_lib.h"
#include "type1_protos.h"

/* Locale */
#define CATCOMP_NUMBERS
#include "lstrings.h"
STRPTR __asm GetString(register __a0 struct LocaleInfo *li, register __d0 ULONG id);
extern struct LocaleInfo LInfo;
#define LString(id) GetString(&LInfo, id)

extern int QuickInstall;
extern unsigned char *UnicodeArray;
extern unsigned char UnicodePages[256];
extern unsigned char *Pages[257];
extern unsigned char PageNumbers[256][12];

extern struct Library *Type1Base;
extern struct GlyphEngine *engine;

/*************************************************************/
/* Defines */
/*************************************************************/
#define VERSION		"3.0"
#define T1LIBVERSION	3
#define CATVERSION	3
#define REG(x) register __ ## x
#define ASM    __asm
#define SAVEDS __saveds
#define MAX(a,b) __builtin_max(a,b)
#define MIN(a,b) __builtin_min(a,b)
#define EXALLBUFSIZE 1024
#ifndef MAKE_ID
#define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
#endif

// TetiSoft: OT_SymbolSet values
#define BUILTINENCODING		0x00005049	// "PI" - PostScript Internal
#define UNICODE			0x00005543	// "UC" - Unicode
#define STANDARDENCODING	0x00005053	// "PS" - PostScript Standard
#define CEENCODING		0x00004345	// "CE" - CodePage 1250 WinLatin2 = PostScript CE
#define WINCYRILLICENCODING	0x00005743	// "WC" - CodePage 1251 WinCyrillic
#define WINLATIN1ENCODING	0x00005731	// "W1" - CodePage 1252 WinLatin1
#define PSISOLATIN1ENCODING	0x0000504C	// "PL" - PostScript ISOLatin1
#define ISOLATIN1ENCODING	0x00004c31	// "L1" - ISO 8859-1  Latin 1
#define ISOLATIN2ENCODING	0x00004c32	// "L2" - ISO 8859-2  Latin 2
#define ISOLATIN3ENCODING	0x00004c33	// "L3" - ISO 8859-3  Latin 3
#define ISOLATIN4ENCODING	0x00004c34	// "L4" - ISO 8859-4  Latin 4
#define ISOCYRILLICENCODING	0x00004359	// "CY" - ISO 8859-5  Cyrillic
#define ISOARABICENCODING	0x00004152	// "AR" - ISO 8859-6  Arabic
#define ISOGREEKENCODING	0x00004752	// "GR" - ISO 8859-7  Greek
#define ISOHEBREWENCODING	0x00004845	// "HE" - ISO 8859-8  Hebrew
#define ISOLATIN5ENCODING	0x00004c35	// "L5" - ISO 8859-9  Latin 5
#define ISOLATIN6ENCODING	0x00004c36	// "L6" - ISO 8859-10 Latin 6
#define ISOTHAIENCODING		0x00005448	// "TH" - ISO 8859-11 Thai
#define ISOLATIN7ENCODING	0x00004c37	// "L7" - ISO 8859-13 Latin 7
#define ISOLATIN8ENCODING	0x00004c38	// "L8" - ISO 8859-14 Latin 8
#define ISOLATIN9ENCODING	0x00004c39	// "L9" - ISO 8859-15 Latin 9
#define KOI8RENCODING		0x00004b38	// "K8" - koi8r
#define E2ENCODING		0x00004532	// "E2" - E2
#define NRENCODINGS 22	// Number of non-builtin Encodings
/*
** Private tags, only useful for communication between T1manager and type1.library
*/
#define OT_Spec1_PFABFileName	(OT_Spec1  | OT_Indirect)
#define OT_Spec2_AFMFileName	(OT_Level1 | OT_Indirect | 0x102)
#define OT_Spec3_FontName	(OT_Level1 | 0x103)
#define OT_Spec4_ItalicAngle	(OT_Level1 | 0x104)	// TetiSoft
#define OT_Spec5_Weight		(OT_Level1 | 0x105)	// TetiSoft
#define OT_Spec6_OwnEncoding	(OT_Level1 | 0x106)	// TetiSoft
#define OT_Spec7_UnicodeArray	(OT_Level1 | 0x107)	// TetiSoft


/*************************************************************/
/* Structure Definitions */
/*************************************************************/
struct otagnode
{
	struct MinNode MyNode;
	ULONG ti_Tag;
	ULONG ti_Data;
	ULONG size;
};

struct AvailSizeList	// TetiSoft
{
	UWORD	Count;	// first word is number of following elements
	UWORD	Sizes[OT_MAXAVAILSIZES];
};

struct OTagValues	// TetiSoft
{
	ULONG SerifFlag;
	ULONG StemWeight;
	ULONG HorizStyle;
	ULONG IsFixed;
	ULONG SlantStyle;
	LONG SpaceWidth;
	ULONG YSizeFactor;
	ULONG InhibitAlgoStyle;
	char FamilyName[100];
	char BoldName[100];
	char ItalicName[100];
	char BoldItalicName[100];
	char OTagFileName[MAXFONTPATH];
	char Type1FileName[MAXFONTPATH];
	char AFMFileName[MAXFONTPATH];
	struct AvailSizeList AvailSizes;
	ULONG SymbolSet;
	ULONG SpaceFactor;
	ULONG OwnEncoding;
};

/*************************************************************/
/* Local Prototypes */
/*************************************************************/
#define MyErrorMsg1(a,b) { char ReqText[256]; sprintf(ReqText, a, b); MyErrorMsg(ReqText); }
#define MyErrorMsg2(a,b, c) { char ReqText[256]; sprintf(ReqText, a, b, c); MyErrorMsg(ReqText); }	// TetiSoft
#define MyWarningMsg1(a,b) { char ReqText[256]; sprintf(ReqText, a, b); MyWarningMsg(ReqText); }
void ErrorReq(char *Name,LONG Error);
void MyErrorMsg(char *ErrorMsg);
void MyWarningMsg(char *WarningMsg);	// TetiSoft
void AddFontToListview(char *otagname, char *filebuf);

BOOL T1_InstallFont(char *filename, char *destdrawer, struct OTagValues *Presets, BOOL Update);
struct MinList *NewOTagList(void);
int AddOTag(struct MinList *list, ULONG tag, ULONG data, ULONG size);
ULONG SizeOTagList(struct MinList *list);
void DumpOTagList(struct MinList *list, void *buf);
void FreeOTagList(struct MinList *list);
BOOL DoAllAssigns(char *dos_path, BOOL (*passback_func) (BPTR lock, void (*passfunc) (char *filename, int filesize)), void (*passfunc) (char *filename, int filesize));
BOOL AddFontdir(BPTR lock, void (*passfunc) (char *filename, int filesize));
void FreeFontdirparts(void);
BOOL ScanDir(BPTR lock, void (*passfunc) (char *filename, int filesize));
void DeleteBitmaps(char *filename, int filesize);
void CheckFile(char *filename, int filesize);
BOOL IsOtagType1(char *buf);
BOOL ReadOTagFile(char *filename);
//BOOL RenderPreview(char *filename, ULONG pointheight, struct RastPort *rp, LONG leftedge, LONG topedge, LONG width, LONG height, char *previewstring);
BOOL RenderPreview(char *filename, ULONG pointheight, ULONG lineheight, int fixedwidth, struct RastPort *rp, LONG leftedge, LONG topedge, LONG width, LONG height, unsigned char *previewstring, unsigned char *previewstring2, struct TagItem *AddOTags);	// TetiSoft
BOOL GetUnicodeArray(char *Type1File);

int bitline(char *FontDir, char *FontName, long **Sizes);		// TetiSoft
BOOL CalcSpaceWidthYSizeFactor(Object *win, char *Type1File, ULONG IsFixed, ULONG SymbolSet, FIXED *SpaceWidthPtr, ULONG *YSizeFactorPtr);	// TetiSoft
BOOL HasOwnEncoding(char *Type1File);	// TetiSoft
