Okay, I've another question relating to this problem.
problem with an assertion. It appears that when resizing the window and the
window practically disappears, an assertion is thrown.
Here's my code...
text expands just fine. However, when the window is sized to the point of
disappearing the assertion is thrown.
I've tried the following...
does not work if it is shrunk vertically. Any ideas how I can prevent this
The assertion is thrown in afxwin1.inl Line 620... the line there is as
follows...
Thank you once again.
"Joseph M. Newcomer" wrote:
Lengths are an irrelevant concept with variable-pitch fonts. In fact, the nWidth is
largely irrelevant, and is mostly ignored.
Forget any concept of "average width". There is only one reliable way to figure out the
width of a font, and that is GetTextExtent (ISO-8859, at least. For fonts with complex
rendering rules such as overlapping characters, it is not clear that GetTextExtent is the
best choice, but that is a long a complex discussion, and I find the idea that the primary
API for determining text length is not to be trusted--but that no cross-reference is given
to the suitable API--is offensive. But for your purposes, GetTextExtent is what you need
to use).
While "on the average" a font has a width, this is not necessarily set by
CreateFont[Indirect]. The matching algorithm only tries to approximate the desired
specification, but never exactly match it. So IIIIIIIIII and WWWWWWWWWW are quite
different widths, and in neither case is the average width worthwhile in predicting the
value which is the ACTUAL width of the string!
Using my FontExplorer, I can show that for MS Sans Serif 8:
TEXTMETRICS:
tmAveCharWidth 5
tmMaxCharWidth 11
and the widths for "Canoe" are 7,6,6,6,6,6. Note that all the character widths are > 5!
(The Font Explorer is part of our Win32 Programming download; you can get this at
http://www.flounder.com/download.htm Thecomplete download is 13MB)
If you gave the exact parameters you use for your font (instead of a vague handwave and
something about one of the parameters being 10) I could show a lot more information.
The "whitespace" around a character is a fairly complex discussion; it involves
pseudo-kerning (the ABC Widths parameters to each glyph in each font), which is all you
normally get, but there is also pair-kerning and track-kerning. This rarely has any
impact on what you are seeing.
Key here is you are relying on a demonstrably irrelevant computation, and you cannot do
that. You must use a relevant computation, such as GetTextExtent.
joe
On Wed, 14 Feb 2007 13:20:32 -0800, Trecius <Trecius@discussions.microsoft.com> wrote:
Hello, Newsgroupians:
I am creating a font, using CFont, but I'm running into problems. When I
create the font, using the function .CreateFont(nHeight, nWidth...), I can
create a font. However, when I use the font in a DC, the LENGTH of the
string, in LP, is not equal to the expected length. For example, suppose I
created a font with nWidth = 10. Therefore, ON AVERAGE, each character
should be 10 LP wide. If I want to output the string "Canoe" to the DC, I
would expect the length of the string to be 50 DP, for strlen("Canoe") = 5,
and 5 * 10 = 50.
I know the AVERAGE length that the string will occupy in DP -- 50 points. I
use this length to position a window next to the string I'm outputing as
follows...
------------------
Canoe | (WINDOW |
------------------
Therefore, if I ever change the string Canoe, the window will move with the
expansion or contraction of the length of the string.
However, at times, my string is truncated -- or hidden -- by the window as
follows...
------------------
Cano | (WINDOW |
------------------
The 'e' is truncated or sometimes slashed in half. Again, I know that when
defining a font, the width is the AVERAGE width and Windows MIGHT select
something different. How can I align my window to my variable-length string
without truncating the string?
Here are the things I already know:
1. When defining a font, the width is the AVERAGE width of a character.
For example, the lowercase letter 'l' has a smaller width than the lowercase
letter 'e.'
2. Again, when defining a font, the AVERAGE width you pass in may not be
the width that Windows selects.
Also, there are small "whitespaces" partioning each letter of a string. Is
the width of the whitespace included in the width of the character? Is this
why my calculated length is incorrect and the last letter truncated? If so,
is it possible to calculate this whitespace?
Thank you.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm