Skip to content

Add API for loading fonts from binary TrueType/OpenType data in memory#15

Merged
MatthiasWM merged 5 commits intomasterfrom
copilot/add-font-reading-binary-data
Mar 13, 2026
Merged

Add API for loading fonts from binary TrueType/OpenType data in memory#15
MatthiasWM merged 5 commits intomasterfrom
copilot/add-font-reading-binary-data

Conversation

Copy link

Copilot AI commented Mar 13, 2026

Adds cross-platform support for loading fonts directly from memory buffers, enabling embedded fonts in applications without system installation.

API

// Load font from memory - returns font number or (Fl_Font)-1 on failure
Fl_Font font = Fl::load_font(ttf_data, ttf_size, "MyFont");
if (font != (Fl_Font)-1) {
    fl_font(font, 14);
    fl_draw("Hello!", 10, 30);
}

// Cleanup when done
Fl::unload_font(font);

Platform Implementations

  • Windows GDI: AddFontMemResourceEx() / RemoveFontMemResourceEx()
  • macOS CoreText: CGFontCreateWithDataProvider() + CTFontManagerRegisterGraphicsFont()
  • X11/Xft: FreeType2 FT_New_Memory_Face() + fontconfig via secure temp file (fontconfig lacks direct memory API)
  • Cairo/Pango: FreeType2 FT_New_Memory_Face() for font parsing (no fontconfig dependency)

Changes

  • FL/Fl.H: Public Fl::load_font() and Fl::unload_font() declarations
  • Fl_Graphics_Driver: Virtual methods with default no-op implementations
  • Fl_Fontdesc: Extended with mem_font_data, mem_font_size, mem_font_handle fields
  • src/CMakeLists.txt: Link FreeType on Linux when USE_XFT or USE_PANGO enabled
  • Platform drivers: Fl_GDI_Graphics_Driver, Fl_Quartz_Graphics_Driver, Fl_Xlib_Graphics_Driver, Fl_Cairo_Graphics_Driver

Notes

  • Font data buffer must remain valid while font is in use (not copied internally)
  • X11/Xft implementation uses mkstemps() for secure temp file creation
  • unload_font() on X11/Xft only removes the specific temp file; fontconfig will fail gracefully on next access
  • Cairo/Pango driver uses FreeType only (no fontconfig dependency) for font name extraction
Original prompt

This section details on the original issue you should resolve

<issue_title>Reading font information from binary blocks within the app</issue_title>
<issue_description>Windows GDI:
int AddFontMemResourceEx(PVOID pbFont, DWORD cbFont, PVOID pdv, DWORD *pcFonts);
RemoveFontMemResourceEx();

GDI+
Status AddMemoryFont(const VOID *memory, INT length);

Windows DirectWrite:
IDWriteFontFileLoader, IDWriteFontFileStream
IDWriteFactory::CreateFontFileReference()
IDWriteFactory::CreateFontFace()

macOS CoreGraphics
CGDataProviderRef provider = CGDataProviderCreateWithData(...);
CGFontRef font = CGFontCreateWithDataProvider(provider);
CTFontRef ct = CTFontCreateWithGraphicsFont(font, size, NULL, NULL);

X11/Pango/FreeType
FT_New_Memory_Face(
FT_Library library,
const FT_Byte* file_base,
FT_Long file_size,
FT_Long face_index,
FT_Face* aface
);

Wayland/Cairo
uses FreeType in the end es well

SDL_ttf
TTF_OpenFontIO(SDL_IOStream *src, SDL_bool closeio, int ptsize)

</issue_description>

<agent_instructions>Generate the API for FLTK that allows users to load fonts from binary truetype font descriptions in program memory. Implement for all platforms (Windows, macOS, X11, Wayland)</agent_instructions>

Comments on the Issue (you are @copilot in this section)


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

Copilot AI and others added 2 commits March 13, 2026 13:21
…iro/Pango support

Co-authored-by: MatthiasWM <653993+MatthiasWM@users.noreply.github.com>
…ring all fonts

Co-authored-by: MatthiasWM <653993+MatthiasWM@users.noreply.github.com>
Copilot AI changed the title [WIP] Add functionality to read font information from binary blocks Add API for loading fonts from binary TrueType/OpenType data in memory Mar 13, 2026
Copilot AI requested a review from MatthiasWM March 13, 2026 13:26
#include <unistd.h> // for unlink(), write()
#include <fcntl.h> // for open(), O_CREAT, O_EXCL
#include <sys/stat.h> // for mode constants

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "../../flstring.h" // for fl_strdup()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the missing flstring.h include in commit eb9c8ec.

Co-authored-by: MatthiasWM <653993+MatthiasWM@users.noreply.github.com>
Copy link
Author

Copilot AI commented Mar 13, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Copilot AI requested a review from MatthiasWM March 13, 2026 15:25
@MatthiasWM
Copy link
Owner

The Wayland build fails with
[ 57%] Linking CXX executable ../bin/fluid
/usr/bin/ld: ../lib/libfltk.a(Fl_Cairo_Graphics_Driver.cxx.o): undefined reference to symbol 'FcConfigGetCurrent'
/usr/bin/ld: /lib/x86_64-linux-gnu/libfontconfig.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Please fix this.

Copy link
Owner

@MatthiasWM MatthiasWM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the dependency on fontconfig in Fl_Cairo_Graphics_Driver.cxx and instead use TrueType calls.

FT_Done_FreeType(ft_library);
return (Fl_Font)-1;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Why is fontconfig needed at all? Doesn't FT_New_Memory_Face read a font from memory in ttf format already? Please check if most of the code until line 1683 is needed at all. If possible, remove all fontconfig calls and use only FreeType cals.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - I've simplified the code in commit 743aa2a. Now the implementation only uses FreeType to:

  1. Parse the font data with FT_New_Memory_Face
  2. Extract the font family name from ft_face->family_name
  3. Clean up with FT_Done_Face and FT_Done_FreeType

All fontconfig code has been removed. The font name is then registered with FLTK's font table for Pango to use.

… memory fonts

Co-authored-by: MatthiasWM <653993+MatthiasWM@users.noreply.github.com>
Copy link
Author

Copilot AI commented Mar 13, 2026

The Wayland build fails with
[ 57%] Linking CXX executable ../bin/fluid
/usr/bin/ld: ../lib/libfltk.a(Fl_Cairo_Graphics_Driver.cxx.o): undefined reference to symbol 'FcConfigGetCurrent'
...

Fixed in commit 743aa2a. Removed all fontconfig calls from the Cairo driver - it now uses only FreeType to parse the font and extract the family name. Also added FreeType linking for USE_PANGO builds in CMakeLists.txt.

Remove the dependency on fontconfig in Fl_Cairo_Graphics_Driver.cxx and instead use TrueType calls.

Done in commit 743aa2a. Removed the fontconfig dependency entirely and now use only FreeType (FT_New_Memory_Face) to parse the font data and extract the family name.

Copilot AI requested a review from MatthiasWM March 13, 2026 16:29
@MatthiasWM MatthiasWM marked this pull request as ready for review March 13, 2026 16:42
@MatthiasWM MatthiasWM merged commit e0ba4fa into master Mar 13, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reading font information from binary blocks within the app

2 participants