Find font (size problem)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Find font (size problem)

Hybrid View

  1. #1
    Join Date
    Jul 2001
    Posts
    293

    Question 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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    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

  3. #3
    Join Date
    Jul 2001
    Posts
    293

    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?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: Find font (size problem)

    1. Get the DC of the control.
    2. Create font Arial with some size.
    3. Select font in the DC.
    4. Use some of the CDC methods (like GetTextExtent) to obtain the width of the displayed text.
    5. Compare it with the predefined width (100px in your example).
    6. If the obtained width is more than the predefined - decrease font size, if it is less - increase font size, and goto the p.2.
    7. ...
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2001
    Posts
    293

    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.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,100

    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.

  7. #7
    Join Date
    Jul 2001
    Posts
    293

    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

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,100

    Re: Find font (size problem)

    Quote Originally Posted by Ralf Schneider View Post
    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.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,605

    Re: Find font (size problem)

    Have a look at CDC::GetTextMetrics
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center