Skip to content

Conversation

@typemytype
Copy link
Owner

fixing #584

@justvanrossum
Copy link
Collaborator

Alternatively, we could try to use CTFontDescriptorCreateCopyWithAttributes to set the url on the descriptors, after this line:

descriptors = CoreText.CTFontManagerCreateFontDescriptorsFromData(data)

@typemytype typemytype changed the title remove FontTools to list named instances [WIP] remove FontTools to list named instances May 20, 2025
@typemytype typemytype changed the title [WIP] remove FontTools to list named instances remove FontTools to list named instances May 20, 2025
@typemytype
Copy link
Owner Author

I tried that but did not succeed to add the url in a font descriptor... some attributes are lost while converting from CTFonts to fontDescriptors:

# run the script once
# edit the font on disk (different mod time is enough) 

path = 'pathToFont.ttf'


from drawBot.context.baseContext import getFontDescriptorsFromPath
import CoreText

descriptors = getFontDescriptorsFromPath(path)

url = CoreText.CTFontDescriptorCopyAttribute(descriptors[0], CoreText.kCTFontURLAttribute)
print(url)

# convert to CTFont
ctfont = CoreText.CTFontCreateWithFontDescriptor(descriptors[0], 10, None)

# convert back to a font descriptor
url = CoreText.CTFontDescriptorCopyAttribute(ctfont.fontDescriptor(), CoreText.kCTFontURLAttribute)
print(url)

@justvanrossum
Copy link
Collaborator

That's not what I see happening with this snippet. On the first run it prints the path in both cases, and when I change the font, it prints None for both. Or did you mean I should run this in your branch?

@justvanrossum
Copy link
Collaborator

Ok, after poking around, I see what you mean. Odd!

@typemytype
Copy link
Owner Author

a full example without using the caching DrawBot provides:

import AppKit
import CoreText

path = 'pathToFont.ttf'
# create font descriptors from data, so no url available
data = AppKit.NSData.dataWithContentsOfFile_(path)
descriptors = CoreText.CTFontManagerCreateFontDescriptorsFromData(data)

attributes = {
    CoreText.kCTFontURLAttribute: AppKit.NSURL.fileURLWithPath_(path)
}
# make a copy with adding attributes
descriptors = [CoreText.CTFontDescriptorCreateCopyWithAttributes(descriptor, attributes) for descriptor in descriptors]
# test attributes
url = CoreText.CTFontDescriptorCopyAttribute(descriptors[0], CoreText.kCTFontURLAttribute)
print(url)

# convert to CTFont
ctfont = CoreText.CTFontCreateWithFontDescriptor(descriptors[0], 10, None)

# convert back to a font descriptor
url = CoreText.CTFontDescriptorCopyAttribute(ctfont.fontDescriptor(), CoreText.kCTFontURLAttribute)
# url is gone
print(url)

path = fontNameOrPath

descriptors = getFontDescriptorsFromPath(path)
return variation.getNamedInstancesForDescriptors(descriptors)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We're somehow ignoring fontNumber here.

Yet I don't know what to do, since CTFontManagerCreateFontDescriptorsFromData() does not (can not?) distinguish between "multiple instances in a VF", "multiple statics in a TTC" or even "multiple instances for multiple VFs in a TTC".

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.

3 participants