Leptonica 1.68
C Image Processing Library

bmf.c File Reference

Generation of bitmap fonts. More...

#include "allheaders.h"

Go to the source code of this file.

Defines

#define NFONTS   9
#define DEBUG_BASELINE   0
#define DEBUG_CHARS   0
#define DEBUG_FONT_GEN   0

Functions

static l_int32 pixGetTextBaseline (PIX *pixs, l_int32 *tab8, l_int32 *py)
static l_int32 bmfMakeAsciiTables (L_BMF *bmf)
L_BMFbmfCreate (const char *dir, l_int32 size)
void bmfDestroy (L_BMF **pbmf)
PIXbmfGetPix (L_BMF *bmf, char chr)
l_int32 bmfGetWidth (L_BMF *bmf, char chr, l_int32 *pw)
l_int32 bmfGetBaseline (L_BMF *bmf, char chr, l_int32 *pbaseline)
PIXApixaGetFont (const char *dir, l_int32 size, l_int32 *pbl0, l_int32 *pbl1, l_int32 *pbl2)
l_int32 pixaSaveFont (const char *indir, const char *outdir, l_int32 size)
PIXApixaGenerateFont (const char *dir, l_int32 size, l_int32 *pbl0, l_int32 *pbl1, l_int32 *pbl2)

Variables

static const char * inputfonts []
static const char * outputfonts []
static const l_int32 baselines [NFONTS][3]
static const l_float32 VERT_FRACT_SEP = 0.3

Detailed Description

Generation of bitmap fonts.

 Acquisition and generation of bitmap fonts.

     L_BMF           *bmfCreate()
     L_BMF           *bmfDestroy()

     PIX             *bmfGetPix()
     l_int32          bmfGetWidth()
     l_int32          bmfGetBaseline()

     PIXA            *pixaGetFont()
     l_int32          pixaSaveFont()
     PIXA            *pixaGenerateFont()
     static l_int32   pixGetTextBaseline()
     static l_int32   bmfMakeAsciiTables()

 This is not a very general utility, because it only uses bitmap
 representations of a single font, Palatino-Roman, with the
 normal style.  It uses bitmaps generated for nine sizes, from
 4 to 20 pts, rendered at 300 ppi.  Generalization to different
 fonts, styles and sizes is straightforward.

 I chose Palatino-Roman is because I like it.
 The input font images were generated from a set of small
 PostScript files, such as chars-12.ps, which were rendered
 into the inputfont[] bitmap files using GhostScript.  See, for
 example, the bash script prog/ps2tiff, which will "rip" a
 PostScript file into a set of ccitt-g4 compressed tiff files.

 The set of ascii characters from 32 through 126 are the 95
 printable ascii chars.  Palatino-Roman is missing char 92, '\'.
 I have substituted '/', char 47, for 92, so that there will be
 no missing printable chars in this set.  The space is char 32,
 and I have given it a width equal to twice the width of '!'.

Definition in file bmf.c.


Define Documentation

#define NFONTS   9

Definition at line 57 of file bmf.c.

Referenced by pixaGenerateFont(), and pixaGetFont().

#define DEBUG_BASELINE   0

Definition at line 76 of file bmf.c.

#define DEBUG_CHARS   0

Definition at line 77 of file bmf.c.

#define DEBUG_FONT_GEN   0

Definition at line 78 of file bmf.c.


Function Documentation

static l_int32 pixGetTextBaseline ( PIX pixs,
l_int32 tab8,
l_int32 py 
) [static]

pixGetTextBaseline()

Input: pixs (1 bpp, one textline character set) tab8 (<optional> pixel sum table) &y (<return> baseline value) Return: 0 if OK, 1 on error

Notes: (1) Method: find the largest difference in pixel sums from one raster line to the next one below it. The baseline is the upper raster line for the pair of raster lines that maximizes this function.

Definition at line 564 of file bmf.c.

References ERROR_INT, FREE, L_MAX, makePixelSumTab8(), numaDestroy(), numaGetCount(), numaGetIValue(), pixCountPixelsByRow(), and PROCNAME.

Referenced by pixaGenerateFont().

static l_int32 bmfMakeAsciiTables ( L_BMF bmf) [static]

bmfMakeAsciiTables

Input: bmf Return: 0 if OK, 1 on error

Notes: (1) This makes three tables, each of size 128, as follows:

  • fonttab is a table containing the index of the Pix that corresponds to each input ascii character; it maps (ascii-index) --> Pixa index
  • baselinetab is a table containing the baseline offset for the Pix that corresponds to each input ascii character; it maps (ascii-index) --> baseline offset
  • widthtab is a table containing the character width in pixels for the Pix that corresponds to that character; it maps (ascii-index) --> bitmap width (2) This also computes
  • lineheight (sum of maximum character extensions above and below the baseline)
  • kernwidth (spacing between characters within a word)
  • spacewidth (space between words)
  • vertlinesep (extra vertical spacing between textlines) (3) The baselines apply as follows: baseline1 (ascii 32 - 57), ascii 92 baseline2 (ascii 58 - 91) baseline3 (ascii 93 - 126) (4) The only array in bmf that is not ascii-based is the array of bitmaps in the pixa, which starts at ascii 32.

Definition at line 637 of file bmf.c.

References L_Bmf::baseline1, L_Bmf::baseline2, L_Bmf::baseline3, L_Bmf::baselinetab, bmfGetPix(), bmfGetWidth(), CALLOC, ERROR_INT, L_Bmf::fonttab, L_Bmf::kernwidth, L_MAX, L_Bmf::lineheight, NULL, pixDestroy(), pixGetHeight(), PROCNAME, L_Bmf::spacewidth, UNDEF, VERT_FRACT_SEP, L_Bmf::vertlinesep, and L_Bmf::widthtab.

Referenced by bmfCreate().

L_BMF* bmfCreate ( const char *  dir,
l_int32  size 
)

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.

Definition at line 102 of file bmf.c.

References L_Bmf::baseline1, L_Bmf::baseline2, L_Bmf::baseline3, bmfDestroy(), bmfMakeAsciiTables(), CALLOC, L_Bmf::directory, ERROR_PTR, L_INFO, NULL, L_Bmf::pixa, pixaGenerateFont(), pixaGetFont(), PROCNAME, size, L_Bmf::size, and stringNew().

Referenced by fpixaDisplayQuadtree(), main(), and pixDisplayColorArray().

void bmfDestroy ( L_BMF **  pbmf)

bmfDestroy()

Input: &bmf (<set to="" null>="">) Return: void

Definition at line 143 of file bmf.c.

References L_Bmf::baselinetab, L_Bmf::directory, L_Bmf::fonttab, FREE, L_WARNING, NULL, L_Bmf::pixa, pixaDestroy(), PROCNAME, and L_Bmf::widthtab.

Referenced by bmfCreate(), fpixaDisplayQuadtree(), main(), and pixDisplayColorArray().

PIX* bmfGetPix ( L_BMF bmf,
char  chr 
)

bmfGetPix()

Input: bmf chr (should be one of the 95 supported printable bitmaps) Return: pix (clone of pix in bmf), or null on error

Definition at line 179 of file bmf.c.

References ERROR_PTR, L_Bmf::fonttab, L_CLONE, L_ERROR_INT, NULL, L_Bmf::pixa, pixaGetPix(), PROCNAME, and UNDEF.

Referenced by bmfMakeAsciiTables(), and pixSetTextline().

l_int32 bmfGetWidth ( L_BMF bmf,
char  chr,
l_int32 pw 
)

bmfGetWidth()

Input: bmf chr (should be one of the 95 supported bitmaps) &w (<return> character width; -1 if not printable) Return: 0 if OK, 1 on error

Definition at line 213 of file bmf.c.

References ERROR_INT, L_Bmf::fonttab, L_CLONE, L_ERROR_INT, NULL, L_Bmf::pixa, pixaGetPix(), pixDestroy(), pixGetWidth(), PROCNAME, and UNDEF.

Referenced by bmfGetLineStrings(), bmfGetStringWidth(), bmfMakeAsciiTables(), and pixSetTextblock().

l_int32 bmfGetBaseline ( L_BMF bmf,
char  chr,
l_int32 pbaseline 
)

bmfGetBaseline()

Input: bmf chr (should be one of the 95 supported bitmaps) &baseline (<return>; distance below UL corner of bitmap char) Return: 0 if OK, 1 on error

