Jump to content
Carlo Barazzetta

Looking for Icon Fonts support in Delphi for High-DPI and Themed app?

Recommended Posts

IconFontsImageList components by @Carlo Barazzetta could be the answer: you can explorer the complete wiki to see how it works.

 

Summary of library:

  • An IconFontsImageCollection component that inherits from Delphi's CustomImageCollection and is compatible with VirtualImageList
  • A IconFontsVirtualImageList, to use with Delphi version older than 10.3
  • A rendering engine of Icon-fonts using GDI+ (from Delphi XE4)
  • A complete backward compatibility with older Delphi versions (from Delphi 7)
  • A useful Collection and Component editor, with support for Category of Icons
  • A custom CharMap viewer, to easily select icons contained in any Font
  • Support for changing the Color based on the active VCL Style. 
  • High performance of drawing engine
  • Support for FMX (also for mobile platforms)
  • It's free and open-source

 

Icons based on Fonts are a good alternative to bitmaps because they need only the Font installed in the system to obtain thousands of images (like the "Material Design Font Desktop.ttf" font: https://github.com/Templarian/MaterialDesign-Font). The icons scales perfectly, so, you don't need to multiple resolutions of your images to match the DPI of the monitors and multiple colors for Theme used. 

The Collection of Icons can be rendered by a single Font/Color defined at collection level, or by different Fonts/Color defined at Icon level, so you can mix different icons from different Fonts in a single collection.

 

The library is quite stable, but any contribution is welcome!

 

ImageEditor.jpg

  • Like 5

Share this post


Link to post
Posted (edited)

Are you sure that the font has to be installed?

I'm loading a custom font from the resources and it's working fine:

 

    // Load custom Fonts before creating any forms
    ResStream := TResourceStream.Create(hInstance, 'MONTSERRAT_MEDIUM', RT_RCDATA);
    try
      AddFontMemResourceEx(ResStream.Memory, ResStream.Size, nil, @FontsCount);
    finally
      ResStream.Free;
    end;

 

Edited by Attila Kovacs

Share this post


Link to post
12 hours ago, Attila Kovacs said:

Are you sure that the font has to be installed?

I'm loading a custom font from the resources and it's working fine:

 


    // Load custom Fonts before creating any forms
    ResStream := TResourceStream.Create(hInstance, 'MONTSERRAT_MEDIUM', RT_RCDATA);
    try
      AddFontMemResourceEx(ResStream.Memory, ResStream.Size, nil, @FontsCount);
    finally
      ResStream.Free;
    end;

 

Yes, to develop the app the font must be installed on the system in order to use it in the IDE.
Deploying the app you can do as you explain: I have similar code in the demo, which responds to the OnFontMissing event raised by the component if Font is not installed:

 

  //The "material desktop font is not installed into system: load and install now from disk
  LFontFileName := ExtractFilePath(Application.ExeName)+'..\Fonts\Material Design Icons Desktop.ttf';
  if FileExists(LFontFileName) then
  begin
    {$IFNDEF D2010+}
    AddFontResource(PChar(LFontFileName));
    {$ELSE}
    AddFontResource(PWideChar(LFontFileName));
    {$ENDIF}
    SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
  end

 

  • Like 1

Share this post


Link to post
3 hours ago, Carlo Barazzetta said:

Yes, to develop the app the font must be installed on the system in order to use it in the IDE.
Deploying the app you can do as you explain:

If you wanted to avoid the requirement of having the fonts installed globally in Windows you could write a design package with the fonts linked in as resources. The package, when loaded by the IDE, could load the fonts from the resources (using the code shown by @Attila Kovacs) and call Screen.ResetFonts to signal the IDE to reinitialize its font list . After that the fonts would be available to the IDE.

Share this post


Link to post
Posted (edited)
3 hours ago, Ondrej Kelle said:

If you wanted to avoid the requirement of having the fonts installed globally in Windows you could write a design package with the fonts linked in as resources. The package, when loaded by the IDE, could load the fonts from the resources (using the code shown by @Attila Kovacs) and call Screen.ResetFonts to signal the IDE to reinitialize its font list . After that the fonts would be available to the IDE.

Indeed:

From https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-addfontmemresourceex

Quote

 

This function allows an application to get a font that is embedded in a document or a webpage. A font that is added by AddFontMemResourceEx is always private to the process that made the call and is not enumerable.

A memory image can contain more than one font. When this function succeeds, pcFonts is a pointer to a DWORD whose value is the number of fonts added to the system as a result of this call. For example, this number could be 2 for the vertical and horizontal faces of an Asian font.

When the function succeeds, the caller of this function can free the memory pointed to by pbFont because the system has made its own copy of the memory. To remove the fonts that were installed, call RemoveFontMemResourceEx. However, when the process goes away, the system will unload the fonts even if the process did not call RemoveFontMemResource.

 

 

Edited by pyscripter

Share this post


Link to post


Actually the components do not install any fonts on purpose, to avoid thinking they only works with a specific font ... The demo uses a sample font, and also shows how to load it on-the-fly if it is not installed on the system.
The downside is that when you open the demo you don't see the icons right away in the IDE and this could make you think that the component is not working properly...
The other aspect is that any font loaded as a resource in the IDE affects the possibility of installing a new version on the same font in the system...
I think that letting the developer choose to install the fonts he needs to use in his applications to use them with the components is the right and least invasive choice, don't you agree?

Share this post


Link to post

The transition to "Virtual" is now complete (released ver.2.3).
Demos for 10.3 and 10.4 now uses native TVirtualImageList + TIconFontsImageCollection, and the TIconFontImage component can also be linked to VirtualImageList and IconFontsVirtualImageList (not only to IconFontsImageList).

I'm searching for users and contributors to the project, expecially for older Delphi versions (because the components are compatible from Delphi 7 to current): it's very hard to me to maintain backward compatibility without help.
From Delphi XE4 they uses GDI+ to render icons. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×