aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/PdfViewer/SkPdfFont.cpp
blob: 520c820f1bdb0bb1cc903cc56fd467e9efbb07ce (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "SkPdfFont.h"

std::map<std::string, SkPdfStandardFontEntry>& getStandardFonts() {
    static std::map<std::string, SkPdfStandardFontEntry> gPdfStandardFonts;

    // TODO (edisonn): , vs - ? what does it mean?
    // TODO (edisonn): MT, PS, Oblique=italic?, ... what does it mean?
    if (gPdfStandardFonts.empty()) {
        gPdfStandardFonts["Arial"] = {"Arial", false, false};
        gPdfStandardFonts["Arial,Bold"] = {"Arial", true, false};
        gPdfStandardFonts["Arial,BoldItalic"] = {"Arial", true, true};
        gPdfStandardFonts["Arial,Italic"] = {"Arial", false, true};
        gPdfStandardFonts["Arial-Bold"] = {"Arial", true, false};
        gPdfStandardFonts["Arial-BoldItalic"] = {"Arial", true, true};
        gPdfStandardFonts["Arial-BoldItalicMT"] = {"Arial", true, true};
        gPdfStandardFonts["Arial-BoldMT"] = {"Arial", true, false};
        gPdfStandardFonts["Arial-Italic"] = {"Arial", false, true};
        gPdfStandardFonts["Arial-ItalicMT"] = {"Arial", false, true};
        gPdfStandardFonts["ArialMT"] = {"Arial", false, false};
        gPdfStandardFonts["Courier"] = {"Courier New", false, false};
        gPdfStandardFonts["Courier,Bold"] = {"Courier New", true, false};
        gPdfStandardFonts["Courier,BoldItalic"] = {"Courier New", true, true};
        gPdfStandardFonts["Courier,Italic"] = {"Courier New", false, true};
        gPdfStandardFonts["Courier-Bold"] = {"Courier New", true, false};
        gPdfStandardFonts["Courier-BoldOblique"] = {"Courier New", true, true};
        gPdfStandardFonts["Courier-Oblique"] = {"Courier New", false, true};
        gPdfStandardFonts["CourierNew"] = {"Courier New", false, false};
        gPdfStandardFonts["CourierNew,Bold"] = {"Courier New", true, false};
        gPdfStandardFonts["CourierNew,BoldItalic"] = {"Courier New", true, true};
        gPdfStandardFonts["CourierNew,Italic"] = {"Courier New", false, true};
        gPdfStandardFonts["CourierNew-Bold"] = {"Courier New", true, false};
        gPdfStandardFonts["CourierNew-BoldItalic"] = {"Courier New", true, true};
        gPdfStandardFonts["CourierNew-Italic"] = {"Courier New", false, true};
        gPdfStandardFonts["CourierNewPS-BoldItalicMT"] = {"Courier New", true, true};
        gPdfStandardFonts["CourierNewPS-BoldMT"] = {"Courier New", true, false};
        gPdfStandardFonts["CourierNewPS-ItalicMT"] = {"Courier New", false, true};
        gPdfStandardFonts["CourierNewPSMT"] = {"Courier New", false, false};
        gPdfStandardFonts["Helvetica"] = {"Helvetica", false, false};
        gPdfStandardFonts["Helvetica,Bold"] = {"Helvetica", true, false};
        gPdfStandardFonts["Helvetica,BoldItalic"] = {"Helvetica", true, true};
        gPdfStandardFonts["Helvetica,Italic"] = {"Helvetica", false, true};
        gPdfStandardFonts["Helvetica-Bold"] = {"Helvetica", true, false};
        gPdfStandardFonts["Helvetica-BoldItalic"] = {"Helvetica", true, true};
        gPdfStandardFonts["Helvetica-BoldOblique"] = {"Helvetica", true, true};
        gPdfStandardFonts["Helvetica-Italic"] = {"Helvetica", false, true};
        gPdfStandardFonts["Helvetica-Oblique"] = {"Helvetica", false, true};
        gPdfStandardFonts["Times-Bold"] = {"Times New Roman", true, false};
        gPdfStandardFonts["Times-BoldItalic"] = {"Times New Roman", true, true};
        gPdfStandardFonts["Times-Italic"] = {"Times New Roman", false, true};
        gPdfStandardFonts["Times-Roman"] = {"Times New Roman", false, false};
        gPdfStandardFonts["TimesNewRoman"] = {"Times New Roman", false, false};
        gPdfStandardFonts["TimesNewRoman,Bold"] = {"Times New Roman", true, false};
        gPdfStandardFonts["TimesNewRoman,BoldItalic"] = {"Times New Roman", true, true};
        gPdfStandardFonts["TimesNewRoman,Italic"] = {"Times New Roman", false, true};
        gPdfStandardFonts["TimesNewRoman-Bold"] = {"Times New Roman", true, false};
        gPdfStandardFonts["TimesNewRoman-BoldItalic"] = {"Times New Roman", true, true};
        gPdfStandardFonts["TimesNewRoman-Italic"] = {"Times New Roman", false, true};
        gPdfStandardFonts["TimesNewRomanPS"] = {"Times New Roman", false, false};
        gPdfStandardFonts["TimesNewRomanPS-Bold"] = {"Times New Roman", true, false};
        gPdfStandardFonts["TimesNewRomanPS-BoldItalic"] = {"Times New Roman", true, true};
        gPdfStandardFonts["TimesNewRomanPS-BoldItalicMT"] = {"Times New Roman", true, true};
        gPdfStandardFonts["TimesNewRomanPS-BoldMT"] = {"Times New Roman", true, false};
        gPdfStandardFonts["TimesNewRomanPS-Italic"] = {"Times New Roman", false, true};
        gPdfStandardFonts["TimesNewRomanPS-ItalicMT"] = {"Times New Roman", false, true};
        gPdfStandardFonts["TimesNewRomanPSMT"] = {"Times New Roman", false, false};
        gPdfStandardFonts["Symbol"] = {"Symbol", false, false};
        gPdfStandardFonts["ZapfDingbats"] = {"ZapfDingbats", false, false};

        // TODO(edisonn): these are hacks. Load Post Script font name.
        // see FT_Get_Postscript_Name
        // Font config is not using it, yet.
        //https://bugs.freedesktop.org/show_bug.cgi?id=18095
        gPdfStandardFonts["Arial-Black"] = {"Arial", true, false};
        gPdfStandardFonts["DejaVuSans"] = {"DejaVu Sans", false, false};
        gPdfStandardFonts["DejaVuSansMono"] = {"DejaVuSans Mono", false, false};
        gPdfStandardFonts["DejaVuSansMono-Bold"] = {"DejaVuSans Mono", true, false};
        gPdfStandardFonts["DejaVuSansMono-Oblique"] = {"DejaVuSans Mono", false, true};
        gPdfStandardFonts["Georgia-Bold"] = {"Georgia", true, false};
        gPdfStandardFonts["Georgia-BoldItalic"] = {"Georgia", true, true};
        gPdfStandardFonts["Georgia-Italic"] = {"Georgia", false, true};
        gPdfStandardFonts["TrebuchetMS"] = {"Trebuchet MS", false, false};
        gPdfStandardFonts["TrebuchetMS-Bold"] = {"Trebuchet MS", true, false};
        gPdfStandardFonts["Verdana-Bold"] = {"Verdana", true, false};
        gPdfStandardFonts["WenQuanYiMicroHei"] = {"WenQuanYi Micro Hei", false, false};
    }

    return gPdfStandardFonts;
}

SkTypeface* SkTypefaceFromPdfStandardFont(const char* fontName, bool bold, bool italic) {
    std::map<std::string, SkPdfStandardFontEntry>& standardFontMap = getStandardFonts();

    SkTypeface* typeface = NULL;
    if (standardFontMap.find(fontName) != standardFontMap.end()) {
        SkPdfStandardFontEntry fontData = standardFontMap[fontName];

        // TODO(edisonn): How does the bold/italic specified in standard definition combines with
        // the one in /font key? use OR for now.
        bold = bold || fontData.fIsBold;
        italic = italic || fontData.fIsItalic;

        typeface = SkTypeface::CreateFromName(
            fontData.fName,
            SkTypeface::Style((bold ? SkTypeface::kBold : 0) |
                              (italic ? SkTypeface::kItalic : 0)));
    } else {
        typeface = SkTypeface::CreateFromName(
            fontName,
            SkTypeface::kNormal);
    }

    if (typeface) {
        typeface->ref();
    }
    return typeface;
}

SkPdfFont* SkPdfFontFromName(SkPdfObject* obj, const char* fontName) {
    SkTypeface* typeface = SkTypefaceFromPdfStandardFont(fontName, false, false);
    if (typeface != NULL) {
        return new SkPdfStandardFont(typeface);
    }
//        SkPdfObject* font = obtainFont(pdfContext, fontName);
//        if (!font->asFontDictionary()) {
//            return NULL;
//        }
//        SkPdfFont::fontFromPdfDictionary(font->asDictionary());
//    }
    // TODO(edisonn): deal with inherited fonts, load from parent objects
    return SkPdfFont::Default();
}

SkPdfFont* SkPdfFont::fontFromPdfDictionary(SkPdfFontDictionary* dict) {
    if (dict == NULL) {
        return NULL;  // TODO(edisonn): report default one?
    }

    switch (dict->getType()) {
        case kType0FontDictionary_SkPdfObjectType:
            return fontFromType0FontDictionary(dict->asType0FontDictionary());

        case kTrueTypeFontDictionary_SkPdfObjectType:
            return fontFromTrueTypeFontDictionary(dict->asTrueTypeFontDictionary());

        case kType1FontDictionary_SkPdfObjectType:
            return fontFromType1FontDictionary(dict->asType1FontDictionary());

        case kCIDFontDictionary_SkPdfObjectType:
            return fontFromCIDFontDictionary(dict->asCIDFontDictionary());

        case kMultiMasterFontDictionary_SkPdfObjectType:
            return fontFromMultiMasterFontDictionary(dict->asMultiMasterFontDictionary());

        case kType3FontDictionary_SkPdfObjectType:
            return fontFromType3FontDictionary(dict->asType3FontDictionary());
    }
    return NULL;  // TODO(edisonn): report error?
}

SkPdfType0Font* SkPdfFont::fontFromType0FontDictionary(SkPdfType0FontDictionary* dict) {
    if (dict == NULL) {
        return NULL;  // default one?
    }

    return new SkPdfType0Font(dict);
}

SkPdfType1Font* SkPdfFont:: fontFromType1FontDictionary(SkPdfType1FontDictionary* dict) {
    if (dict == NULL) {
        return NULL;  // default one?
    }

    return new SkPdfType1Font(dict);
}

SkPdfType3Font* SkPdfFont::fontFromType3FontDictionary(SkPdfType3FontDictionary* dict) {
    if (dict == NULL) {
        return NULL;  // default one?
    }

    return new SkPdfType3Font(dict);
}

SkPdfTrueTypeFont* SkPdfFont::fontFromTrueTypeFontDictionary(SkPdfTrueTypeFontDictionary* dict) {
    if (dict == NULL) {
        return NULL;  // default one?
    }

    return new SkPdfTrueTypeFont(dict);
}

SkPdfCIDFont* SkPdfFont::fontFromCIDFontDictionary(SkPdfCIDFontDictionary* dict) {
    if (dict == NULL) {
        return NULL;  // default one?
    }

    return new SkPdfCIDFont(dict);
}

SkPdfMultiMasterFont* SkPdfFont::fontFromMultiMasterFontDictionary(SkPdfMultiMasterFontDictionary* dict) {
    if (dict == NULL) {
        return NULL;  // default one?
    }

    return new SkPdfMultiMasterFont(dict);
}

static int skstoi(const SkPdfString* str) {
    int ret = 0;
    for (int i = 0 ; i < str->podofo()->GetString().GetLength(); i++) {
        ret = (ret << 8) + ((unsigned char*)str->podofo()->GetString().GetString())[i];
    }
    return ret;
}

SkPdfType0Font::SkPdfType0Font(SkPdfType0FontDictionary* dict) {
    fBaseFont = SkPdfFontFromName(dict, dict->BaseFont().c_str());

    // TODO(edisonn): load encoding, let it now to be Identity-H by default
    if (dict->has_Encoding()) {
        if (dict->isEncodingAName()) {
            //report encoding not supported
            //fEncoding = loadEncodingFromName(dict->getEncodingAsName().c_str());
        } else if (dict->isEncodingAStream()) {
            //fEncoding = loadEncodingFromStream(dict->getEncodingAsStream());
        } else {
            // error
        }
    }

    fCMapEncoding = NULL;
    fCMapEncodingFlag = NULL;

    if (dict->has_ToUnicode()) {
        const SkPdfStream* stream = dict->ToUnicode();
        SkPdfTokenizer tokenizer(stream);
        PdfToken token;

        fCMapEncoding = new unsigned short[256 * 256];
        fCMapEncodingFlag = new unsigned char[256 * 256];
        for (int i = 0 ; i < 256 * 256; i++) {
            fCMapEncoding[i] = i;
            fCMapEncodingFlag[i] = 0;
        }

        // TODO(edisonn): deal with multibyte character, or longer strings.
        // Ritght now we deal with up 2 characters, e.g. <0020> but not longer like <00660066006C>
        //2 beginbfrange
        //<0000> <005E> <0020>
        //<005F> <0061> [<00660066> <00660069> <00660066006C>]

        while (tokenizer.readToken(&token)) {

            if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "begincodespacerange") == 0) {
                while (tokenizer.readToken(&token) && !(token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "endcodespacerange") == 0)) {
//                    tokenizer.PutBack(token);
//                    tokenizer.readToken(&token);
                    // TODO(edisonn): check token type! ignore/report errors.
                    int start = skstoi(token.fObject->asString());
                    tokenizer.readToken(&token);
                    int end = skstoi(token.fObject->asString());
                    for (int i = start; i <= end; i++) {
                        fCMapEncodingFlag[i] |= 1;
                    }
                }
            }

            if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "beginbfchar") == 0) {
                while (tokenizer.readToken(&token) && !(token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "endbfchar") == 0)) {
//                    tokenizer.PutBack(token);
//                    tokenizer.readToken(&token);
                    int from = skstoi(token.fObject->asString());
                    tokenizer.readToken(&token);
                    int to = skstoi(token.fObject->asString());

                    fCMapEncodingFlag[from] |= 2;
                    fCMapEncoding[from] = to;
                }
            }

            if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "beginbfrange") == 0) {
                while (tokenizer.readToken(&token) && !(token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "endbfrange") == 0)) {
//                    tokenizer.PutBack(token);
//                    tokenizer.readToken(&token);
                    int start = skstoi(token.fObject->asString());
                    tokenizer.readToken(&token);
                    int end = skstoi(token.fObject->asString());


                    tokenizer.readToken(&token); // [ or just an array directly?
//                    tokenizer.PutBack(token);

                    if (token.fType == kObject_TokenType && token.fObject->asString()) {
//                        tokenizer.readToken(&token);
                        int value = skstoi(token.fObject->asString());

                        for (int i = start; i <= end; i++) {
                            fCMapEncodingFlag[i] |= 2;
                            fCMapEncoding[i] = value;
                            value++;
                            // if i != end, verify last byte id not if, ignore/report error
                        }

                        // read one string
                    } else if (token.fType == kObject_TokenType && token.fObject->asArray()) {
//                        tokenizer.readToken(&token);
                        for (int i = 0; i < token.fObject->asArray()->size(); i++) {
                            fCMapEncodingFlag[start + i] |= 2;
                            fCMapEncoding[start + i] = skstoi((*token.fObject->asArray())[i]->asString());
                        }
                        // read array
                    }

                    //fCMapEncodingFlag[from] = 1;
                    //fCMapEncoding[from] = to;
                }
            }
        }
    }
}