Subject: alib.24 etc.

From: YLu <[email-masked]>
Date: Fri, 22 May 1992 15:58:31 BST

The program 'c2cc' by Bo Yang seems to lose one vertical scanning line
of the original bitmap. You can view the converted bitmap file using
'cc', the library viewer on PC by Zhang RoXin.

Here is another little program I wrote before. It seems to do the work.
To convert alib.24 to cckai.24, which can be used by chtex, use

gbfconvt -b alib.24 cckai.24

To use this font file with chtex on a unix system,

setenv CCLIB24jt ^/lib/cckai.24
chtex -w gb_file

The program works on MS-DOS machines.

[email-masked]
zWB@Sn3/

-------- CUT HERE --------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifndef BYTE
#define BYTE		8
#endif
#ifndef FONTSIZE
#define FONTSIZE	24
#endif
#ifndef QU
#define QU		94
#endif

#ifdef __MSDOS__
#define RMODE		"rb"
#define WMODE		"wb"
#define DIR_DELIM	'\\'
#else				/* unix */
#define RMODE		"r"
#define WMODE		"w"
#define DIR_DELIM	'/'
#endif

FILE *ifp, *ofp;
char old_buf[BUFSIZ], new_buf[BUFSIZ];
char *program;

int fill_qu = 0;
int to_row = 1;
int fontsize = FONTSIZE;
int old_row, old_col;
int new_row, new_col;
int num_bytes;

void usage();
void fill_blk();
int col2row(), row2col();

main(argc, argv)
int argc;
char **argv;
{
    /* get program name */
    if ((program = strrchr(argv[0], DIR_DELIM)) == NULL)
	program = argv[0];
    else
	program++;

    /* process command arguments */
    /* better to use getopt() */
    while (--argc > 0 && (*++argv)[0] == '-')
	switch ((*argv)[1]) {
	case 'f':
	    fontsize = atoi(*argv + 2);
	    break;
	case 'r':
	    to_row = 1;
	    break;
	case 'c':
	    to_row = 0;
	    break;
	case 'b':
	    fill_qu = 1;
	    break;
	default:
	    usage();
	    break;
	}

    if (argc < 2)
	usage();
    else if ((ifp = fopen(argv[0], RMODE)) == NULL) {
	(void) fprintf(stderr, "cannot open input file: %s\n", argv[0]);
	exit(1);
    } else if ((ofp = fopen(argv[1], WMODE)) == NULL) {
	(void) fprintf(stderr, "cannot open output file: %s\n", argv[1]);
	exit(1);
    }
    /* sanity check */
    if ((fontsize % 8 != 0) || (fontsize == 0)) {
	(void) fprintf(stderr, "can only convert square fonts!\n");
	exit(1);
    }
    if (to_row)
	col2row();
    else
	row2col();

    (void) fclose(ifp);
    (void) fclose(ofp);
}

void usage()
{
    fprintf(stderr, "Usage:\t%s [-fSIZE] [-r|c] [-b] [input] [output]\n\n",
 program);
    fprintf(stderr, "\tConvert square GB bitmap font format\n");
    fprintf(stderr, "\t-f font size if not %dx%d\n", FONTSIZE, FONTSIZE);
    fprintf(stderr, "\t-r convert from column order to row order (default)\n");
    fprintf(stderr, "\t-c convert from row order to column order\n");
    fprintf(stderr, "\t-b fill blank qu\n");
    exit(1);
}

void fill_blk(size)
int size;
{
    char c = 0;
    int i;

    for (i = 0; i < size; i++)
	(void) fputc(c, ofp);
}

int col2row()
{
    int i, j;
    int new_ri, new_ci;
    int old_ri, old_c0, old_ci;
    int old_bi;
    int old_idx;

    int blk_start = 9 * QU;
    int blk_end = 15 * QU;
    int gbcode = 0;

    new_row = old_col = fontsize;
    new_col = old_row = fontsize / BYTE;
    num_bytes = new_row * new_col;

    while (1) {
	gbcode++;
	if (fill_qu && gbcode >= blk_start && gbcode < blk_end) {
	    fill_blk((blk_end - blk_start) * num_bytes);
	    gbcode += blk_end - blk_start;
	}
	/* read num_bytes long bitmap of one code */
	(void) fread(old_buf, sizeof(char), num_bytes, ifp);
	if (feof(ifp) || ferror(ifp)) {
	    return;
	}
	for (i = 0; i < num_bytes; i++) {
	    new_buf[i] = 0;
	    new_ri = i / new_col;
	    new_ci = i % new_col;
	    old_ri = new_ri / BYTE;
	    old_c0 = new_ci * BYTE;
	    old_bi = new_ri % BYTE;
	    for (j = 0; j < BYTE; j++) {
		old_ci = old_c0 + j;
		old_idx = old_ci * old_row + old_ri;
		new_buf[i] |= ((old_buf[old_idx] << old_bi) & 0x80) >> j;
	    }
	}
	if (fwrite(new_buf, sizeof(char), num_bytes, ofp) != num_bytes) {
	    (void) fprintf(stderr, "output error!\n");
	    exit(-1);
	}
    }
}

int row2col()
{
    int i, j;
    int new_ri, new_ci;
    int old_ri, old_r0, old_ci;
    int old_bi;
    int old_idx;

    new_row = old_col = fontsize / BYTE;
    new_col = old_row = fontsize;
    num_bytes = new_row * new_col;

    while (1) {
	/* read num_bytes long bitmap of one code */
	(void) fread(old_buf, sizeof(char), num_bytes, ifp);
	if (feof(ifp) || ferror(ifp)) {
	    return;
	}
	for (i = 0; i < num_bytes; i++) {
	    new_buf[i] = 0;
	    new_ri = i % new_row;
	    new_ci = i / new_row;
	    old_ci = new_ci / BYTE;
	    old_r0 = new_ri * BYTE;
	    old_bi = new_ci % BYTE;
	    for (j = 0; j < BYTE; j++) {
		old_ri = old_r0 + j;
		old_idx = old_ri * old_col + old_ci;
		new_buf[i] |= ((old_buf[old_idx] << old_bi) & 0x80) >> j;
	    }
	}
	if (fwrite(new_buf, sizeof(char), num_bytes, ofp) != num_bytes) {
	    (void) fprintf(stderr, "output error!\n");
	    exit(-1);
	}
    }
}