Ticket #17049: test_coretext.m

File test_coretext.m, 1.7 KB (added by pguyot (Paul Guyot), 16 years ago)

Simple test program that checks ascent, descent, underline position and thickness for ATS and CoreText APIs. They provide the same values.

Line 
1#import <Cocoa/Cocoa.h>
2#include <stdio.h>
3
4int main(int argc, char** argv) {
5    if (argc != 3) {
6        printf("Syntax error. %s fontname size\n", argv[0]);
7        return 1;
8    }
9    const char* font_name = argv[1];
10    int size = atoi(argv[2]);
11   
12    CFStringRef cfstr = CFStringCreateWithCString (NULL, font_name, kCFStringEncodingUTF8);
13    ATSFontRef ats_font = ATSFontFindFromPostScriptName(cfstr, kATSOptionFlagsDefault);
14    CFRelease(cfstr);
15    if (ats_font == kATSFontRefUnspecified) {
16        printf("Undefined font %s\n", font_name);
17        return 1;
18    }
19   
20    ATSFontMetrics ats_metrics;
21    ATSFontGetHorizontalMetrics(ats_font, kATSOptionFlagsDefault, &ats_metrics);
22
23    CGFontRef cg_font = CGFontCreateWithPlatformFont (&ats_font);
24    CTFontRef ct_font = CTFontCreateWithGraphicsFont(cg_font, size, NULL, NULL);
25    double ats_ascent = ats_metrics.ascent * size;
26    double ct_ascent = CTFontGetAscent(ct_font);
27    double ats_descent = ats_metrics.descent * size;
28    double ct_descent = -CTFontGetDescent(ct_font);
29    double ats_underline_pos = ats_metrics.underlinePosition * size;
30    double ct_underline_pos = CTFontGetUnderlinePosition(ct_font);
31    double ats_underline_thickness = ats_metrics.underlineThickness * size;
32    double ct_underline_thickness = CTFontGetUnderlineThickness(ct_font);
33
34    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);
35    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);
36   
37    return 0;
38}