Leptonica  1.82.0
Image processing and image analysis suite
bmf.c File Reference
#include <string.h>
#include "allheaders.h"
#include "bmfdata.h"

Go to the source code of this file.

Macros

#define DEBUG_BASELINE   0
 
#define DEBUG_CHARS   0
 
#define DEBUG_FONT_GEN   0
 

Functions

static PIXApixaGenerateFontFromFile (const char *dir, l_int32 fontsize, l_int32 *pbl0, l_int32 *pbl1, l_int32 *pbl2)
 
static PIXApixaGenerateFontFromString (l_int32 fontsize, l_int32 *pbl0, l_int32 *pbl1, l_int32 *pbl2)
 
static PIXApixaGenerateFont (PIX *pixs, l_int32 fontsize, l_int32 *pbl0, l_int32 *pbl1, l_int32 *pbl2)
 
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 fontsize)
 
void bmfDestroy (L_BMF **pbmf)
 
PIXbmfGetPix (L_BMF *bmf, char chr)
 
l_ok bmfGetWidth (L_BMF *bmf, char chr, l_int32 *pw)
 
l_ok bmfGetBaseline (L_BMF *bmf, char chr, l_int32 *pbaseline)
 
PIXApixaGetFont (const char *dir, l_int32 fontsize, l_int32 *pbl0, l_int32 *pbl1, l_int32 *pbl2)
 
l_ok pixaSaveFont (const char *indir, const char *outdir, l_int32 fontsize)
 

Variables

static const l_float32 VertFractSep = 0.3
 

Detailed Description


  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()
      static PIXA     *pixaGenerateFontFromFile()
      static PIXA     *pixaGenerateFontFromString()
      static 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 an LR flip of '/', char 47, for 92, so that
  there are 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.

Function Documentation

◆ bmfCreate()

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

bmfCreate()

Parameters
[in]dir[optional] directory holding pixa of character set
[in]fontsize4, 6, 8, ... , 20
Returns
bmf holding the bitmap font and associated information
Notes:
     (1) If dir == null, this generates the font bitmaps from a
         compiled string.
     (2) Otherwise, this tries to read a pre-computed pixa file with the
         95 ascii chars in it.  If the file is not found, it then
         attempts to generate the pixa and associated baseline
         data from a tiff image containing all the characters.  If
         that fails, it uses the compiled string.

Definition at line 117 of file bmf.c.

References L_Bmf::baseline1, L_Bmf::baseline2, L_Bmf::baseline3, bmfDestroy(), bmfMakeAsciiTables(), L_Bmf::directory, L_Bmf::pixa, pixaGenerateFontFromFile(), pixaGenerateFontFromString(), pixaGetFont(), L_Bmf::size, and stringNew().

Referenced by boxaDisplayTiled(), compareTilesByHisto(), dewarpaShowArrays(), dewarpShowResults(), fpixaDisplayQuadtree(), partifyPixac(), pixaAddPixWithText(), pixaConvertToNUpPixa(), pixaDisplayTiledByIndex(), pixaDisplayTiledWithText(), pixaSelectToPdf(), pixCompareTilesByHisto(), pixDisplayColorArray(), recogShowPath(), and showExtractNumbers().

◆ bmfDestroy()

◆ bmfGetBaseline()

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

bmfGetBaseline()

Parameters
[in]bmf
[in]chrshould be one of the 95 supported bitmaps
[out]pbaselinedistance below UL corner of bitmap char
Returns
0 if OK, 1 on error

Definition at line 278 of file bmf.c.

References L_Bmf::baselinetab.

◆ bmfGetPix()

PIX* bmfGetPix ( L_BMF bmf,
char  chr 
)

bmfGetPix()

Parameters
[in]bmf
[in]chrshould be one of the 95 supported printable bitmaps
Returns
pix clone of pix in bmf, or NULL on error

Definition at line 204 of file bmf.c.

References L_Bmf::fonttab, L_CLONE, L_Bmf::pixa, and pixaGetPix().

Referenced by bmfMakeAsciiTables().

◆ bmfGetWidth()

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

bmfGetWidth()

Parameters
[in]bmf
[in]chrshould be one of the 95 supported bitmaps
[out]pwcharacter width; -1 if not printable
Returns
0 if OK, 1 on error

Definition at line 239 of file bmf.c.

References L_Bmf::fonttab, L_Bmf::pixa, and pixaGetPixDimensions().

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

◆ bmfMakeAsciiTables()

static l_int32 bmfMakeAsciiTables ( L_BMF bmf)
static

bmfMakeAsciiTables

Parameters
[in]bmf
Returns
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 804 of file bmf.c.

References L_Bmf::baseline1, L_Bmf::baseline2, L_Bmf::baseline3, L_Bmf::baselinetab, bmfGetPix(), bmfGetWidth(), L_Bmf::fonttab, and L_Bmf::widthtab.

Referenced by bmfCreate().

◆ pixaGenerateFont()

static PIXA * pixaGenerateFont ( PIX pixs,
l_int32  fontsize,
l_int32 *  pbl0,
l_int32 *  pbl1,
l_int32 *  pbl2 
)
static

pixaGenerateFont()

Parameters
[in]pixsof 95 characters in 3 rows
[in]fontsize4, 6, 8, ... , 20, in pts at 300 ppi
[out]pbl0baseline of row 1
[out]pbl1baseline of row 2
[out]pbl2baseline of row 3
Returns
pixa of font bitmaps for 95 characters, or NULL on error
Notes:
     (1) This does all the work.  See pixaGenerateFontFromFile()
         for an overview.
     (2) The pix is for one of the 9 fonts.  fontsize is only
         used here for debugging.

Definition at line 558 of file bmf.c.

◆ pixaGenerateFontFromFile()

static PIXA * pixaGenerateFontFromFile ( const char *  dir,
l_int32  fontsize,
l_int32 *  pbl0,
l_int32 *  pbl1,
l_int32 *  pbl2 
)
static

pixaGenerateFontFromFile()

Parameters
[in]dirdirectory holding image of character set
[in]fontsize4, 6, 8, ... , 20, in pts at 300 ppi
[out]pbl0baseline of row 1
[out]pbl1baseline of row 2
[out]pbl2baseline of row 3
Returns
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.

Definition at line 436 of file bmf.c.

Referenced by bmfCreate(), and pixaSaveFont().

◆ pixaGenerateFontFromString()

static PIXA * pixaGenerateFontFromString ( l_int32  fontsize,
l_int32 *  pbl0,
l_int32 *  pbl1,
l_int32 *  pbl2 
)
static

pixaGenerateFontFromString()

Parameters
[in]fontsize4, 6, 8, ... , 20, in pts at 300 ppi
[out]pbl0baseline of row 1
[out]pbl1baseline of row 2
[out]pbl2baseline of row 3
Returns
pixa of font bitmaps for 95 characters, or NULL on error
Notes:
     (1) See pixaGenerateFontFromFile() for details.

Definition at line 487 of file bmf.c.

Referenced by bmfCreate(), and pixaSaveFont().

◆ pixaGetFont()

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

pixaGetFont()

Parameters
[in]dirdirectory holding pixa of character set
[in]fontsize4, 6, 8, ... , 20
[out]pbl0baseline of row 1
[out]pbl1baseline of row 2
[out]pbl2baseline of row 3
Returns
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 324 of file bmf.c.

Referenced by bmfCreate().

◆ pixaSaveFont()

l_ok pixaSaveFont ( const char *  indir,
const char *  outdir,
l_int32  fontsize 
)

pixaSaveFont()

Parameters
[in]indir[optional] directory holding image of character set
[in]outdirdirectory into which the output pixa file will be written
[in]fontsizein pts, at 300 ppi
Returns
0 if OK, 1 on error
Notes:
     (1) This saves a font of a particular size.
     (2) If indir == null, this generates the font bitmaps from a
         compiled string.
     (3) prog/genfonts calls this function for each of the
         nine font sizes, to generate all the font pixa files.

Definition at line 374 of file bmf.c.

References pathJoin(), pixaGenerateFontFromFile(), and pixaGenerateFontFromString().

◆ pixGetTextBaseline()

static l_int32 pixGetTextBaseline ( PIX pixs,
l_int32 *  tab8,
l_int32 *  py 
)
static

pixGetTextBaseline()

Parameters
[in]pixs1 bpp, one textline character set
[in]tab8[optional] pixel sum table
[out]pybaseline value
Returns
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 729 of file bmf.c.

References makePixelSumTab8(), numaDestroy(), numaGetCount(), numaGetIValue(), and pixCountPixelsByRow().