Ejemplo n.º 1
0
/* Plain copy of character bitmap PSF->ZX */
static void copychar(PSF_FILE *src, ZXF_FILE *dst, int from, int to)
{
	psf_dword idx;
	psf_byte *sptr;
	zxf_byte *dptr;
	static psf_byte nil8[8];

/* If the PSF has a Unicode directory, look up the tokens in it */
	if (psf_is_unicode(src))
	{
		sptr = nil8;
		if (!psf_unicode_lookup(src, from, &idx))
			sptr = src->psf_data + 8 *  idx;
/* I don't expect many fonts to have the uparrow. So if that can't be found, 
 * fall back on 0x5E */
		else if (from == 0x2191 && !psf_unicode_lookup(src, 0x5E, &idx))
			sptr = src->psf_data + 8 *  idx;
	}
	else
	{
		/* 0x18 is the most likely place to find an uparrow in a
		 * non-unicode PSF. */
		if (from == 0x2191) from = 0x18;
		sptr = src->psf_data + 8 *  from;
	}
	dptr = dst->zxf_data + 8 * (to - 32);
    
	memcpy(dptr, sptr, 8);
}
Ejemplo n.º 2
0
/* Do the conversion */
char *cnv_execute(FILE *infile, FILE *outfile)
{	
	int rv, x;
	PSF_FILE psf;
	ZXF_FILE zxf;

	psf_file_new(&psf);
	rv = psf_file_read(&psf, infile);	
	if (rv != PSF_E_OK) return psf_error_string(rv);

	if (psf.psf_height != 8 || psf.psf_width != 8)
	{
		psf_file_delete(&psf);
		return "PSF characters must be 8x8 for a Spectrum font.";
	}
	if (mode == -1)
	{
		if (psf_is_unicode(&psf)) mode = MD_MERGE1;
		else			  mode = MD_BARE;
	}

	zxf_file_new(&zxf);
	if (rv == PSF_E_OK) rv = zxf_file_create(&zxf);	
	if (rv == PSF_E_OK)
	{
            if (mode == MD_BARE)
		 for (x = 32; x <= 127; x++) copychar(&psf, &zxf, x, x);
            else for (x = 32; x <= 127; x++) switch(x)
                {
                case 0x5E: copychar(&psf, &zxf, 0x2191, x); break; 
                case 0x60: copychar(&psf, &zxf, 0xA3, x); break;
                case 0x7F: copychar(&psf, &zxf, 0xA9, x); break;
                default:   copychar(&psf, &zxf, x, x);    break;
                }
	    rv = zxf_file_write(&zxf, outfile, zxf_format, auxfile);
	}
	psf_file_delete(&psf);
	
	if (rv != ZXF_E_OK)
	{
		zxf_file_delete(&zxf);
		return zxf_error_string(rv);
	}
	zxf_file_delete(&zxf);
	if (auxfile) free(auxfile);
	return NULL;
}
Ejemplo n.º 3
0
char *cnv_execute(FILE *infile, FILE *outfile)
{	
	int rv, n, max;
	psf_dword ch, f, l, z, glyph;
//	int ofile = fileno (outfile);

	psf_file_new(&psf);
        rv = psf_file_read(&psf, infile);

	if (rv != PSF_E_OK) return psf_error_string(rv);
	if (codepage && !psf_is_unicode(&psf))
	{
		psf_file_delete(&psf);
		return "Cannot extract by codepage; source file has no Unicode table";
	}

	/* Most systems that use raw fonts depend on the font being 1 byte 
	 * wide */
	if (psf.psf_width > 8) 
		fprintf(stderr, "Warning: Font is wider than 8 bits.\n");

	f = (first >= 0) ? first : 0;
	l = (last  >= 0) ? last  :((int)(psf.psf_length - 1));
	if (codepage && l >= 256) l = 255;
	if (l >= psf.psf_length) l = psf.psf_length;
	
	struct XFF xff;
	xff.w = psf.psf_width;
	xff.h = psf.psf_height;
	xff.w_byte = (xff.w + 7) / 8;
	xff.glyph_size = xff.h * xff.w_byte;
	xff.nGlyphs = l-f+1;
	xff.chkxor = 0;
	xff.reserved = 2;
	fwrite (&xff, sizeof(xff), 1, outfile);

	if (doflip)
	{
		max = (last+1) * psf.psf_charlen;
		for (n = first * psf.psf_charlen; n < max; n++)
		{
			psf.psf_data[n] = flip(psf.psf_data[n]);
		}
	}
	for (ch = f; ch <= l; ch++)
	{
		psf_byte *src;

		if (codepage)
		{
			if (ch < 256 && !psf_unicode_lookupmap(&psf, codepage, 
						ch, &glyph, NULL))
			{
				src = psf.psf_data + glyph * psf.psf_charlen;
			}
			else
			{
				if (ch < 256 && !psf_unicode_banned(codepage->psfm_tokens[ch][0])) fprintf(stderr, "Warning: U+%04lx not found in font\n", codepage->psfm_tokens[ch][0]);
				src = NULL;
			}
		}
		else	src = psf.psf_data + ch * psf.psf_charlen;
		
		if (!src) for (z = 0; z < psf.psf_charlen; z++)
		{
			if (fputc(0, outfile) == EOF)
			{
				psf_file_delete(&psf);
				return "Could not write to output.";
			}	
		}	
		else if (fwrite(src, 1, psf.psf_charlen, outfile) < 
				psf.psf_charlen)
		{
			psf_file_delete(&psf);
			return "Could not write to output.";
		}
		// padding
	}	
	
	long int pos = ftell(outfile);
	unsigned char buf;
	unsigned char chkxor = 0;
	fseek (outfile, 0, SEEK_SET);
	while (fread(&buf, 1,1,outfile)) {
		chkxor ^= buf;
	}
	printf ("%hhx\n", chkxor);
	fseek(outfile, 6, SEEK_SET);
	fwrite(&chkxor, 1, 1, outfile);
	fseek(outfile, pos, SEEK_SET);
	psf_file_delete(&psf);	
	return NULL;
}
Ejemplo n.º 4
0
/* Do the conversion */
char *cnv_multi(int nfiles, char **infiles, FILE *outfile)
{ 
        int rv, nf, nc;
	psf_dword mc;
        FILE *fp;
	int w = -1, h = -1;
	psf_unicode_dirent *ude;

/* Load all the source files */
	psf_in = malloc(nfiles * sizeof(PSF_FILE));
	if (!psf_in) return "Out of memory";
        for (nf = 0; nf < nfiles; nf++)
        { 
		psf_file_new(&psf_in[nf]);
                if (!strcmp(infiles[nf], "-")) fp = stdin;
                else  fp = fopen(infiles[nf], "rb");
          
                if (!fp) 
		{ 
			perror(infiles[nf]); 
			return "Cannot open font file."; 
		}
		rv = psf_file_read(&psf_in[nf], fp);
                if (fp != stdin) fclose(fp);
		if (rv != PSF_E_OK) 
		{
			sprintf(msgbuf, "%s: %s", infiles[nf],
					psf_error_string(rv));
			return msgbuf;
		}
		if (w == -1 && h == -1)
		{
			w = psf_in[nf].psf_width;
			h = psf_in[nf].psf_height;
		}
		else if (w != psf_in[nf].psf_width || h != psf_in[nf].psf_height)
		{
			return "All fonts must have the same dimensions.";
		}
		if (!psf_is_unicode(&psf_in[nf]))
		{
			sprintf(msgbuf, "%s has no Unicode table.", infiles[nf]);
			return msgbuf;
		}
	}
/* Work out how many unique characters there are */
        for (nf = 0; nf < nfiles; nf++)
        { 
		for (nc = 0; nc < psf_in[nf].psf_length; nc++)
		{
			/* Skip characters with no Unicode mapping */
			if (!psf_in[nf].psf_dirents_used[nc]) continue;
			rv = unicode_add(psf_in[nf].psf_dirents_used[nc]);
			if (rv) 
			{
				free_buffers();
				return psf_error_string(rv);
			}
		}
	}
	rv = psf_file_create(&psf_out, w, h, unicodes_count, 1);
	if (rv) 
	{
		free_buffers();
		return psf_error_string(rv);
	}
        for (nf = 0; nf < nfiles; nf++)
        { 
		for (nc = 0; nc < psf_in[nf].psf_length; nc++)
		{
			/* Skip characters with no Unicode mapping */
			if (!psf_in[nf].psf_dirents_used[nc]) continue;
			mc = unicode_lookup(psf_in[nf].psf_dirents_used[nc]);
			memcpy(psf_out.psf_data + mc * psf_out.psf_charlen,
			       psf_in[nf].psf_data + nc * psf_in[nf].psf_charlen,
			       psf_out.psf_charlen);
		}
	}
	/* Do the Unicode directory */
	rv = 0;
	for (nf = 0; nf < unicodes_count; nf++)
	{
		for (ude = unicodes[nf]; ude != NULL; ude = ude->psfu_next)
		{
			rv = psf_unicode_add(&psf_out, nf, ude->psfu_token);
			if (rv) break;
		}
		if (rv) break;
	}
	if (!rv) rv = psf_file_write(&psf_out, outfile);
	psf_file_delete(&psf_out);
	free_buffers();
        for (nf = 0; nf < nfiles; nf++)
        { 
		psf_file_delete(&psf_in[nf]);
	}
	free(psf_in);
	if (rv) return psf_error_string(rv);
	return NULL;
}