File: QPDFFileSpecObjectHelper.cc

package info (click to toggle)
qpdf 12.3.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 72,644 kB
  • sloc: cpp: 59,031; perl: 12,172; ansic: 6,809; sh: 1,231; python: 1,041; xml: 43; makefile: 42
file content (129 lines) | stat: -rw-r--r-- 3,103 bytes parent folder | download | duplicates (2)
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
#include <qpdf/QPDFFileSpecObjectHelper.hh>

#include <qpdf/QPDF.hh>
#include <qpdf/QPDFObjectHandle_private.hh>
#include <qpdf/QTC.hh>
#include <qpdf/QUtil.hh>

#include <array>
#include <string>

using namespace std::literals;
using namespace qpdf;

class QPDFFileSpecObjectHelper::Members
{
};

QPDFFileSpecObjectHelper::QPDFFileSpecObjectHelper(QPDFObjectHandle oh) :
    QPDFObjectHelper(oh)
{
    if (!oh.isDictionary()) {
        warn("Embedded file object is not a dictionary");
        return;
    }
    if (!oh.isDictionaryOfType("/Filespec")) {
        warn("Embedded file object's type is not /Filespec");
    }
}

static const std::array<std::string, 5> name_keys = {"/UF"s, "/F"s, "/Unix"s, "/DOS"s, "/Mac"s};

std::string
QPDFFileSpecObjectHelper::getDescription()
{
    if (String Desc = oh().getKey("/Desc")) {
        return Desc.utf8_value();
    }
    return {};
}

std::string
QPDFFileSpecObjectHelper::getFilename()
{
    for (auto const& i: name_keys) {
        if (String k = get(i)) {
            return k.utf8_value();
        }
    }
    return {};
}

std::map<std::string, std::string>
QPDFFileSpecObjectHelper::getFilenames()
{
    std::map<std::string, std::string> result;
    for (auto const& i: name_keys) {
        if (String k = get(i)) {
            result[i] = k.utf8_value();
        }
    }
    return result;
}

QPDFObjectHandle
QPDFFileSpecObjectHelper::getEmbeddedFileStream(std::string const& key)
{
    if (Dictionary EF = get("/EF")) {
        if (!key.empty() && EF.contains(key)) {
            if (auto result = EF[key]) {
                return result;
            }
        }
        for (auto const& i: name_keys) {
            if (Stream k = EF[i]) {
                return k;
            }
        }
    }
    return Null::temp();
}

QPDFObjectHandle
QPDFFileSpecObjectHelper::getEmbeddedFileStreams()
{
    return oh().getKey("/EF");
}

QPDFFileSpecObjectHelper
QPDFFileSpecObjectHelper::createFileSpec(
    QPDF& qpdf, std::string const& filename, std::string const& fullpath)
{
    return createFileSpec(
        qpdf,
        filename,
        QPDFEFStreamObjectHelper::createEFStream(qpdf, QUtil::file_provider(fullpath)));
}

QPDFFileSpecObjectHelper
QPDFFileSpecObjectHelper::createFileSpec(
    QPDF& qpdf, std::string const& filename, QPDFEFStreamObjectHelper efsoh)
{
    auto UF = String::utf16(filename);
    return {qpdf.makeIndirectObject(Dictionary(
        {{"/Type", Name("/Filespec")},
         {"/F", UF},
         {"/UF", UF},
         {"/EF", Dictionary({{"/F", efsoh}, {"/UF", efsoh}})}}))};
}

QPDFFileSpecObjectHelper&
QPDFFileSpecObjectHelper::setDescription(std::string const& desc)
{
    oh().replaceKey("/Desc", String::utf16(desc));
    return *this;
}

QPDFFileSpecObjectHelper&
QPDFFileSpecObjectHelper::setFilename(
    std::string const& unicode_name, std::string const& compat_name)
{
    auto uf = String::utf16(unicode_name);
    oh().replaceKey("/UF", uf);
    if (compat_name.empty()) {
        oh().replaceKey("/F", uf);
    } else {
        oh().replaceKey("/F", String(compat_name));
    }
    return *this;
}