parse.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright 2014 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. exports.parseAdobeCMap = function (content) {
  16. let m = /(\bbegincmap\b[\s\S]*?)\bendcmap\b/.exec(content);
  17. if (!m) {
  18. throw new Error("cmap was not found");
  19. }
  20. const body = m[1].replace(/\r\n?/g, "\n");
  21. const result = {
  22. type: 1,
  23. wmode: 0,
  24. comment:
  25. "Copyright 1990-2009 Adobe Systems Incorporated.\nAll rights reserved.\nSee ./LICENSE",
  26. usecmap: null,
  27. body: [],
  28. };
  29. m = /\/CMapType\s+(\d+)\s+def\b/.exec(body);
  30. result.type = +m[1];
  31. m = /\/WMode\s+(\d+)\s+def\b/.exec(body);
  32. result.wmode = +m[1];
  33. m = /\/([\w-]+)\s+usecmap\b/.exec(body);
  34. if (m) {
  35. result.usecmap = m[1];
  36. }
  37. const re =
  38. /(\d+)\s+(begincodespacerange|beginnotdefrange|begincidchar|begincidrange|beginbfchar|beginbfrange)\n([\s\S]*?)\n(endcodespacerange|endnotdefrange|endcidchar|endcidrange|endbfchar|endbfrange)/g;
  39. while ((m = re.exec(body))) {
  40. const lines = m[3].toLowerCase().split("\n");
  41. switch (m[2]) {
  42. case "begincodespacerange":
  43. result.body.push({
  44. type: 0,
  45. items: lines.map(function (line) {
  46. const m2 = /<(\w+)>\s+<(\w+)>/.exec(line);
  47. return { start: m2[1], end: m2[2] };
  48. }),
  49. });
  50. break;
  51. case "beginnotdefrange":
  52. result.body.push({
  53. type: 1,
  54. items: lines.map(function (line) {
  55. const m2 = /<(\w+)>\s+<(\w+)>\s+(\d+)/.exec(line);
  56. return { start: m2[1], end: m2[2], code: +m2[3] };
  57. }),
  58. });
  59. break;
  60. case "begincidchar":
  61. result.body.push({
  62. type: 2,
  63. items: lines.map(function (line) {
  64. const m2 = /<(\w+)>\s+(\d+)/.exec(line);
  65. return { char: m2[1], code: +m2[2] };
  66. }),
  67. });
  68. break;
  69. case "begincidrange":
  70. result.body.push({
  71. type: 3,
  72. items: lines.map(function (line) {
  73. const m2 = /<(\w+)>\s+<(\w+)>\s+(\d+)/.exec(line);
  74. return { start: m2[1], end: m2[2], code: +m2[3] };
  75. }),
  76. });
  77. break;
  78. case "beginbfchar":
  79. result.body.push({
  80. type: 4,
  81. items: lines.map(function (line) {
  82. const m2 = /<(\w+)>\s+<(\w+)>/.exec(line);
  83. return { char: m2[1], code: m2[2] };
  84. }),
  85. });
  86. break;
  87. case "beginbfrange":
  88. result.body.push({
  89. type: 5,
  90. items: lines.map(function (line) {
  91. const m2 = /<(\w+)>\s+<(\w+)>\s+<(\w+)>/.exec(line);
  92. return { start: m2[1], end: m2[2], code: m2[3] };
  93. }),
  94. });
  95. break;
  96. }
  97. }
  98. return result;
  99. };