#include "fntfilst.h"
#include "fontenc.h"
#ifndef FONTMODULE
#include <stdio.h>
#else
#include "xf86_ansic.h"
#endif
#include "spint.h"
#include "bics-unicode.h"
SpeedoFontPtr sp_fp_cur = (SpeedoFontPtr) 0;
#ifdef EXTRAFONTS
#include "ncdkeys.h"
#endif
#include "keys.h"
#ifdef EXTRAFONTS
static ufix8 skey[] =
{
SKEY0,
SKEY1,
SKEY2,
SKEY3,
SKEY4,
SKEY5,
SKEY6,
SKEY7,
SKEY8
};
static ufix8 rkey[] =
{
RKEY0,
RKEY1,
RKEY2,
RKEY3,
RKEY4,
RKEY5,
RKEY6,
RKEY7,
RKEY8
};
#endif
#ifdef XSAMPLEFONTS
static ufix8 xkey[] =
{
XKEY0,
XKEY1,
XKEY2,
XKEY3,
XKEY4,
XKEY5,
XKEY6,
XKEY7,
XKEY8
};
#endif
static ufix8 mkey[] =
{
KEY0,
KEY1,
KEY2,
KEY3,
KEY4,
KEY5,
KEY6,
KEY7,
KEY8
};
static fix15
read_2b(ufix8 *ptr)
{
fix15 tmp;
tmp = *ptr++;
tmp = (tmp << 8) + *ptr;
return tmp;
}
static fix31
read_4b(ufix8 *ptr)
{
fix31 tmp;
tmp = *ptr++;
tmp = (tmp << 8) + *ptr++;
tmp = (tmp << 8) + *ptr++;
tmp = (tmp << 8) + *ptr;
return tmp;
}
buff_t *
sp_load_char_data(fix31 file_offset, fix15 num, fix15 cb_offset)
{
SpeedoMasterFontPtr master = sp_fp_cur->master;
if (fseek(master->fp, (long) file_offset, (int) 0)) {
SpeedoErr("can't seek to char\n");
}
if ((num + cb_offset) > master->mincharsize) {
SpeedoErr("char buf overflow\n");
}
if (fread((master->c_buffer + cb_offset), sizeof(ufix8), num,
master->fp) != num) {
SpeedoErr("can't get char data\n");
}
master->char_data.org = (ufix8 *) master->c_buffer + cb_offset;
master->char_data.no_bytes = num;
return &master->char_data;
}
struct speedo_encoding {
char *name;
int *enc;
int enc_size;
};
static int
find_encoding(const char *fontname, const char *filename,
int **enc, int *enc_size)
{
static struct speedo_encoding *known_encodings=0;
static int number_known_encodings=0;
static int known_encodings_size=0;
char *encoding_name;
int iso8859_1;
FontMapPtr mapping;
int i, j, k, size;
struct speedo_encoding *temp;
int *new_enc;
char *new_name;
iso8859_1 = 0;
encoding_name = FontEncFromXLFD(fontname, strlen(fontname));
if(!encoding_name) {
encoding_name="iso8859-1";
iso8859_1=1;
}
iso8859_1 = iso8859_1 || !strcmp(encoding_name, "iso8859-1");
for(i=0; i<number_known_encodings; i++) {
if(!strcmp(encoding_name, known_encodings[i].name)) {
*enc=known_encodings[i].enc;
*enc_size=known_encodings[i].enc_size;
return Successful;
}
}
if(known_encodings==0) {
if((known_encodings=
(struct speedo_encoding*)xalloc(2*sizeof(struct speedo_encoding)))
==0)
return AllocError;
number_known_encodings=0;
known_encodings_size=2;
}
if(number_known_encodings >= known_encodings_size) {
if((temp=
(struct speedo_encoding*)xrealloc(known_encodings,
2*sizeof(struct speedo_encoding)*
known_encodings_size))==0)
return AllocError;
known_encodings=temp;
known_encodings_size*=2;
}
mapping=0;
if(!iso8859_1) {
mapping = FontEncMapFind(encoding_name,
FONT_ENCODING_UNICODE, -1, -1,
filename);
}
#define SPEEDO_RECODE(c) \
(mapping? \
unicode_to_bics(FontEncRecode(c, mapping)): \
unicode_to_bics(c))
if((new_name = (char*)xalloc(strlen(encoding_name)))==0)
return AllocError;
strcpy(new_name, encoding_name);
size=0;
for(i=0; i < (mapping?mapping->encoding->size:256) && i < 256; i++)
if(SPEEDO_RECODE(i)>=0)
size++;
new_enc = (int*)xalloc(2*size*sizeof(int));
if(!new_enc) {
xfree(new_name);
return AllocError;
}
for(i=j=0; i < (mapping?mapping->encoding->size:256) && i < 256; i++)
if((k = SPEEDO_RECODE(i))>=0) {
new_enc[2*j] = i;
new_enc[2*j+1] = k;
j++;
}
known_encodings[number_known_encodings].name = new_name;
known_encodings[number_known_encodings].enc = new_enc;
known_encodings[number_known_encodings].enc_size = size;
number_known_encodings++;
*enc = new_enc;
*enc_size = size;
return Successful;
#undef SPEEDO_RECODE
}
int
sp_open_master(const char *fontname, const char *filename,
SpeedoMasterFontPtr *master)
{
SpeedoMasterFontPtr spmf;
ufix8 tmp[16];
ufix16 cust_no;
FILE *fp;
ufix32 minbufsize;
ufix16 mincharsize;
ufix8 *f_buffer;
ufix8 *c_buffer;
int ret;
ufix8 *key;
spmf = (SpeedoMasterFontPtr) xalloc(sizeof(SpeedoMasterFontRec));
if (!spmf)
return AllocError;
bzero(spmf, sizeof(SpeedoMasterFontRec));
spmf->entry = NULL;
spmf->f_buffer = NULL;
spmf->c_buffer = NULL;
spmf->fname = (char *) xalloc(strlen(filename) + 1);
if (!spmf->fname)
return AllocError;
fp = fopen(filename, "r");
if (!fp) {
ret = BadFontName;
goto cleanup;
}
strcpy(spmf->fname, filename);
spmf->fp = fp;
spmf->state |= MasterFileOpen;
if (fread(tmp, sizeof(ufix8), 16, fp) != 16) {
ret = BadFontName;
goto cleanup;
}
minbufsize = (ufix32) read_4b(tmp + FH_FBFSZ);
f_buffer = (ufix8 *) xalloc(minbufsize);
if (!f_buffer) {
ret = AllocError;
goto cleanup;
}
spmf->f_buffer = f_buffer;
fseek(fp, (ufix32) 0, 0);
if (fread(f_buffer, sizeof(ufix8), (ufix16) minbufsize, fp) != minbufsize) {
ret = BadFontName;
goto cleanup;
}
spmf->copyright = (char *) (f_buffer + FH_CPYRT);
spmf->mincharsize = mincharsize = read_2b(f_buffer + FH_CBFSZ);
c_buffer = (ufix8 *) xalloc(mincharsize);
if (!c_buffer) {
ret = AllocError;
goto cleanup;
}
spmf->c_buffer = c_buffer;
spmf->font.org = spmf->f_buffer;
spmf->font.no_bytes = minbufsize;
cust_no = sp_get_cust_no(spmf->font);
#ifdef EXTRAFONTS
if (cust_no == SCUS0) {
key = skey;
} else if (cust_no == RCUS0) {
key = rkey;
} else
#endif
#ifdef XSAMPLEFONTS
if (cust_no == XCUS0) {
key = xkey;
} else
#endif
if (cust_no == CUS0) {
key = mkey;
} else {
SpeedoErr("Non - standard encryption for \"%s\"\n", filename);
ret = BadFontName;
goto cleanup;
}
spmf->key = key;
sp_set_key(key);
spmf->first_char_id = read_2b(f_buffer + FH_FCHRF);
spmf->num_chars = read_2b(f_buffer + FH_NCHRL);
spmf->enc = 0;
spmf->enc_size = 0;
#ifdef EXTRAFONTS
{
char *f;
f = strrchr(filename, '/');
if (f) {
f++;
if (strncmp(f, "bx113", 5) == 0) {
spmf->enc = adobe_map;
spmf->enc_size = adobe_map_size;
}
}
}
#endif
if(!spmf->enc)
if((ret=find_encoding(fontname, filename, &spmf->enc, &spmf->enc_size))
!=Successful)
goto cleanup;
spmf->first_char_id = spmf->enc[0];
spmf->max_id = spmf->enc[(spmf->enc_size - 1) * 2];
spmf->num_chars = spmf->enc_size;
*master = spmf;
return Successful;
cleanup:
*master = (SpeedoMasterFontPtr) 0;
sp_close_master_font(spmf);
return ret;
}
void
sp_close_master_font(SpeedoMasterFontPtr spmf)
{
if (!spmf)
return;
if (spmf->state & MasterFileOpen)
fclose(spmf->fp);
if (spmf->entry)
spmf->entry->u.scalable.extra->private = NULL;
xfree(spmf->fname);
xfree(spmf->f_buffer);
xfree(spmf->c_buffer);
xfree(spmf);
}
void
sp_close_master_file(SpeedoMasterFontPtr spmf)
{
(void) fclose(spmf->fp);
spmf->state &= ~MasterFileOpen;
}
void
sp_reset_master(SpeedoMasterFontPtr spmf)
{
sp_set_key(spmf->key);
if (!(spmf->state & MasterFileOpen)) {
spmf->fp = fopen(spmf->fname, "r");
spmf->state |= MasterFileOpen;
}
fseek(spmf->fp, 0, 0);
}