summaryrefslogtreecommitdiffstats
path: root/src/extension/internal/pdfinput/poppler-utils.cpp
blob: 26746dc95f14cb7c4548bf4976817905f95fe5c5 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * PDF parsing utilities for libpoppler.
 *//*
 * Authors:
 *    Martin Owens
 * 
 * Copyright (C) 2022 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include "poppler-utils.h"

#include "2geom/affine.h"
#include "GfxFont.h"
#include "GfxState.h"
#include "PDFDoc.h"
#include "libnrtype/font-factory.h"

/**
 * Get the default transformation state from the GfxState
 */
Geom::Affine stateToAffine(GfxState *state)
{
    return ctmToAffine(state->getCTM());
}

/**
 * Convert a transformation matrix to a lib2geom affine object.
 */
Geom::Affine ctmToAffine(const double *ctm)
{
    if (!ctm)
        return Geom::identity();
    return Geom::Affine(ctm[0], ctm[1], ctm[2], ctm[3], ctm[4], ctm[5]);
}

void ctmout(const char *label, const double *ctm)
{
    std::cout << "C:" << label << ":" << ctm[0] << "," << ctm[1] << "," << ctm[2] << "," << ctm[3] << "," << ctm[4]
              << "," << ctm[5] << "\n";
}

void affout(const char *label, Geom::Affine ctm)
{
    std::cout << "A:" << label << ":" << ctm[0] << "," << ctm[1] << "," << ctm[2] << "," << ctm[3] << "," << ctm[4]
              << "," << ctm[5] << "\n";
}

//------------------------------------------------------------------------
// GfxFontDict from GfxFont.cc in poppler 22.09
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// See poppler source code for full list of copyright holders.
//------------------------------------------------------------------------

InkFontDict::InkFontDict(XRef *xref, Ref *fontDictRef, Dict *fontDict)
{
    Ref r;

    fonts.resize(fontDict->getLength());
    for (std::size_t i = 0; i < fonts.size(); ++i) {
        const Object &obj1 = fontDict->getValNF(i);
        Object obj2 = obj1.fetch(xref);
        if (obj2.isDict()) {
            if (obj1.isRef()) {
                r = obj1.getRef();
            } else if (fontDictRef) {
                // legal generation numbers are five digits, so we use a
                // 6-digit number here
                r.gen = 100000 + fontDictRef->num;
                r.num = i;
            } else {
                // no indirect reference for this font, or for the containing
                // font dict, so hash the font and use that
                r.gen = 100000;
                r.num = hashFontObject(&obj2);
            }
            // Newer poppler will require some reworking as it gives a shared ptr.
            fonts[i] = GfxFont::makeFont(xref, fontDict->getKey(i), r, obj2.getDict());
            if (fonts[i] && !fonts[i]->isOk()) {
                fonts[i] = nullptr;
            }
        } else {
            error(errSyntaxError, -1, "font resource is not a dictionary");
            fonts[i] = nullptr;
        }
    }
}

FontPtr InkFontDict::lookup(const char *tag) const
{
    for (const auto &font : fonts) {
        if (font && font->matches(tag)) {
            return font;
        }
    }
    return nullptr;
}

// FNV-1a hash
class FNVHash
{
public:
    FNVHash() { h = 2166136261U; }

    void hash(char c)
    {
        h ^= c & 0xff;
        h *= 16777619;
    }

    void hash(const char *p, int n)
    {
        int i;
        for (i = 0; i < n; ++i) {
            hash(p[i]);
        }
    }

    int get31() { return (h ^ (h >> 31)) & 0x7fffffff; }

private:
    unsigned int h;
};

int InkFontDict::hashFontObject(Object *obj)
{
    FNVHash h;

    hashFontObject1(obj, &h);
    return h.get31();
}

void InkFontDict::hashFontObject1(const Object *obj, FNVHash *h)
{
    const GooString *s;
    const char *p;
    double r;
    int n, i;

    switch (obj->getType()) {
        case objBool:
            h->hash('b');
            h->hash(obj->getBool() ? 1 : 0);
            break;
        case objInt:
            h->hash('i');
            n = obj->getInt();
            h->hash((char *)&n, sizeof(int));
            break;
        case objReal:
            h->hash('r');
            r = obj->getReal();
            h->hash((char *)&r, sizeof(double));
            break;
        case objString:
            h->hash('s');
            s = obj->getString();
            h->hash(s->c_str(), s->getLength());
            break;
        case objName:
            h->hash('n');
            p = obj->getName();
            h->hash(p, (int)strlen(p));
            break;
        case objNull:
            h->hash('z');
            break;
        case objArray:
            h->hash('a');
            n = obj->arrayGetLength();
            h->hash((char *)&n, sizeof(int));
            for (i = 0; i < n; ++i) {
                const Object &obj2 = obj->arrayGetNF(i);
                hashFontObject1(&obj2, h);
            }
            break;
        case objDict:
            h->hash('d');
            n = obj->dictGetLength();
            h->hash((char *)&n, sizeof(int));
            for (i = 0; i < n; ++i) {
                p = obj->dictGetKey(i);
                h->hash(p, (int)strlen(p));
                const Object &obj2 = obj->dictGetValNF(i);
                hashFontObject1(&obj2, h);
            }
            break;
        case objStream:
            // this should never happen - streams must be indirect refs
            break;
        case objRef:
            h->hash('f');
            n = obj->getRefNum();
            h->hash((char *)&n, sizeof(int));
            n = obj->getRefGen();
            h->hash((char *)&n, sizeof(int));
            break;
        default:
            h->hash('u');
            break;
    }
}

std::string getNameWithoutSubsetTag(FontPtr font)
{
    if (!font->getName())
        return {};

    std::string tagname = font->getName()->c_str();
    unsigned int i;
    for (i = 0; i < tagname.size(); ++i) {
        if (tagname[i] < 'A' || tagname[i] > 'Z') {
            break;
        }
    }
    if (i != 6 || tagname.size() <= 7 || tagname[6] != '+')
        return tagname;
    return tagname.substr(7);
}

/**
 * Extract all the useful information from the GfxFont object
 */
