summaryrefslogtreecommitdiffstats
path: root/libass/ass_coretext.c
blob: 75a55145d4db6a7ee854b98eab3437c34ad1f5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (C) 2013 Stefano Pigozzi <stefano.pigozzi@gmail.com>
 *
 * This file is part of libass.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "config.h"

#ifdef CONFIG_CORETEXT

#include <CoreFoundation/CoreFoundation.h>
#include <CoreText/CoreText.h>

#include "ass_coretext.h"

#define CT_FONTS_EAGER_LOAD 0
#define CT_FONTS_LAZY_LOAD  !CT_FONTS_EAGER_LOAD

static char *cfstr2buf(CFStringRef string)
{
    const char *buf_ptr = CFStringGetCStringPtr(string, kCFStringEncodingUTF8);
    if (buf_ptr) {
        return strdup(buf_ptr);
    } else {
        size_t buf_len = CFStringGetLength(string) + 1;
        char *buf = malloc(buf_len);
        CFStringGetCString(string, buf, buf_len, kCFStringEncodingUTF8);
        return buf;
    }
}

static void destroy_font(void *priv)
{
    CFCharacterSetRef set = priv;
    CFRelease(set);
}

static int check_glyph(void *priv, uint32_t code)
{
    CFCharacterSetRef set = priv;

    if (!set)
        return 1;

    if (code == 0)
        return 1;

    return CFCharacterSetIsLongCharacterMember(set, code);
}

static char *get_font_file(CTFontDescriptorRef fontd)
{
    CFURLRef url = CTFontDescriptorCopyAttribute(fontd, kCTFontURLAttribute);
    CFStringRef path = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
    char *buffer = cfstr2buf(path);
    CFRelease(path);
    CFRelease(url);
    return buffer;
}

static void get_name(CTFontDescriptorRef fontd, CFStringRef attr,
                     char **array, int *idx)
{

    CFStringRef name = CTFontDescriptorCopyAttribute(fontd, attr);
    if (name) {
        array[*idx] = cfstr2buf(name);
        CFRelease(name);
        *idx += 1;
    }
}

static void get_trait(CFDictionaryRef traits, CFStringRef attribute,
                      float *trait)
{
    CFNumberRef cftrait = CFDictionaryGetValue(traits, attribute);
    if (!CFNumberGetValue(cftrait, kCFNumberFloatType, trait))
        *trait = 0.0;
}

static void get_font_traits(CTFontDescriptorRef fontd,
                            ASS_FontProviderMetaData *meta)
{
    float weight, slant, width;

    CFDictionaryRef traits =
        CTFontDescriptorCopyAttribute(fontd, kCTFontTraitsAttribute);

    get_trait(traits, kCTFontWeightTrait, &weight);
    get_trait(traits, kCTFontSlantTrait,  &slant);
    get_trait(traits, kCTFontWidthTrait,  &width);

    CFRelease(traits);

    // Printed all of my system fonts (see if'deffed code below). Here is how
    // CoreText 'normalized' weights maps to CSS/libass:

    // opentype:   0   100   200   300   400   500   600   700   800   900
    // css:                 LIGHT        REG   MED  SBOLD BOLD  BLACK  EXTRABL
    // libass:                   LIGHT  MEDIUM            BOLD
    // coretext:            -0.4         0.0   0.23  0.3   0.4   0.62

    if (weight >= 0.62)
        meta->weight = 800;
    else if (weight >= 0.4)
        meta->weight = 700;
    else if (weight >= 0.3)
        meta->weight = 600;
    else if (weight >= 0.23)
        meta->weight = 500;
    else if (weight >= -0.4)
        meta->weight = 400;
    else
        meta->weight = 200;

    if (slant > 0.03)
        meta->slant  = FONT_SLANT_ITALIC;
    else
        meta->slant  = FONT_SLANT_NONE;

    if (width <= -0.2)
        meta->width = FONT_WIDTH_CONDENSED;
    else if (width >= 0.2)
        meta->width = FONT_WIDTH_EXPANDED;
    else
        meta->width  = FONT_WIDTH_NORMAL;

#if 0
    char *name[1];
    int idx = 0;
    get_name(fontd, kCTFontDisplayNameAttribute, name, &idx);
    char *file = get_font_file(fontd);
    printf(
       "Font traits for: %-40s [%-50s] "
       "<slant: %f, %03d>, <weight: (%f, %03d)>, <width: %f, %03d>\n",
       name[0], file,
       slant, meta->slant, weight, meta->weight, width, meta->width);
    free(name[0]);
    free(file);
#endif
}

static void process_descriptors(ASS_FontProvider *provider, CFArrayRef fontsd)
{
    ASS_FontProviderMetaData meta;
    char *families[1];
    char *identifiers[1];
    char *fullnames[1];

    if (!fontsd)
        return;

    for (int i = 0; i < CFArrayGetCount(fontsd); i++) {
        CTFontDescriptorRef fontd = CFArrayGetValueAtIndex(fontsd, i);
        int index = -1;

        char *path = get_font_file(fontd);
        if (strcmp("", path) == 0) {
            // skip the font if the URL field in the font descriptor is empty
            free(path);
            continue;
        }

        memset(&meta, 0, sizeof(meta));
        get_font_traits(fontd, &meta);

        get_name(fontd, kCTFontFamilyNameAttribute, families, &meta.n_family);
        meta.families = families;

        int zero = 0;
        get_name(fontd, kCTFontNameAttribute, identifiers, &zero);
        get_name(fontd, kCTFontDisplayNameAttribute, fullnames, &meta.n_fullname);
        meta.fullnames = fullnames;

        CFCharacterSetRef chset =
            CTFontDescriptorCopyAttribute(fontd, kCTFontCharacterSetAttribute);
        ass_font_provider_add_font(provider, &meta, path, index,
                                   identifiers[0], (void*)chset);

        for (int j = 0; j < meta.n_family; j++)
            free(meta.families[j]);

        for (int j = 0; j < meta.n_fullname; j++)
            free(meta.fullnames[j]);

        free(identifiers[0]);

        free(path);
    }
}

#if CT_FONTS_EAGER_LOAD
static void scan_fonts(ASS_Library *lib, ASS_FontProvider *provider)
{

    CTFontCollectionRef coll = CTFontCollectionCreateFromAvailableFonts(NULL);
    CFArrayRef fontsd = CTFontCollectionCreateMatchingFontDescriptors(coll);

    process_descriptors(provider, fontsd);

    CFRelease(fontsd);
    CFRelease(coll);
}
#endif

#if CT_FONTS_LAZY_LOAD
static void match_fonts(ASS_Library *lib, ASS_FontProvider *provider,
                        char *name)
{
    void *descr_ary[1];

    CFStringRef cfname =
        CFStringCreateWithCString(NULL, name, kCFStringEncodingUTF8);
    CFMutableDictionaryRef cfattrs = CFDictionaryCreateMutable(NULL, 0, 0, 0);
    CFDictionaryAddValue(cfattrs, kCTFontDisplayNameAttribute, cfname);
    CTFontDescriptorRef ctdescr = CTFontDescriptorCreateWithAttributes(cfattrs);

    descr_ary[0] = (void *)ctdescr;
    CFArrayRef descriptors =
        CFArrayCreate(NULL, (const void **)&descr_ary, 1, NULL);

    CTFontCollectionRef ctcoll =
        CTFontCollectionCreateWithFontDescriptors(descriptors, 0);

    CFArrayRef fontsd =
        CTFontCollectionCreateMatchingFontDescriptors(ctcoll);

    process_descriptors(provider, fontsd);

    if (fontsd)
        CFRelease(fontsd);
    CFRelease(ctcoll);
    CFRelease(cfattrs);
    CFRelease(ctdescr);
    CFRelease(descriptors);
    CFRelease(cfname);
}
#endif

static ASS_FontProviderFuncs coretext_callbacks = {
    NULL,
    check_glyph,
    destroy_font,
    NULL,
#if CT_FONTS_EAGER_LOAD
    NULL
#else
    match_fonts
#endif
};

ASS_FontProvider *
ass_coretext_add_provider(ASS_Library *lib, ASS_FontSelector *selector)
{
    ASS_FontProvider *provider =
        ass_font_provider_new(selector, &coretext_callbacks, NULL);

#if CT_FONTS_EAGER_LOAD
    scan_fonts(lib, provider);
#endif

    return provider;
}

#endif