コード例 #1
0
ファイル: psf2zx.c プロジェクト: damieng/johnelliot
/* 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;
}
コード例 #2
0
ファイル: psfmerge.c プロジェクト: damieng/johnelliot
/* 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;
}
コード例 #3
0
ファイル: psf2xff.c プロジェクト: thequux/Xenon
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;
}
コード例 #4
0
ファイル: psf2wyse.c プロジェクト: damieng/johnelliot
char *cnv_execute(FILE *fpin, FILE *fpout)
{	
	int rv;
	PSF_FILE psf;
	psf_dword ch;
	psf_dword y, wb;

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

	if (psf.psf_width > 8)
	{
		fprintf(stderr, "Warning: Input file is wider than 8 pixels. Truncating at 8.\n");
	}
	if (height > MAX_H)
	{
		fprintf(stderr, "Warning: Height cannot exceed %d. Truncating.\n", MAX_H);
		height = MAX_H;
	}
	if ((psf.psf_length - 1) < first) first = psf.psf_length - 1;
	if ((psf.psf_length - 1) < last)  last  = psf.psf_length - 1;

	/* wb = number of bytes per line */	
	wb = psf.psf_charlen / psf.psf_height;	
	for (ch = first; ch <= last; ch++)
	{
		memset(charbuf, 0, sizeof(charbuf));

		/* Populate character buffer */
		for (y = 0; y < psf.psf_height; y++)
		{
			if (y >= MAX_H) break;
			charbuf[y] = psf.psf_data[ch * psf.psf_charlen + 
			       			  y * wb];	
		}
		fputc(0x1B, fpout);		// ESC
		fputc('c',  fpout);		// c
		fputc('A',  fpout);		// A  : Redefine
		fputc(bank + '0', fpout);	// Bank
		fprintf(fpout, "%02x", dest);	// Dest cell
		for (y = 0; y < height; y++)
		{
			fprintf(fpout, "%02x", charbuf[y]);	// Char shape
		}
		fputc(0x19, fpout);		// End of char definition
		for (y = 0; y < nulls; y++)
		{
			fputc(0, fpout);
		}
		++dest;
		if (dest == 0x80)
		{
			dest = 0;
			++bank;
			if (bank > 3) break;
		}
	}

	psf_file_delete(&psf);
	return 0;
}