#import <Cocoa/Cocoa.h>
#include <stdio.h>

int main(int argc, char** argv) {
    if (argc != 3) {
        printf("Syntax error. %s fontname size\n", argv[0]);
        return 1;
    }
    const char* font_name = argv[1];
    int size = atoi(argv[2]);
    
    CFStringRef cfstr = CFStringCreateWithCString (NULL, font_name, kCFStringEncodingUTF8);
    ATSFontRef ats_font = ATSFontFindFromPostScriptName(cfstr, kATSOptionFlagsDefault);
    CFRelease(cfstr);
    if (ats_font == kATSFontRefUnspecified) {
        printf("Undefined font %s\n", font_name);
        return 1;
    }
    
    ATSFontMetrics ats_metrics;
    ATSFontGetHorizontalMetrics(ats_font, kATSOptionFlagsDefault, &ats_metrics);

    CGFontRef cg_font = CGFontCreateWithPlatformFont (&ats_font);
    CTFontRef ct_font = CTFontCreateWithGraphicsFont(cg_font, size, NULL, NULL);
    double ats_ascent = ats_metrics.ascent * size;
    double ct_ascent = CTFontGetAscent(ct_font);
    double ats_descent = ats_metrics.descent * size;
    double ct_descent = -CTFontGetDescent(ct_font);
    double ats_underline_pos = ats_metrics.underlinePosition * size;
    double ct_underline_pos = CTFontGetUnderlinePosition(ct_font);
    double ats_underline_thickness = ats_metrics.underlineThickness * size;
    double ct_underline_thickness = CTFontGetUnderlineThickness(ct_font);

    printf("size = %i, ats_ascent = %f, ct_ascent = %f, ats_descent = %f, ct_descent = %f\n", size, ats_ascent, ct_ascent, ats_descent, ct_descent);
    printf("ats_underline_pos = %f, ct_underline_pos = %f, ats_underline_thickness = %f, ct_underline_thickness = %f\n", ats_underline_pos, ct_underline_pos, ats_underline_thickness, ct_underline_thickness);
    
    return 0;
}
