Leptonica 1.54
Файл src/bmf.c
#include "allheaders.h"

Макросы

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

Функции

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)

Переменные

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

Макросы

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

Функции

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.

void bmfDestroy ( L_BMF **  pbmf)

bmfDestroy()

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

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

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

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

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.
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.

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.

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.

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.


Переменные

const l_int32 baselines[NFONTS][3] [static]
Инициализатор
 {{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}}
const char* inputfonts[] [static]
Инициализатор
 {"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"}
const char* outputfonts[] [static]
Инициализатор
 {"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"}
const l_float32 VERT_FRACT_SEP = 0.3 [static]