CFF.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Copyright (c) 2023, Rodrigo Tobar <rtobarc@gmail.com>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. // CFF spec: https://adobe-type-tools.github.io/font-tech-notes/pdfs/5176.CFF.pdf
  7. #include <AK/Debug.h>
  8. #include <AK/Endian.h>
  9. #include <AK/String.h>
  10. #include <LibGfx/Forward.h>
  11. #include <LibPDF/Encoding.h>
  12. #include <LibPDF/Error.h>
  13. #include <LibPDF/Fonts/CFF.h>
  14. #include <LibPDF/Reader.h>
  15. namespace PDF {
  16. // The built-in encodings map codes to SIDs.
  17. // CFF spec, "Appendix B Predefined Encodings, Standard Encoding"
  18. // clang-format off
  19. static constexpr Array s_predefined_encoding_standard = to_array<CFF::SID>({
  20. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  22. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  23. 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,
  24. 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,
  25. 90, 91, 92, 93, 94, 95, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 0, 111, 112,
  27. 113, 114, 0, 115, 116, 117, 118, 119, 120, 121, 122, 0, 123, 0, 124, 125, 126, 127, 128, 129, 130, 131, 0, 132, 133, 0, 134, 135, 136,
  28. 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138, 0, 139, 0, 0, 0, 0, 140, 141, 142, 143, 0,
  29. 0, 0, 0, 0, 144, 0, 0,
  30. 0, 145, 0, 0, 146, 147, 148,
  31. 149, 0, 0, 0, 0,
  32. });
  33. static_assert(s_predefined_encoding_standard.size() == 256);
  34. // clang-format on
  35. // CFF spec, "Appendix B Predefined Encodings, Expert Encoding"
  36. // clang-format off
  37. static constexpr Array s_predefined_encoding_expert = to_array<CFF::SID>({
  38. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  39. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 229, 230, 0,
  40. 231, 232, 233, 234, 235, 236, 237, 238, 13, 14, 15, 99, 239, 240, 241, 242, 243, 244,
  41. 245, 246, 247, 248, 27, 28, 249, 250, 251, 252, 0, 253, 254, 255, 256, 257, 0, 0, 0, 258, 0, 0, 259, 260, 261, 262, 0, 0, 263,
  42. 264, 265, 0, 266, 109, 110, 267, 268, 269, 0, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288,
  43. 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 304, 305, 306, 0, 0, 307, 308, 309, 310,
  45. 311, 0, 312, 0, 0, 313, 0, 0, 314, 315, 0, 0, 316, 317, 318, 0, 0, 0, 158, 155, 163, 319, 320, 321, 322, 323, 324, 325, 0,
  46. 0, 326, 150, 164, 169, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350,
  47. 351, 352, 353, 354, 355, 356, 357, 358, 359, 360,
  48. 361, 362, 363, 364, 365, 366, 367, 368, 369, 370,
  49. 371, 372, 373, 374, 375, 376, 377, 378,
  50. });
  51. static_assert(s_predefined_encoding_expert.size() == 256);
  52. // clang-format on
  53. // Charsets map GIDs to SIDs.
  54. // CFF spec, "Appendix C Predefined Charsets, Expert"
  55. // clang-format off
  56. static constexpr auto s_predefined_charset_expert = to_array<CFF::SID>({
  57. 1, 229, 230, 231, 232,
  58. 233, 234, 235, 236, 237,
  59. 238, 13, 14, 15, 99,
  60. 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 27, 28, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 109, 110,
  61. 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298,
  62. 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 158, 155, 163, 319, 320, 321, 322, 323, 324, 325, 326, 150,
  63. 164, 169, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342,
  64. 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360,
  65. 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378,
  66. });
  67. // clang-format on
  68. // CFF spec, "Appendix C Predefined Charsets, Expert Subset"
  69. // clang-format off
  70. static constexpr auto s_predefined_charset_expert_subset = to_array<CFF::SID>({
  71. 1, 231, 232, 235, 236, 237, 238, 13, 14, 15, 99,
  72. 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 27,
  73. 28, 249, 250, 251, 253, 254, 255, 256, 257, 258, 259,
  74. 260, 261, 262, 263, 264, 265, 266, 109, 110, 267, 268, 269, 270, 272, 300, 301, 302, 305,
  75. 314, 315, 158, 155, 163, 320, 321, 322, 323, 324, 325, 326, 150, 164, 169, 327, 328, 329,
  76. 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346,
  77. });
  78. // clang-format on
  79. PDFErrorOr<NonnullRefPtr<CFF>> CFF::create(ReadonlyBytes const& cff_bytes, RefPtr<Encoding> encoding)
  80. {
  81. Reader reader(cff_bytes);
  82. // CFF spec, "6 Header"
  83. // skip major, minor version
  84. reader.consume(2);
  85. auto header_size = TRY(reader.try_read<Card8>());
  86. // skip offset size
  87. reader.consume(1);
  88. reader.move_to(header_size);
  89. // CFF spec, "7 Name INDEX"
  90. Vector<String> font_names;
  91. TRY(parse_index(reader, [&](ReadonlyBytes const& data) -> PDFErrorOr<void> {
  92. auto font_name = TRY(String::from_utf8(data));
  93. dbgln_if(CFF_DEBUG, "CFF font name '{}'", font_name);
  94. return TRY(font_names.try_append(font_name));
  95. }));
  96. if (font_names.size() != 1)
  97. return error("CFFs with more than one font not yet implemented");
  98. auto cff = adopt_ref(*new CFF());
  99. cff->set_font_matrix({ 0.001f, 0.0f, 0.0f, 0.001f, 0.0f, 0.0f });
  100. // CFF spec, "8 Top DICT INDEX"
  101. int charset_offset = 0;
  102. int encoding_offset = 0;
  103. auto charstrings_offset = 0;
  104. Vector<ByteBuffer> local_subroutines;
  105. float defaultWidthX = 0;
  106. float nominalWidthX = 0;
  107. int fdselect_offset = 0;
  108. TRY(parse_index(reader, [&](ReadonlyBytes const& element_data) {
  109. Reader element_reader { element_data };
  110. return parse_dict<TopDictOperator>(element_reader, [&](TopDictOperator op, Vector<DictOperand> const& operands) -> PDFErrorOr<void> {
  111. switch (op) {
  112. case TopDictOperator::Version:
  113. case TopDictOperator::Notice:
  114. case TopDictOperator::FullName:
  115. case TopDictOperator::FamilyName:
  116. case TopDictOperator::Weight:
  117. case TopDictOperator::FontBBox:
  118. case TopDictOperator::UniqueID:
  119. case TopDictOperator::XUID:
  120. case TopDictOperator::Copyright:
  121. case TopDictOperator::IsFixedPitch:
  122. case TopDictOperator::ItalicAngle:
  123. case TopDictOperator::UnderlinePosition:
  124. case TopDictOperator::UnderlineThickness:
  125. case TopDictOperator::PaintType:
  126. case TopDictOperator::FontMatrix:
  127. case TopDictOperator::StrokeWidth:
  128. case TopDictOperator::PostScript:
  129. case TopDictOperator::BaseFontName:
  130. case TopDictOperator::BaseFontBlend:
  131. break;
  132. case TopDictOperator::CharstringType: {
  133. int charstring_type = 2;
  134. if (!operands.is_empty())
  135. charstring_type = operands[0].get<int>();
  136. if (charstring_type != 2)
  137. dbgln("CFF: has unimplemented CharstringType, might not look right");
  138. break;
  139. }
  140. case TopDictOperator::SyntheticBase:
  141. dbgln("CFF: has unimplemented SyntheticBase, might not look right");
  142. break;
  143. case TopDictOperator::Encoding: {
  144. if (!operands.is_empty())
  145. encoding_offset = operands[0].get<int>();
  146. break;
  147. }
  148. case TopDictOperator::Charset: {
  149. if (!operands.is_empty())
  150. charset_offset = operands[0].get<int>();
  151. break;
  152. }
  153. case TopDictOperator::CharStrings: {
  154. if (!operands.is_empty())
  155. charstrings_offset = operands[0].get<int>();
  156. break;
  157. }
  158. case TopDictOperator::Private: {
  159. auto private_dict_size = operands[0].get<int>();
  160. auto private_dict_offset = operands[1].get<int>();
  161. Reader priv_dict_reader { cff_bytes.slice(private_dict_offset, private_dict_size) };
  162. TRY(parse_dict<PrivDictOperator>(priv_dict_reader, [&](PrivDictOperator op, Vector<DictOperand> const& operands) -> PDFErrorOr<void> {
  163. switch (op) {
  164. case PrivDictOperator::BlueValues:
  165. case PrivDictOperator::OtherBlues:
  166. case PrivDictOperator::FamilyBlues:
  167. case PrivDictOperator::FamilyOtherBlues:
  168. case PrivDictOperator::BlueScale:
  169. case PrivDictOperator::BlueShift:
  170. case PrivDictOperator::BlueFuzz:
  171. case PrivDictOperator::StemSnapH:
  172. case PrivDictOperator::StemSnapV:
  173. case PrivDictOperator::ForceBold:
  174. case PrivDictOperator::LanguageGroup:
  175. case PrivDictOperator::ExpansionFactor:
  176. case PrivDictOperator::InitialRandomSeed:
  177. // Ignore hinting-related operators for now.
  178. break;
  179. case PrivDictOperator::StdHW:
  180. case PrivDictOperator::StdVW:
  181. // FIXME: What do these do?
  182. break;
  183. case PrivDictOperator::Subrs: {
  184. // CFF spec, "16 Local/Global Subrs INDEXes"
  185. // "Local subrs are stored in an INDEX structure which is located via the offset operand of the Subrs operator in the Private DICT."
  186. auto subrs_offset = operands[0].get<int>();
  187. Reader subrs_reader { cff_bytes.slice(private_dict_offset + subrs_offset) };
  188. TRY(parse_index(subrs_reader, [&](ReadonlyBytes const& subroutine_bytes) -> PDFErrorOr<void> {
  189. return TRY(local_subroutines.try_append(TRY(ByteBuffer::copy(subroutine_bytes))));
  190. }));
  191. dbgln_if(CFF_DEBUG, "CFF has {} subr entries", local_subroutines.size());
  192. break;
  193. }
  194. case PrivDictOperator::DefaultWidthX:
  195. if (!operands.is_empty())
  196. defaultWidthX = to_number(operands[0]);
  197. break;
  198. case PrivDictOperator::NominalWidthX:
  199. if (!operands.is_empty())
  200. nominalWidthX = to_number(operands[0]);
  201. break;
  202. default:
  203. dbgln("CFF: Unhandled private dict entry {}", static_cast<int>(op));
  204. }
  205. return {};
  206. }));
  207. break;
  208. }
  209. case TopDictOperator::FDSelect:
  210. if (!operands.is_empty())
  211. fdselect_offset = operands[0].get<int>();
  212. break;
  213. case TopDictOperator::CIDFontVersion:
  214. case TopDictOperator::CIDFontRevision:
  215. case TopDictOperator::CIDFontType:
  216. case TopDictOperator::CIDCount:
  217. case TopDictOperator::UIDBase:
  218. case TopDictOperator::FDArray:
  219. case TopDictOperator::FontName:
  220. // Keys for CID-keyed fonts that we don't need, at least at the moment.
  221. break;
  222. default:
  223. dbgln("CFF: Unhandled top dict entry {}", static_cast<int>(op));
  224. }
  225. return {};
  226. });
  227. }));
  228. auto strings = TRY(parse_strings(reader));
  229. // CFF spec "16 Local/Global Subrs INDEXes"
  230. // "Global subrs are stored in an INDEX structure which follows the String INDEX."
  231. Vector<ByteBuffer> global_subroutines;
  232. TRY(parse_index(reader, [&](ReadonlyBytes const& subroutine_bytes) -> PDFErrorOr<void> {
  233. return TRY(global_subroutines.try_append(TRY(ByteBuffer::copy(subroutine_bytes))));
  234. }));
  235. dbgln_if(CFF_DEBUG, "CFF has {} gsubr entries", global_subroutines.size());
  236. // Create glyphs (now that we have the subroutines) and associate missing information to store them and their encoding
  237. auto glyphs = TRY(parse_charstrings(Reader(cff_bytes.slice(charstrings_offset)), local_subroutines, global_subroutines));
  238. // CFF spec, "Table 16 Encoding ID"
  239. // FIXME: Only read this if the built-in encoding is actually needed? (ie. `if (!encoding)`)
  240. Vector<u8> encoding_codes; // Maps GID to its codepoint.
  241. HashMap<Card8, SID> encoding_supplemental; // Maps codepoint to SID.
  242. switch (encoding_offset) {
  243. case 0:
  244. dbgln_if(CFF_DEBUG, "CFF predefined encoding Standard");
  245. for (size_t i = 1; i < s_predefined_encoding_standard.size(); ++i)
  246. TRY(encoding_supplemental.try_set(i, s_predefined_encoding_standard[i]));
  247. break;
  248. case 1:
  249. dbgln_if(CFF_DEBUG, "CFF predefined encoding Expert");
  250. for (size_t i = 1; i < s_predefined_encoding_expert.size(); ++i)
  251. TRY(encoding_supplemental.try_set(i, s_predefined_encoding_expert[i]));
  252. break;
  253. default:
  254. encoding_codes = TRY(parse_encoding(Reader(cff_bytes.slice(encoding_offset)), encoding_supplemental));
  255. break;
  256. }
  257. // CFF spec, "Table 22 Charset ID"
  258. Vector<DeprecatedFlyString> charset_names;
  259. switch (charset_offset) {
  260. case 0:
  261. dbgln_if(CFF_DEBUG, "CFF predefined charset ISOAdobe");
  262. // CFF spec, "Appendix C Predefined Charsets, ISOAdobe"
  263. for (SID sid = 1; sid <= 228; sid++)
  264. TRY(charset_names.try_append(resolve_sid(sid, strings)));
  265. break;
  266. case 1:
  267. dbgln_if(CFF_DEBUG, "CFF predefined charset Expert");
  268. for (SID sid : s_predefined_charset_expert)
  269. TRY(charset_names.try_append(resolve_sid(sid, strings)));
  270. break;
  271. case 2:
  272. dbgln_if(CFF_DEBUG, "CFF predefined charset Expert Subset");
  273. for (SID sid : s_predefined_charset_expert_subset)
  274. TRY(charset_names.try_append(resolve_sid(sid, strings)));
  275. break;
  276. default: {
  277. auto charset = TRY(parse_charset(Reader { cff_bytes.slice(charset_offset) }, glyphs.size()));
  278. for (SID sid : charset)
  279. TRY(charset_names.try_append(resolve_sid(sid, strings)));
  280. break;
  281. }
  282. }
  283. // CFF spec, "19 FDSelect"
  284. if (fdselect_offset != 0) {
  285. auto fdselect = TRY(parse_fdselect(Reader { cff_bytes.slice(fdselect_offset) }, glyphs.size()));
  286. dbgln_if(CFF_DEBUG, "CFF has {} FDSelect entries", fdselect.size());
  287. }
  288. // Adjust glyphs' widths as they are deltas from nominalWidthX
  289. for (auto& glyph : glyphs) {
  290. if (!glyph.has_width())
  291. glyph.set_width(defaultWidthX);
  292. else
  293. glyph.set_width(glyph.width() + nominalWidthX);
  294. }
  295. for (size_t i = 0; i < glyphs.size(); i++) {
  296. if (i == 0) {
  297. TRY(cff->add_glyph(0, move(glyphs[0])));
  298. continue;
  299. }
  300. auto const& name = charset_names[i - 1];
  301. TRY(cff->add_glyph(name, move(glyphs[i])));
  302. }
  303. cff->consolidate_glyphs();
  304. // Encoding given or read
  305. if (encoding) {
  306. dbgln_if(CFF_DEBUG, "CFF using external encoding");
  307. cff->set_encoding(move(encoding));
  308. } else {
  309. dbgln_if(CFF_DEBUG, "CFF using embedded encoding");
  310. auto encoding = Encoding::create();
  311. for (size_t i = 0; i < glyphs.size(); i++) {
  312. if (i == 0) {
  313. encoding->set(0, ".notdef");
  314. continue;
  315. }
  316. if (i >= encoding_codes.size() || i >= charset_names.size())
  317. break;
  318. auto code = encoding_codes[i - 1];
  319. auto char_name = charset_names[i - 1];
  320. encoding->set(code, char_name);
  321. }
  322. for (auto const& entry : encoding_supplemental)
  323. encoding->set(entry.key, resolve_sid(entry.value, strings));
  324. cff->set_encoding(move(encoding));
  325. }
  326. return cff;
  327. }
  328. /// Appendix A: Standard Strings
  329. static constexpr Array s_cff_builtin_names {
  330. ".notdef"sv,
  331. "space"sv,
  332. "exclam"sv,
  333. "quotedbl"sv,
  334. "numbersign"sv,
  335. "dollar"sv,
  336. "percent"sv,
  337. "ampersand"sv,
  338. "quoteright"sv,
  339. "parenleft"sv,
  340. "parenright"sv,
  341. "asterisk"sv,
  342. "plus"sv,
  343. "comma"sv,
  344. "hyphen"sv,
  345. "period"sv,
  346. "slash"sv,
  347. "zero"sv,
  348. "one"sv,
  349. "two"sv,
  350. "three"sv,
  351. "four"sv,
  352. "five"sv,
  353. "six"sv,
  354. "seven"sv,
  355. "eight"sv,
  356. "nine"sv,
  357. "colon"sv,
  358. "semicolon"sv,
  359. "less"sv,
  360. "equal"sv,
  361. "greater"sv,
  362. "question"sv,
  363. "at"sv,
  364. "A"sv,
  365. "B"sv,
  366. "C"sv,
  367. "D"sv,
  368. "E"sv,
  369. "F"sv,
  370. "G"sv,
  371. "H"sv,
  372. "I"sv,
  373. "J"sv,
  374. "K"sv,
  375. "L"sv,
  376. "M"sv,
  377. "N"sv,
  378. "O"sv,
  379. "P"sv,
  380. "Q"sv,
  381. "R"sv,
  382. "S"sv,
  383. "T"sv,
  384. "U"sv,
  385. "V"sv,
  386. "W"sv,
  387. "X"sv,
  388. "Y"sv,
  389. "Z"sv,
  390. "bracketleft"sv,
  391. "backslash"sv,
  392. "bracketright"sv,
  393. "asciicircum"sv,
  394. "underscore"sv,
  395. "quoteleft"sv,
  396. "a"sv,
  397. "b"sv,
  398. "c"sv,
  399. "d"sv,
  400. "e"sv,
  401. "f"sv,
  402. "g"sv,
  403. "h"sv,
  404. "i"sv,
  405. "j"sv,
  406. "k"sv,
  407. "l"sv,
  408. "m"sv,
  409. "n"sv,
  410. "o"sv,
  411. "p"sv,
  412. "q"sv,
  413. "r"sv,
  414. "s"sv,
  415. "t"sv,
  416. "u"sv,
  417. "v"sv,
  418. "w"sv,
  419. "x"sv,
  420. "y"sv,
  421. "z"sv,
  422. "braceleft"sv,
  423. "bar"sv,
  424. "braceright"sv,
  425. "asciitilde"sv,
  426. "exclamdown"sv,
  427. "cent"sv,
  428. "sterling"sv,
  429. "fraction"sv,
  430. "yen"sv,
  431. "florin"sv,
  432. "section"sv,
  433. "currency"sv,
  434. "quotesingle"sv,
  435. "quotedblleft"sv,
  436. "guillemotleft"sv,
  437. "guilsinglleft"sv,
  438. "guilsinglright"sv,
  439. "fi"sv,
  440. "fl"sv,
  441. "endash"sv,
  442. "dagger"sv,
  443. "daggerdbl"sv,
  444. "periodcentered"sv,
  445. "paragraph"sv,
  446. "bullet"sv,
  447. "quotesinglbase"sv,
  448. "quotedblbase"sv,
  449. "quotedblright"sv,
  450. "guillemotright"sv,
  451. "ellipsis"sv,
  452. "perthousand"sv,
  453. "questiondown"sv,
  454. "grave"sv,
  455. "acute"sv,
  456. "circumflex"sv,
  457. "tilde"sv,
  458. "macron"sv,
  459. "breve"sv,
  460. "dotaccent"sv,
  461. "dieresis"sv,
  462. "ring"sv,
  463. "cedilla"sv,
  464. "hungarumlaut"sv,
  465. "ogonek"sv,
  466. "caron"sv,
  467. "emdash"sv,
  468. "AE"sv,
  469. "ordfeminine"sv,
  470. "Lslash"sv,
  471. "Oslash"sv,
  472. "OE"sv,
  473. "ordmasculine"sv,
  474. "ae"sv,
  475. "dotlessi"sv,
  476. "lslash"sv,
  477. "oslash"sv,
  478. "oe"sv,
  479. "germandbls"sv,
  480. "onesuperior"sv,
  481. "logicalnot"sv,
  482. "mu"sv,
  483. "trademark"sv,
  484. "Eth"sv,
  485. "onehalf"sv,
  486. "plusminus"sv,
  487. "Thorn"sv,
  488. "onequarter"sv,
  489. "divide"sv,
  490. "brokenbar"sv,
  491. "degree"sv,
  492. "thorn"sv,
  493. "threequarters"sv,
  494. "twosuperior"sv,
  495. "registered"sv,
  496. "minus"sv,
  497. "eth"sv,
  498. "multiply"sv,
  499. "threesuperior"sv,
  500. "copyright"sv,
  501. "Aacute"sv,
  502. "Acircumflex"sv,
  503. "Adieresis"sv,
  504. "Agrave"sv,
  505. "Aring"sv,
  506. "Atilde"sv,
  507. "Ccedilla"sv,
  508. "Eacute"sv,
  509. "Ecircumflex"sv,
  510. "Edieresis"sv,
  511. "Egrave"sv,
  512. "Iacute"sv,
  513. "Icircumflex"sv,
  514. "Idieresis"sv,
  515. "Igrave"sv,
  516. "Ntilde"sv,
  517. "Oacute"sv,
  518. "Ocircumflex"sv,
  519. "Odieresis"sv,
  520. "Ograve"sv,
  521. "Otilde"sv,
  522. "Scaron"sv,
  523. "Uacute"sv,
  524. "Ucircumflex"sv,
  525. "Udieresis"sv,
  526. "Ugrave"sv,
  527. "Yacute"sv,
  528. "Ydieresis"sv,
  529. "Zcaron"sv,
  530. "aacute"sv,
  531. "acircumflex"sv,
  532. "adieresis"sv,
  533. "agrave"sv,
  534. "aring"sv,
  535. "atilde"sv,
  536. "ccedilla"sv,
  537. "eacute"sv,
  538. "ecircumflex"sv,
  539. "edieresis"sv,
  540. "egrave"sv,
  541. "iacute"sv,
  542. "icircumflex"sv,
  543. "idieresis"sv,
  544. "igrave"sv,
  545. "ntilde"sv,
  546. "oacute"sv,
  547. "ocircumflex"sv,
  548. "odieresis"sv,
  549. "ograve"sv,
  550. "otilde"sv,
  551. "scaron"sv,
  552. "uacute"sv,
  553. "ucircumflex"sv,
  554. "udieresis"sv,
  555. "ugrave"sv,
  556. "yacute"sv,
  557. "ydieresis"sv,
  558. "zcaron"sv,
  559. "exclamsmall"sv,
  560. "Hungarumlautsmall"sv,
  561. "dollaroldstyle"sv,
  562. "dollarsuperior"sv,
  563. "ampersandsmall"sv,
  564. "Acutesmall"sv,
  565. "parenleftsuperior"sv,
  566. "parenrightsuperior"sv,
  567. "twodotenleader"sv,
  568. "onedotenleader"sv,
  569. "zerooldstyle"sv,
  570. "oneoldstyle"sv,
  571. "twooldstyle"sv,
  572. "threeoldstyle"sv,
  573. "fouroldstyle"sv,
  574. "fiveoldstyle"sv,
  575. "sixoldstyle"sv,
  576. "sevenoldstyle"sv,
  577. "eightoldstyle"sv,
  578. "nineoldstyle"sv,
  579. "commasuperior"sv,
  580. "threequartersemdash"sv,
  581. "periodsuperior"sv,
  582. "questionsmall"sv,
  583. "asuperior"sv,
  584. "bsuperior"sv,
  585. "centsuperior"sv,
  586. "dsuperior"sv,
  587. "esuperior"sv,
  588. "isuperior"sv,
  589. "lsuperior"sv,
  590. "msuperior"sv,
  591. "nsuperior"sv,
  592. "osuperior"sv,
  593. "rsuperior"sv,
  594. "ssuperior"sv,
  595. "tsuperior"sv,
  596. "ff"sv,
  597. "ffi"sv,
  598. "ffl"sv,
  599. "parenleftinferior"sv,
  600. "parenrightinferior"sv,
  601. "Circumflexsmall"sv,
  602. "hyphensuperior"sv,
  603. "Gravesmall"sv,
  604. "Asmall"sv,
  605. "Bsmall"sv,
  606. "Csmall"sv,
  607. "Dsmall"sv,
  608. "Esmall"sv,
  609. "Fsmall"sv,
  610. "Gsmall"sv,
  611. "Hsmall"sv,
  612. "Ismall"sv,
  613. "Jsmall"sv,
  614. "Ksmall"sv,
  615. "Lsmall"sv,
  616. "Msmall"sv,
  617. "Nsmall"sv,
  618. "Osmall"sv,
  619. "Psmall"sv,
  620. "Qsmall"sv,
  621. "Rsmall"sv,
  622. "Ssmall"sv,
  623. "Tsmall"sv,
  624. "Usmall"sv,
  625. "Vsmall"sv,
  626. "Wsmall"sv,
  627. "Xsmall"sv,
  628. "Ysmall"sv,
  629. "Zsmall"sv,
  630. "colonmonetary"sv,
  631. "onefitted"sv,
  632. "rupiah"sv,
  633. "Tildesmall"sv,
  634. "exclamdownsmall"sv,
  635. "centoldstyle"sv,
  636. "Lslashsmall"sv,
  637. "Scaronsmall"sv,
  638. "Zcaronsmall"sv,
  639. "Dieresissmall"sv,
  640. "Brevesmall"sv,
  641. "Caronsmall"sv,
  642. "Dotaccentsmall"sv,
  643. "Macronsmall"sv,
  644. "figuredash"sv,
  645. "hypheninferior"sv,
  646. "Ogoneksmall"sv,
  647. "Ringsmall"sv,
  648. "Cedillasmall"sv,
  649. "questiondownsmall"sv,
  650. "oneeighth"sv,
  651. "threeeighths"sv,
  652. "fiveeighths"sv,
  653. "seveneighths"sv,
  654. "onethird"sv,
  655. "twothirds"sv,
  656. "zerosuperior"sv,
  657. "foursuperior"sv,
  658. "fivesuperior"sv,
  659. "sixsuperior"sv,
  660. "sevensuperior"sv,
  661. "eightsuperior"sv,
  662. "ninesuperior"sv,
  663. "zeroinferior"sv,
  664. "oneinferior"sv,
  665. "twoinferior"sv,
  666. "threeinferior"sv,
  667. "fourinferior"sv,
  668. "fiveinferior"sv,
  669. "sixinferior"sv,
  670. "seveninferior"sv,
  671. "eightinferior"sv,
  672. "nineinferior"sv,
  673. "centinferior"sv,
  674. "dollarinferior"sv,
  675. "periodinferior"sv,
  676. "commainferior"sv,
  677. "Agravesmall"sv,
  678. "Aacutesmall"sv,
  679. "Acircumflexsmall"sv,
  680. "Atildesmall"sv,
  681. "Adieresissmall"sv,
  682. "Aringsmall"sv,
  683. "AEsmall"sv,
  684. "Ccedillasmall"sv,
  685. "Egravesmall"sv,
  686. "Eacutesmall"sv,
  687. "Ecircumflexsmall"sv,
  688. "Edieresissmall"sv,
  689. "Igravesmall"sv,
  690. "Iacutesmall"sv,
  691. "Icircumflexsmall"sv,
  692. "Idieresissmall"sv,
  693. "Ethsmall"sv,
  694. "Ntildesmall"sv,
  695. "Ogravesmall"sv,
  696. "Oacutesmall"sv,
  697. "Ocircumflexsmall"sv,
  698. "Otildesmall"sv,
  699. "Odieresissmall"sv,
  700. "OEsmall"sv,
  701. "Oslashsmall"sv,
  702. "Ugravesmall"sv,
  703. "Uacutesmall"sv,
  704. "Ucircumflexsmall"sv,
  705. "Udieresissmall"sv,
  706. "Yacutesmall"sv,
  707. "Thornsmall"sv,
  708. "Ydieresissmall"sv,
  709. "001.000"sv,
  710. "001.001"sv,
  711. "001.002"sv,
  712. "001.003"sv,
  713. "Black"sv,
  714. "Bold"sv,
  715. "Book"sv,
  716. "Light"sv,
  717. "Medium"sv,
  718. "Regular"sv,
  719. "Roman"sv,
  720. "Semibold"sv,
  721. };
  722. PDFErrorOr<Vector<StringView>> CFF::parse_strings(Reader& reader)
  723. {
  724. // CFF spec "10 String Index"
  725. Vector<StringView> strings;
  726. TRY(parse_index(reader, [&](ReadonlyBytes const& string) -> PDFErrorOr<void> {
  727. return TRY(strings.try_append(string));
  728. }));
  729. dbgln_if(CFF_DEBUG, "CFF has {} additional strings in string table", strings.size());
  730. return strings;
  731. }
  732. DeprecatedFlyString CFF::resolve_sid(SID sid, Vector<StringView> const& strings)
  733. {
  734. if (sid < s_cff_builtin_names.size())
  735. return DeprecatedFlyString(s_cff_builtin_names[sid]);
  736. if (sid - s_cff_builtin_names.size() < strings.size())
  737. return DeprecatedFlyString(strings[sid - s_cff_builtin_names.size()]);
  738. dbgln("Couldn't find string for SID {}, going with space", sid);
  739. return DeprecatedFlyString("space");
  740. }
  741. PDFErrorOr<Vector<CFF::SID>> CFF::parse_charset(Reader&& reader, size_t glyph_count)
  742. {
  743. // CFF spec, "13 Charsets"
  744. Vector<SID> names;
  745. auto format = TRY(reader.try_read<Card8>());
  746. if (format == 0) {
  747. // CFF spec, "Table 17 Format 0"
  748. dbgln_if(CFF_DEBUG, "CFF charset format 0");
  749. for (size_t i = 0; i < glyph_count - 1; i++) {
  750. SID sid = TRY(reader.try_read<BigEndian<SID>>());
  751. TRY(names.try_append(sid));
  752. }
  753. } else if (format == 1) {
  754. // CFF spec, "Table 18 Format 1"
  755. dbgln_if(CFF_DEBUG, "CFF charset format 1");
  756. while (names.size() < glyph_count - 1) {
  757. // CFF spec, "Table 19 Range1 Format (Charset)"
  758. auto first_sid = TRY(reader.try_read<BigEndian<SID>>());
  759. int left = TRY(reader.try_read<Card8>());
  760. for (SID sid = first_sid; left >= 0; left--, sid++)
  761. TRY(names.try_append(sid));
  762. }
  763. } else if (format == 2) {
  764. // CFF spec, "Table 20 Format 2"
  765. // "Format 2 differs from format 1 only in the size of the Left field in each range."
  766. dbgln_if(CFF_DEBUG, "CFF charset format 2");
  767. while (names.size() < glyph_count - 1) {
  768. // CFF spec, "Table 21 Range2 Format"
  769. auto first_sid = TRY(reader.try_read<BigEndian<SID>>());
  770. int left = TRY(reader.try_read<BigEndian<Card16>>());
  771. for (SID sid = first_sid; left >= 0; left--, sid++)
  772. TRY(names.try_append(sid));
  773. }
  774. } else {
  775. dbgln("CFF: Unknown charset format {}", format);
  776. }
  777. return names;
  778. }
  779. PDFErrorOr<Vector<u8>> CFF::parse_fdselect(Reader&& reader, size_t glyph_count)
  780. {
  781. Vector<u8> fd_selector_array; // Maps GIDs to their FD index.
  782. // CFF spec, "19 FDSelect"
  783. auto format = TRY(reader.try_read<Card8>());
  784. if (format == 0) {
  785. // CFF spec, "Table 27 Format 0"
  786. // "(This format is identical to charset format 0 except that the notdef glyph is included in this case.)"
  787. dbgln_if(CFF_DEBUG, "CFF fdselect format 0");
  788. fd_selector_array.ensure_capacity(glyph_count);
  789. for (size_t i = 0; i < glyph_count; i++)
  790. fd_selector_array.append(TRY(reader.try_read<Card8>()));
  791. } else if (format == 3) {
  792. // CFF spec, "Table 28 Format 3"
  793. dbgln_if(CFF_DEBUG, "CFF fdselect format 3");
  794. // The spec presents this as n "Card16 first; Card8 fd;" struct entries followed by a Char16 sentinel value.
  795. // But the code is shorter if we treat it as a Char16 start value followed by n "Card8 fd; Card16 end;" struct entries.
  796. Card16 n_ranges = TRY(reader.try_read<BigEndian<Card16>>());
  797. Card16 begin = TRY(reader.try_read<BigEndian<Card16>>());
  798. // "The first range must have a 'first' GID of 0."
  799. if (begin != 0)
  800. return error("CFF fdselect format 3 first range must have a 'first' GID of 0");
  801. for (Card16 i = 0; i < n_ranges; i++) {
  802. auto fd = TRY(reader.try_read<Card8>());
  803. auto end = TRY(reader.try_read<BigEndian<Card16>>());
  804. for (Card16 j = begin; j < end; j++)
  805. fd_selector_array.append(fd);
  806. begin = end;
  807. }
  808. // "The sentinel GID is set equal to the number of glyphs in the font."
  809. if (begin != glyph_count)
  810. return error("CFF fdselect format 3 last range must end at the number of glyphs in the font");
  811. } else {
  812. dbgln("CFF: Unknown fdselect format {}", format);
  813. }
  814. return fd_selector_array;
  815. }
  816. PDFErrorOr<Vector<CFF::Glyph>> CFF::parse_charstrings(Reader&& reader, Vector<ByteBuffer> const& local_subroutines, Vector<ByteBuffer> const& global_subroutines)
  817. {
  818. // CFF spec, "14 CharStrings INDEX"
  819. Vector<Glyph> glyphs;
  820. TRY(parse_index(reader, [&](ReadonlyBytes const& charstring_data) -> PDFErrorOr<void> {
  821. GlyphParserState state;
  822. auto glyph = TRY(parse_glyph(charstring_data, local_subroutines, global_subroutines, state, true));
  823. return TRY(glyphs.try_append(glyph));
  824. }));
  825. dbgln_if(CFF_DEBUG, "CFF has {} glyphs", glyphs.size());
  826. return glyphs;
  827. }
  828. PDFErrorOr<Vector<u8>> CFF::parse_encoding(Reader&& reader, HashMap<Card8, SID>& supplemental)
  829. {
  830. // CFF spec, "12 Encodings"
  831. Vector<u8> encoding_codes;
  832. auto format_raw = TRY(reader.try_read<Card8>());
  833. auto format = format_raw & 0x7f;
  834. if (format == 0) {
  835. // CFF spec, "Table 11 Format 0"
  836. auto n_codes = TRY(reader.try_read<Card8>());
  837. dbgln_if(CFF_DEBUG, "CFF encoding format 0, {} codes", n_codes);
  838. for (u8 i = 0; i < n_codes; i++) {
  839. TRY(encoding_codes.try_append(TRY(reader.try_read<Card8>())));
  840. }
  841. } else if (format == 1) {
  842. // CFF spec, "Table 12 Format 1"
  843. auto n_ranges = TRY(reader.try_read<Card8>());
  844. dbgln_if(CFF_DEBUG, "CFF encoding format 1, {} ranges", n_ranges);
  845. for (u8 i = 0; i < n_ranges; i++) {
  846. // CFF spec, "Table 13 Range1 Format (Encoding)"
  847. auto first_code = TRY(reader.try_read<Card8>());
  848. int left = TRY(reader.try_read<Card8>());
  849. for (u8 code = first_code; left >= 0; left--, code++)
  850. TRY(encoding_codes.try_append(code));
  851. }
  852. } else
  853. return error(ByteString::formatted("Invalid encoding format: {}", format));
  854. if (format_raw & 0x80) {
  855. // CFF spec, "Table 14 Supplemental Encoding Data"
  856. auto n_sups = TRY(reader.try_read<Card8>());
  857. dbgln_if(CFF_DEBUG, "CFF encoding, {} supplemental entries", n_sups);
  858. for (u8 i = 0; i < n_sups; i++) {
  859. // CFF spec, "Table 15 Supplement Format"
  860. auto code = TRY(reader.try_read<Card8>());
  861. SID name = TRY(reader.try_read<SID>());
  862. TRY(supplemental.try_set(code, name));
  863. }
  864. }
  865. return encoding_codes;
  866. }
  867. template<typename OperatorT>
  868. PDFErrorOr<void> CFF::parse_dict(Reader& reader, DictEntryHandler<OperatorT>&& handler)
  869. {
  870. // CFF spec, "4 DICT data"
  871. Vector<DictOperand> operands;
  872. while (reader.remaining() > 0) {
  873. auto b0 = reader.read<u8>();
  874. // "Operators and operands may be distinguished by inspection of their first byte: 0-21 specify operators"
  875. if (b0 <= 21) {
  876. auto op = TRY(parse_dict_operator<OperatorT>(b0, reader));
  877. TRY(handler(op, operands));
  878. operands.clear();
  879. continue;
  880. }
  881. // An operand
  882. TRY(operands.try_append(TRY(load_dict_operand(b0, reader))));
  883. }
  884. return {};
  885. }
  886. template PDFErrorOr<void> CFF::parse_dict<CFF::TopDictOperator>(Reader&, DictEntryHandler<TopDictOperator>&&);
  887. template PDFErrorOr<void> CFF::parse_dict<CFF::PrivDictOperator>(Reader&, DictEntryHandler<PrivDictOperator>&&);
  888. template<typename OperatorT>
  889. PDFErrorOr<OperatorT> CFF::parse_dict_operator(u8 b0, Reader& reader)
  890. {
  891. // CFF spec, "4 DICT data"
  892. VERIFY(b0 <= 21);
  893. // "Two-byte operators have an initial escape byte of 12."
  894. if (b0 != 12)
  895. return OperatorT { (int)b0 };
  896. auto b1 = TRY(reader.try_read<u8>());
  897. return OperatorT { b0 << 8 | b1 };
  898. }
  899. template PDFErrorOr<CFF::TopDictOperator> CFF::parse_dict_operator(u8, Reader&);
  900. PDFErrorOr<void> CFF::parse_index(Reader& reader, IndexDataHandler&& data_handler)
  901. {
  902. // CFF spec, "5 INDEX Data"
  903. Card16 count = TRY(reader.try_read<BigEndian<Card16>>());
  904. if (count == 0)
  905. return {};
  906. auto offset_size = TRY(reader.try_read<OffSize>());
  907. if (offset_size > 4)
  908. return error("CFF INDEX Data offset_size > 4 not supported");
  909. return parse_index_data(offset_size, count, reader, data_handler);
  910. }
  911. PDFErrorOr<void> CFF::parse_index_data(OffSize offset_size, Card16 count, Reader& reader, IndexDataHandler& handler)
  912. {
  913. // CFF spec, "5 INDEX Data"
  914. u32 last_data_end = 1;
  915. auto read_offset = [&]() -> PDFErrorOr<u32> {
  916. u32 offset = 0;
  917. for (OffSize i = 0; i < offset_size; ++i)
  918. offset = (offset << 8) | TRY(reader.try_read<u8>());
  919. return offset;
  920. };
  921. auto offset_refpoint = reader.offset() + offset_size * (count + 1) - 1;
  922. for (u16 i = 0; i < count; i++) {
  923. reader.save();
  924. reader.move_by(offset_size * i);
  925. u32 data_start = TRY(read_offset());
  926. last_data_end = TRY(read_offset());
  927. auto data_size = last_data_end - data_start;
  928. reader.move_to(offset_refpoint + data_start);
  929. TRY(handler(reader.bytes().slice(reader.offset(), data_size)));
  930. reader.load();
  931. }
  932. reader.move_to(offset_refpoint + last_data_end);
  933. return {};
  934. }
  935. int CFF::load_int_dict_operand(u8 b0, Reader& reader)
  936. {
  937. // CFF spec, "Table 3 Operand Encoding"
  938. if (b0 >= 32 && b0 <= 246) {
  939. return b0 - 139;
  940. }
  941. if (b0 >= 247 && b0 <= 250) {
  942. auto b1 = reader.read<u8>();
  943. return (b0 - 247) * 256 + b1 + 108;
  944. }
  945. if (b0 >= 251 && b0 <= 254) {
  946. auto b1 = reader.read<u8>();
  947. return -(b0 - 251) * 256 - b1 - 108;
  948. }
  949. if (b0 == 28) {
  950. auto b1 = reader.read<u8>();
  951. auto b2 = reader.read<u8>();
  952. return b1 << 8 | b2;
  953. }
  954. if (b0 == 29) {
  955. auto b1 = reader.read<u8>();
  956. auto b2 = reader.read<u8>();
  957. auto b3 = reader.read<u8>();
  958. auto b4 = reader.read<u8>();
  959. return b1 << 24 | b2 << 16 | b3 << 8 | b4;
  960. }
  961. VERIFY_NOT_REACHED();
  962. }
  963. float CFF::load_float_dict_operand(Reader& reader)
  964. {
  965. // CFF spec, "Table 5 Nibble Definitions"
  966. StringBuilder sb;
  967. auto add_nibble = [&](char nibble) {
  968. if (nibble < 0xa)
  969. sb.append('0' + nibble);
  970. else if (nibble == 0xa)
  971. sb.append('.');
  972. else if (nibble == 0xb)
  973. sb.append('E');
  974. else if (nibble == 0xc)
  975. sb.append("E-"sv);
  976. else if (nibble == 0xe)
  977. sb.append('-');
  978. };
  979. while (true) {
  980. auto byte = reader.read<u8>();
  981. char nibble1 = (byte & 0xf0) >> 4;
  982. char nibble2 = byte & 0x0f;
  983. if (nibble1 == 0xf)
  984. break;
  985. add_nibble(nibble1);
  986. if (nibble2 == 0xf)
  987. break;
  988. add_nibble(nibble2);
  989. }
  990. auto result = AK::StringUtils::convert_to_floating_point<float>(sb.string_view());
  991. return result.release_value();
  992. }
  993. PDFErrorOr<CFF::DictOperand> CFF::load_dict_operand(u8 b0, Reader& reader)
  994. {
  995. // CFF spec, "4 DICT data"
  996. if (b0 == 30)
  997. return load_float_dict_operand(reader);
  998. if (b0 >= 28)
  999. return load_int_dict_operand(b0, reader);
  1000. return Error { Error::Type::MalformedPDF, ByteString::formatted("Unknown CFF dict element prefix: {}", b0) };
  1001. }
  1002. }