gimpfontfactory.c:Check for font type directy not through FT

Checking for valid font files thourgh harfbuzz or freetype (as it's done currently)
is slow, to speed up font loading a but, a less robust but faster check is done, which
is just reading and checking the first 4 bytes of every font file.

(cherry picked from commit c0e5107eee)
This commit is contained in:
Idriss Fekir 2025-09-25 00:25:15 +02:00 committed by Jehan
parent abc9e3ed64
commit 4497bf00c1

View file

@ -850,44 +850,40 @@ gimp_font_factory_load_names (GimpFontFactory *factory)
continue;
}
if (FT_New_Face (ft, file, 0, &face))
{
g_string_append_printf (ignored_fonts, "- %s (Failed To Create A FreeType Face)\n", file);
n_ignored++;
continue;
}
/*
* Pango doesn't support non SFNT fonts because harfbuzz doesn't support them.
* woff and woff2, not supported by pango (because they are not yet supported by harfbuzz,
* when using harfbuzz's default loader, which is how pango uses it).
* pcf,pcf.gz are bitmap font formats, not supported by pango (because of harfbuzz).
* afm, pfm, pfb are type1 font formats, not supported by pango (because of harfbuzz).
* This check is less robust than trying to create an FreeType face or a harfbuzz blob,
* but it's much faster.
*/
if (face->face_flags & FT_FACE_FLAG_SFNT)
{
/* If this is an SFNT wrapper, read the first 4 bytes to see if it is a WOFF[2] font. */
char buf[4] = {0};
int fd = g_open ((gchar *) file, O_RDONLY, 0);
{
char buf[4] = {0};
int fd = g_open ((gchar *) file, O_RDONLY, 0);
read (fd, buf, 4);
g_close (fd, NULL);
FT_Done_Face (face);
read (fd, buf, 4);
g_close (fd, NULL);
if (buf[0] == 'w' && buf[1] == 'O' && buf[2] == 'F' && (buf[3] == 'F' || buf[3] == '2'))
{
g_string_append_printf (ignored_fonts, "- %s (WOFF[2] font)\n", file);
n_ignored++;
continue;
}
if (buf[0] == 'w' && buf[1] == 'O' && buf[2] == 'F' && (buf[3] == 'F' || buf[3] == '2'))
{
g_string_append_printf (ignored_fonts, "- %s (WOFF[2] font)\n", file);
n_ignored++;
continue;
}
}
else
{
FT_Done_Face (face);
g_string_append_printf (ignored_fonts, "- %s (NON SFNT font)\n", file);
n_ignored++;
continue;
}
if ((buf[0] != 0x00 || buf[1] != 0x01 || buf[2] != 0x00 || buf[3] != 0x00) && /* older truetype */
(buf[0] != 't' || buf[1] != 'y' || buf[2] != 'p' || buf[3] != '1') && /* type1 wrapped in sfnt wrapper */
(buf[0] != 't' || buf[1] != 'r' || buf[2] != 'u' || buf[3] != 'e') && /* truetype */
(buf[0] != 'O' || buf[1] != 'T' || buf[2] != 'T' || buf[3] != 'O') && /* opentype */
(buf[0] != 't' || buf[1] != 't' || buf[2] != 'c' || buf[3] != 'f')) /* truetype collection */
{
g_string_append_printf (ignored_fonts, "- %s (NON SFNT font)\n", file);
n_ignored++;
continue;
}
}
/* Some variable fonts have only a family name and a font version.
* But we also check in case there is no family name */