Announcement

Collapse
No announcement yet.

Anyone used GetCharABCWidthsFloatA function

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

  • Anyone used GetCharABCWidthsFloatA function

    I'm trying to determine what characters in the currently assigned GRAPHIC font are actually defined. The font is always a fixed width font.

    So I figured I'd use GetCharABCWidthsFloatA to get the widths of each character and compare that to what GRAPHIC CELL SIZE indicates. What I'm trying to avoid is mispositioning of graphic text when the characters being printed are not defined in the current font.

    The function returns 3 values for each character, the A/B/C values which are the leading spacing, the glyph width and the trailing spacing.

    What I seem to be getting returned is always a zero for the A and C values, the B value seems reasonable based on what character I request. Except I can get width values that exceed the CELL SIZE value (like the letter 'm')

    I'm not sure I'm using the function correctly, or I'm misinterpreting the results.

    Here's my little test program, any help/suggestions would be appreciated.

    George
    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE ONCE "Win32api.inc"
    #INCLUDE ONCE "WinGDI.inc"
    GLOBAL hFixedFont, FontWidth, FontHeight AS LONG
    GLOBAL hWnd, hDC AS DWORD
    
    FUNCTION PBMAIN () AS LONG
    DIM fontanswer(1 TO 10) AS abcfloat
    LOCAL i, j AS LONG
       DIALOG NEW PIXELS, 0, "CharWidth", 50, 50, 500, 500, _
              %WS_CAPTION OR %WS_THICKFRAME, 0 TO hWnd
       CONTROL ADD GRAPHIC, hWnd, 101, "", 0, 0, 500, 500
       GRAPHIC ATTACH hWnd, 101
       FONT NEW "Courier New", 12, 1, 1, 1 TO hFixedFont
       GRAPHIC SET FONT hFixedFont
       GRAPHIC CELL SIZE TO FontWidth, FontHeight
       GRAPHIC PRINT FORMAT$(FontWidth) + " x " + FORMAT$(FontHeight)
    
       hDC = GetDC(hWnd)
       FOR i = 100 TO 110
          GetCharABCWidthsFloatA(hDC, i, i, fontanswer(1))
          GRAPHIC PRINT CHR$(i) + " " + FORMAT$(fontanswer(1).abcFA, "00") + " | " + FORMAT$(fontanswer(1).abcFB, "00")  + " | " + FORMAT$(fontanswer(1).abcFC, "00")
       NEXT i
       DIALOG SHOW MODAL hWnd
    END FUNCTION

  • #2
    ?? Aren't characters just images once they're put in a graphic control or window?? no more cells or character borders, just pixels of different color from background.

    Cheers,
    Dale

    Comment


    • #3
      George,

      You will get different results if you use this
      Code:
       '   hDC = GetDC(hWnd)
         Graphic Get Dc To hDC
      Rgds, Dave

      Comment


      • #4
        Originally posted by George Deluca View Post
        I'm trying to determine what characters in the currently assigned GRAPHIC font are actually defined. The font is always a fixed width font.

        So I figured I'd use GetCharABCWidthsFloatA to get the widths of each character and compare that to what GRAPHIC CELL SIZE indicates. What I'm trying to avoid is mispositioning of graphic text when the characters being printed are not defined in the current font.

        The function returns 3 values for each character, the A/B/C values which are the leading spacing, the glyph width and the trailing spacing.

        What I seem to be getting returned is always a zero for the A and C values, the B value seems reasonable based on what character I request. Except I can get width values that exceed the CELL SIZE value (like the letter 'm')
        Had you already not been satisfied with GRAPHIC TEXT SIZE?
        Code:
        #COMPILE EXE
        #DIM ALL
        #INCLUDE ONCE "Win32api.inc"
        #INCLUDE ONCE "WinGDI.inc"
        GLOBAL hFixedFont, FontWidth, FontHeight AS LONG
        GLOBAL hWnd, hDC AS DWORD
        
        FUNCTION PBMAIN () AS LONG
        DIM fontanswer(1 TO 10) AS abcfloat
        LOCAL i, j AS LONG
           DIALOG NEW PIXELS, 0, "CharWidth", 50, 50, 500, 500, _
                  %WS_CAPTION OR %WS_THICKFRAME, 0 TO hWnd
           CONTROL ADD GRAPHIC, hWnd, 101, "", 0, 0, 500, 500
           GRAPHIC ATTACH hWnd, 101
           FONT NEW "Courier New", 12, 1, 1, 1 TO hFixedFont
           GRAPHIC SET FONT hFixedFont
           GRAPHIC CELL SIZE TO FontWidth, FontHeight
           GRAPHIC PRINT FORMAT$(FontWidth) + " x " + FORMAT$(FontHeight)
        
           FOR i = 100 TO 110
              GRAPHIC PRINT CHR$(i) + " width" + STR$(GRAPHIC(TEXT.SIZE.X, CHR$(i)))+ _
                " height" + STR$(GRAPHIC(TEXT.SIZE.Y, CHR$(i)))
           NEXT i
           DIALOG SHOW MODAL hWnd
        END FUNCTION
        The world is strange and wonderful.*
        I reserve the right to be horrifically wrong.
        Please maintain a safe following distance.
        *wonderful sold separately.

        Comment


        • #5
          Dave: I don't follow - not setting hDC - the function then simply returns 0 for all A/B/C values. Can you elaborate?


          Hmmm, maybe I haven't been as clear as I hoped. GRAPHIC CELL SIZE is the default for the fixed pitch font, it doesn't consider undefined characters.

          If, after doing a GRAPHIC SET POS, I do a GRAPHIC PRINT "ABC", I know exactly where the "C" is positioned. (Based on the last SET POS and CELL SIZE)

          If instead I did a GRAPHIC PRINT "A" + CHR$(0) + "C", the "C" will be mis-positioned if the CHR$(0) character in the font is undefined. This throws off all further GRAPHIC PRINT statement on that line until I do another SET POS. This app is a text editor, having stuff mis-positioned on the screen is simply deadly.

          So I'd like to perform a translate of any non-displayable characters in a string to some known default character that IS defined.

          Yes, I know text is supposed to be "just text', but not in the real world, a lot of unprintable chars can occur (FF, VT, international characters, etc.)

          My goal is to create a string of the displayable characters in the current font. Then I can translate all others to a 'normal' default character.

          George

          Comment


          • #6
            George,

            You were using the hDC of the dialog ( hDC = GetDC(hWnd) )

            You need to use the DC of the Graphic control - Graphic Get DC to hDC.


            Rgds, Dave

            Comment


            • #7
              Dave: Gotcha!

              Now tested, works exactly as I wanted. Thanks, I knew it had to be something I'd overlooked.

              George

              Comment


              • #8
                GetGlyphIndices will help to determine what characters in the currently assigned GRAPHIC font are actually defined.


                Click image for larger version

Name:	GetGlyphIndices.png
Views:	1
Size:	93.9 KB
ID:	777839
                Code:
                #COMPILE EXE '#Win 9.07 (D:\Basic\Bas\Jose Roca\Forum\Jose\Windows API Headers\1.19 (PB9.x)\uz)#
                #DIM ALL
                #INCLUDE ONCE "Win32api.inc"
                #INCLUDE ONCE "WinGDI.inc"
                
                GLOBAL hDlg      AS DWORD
                GLOBAL hDC       AS DWORD
                GLOBAL sFont     AS STRING
                '_____________________________________________________________________________
                
                FUNCTION GraphPrint(index AS LONG, CharWidth AS ABCFLOAT) AS LONG
                 LOCAL TotalCharWidth AS SINGLE
                 LOCAL TextWidth      AS SINGLE
                 LOCAL TextHeight     AS SINGLE
                
                 GRAPHIC TEXT SIZE CHR$(index) TO TextWidth, TextHeight 'Ex: 141 = 0, 31 and 142 = 16, 31.
                
                 TotalCharWidth = CharWidth.abcFA + CharWidth.abcFB + CharWidth.abcFC
                 GRAPHIC PRINT FORMAT$(index, "000")                & " "   & _ 'Index aka ASC()
                               IIF$(TextWidth, CHR$(index), " ")    & " "   & _ 'Put a space if witdh is zero
                               FORMAT$(CharWidth.abcFA, "+0;-0; 0") & " "   & _ 'Lest spacing
                               FORMAT$(CharWidth.abcFB, "+0;-0;+0") & " "   & _ 'Character width
                               FORMAT$(CharWidth.abcFC, "+0;-0;+0") & " = " & _ 'Right spacing
                               FORMAT$(TotalCharWidth)              & " ("  & _ 'Total character width
                               FORMAT$(TextWidth, ";;00")           & ")"       'GRAPHIC TEXT SIZE
                
                END FUNCTION
                '_____________________________________________________________________________
                
                CALLBACK FUNCTION DlgProc
                 LOCAL sConvert  AS STRING
                 LOCAL sLog      AS STRING
                 LOCAL CharCount AS LONG
                 LOCAL index     AS LONG
                
                 SELECT CASE CBMSG
                
                   CASE %WM_INITDIALOG
                     PostMessage(hDlg, %WM_APP, 0, 0)
                
                   CASE %WM_APP
                     sConvert = CHR$(0 TO 255)
                     DIM pgi(0 TO 255) AS WORD
                
                     'The GetGlyphIndices function translates a string into an array of glyph indices.
                     'The function can be used to determine whether a glyph exists in a font.
                     CharCount = GetGlyphIndices(hDc, BYVAL STRPTR(sConvert), LEN(sConvert), BYVAL VARPTR(pgi(0)), %GGI_MARK_NONEXISTING_GLYPHS)
                     FOR index = 0 TO 256 / 8 - 1
                       sLog &= " Chr " & FORMAT$(index,       "000") & " " & IIF$(pgi(index      ) = &HFFFF??, "[NO-DEF]", "[" & CHR$(index      ) & "]") & $TAB & _
                               " Chr " & FORMAT$(index + 032, "000") & " " & IIF$(pgi(index + 032) = &HFFFF??, "[NO-DEF]", "[" & CHR$(index + 032) & "]") & $TAB & _
                               " Chr " & FORMAT$(index + 064, "000") & " " & IIF$(pgi(index + 064) = &HFFFF??, "[NO-DEF]", "[" & CHR$(index + 064) & "]") & $TAB & _
                               " Chr " & FORMAT$(index + 096, "000") & " " & IIF$(pgi(index + 096) = &HFFFF??, "[NO-DEF]", "[" & CHR$(index + 096) & "]") & $TAB & _
                               " Chr " & FORMAT$(index + 128, "000") & " " & IIF$(pgi(index + 128) = &HFFFF??, "[NO-DEF]", "[" & CHR$(index + 128) & "]") & $TAB & _
                               " Chr " & FORMAT$(index + 160, "000") & " " & IIF$(pgi(index + 160) = &HFFFF??, "[NO-DEF]", "[" & CHR$(index + 160) & "]") & $TAB & _
                               " Chr " & FORMAT$(index + 192, "000") & " " & IIF$(pgi(index + 192) = &HFFFF??, "[NO-DEF]", "[" & CHR$(index + 192) & "]") & $TAB & _
                               " Chr " & FORMAT$(index + 224, "000") & " " & IIF$(pgi(index + 224) = &HFFFF??, "[NO-DEF]", "[" & CHR$(index + 224) & "]") & $CRLF
                     NEXT
                
                     MessageBox(%HWND_DESKTOP, "GetGlyphIndices - " & sFont & $CRLF & sLog, _
                                "GetGlyphIndices for """ & sFont & """, [NO-DEF] mean that character is not defined.", _
                                %MB_OK OR %MB_TOPMOST)
                
                  END SELECT
                
                END FUNCTION
                '_____________________________________________________________________________
                
                FUNCTION PBMAIN () AS LONG
                 DIM   CharWidth(0 TO 255) AS ABCFLOAT
                 LOCAL hFont AS DWORD
                 LOCAL index AS LONG
                 LOCAL hIcon AS DWORD
                
                 sFont = "Courier New"
                
                 DIALOG NEW PIXELS, %HWND_DESKTOP, "GetCharABCWidthsFloat - " & sFont, , , 420, 1000, _
                 %WS_CAPTION OR %WS_MINIMIZEBOX OR %WS_SIZEBOX OR %WS_SYSMENU, 0 TO hDlg
                
                 CONTROL ADD GRAPHIC, hDlg, 101, "", 0, 0, 420, 1000
                 GRAPHIC ATTACH hDlg, 101
                
                 FONT NEW sFont, 12 + 08, 1, 1, 1 TO hFont
                 GRAPHIC SET FONT hFont
                 GRAPHIC GET DC TO hDC
                 GRAPHIC PRINT "GetCharABCWidthsFloat"
                 IF GetCharABCWidthsFloat(hDC, 0, 255, CharWidth(0)) THEN
                   GRAPHIC PRINT "Chr    L   W  R Total(GS)" 'Chr Glyph LeftSpacing Width RightSpacing TotalWidth Graphic_text_Size
                   FOR index = ASC("A") TO ASC("Z")
                     GraphPrint(index, CharWidth(index))
                   NEXT
                   index = 141 : GraphPrint(index, CharWidth(index))
                   index = 142 : GraphPrint(index, CharWidth(index))
                   index = 143 : GraphPrint(index, CharWidth(index))
                   'The character increment (total width) of a character is the sum of the A, B, and C spaces.
                   'Either the A or the C space can be negative to indicate underhangs or overhangs.
                 END IF
                
                 hIcon = ExtractIcon(GetModuleHandle(""), "Shell32.dll", 294)
                 SetClassLong(hDlg, %GCL_HICON, hIcon)
                
                 DIALOG SHOW MODAL hDlg CALL DlgProc
                 DestroyIcon(hIcon)
                
                END FUNCTION
                '_____________________________________________________________________________
                '
                Last edited by Pierre Bellisle; 8 Jan 2019, 11:44 AM.

                Comment


                • #9

                  GetCharABCWidthsFloatA - I don't quite know what it is producing. Your program shows "i" and "j" as having the same size 4. There must be some terrible rounding as "i" = 646 units wide and "j" = 862 units wide. The code from Kurt is rather misleading as every character in the font has the same width 2048 UPM.

                  Comment


                  • #10
                    Robert,

                    > The font is always a fixed width font
                    "Not my circus, not my monkeys."

                    Comment


                    • #11
                      Pierre:, I had just started to explore the various Glyph API calls as the GetCharABCWidth APIs didn't seem to be doing it. Your example is exactly what I'm looking for, many, many thanks.

                      George

                      Comment

                      Working...
                      X