aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/SkWhitelistTypefaces.cpp
blob: f51a3b3c24e998d723e25ed88782bd6ecaad0947 (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
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkFontDescriptor.h"
#include "SkOpts.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkTypeface.h"
#include "SkUtils.h"
#include "../sfnt/SkOTUtils.h"

#include "SkWhitelistChecksums.inc"

#define WHITELIST_DEBUG 0

extern void WhitelistSerializeTypeface(const SkTypeface*, SkWStream* );
sk_sp<SkTypeface> WhitelistDeserializeTypeface(SkStream* );
extern bool CheckChecksums();
extern bool GenerateChecksums();

#if WHITELIST_DEBUG
static bool timesNewRomanSerializedNameOnly = false;
#endif

#define SUBNAME_PREFIX "sk_"

static bool font_name_is_local(const char* fontName, SkFontStyle style) {
    if (!strcmp(fontName, "DejaVu Sans")) {
        return true;
    }
    sk_sp<SkTypeface> defaultFace(SkTypeface::MakeFromName(nullptr, style));
    sk_sp<SkTypeface> foundFace(SkTypeface::MakeFromName(fontName, style));
    return defaultFace != foundFace;
}

static int whitelist_name_index(const SkTypeface* tf) {

    SkString fontNameStr;
    sk_sp<SkTypeface::LocalizedStrings> nameIter(
        SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*tf));
    SkTypeface::LocalizedString familyNameLocalized;
    while (nameIter->next(&familyNameLocalized)) {
        fontNameStr = familyNameLocalized.fString;
        // check against permissible list of names
        for (int i = 0; i < whitelistCount; ++i) {
            if (fontNameStr.equals(whitelist[i].fFontName)) {
                return i;
            }
        }
    }
#if WHITELIST_DEBUG
    sk_sp<SkTypeface::LocalizedStrings> debugIter(
        SkOTUtils::LocalizedStrings_NameTable::CreateForFamilyNames(*tf));
    while (debugIter->next(&familyNameLocalized)) {
        SkDebugf("no match fontName=\"%s\"\n", familyNameLocalized.fString.c_str());
    }
#endif
    return -1;
}

static uint32_t compute_checksum(const SkTypeface* tf) {
    std::unique_ptr<SkFontData> fontData = tf->makeFontData();
    if (!fontData) {
        return 0;
    }
    SkStreamAsset* fontStream = fontData->getStream();
    if (!fontStream) {
        return 0;
    }
    SkTDArray<char> data;
    size_t length = fontStream->getLength();
    if (!length) {
        return 0;
    }
    data.setCount((int) length);
    if (!fontStream->peek(data.begin(), length)) {
        return 0;
    }
    return SkOpts::hash(data.begin(), length);
}

static void serialize_sub(const char* fontName, SkFontStyle style, SkWStream* wstream) {
    SkFontDescriptor desc;
    SkString subName(SUBNAME_PREFIX);
    subName.append(fontName);
    const char* familyName = subName.c_str();
    desc.setFamilyName(familyName);
    desc.setStyle(style);
    desc.serialize(wstream);
#if WHITELIST_DEBUG
    for (int i = 0; i < whitelistCount; ++i) {
        if (!strcmp(fontName, whitelist[i].fFontName)) {
            if (!whitelist[i].fSerializedSub) {
                whitelist[i].fSerializedSub = true;
                SkDebugf("%s %s\n", __FUNCTION__, familyName);
            }
            break;
        }
    }
#endif
}

static bool is_local(const SkTypeface* tf) {
    bool isLocal = false;
    SkFontDescriptor desc;
    tf->getFontDescriptor(&desc, &isLocal);
    return isLocal;
}

static void serialize_full(const SkTypeface* tf, SkWStream* wstream) {
    bool isLocal = false;
    SkFontDescriptor desc;
    tf->getFontDescriptor(&desc, &isLocal);

    // Embed font data if it's a local font.
    if (isLocal && !desc.hasFontData()) {
        desc.setFontData(tf->makeFontData());
    }
    desc.serialize(wstream);
}

static void serialize_name_only(const SkTypeface* tf, SkWStream* wstream) {
    bool isLocal = false;
    SkFontDescriptor desc;
    tf->getFontDescriptor(&desc, &isLocal);
    SkASSERT(!isLocal);
#if WHITELIST_DEBUG
    const char* familyName = desc.getFamilyName();
    if (familyName) {
        if (!strcmp(familyName, "Times New Roman")) {
            if (!timesNewRomanSerializedNameOnly) {
                timesNewRomanSerializedNameOnly = true;
                SkDebugf("%s %s\n", __FUNCTION__, familyName);
            }
        } else {
            for (int i = 0; i < whitelistCount; ++i) {
                if (!strcmp(familyName, whitelist[i].fFontName)) {
                    if (!whitelist[i].fSerializedNameOnly) {
                        whitelist[i].fSerializedNameOnly = true;
                        SkDebugf("%s %s\n", __FUNCTION__, familyName);
                    }
                    break;
                }
            }
        }
    }
#endif
    desc.serialize(wstream);
}

void WhitelistSerializeTypeface(const SkTypeface* tf, SkWStream* wstream) {
    if (!is_local(tf)) {
        serialize_name_only(tf, wstream);
        return;
    }
    int whitelistIndex = whitelist_name_index(tf);
    if (whitelistIndex < 0) {
        serialize_full(tf, wstream);
        return;
    }
    const char* fontName = whitelist[whitelistIndex].fFontName;
    if (!font_name_is_local(fontName, tf->fontStyle())) {
#if WHITELIST_DEBUG
        SkDebugf("name not found locally \"%s\" style=%d\n", fontName, tf->style());
#endif
        serialize_full(tf, wstream);
        return;
    }
    uint32_t checksum = compute_checksum(tf);
    if (whitelist[whitelistIndex].fChecksum != checksum) {
#if WHITELIST_DEBUG
        if (whitelist[whitelistIndex].fChecksum) {
            SkDebugf("!!! checksum changed !!!\n");
        }
        SkDebugf("checksum updated\n");
        SkDebugf("    { \"%s\", 0x%08x },\n", fontName, checksum);
#endif
        whitelist[whitelistIndex].fChecksum = checksum;
    }
    serialize_sub(fontName, tf->fontStyle(), wstream);
}

sk_sp<SkTypeface> WhitelistDeserializeTypeface(SkStream* stream) {
    SkFontDescriptor desc;
    if (!SkFontDescriptor::Deserialize(stream, &desc)) {
        return nullptr;
    }

    std::unique_ptr<SkFontData> data = desc.detachFontData();
    if (data) {
        sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(std::move(data)));
        if (typeface) {
            return typeface;
        }
    }
    const char* familyName = desc.getFamilyName();
    if (!strncmp(SUBNAME_PREFIX, familyName, sizeof(SUBNAME_PREFIX) - 1)) {
        familyName += sizeof(SUBNAME_PREFIX) - 1;
    }
    return SkTypeface::MakeFromName(familyName, desc.getStyle());
}

bool CheckChecksums() {
    for (int i = 0; i < whitelistCount; ++i) {
        const char* fontName = whitelist[i].fFontName;
        sk_sp<SkTypeface> tf(SkTypeface::MakeFromName(fontName, SkFontStyle()));
        uint32_t checksum = compute_checksum(tf.get());
        if (whitelist[i].fChecksum != checksum) {
            return false;
        }
    }
    return true;
}

const char checksumFileName[] = "SkWhitelistChecksums.inc";

const char checksumHeader[] =
"/*"                                                                        "\n"
" * Copyright 2015 Google Inc."                                             "\n"
" *"                                                                        "\n"
" * Use of this source code is governed by a BSD-style license that can be" "\n"
" * found in the LICENSE file."                                             "\n"
" *"                                                                        "\n"
" * %s() in %s generated %s."                                               "\n"
" * Run 'whitelist_typefaces --generate' to create anew."                   "\n"
" */"                                                                       "\n"
""                                                                          "\n"
"#include \"SkTDArray.h\""                                                  "\n"
""                                                                          "\n"
"struct Whitelist {"                                                        "\n"
"    const char* fFontName;"                                                "\n"
"    uint32_t fChecksum;"                                                   "\n"
"    bool fSerializedNameOnly;"                                             "\n"
"    bool fSerializedSub;"                                                  "\n"
"};"                                                                        "\n"
""                                                                          "\n"
"static Whitelist whitelist[] = {"                                          "\n";

const char checksumEntry[] =
"    { \"%s\", 0x%08x, false, false },"                                     "\n";

const char checksumTrailer[] =
"};"                                                                        "\n"
""                                                                          "\n"
"static const int whitelistCount = (int) SK_ARRAY_COUNT(whitelist);"        "\n";


#include "SkOSFile.h"

bool GenerateChecksums() {
    FILE* file = sk_fopen(checksumFileName, kWrite_SkFILE_Flag);
    if (!file) {
        SkDebugf("Can't open %s for writing.\n", checksumFileName);
        return false;
    }
    SkString line;
    line.printf(checksumHeader, __FUNCTION__, __FILE__, checksumFileName);
    sk_fwrite(line.c_str(), line.size(), file);
    for (int i = 0; i < whitelistCount; ++i) {
        const char* fontName = whitelist[i].fFontName;
        sk_sp<SkTypeface> tf(SkTypeface::MakeFromName(fontName, SkFontStyle()));
        uint32_t checksum = compute_checksum(tf.get());
        line.printf(checksumEntry, fontName, checksum);
        sk_fwrite(line.c_str(), line.size(), file);
    }
    sk_fwrite(checksumTrailer, sizeof(checksumTrailer) - 1, file);
    sk_fclose(file);
    return true;
}