Lee_Robert_Willis@cup.portal.com (10/14/90)
Does anyone know how to find the width of a single character within a proportional font, without using 'TextLength()'? (The font/character pair may not be the current font of the RastPort.) Using the RKM section on Text (1.3 Chapter 24), I constructed the following function. It checks if the font is proportional or not, If it is, it then returns the sum of 'tf_CharSpace' + 'tf_CharKern' for the supplied character 'c'. Unfortunately, this does not appear to be correct. It comes close, but I appear to be short a pixel or two with each character. I'm stuck. Thanx in advance, Lee (Lee_Robert_Willis@cup.portal.com) ----------------------------------------------------------------------- Source Code ----------------------------------------------------------------------- #define FONT_IS_PROPORTIONAL(f) (f->tf_Flags & FPF_PROPORTIONAL) short Width_of_Char( unsigned char c, struct TextFont *Font ) { short width; /* pixels */ USHORT char_index; UWORD *CharSpacing, *CharKerning; if FONT_IS_PROPORTIONAL( Font ) { /* Make sure 'c' is within the legal character set * for 'Font'. */ if ((c >= Font->tf_LoChar) && (c <= Font->tf_HiChar)) { /* map 'c' into the appropriate index for the * 'Font->tf_CharSpace' array. */ char_index = c - Font->tf_LoChar; CharSpacing = (UWORD *) Font->tf_CharSpace; width = CharSpacing[ char_index ]; /* Add in kerning, if applicable. */ if ((CharKerning = (UWORD *) Font->tf_CharKern)) width += CharKerning[ char_index ]; } else width = Font->tf_XSize; /* ERROR: 'c' is not in range. */ } else /* All font characters are the same size. */ width = Font->tf_XSize; return width; }
mcmahan@netcom.UUCP (Dave Mc Mahan) (10/14/90)
In a previous article, Lee_Robert_Willis@cup.portal.com writes: > > >Does anyone know how to find the width of a single character within >a proportional font, without using 'TextLength()'? (The font/character >pair may not be the current font of the RastPort.) > >Using the RKM section on Text (1.3 Chapter 24), I constructed the >following function. It checks if the font is proportional or not, >If it is, it then returns the sum of 'tf_CharSpace' + 'tf_CharKern' >for the supplied character 'c'. > >Unfortunately, this does not appear to be correct. It comes close, >but I appear to be short a pixel or two with each character. I'm stuck. Have you thought about using the function called IntuiTextLength() with the IntuiText string equal to the character you wish to know the width of? I would guess that all you need to do is declare an IntuiText structure, put the desired character in a NULL terminated string of length 1, set the IntuiText.IText field to the address of the string, and set the ITextFont to the proper font. This function would (I assume) give you what you want. Of couse, I have never tried it with proportional fonts, so you just never know, do you? :-) > Lee > (Lee_Robert_Willis@cup.portal.com) -dave
peter@cbmvax.commodore.com (Peter Cherna) (10/15/90)
In article <34830@cup.portal.com> Lee_Robert_Willis@cup.portal.com writes: > > >Does anyone know how to find the width of a single character within >a proportional font, without using 'TextLength()'? (The font/character >pair may not be the current font of the RastPort.) Easy. Create a spare RastPort, and establish its font to the one you want to measure. Something like: struct RastPort tempRP; InitRastPort( &tempRP ); SetFont( &tempRP, myfont ); len = TextLength( &tempRP, text, len ); > (Lee_Robert_Willis@cup.portal.com) Peter -- Peter Cherna, Software Engineer, Commodore-Amiga, Inc. {uunet|rutgers}!cbmvax!peter peter@cbmvax.cbm.commodore.com My opinions do not necessarily represent the opinions of my employer. "Television is a medium because it is neither rare nor well-done."