It is currently Mon Dec 16, 2019 9:11 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Handling of many used fonts
PostPosted: Mon Mar 25, 2019 11:22 am 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4468
Location: Germany
Hi,

I wrote an own gadget which loads a font depending on the used size of the gadget.
This gadget is used many times in the code.
Also other sizes of the same font are loaded.

I wanted a better solution than always loading and freeing maybe the same fonts.
(And sometimes they can not freed)

So I wrote a small code to handle the used fonts.
Unfortunalely I had to write it as module, because I wanted it also from inside of an other module.

Maybe it is useful for others too.
It reduces the RAM usage of my code by 500k :wink:

Best: save it as FontList.pbi

Code:
CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


DeclareModule FontList
 
  Structure FontListStructure
    Font$
    Font.i
    InUse.i
  EndStructure
 
  Global NewList FontList.FontListStructure()
 
  Declare.i LoadToFontList(Font.i, Name$, Height.i, Flags=0)
  Declare FreeFromFontList(Font.i)
 
EndDeclareModule


Module FontList
 
  Procedure.i LoadToFontList(Font.i, Name$, Height.i, Flags=0)
   
    Protected Font$, FontAlreadyLoaded.i
   
   
    Font$ = Name$ + "_" + Str(Height) + "_" + Str(Flags)
   
    ForEach FontList()
      If FontList()\Font$ = Font$
        FontList()\InUse + 1
        FontAlreadyLoaded = #True
        Break
      EndIf
    Next
   
    If Not FontAlreadyLoaded
      AddElement(FontList())
      FontList()\Font = LoadFont(Font, Name$, Height, Flags)
      FontList()\InUse = 1
      FontList()\Font$ = Font$
    EndIf
   
    ProcedureReturn FontList()\Font
   
  EndProcedure
 
 
  Procedure FreeFromFontList(Font.i)
   
    ForEach FontList()
      If FontList()\Font = Font
        FontList()\InUse - 1
        If FontList()\InUse = 0
          FreeFont(Font)
          DeleteElement(FontList())
        EndIf
        Break
      EndIf
    Next
   
  EndProcedure
EndModule


CompilerIf #PB_Compiler_IsMainFile
 
  Define Font1.i, Font2.i, Font3.i, Font4.i, Font5.i
 
  Font1 = FontList::LoadToFontList(#PB_Any, "Arial", 12)
  Debug Font1
  Debug ListSize(FontList::FontList())
  Debug "----"
 
  Font2 = FontList::LoadToFontList(#PB_Any, "Arial", 12)
  Debug Font2
  Debug ListSize(FontList::FontList())
  Debug "----"
 
  Font3 = FontList::LoadToFontList(#PB_Any, "Arial", 12)
  Debug Font3
  Debug ListSize(FontList::FontList())
  Debug "----"
 
  Font4 = FontList::LoadToFontList(#PB_Any, "Arial", 14)
  Debug Font4
  Debug ListSize(FontList::FontList())
  Debug "----"
 
  Font5 = FontList::LoadToFontList(#PB_Any, "Arial", 14, #PB_Font_Bold)
  Debug Font5
  Debug ListSize(FontList::FontList())
  Debug "----"
 
  ForEach FontList::FontList()
    Debug FontList::FontList()\Font$ + " in use: " + Str(FontList::FontList()\InUse)
  Next
  Debug "----"
 
  FontList::FreeFromFontList(Font2)
  Debug ListSize(FontList::FontList())
  Debug "----"
 
  FontList::FreeFromFontList(Font1)
  Debug ListSize(FontList::FontList())
  Debug "----"
 
  FontList::FreeFromFontList(Font3)
  Debug ListSize(FontList::FontList())
  Debug "----"
 
CompilerEndIf

Bernd


Top
 Profile  
Reply with quote  
 Post subject: Re: Handling of many used fonts
PostPosted: Mon Mar 25, 2019 12:51 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2073
Location: Germany
Thanks :wink:

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye