Glitch dynamically loading Merriweather fonts
Closed this issue · 2 comments
I stumbled on an issue where the Merriweather-Regular font is not dynamically loaded if another variant of Merriweather was dynamically loaded before it. You can reproduce this behavior by dynamically registering either Merriweather-Bold, Merriweather-Italic, or Merriweather-Light, then attempting to dynamically register Merriweather-Regular. If you set breakpoints you'll notice the code that performs the dynamic load is skipped and one of the previously registered fonts is returned instead. It appears that the last of the previously registered fonts is the one used but I didn't test this as thoroughly as I'd want to say its for certain.
The Open Sans fonts do not have this behavior.
My hunch is this happens with the Merriweather font because the Merriweather-Regular font has a font name of "Merriweather" which is the same as the font family. The theory is when calling [UIFont fontWithName:size:]
, UIFont sees that the value of fontName
matches a font family in the font table, and returns the last variant registered. Any ideas on how to confirm or disprove this?
Other observations:
I tried adjusting the conditional in [WPFontManager fontNamed:resourceName:fontType:size:]
to check that the initialized font's name matched the fontName requested. If there was a mismatch it would proceed to dynamically load the requested font.
if (!font || ![fontName isEqualToString:font.fontName]) {
This resulted in an interesting change of behavior. After first dynamically loading one of the other Merriweather fonts I could then dynamically load and use the regular font if it was a different size. However, if the size was the same as the previously loaded font variant, it would return that variant instead.
I also tried dynamically loading the fonts via CTFontManagerRegisterFontsForURL
instead of CTFontManagerRegisterGraphicsFont
. While this change made for less code it didn't solve the original issue.
Workaround:
It seems like the glitch can be avoided by loading the Merriweather-Regular font before any of the variants are loaded. So far I haven't seen any cases where the wrong variant is subsequently returned.
Let's reopen if I'm wrong :P