Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a character is supported by a font

I'm working on an app with a text field. The text wrote in this field will be printed and I have an issue with some characters like emoji, chinese characters, etc... because the font do not provide these characters.

It's why I want to get all the character provided by a font (The font is downloaded so I can deal directly with the file or with an UIFont object).

I heard about CTFontGetGlyphsForCharacters but I'm not sure that this function do what I want and I can't get it work.

Here is my code :

CTFontRef fontRef = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL);
NSString *characters = @"🐯"; // emoji character
NSUInteger count = characters.length;
CGGlyph glyphs[count];
if (CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[characters cStringUsingEncoding:NSUTF8StringEncoding], glyphs, count) == false)
    NSLog(@"CTFontGetGlyphsForCharacters failed.");

Here CTFontGetGlyphsForCharacters return false. It's what I want because the character '🐯' is not provided by the font used.
The problem is when I replace NSString *characters = @"🐯" by NSString *characters = @"abc", CTFontGetGlyphsForCharacters return again false. Obviously, my font provide a glyph for all the ASCII characters.

like image 220
Morniak Avatar asked Apr 17 '14 14:04

Morniak


People also ask

How can I tell if a font contains a character?

Try pasting the copied text/symbol into a blank Microsoft Word doc. The content should appear properly if Word is set to Keep Source Formatting by default for pasted text. If so, select the content and the Word font menu will show you the source font on your computer that contains the necessary character.

How can I tell if a font is Unicode?

Right-click your font and select Properties . Select the tab "CharSet/Unicode". If the Font Encoding Type is not Symbol and the Supported Unicode Ranges list anything besides or in addition to Basic Latin and Latin-1 Supplement, your font is a Unicode font or is compatible with Unicode.

What is the range of Truetype font?

Each point must be within the range -16384 and +16383 FUnits.


1 Answers

I finally solve it :

- (BOOL)isCharacter:(unichar)character supportedByFont:(UIFont *)aFont
{
    UniChar characters[] = { character };
    CGGlyph glyphs[1] = { };
    CTFontRef ctFont = CTFontCreateWithName((CFStringRef)aFont.fontName, aFont.pointSize, NULL);
    BOOL ret = CTFontGetGlyphsForCharacters(ctFont, characters, glyphs, 1);
    CFRelease(ctFont);
    return ret;
}
like image 161
Morniak Avatar answered Sep 26 '22 21:09

Morniak