Skip to content

Restore ability to specify FreeType font "named instance index" #8775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

Valakor
Copy link
Contributor

@Valakor Valakor commented Jul 5, 2025

The member ImFontConfig::FontNo is used as the argument face_index when FreeType is enabled which is a 32-bit value where the upper and lower 16 bits have different uses. One of the recent font rendering changes for 1.92 seems to have changed this member from int to ImS8 which prevents specifying the full 32 bits. The upper 16 bits allow for specifying the "named instance index" / named font variation in certain fonts, like predefined font weights (Bold, SemiBold, Light, etc.).

For completeness I've also fixed the call to FT_New_Memory_Face to use the correct casts to FT_Long which is a typedef of signed long rather than uint32_t to avoid a signed -> unsigned -> signed roundtrip.

From the FreeType doc comments:

   *   face_index ::
   *     This field holds two different values.  Bits 0-15 are the index of
   *     the face in the font file (starting with value~0).  Set it to~0 if
   *     there is only one face in the font file.
   *
   *     [Since 2.6.1] Bits 16-30 are relevant to TrueType GX and OpenType
   *     Font Variations only, specifying the named instance index for the
   *     current face index (starting with value~1; value~0 makes FreeType
   *     ignore named instances).  For non-variation fonts, bits 16-30 are
   *     ignored.  Assuming that you want to access the third named instance
   *     in face~4, `face_index` should be set to 0x00030004.  If you want
   *     to access face~4 without variation handling, simply set
   *     `face_index` to value~4.

Valakor added 2 commits July 5, 2025 08:51
When used with FreeType this value is passed as `face_index` which is interpreted as two 16-bit values: the lower 16 bits are the "face index" and the upper 16 are the "named instance index" for the selected face. Specifying the instance index allows for things like predefined font weights (Bold, SemiBold, Light, etc.) in many fonts.
The function `FT_New_Memory_Face` takes `FT_Long`s as arguments, which are typedefs of `signed long`. Avoid the round-trip signed->unsigned->signed cast by just casting to the destination type directly.
ocornut pushed a commit that referenced this pull request Jul 5, 2025
When used with FreeType this value is passed as `face_index` which needs to be 32-bits.

# Conflicts:
#	docs/CHANGELOG.txt
@ocornut
Copy link
Owner

ocornut commented Jul 5, 2025

Thanks for your PR. I have merged this as 0448428 with slight adjustment (the value was a specific area of the struct layout and changing from s8 to s32 increased structure size from 144 to 152 bytes. I shuffled things around to keep the struct 144 bytes with default settings).

@ocornut ocornut closed this Jul 5, 2025
@ocornut ocornut added the bug label Jul 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants