Re: Font matching
Re: Font matching
- Subject: Re: Font matching
- From: Benjamin Stiglitz <email@hidden>
- Date: Mon, 6 Jul 2009 10:42:13 -0400
- Mail-followup-to: email@hidden
> Hi, I have an application where I need to be able to find the closest
> equivalent font should a font be missing. e.g. someone creates a file
> and sends it to someone else, who doesn't have the font that was used to
> create the original file installed on their system.
>
> I thought that maybe saving the NSFontDescriptor in the file should be
> able to allow me to find the best font using the
> matchingFontDescriptorsWithMandatoryKeys method passing nil as the
> mandatory keys
That’s pretty much exactly the intention of the descriptor. Since
NSFontDescriptor is toll-free bridged, you’ll find that many of these
questions are answered in the CTFontDescriptor documentation.
> I have no idea what it would do if the font was either there or
> missing:
> - if the font was there, would it return just one font descriptor, or
> potentially multiple?
"If descriptor itself is normalized, then the array will contain only
one item: the original descriptor. In the context of font descriptors,
normalized infers that the input values were matched up with actual
existing fonts, and the descriptors for those existing fonts are the
returned normalized descriptors."
> - if it was able to be exactly matched, is there some way of knowing
> this (e.g. is isEqual: implemented on NSFontDescriptor?)
You can compare the -fontAttributes dictionaries.
> - if the font is not there, will it continue to fall back until it finds
> a suitable font? If so, will it return only one item in the array? What
> does it try to match in the algorithm?
It will fall back to the system font, yes. If there are mandatory keys
there may not be any matching font. The algorithm tries to match as many
keys as it can when finding a suitable font.
> - if it does return multiple descriptors, are they ordered so that I
> know which one is considered the best match?
There is an ordering, but it's not likely to help you. The
-fontAttributes dictionary will come in handy for sorting by relevance.
> - if I pass in nil as the mandatory keys, will it *always* return at
> least one descriptor?
Yes.
> Now sometimes my file is edited by someone using windoze, and the font
> name doesn't exactly match what you see on the Mac even though it is
> essentially the same font and usually the name of one is a substring of
> the other. Is there an elegant way to handle this?
If the name isn’t a mandatory key you can try a binary search of the
result list (sorted by name) and use a heuristic of your choosing to
decide whether the font matches. You may want to instantiate a font for
the descriptor and compare the display and family names as well as the
standard (PostScript) name.
If you have more questions you’re best off heading to the coretext-dev
list, where the ATS and CoreText engineers are more likely to hang out.
-Ben
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden