src/textops.c
l_int32
pixSetTextline(PIX         *pixs,
               L_BMF       *bmf,
               const char  *textstr,
               l_uint32     val,
               l_int32      x0,
               l_int32      y0,
               l_int32     *pwidth,
               l_int32     *poverflow)
{
char      chr; 
l_int32   d, i, x, w, nchar, baseline, index, rval, gval, bval;
l_uint32  textcolor;
PIX      *pix;
PIXCMAP  *cmap;

    PROCNAME("pixSetTextline");

    if (!pixs)
        return ERROR_INT("pixs not defined", procName, 1);
    if (!bmf)
        return ERROR_INT("bmf not defined", procName, 1);
    if (!textstr)
        return ERROR_INT("teststr not defined", procName, 1);
    if (val < 0) {
        L_WARNING("val must be non-negative; setting to 0", procName);
        val = 0;
    }

    d = pixGetDepth(pixs);
    cmap = pixGetColormap(pixs);
    if (d == 1 && val > 1)
        val = 1;
    else if (d == 2 && val > 3 && !cmap)
        val = 2;
    else if (d == 4 && val > 15 && !cmap)
        val = 8;
    else if (d == 8 && val > 0xff && !cmap)
        val = 128;
    else if (d == 16 && val > 0xffff)
        val = 0x8000;
    else if (d == 32 && val < 256)
        val = 0x80808000;

        /* If cmapped, add the color if necessary to the cmap.  If the
         * cmap is full, use the nearest color to the requested color. */
    if (cmap) {
        extractRGBValues(val, &rval, &gval, &bval);
        pixcmapAddNearestColor(cmap, rval, gval, bval, &index);
        pixcmapGetColor(cmap, index, &rval, &gval, &bval);
        composeRGBPixel(rval, gval, bval, &textcolor);
    } else
        textcolor = val;

    nchar = strlen(textstr);
    x = x0;
    for (i = 0; i < nchar; i++) {
        chr = textstr[i];
        if ((l_int32)chr == 10) continue;  /* NL */
        pix = bmfGetPix(bmf, chr);
        bmfGetBaseline(bmf, chr, &baseline);
        pixPaintThroughMask(pixs, pix, x, y0 - baseline, textcolor);
        w = pixGetWidth(pix);
        x += w + bmf->kernwidth;
        pixDestroy(&pix);
    }

    if (pwidth)
        *pwidth = x - bmf->kernwidth - x0;
    if (poverflow)
        *poverflow = (x > pixGetWidth(pixs) - 1) ? 1 : 0;
    return 0;
}
/*!
 *  pixSetTextline()
 *
 *      Input:  pixs (input image)
 *              bmf (bitmap font data)
 *              textstr (text string to be set on the line)
 *              val (color to set the text)
 *              x0 (left edge for first char)
 *              y0 (baseline location for all text on line)
 *              &width (<optional return> width of generated text)
 *              &overflow (<optional return> 0 if text is contained in
 *                         input pix; 1 if it is clipped)
 *      Return: 0 if OK, 1 on error
 *
 *  Notes:
 *      (1) This function paints a line of text over an image.
 *      (2) @val is the pixel value to be painted through the font mask.
 *          It should be chosen to agree with the depth of pixs.
 *          If it is out of bounds, an intermediate value is chosen.
 *          For RGB, use hex notation: 0xRRGGBB00, where RR is the
 *          hex representation of the red intensity, etc.
 *          The last two hex digits are 00 (byte value 0), assigned to
 *          the A component.  Note that, as usual, RGBA proceeds from 
 *          left to right in the order from MSB to LSB (see pix.h
 *          for details).
 *      (3) If there is a colormap, this does the best it can to use
 *          the requested color, or something similar to it.
 */


(1) This function paints a line of text over an image.
(2) @val is the pixel value to be painted through the font mask.
It should be chosen to agree with the depth of pixs.
If it is out of bounds, an intermediate value is chosen.
For RGB, use hex notation: 0xRRGGBB00, where RR is the
hex representation of the red intensity, etc.
The last two hex digits are 00 (byte value 0), assigned to
the A component.  Note that, as usual, RGBA proceeds from
left to right in the order from MSB to LSB (see pix.h
for details).
(3) If there is a colormap, this does the best it can to use
the requested color, or something similar to it.
( 1 ) This function paints a line of text over an image . ( 2 ) @val is the pixel value to be painted through the font mask . It should be chosen to agree with the depth of pixs . If it is out of bounds an intermediate value is chosen . For RGB use hex notation : 0xRRGGBB00 where RR is the hex representation of the red intensity etc . The last two hex digits are 00 ( byte value 0 ) assigned to the A component . Note that as usual RGBA proceeds from left to right in the order from MSB to LSB ( see pix . h for details ) . ( 3 ) If there is a colormap this does the best it can to use the requested color or something similar to it .
l_int32
pixSetTextline(PIX         *pixs,
               L_BMF       *bmf,
               const char  *textstr,
               l_uint32     val,
               l_int32      x0,
               l_int32      y0,
               l_int32     *pwidth,
               l_int32     *poverflow)
{
char      chr; 
l_int32   d, i, x, w, nchar, baseline, index, rval, gval, bval;
l_uint32  textcolor;
PIX      *pix;
PIXCMAP  *cmap;
    PROCNAME("pixSetTextline");
    if (!pixs)
        return ERROR_INT("pixs not defined", procName, 1);
    if (!bmf)
        return ERROR_INT("bmf not defined", procName, 1);
    if (!textstr)
        return ERROR_INT("teststr not defined", procName, 1);
    if (val < 0) {
        L_WARNING("val must be non-negative; setting to 0", procName);
        val = 0;
    }
    d = pixGetDepth(pixs);
    cmap = pixGetColormap(pixs);
    if (d == 1 && val > 1)
        val = 1;
    else if (d == 2 && val > 3 && !cmap)
        val = 2;
    else if (d == 4 && val > 15 && !cmap)
        val = 8;
    else if (d == 8 && val > 0xff && !cmap)
        val = 128;
    else if (d == 16 && val > 0xffff)
        val = 0x8000;
    else if (d == 32 && val < 256)
        val = 0x80808000;
    if (cmap) {
        extractRGBValues(val, &rval, &gval, &bval);
        pixcmapAddNearestColor(cmap, rval, gval, bval, &index);
        pixcmapGetColor(cmap, index, &rval, &gval, &bval);
        composeRGBPixel(rval, gval, bval, &textcolor);
    } else
        textcolor = val;
    nchar = strlen(textstr);
    x = x0;
    for (i = 0; i < nchar; i++) {
        chr = textstr[i];
        if ((l_int32)chr == 10) continue;  
        pix = bmfGetPix(bmf, chr);
        bmfGetBaseline(bmf, chr, &baseline);
        pixPaintThroughMask(pixs, pix, x, y0 - baseline, textcolor);
        w = pixGetWidth(pix);
        x += w + bmf->kernwidth;
        pixDestroy(&pix);
    }
    if (pwidth)
        *pwidth = x - bmf->kernwidth - x0;
    if (poverflow)
        *poverflow = (x > pixGetWidth(pixs) - 1) ? 1 : 0;
    return 0;
}
l_int32 pixSetTextline ( PIX * pixs , L_BMF * bmf , const char * textstr , l_uint32 val , l_int32 x0 , l_int32 y0 , l_int32 * pwidth , l_int32 * poverflow ) { char chr ; l_int32 d , i , x , w , nchar , baseline , index , rval , gval , bval ; l_uint32 textcolor ; PIX * pix ; PIXCMAP * cmap ; PROCNAME ( " " ) ; if ( ! pixs ) return ERROR_INT ( " " , procName , 1 ) ; if ( ! bmf ) return ERROR_INT ( " " , procName , 1 ) ; if ( ! textstr ) return ERROR_INT ( " " , procName , 1 ) ; if ( val < 0 ) { L_WARNING ( " " , procName ) ; val = 0 ; } d = pixGetDepth ( pixs ) ; cmap = pixGetColormap ( pixs ) ; if ( d == 1 && val > 1 ) val = 1 ; else if ( d == 2 && val > 3 && ! cmap ) val = 2 ; else if ( d == 4 && val > 15 && ! cmap ) val = 8 ; else if ( d == 8 && val > 0xff && ! cmap ) val = 128 ; else if ( d == 16 && val > 0xffff ) val = 0x8000 ; else if ( d == 32 && val < 256 ) val = 0x80808000 ; if ( cmap ) { extractRGBValues ( val , & rval , & gval , & bval ) ; pixcmapAddNearestColor ( cmap , rval , gval , bval , & index ) ; pixcmapGetColor ( cmap , index , & rval , & gval , & bval ) ; composeRGBPixel ( rval , gval , bval , & textcolor ) ; } else textcolor = val ; nchar = strlen ( textstr ) ; x = x0 ; for ( i = 0 ; i < nchar ; i ++ ) { chr = textstr [ i ] ; if ( ( l_int32 ) chr == 10 ) continue ; pix = bmfGetPix ( bmf , chr ) ; bmfGetBaseline ( bmf , chr , & baseline ) ; pixPaintThroughMask ( pixs , pix , x , y0 - baseline , textcolor ) ; w = pixGetWidth ( pix ) ; x += w + bmf -> kernwidth ; pixDestroy ( & pix ) ; } if ( pwidth ) * pwidth = x - bmf -> kernwidth - x0 ; if ( poverflow ) * poverflow = ( x > pixGetWidth ( pixs ) - 1 ) ? 1 : 0 ; return 0 ; }
pixSetTextline()
      Input:  pixs (input image)
              bmf (bitmap font data)
              textstr (text string to be set on the line)
              val (color to set the text)
              x0 (left edge for first char)
              y0 (baseline location for all text on line)
              &width (<optional return> width of generated text)
              &overflow (<optional return> 0 if text is contained in
                         input pix; 1 if it is clipped)
      Return: 0 if OK, 1 on error
pixSetTextline () Input : pixs ( input image ) bmf ( bitmap font data ) textstr ( text string to be set on the line ) val ( color to set the text ) x0 ( left edge for first char ) y0 ( baseline location for all text on line ) &width ( <optional return > width of generated text ) &overflow ( <optional return > 0 if text is contained in input pix ; 1 if it is clipped ) Return : 0 if OK 1 on error
/* If cmapped, add the color if necessary to the cmap.  If the
         * cmap is full, use the nearest color to the requested color. */ /* NL */