/* vi: set sw=4 ts=4: */
/*
 * KON2 - Kanji ON Console -
 * Copyright (C) 1992-1996 Takashi MANABE (manabe@papilio.tutics.tut.ac.jp)
 *
 * CCE - Console Chinese Environment -
 * Copyright (C) 1998-2003 Rui He (rhe@3eti.com)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 */

/* font.h -- font information */
#ifndef	__CCE_FONT_H__
#define	__CCE_FONT_H__

#define FONTWIDTHBYTES(width) 	  ((width) / 8 + (((width) % 8 > 0) ? 1: 0))  
#define FONTBYTES(width, height)  (FONTWIDTHBYTES(width) * (height))

extern int fontwidth, fontheight, fontbytes; /* for English chars */

extern int fontbytes2;  /* for Chinese/Japanese/Korean chars */

#define CHR_SFLD        0x80
#define CHR_DBC         0x20
#define CHR_DFLD        (CHR_SFLD|CHR_DBC)

typedef struct  _FontInfo_t
{
	
    u_int  size;           /* Allocated/Shared memory size, in Little Endian */
    u_char height, width;   /* Font Width/Height */
    u_char type;          /* Font Coding,  Index|CHR_SFLD or Index|CHR_DFLD */
	u_char reserved;      /* padding to make it 4bytes aligned */
}FontInfo;

#define FR_ATTACH       1
#define FR_PROXY        2
#define FR_X11FONT		3  /* added for X11 pcf/bdf font */
#define FR_FREETYPEFONT	4  /* added for freetype font */

typedef struct _FontRegs_t
{
    u_int (*addr)(u_char ch1, u_char ch2);   /* calculate address */
    u_int size;                              /* memory size */ 
    char *registry, *bitmap;                 /* registry and bitmap pointer */
    u_char height, width;                      /* char parameters */
    u_char sign0, sign1;
    u_char stat;                             /* FR_ATTACH or FR_PROXY */
    int max;                               /* Number of Chars */

	int fd; 				/* opened file descriptor for font file, -1 for not opened */
	FontInfo *fi;			/* newly added: -1 for not mapped */
}FontRegs;

typedef struct _LangInfo_t 
{
    u_char
	sb,  /* single byte font index , without CHR_SFLD, CHR_DFLD */
	db,  /* double byte font index */
	sysCoding;  /* system coding index CODE_BIG5, CODE_GB*/
}LangInfo;

extern LangInfo lInfo;

extern FontRegs fSRegs[], fDRegs[];
extern FontRegs *sbFReg, *dbFReg;

extern const int Coding2FontRegDB[];
extern const int Coding2FontRegSB[]; /* single byte */

void FontInit(void);
void FontAttach(void);
void FontDetach(int remove);
int ConfigFont(const char *str);

int MmapFont(int encoding, FontRegs *fr);
int MunmapFont(int encoding, FontRegs *fr);

extern u_char *GetX11FontBits(void *fr, int codepoint); /* return NULL for failure */

#if 0

static inline u_char *GetDBFontBits(FontRegs *dbFReg, int ch1, int ch2)
{
#ifdef SUPPORT_X11FONT
    if (dbFReg->stat == FR_X11FONT)  /* bdf or pcf font */
    {
        return GetX11FontBits(dbFReg->bitmap, (ch1 << 8) | ch2);
    }
    else /* mmaped */
#endif
	{
        u_int fnt = dbFReg->addr(ch1, ch2);
        if (fnt < dbFReg->size) return dbFReg->bitmap + fnt;
	}
	return NULL;	
}

static inline u_char *GetSBFontBits(FontRegs *sbFReg, int ch)
{
#ifdef SUPPORT_X11FONT
	if (sbFReg->stat == FR_X11FONT) /* bdf of pcf font */
	{
		return GetX11FontBits(sbFReg->bitmap, ch);
	}
	else /* mmaped */
#endif
	{
		return sbFReg->bitmap + ch * fontbytes;
	}
}

#endif

#endif
