Announcement

Collapse
No announcement yet.

ANSI_FIXED_FONT width

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ANSI_FIXED_FONT width

    I'm writing a log-file viewer which uses ANSI_FIXED_FONT. I use ExtTextOut to display the data, declaring a %null to "the optional array of values that indicate the distance between origins of adjacent character cells". For some mouse-related function I need to know that "distance" and use:
    Code:
    GetObject GetStockObject( %ANSI_FIXED_FONT ), SIZEOF( font ), font
    I get font.lfWidth - On my system this value is greater by 1 with respect to the distance ExtTextOut uses. I tried GetCharWidth32, but it doesn't work. I also tried GetTextExtentPoint32 on a single character - it works, but I find it a bit too complicated. Is there a simple and reliable way to get this value?

    ------------------
    Rgds, Aldo

  • #2
    I have tested many different ways and found GetTextExtentPoint32
    to be the easiest way to get a somewhat correct width of a string.

    If you use fixed font size, why not grab the width in WM_CREATE and/or
    WM_SETFONT and use this stored value? Something like the following works
    fine for me in an editor I'm working on: (the one I use as viewer in the
    latest beta of Poffs, for example)
    Code:
      CASE %WM_SETFONT
         IF wParam THEN
            hdc = GetDC (hWnd)
            hFont = wParam
            CALL SelectObject (hdc, wParam)               'wParam is font handle
            CALL GetTextMetrics (hdc, tm)                 'Get font parameters
            cWidth  = tm.tmAveCharWidth                   'Average character width
            cHeight = tm.tmHeight + tm.tmExternalLeading  'Line spacing
            CALL SelectObject (hdc, wParam)
            CALL ReleaseDC (hWnd, hdc)
         END IF
    Then you can just multiply cWidth * LEN(txt) to get string width
    when using fixed size fonts. To get mouse/caret position on a line,
    I use something like:
    Code:
      CASE %WM_LBUTTONDOWN
         xCaret = LOWRD(lParam) / cWidth  'horizontal caret position
    ------------------


    [This message has been edited by Borje Hagsten (edited April 18, 2001).]

    Comment


    • #3
      Borje,

      thanks for your quick reply - I didn't try GetTextMetrics.

      Now I'm puzzled on Windows APIs. Look at the following table:
      Code:
      function       font             height   width
      ----------------------------------------------
      GetObject      ANSI_FIXED_FONT  13       9
         "           ANSI_VAR_FONT    13       9       
      GetTextMetrics ANSI_FIXED_FONT  12       8
            "        ANSI_VAR_FONT    12       5
      I don't know how can we be certain on API responses...


      ------------------
      Rgds, Aldo

      Comment


      • #4
        Be careful - as Borje subtly pointed out about the structure returned by GetTextMetrics - the .tmHeight member does not include the .tmExternalLeading, so you may not be comparing apples with apples.

        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          Lance,

          the .tmExternalLeading value is 0 for both ANSI_FIXED_FONT and ANSI_VAR_FONT. Hence all this is very confused. Some time ago I tried to select a font that had a given height - that is, the height of a letter like a capital "A". This is impossible because GetTextMetrics don't describes all fonts the same way. May be some fonts (like MS Sans Serif) consider the external leading to be included in the character map. For this reasons, I gave up; I consider an impossible task to write an automatic procedure to select a font with a given height (intended as the height the capital letters will use).

          ------------------
          Rgds, Aldo

          Comment


          • #6
            With ANSI_FIXED_FONT, usually no problem, but with ANSI_VAR_FONT I
            agree, seems impossible to determine. With TRUE TYPE fonts, one can
            get extended font data via for example GetOutlineTextMetrics. With
            any font, it's a living hell to do it properly. Typical MS design..

            Problem is even bigger in Sweden (and Germany, etc), because we use some
            characters with stuff above letters, like Å, Ä and Ö. In some situations, the
            ring and/or dots above those letters becomes invisible if not tmExternalLeading
            has been considered. In US alphabet (and some other languages), I don't
            think it matters, which I guess is why many US developers forget that part..

            There is some pages about this in win32.hlp, beginning at "Fonts and Text".
            Use the ">>" button to read through what MS has to say about it. Not that
            it helped me much, but at least some interesting info and samples there..


            ------------------

            Comment


            • #7
              Borje,

              also italian language has its problems (not as frequent as other languages - we use the accented capital É). The procedure I described in my last post worked on EnumFontFamilies and EnumFonts, but I discarded it at all because of the problems we are speaking on. In this moment I have a new idea: to TEST the font drawing some letters into a memory bitmap and verifying the rows of pixels each letter fills. This way one could be certain about the height of lower-case and upper-case letters, the rows needed to draw accents, rings, etc. May be I'll work on it... (has anibody written any similar?).

              It is incredible how complicated can be a "simple" task as to select a font...

              ------------------


              [This message has been edited by Aldo Cavini (edited April 18, 2001).]
              Rgds, Aldo

              Comment


              • #8
                i did it! look at http://www.powerbasic.com/support/pb...ad.php?t=22992

                my function gets the exact ascent, descent and maximum ascent for a given font. it creates a monochrome di bitmap and writes characters into it - then verifies the number of used pixel rows. of course the function can be modified to get other values, or to get values for bigger fonts (i limited the function to 100 rows ascent, 50 rows descent).

                ------------------
                Rgds, Aldo

                Comment

                Working...
                X