FontData::FontData(FontPtr font)
{
    // Level one parsing is taking the data from the PDF font, although this
    // information is almost always missing. Perhaps sometimes it's not.
    found = false;

    // Style: italic, oblique, normal
    style = font->isItalic() ? "italic" : "";

    // Weight: normal, bold, etc
    weight = "normal";
    switch (font->getWeight()) {
        case GfxFont::WeightNotDefined:
            break;
        case GfxFont::W400:
            weight = "normal";
            break;
        case GfxFont::W700:
            weight = "bold";
            break;
        default:
            weight = std::to_string(font->getWeight() * 100);
            break;
    }

    // Stretch: condensed or expanded
    stretch = "";
    switch (font->getStretch()) {
        case GfxFont::UltraCondensed:
            stretch = "ultra-condensed";
            break;
        case GfxFont::ExtraCondensed:
            stretch = "extra-condensed";
            break;
        case GfxFont::Condensed:
            stretch = "condensed";
            break;
        case GfxFont::SemiCondensed:
            stretch = "semi-condensed";
            break;
        case GfxFont::Normal:
            stretch = "normal";
            break;
        case GfxFont::SemiExpanded:
            stretch = "semi-expanded";
            break;
        case GfxFont::Expanded:
            stretch = "expanded";
            break;
        case GfxFont::ExtraExpanded:
            stretch = "extra-expanded";
            break;
        case GfxFont::UltraExpanded:
            stretch = "ultra-expanded";
            break;
    }

    name = getNameWithoutSubsetTag(font);
    // Use this when min-poppler version is newer:
    // name = font->getNameWithoutSubsetTag();

    // Embeded CID Fonts don't have family names
    if (!font->getFamily())
        return;

    family = font->getFamily()->c_str();

    // Level two parsing, we break off the font description part of the name
    // which often contains font data and use it as a pango font description.
    auto desc_str = family;
    auto pos = name.find("-");
    if (pos != std::string::npos) {
        // Insert spaces where we see capital letters.
        std::stringstream ret;
        auto str = name.substr(pos + 1, name.size());
        for (char l : str) {
            if (l >= 'A' && l <= 'Z')
                ret << " ";
            ret << l;
        }
        desc_str = desc_str + ret.str();
    }

    // Now we pull data out of the description.
    if (auto desc = pango_font_description_from_string(desc_str.c_str())) {
        auto new_family = pango_font_description_get_family(desc);
        if (FontFactory::get().hasFontFamily(new_family)) {
            family = new_family;

            // Style from pango description
            switch (pango_font_description_get_style(desc)) {
                case PANGO_STYLE_ITALIC:
                    style = "italic";
                    break;
                case PANGO_STYLE_OBLIQUE:
                    style = "oblique";
                    break;
            }

            // Weight from pango description
            auto pw = pango_font_description_get_weight(desc);
            if (pw != PANGO_WEIGHT_NORMAL) {
                weight = std::to_string(pw); // Number 100-1000
            }

            // Stretch from pango description
            switch (pango_font_description_get_stretch(desc)) {
                case PANGO_STRETCH_ULTRA_CONDENSED:
                    stretch = "ultra-condensed";
                    break;
                case PANGO_STRETCH_EXTRA_CONDENSED:
                    stretch = "extra-condensed";
                    break;
                case PANGO_STRETCH_CONDENSED:
                    stretch = "condensed";
                    break;
                case PANGO_STRETCH_SEMI_CONDENSED:
                    stretch = "semi-condensed";
                    break;
                case PANGO_STRETCH_SEMI_EXPANDED:
                    stretch = "semi-expanded";
                    break;
                case PANGO_STRETCH_EXPANDED:
                    stretch = "expanded";
                    break;
                case PANGO_STRETCH_EXTRA_EXPANDED:
                    stretch = "extra-expanded";
                    break;
                case PANGO_STRETCH_ULTRA_EXPANDED:
                    stretch = "ultra-expanded";
                    break;
            }

            // variant = TODO Convert to variant pango_font_description_get_variant(desc)

            found = true;
            // All information has been processed, don't over-write with level three.
            return;
        }
        // Sometimes it's possible to match the description string directly.
        if (auto desc = pango_font_description_from_string(family.c_str())) {
            auto new_family = pango_font_description_get_family(desc);
            if (FontFactory::get().hasFontFamily(new_family)) {
                family = new_family;
            }
        }
    }

    found = FontFactory::get().hasFontFamily(family);
    // TODO: If !found we could suggest a substitute

    // Level three parsing, we take our name and attempt to match known style names
    // Copy id-name stored in PDF and make it lower case and strip whitespaces
    std::string source = name;
    transform(source.begin(), source.end(), source.begin(), ::tolower);
    source.erase(std::remove_if(source.begin(), source.end(), ::isspace), source.end());
    auto contains = [=](const std::string &other) { return source.find(other) != std::string::npos; };

    if (contains("italic") || contains("slanted")) {
        style = "italic";
    } else if (contains("oblique")) {
        style = "oblique";
    }

    // Ordered by string matching pass through.
    static std::map<std::string, std::string> weights{
        // clang-format off
        {"bold",        "bold"},
        {"ultrabold",   "800"},
        {"extrabold",   "800"},
        {"demibold",    "600"},
        {"semibold",    "600"},
        {"thin",        "100"},
        {"ultralight",  "200"},
        {"extralight",  "200"},
        {"light",       "300"},
        {"black",       "900"},
        {"heavy",       "900"},
        {"medium",      "500"},
        {"book",        "normal"},
        {"regular",     "normal"},
        {"roman",       "normal"},
        {"normal",      "normal"},
        // clang-format on
    };
    // Apply the font weight translations
    for (auto w : weights) {
        if (contains(w.first))
            weight = w.second;
    }

    static std::map<std::string, std::string> stretches{
        // clang-format off
        {"ultracondensed", "ultra-condensed"},
        {"extracondensed", "extra-condensed"},
        {"semicondensed", "semi-condensed"},
        {"condensed", "condensed"},
        {"ultraexpanded", "ultra-expanded"},
        {"extraexpanded", "extra-expanded"},
        {"semiexpanded", "semi-expanded"},
        {"expanded", "expanded"},
        // clang-format on
    };
    // Apply the font weight translations
    for (auto s : stretches) {
        if (contains(s.first))
            stretch = s.second;
    }
}

/*
    MatchingChars
    Count for how many characters s1 matches sp taking into account
    that a space in sp may be removed or replaced by some other tokens
    specified in the code. (Bug LP #179589)
*/
static size_t MatchingChars(std::string s1, std::string sp)
{
    size_t is = 0;
    size_t ip = 0;

    while (is < s1.length() && ip < sp.length()) {
        if (s1[is] == sp[ip]) {
            is++;
            ip++;
        } else if (sp[ip] == ' ') {
            ip++;
            if (s1[is] == '_') { // Valid matches to spaces in sp.
                is++;
            }
        } else {
            break;
        }
    }
    return ip;
}

/*
 * Scan the available fonts to find the font name that best match.
 *
 * If nothing can be matched, returns an empty string.
 */
std::string FontData::getSubstitute() const
{
    if (found)
        return "";

    double bestMatch = 0;
    std::string bestFontname = "";

    for (auto fontname : FontFactory::get().GetAllFontNames()) {
        // At least the first word of the font name should match.
        size_t minMatch = fontname.find(" ");
        if (minMatch == std::string::npos) {
            minMatch = fontname.length();
        }

        size_t Match = MatchingChars(family, fontname);
        if (Match >= minMatch) {
            double relMatch = (float)Match / (fontname.length() + family.length());
            if (relMatch > bestMatch) {
                bestMatch = relMatch;
                bestFontname = fontname;
            }
        }
    }
    return bestFontname.empty() ? "Arial" : bestFontname;
}

std::string FontData::getSpecification() const
{
    return family + (style.empty() ? "" : "-" + style);
}

//------------------------------------------------------------------------
// scanFonts from FontInfo.cc
//------------------------------------------------------------------------

void _getFontsRecursive(std::shared_ptr<PDFDoc> pdf_doc, Dict *resources, const FontList &fontsList,
                        std::set<int> &visitedObjects, int page)
{
    auto xref = pdf_doc->getXRef();

    InkFontDict *fontDict = nullptr;
    const Object &obj1 = resources->lookupNF("Font");
    if (obj1.isRef()) {
        Object obj2 = obj1.fetch(xref);
        if (obj2.isDict()) {
            auto r = obj1.getRef();
            fontDict = new InkFontDict(xref, &r, obj2.getDict());
        }
    } else if (obj1.isDict()) {
        fontDict = new InkFontDict(xref, nullptr, obj1.getDict());
    }

    if (fontDict) {
        for (int i = 0; i < fontDict->getNumFonts(); ++i) {
            auto font = fontDict->getFont(i);
            if (fontsList->find(font) == fontsList->end()) {
                // Create new font data
                fontsList->emplace(font, FontData(font));
            }
            fontsList->at(font).pages.insert(page);
        }
    }

    // recursively scan any resource dictionaries in objects in this resource dictionary
    const char *resTypes[] = {"XObject", "Pattern"};
    for (const char *resType : resTypes) {
        Object objDict = resources->lookup(resType);
        if (!objDict.isDict())
            continue;

        for (int i = 0; i < objDict.dictGetLength(); ++i) {
            Ref obj2Ref;
            const Object obj2 = objDict.getDict()->getVal(i, &obj2Ref);
            if (obj2Ref != Ref::INVALID() && !visitedObjects.insert(obj2Ref.num).second)
                continue;

            if (!obj2.isStream())
                continue;

            Ref resourcesRef;
            const Object resObj = obj2.streamGetDict()->lookup("Resources", &resourcesRef);
            if (resourcesRef != Ref::INVALID() && !visitedObjects.insert(resourcesRef.num).second)
                continue;

            if (resObj.isDict() && resObj.getDict() != resources) {
                _getFontsRecursive(pdf_doc, resObj.getDict(), fontsList, visitedObjects, page);
            }
        }
    }
}

FontList getPdfFonts(std::shared_ptr<PDFDoc> pdf_doc)
{
    auto fontsList = std::make_shared<std::map<FontPtr, FontData>>();
    auto count = pdf_doc->getCatalog()->getNumPages();
    std::set<int> visitedObjects;

    for (auto page_num = 1; page_num <= count; page_num++) {
        auto page = pdf_doc->getCatalog()->getPage(page_num);
        auto resources = page->getResourceDict();
        _getFontsRecursive(pdf_doc, resources, fontsList, visitedObjects, page_num);
    }
    return fontsList;
}


std::string getDictString(Dict *dict, const char *key)
{
    Object obj = dict->lookup(key);

    if (!obj.isString()) {
        return "";
    }

    const GooString *value = obj.getString();
    if (value->hasUnicodeMarker()) {
        return g_convert(value->getCString () + 2, value->getLength () - 2,
                         "UTF-8", "UTF-16BE", NULL, NULL, NULL);
    } else if (value->hasUnicodeMarkerLE()) {
        return g_convert(value->getCString () + 2, value->getLength () - 2,
                         "UTF-8", "UTF-16LE", NULL, NULL, NULL);
    }
    return value->toStr();
}