Skip to content

Instantly share code, notes, and snippets.

@andlabs
Forked from chearon/whatsthefont.c
Last active January 3, 2017 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andlabs/253a5b712959cdbb5a8ccc9bb555b823 to your computer and use it in GitHub Desktop.
Save andlabs/253a5b712959cdbb5a8ccc9bb555b823 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>
int
main(int argc, char** argv)
{
if (argc == 2) {
char* filepath = *(argv+1);
CFStringRef cffilepath = CFStringCreateWithCString(NULL, filepath, kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, cffilepath, kCFURLPOSIXPathStyle, false);
CFArrayRef descs = CTFontManagerCreateFontDescriptorsFromURL(url);
CTFontDescriptorRef desc = (CTFontDescriptorRef) CFArrayGetValueAtIndex(descs, 0);
CFDictionaryRef dict = CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute);
CFNumberRef cfnumber = (CFNumberRef)CFDictionaryGetValue(dict, kCTFontSymbolicTrait);
double value;
CTFontSymbolicTraits traitsval;
if (cfnumber != NULL && CFNumberGetValue(cfnumber, kCFNumberSInt32Type, &traitsval)) {
CFNumberGetValue(cfnumber, kCFNumberFloat64Type, &value);
printf("traits 0x%X ", traitsval);
} else printf("traits N/A ");
cfnumber = (CFNumberRef)CFDictionaryGetValue(dict, kCTFontSlantTrait);
if (cfnumber != NULL && CFNumberGetValue(cfnumber, kCFNumberCGFloatType, &value)) {
CFNumberGetValue(cfnumber, kCFNumberFloat64Type, &value);
printf("slant %f ", value);
} else printf("slant N/A ");
printf("%s\n", filepath);
}
}
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <CoreText/CoreText.h>
int
main(int argc, char** argv)
{
if (argc == 2) {
char* filepath = *(argv+1);
CFStringRef cffilepath = CFStringCreateWithCString(NULL, filepath, kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, cffilepath, kCFURLPOSIXPathStyle, false);
CFArrayRef descs = CTFontManagerCreateFontDescriptorsFromURL(url);
CTFontDescriptorRef desc = (CTFontDescriptorRef) CFArrayGetValueAtIndex(descs, 0);
CFDictionaryRef dict = CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute);
CFNumberRef cfnumber = (CFNumberRef)CFDictionaryGetValue(dict, kCTFontWidthTrait);
double value;
if (cfnumber != NULL && CFNumberGetValue(cfnumber, kCFNumberCGFloatType, &value)) {
CFNumberGetValue(cfnumber, kCFNumberFloat64Type, &value);
printf("%f %s\n", value, filepath);
} else printf("N/A %s\n", filepath);
}
}
#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);
CFArrayRef descs = CTFontManagerCreateFontDescriptorsFromURL(url);
CTFontDescriptorRef desc = (CTFontDescriptorRef) CFArrayGetValueAtIndex(descs, 0);
CFDictionaryRef dict = CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute);
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);
}
}
}
}
#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)
{
char b[100];
printf("%d\n",getpid());
read(0,b,100);
const char* filepath = "/Library/Fonts/Impact.ttf";
FT_Library library;
FT_Face face;
CFStringRef cffilepath = CFStringCreateWithCString(NULL, filepath, kCFStringEncodingUTF8);
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, cffilepath, kCFURLPOSIXPathStyle, false);
CFArrayRef descs = CTFontManagerCreateFontDescriptorsFromURL(url);
CTFontDescriptorRef desc = (CTFontDescriptorRef) CFArrayGetValueAtIndex(descs, 0);
CFDictionaryRef dict = CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute);
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);
}
}
}
// modified by pietro gagliardi 23 december 2016
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <CoreText/CoreText.h>
char filepath[1024];
int
main(int argc, char** argv)
{
CTFontCollectionRef fc;
CFArrayRef descs;
CFIndex i, n;
fc = CTFontCollectionCreateFromAvailableFonts(NULL);
descs = CTFontCollectionCreateMatchingFontDescriptors(fc);
n = CFArrayGetCount(descs);
for (i = 0; i < n; i++) {
CTFontDescriptorRef desc;
CFNumberRef cfnumber;
desc = (CTFontDescriptorRef) CFArrayGetValueAtIndex(descs, i);
CFStringRef cffilepath = (CFStringRef) CTFontDescriptorCopyAttribute(desc, kCTFontDisplayNameAttribute);
CFStringGetCString(cffilepath, filepath, 1024, kCFStringEncodingUTF8);
CFDictionaryRef dict = (CFDictionaryRef) CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute);
cfnumber = (CFNumberRef)CFDictionaryGetValue(dict, kCTFontSymbolicTrait);
double value;
CTFontSymbolicTraits traitsval;
if (cfnumber != NULL && CFNumberGetValue(cfnumber, kCFNumberSInt32Type, &traitsval)) {
CFNumberGetValue(cfnumber, kCFNumberFloat64Type, &value);
printf("traits 0x%X ", traitsval);
} else printf("traits N/A ");
cfnumber = (CFNumberRef)CFDictionaryGetValue(dict, kCTFontSlantTrait);
if (cfnumber != NULL && CFNumberGetValue(cfnumber, kCFNumberCGFloatType, &value)) {
CFNumberGetValue(cfnumber, kCFNumberFloat64Type, &value);
printf("slant %f ", value);
} else printf("slant N/A ");
printf("%s\n", filepath);
}
return 0;
}
// modified by pietro gagliardi 23 december 2016
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <CoreText/CoreText.h>
char filepath[1024];
int
main(int argc, char** argv)
{
CTFontCollectionRef fc;
CFArrayRef descs;
CFIndex i, n;
fc = CTFontCollectionCreateFromAvailableFonts(NULL);
descs = CTFontCollectionCreateMatchingFontDescriptors(fc);
n = CFArrayGetCount(descs);
for (i = 0; i < n; i++) {
CTFontDescriptorRef desc;
CFNumberRef cfnumber;
desc = (CTFontDescriptorRef) CFArrayGetValueAtIndex(descs, i);
CFStringRef cffilepath = (CFStringRef) CTFontDescriptorCopyAttribute(desc, kCTFontDisplayNameAttribute);
CFStringGetCString(cffilepath, filepath, 1024, kCFStringEncodingUTF8);
CFDictionaryRef dict = (CFDictionaryRef) CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute);
cfnumber = (CFNumberRef)CFDictionaryGetValue(dict, kCTFontWeightTrait);
double value;
if (cfnumber != NULL && CFNumberGetValue(cfnumber, kCFNumberCGFloatType, &value)) {
CFNumberGetValue(cfnumber, kCFNumberFloat64Type, &value);
printf("weight %f ", value);
} else printf("weight N/A ");
cfnumber = (CFNumberRef)CFDictionaryGetValue(dict, kCTFontWidthTrait);
if (cfnumber != NULL && CFNumberGetValue(cfnumber, kCFNumberCGFloatType, &value)) {
CFNumberGetValue(cfnumber, kCFNumberFloat64Type, &value);
printf("width %f ", value);
} else printf("width N/A ");
printf("%s\n", filepath);
}
return 0;
}
// modified by pietro gagliardi 23 december 2016
#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
char filepath[1024];
int
main(int argc, char** argv)
{
CTFontCollectionRef fc;
CFArrayRef descs;
CFIndex i, n;
fc = CTFontCollectionCreateFromAvailableFonts(NULL);
descs = CTFontCollectionCreateMatchingFontDescriptors(fc);
n = CFArrayGetCount(descs);
for (i = 0; i < n; i++) {
CTFontDescriptorRef desc;
desc = (CTFontDescriptorRef) CFArrayGetValueAtIndex(descs, i);
FT_Library library;
FT_Face face;
CFNumberRef cfnumber = (CFNumberRef) CTFontDescriptorCopyAttribute(desc, kCTFontFormatAttribute);
UInt32 fmt;
CFNumberGetValue(cfnumber, kCFNumberSInt32Type, &fmt);
switch (fmt) {
case kCTFontFormatTrueType:
case kCTFontFormatOpenTypeTrueType:
break;
default:
continue;
}
CFURLRef url = (CFURLRef) CTFontDescriptorCopyAttribute(desc, kCTFontURLAttribute);
CFStringRef cffilepath = CFURLCopyPath(url);
CFStringGetCString(cffilepath, filepath, 1024, kCFStringEncodingUTF8);
CFDictionaryRef dict = (CFDictionaryRef) CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute);
cfnumber = (CFNumberRef)CFDictionaryGetValue(dict, kCTFontWeightTrait);
double value;
CTFontRef font = CTFontCreateWithFontDescriptor(desc, 0, NULL);
CFDataRef cftable = CTFontCopyTable(font, kCTFontTableOS2, 0);
if (cfnumber != NULL && CFNumberGetValue(cfnumber, kCFNumberCGFloatType, &value)) {
CFNumberGetValue(cfnumber, kCFNumberFloat64Type, &value);
if (cftable != NULL) {
TT_OS2* table = (TT_OS2*)CFDataGetBytePtr(cftable);
if (table != NULL)
printf("%u %f %s\n", CFSwapInt16BigToHost(table->usWeightClass), value, filepath);
else
printf("N/A %f %s\n", value, filepath);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment