PS1FontProgram.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2022, Julian Offenhäuser <offenhaeuser@protonmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibPDF/CommonNames.h>
  7. #include <LibPDF/Encoding.h>
  8. #include <LibPDF/Fonts/PS1FontProgram.h>
  9. #include <LibPDF/Reader.h>
  10. #include <ctype.h>
  11. #include <math.h>
  12. namespace PDF {
  13. PDFErrorOr<NonnullRefPtr<Type1FontProgram>> PS1FontProgram::create(ReadonlyBytes const& bytes, RefPtr<Encoding> encoding, size_t cleartext_length, size_t encrypted_length)
  14. {
  15. Reader reader(bytes);
  16. if (reader.remaining() == 0)
  17. return error("Empty font program");
  18. reader.move_to(0);
  19. if (reader.remaining() < 2 || !reader.matches("%!"))
  20. return error("Not a font program");
  21. if (!seek_name(reader, CommonNames::Encoding))
  22. return error("Missing encoding array");
  23. auto font_program = adopt_ref(*new PS1FontProgram());
  24. if (encoding) {
  25. // 9.6.6.2 Encodings for Type 1 Fonts:
  26. // An Encoding entry may override a Type 1 font’s mapping from character codes to character names.
  27. font_program->set_encoding(move(encoding));
  28. } else {
  29. if (TRY(parse_word(reader)) == "StandardEncoding") {
  30. font_program->set_encoding(Encoding::standard_encoding());
  31. } else {
  32. auto encoding = Encoding::create();
  33. while (reader.remaining()) {
  34. auto word = TRY(parse_word(reader));
  35. if (word == "readonly") {
  36. break;
  37. } else if (word == "dup") {
  38. u8 char_code = TRY(parse_int(reader));
  39. auto name = TRY(parse_word(reader));
  40. encoding->set(char_code, name.starts_with('/') ? name.substring_view(1) : name.view());
  41. }
  42. }
  43. font_program->set_encoding(move(encoding));
  44. }
  45. }
  46. bool found_font_matrix = seek_name(reader, "FontMatrix");
  47. if (found_font_matrix) {
  48. auto array = TRY(parse_number_array(reader, 6));
  49. font_program->set_font_matrix({ array[0], array[1], array[2], array[3], array[4], array[5] });
  50. } else {
  51. font_program->set_font_matrix({ 0.001f, 0.0f, 0.0f, 0.001f, 0.0f, 0.0f });
  52. }
  53. auto decrypted = TRY(decrypt(reader.bytes().slice(cleartext_length, encrypted_length), 55665, 4));
  54. TRY(font_program->parse_encrypted_portion(decrypted));
  55. return font_program;
  56. }
  57. PDFErrorOr<void> PS1FontProgram::parse_encrypted_portion(ByteBuffer const& buffer)
  58. {
  59. Reader reader(buffer);
  60. if (seek_name(reader, "lenIV"))
  61. m_lenIV = TRY(parse_int(reader));
  62. Vector<ByteBuffer> subroutines;
  63. if (seek_name(reader, "Subrs"))
  64. subroutines = TRY(parse_subroutines(reader));
  65. if (!seek_name(reader, "CharStrings"))
  66. return error("Missing char strings array");
  67. while (reader.remaining()) {
  68. auto word = TRY(parse_word(reader));
  69. VERIFY(!word.is_empty());
  70. if (word == "end")
  71. break;
  72. if (word[0] == '/') {
  73. auto encrypted_size = TRY(parse_int(reader));
  74. auto rd = TRY(parse_word(reader));
  75. if (rd == "-|" || rd == "RD") {
  76. auto line = TRY(decrypt(reader.bytes().slice(reader.offset(), encrypted_size), m_encryption_key, m_lenIV));
  77. reader.move_by(encrypted_size);
  78. auto glyph_name = word.substring_view(1);
  79. GlyphParserState state;
  80. TRY(add_glyph(glyph_name, TRY(parse_glyph(line, subroutines, {}, state, false))));
  81. }
  82. }
  83. }
  84. consolidate_glyphs();
  85. return {};
  86. }
  87. PDFErrorOr<Vector<ByteBuffer>> PS1FontProgram::parse_subroutines(Reader& reader) const
  88. {
  89. if (!reader.matches_number())
  90. return error("Expected array length");
  91. auto length = TRY(parse_int(reader));
  92. VERIFY(length >= 0);
  93. Vector<ByteBuffer> array;
  94. TRY(array.try_resize(length));
  95. while (reader.remaining()) {
  96. auto word = TRY(parse_word(reader));
  97. VERIFY(!word.is_empty());
  98. if (word == "dup") {
  99. auto index = TRY(parse_int(reader));
  100. auto entry = TRY(parse_word(reader));
  101. if (entry.is_empty())
  102. return error("Empty array entry");
  103. if (index >= length)
  104. return error("Array index out of bounds");
  105. if (isdigit(entry[0])) {
  106. auto maybe_encrypted_size = entry.to_number<int>();
  107. if (!maybe_encrypted_size.has_value())
  108. return error("Malformed array");
  109. auto rd = TRY(parse_word(reader));
  110. if (rd == "-|" || rd == "RD") {
  111. array[index] = TRY(decrypt(reader.bytes().slice(reader.offset(), maybe_encrypted_size.value()), m_encryption_key, m_lenIV));
  112. reader.move_by(maybe_encrypted_size.value());
  113. }
  114. } else {
  115. array[index] = TRY(ByteBuffer::copy(entry.bytes()));
  116. }
  117. } else if (word == "index" || word == "def" || word == "ND") {
  118. break;
  119. }
  120. }
  121. return array;
  122. }
  123. PDFErrorOr<Vector<float>> PS1FontProgram::parse_number_array(Reader& reader, size_t length)
  124. {
  125. Vector<float> array;
  126. TRY(array.try_resize(length));
  127. reader.consume_whitespace();
  128. if (!reader.consume('['))
  129. return error("Expected array to start with '['");
  130. reader.consume_whitespace();
  131. for (size_t i = 0; i < length; ++i)
  132. array.at(i) = TRY(parse_float(reader));
  133. if (!reader.consume(']'))
  134. return error("Expected array to end with ']'");
  135. return array;
  136. }
  137. PDFErrorOr<ByteString> PS1FontProgram::parse_word(Reader& reader)
  138. {
  139. reader.consume_whitespace();
  140. auto start = reader.offset();
  141. reader.move_while([&](char c) {
  142. return !reader.matches_whitespace() && c != '[' && c != ']';
  143. });
  144. auto end = reader.offset();
  145. if (reader.matches_whitespace())
  146. reader.consume();
  147. return StringView(reader.bytes().data() + start, end - start);
  148. }
  149. PDFErrorOr<float> PS1FontProgram::parse_float(Reader& reader)
  150. {
  151. auto word = TRY(parse_word(reader));
  152. return strtof(ByteString(word).characters(), nullptr);
  153. }
  154. PDFErrorOr<int> PS1FontProgram::parse_int(Reader& reader)
  155. {
  156. auto maybe_int = TRY(parse_word(reader)).to_number<int>();
  157. if (!maybe_int.has_value())
  158. return error("Invalid int");
  159. return maybe_int.value();
  160. }
  161. PDFErrorOr<ByteBuffer> PS1FontProgram::decrypt(ReadonlyBytes const& encrypted, u16 key, size_t skip)
  162. {
  163. auto decrypted = TRY(ByteBuffer::create_uninitialized(encrypted.size() - skip));
  164. u16 R = key;
  165. u16 c1 = 52845;
  166. u16 c2 = 22719;
  167. for (size_t i = 0; i < encrypted.size(); ++i) {
  168. u8 C = encrypted[i];
  169. u8 P = C ^ (R >> 8);
  170. R = (C + R) * c1 + c2;
  171. if (i >= skip)
  172. decrypted[i - skip] = P;
  173. }
  174. return decrypted;
  175. }
  176. bool PS1FontProgram::seek_name(Reader& reader, ByteString const& name)
  177. {
  178. auto start = reader.offset();
  179. reader.move_to(0);
  180. while (reader.remaining()) {
  181. if (reader.consume('/') && reader.matches(name.characters())) {
  182. // Skip name
  183. reader.move_while([&](char) {
  184. return reader.matches_regular_character();
  185. });
  186. reader.consume_whitespace();
  187. return true;
  188. }
  189. }
  190. // Jump back to where we started
  191. reader.move_to(start);
  192. return false;
  193. }
  194. }