Warning: main(/www/www/htdocs/style/globals.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/photon/lib_ref/pf/pftextwidthbytes.html on line 1

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/globals.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/photon/lib_ref/pf/pftextwidthbytes.html on line 1

Warning: main(/www/www/htdocs/style/header.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/photon/lib_ref/pf/pftextwidthbytes.html on line 8

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/header.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/photon/lib_ref/pf/pftextwidthbytes.html on line 8

PfTextWidthBytes()

Calculate the width of a char string of multibyte UTF-8 characters

Synopsis:

#include <photon/Pf.h>
int PfTextWidthBytes( const char *font,
                      const char *str,
                      int len );

Arguments:

font
The name of the desired font. You should use PfGenerateFontName() to create this name.
str
A char string of multibyte UTF-8 characters.
len
The length of the string, in bytes. If len is 0, strlen(str) is assumed.

Library:

ph

Description:

PfTextWidthBytes() is a convenience function that calculates the width of the given string in the given font, using the formula:

extent.lr.x - min(extent.ul.x, 0) + 1

PfTextWidthChars() is similar, but you give it the number of characters in the string rather than the number of bytes.

Returns:

The width of the string, or 0 if an error occurred.

Examples:

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

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

int main(int argc, char *argv[])
{   PtArg_t args[8];
    PhPoint_t win_size, pntPOS, pntDIM;
    short nArgs = 0;
    PtWidget_t * pwndMain = NULL, * pobjRaw = NULL;

    PtInit (NULL);

    //  set base pwndMain parms
    win_size.x = 450;
    win_size.y = 600;

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

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

    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);

    (void) PtRealizeWidget(pwndMain);

    PtMainLoop ();

    return(0);
}

int fnDrawCanvas( PtWidget_t * ptsWidget, PhTile_t * ptsDamage )
{   PhRect_t rect;
    PhPoint_t tsPos = {0, 0};
    PgColor_t old;
    int iLen = 0;
    char szHelvetica12[MAX_FONT_TAG];

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

    PtSuperClassDraw(PtBasic, ptsWidget, ptsDamage);

    old = PgSetStrokeColor(Pg_BLACK);

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

    PgSetFont(PfGenerateFontName("Helvetica", 0, 12,
              szHelvetica12));
    PgSetTextColor(Pg_BLACK);
    PgDrawText("Hello", 5, &tsPos, 0);

    if((iLen = PfTextWidthBytes(szHelvetica12,
                                "Hello", 0)) == 0)
      return(Pt_CONTINUE);

    PgDrawILine(tsPos.x, tsPos.y, tsPos.x + iLen, tsPos.y);

    PgSetStrokeColor(old);

    return( Pt_CONTINUE );
}

Classification:

Photon

Safety:
Interrupt handler No
Signal handler No
Thread No

See also:

PfGenerateFontName(), PfTextWidthChars()

Fonts chapter of the Photon Programmer's Guide


Warning: main(/www/www/htdocs/style/footer.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/photon/lib_ref/pf/pftextwidthbytes.html on line 211

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/footer.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/photon/lib_ref/pf/pftextwidthbytes.html on line 211