summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibPDF/Encoding.cpp
blob: f18152dafee7edfafd7e11511b878c5668ac9d70 (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
/*
 * Copyright (c) 2022, Matthew Olsson <mattco@serenityos.org>
 * Copyright (c) 2022, Julian Offenhäuser <offenhaeuser@protonmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Utf8View.h>
#include <LibPDF/CommonNames.h>
#include <LibPDF/Encoding.h>

namespace PDF {

PDFErrorOr<NonnullRefPtr<Encoding>> Encoding::create(HashMap<u16, CharDescriptor> descriptors)
{
    auto encoding = adopt_ref(*new Encoding());
    encoding->m_descriptors = descriptors;

    for (auto& descriptor : descriptors)
        encoding->m_name_mapping.set(descriptor.value.name, descriptor.value.code_point);

    return encoding;
}

PDFErrorOr<NonnullRefPtr<Encoding>> Encoding::from_object(Document* document, NonnullRefPtr<Object> const& obj)
{
    if (obj->is<NameObject>()) {
        auto name = obj->cast<NameObject>()->name();
        if (name == "StandardEncoding")
            return standard_encoding();
        if (name == "MacRomanEncoding")
            return mac_encoding();
        if (name == "WinAnsiEncoding")
            return windows_encoding();

        VERIFY_NOT_REACHED();
    }

    // Make a custom encoding
    auto dict = obj->cast<DictObject>();

    RefPtr<Encoding> base_encoding;
    if (dict->contains(CommonNames::BaseEncoding)) {
        auto base_encoding_obj = MUST(dict->get_object(document, CommonNames::BaseEncoding));
        base_encoding = TRY(Encoding::from_object(document, base_encoding_obj));
    } else {
        base_encoding = Encoding::standard_encoding();
    }

    auto encoding = adopt_ref(*new Encoding());

    encoding->m_descriptors = base_encoding->descriptors();
    encoding->m_name_mapping = base_encoding->name_mapping();

    auto differences_array = TRY(dict->get_array(document, CommonNames::Differences));

    u16 current_code_point = 0;
    bool first = true;

    for (auto& item : *differences_array) {
        if (item.has_u32()) {
            current_code_point = item.to_int();
            first = false;
        } else {
            VERIFY(item.has<NonnullRefPtr<Object>>());
            VERIFY(!first);
            auto& object = item.get<NonnullRefPtr<Object>>();
            auto name = object->cast<NameObject>()->name();

            encoding->m_descriptors.set(current_code_point, { name, base_encoding->m_name_mapping.ensure(name) });
            current_code_point++;
        }
    }

    return encoding;
}

NonnullRefPtr<Encoding> Encoding::standard_encoding()
{
    static NonnullRefPtr<Encoding> encoding = adopt_ref(*new Encoding());
    if (encoding->m_descriptors.is_empty()) {
#define ENUMERATE(string, name, standard_code, mac_code, win_code, pdf_code)   \
    auto name##_code_point = *Utf8View(string##sv).begin();                    \
    encoding->m_descriptors.set(standard_code, { string, name##_code_point }); \
    encoding->m_name_mapping.set(#name, name##_code_point);
        ENUMERATE_LATIN_CHARACTER_SET(ENUMERATE)
#undef ENUMERATE
    }

    return encoding;
}

NonnullRefPtr<Encoding> Encoding::mac_encoding()
{
    static NonnullRefPtr<Encoding> encoding = adopt_ref(*new Encoding());
    if (encoding->m_descriptors.is_empty()) {
#define ENUMERATE(string, name, standard_code, mac_code, win_code, pdf_code) \
    auto name##_code_point = *Utf8View(string##sv).begin();                  \
    encoding->m_descriptors.set(mac_code, { string, name##_code_point });    \
    encoding->m_name_mapping.set(#name, name##_code_point);
        ENUMERATE_LATIN_CHARACTER_SET(ENUMERATE)
#undef ENUMERATE
    }

    return encoding;
}

NonnullRefPtr<Encoding> Encoding::windows_encoding()
{
    static NonnullRefPtr<Encoding> encoding = adopt_ref(*new Encoding());
    if (encoding->m_descriptors.is_empty()) {
#define ENUMERATE(string, name, standard_code, mac_code, win_code, pdf_code) \
    auto name##_code_point = *Utf8View(string##sv).begin();                  \
    encoding->m_descriptors.set(win_code, { string, name##_code_point });    \
    encoding->m_name_mapping.set(#name, name##_code_point);
        ENUMERATE_LATIN_CHARACTER_SET(ENUMERATE)
#undef ENUMERATE
        encoding->m_windows = true;
    }

    return encoding;
}

NonnullRefPtr<Encoding> Encoding::pdf_doc_encoding()
{
    static NonnullRefPtr<Encoding> encoding = adopt_ref(*new Encoding());
    if (encoding->m_descriptors.is_empty()) {
#define ENUMERATE(string, name, standard_code, mac_code, win_code, pdf_code) \
    auto name##_code_point = *Utf8View(string##sv).begin();                  \
    encoding->m_descriptors.set(pdf_code, { string, name##_code_point });    \
    encoding->m_name_mapping.set(#name, name##_code_point);
        ENUMERATE_LATIN_CHARACTER_SET(ENUMERATE)
#undef ENUMERATE
    }

    return encoding;
}

NonnullRefPtr<Encoding> Encoding::symbol_encoding()
{
    static NonnullRefPtr<Encoding> encoding = adopt_ref(*new Encoding());
    if (encoding->m_descriptors.is_empty()) {
#define ENUMERATE(string, name, code)                                 \
    auto name##_code_point = *Utf8View(string##sv).begin();           \
    encoding->m_descriptors.set(code, { string, name##_code_point }); \
    encoding->m_name_mapping.set(#name, name##_code_point);
        ENUMERATE_SYMBOL_CHARACTER_SET(ENUMERATE)
#undef ENUMERATE
    }

    return encoding;
}

NonnullRefPtr<Encoding> Encoding::zapf_encoding()
{
    static NonnullRefPtr<Encoding> encoding = adopt_ref(*new Encoding());
    if (encoding->m_descriptors.is_empty()) {
#define ENUMERATE(string, name, code)                                 \
    auto name##_code_point = *Utf8View(string##sv).begin();           \
    encoding->m_descriptors.set(code, { string, name##_code_point }); \
    encoding->m_name_mapping.set(#name, name##_code_point);
        ENUMERATE_ZAPF_DINGBATS_CHARACTER_SET(ENUMERATE)
#undef ENUMERATE
    }

    return encoding;
}

CharDescriptor const& Encoding::get_char_code_descriptor(u16 char_code) const
{
    return const_cast<Encoding*>(this)->m_descriptors.ensure(char_code);
}

bool Encoding::should_map_to_bullet(u16 char_code) const
{
    // PDF Annex D table D.2, note 3:
    // In WinAnsiEncoding, all unused codes greater than 40 (octal) map to the bullet character. However, only
    // code 225 (octal) shall be specifically assigned to the bullet character; other codes are subject to future re-assignment.
    return m_windows && char_code > 040 && !m_descriptors.contains(char_code);
}

}