Definition at line 257 of file bmf.c.

References L_Bmf::baselinetab, ERROR_INT, L_ERROR_INT, PROCNAME, and UNDEF.

Referenced by pixSetTextline().

PIXA* pixaGetFont ( const char *  dir,
l_int32  size,
l_int32 pbl0,
l_int32 pbl1,
l_int32 pbl2 
)

pixaGetFont()

Input: dir (directory holding pixa of character set) size (4, 6, 8, ... , 20) &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) This reads a pre-computed pixa file with the 95 ascii chars.

Definition at line 301 of file bmf.c.

References baselines, ERROR_PTR, FREE, genPathname(), L_WARNING, NFONTS, NULL, outputfonts, pixaRead(), and PROCNAME.

Referenced by bmfCreate(), and main().

l_int32 pixaSaveFont ( const char *  indir,
const char *  outdir,
l_int32  size 
)

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.

Definition at line 347 of file bmf.c.

References ERROR_INT, FREE, genPathname(), NULL, outputfonts, pixaDestroy(), pixaGenerateFont(), pixaGetCount(), pixaWrite(), and PROCNAME.

Referenced by main().

PIXA* pixaGenerateFont ( const char *  dir,
l_int32  size,
l_int32 pbl0,
l_int32 pbl1,
l_int32 pbl2 
)

pixaGenerateFont()

Input: dir (directory holding image of character set) size (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.

Computation of the bitmaps and baselines for a single font takes from 40 to 200 msec on a 2 GHz processor, depending on the size. Use pixaGetFont() to read the generated character set directly from files that were produced in prog/genfonts.c using this function.

Definition at line 405 of file bmf.c.

References boxaDestroy(), boxaGetBox(), boxaGetCount(), boxaRemoveBox(), boxaSort(), boxDestroy(), ERROR_PTR, FREE, genPathname(), Box::h, IFF_PNG, inputfonts, L_CLONE, L_COPY, L_FLIP_PIXELS, L_INFO_INT2, L_INSERT, L_SORT_BY_X, L_SORT_INCREASING, makePixelSumTab8(), NFONTS, NULL, pixaAddPix(), pixaCreate(), pixaDisplayTiled(), pixaGetCount(), pixaGetPix(), pixaReplacePix(), pixClipRectangle(), pixCloseSafeBrick(), pixConnComp(), pixCopy(), pixCreate(), pixDestroy(), pixDisplay(), pixFlipLR(), pixGetHeight(), pixGetTextBaseline(), pixGetWidth(), pixMorphSequence(), pixRead(), pixRenderLine(), pixWrite(), PROCNAME, Box::w, Box::x, and Box::y.

Referenced by bmfCreate(), main(), and pixaSaveFont().


Variable Documentation

const char* inputfonts[] [static]
Initial value:
 {"chars-4.tif", "chars-6.tif",
                                    "chars-8.tif", "chars-10.tif",
                                    "chars-12.tif", "chars-14.tif",
                                    "chars-16.tif", "chars-18.tif",
                                    "chars-20.tif"}

Definition at line 58 of file bmf.c.

Referenced by pixaGenerateFont().

const char* outputfonts[] [static]
Initial value:
 {"chars-4.pixa", "chars-6.pixa",
                                     "chars-8.pixa", "chars-10.pixa",
                                     "chars-12.pixa", "chars-14.pixa",
                                     "chars-16.pixa", "chars-18.pixa",
                                     "chars-20.pixa"}

Definition at line 63 of file bmf.c.

Referenced by pixaGetFont(), and pixaSaveFont().

const l_int32 baselines[NFONTS][3] [static]
Initial value:
 {{11, 12, 12}, {18, 18, 18},
                                             {24, 24, 24}, {30, 30, 30},
                                             {36, 36, 36}, {42, 42, 42},
                                             {48, 48, 48}, {54, 54, 54},
                                             {60, 60, 60}}

Definition at line 68 of file bmf.c.

Referenced by pixaGetFont().

const l_float32 VERT_FRACT_SEP = 0.3 [static]

Definition at line 73 of file bmf.c.

Referenced by bmfMakeAsciiTables().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines