PfExtentTextToRect()

Calculate the extent of a string, up to a given rectangle

Synopsis:

#include <photon/Pf.h>
int PfExtentTextToRect(PhRect_t *ptsExtent,
                       char *pkszFont,
                       PhRect_t *ptsRect,
                       char const *pkszString,
                       int iLen );

Arguments:

ptsExtent
A pointer to a PhRect_t structure that's used to store the resultant extent.
pkszFont
The font name, as created by PfGenerateFontName().
ptsRect
A pointer to a PhRect_t structure that defines the rectangle that limits the extent.
pkszString
The actual string to extent. The string must be a multibyte string; wchar_t strings are not supported.
iLen
The length, in bytes, of pkszString. If iLen is 0, strlen(pkszString) is assumed.

Library:

ph

Description:

PfExtentTextToRect() extents a string, pkszString, of length iLen, and font pkszFont, up to the bounds specified by ptsRect. The resultant extent, which fits within the bounds of ptsRect, is placed in ptsExtent.

Returns:

The number of characters that will fit within ptsRect, or -1 if an error occurred (errno is set).

Examples:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <Ap.h>
#include <Ph.h>
#include <Pt.h>
#include <errno.h>

PtWidget_t * pwndMain = NULL, * pbtn = NULL, * pobjRaw = NULL;
char pcText[] = "pAfaBfbfffffffffffffffCfcXfxYfyZfzf";
char pcGB[] = "\323\316\317\267";
char szFont[MAX_FONT_TAG];

int fnDrawCanvas( PtWidget_t * ptsWidget,
                  PhTile_t * ptsDamage );

#define BUFFER_SIZE 256

int main (int argc, char *argv[])
{   PtArg_t args[4];
    PhPoint_t win_size, pntPOS, pntDIM;
    short nArgs = 0;
    char * pmbGB = NULL;
    struct PxTransCtrl * ptsTrans = NULL;
    int iTemp1 = 0, iTemp2 = 0;

    if((pmbGB = calloc(BUFFER_SIZE, sizeof(char))) == NULL)
      return(EXIT_FAILURE);

    PtInit (NULL);

    if(argc > 1)
    {  if(PfGenerateFontName(argv[1], 0, 9, szFont) == NULL)
         PfGenerateFontName("TextFont", 0, 9, szFont);
    }
    else
      PfGenerateFontName("TextFont", 0, 9, szFont);

    if((ptsTrans = PxTranslateSet(NULL, "GB2312-80")) == NULL)
      return(EXIT_FAILURE);

    if(PxTranslateToUTF(ptsTrans, pcGB, 4, &iTemp1, pmbGB,
                        BUFFER_SIZE, &iTemp2) == -1)
      printf("Could not translate from GB to UTF.\n");

    if(argc > 2)
      strcpy (pcText, pmbGB);

    // Set base pwndMain parameters.
    win_size.x = 450;
    win_size.y = 450;

    PtSetArg(&args[0],Pt_ARG_DIM, &win_size, 0);

    // window title = name  of program
    PtSetArg(&args[1],Pt_ARG_WINDOW_TITLE,
             "PfExtentTextToRect", 0);

    pwndMain = PtCreateWidget (PtWindow, Pt_NO_PARENT, 2, args);

    nArgs = 0;
    pntPOS.x = 100;
    pntPOS.y = 10;
    PtSetArg(&args[nArgs], Pt_ARG_POS, &pntPOS, 0);
    nArgs++;
    PtSetArg(&args[nArgs], Pt_ARG_TEXT_STRING, pcText, NULL);
    nArgs++;
    PtSetArg(&args[nArgs], Pt_ARG_TEXT_FONT, szFont, NULL);
    nArgs++;
    pbtn = PtCreateWidget(PtButton, pwndMain, nArgs, args);
    PtRealizeWidget(pbtn);

    pntPOS.y = 100;
    pntPOS.x = 75;
    pntDIM.x = 300;
    pntDIM.y = 300;
    PtSetArg(&args[0], Pt_ARG_POS, &pntPOS, 0);
    PtSetArg(&args[1], Pt_ARG_DIM, &pntDIM, 0);
    PtSetArg(&args[2], Pt_ARG_RAW_DRAW_F, fnDrawCanvas, 0L);
    pobjRaw = PtCreateWidget(PtRaw, pwndMain, 3, args);

    PtRealizeWidget(pwndMain);

    PtMainLoop ();

    free(pmbGB);

    return(0);
}


int fnDrawCanvas( PtWidget_t * ptsWidget, PhTile_t * ptsDamage )
{   PhRect_t tsExtentClip;
    PhRect_t rect;
    PhPoint_t pnt;
    PhRect_t tsExtent;
    PgColor_t old;
    PhPoint_t pnt2;
    PhPoint_t tsPos = {0, 0};
    int iRet = 0;
    int iBytes = 0;

    // find our canvas
    PtCalcCanvas(pobjRaw, &rect);

    PtSuperClassDraw( PtBasic, ptsWidget, ptsDamage );

    old = PgSetStrokeColor(Pg_BLACK);

    PfExtentText(&tsExtent, &tsPos, szFont,
                 pcText, strlen(pcText));

    // draw text
    pnt.x = 10 + rect.ul.x;
    pnt.y = 100 + rect.ul.y;

    PgSetFont(szFont);
    PgSetTextColor(Pg_BLACK);
    PgDrawText(pcText, strlen(pcText), &pnt, 0);

    pnt.x -= 10;
    pnt2.x = pnt.x + tsExtent.lr.x + 20;
    pnt2.y = pnt.y;

    PgSetStrokeColor(Pg_BLUE);

    PgDrawLine(&pnt, &pnt2);

    pnt.x = 10 + rect.ul.x;
    pnt.y = 100 + rect.ul.y;

    PgSetStrokeColor(Pg_RED);

    PgDrawIRect(tsExtent.ul.x + pnt.x, tsExtent.ul.y + pnt.y,
                (tsExtent.lr.x - min(tsExtent.ul.x, 0) + 1) +
                pnt.x, tsExtent.lr.y + pnt.y, Pg_DRAW_STROKE);

    if((iRet = PfExtentTextToRect(&tsExtentClip, szFont,
                                  &tsExtent, pcText,
                                  strlen(pcText))) == -1)
      printf("PfExtentTextToRect failed 1.\n");
    else
    {  printf("lrx == %d, %d characters in string.\n",
              tsExtent.lr.x, utf8strlen(pcText, &iBytes));
       printf("PfExtentTextToRect lrx == %d, %d characters will fit\
 in clip of %d.\n", tsExtentClip.lr.x, iRet, tsExtent.lr.x);
    }

    tsExtent.lr.x /= 2;

    if((iRet = PfExtentTextToRect(&tsExtentClip, szFont,
                                  &tsExtent, pcText,
                                  strlen(pcText))) == -1)
      printf("PfExtentTextToRect failed 2.\n");
    else
    {  printf("lrx == %d, %d characters in string.\n",
              tsExtent.lr.x, utf8strlen(pcText, &iBytes));
       printf("PfExtentTextToRect lrx == %d, %d characters will\
 fit in clip of %d.\n", tsExtentClip.lr.x, iRet, tsExtent.lr.x);
    }

    pnt.x = 10 + rect.ul.x;
    pnt.y = 150 + rect.ul.y;

    PgDrawText(pcText, iRet, &pnt, 0);
    PgDrawIRect(tsExtentClip.ul.x + pnt.x,
                tsExtentClip.ul.y + pnt.y,
                (tsExtentClip.lr.x -
                min(tsExtentClip.ul.x, 0) + 1) + pnt.x,
                tsExtentClip.lr.y + pnt.y,
                Pg_DRAW_STROKE);

    tsExtent.lr.x /= 2;

    if((iRet = PfExtentTextToRect(&tsExtentClip, szFont,
                                  &tsExtent, pcText,
                                  strlen(pcText))) == -1)
      printf("PfExtentTextToRect failed 3.\n");
    else
    {  printf("lrx == %d, %d characters in string.\n",
              tsExtent.lr.x, utf8strlen(pcText, &iBytes));
       printf("PfExtentTextToRect lrx == %d, %d characters will\
 fit in clip of %d.\n", tsExtentClip.lr.x, iRet, tsExtent.lr.x);
    }

    pnt.x = 10 + rect.ul.x;
    pnt.y = 200 + rect.ul.y;

    PgDrawText(pcText, iRet, &pnt, 0);
    PgDrawIRect(tsExtentClip.ul.x + pnt.x,
                tsExtentClip.ul.y + pnt.y,
                (tsExtentClip.lr.x -
                min(tsExtentClip.ul.x, 0) + 1) + pnt.x,
                tsExtentClip.lr.y + pnt.y,
                Pg_DRAW_STROKE);

    PgSetStrokeColor(old);

    return( Pt_CONTINUE );
}

Classification:

Photon

Safety:
Interrupt handler No
Signal handler No
Thread No

See also:

PfExtentComponents(), PfExtentFractTextCharPositions(), PfExtent(), PfExtentCx(), PfExtentText(), PfExtentTextCharPositions(), PfExtentWideText(), PfFractionalExtentText(), PfGenerateFontName(), PhRect_t

Fonts chapter of the Photon Programmer's Guide