📄 font_pcf.c
字号:
size = dwswap(size); m = *metrics = (struct metric_entry *) malloc(size * sizeof(struct metric_entry)); FREAD(file, m, sizeof(struct metric_entry) * size); for (i=0; i < size; i++) { m[i].leftBearing = dwswap(m[i].leftBearing); m[i].rightBearing = dwswap(m[i].rightBearing); m[i].width = dwswap(m[i].width); m[i].ascent = dwswap(m[i].ascent); m[i].descent = dwswap(m[i].descent); m[i].attributes = dwswap(m[i].attributes); } return size; } else { unsigned short size; int i; struct metric_entry *m; FREAD(file, &size, sizeof(size)); /* 16 bits - Number of metrics */ size = wswap(size); m = *metrics = (struct metric_entry *) malloc(size * sizeof(struct metric_entry)); for (i = 0; i < size; i++) { unsigned char val; FREAD(file, &val, sizeof(val)); m[i].leftBearing = val - 0x80; FREAD(file, &val, sizeof(val)); m[i].rightBearing = val - 0x80; FREAD(file, &val, sizeof(val)); m[i].width = val - 0x80; FREAD(file, &val, sizeof(val)); m[i].ascent = val - 0x80; FREAD(file, &val, sizeof(val)); m[i].descent = val - 0x80; } return size; }}static intpcf_read_encoding(FILE * file, struct encoding_entry **encoding){ int offset, n; unsigned short code; unsigned long format; struct encoding_entry *e; if ((offset = pcf_get_offset(PCF_BDF_ENCODINGS)) == -1) return -1; FSEEK(file, offset, SEEK_SET); FREAD(file, &format, sizeof(format)); format = dwswap(format); e = *encoding = (struct encoding_entry *) malloc(sizeof(struct encoding_entry)); FREAD(file, &e->min_byte2, sizeof(e->min_byte2)); FREAD(file, &e->max_byte2, sizeof(e->max_byte2)); FREAD(file, &e->min_byte1, sizeof(e->min_byte1)); FREAD(file, &e->max_byte1, sizeof(e->max_byte1)); FREAD(file, &e->defaultchar, sizeof(e->defaultchar)); e->min_byte2 = wswap(e->min_byte2); e->max_byte2 = wswap(e->max_byte2); e->min_byte1 = wswap(e->min_byte1); e->max_byte1 = wswap(e->max_byte1); e->defaultchar = wswap(e->defaultchar); e->count = (e->max_byte2 - e->min_byte2 + 1) * (e->max_byte1 - e->min_byte1 + 1); e->map = (unsigned short *) malloc(e->count * sizeof(unsigned short)); DPRINTF("def char %d (%x)\n", e->defaultchar, e->defaultchar); for (n = 0; n < e->count; ++n) { FREAD(file, &code, sizeof(code)); e->map[n] = wswap(code); /*DPRINTF("ncode %x (%c) %x\n", n, n, e->map[n]);*/ } DPRINTF("size %d byte1 %d,%d byte2 %d,%d\n", e->count, e->min_byte1, e->max_byte1, e->min_byte2, e->max_byte2); return e->count;}static intpcf_read_toc(FILE * file, struct toc_entry **toc, unsigned long *size){ char version[5]; FSEEK(file, 0, SEEK_SET); FREAD(file, &version, 4); /* 32 bits - version */ version[4] = 0; /* Verify the version */ if (strcmp(version, PCF_VERSION)) { DPRINTF("Bad .PCF file\n"); DPRINTF("Got %2.2x %2.2x %2.2x %2.2x and I expected %2.2x %2.2x %2.2x %2.2x\n", version[0], version[1], version[2], version[3], 'p', 'c', 'f', '\0'); return -1; } FREAD(file, size, sizeof(*size)); *size = dwswap(*size); *toc = (struct toc_entry *) calloc(sizeof(struct toc_entry), *size); if (!*toc) return -1; /* Read in the entire table of contents */ FREAD(file, *toc, sizeof(struct toc_entry) * *size); return 0;}PMWCOREFONTpcf_createfont(const char *name, MWCOORD height, int attr){ FILE *file = 0; MWCOREFONT *pf = 0; int offset; int i; int count; int bsize; int bwidth; int err = 0; struct metric_entry *metrics = 0; struct encoding_entry *encoding = 0; MWIMAGEBITS *output; unsigned char *glyphs = 0; unsigned long *glyphs_offsets = 0; int max_width = 0, max_descent = 0, max_ascent = 0, max_height = 0; int glyph_count; unsigned short *goffset = 0; unsigned char *gwidth = 0; int endian, uc16; char fname[256]; /* Try to open the file */ file = FOPEN(name, "rb"); if (!file) { strcpy(fname, PCF_FONT_DIR "/"); strcpy(fname + sizeof(PCF_FONT_DIR), name); file = FOPEN(fname, "rb"); } if (!file) return NULL; if (!(pf = (MWCOREFONT *) malloc(sizeof(MWCOREFONT)))) { err = -1; goto leave_func; } if (!(pf->cfont = (PMWCFONT) calloc(sizeof(MWCFONT), 1))) { err = -1; goto leave_func; } /* Read the table of contents */ if (pcf_read_toc(file, &toc, &toc_size) == -1) { err = -1; goto leave_func; } for (i=0; i < toc_size; i++) { toc[i].type = dwswap(toc[i].type); toc[i].format = dwswap(toc[i].format); toc[i].size = dwswap(toc[i].size); toc[i].offset = dwswap(toc[i].offset); } /* Now, read in the bitmaps */ glyph_count = pcf_readbitmaps(file, &glyphs, &bsize, &endian, &glyphs_offsets); if (glyph_count == -1) { err = -1; goto leave_func; } if (pcf_read_encoding(file, &encoding) == -1) { err = -1; goto leave_func; } pf->cfont->firstchar = encoding->min_byte2 * (encoding->min_byte1 + 1); /* Read in the metrics */ count = pcf_readmetrics(file, &metrics); /* Calculate the various values */ for (i = 0; i < count; i++) { if (metrics[i].width > max_width) max_width = metrics[i].width; if (metrics[i].ascent > max_ascent) max_ascent = metrics[i].ascent; if (metrics[i].descent > max_descent) max_descent = metrics[i].descent; } max_height = max_ascent + max_descent; pf->cfont->maxwidth = max_width; pf->cfont->height = max_height; pf->cfont->ascent = max_ascent; DPRINTF("glyph_count = %d (%x)\n", glyph_count, glyph_count); /* Allocate enough room to hold all of the files and the offsets */ bwidth = (max_width + 15) / 16; pf->cfont->bits = (MWIMAGEBITS *) calloc((max_height * (sizeof(MWIMAGEBITS) * bwidth)), glyph_count); goffset = (unsigned short *) malloc(glyph_count * sizeof(unsigned short)); gwidth = (unsigned char *) malloc(glyph_count * sizeof(unsigned char)); output = (MWIMAGEBITS *) pf->cfont->bits; offset = 0; for (i = 0; i < glyph_count; i++) { int y = max_height; int h, w, lwidth; unsigned long *ptr = (unsigned long *) (glyphs + glyphs_offsets[i]); lwidth = (metrics[i].width + 15) / 16; gwidth[i] = (unsigned char) metrics[i].width; goffset[i] = offset; offset += (lwidth * max_height); for (h = 0; h < (max_ascent - metrics[i].ascent); h++) { for (w = 0; w < lwidth; w++) *output++ = 0; y--; } for (h = 0; h < (metrics[i].ascent + metrics[i].descent); h++) { unsigned short *val = (unsigned short *) ptr; for (w = 0; w < lwidth; w++) {#if MW_CPU_BIG_ENDIAN if (endian == PCF_LSB_FIRST) word_reverse_swap((unsigned char *) &val[w]); else reverse((unsigned char *)&val[w],sizeof(val[w]));#else if (endian == PCF_MSB_FIRST) word_reverse_swap((unsigned char *) &val[w]);#endif *output++ = val[w]; } ptr += (lwidth + 1) / 2; y--; } for (; y > 0; y--) for (w = 0; w < lwidth; w++) *output++ = 0; } /* reorder offsets and width according to encoding map */ pf->cfont->offset = (unsigned long *) malloc(encoding->count * sizeof(unsigned long)); pf->cfont->width = (unsigned char *) malloc(encoding->count * sizeof(unsigned char)); for (i = 0; i < encoding->count; ++i) { unsigned short n = encoding->map[i]; if (n == 0xffff) /* map non-existent chars to default char */ n = encoding->map[encoding->defaultchar]; pf->cfont->offset[i] = goffset[n]; pf->cfont->width[i] = gwidth[n]; } pf->cfont->size = encoding->count; uc16 = pf->cfont->firstchar > 255 || (pf->cfont->firstchar + pf->cfont->size) > 255; pf->fontprocs = uc16? &pcf_fontprocs16: &pcf_fontprocs; pf->fontsize = pf->fontrotation = pf->fontattr = 0; pf->name = "PCF";leave_func: if (goffset) free(goffset); if (gwidth) free(gwidth); if (encoding) { if (encoding->map) free(encoding->map); free(encoding); } if (metrics) free(metrics); if (glyphs) free(glyphs); if (glyphs_offsets) free(glyphs_offsets); if (toc) free(toc); toc = 0; toc_size = 0; if (file) FCLOSE(file); if (err == 0 && pf) return pf; pcf_unloadfont((PMWFONT)pf); return 0;}voidpcf_unloadfont(PMWFONT font){ PMWCOREFONT pf = (PMWCOREFONT) font; PMWCFONT pfc = pf->cfont; if (pfc) { if (pfc->width) free(pf->cfont->width); if (pfc->offset) free(pf->cfont->offset); if (pfc->bits) free(pf->cfont->bits); free(pf->cfont); } free(font);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -