Skip to content

Instantly share code, notes, and snippets.

@chearon
Last active December 23, 2016 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chearon/4e3042d5d752a8eaa1a1ea6fecc98010 to your computer and use it in GitHub Desktop.
Save chearon/4e3042d5d752a8eaa1a1ea6fecc98010 to your computer and use it in GitHub Desktop.
Compare FT and OSX font weights for a font
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <CoreText/CoreText.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_TRUETYPE_TABLES_H
#include FT_SFNT_NAMES_H
#include FT_TRUETYPE_IDS_H
#ifndef FT_SFNT_OS2
#define FT_SFNT_OS2 ft_sfnt_os2
#endif
int
main(int argc, char** argv)
{
if (argc == 2) {
char* filepath = *(argv+1);
FT_Library library;
FT_Face face;
CFStringRef cffilepath = CFStringCreateWithCString(NULL, filepath, kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, cffilepath, kCFURLPOSIXPathStyle, false);
CGDataProviderRef data = CGDataProviderCreateWithURL(url);
CGFontRef cgfont = CGFontCreateWithDataProvider(data);
CTFontRef ctfont = CTFontCreateWithGraphicsFont(cgfont, 0.0, NULL, NULL);
CFDictionaryRef dict = CTFontCopyTraits(ctfont);
CFNumberRef cfnumber = (CFNumberRef)CFDictionaryGetValue(dict, kCTFontWeightTrait);
double value;
if (cfnumber != NULL && CFNumberGetValue(cfnumber, kCFNumberCGFloatType, &value)) {
CFNumberGetValue(cfnumber, kCFNumberFloat64Type, &value);
if (!FT_Init_FreeType(&library) && !FT_New_Face(library, (const char*)filepath, 0, &face)) {
TT_OS2* table = (TT_OS2*)FT_Get_Sfnt_Table(face, FT_SFNT_OS2);
printf("%u %f\n", table->usWeightClass, value);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment