PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: font resizing  (Read 1248 times)

raymw

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 64
font resizing
« on: February 05, 2017, 06:33:38 PM »

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.

Code: [Select]
  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.

 
Logged

Paul Squires

  • Administrator
  • Master FireFly Member
  • *****
  • Posts: 8104
  • Windows 10
    • PlanetSquires Software
Re: font resizing
« Reply #1 on: February 06, 2017, 03:13:13 PM »

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.
Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor

raymw

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 64
Re: font resizing
« Reply #2 on: February 06, 2017, 04:57:59 PM »

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?
Logged

Paul Squires

  • Administrator
  • Master FireFly Member
  • *****
  • Posts: 8104
  • Windows 10
    • PlanetSquires Software
Re: font resizing
« Reply #3 on: February 06, 2017, 06:17:39 PM »

Something like this....

Code: [Select]
' 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.
Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor

raymw

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 64
Re: font resizing
« Reply #4 on: February 06, 2017, 09:14:40 PM »

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

Best wishes,
Ray
Logged

raymw

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 64
Re: font resizing
« Reply #5 on: February 07, 2017, 08:45:04 PM »

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 -
Code: [Select]
  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.
Logged

Josť Roca

  • FireFly3 Registered User
  • Master FireFly Member
  • *
  • Posts: 2785
    • Josť Roca Software
Re: font resizing
« Reply #6 on: February 07, 2017, 09:16:39 PM »

> 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:

Code: [Select]
DeleteObject(cast(HGDIOBJ, cast(HFONT, (myfont))))

raymw

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 64
Re: font resizing
« Reply #7 on: February 07, 2017, 09:43:06 PM »

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.
Logged

raymw

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 64
Re: font resizing
« Reply #8 on: February 07, 2017, 10:03:29 PM »

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.
Logged

Josť Roca

  • FireFly3 Registered User
  • Master FireFly Member
  • *
  • Posts: 2785
    • Josť Roca Software
Re: font resizing
« Reply #9 on: February 07, 2017, 10:16:50 PM »

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

Code: [Select]
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

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 64
Re: font resizing
« Reply #10 on: February 08, 2017, 07:39:38 AM »

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.
Logged

raymw

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 64
Re: font resizing
« Reply #11 on: February 08, 2017, 09:46:21 AM »

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.
Logged

Paul Squires

  • Administrator
  • Master FireFly Member
  • *****
  • Posts: 8104
  • Windows 10
    • PlanetSquires Software
Re: font resizing
« Reply #12 on: February 10, 2017, 08:08:56 AM »

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.
Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor

raymw

  • FireFly3 User
  • Junior FireFly Member
  • *
  • Posts: 64
Re: font resizing
« Reply #13 on: February 10, 2017, 09:43:50 AM »

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
Logged

Paul Squires

  • Administrator
  • Master FireFly Member
  • *****
  • Posts: 8104
  • Windows 10
    • PlanetSquires Software
Re: font resizing
« Reply #14 on: February 10, 2017, 10:08:40 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.

Quote
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
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.
 
Logged
Paul Squires
PlanetSquires Software
FireFly Visual Designer, WinFBE Editor
Pages: [1] 2