/*! * pixaSaveFont() * * Input: indir (directory holding image of character set) * outdir (directory into which the output pixa file * will be written) * size (in pts, at 300 ppi) * Return: 0 if OK, 1 on error * * Notes: * (1) This saves a font of a particular size. * (2) prog/genfonts calls this function for each of the * nine font sizes, to generate all the font pixa files. */ l_int32 pixaSaveFont(const char *indir, const char *outdir, l_int32 size) { char *pathname; l_int32 bl1, bl2, bl3; PIXA *pixa; PROCNAME("pixaSaveFont"); if (size < 4 || size > 20 || (size % 2)) return ERROR_INT("size must be in {4, 6, ..., 20}", procName, 1); if ((pixa = pixaGenerateFont(indir, size, &bl1, &bl2, &bl3)) == NULL) return ERROR_INT("pixa not made", procName, 1); pathname = genPathname(outdir, outputfonts[(size - 4) / 2]); pixaWrite(pathname, pixa); #if DEBUG_FONT_GEN fprintf(stderr, "Found %d chars in font size %d\n", pixaGetCount(pixa), size); fprintf(stderr, "Baselines are at: %d, %d, %d\n", bl1, bl2, bl3); #endif /* DEBUG_FONT_GEN */ FREE(pathname); pixaDestroy(&pixa); return 0; }
/*! * pixaGenerateFontFromFile() * * Input: dir (directory holding image of character set) * fontsize (4, 6, 8, ... , 20, in pts at 300 ppi) * &bl1 (<return> baseline of row 1) * &bl2 (<return> baseline of row 2) * &bl3 (<return> baseline of row 3) * Return: pixa of font bitmaps for 95 characters, or null on error * * These font generation functions use 9 sets, each with bitmaps * of 94 ascii characters, all in Palatino-Roman font. * Each input bitmap has 3 rows of characters. The range of * ascii values in each row is as follows: * row 0: 32-57 (32 is a space) * row 1: 58-91 (92, '\', is not represented in this font) * row 2: 93-126 * We LR flip the '/' char to generate a bitmap for the missing * '\' character, so that we have representations of all 95 * printable chars. * * Typically, use pixaGetFont() to generate the character bitmaps * in memory for a bmf. This will simply access the bitmap files * in a serialized pixa that were produced in prog/genfonts.c using * this function. */ PIXA * pixaGenerateFontFromFile(const char *dir, l_int32 fontsize, l_int32 *pbl0, l_int32 *pbl1, l_int32 *pbl2) { char *pathname; l_int32 fileno; PIX *pix; PIXA *pixa; PROCNAME("pixaGenerateFontFromFile"); if (!pbl0 || !pbl1 || !pbl2) return (PIXA *)ERROR_PTR("&bl not all defined", procName, NULL); *pbl0 = *pbl1 = *pbl2 = 0; if (!dir) return (PIXA *)ERROR_PTR("dir not defined", procName, NULL); fileno = (fontsize / 2) - 2; if (fileno < 0 || fileno > NUM_FONTS) return (PIXA *)ERROR_PTR("font size invalid", procName, NULL); pathname = genPathname(dir, inputfonts[fileno]); pix = pixRead(pathname); FREE(pathname); if (!pix) return (PIXA *)ERROR_PTR("pix not all defined", procName, NULL); pixa = pixaGenerateFont(pix, fontsize, pbl0, pbl1, pbl2); pixDestroy(&pix); return pixa; }
/*! * bmfCreate() * * Input: dir (directory holding pixa of character set) * size (4, 6, 8, ... , 20) * Return: bmf (holding the bitmap font and associated information) * * Notes: * (1) This first tries to read a pre-computed pixa file with the * 95 ascii chars in it. If the file is not found, it * creates the pixa from the raw image. It then generates all * associated data required to use the bmf. */ L_BMF * bmfCreate(const char *dir, l_int32 size) { L_BMF *bmf; PIXA *pixa; PROCNAME("bmfCreate"); if ((bmf = (L_BMF *)CALLOC(1, sizeof(L_BMF))) == NULL) return (L_BMF *)ERROR_PTR("bmf not made", procName, NULL); /* Look for the pixa */ pixa = pixaGetFont(dir, size, &bmf->baseline1, &bmf->baseline2, &bmf->baseline3); /* If not found, make it */ if (!pixa) { L_INFO("Generating pixa of bitmap fonts", procName); pixa = pixaGenerateFont(dir, size, &bmf->baseline1, &bmf->baseline2, &bmf->baseline3); if (!pixa) { bmfDestroy(&bmf); return (L_BMF *)ERROR_PTR("font pixa not made", procName, NULL); } } bmf->pixa = pixa; bmf->size = size; bmf->directory = stringNew(dir); bmfMakeAsciiTables(bmf); return bmf; }
/*! * pixaGenerateFontFromString() * * Input: fontsize (4, 6, 8, ... , 20, in pts at 300 ppi) * &bl1 (<return> baseline of row 1) * &bl2 (<return> baseline of row 2) * &bl3 (<return> baseline of row 3) * Return: pixa of font bitmaps for 95 characters, or null on error * * Notes: * (1) See pixaGenerateFontFromFile() for details. */ PIXA * pixaGenerateFontFromString(l_int32 fontsize, l_int32 *pbl0, l_int32 *pbl1, l_int32 *pbl2) { l_uint8 *data; l_int32 redsize, nbytes; PIX *pix; PIXA *pixa; PROCNAME("pixaGenerateFontFromString"); if (!pbl0 || !pbl1 || !pbl2) return (PIXA *)ERROR_PTR("&bl not all defined", procName, NULL); *pbl0 = *pbl1 = *pbl2 = 0; redsize = (fontsize / 2) - 2; if (redsize < 0 || redsize > NUM_FONTS) return (PIXA *)ERROR_PTR("invalid font size", procName, NULL); if (fontsize == 4) { data = decodeBase64(fontdata_4, strlen(fontdata_4), &nbytes); } else if (fontsize == 6) { data = decodeBase64(fontdata_6, strlen(fontdata_6), &nbytes); } else if (fontsize == 8) { data = decodeBase64(fontdata_8, strlen(fontdata_8), &nbytes); } else if (fontsize == 10) { data = decodeBase64(fontdata_10, strlen(fontdata_10), &nbytes); } else if (fontsize == 12) { data = decodeBase64(fontdata_12, strlen(fontdata_12), &nbytes); } else if (fontsize == 14) { data = decodeBase64(fontdata_14, strlen(fontdata_14), &nbytes); } else if (fontsize == 16) { data = decodeBase64(fontdata_16, strlen(fontdata_16), &nbytes); } else if (fontsize == 18) { data = decodeBase64(fontdata_18, strlen(fontdata_18), &nbytes); } else { /* fontsize == 20 */ data = decodeBase64(fontdata_20, strlen(fontdata_20), &nbytes); } if (!data) return (PIXA *)ERROR_PTR("data not made", procName, NULL); pix = pixReadMem(data, nbytes); FREE(data); if (!pix) return (PIXA *)ERROR_PTR("pix not made", procName, NULL); pixa = pixaGenerateFont(pix, fontsize, pbl0, pbl1, pbl2); pixDestroy(&pix); return pixa; }