Skip to content
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

feat: truetype font features #692

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

erics118
Copy link

@erics118 erics118 commented Feb 19, 2025

Adds support for TrueType font features

You can use OpenType font features too, but you need to go through the docs, or <CoreText/SFNTLayoutTypes.h> to find the corresponding TrueType codes.

For example, the C code is:

int feat_name = kNumberSpacingType; // = 6
int feat_value = kMonospacedNumbersSelector; // = 0

CFNumberRef name = CFNumberCreate(default_allocator, kCFNumberIntType, &feat_name);
CFNumberRef value = CFNumberCreate(default_allocator, kCFNumberIntType, &feat_value);

descriptor = CTFontDescriptorCreateCopyWithFeature(descriptor, name, value);

The corresponding way to do this now is:

# one feature
sketchybar --set item label.font.features="6:0"
# multiple features
sketchybar --set item label.font.features="6:0,37:1"

I'm not sure if there is a nicer way of doing this though

Closes #670
In the above issue, the corresponding features string would be "14:4" (kTypographicExtras, kSlashedZeroOnSelector)

Related: #187

src/font.c Outdated
CFNumberRef name = CFNumberCreate(default_allocator, kCFNumberIntType, &feat_name);
CFNumberRef value = CFNumberCreate(default_allocator, kCFNumberIntType, &feat_value);

descriptor = CTFontDescriptorCreateCopyWithFeature(descriptor, name, value);
Copy link
Owner

Choose a reason for hiding this comment

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

I believe this causes a memory leak, because it copies the existing descriptor and then overwrites the pointer.

@FelixKratz
Copy link
Owner

FelixKratz commented Feb 20, 2025

What we could think about would be to accept both the format you introduced, which is the „low-level“ syntax along with some common features which we give a sensible selector name and internally do the mapping for. What do you think?

@erics118
Copy link
Author

I hesitate to use our own namings of these features, as these selectors are very standardized.

What about just manually creating a mapping from opentype features to truetype features? It would just include the most common few features, as there are way too many. This way, users could use some of the opentype names (as i believe opentype is much more prevalent outside of apple)

ie, some sort of mapping like this:

struct {
    char opentype_tag[5];
    int truetype_feature;
    int truetype_selector;
};
// an array of:
{"pnum", kNumberSpacingType, kProportionalNumbersSelector}, 
{"tnum", kNumberSpacingType, kMonospacedNumbersSelector}, 
{"smcp", kLowerCaseType, kLowerCaseSmallCapsSelector},
{"onum", kNumberCaseType, kLowerCaseNumbersSelector},

and then, accept stuff like the CSS font-feature-settings, with something like

sketchybar --set item label.font.features="tnum,ss07,ss13"

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.

Support for OpenType Font features / stylistic set
2 participants