![]() |
![]() |
![]() |
![]() |
Calculate the width of a char string of multibyte UTF-8 characters
int PfTextWidthBytes( const char *font, const char *str, int len );
ph
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
The font argument is the name of the desired font. You should use PfGenerateFontName() to create this.
The str argument is a char string of multibyte UTF-8 characters. The len argument gives the length of the string, in bytes. If len is 0, strlen(str) is assumed.
PfTextWidthChars() is similar, but you give it the number of characters in the string rather than the number of bytes.
The width of the string, or 0 if an error occurred.
#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); 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 ); }
Photon
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | No |
PfGenerateFontName(), PfTextWidthChars()
Fonts chapter of the Photon Programmer's Guide
![]() |
![]() |
![]() |
![]() |