|
| 1 | +#include <odr/internal/pdf/pdf_cmap_parser.hpp> |
| 2 | + |
| 3 | +#include <odr/internal/pdf/pdf_cmap.hpp> |
| 4 | +#include <odr/internal/util/byte_util.hpp> |
| 5 | + |
| 6 | +#include <iostream> |
| 7 | + |
| 8 | +namespace odr::internal::pdf { |
| 9 | + |
| 10 | +using char_type = std::streambuf::char_type; |
| 11 | +using int_type = std::streambuf::int_type; |
| 12 | +static constexpr int_type eof = std::streambuf::traits_type::eof(); |
| 13 | + |
| 14 | +CMapParser::CMapParser(std::istream &in) : m_parser(in) {} |
| 15 | + |
| 16 | +std::istream &CMapParser::in() const { return m_parser.in(); } |
| 17 | + |
| 18 | +std::streambuf &CMapParser::sb() const { return m_parser.sb(); } |
| 19 | + |
| 20 | +const ObjectParser &CMapParser::parser() const { return m_parser; } |
| 21 | + |
| 22 | +std::variant<Object, std::string> CMapParser::read_token() const { |
| 23 | + if (m_parser.peek_number()) { |
| 24 | + return std::visit([](auto n) { return Object(n); }, |
| 25 | + m_parser.read_integer_or_real()); |
| 26 | + } |
| 27 | + if (m_parser.peek_string()) { |
| 28 | + return std::visit([](auto s) { return Object(std::move(s)); }, |
| 29 | + m_parser.read_string()); |
| 30 | + } |
| 31 | + if (m_parser.peek_name()) { |
| 32 | + return m_parser.read_name(); |
| 33 | + } |
| 34 | + if (m_parser.peek_dictionary()) { |
| 35 | + return m_parser.read_dictionary(); |
| 36 | + } |
| 37 | + |
| 38 | + std::string token; |
| 39 | + while (true) { |
| 40 | + int_type c = sb().sgetc(); |
| 41 | + if (c == eof) { |
| 42 | + in().setstate(std::ios::eofbit); |
| 43 | + return token; |
| 44 | + } |
| 45 | + if (ObjectParser::is_whitespace(c)) { |
| 46 | + return token; |
| 47 | + } |
| 48 | + sb().sbumpc(); |
| 49 | + token += (char_type)c; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void CMapParser::read_codespacerange(std::uint32_t n, CMap &cmap) const { |
| 54 | + m_parser.skip_whitespace(); |
| 55 | + for (std::uint32_t i = 0; i < n; ++i) { |
| 56 | + auto from_glyph = m_parser.read_object(); |
| 57 | + m_parser.skip_whitespace(); |
| 58 | + auto to_glyph = m_parser.read_object(); |
| 59 | + m_parser.skip_whitespace(); |
| 60 | + |
| 61 | + // TODO |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +void CMapParser::read_bfchar(std::uint32_t n, CMap &cmap) const { |
| 66 | + m_parser.skip_whitespace(); |
| 67 | + for (std::uint32_t i = 0; i < n; ++i) { |
| 68 | + std::string glyph = m_parser.read_object().as_string(); |
| 69 | + m_parser.skip_whitespace(); |
| 70 | + std::string unicode = m_parser.read_object().as_string(); |
| 71 | + m_parser.skip_whitespace(); |
| 72 | + |
| 73 | + util::reverse_bytes(reinterpret_cast<char16_t *>(unicode.data()), |
| 74 | + (std::size_t)unicode.size() / 2); |
| 75 | + std::u16string_view unicode16( |
| 76 | + reinterpret_cast<const char16_t *>(unicode.data()), unicode.size() / 2); |
| 77 | + |
| 78 | + if (glyph.length() != 1) { |
| 79 | + throw std::runtime_error("unexpected glyph length"); |
| 80 | + } |
| 81 | + if (unicode16.length() != 1) { |
| 82 | + throw std::runtime_error("unexpected unicode length"); |
| 83 | + } |
| 84 | + |
| 85 | + cmap.map_bfchar(glyph[0], unicode16[0]); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void CMapParser::read_bfrange(std::uint32_t n, CMap &cmap) const { |
| 90 | + m_parser.skip_whitespace(); |
| 91 | + for (std::uint32_t i = 0; i < n; ++i) { |
| 92 | + auto from_glyph = m_parser.read_object(); |
| 93 | + m_parser.skip_whitespace(); |
| 94 | + auto to_glyph = m_parser.read_object(); |
| 95 | + m_parser.skip_whitespace(); |
| 96 | + auto unicode = m_parser.read_object(); |
| 97 | + m_parser.skip_whitespace(); |
| 98 | + |
| 99 | + // TODO |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +CMap CMapParser::parse_cmap() const { |
| 104 | + CMap cmap; |
| 105 | + |
| 106 | + std::uint32_t last_int{}; |
| 107 | + |
| 108 | + m_parser.skip_whitespace(); |
| 109 | + while (true) { |
| 110 | + Token token = read_token(); |
| 111 | + if (in().eof()) { |
| 112 | + break; |
| 113 | + } |
| 114 | + m_parser.skip_whitespace(); |
| 115 | + |
| 116 | + if (std::holds_alternative<Object>(token)) { |
| 117 | + const Object &object = std::get<Object>(token); |
| 118 | + if (object.is_integer()) { |
| 119 | + last_int = object.as_integer(); |
| 120 | + } |
| 121 | + } else if (std::holds_alternative<std::string>(token)) { |
| 122 | + const std::string &command = std::get<std::string>(token); |
| 123 | + if (command == "begincodespacerange") { |
| 124 | + read_codespacerange(last_int, cmap); |
| 125 | + } else if (command == "beginbfchar") { |
| 126 | + read_bfchar(last_int, cmap); |
| 127 | + } else if (command == "beginbfrange") { |
| 128 | + read_bfrange(last_int, cmap); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return cmap; |
| 134 | +} |
| 135 | + |
| 136 | +} // namespace odr::internal::pdf |
0 commit comments