Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have printing code that draws grid on the paper.

Grid has 4 columns, and they have equal horizontal length. Height of the cell is tenth of the paper size. Total number of rows is unknown but I know for a fact that there will be at least one row.

Each cell has same physical size-> width is quarter of the paper width, and height is one tenth of the paper height. Maximum number of characters that can fit into cell is 50.

The problem I face is choosing proper font size so text of maximum length can fit into cell.

Browsing through MSDN documentation and WinAPI examples, I saw that they use GetTextExtPoint32 for similar purposes, but this works only if font already exists and is selected into device context, which is not the case here.

The only thing that crossed my mind was to create "dummy font", see if the example text can fit into cell, and then adjust it's size if the test fails. I have also found this blog[^] that recommends interesting approach to this problem, but being inexperienced I can't decide if "this is the proper way to go".

Can you recommend a correct solution for my problem?

If further info / edit is required, leave a comment and I will react as soon as possible.
Posted

1 solution

Quote:
The only thing that crossed my mind was to create "dummy font", see if the example text can fit into cell, and then adjust it's size if the test fails
That's a feasible approach indeed. But what do you mean with 'dummy' font? You need to know in advance the exact font you are going to use, otherwise you cannot do a correct calculation (By the way, as a rule of thumnb, while I deal with a proportional font, I use the width of the 'm' character in order to overstate the required line width).
If it is an option, then use a monospaced font, that would simplify a lot your job.
I suppose the blog you linked is a step forward your needs: there good proportions (i.e. pleasant for the eyes) between line height and width are discussed.
 
Share this answer
 
v2
Comments
AlwaysLearningNewStuff 29-Jun-14 16:13pm    
By a "dummy font", I mean to hardcode some height as a starting one. Then I would select that font into DC and do the testing. If the result is not good, then I would adjust the size of the font, and recreate the font with proper size. Then I would select the proper font into DC. Something like this:

long fontHeight = -MulDiv( 14, GetDeviceCaps( hdc, LOGPIXELSY, 72 );

HFONT font = CreateFont( fontHeight, ... );
HFONT oldFont = SelectObject( hdc, font );

// do the testing

//if fails calculate the proper size
// ...
//========== some code that adjusts font size ============//
fontHeight = //...
// now adjust the font and reselect it
SelectObject( hdc, oldFont );
DeleteObject( font );
CreateFont( fontHeight, ... );
oldFont = SelectObject( hdc, font );

// the rest is standard stuff...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900