Skip to content

Instantly share code, notes, and snippets.

@steveatinfincia
Last active August 29, 2015 14:07
Show Gist options
  • Save steveatinfincia/38aa73fed2b11d80efd3 to your computer and use it in GitHub Desktop.
Save steveatinfincia/38aa73fed2b11d80efd3 to your computer and use it in GitHub Desktop.
Font detection in Codepoints
NSMutableAttributedString *displayedUnicodeString = [[NSMutableAttributedString alloc] initWithString:unicodeCharacter attributes:fontattributes];
[glyphView.character.textStorage setAttributedString:displayedUnicodeString];
// determine which font will actually be used by the character textview during rendering
NSFont *actualFont = [glyphView.character.textStorage attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL];
// if it matches the users selected font, that font has a glyph for this character
BOOL characterFoundInFont = [selectedFont.fontName isEqualToString:actualFont.fontName];
if (characterFoundInFont || useFallbackBehavior) {
// never allow the lastresort font to display glyphs, they aren't what the user wants
if ([actualFont.displayName isEqualToString:@"LastResort"]) {
glyphView.type = RowTypeGlyphInvalid;
glyphView.character.string = @"";
[glyphView.character setToolTip:@"This character was only found in the LastResort font, no accurate glyph is available"];
}
// character has no glyph at all because it is a control character, like backspace or carriage return
else if (controlCharacter) {
glyphView.type = RowTypeInvisible;
[glyphView.character setToolTip:@"This is a control or invisible character with no glyph"];
}
// font glyph available
else {
glyphView.type = RowTypeGlyphFound;
[glyphView.character setToolTip:[NSString stringWithFormat:@"This character is being rendered with the font: %@", actualFont.displayName]];
}
}
// fallback behavior is disabled and the users selected font can't render the glyph, so display nothing
else {
glyphView.type = RowTypeGlyphNotFound;
glyphView.character.string = @"";
[glyphView.character setToolTip:@"This character was not found in the selected font"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment