• Welcome to PlanetSquires Forums.
 

font resizing

Started by raymw, February 05, 2017, 06:33:38 PM

Previous topic - Next topic

raymw

I set up a form with two text boxes and a button. I preset text1 font to be larger than text2, and when the button is selected, it gets the font from text1, and sets it to text2. that works. However, select the button more than once and textbox2 returns to original size, but bold text.

  Sub fontsize()
   
    Dim fo1 as hFont
    Dim fo2 as hfont   
   
      fo1 =  FF_Control_GetFont( hwnd_form1_text1)
      fo2=fo1
      FF_Control_SetFont( hwnd_form1_text2, fo2 )           
       
   
   End Sub



If I use getfont, how do I adjust the size of the font, I would have expected a hook into the various attributes of the font?

I have tried using FF_MakefontEX, but just can't anything to compile, my not understanding the syntax/structure of this is most likely the problem. I guess I learn best by example, but I have not found any examples at the level of my current understanding, at the moment. I would be grateful if someone could perhaps modify the few lines of code (above) to show how having got a font, it can be modified, (by size, say), and reused until it is modified again.


Paul Squires

Looks like the FF_Control_SetFont function may be a little broken. Years ago, Firefly would keep track of what font handles were assigned and active within the running program (via a couple of global arrays). When setting a font via FF_Control_SetFont, those arrays would be checked to see if the font already existed and it was was, then it would not be deleted before the new font assigned...because this would invalidate a font belonging to another control. I just did a quick look at the Firefly source and it appears that when the fonts are created they are never assigned to those global arrays in the first place. Something must have changed in the code over the years and this got broken.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

raymw

Hi Paul, thanks for your reply. Is FF_MakeFontEX working? If so can you write an example, FF_MakeFontEX line with some example values, so I can set a font (at least once) to my own design. If I knew how, presumably I could access the windows api directly, are there any notes on the syntax/format of doing so, or do I stand no chance?

Paul Squires

Something like this....


' nWeight is FW_BOLD for bold and FW_NORMAL for normal wight
' nItalic, nUnderline, nStrikeout are all TRUE/FALSE depending on what you need.
DIM AS HFONT  myFont = FF_MakeFontEX( "Arial", 14, nWeight, nItalic, nUnderline, nStrikeOut )


If you decide to create and set your own fonts then you are responsible for destroying the fonts when you are finished with them otherwise you will encounter GDI leaks. Use DeleteObject(myFont) to destroy the font.

Most people create the fonts they need as global/shared variables and create them at program startup and destroy them at shutdown. That makes it easier to manage.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

raymw

Thanks Paul,
I'll experiment over the next few days, a bit late here (5hrs ahead)

Best wishes,
Ray

raymw

This is what I have found- deleteobject(myfont) does not seem to work. However delete(myfont) does. The size can be from 0 to over 80 (I got fed up with testing beyond that, plenty large enough for what I wanted). code below -  Sub fontsize()
     

'Dim as HFONT  myFont = FF_MakeFontEX( "Arial", size, nWeight, nItalic, nUnderline, nStrikeOut )
' 0 is not set, 1 set
                         
                  fsize=fsize+1 
Dim as HFONT  myFont = FF_MakeFontEX( "Tahoma", fsize, 0,0,0,0 )
                 Print myfont,fsize
     
      FF_Control_SetFont( hwnd_form1_text2, myfont )   
  Delete (myfont)
 
   End Sub


fsize was dimensioned as shared long, and subroutine called every time the button was hit. Although font sizes are not listed as sequential - e.g, sequence is often given as point sizes similar to 24,26,28,36,48 , the sizes can be 24,25,26,27,28 etc.
I'm not sure wrt the ff_setfont problem, very occasionally it seemed that the font size froze, and I managed to crash firefly a few times.
Anyway, this was just a test to see how evenly the fonts would scale, and I'll see how it responds in my final program. Thanks for your help Paul.

José Roca

> This is what I have found- deleteobject(myfont) does not seem to work. However delete(myfont) does.

This is plainly wrong. Only use Delete to delete data allocated with the New operator

If by "does not seem to work" you mean some kind of syntax error or warning, it is because FB is very picky about casting. You have to use:


DeleteObject(cast(HGDIOBJ, cast(HFONT, (myfont))))


raymw

Thanks, Jose. I'll give it a try. Using delete probably gave the interesting results I was getting, wrt rapid font changes.

I've just tested it, and it does not allow the font size to be changed after the first time. In my earlier post in this thread, Paul responded that there was something not quite right with the setfont function. I've found that using delete, although not right, seems to allow me to do what I want to do, whereas using deleteobject does not.

raymw

OK, this is the fundamentals of the problem, even simpler than when I first mentioned. Design a form with a text box on it. Write the code such that when the height of form is increased, the text box and the font size increases, too. It has to be done  on the fly, not just drag the form to size, and then the text size jumps to fit. Write the code, test it, and show all the code (It'll only be few functions). It is not much help to me, trying to get to grips with the terminology, the syntax, etc., if I can't tell if is my lack of understanding, or bugs in the software that is the problem. If there is a work around by having to make direct calls to the windows api/whatever, then please show the code. I would like to be able to pull it apart, learn by example, whatever you like to call it.

José Roca

I don't use visual designers. What I'm saying is that it is wrong to use Delete instead of DeleteObject to delete a font object.

Besides, you are deleting the font after setting it


FF_Control_SetFont( hwnd_form1_text2, myfont )   
Delete (myfont)


When what you should delete is the old font used by the control.

Seudo-code:

- hOldFont = Get the handle of the font used by the contol
- hNewFont = Make new font
- Set the new font
- Delete the old font

raymw

Thanks Jose, I'll try again, I'm thinking it will run just as well without any delete, but Paul mentioned that the setfont was broken, (second post, above) and maybe that is why I'm finding the problem with this. If the font object is not created properly, then I expect the delete object can't delete it properly. I am specifically asking for someone with more experience of firefly/freebasic, to actually write the code to test if it is functioning correctly, and if it is not, then hopefully they will be able to solve it in some other way, and offer a working solution.

raymw

as far as I can tell, it runs fine, without deleting the font, at least in my simple tests. I'll see how it pans out in a more complex program.

Paul Squires

I am going to look at the FF_CONTROL_SETFONT and FF_CONTROL_GETFONT code today and hopefully post a new FF exe for you to test.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

raymw

Thanks Paul.

fwiw, I actually got the font resizing to work well enough, but there is some fine tuning required to my size calculations. I think there is a misunderstanding of what is meant by 'a font', and what is meant by 'making a font'. For me, a font would be, say, 'courier' - it has various attributes, size, weight, etc. If I were to make a font, then it could be called, say, 'fred' with its own size range, etc., and its own shape. I'm more familiar when using fonts, to be able to refer to changing the font size by doing something like 'fred.size = 15', whereas I think that in ff I have to make a new font with that particular size, and would be expected to delete it before I changed the size again by making another font. My little program works well, changing font sizes all the time, without any font deletion that I am aware of.

However, overall, I'm pretty impressed with firefly, once I get to grips with the different ways of doing things, and I'm about to see how the graphics/drawing stuff is handled, or not. So plenty of more questions ahead, maybe  :D

Paul Squires

Quote from: raymw on February 10, 2017, 09:43:50 AM
... I think that in ff I have to make a new font with that particular size, and would be expected to delete it before I changed the size again by making another font. My little program works well, changing font sizes all the time, without any font deletion that I am aware of.

Yes, you can create and assign fonts to controls without a care in the world...until... you look at the GDI resource usage of your application. Every time you create a font, Windows creates a resource and assigns a font handle to it (HFONT). Windows will not automatically delete that handle so if you create thousands of font, you will get thousands of font resources created. On old systems like Windows 95 and Windows 200, the number of GDI resources per process were limited. It was not uncommon for poorly designed programs to crash the operating system because too many fonts (and other GDI resources ) were created. The creation of fonts without deleting unused fonts results in what is called GDI resource leaks. Similar to memory leaks where the programmer dynamically creates memory but fails to release it.

If you bring up Windows Task Manager, go under the "View" menu, select "Select Columns". Tick the box for GDI Objects. Now as your application executes keep watching the count of those objects. You will see it increase and increase and increase....

BTW, I have the code in Firefly fixed. I just need to wait until I get home in order to upload it to the server. You can test it then.

QuoteHowever, overall, I'm pretty impressed with firefly, once I get to grips with the different ways of doing things, and I'm about to see how the graphics/drawing stuff is handled, or not. So plenty of more questions ahead, maybe  :D
If you are not coming from a Windows API programming background then some concepts require a degree of learning. The object oriented languages and .NETs of the world hide all of the complexity from the programmer. Having a good knowledge of how Windows works at the API level is very valuable.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer