Fitting Text into a Rect
-
I am working on a system were I need to display text in a rect at the best possible resolution. I am passed the a CDC, font name, color, and a CRect. Here is my current approach (which I am not happy with). The rect is passed in logical coords (HIEnglish), so I convert it to device coords with LPtoDP. I fill in the LOGFONT struct with the lf.lfHeight = -rect.Height(). then select the font into the CDC and use the DrawText function with the DT_CALCRECT option to see how big the text will be. If it does not fit, I loop thru this procedure making the font smaller until it fits in the original rect. Does anybody have a better idea? Thanks in advance. Craig
-
I am working on a system were I need to display text in a rect at the best possible resolution. I am passed the a CDC, font name, color, and a CRect. Here is my current approach (which I am not happy with). The rect is passed in logical coords (HIEnglish), so I convert it to device coords with LPtoDP. I fill in the LOGFONT struct with the lf.lfHeight = -rect.Height(). then select the font into the CDC and use the DrawText function with the DT_CALCRECT option to see how big the text will be. If it does not fit, I loop thru this procedure making the font smaller until it fits in the original rect. Does anybody have a better idea? Thanks in advance. Craig
cdsmith wrote: Does anybody have a better idea? Well I don't. If you are concerned about performance then maybe halving the font size each time until it fits, then working up untill it doesn't may be faster. I can think of some other optimizations as well, but you haven't said that performance is an issue, so I assume it isn't. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
-
I am working on a system were I need to display text in a rect at the best possible resolution. I am passed the a CDC, font name, color, and a CRect. Here is my current approach (which I am not happy with). The rect is passed in logical coords (HIEnglish), so I convert it to device coords with LPtoDP. I fill in the LOGFONT struct with the lf.lfHeight = -rect.Height(). then select the font into the CDC and use the DrawText function with the DT_CALCRECT option to see how big the text will be. If it does not fit, I loop thru this procedure making the font smaller until it fits in the original rect. Does anybody have a better idea? Thanks in advance. Craig
Do you try scaling the width accordingly to the results? Example: Desired width = 100 1. pass: Font width = 17 and you've got 135 pixels wide output. 2. pass: Font width = 100*17/135 ~= 12 and you should get roughly 100 pixels. Sure it won't give exact results (because of all the kerning and precision bloat) but I think it's a better approach.