Jump to content
  • Advertisement
Sign in to follow this  
Super Llama

[C++] Best way to apply kerning in a selection algorithm?

This topic is 2842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I just got done writing a caret positioning algorithm for text, and so far it works very well except one problem. It iterates over each character in the string and adds its width (obtained via GetCharABCWidths) to a variable which is compared with the cursor's X position relative to the text object, and returns the position of the character that passed the limit. Again, it works perfectly except for one problem-- it doesn't support kerning, so if you type VAVAVAVAVAVAVA, the caret gets way off from the proper location. What I'm asking is, what's the best way to compensate for this? Will I need to use GetKerningPairs to fill an array for the font, then iterate over each kerning pair and apply it if necessary to each character? That sounds very very unoptimized and I'm kind of hoping that the winapi has a way to just return the kerning coordinates for two characters... but if not, perhaps there is another way? GetTextExtentPoint32, which I've been combining with substr for actually finding the position of the caret, also seems to not apply kerning to its calculations. Any solutions?

Thanks.

EDIT: Alternatively, a way to disable kerning for D3DX Fonts would work just as well.

Share this post


Link to post
Share on other sites
Advertisement
With GetTextExtentPoint32, you have to set the current font in win32. I'm not at home ATM so I can't be more specific, but you have to alter the functions in your font initialization code. I mean don't change back to the default font. (I assume you used the NEHE tutorial for the fonts. It's the main issue with the tutorial, t doesn't explain what's happening).

Anyway, GetTextExtentPoint32 is dead precise if the correct font is set as current.

I hope that helps.

Share this post


Link to post
Share on other sites
So even though I used GetDC on the D3DXFont for GetTextExtentPoint32, it still might not have kerning? It doesn't seem out of sync, I can change the font in my box and the caret draws correctly unless I type a lot of kerned characters. That doesn't quite answer the main question either-- will I need to use GetTextExtentPoint32 with a for loop full of susbtrings (ugh, inefficiency) to process kerning, or is there an optimized way to use GetKerningPairs?

Share this post


Link to post
Share on other sites
How do you create your fonts?


Try GetTextExtentExPoint.
And again, if the font is not set as current, then the kerning will be off. It works, "kerning always works" with GetTextExtentExPoint, but with the current font. Which may be totally different than the font you render.

But maybe we misunderstand each other.....

Share this post


Link to post
Share on other sites
Again, I'm using ID3DXFont, so I'm creating them through D3DX. Is it possible that GetTextExtentPoint32 is recognizing my font size without recognizing my font face? Because like I said, if I change the font size the length still matches up perfectly except for kerning.

Share this post


Link to post
Share on other sites
Raw GDI (i.e. GetTextExtentPoint32) doesn't do kerning at all. If you want kerning you have to do it yourself. GetKerningPairs is a helper function but you still need to manually adjust glyph positions.

I don't know if D3DXFont supports kerning or if it's on by default. But if it is then that would explain what you're seeing.

Unfortunately I don't see an easy solution short of turning off kerning in DX, assuming that's possible.

Share this post


Link to post
Share on other sites

Unfortunately I don't see an easy solution short of turning off kerning in DX, assuming that's possible.


Yeah, that's what I was trying to do but I can't seem to find a way to do it. Speaking of fonts though, I just ran a profiler on my engine and found out that ID3DXFont::DrawText is responsible for a HUGE chunk of the cpu usage, significantly bigger than my node dispatch system and regular sprite rendering combined. I've heard that CD3DFont is faster too and supposedly has "less features..." I wonder if kerning is one of the features it's missing? If so, I'd love to try and use it instead, but for some reason my headers don't seem to include it.

EDIT: So I set the font to wide latin, and the caret drew perfectly. I'm certain that the problem is solely due to kerning, but I can't figure out how to disable kerning in, for example, my Arial font.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!