/* * bdfjpindex.c - Make an index file of BDF font * * Programmmed by Hirotsugu Kakugawa, Hiroshima University * E-Mail: kakugawa@se.hiroshima-u.ac.jp * * Edition History * 27 Dec 1995 * 21 Mar 1996 Chenged to check ENCODING to get character code. */ /* This file is part of VFlib * * Copyright (C) 1995,1996 Hirotsugu KAKUGAWA. All rights reserved. * * VFlib is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY. No author or distributor accepts responsibility * to anyone for the consequences of using it or for whether it serves any * particular purpose or works at all, unless he says so in writing. Refer * to the GNU General Public License for full details. * * Everyone is granted permission to copy, modify and redistribute * VFlib, but only under the conditions described in the GNU * General Public License. A copy of this license is supposed to have been * given to you along with VFlib so you can know your rights and * responsibilities. It should be in a file named COPYING. Among other * things, the copyright notice and this notice must be preserved on all * copies. */ #include #include #include #define BDX_VERSION "1" #define BDF_SUFFIX ".bdf" #define IDX_SUFFIX ".bdx" #ifndef SEEK_SET # define SEEK_SET 0 #endif struct s_ch_table { int ccode; /* char code */ long f_pos; /* file position */ }; typedef struct s_ch_table bdf_char_table; struct s_bdf { int bdf_width; int bdf_height; bdf_char_table *bdf_chars; int bdf_nchar; }; typedef struct s_bdf bdf; bdf *bdf_table = NULL; int Usage(); int ReadBDF(); int MakeBDFIndex(); int BDF_ReadProp(); int BDF_CountChars(); int BDF_ReadIndex(); int _BDF_ReadIndex(); int BDF_SortIndex(); int BDF_Partition(); int WriteIndex(); int main(argc, argv) int argc; char **argv; { char *fname, *bdf_name, *idx_name; FILE *ifp; FILE *ofp; if (argc < 2) Usage(); fname = argv[1]; if ((bdf_name = malloc(strlen(fname)+strlen(BDF_SUFFIX)+1)) == NULL){ fprintf(stderr, "No memory\n"); exit(1); } if ((idx_name = malloc(strlen(fname)+strlen(IDX_SUFFIX)+1)) == NULL){ fprintf(stderr, "No memory\n"); exit(1); } strcpy(bdf_name, fname); strcat(bdf_name, BDF_SUFFIX); if ((ifp = fopen(bdf_name, "r")) == NULL){ fprintf(stderr, "Cannot open %s\n", bdf_name); exit(1); } strcpy(idx_name, fname); strcat(idx_name, IDX_SUFFIX); if ((ofp = fopen(idx_name, "w")) == NULL){ fprintf(stderr, "Cannot open %s\n", idx_name); exit(1); } (void) ReadBDF(ifp); (void) WriteIndex(ofp); return 0; } int Usage() { fprintf(stderr, "Usage: bdfindex bdf-file\n"); fprintf(stderr, " bdf-file should not contain .bdf extension.\n"); fprintf(stderr, " Index file is generated as bdf-file.bdx\n"); fprintf(stderr, " Example: bdfindex jisakan16\n"); exit(1); } int ReadBDF(fp) FILE *fp; { if ((bdf_table = (bdf*)malloc(sizeof(bdf))) == NULL) goto Err3; bdf_table->bdf_width = 0; bdf_table->bdf_height = 0; if (BDF_ReadProp(fp) < 0) goto Err2; bdf_table->bdf_nchar = BDF_CountChars(fp); if (bdf_table->bdf_nchar < 0) goto Err2; bdf_table->bdf_chars = (bdf_char_table*) calloc(bdf_table->bdf_nchar, sizeof(bdf_char_table)); if (bdf_table->bdf_chars == NULL) goto Err2; if (BDF_ReadIndex(fp) < 0) goto Err1; return 0; Err1: free(bdf_table->bdf_chars); Err2: bdf_table = NULL; Err3: return -1; } int BDF_ReadProp(fp) FILE *fp; { int found; char buf[160]; char prop_name[160]; int prop_val; if (bdf_table == NULL){ printf("bdfindex: BDF_ReadProp - ILL ARG\n"); return -1; } found = 0; for (;;){ if (fgets(buf, sizeof(buf), fp) == NULL) break; if (strncmp(buf, "ENDPROPERTIES", 13) == 0) break; if (strncmp(buf, "PIXEL_SIZE", 10) == 0){ sscanf(buf, "%s %d", prop_name, &prop_val); found = 1; bdf_table->bdf_width = prop_val; bdf_table->bdf_height = prop_val; break; } } if (found == 0) return -1; return 0; } int BDF_CountChars(fp) FILE *fp; { return _BDF_ReadIndex(fp, 1); } int BDF_ReadIndex(fp) FILE *fp; { return _BDF_ReadIndex(fp, 0); } int _BDF_ReadIndex(fp, count_only) FILE *fp; int count_only; { int ch_code, ch_index, last_ch, need_sorting; char buf[160]; char prop_name[160]; if (bdf_table == NULL){ printf("bdfindex: BDF_ReadIndex - ILL ARG\n"); return -1; } fseek(fp, 0, SEEK_SET); ch_index = 0; last_ch = 0; need_sorting = 0; for (;;){ if (fgets(buf, sizeof(buf), fp) == NULL) break; if (strncmp(buf, "ENDFONT", 7) == 0) break; if (strncmp(buf, "ENCODING", 8) == 0){ sscanf(buf, "%s %d", prop_name, &ch_code); if (last_ch > ch_code) /* must sort to use binary search */ need_sorting = 1; for (;;){ if (fgets(buf, sizeof(buf), fp) == NULL) goto End; if (strncmp(buf, "ENDCHAR", 7) == 0) break; if (strncmp(buf, "BITMAP", 6) == 0){ if (count_only == 0){ bdf_table->bdf_chars[ch_index].f_pos = (long) ftell(fp); bdf_table->bdf_chars[ch_index].ccode = ch_code; /*printf("%04d: %ld\n", ch_code, bdf_table->bdf_chars[ch_index].f_pos);*/ } last_ch = ch_code; ch_index++; } } } } End: /*printf("XXX nchar = %d\n", ch_index);*/ /*XXX*/ if ((count_only == 0) && (need_sorting == 1)){ /* must sort the index table for binary search */ BDF_SortIndex(bdf_table->bdf_chars, 0, ch_index-1); } return ch_index; } int BDF_SortIndex(bdfchp, x, y) bdf_char_table *bdfchp; int x, y; { int z; if (x < y){ z = BDF_Partition(bdfchp, x, y); (void) BDF_SortIndex(bdfchp, x, z-1); (void) BDF_SortIndex(bdfchp, z+1, y); } return 0; } int BDF_Partition(bdfchp, x, y) bdf_char_table *bdfchp; int x, y; { int t, i, p, itmp; long ltmp; t = bdfchp[x].ccode; i = x+1; p = x; for (;;){ if (y < i){ return p; } else if (bdfchp[i].ccode < t){ itmp = bdfchp[i].ccode; ltmp = bdfchp[i].f_pos; bdfchp[i].ccode = bdfchp[p+1].ccode; bdfchp[i].f_pos = bdfchp[p+1].f_pos; bdfchp[p+1].ccode = itmp; bdfchp[p+1].f_pos = ltmp; i++; p++; } else { i++; } } } int WriteIndex(fp) FILE *fp; { int i; fprintf(fp, "%s\n", BDX_VERSION); fprintf(fp, "%d %d\n", bdf_table->bdf_width, bdf_table->bdf_height); fprintf(fp, "%d\n", bdf_table->bdf_nchar); i = 0; while (i < bdf_table->bdf_nchar){ fprintf(fp, "%04x %lx\n", bdf_table->bdf_chars[i].ccode, bdf_table->bdf_chars[i].f_pos); i++; } return 0; }