src/textops.c
SARRAY *
bmfGetLineStrings(L_BMF       *bmf,
                  const char  *textstr,
                  l_int32      maxw,
                  l_int32      firstindent,
                  l_int32     *ph)
{
char    *linestr;
l_int32  i, ifirst, sumw, newsum, w, nwords, nlines, len, xwidth;
NUMA    *na;
SARRAY  *sa, *sawords;

    PROCNAME("bmfGetLineStrings");

    if (!bmf)
        return (SARRAY *)ERROR_PTR("bmf not defined", procName, NULL);
    if (!textstr)
        return (SARRAY *)ERROR_PTR("teststr not defined", procName, NULL);

    if ((sawords = sarrayCreateWordsFromString(textstr)) == NULL)
        return (SARRAY *)ERROR_PTR("sawords not made", procName, NULL);

    if ((na = bmfGetWordWidths(bmf, textstr, sawords)) == NULL)
        return (SARRAY *)ERROR_PTR("na not made", procName, NULL);
    nwords = numaGetCount(na);
    if (nwords == 0)
        return (SARRAY *)ERROR_PTR("no words in textstr", procName, NULL);
    bmfGetWidth(bmf, 'x', &xwidth);

    if ((sa = sarrayCreate(0)) == NULL)
        return (SARRAY *)ERROR_PTR("sa not made", procName, NULL);

    ifirst = 0;
    numaGetIValue(na, 0, &w);
    sumw = firstindent * xwidth + w;
    for (i = 1; i < nwords; i++) {
        numaGetIValue(na, i, &w);
        newsum = sumw + bmf->spacewidth + w;
        if (newsum > maxw) {
            linestr = sarrayToStringRange(sawords, ifirst, i - ifirst, 2);
            if (!linestr)
                continue;
            len = strlen(linestr);
            if (len > 0)  /* it should always be */
                linestr[len - 1] = '\0';  /* remove the last space */
            sarrayAddString(sa, linestr, 0);
            ifirst = i;
            sumw = w;
        }
        else
            sumw += bmf->spacewidth + w;
    }
    linestr = sarrayToStringRange(sawords, ifirst, nwords - ifirst, 2);
    if (linestr)
        sarrayAddString(sa, linestr, 0);
    nlines = sarrayGetCount(sa);
    *ph = nlines * bmf->lineheight + (nlines - 1) * bmf->vertlinesep;

    sarrayDestroy(&sawords);
    numaDestroy(&na);
    return sa;
}
/*!
 *  bmfGetLineStrings()
 *
 *      Input:  bmf
 *              textstr
 *              maxw (max width of a text line in pixels)
 *              firstindent (indentation of first line, in x-widths)
 *              &h (<return> height required to hold text bitmap)
 *      Return: sarray of text strings for each line, or null on error
 *
 *  Notes:
 *      (1) Divides the input text string into an array of text strings,
 *          each of which will fit within maxw bits of width.
 */
bmfGetLineStrings()
Input:  bmf
textstr
maxw (max width of a text line in pixels)
firstindent (indentation of first line, in x-widths)
&h ( height required to hold text bitmap)
Return: sarray of text strings for each line, or null on error

(1) Divides the input text string into an array of text strings,
each of which will fit within maxw bits of width.
bmfGetLineStrings () Input : bmf textstr maxw ( max width of a text line in pixels ) firstindent ( indentation of first line in x - widths ) &h ( height required to hold text bitmap ) Return : sarray of text strings for each line or null on error ( 1 ) Divides the input text string into an array of text strings each of which will fit within maxw bits of width .
SARRAY *
bmfGetLineStrings(L_BMF       *bmf,
                  const char  *textstr,
                  l_int32      maxw,
                  l_int32      firstindent,
                  l_int32     *ph)
{
char    *linestr;
l_int32  i, ifirst, sumw, newsum, w, nwords, nlines, len, xwidth;
NUMA    *na;
SARRAY  *sa, *sawords;
    PROCNAME("bmfGetLineStrings");
    if (!bmf)
        return (SARRAY *)ERROR_PTR("bmf not defined", procName, NULL);
    if (!textstr)
        return (SARRAY *)ERROR_PTR("teststr not defined", procName, NULL);
    if ((sawords = sarrayCreateWordsFromString(textstr)) == NULL)
        return (SARRAY *)ERROR_PTR("sawords not made", procName, NULL);
    if ((na = bmfGetWordWidths(bmf, textstr, sawords)) == NULL)
        return (SARRAY *)ERROR_PTR("na not made", procName, NULL);
    nwords = numaGetCount(na);
    if (nwords == 0)
        return (SARRAY *)ERROR_PTR("no words in textstr", procName, NULL);
    bmfGetWidth(bmf, 'x', &xwidth);
    if ((sa = sarrayCreate(0)) == NULL)
        return (SARRAY *)ERROR_PTR("sa not made", procName, NULL);
    ifirst = 0;
    numaGetIValue(na, 0, &w);
    sumw = firstindent * xwidth + w;
    for (i = 1; i < nwords; i++) {
        numaGetIValue(na, i, &w);
        newsum = sumw + bmf->spacewidth + w;
        if (newsum > maxw) {
            linestr = sarrayToStringRange(sawords, ifirst, i - ifirst, 2);
            if (!linestr)
                continue;
            len = strlen(linestr);
            if (len > 0)  
                linestr[len - 1] = '\0';  
            sarrayAddString(sa, linestr, 0);
            ifirst = i;
            sumw = w;
        }
        else
            sumw += bmf->spacewidth + w;
    }
    linestr = sarrayToStringRange(sawords, ifirst, nwords - ifirst, 2);
    if (linestr)
        sarrayAddString(sa, linestr, 0);
    nlines = sarrayGetCount(sa);
    *ph = nlines * bmf->lineheight + (nlines - 1) * bmf->vertlinesep;
    sarrayDestroy(&sawords);
    numaDestroy(&na);
    return sa;
}
SARRAY * bmfGetLineStrings ( L_BMF * bmf , const char * textstr , l_int32 maxw , l_int32 firstindent , l_int32 * ph ) { char * linestr ; l_int32 i , ifirst , sumw , newsum , w , nwords , nlines , len , xwidth ; NUMA * na ; SARRAY * sa , * sawords ; PROCNAME ( " " ) ; if ( ! bmf ) return ( SARRAY * ) ERROR_PTR ( " " , procName , NULL ) ; if ( ! textstr ) return ( SARRAY * ) ERROR_PTR ( " " , procName , NULL ) ; if ( ( sawords = sarrayCreateWordsFromString ( textstr ) ) == NULL ) return ( SARRAY * ) ERROR_PTR ( " " , procName , NULL ) ; if ( ( na = bmfGetWordWidths ( bmf , textstr , sawords ) ) == NULL ) return ( SARRAY * ) ERROR_PTR ( " " , procName , NULL ) ; nwords = numaGetCount ( na ) ; if ( nwords == 0 ) return ( SARRAY * ) ERROR_PTR ( " " , procName , NULL ) ; bmfGetWidth ( bmf , ' ' , & xwidth ) ; if ( ( sa = sarrayCreate ( 0 ) ) == NULL ) return ( SARRAY * ) ERROR_PTR ( " " , procName , NULL ) ; ifirst = 0 ; numaGetIValue ( na , 0 , & w ) ; sumw = firstindent * xwidth + w ; for ( i = 1 ; i < nwords ; i ++ ) { numaGetIValue ( na , i , & w ) ; newsum = sumw + bmf -> spacewidth + w ; if ( newsum > maxw ) { linestr = sarrayToStringRange ( sawords , ifirst , i - ifirst , 2 ) ; if ( ! linestr ) continue ; len = strlen ( linestr ) ; if ( len > 0 ) linestr [ len - 1 ] = ' \0 ' ; sarrayAddString ( sa , linestr , 0 ) ; ifirst = i ; sumw = w ; } else sumw += bmf -> spacewidth + w ; } linestr = sarrayToStringRange ( sawords , ifirst , nwords - ifirst , 2 ) ; if ( linestr ) sarrayAddString ( sa , linestr , 0 ) ; nlines = sarrayGetCount ( sa ) ; * ph = nlines * bmf -> lineheight + ( nlines - 1 ) * bmf -> vertlinesep ; sarrayDestroy ( & sawords ) ; numaDestroy ( & na ) ; return sa ; }
bmfGetLineStrings()
      Input:  bmf
              textstr
              maxw (max width of a text line in pixels)
              firstindent (indentation of first line, in x-widths)
              &h (<return> height required to hold text bitmap)
      Return: sarray of text strings for each line, or null on error
bmfGetLineStrings () Input : bmf textstr maxw ( max width of a text line in pixels ) firstindent ( indentation of first line in x - widths ) &h ( <return > height required to hold text bitmap ) Return : sarray of text strings for each line or null on error
/* it should always be */ /* remove the last space */