-
March 20th, 2014, 01:55 AM
#1
Find font (size problem)
Hello,
my problem:
I have a text (singleline) and a fix width for the whole text.
How do I find the correct font (height) for this text?
thx.
Ralf
-
March 20th, 2014, 02:36 AM
#2
Re: Find font (size problem)
Define "correct font".
There might be a lot of fonts with a lot of font sizes that would fit your "fix width" and there might be that no font would fit it!
Victor Nijegorodov
-
March 20th, 2014, 02:58 AM
#3
Re: Find font (size problem)
Hello Victor,
an example:
I need the font "Arial". The width for the text "Hello" shoud be 100px.
How do I find/create it?
-
March 20th, 2014, 04:24 AM
#4
Re: Find font (size problem)
- Get the DC of the control.
- Create font Arial with some size.
- Select font in the DC.
- Use some of the CDC methods (like GetTextExtent) to obtain the width of the displayed text.
- Compare it with the predefined width (100px in your example).
- If the obtained width is more than the predefined - decrease font size, if it is less - increase font size, and goto the p.2.
- ...
Victor Nijegorodov
-
March 20th, 2014, 05:10 AM
#5
Re: Find font (size problem)
ok, that is possible.
But no solution for me, because it is a time critical function (I have to obtain the font for many texts).
Maybe I should do this:
I have to pre-calculate the possible fonts for some typical widths.
-
March 20th, 2014, 07:37 AM
#6
Re: Find font (size problem)
You can make good estimates based on averages. But if you really need precision and the maximum size the font can be to not exceed the limit, then the solution given by victor is the only solution.
Besides... define 'time critical' ? maybe this solution is fast enough. If you're not actually doing drawing, then this is quite fast. Entire wordprocessors and browser renderers rely on that very feature. Whatever you are trying to precalculate, the built in functions will probably do better/faster.
If you really can't afford a performance hit, consider using a monotype font like courier/consolas. Calculating the width of a tekst is then the width of a single char multiplied by the number of chars.
-
March 20th, 2014, 08:08 AM
#7
Re: Find font (size problem)
Hello OReubens,
thank you.
I am doing drawing!
I can not use a monotype font.
Please tell me more about "estimates based on averages". How can I do that?
Ralf
-
March 20th, 2014, 08:33 AM
#8
Re: Find font (size problem)
Have a look at CDC::GetTextMetrics
Victor Nijegorodov
-
March 21st, 2014, 07:32 AM
#9
Re: Find font (size problem)
Originally Posted by Ralf Schneider
I am doing drawing!
I think you misunderstood me.
Yes, you are actually drawing, but usually once once for a given case.
the testing phase where you "try" different sized fonts, you don't actually draw anything, you're only asking the API's to calculate how much width/height it would take if you did actually draw.
So this is only a calculation of size, not actually manipulating pixels. THe calculations are typically very fast. Actual drawing can be an order of magnitude slower, especially when you're goign to screen and want antialiassed or cleartype text. But like I said, you typically only actually draw once.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|