/* * Copyright (c) 2022, Matthew Olsson * Copyright (c) 2022, Julian Offenhäuser * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include namespace PDF { PDFErrorOr> Encoding::create(HashMap 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> Encoding::from_object(Document* document, NonnullRefPtr const& obj) { if (obj->is()) { auto name = obj->cast()->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(); RefPtr 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>()); VERIFY(!first); auto& object = item.get>(); auto name = object->cast()->name(); encoding->m_descriptors.set(current_code_point, { name, base_encoding->m_name_mapping.ensure(name) }); current_code_point++; } } return encoding; } NonnullRefPtr Encoding::standard_encoding() { static NonnullRefPtr encoding = adopt_ref(*new Encoding()); if (encoding->m_descriptors.is_empty()) { #define ENUMERATE(name, standard_code, mac_code, win_code, pdf_code) \ encoding->m_descriptors.set(standard_code, { #name, 0 }); \ encoding->m_name_mapping.set(#name, standard_code); ENUMERATE_LATIN_CHARACTER_SET(ENUMERATE) #undef ENUMERATE } return encoding; } NonnullRefPtr Encoding::mac_encoding() { static NonnullRefPtr encoding = adopt_ref(*new Encoding()); if (encoding->m_descriptors.is_empty()) { #define ENUMERATE(name, standard_code, mac_code, win_code, pdf_code) \ encoding->m_descriptors.set(mac_code, { #name, 0 }); \ encoding->m_name_mapping.set(#name, mac_code); ENUMERATE_LATIN_CHARACTER_SET(ENUMERATE) #undef ENUMERATE } return encoding; } NonnullRefPtr Encoding::windows_encoding() { static NonnullRefPtr encoding = adopt_ref(*new Encoding()); if (encoding->m_descriptors.is_empty()) { #define ENUMERATE(name, standard_code, mac_code, win_code, pdf_code) \ encoding->m_descriptors.set(win_code, { #name, 0 }); \ encoding->m_name_mapping.set(#name, win_code); ENUMERATE_LATIN_CHARACTER_SET(ENUMERATE) #undef ENUMERATE encoding->m_windows = true; } return encoding; } NonnullRefPtr Encoding::pdf_doc_encoding() { static NonnullRefPtr encoding = adopt_ref(*new Encoding()); if (encoding->m_descriptors.is_empty()) { #define ENUMERATE(name, standard_code, mac_code, win_code, pdf_code) \ encoding->m_descriptors.set(pdf_code, { #name, 0 }); \ encoding->m_name_mapping.set(#name, pdf_code); ENUMERATE_LATIN_CHARACTER_SET(ENUMERATE) #undef ENUMERATE } return encoding; } NonnullRefPtr Encoding::symbol_encoding() { static NonnullRefPtr encoding = adopt_ref(*new Encoding()); if (encoding->m_descriptors.is_empty()) { #define ENUMERATE(name, code) \ encoding->m_descriptors.set(code, { #name, 0 }); \ encoding->m_name_mapping.set(#name, code); ENUMERATE_SYMBOL_CHARACTER_SET(ENUMERATE) #undef ENUMERATE } return encoding; } NonnullRefPtr Encoding::zapf_encoding() { static NonnullRefPtr encoding = adopt_ref(*new Encoding()); if (encoding->m_descriptors.is_empty()) { #define ENUMERATE(name, code) \ encoding->m_descriptors.set(code, { #name, 0 }); \ encoding->m_name_mapping.set(#name, code); ENUMERATE_ZAPF_DINGBATS_CHARACTER_SET(ENUMERATE) #undef ENUMERATE } return encoding; } CharDescriptor const& Encoding::get_char_code_descriptor(u16 char_code) const { return const_cast(this)->m_descriptors.ensure(char_code); } u16 Encoding::get_char_code(DeprecatedString const& name) const { auto code_iterator = m_name_mapping.find(name); if (code_iterator != m_name_mapping.end()) return code_iterator->value; return 0; } 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); } }