Announcement

Collapse
No announcement yet.

Changing fonts -- is this right?

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

  • Changing fonts -- is this right?

    Hi everyone;

    I'm developing a dashboard for our manufacturing site. In essence, it queries the state of machines and reports them in a grid-like display. What I would like is that as the program screen changes size, the font used in each of the "cells" is changes to fill it. I'm developing in the FireFly environment, so getting all the controls themselves to resize correctly is taken care of as a property of the control. I did some searching a determined that finding the best font size to till a particular control is not straight forward, so I decided to take a "this size is good enough" approach.

    I've never dealt with changing fonts at run time before, so wanted to post the snippet here to make sure my logic is sound. My understanding is that a font gets gets created, then "lives" in the program until it is destroyed, so when I change from one font to another, I need to destroy the old one. This is the code I'm using. I know it's not pure Win32, but should show the concept

    Code:
    Function MAINFORM_WM_SIZE ( _
         hWndForm As Dword, _ ' handle of Form
         fwSizeType As Long, _ ' type of resizing request 
         nWidth As Long, _ ' new width of client area
         nHeight As Long _ ' new height of client area
    ) As Long
    
    Static OldFont As Dword
    Static OldHgt As Long
    
    Local xLoop As Long
    Local hFont As Dword
    
    Local f As Long
    
    Local w,h As Long
    
    ' all the controls have the same size
    FF_Control_GetSize HWND_MAINFORM_LBLGROUP(0), w, h
    
    ' pick something that's close
    Select Case h
    Case < 52 : f = 20
    Case < 60 : f = 28
    Case < 75 : f = 32
    Case < 85 : f = 36
    Case Else : f = 42
    End Select
    
    ' create the new font handle
    hFont = FF_MakeFontEX("Calibri", f, %False, %False, %False, %False)
    
    'put it on all the controls
    For xLoop = LBound(HWND_MAINFORM_LBLGROUP) To UBound(HWND_MAINFORM_LBLGROUP)
         FF_Control_SetFont HWND_MAINFORM_LBLGROUP(xLoop), hFont
    Next xLoop
    
    For xLoop = LBound(HWND_MAINFORM_LBLMACHINE) To UBound(HWND_MAINFORM_LBLMACHINE)
         FF_Control_SetFont HWND_MAINFORM_LBLMACHINE(xLoop), hFont
    Next xLoop
    
    FF_Control_SetFont HWND_MAINFORM_LBLMSG, hFont
    
    ' if there was an old one, delete it
    If OldFont > 0 Then DeleteObject OldFont
    
    OldFont = hFont
    
    ' debugging data
    FF_StatusBar_SetText HWND_MAINFORM_STATUSBAR, 0, Using$("Hgt # width # new font #", h,w,f)
    
    End Function
    Real programmers use a magnetized needle and a steady hand

  • #2
    Looks ok to me. Good way to test and make sure is to look at GDI object count in for example Task Manager.

    Comment


    • #3
      make sure is to look at GDI object count in for example Task Manager.
      Better, I think, to monitor it from within your program using ..

      Code:
      MACRO GetGDIObjectCount = GetGuiResources (GetCurrentProcess(), %GR_GDIOBJECTS) ' object count of current process
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Howdy Bud!

        >>>finding the best font size to till a particular control is not straight forward, so I decided to take a "this size is good enough" approach.

        Two approaches that make it very easy.

        Code:
        'Credit: Paul Dixon
        'Here's an approximation approach to getting the font size that will fill any area. This has
        'the advantage of making no assumptions about maximum font size.
           fSize& = IIF( w<h, 0.2*w, 0.2*h)
        
        'However, the above equation gives limited results. This second approach, which assumes the
        'font size will be no larger than 1000pts, gives a much better answer - and is very fast!
        Function GetFontSize_Graphic2(w As Long, h As Long, txt$, scalefactor As Single, fontName$) As Long
            Local x As Long, y As Long
            Graphic Font fontName$, 1000, 1
            Graphic Text Size txt$ To x,y
            Function=  1000/IIF( x/w > y/h , x/(w*scalefactor) , y/(h*scalefactor) )
        End Function
        Exact solution functions are also available if you don't like one of the above.

        Comment


        • #5
          Thanks to everyone for the suggestions. I've been using PB since it was TB, and only on DOS. In Windows I've never gone too far "under the hood", so when I do it's a learning experience.

          Gary Beene -- I didn't look too hard at any of the GRAPHIC statements because I was (am) under the impression that they are for when you specifically create a canvas to draw something. Also, looking at the PBWin /10 help file, I don't see GRAPHIC FONT, but do have a GRAPHIC SET FONT. Am I missing something?
          Real programmers use a magnetized needle and a steady hand

          Comment


          • #6
            Originally posted by Bud Durland View Post
            T. Also, looking at the PBWin /10 help file, I don't see GRAPHIC FONT, but do have a GRAPHIC SET FONT. Am I missing something?
            Gary is posting old code

            From the bottom of Help - -> Graphic Set Font
            Restrictions
            GRAPHIC SET FONT replaces GRAPHIC FONT. Note that the GRAPHIC FONT statement is no longer supported, so update your code to use the new syntax.


            Unlike the unsupported GRAPHIC FONT, GRAPHIC SET FONT doesn't take parameters for points and style. You can only specify those by creating a new font with FONT NEW

            Note also that Gary's functions use width and height in pixels. If you are using dialog units, you will have to convert the control size to pixels.

            (Not being a user of FF, I don't know how it would interface with standard PB font and control sizing methods, so can't help further)

            Comment


            • #7
              Howdy, Bud!

              Yes, normally you use a Graphic Control as you describe. But having Graphic Tex Size available is so useful that I often create a Graphic Bitmap to have access to that function.


              Howdy, Stuart!

              Yes, oldie but goodie. Given that the compiler is not under development, the support of Graphic Font is guaranteed for life!

              But, if it floats your boat, convert it over to Graphic Set Font.

              Comment


              • #8
                Originally posted by Gary Beene View Post
                Given that the compiler is not under development, the support of Graphic Font is guaranteed for life!
                That's a very negative assumption given Adam's comments at various times.
                But, if it floats your boat, convert it over to Graphic Set Font.
                For standard PB (I don't know about Firefly) :
                Code:
                FUNCTION GetFontSize_Graphic2(w AS LONG, h AS LONG, TXT AS STRING, scalefactor AS DOUBLE, fontName AS STRING) AS LONG
                LOCAL x AS LONG, y AS LONG
                LOCAL hFont AS DWORD
                FONT NEW fontname,1000,1 TO hFont
                GRAPHIC SET FONT hFOnt
                GRAPHIC TEXT SIZE TXT TO x,y
                FONT END hFont
                FUNCTION= 1000/IIF( x/w > y/h , x/(w*scalefactor) , y/(h*scalefactor) )
                END FUNCTION

                Comment


                • #9
                  Howdy, Stuart!

                  Whoops - you're right. I assumed the worst. I'm normally a very optimistic kind of guy. My bad!

                  I will go fix myself a peanut butter and mayonnaise sandwich and have some pork rinds with that. That will push my attitude back into the right position!

                  Comment


                  • #10
                    If you want to make it as easy as possible at runtime, then construct a table of font sizes matched to control sizes.
                    This should be for one line of text in a control. Whenever there is a redraw, reset font size.
                    The fonts should be static to the callback, or global, or controlled by a SUB or FUNCTION, where they reside.
                    They will be destroyed at program end. When you resize your font, you will END FONT and FONT NEW.
                    Then, CONTROL SET FONT.
                    This may work best in a SUB that holds the font handles as STATIC and has a table for conversion.
                    The world is strange and wonderful.*
                    I reserve the right to be horrifically wrong.
                    Please maintain a safe following distance.
                    *wonderful sold separately.

                    Comment


                    • #11
                      Originally posted by Kurt Kuzba View Post
                      If you want to make it as easy as possible at runtime, then construct a table of font sizes matched to control sizes.
                      This should be for one line of text in a control. Whenever there is a redraw, reset font size.
                      The fonts should be static to the callback, or global, or controlled by a SUB or FUNCTION, where they reside.
                      They will be destroyed at program end. When you resize your font, you will END FONT and FONT NEW.
                      Then, CONTROL SET FONT.
                      This may work best in a SUB that holds the font handles as STATIC and has a table for conversion.
                      That's almost exactly what Bud did in the initial post:
                      Code:
                      ...
                      ' pick something that's close
                      Select Case h
                      Case < 52 : f = 20
                      Case < 60 : f = 28
                      Case < 75 : f = 32
                      Case < 85 : f = 36
                      Case Else : f = 42
                      End Select
                      ...

                      Comment

                      Working...
                      X