2014-03-15 03:22:02 +09:00
|
|
|
/* Copyright 2014 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-07-08 21:35:15 +09:00
|
|
|
function parseAdobeCMap(content) {
|
2021-03-14 02:29:00 +09:00
|
|
|
let m = /(\bbegincmap\b[\s\S]*?)\bendcmap\b/.exec(content);
|
2014-03-15 03:22:02 +09:00
|
|
|
if (!m) {
|
2021-03-14 02:29:00 +09:00
|
|
|
throw new Error("cmap was not found");
|
2014-03-15 03:22:02 +09:00
|
|
|
}
|
|
|
|
|
2023-03-23 20:34:08 +09:00
|
|
|
const body = m[1].replaceAll(/\r\n?/g, "\n");
|
2021-03-14 02:29:00 +09:00
|
|
|
const result = {
|
2014-03-15 03:22:02 +09:00
|
|
|
type: 1,
|
|
|
|
wmode: 0,
|
2021-03-14 02:29:00 +09:00
|
|
|
comment:
|
|
|
|
"Copyright 1990-2009 Adobe Systems Incorporated.\nAll rights reserved.\nSee ./LICENSE",
|
2014-03-15 03:22:02 +09:00
|
|
|
usecmap: null,
|
2021-03-14 02:29:00 +09:00
|
|
|
body: [],
|
2014-03-15 03:22:02 +09:00
|
|
|
};
|
2021-08-01 19:11:10 +09:00
|
|
|
m = /\/CMapType\s+(\d+)\s+def\b/.exec(body);
|
2014-03-15 03:22:02 +09:00
|
|
|
result.type = +m[1];
|
2021-08-01 19:11:10 +09:00
|
|
|
m = /\/WMode\s+(\d+)\s+def\b/.exec(body);
|
2014-03-15 03:22:02 +09:00
|
|
|
result.wmode = +m[1];
|
2021-03-14 02:30:24 +09:00
|
|
|
m = /\/([\w-]+)\s+usecmap\b/.exec(body);
|
2014-03-15 03:22:02 +09:00
|
|
|
if (m) {
|
|
|
|
result.usecmap = m[1];
|
|
|
|
}
|
2021-05-16 17:58:34 +09:00
|
|
|
const re =
|
|
|
|
/(\d+)\s+(begincodespacerange|beginnotdefrange|begincidchar|begincidrange|beginbfchar|beginbfrange)\n([\s\S]*?)\n(endcodespacerange|endnotdefrange|endcidchar|endcidrange|endbfchar|endbfrange)/g;
|
2021-03-14 02:29:00 +09:00
|
|
|
while ((m = re.exec(body))) {
|
|
|
|
const lines = m[3].toLowerCase().split("\n");
|
2021-03-14 02:30:24 +09:00
|
|
|
|
2014-03-15 03:22:02 +09:00
|
|
|
switch (m[2]) {
|
2021-03-14 02:29:00 +09:00
|
|
|
case "begincodespacerange":
|
2014-03-15 03:22:02 +09:00
|
|
|
result.body.push({
|
|
|
|
type: 0,
|
|
|
|
items: lines.map(function (line) {
|
2021-03-14 02:30:24 +09:00
|
|
|
const m2 = /<(\w+)>\s+<(\w+)>/.exec(line);
|
|
|
|
return { start: m2[1], end: m2[2] };
|
2021-03-14 02:29:00 +09:00
|
|
|
}),
|
2014-03-15 03:22:02 +09:00
|
|
|
});
|
|
|
|
break;
|
2021-03-14 02:29:00 +09:00
|
|
|
case "beginnotdefrange":
|
2014-03-15 03:22:02 +09:00
|
|
|
result.body.push({
|
|
|
|
type: 1,
|
|
|
|
items: lines.map(function (line) {
|
2021-03-14 02:30:24 +09:00
|
|
|
const m2 = /<(\w+)>\s+<(\w+)>\s+(\d+)/.exec(line);
|
|
|
|
return { start: m2[1], end: m2[2], code: +m2[3] };
|
2021-03-14 02:29:00 +09:00
|
|
|
}),
|
2014-03-15 03:22:02 +09:00
|
|
|
});
|
|
|
|
break;
|
2021-03-14 02:29:00 +09:00
|
|
|
case "begincidchar":
|
2014-03-15 03:22:02 +09:00
|
|
|
result.body.push({
|
|
|
|
type: 2,
|
|
|
|
items: lines.map(function (line) {
|
2021-03-14 02:30:24 +09:00
|
|
|
const m2 = /<(\w+)>\s+(\d+)/.exec(line);
|
|
|
|
return { char: m2[1], code: +m2[2] };
|
2021-03-14 02:29:00 +09:00
|
|
|
}),
|
2014-03-15 03:22:02 +09:00
|
|
|
});
|
|
|
|
break;
|
2021-03-14 02:29:00 +09:00
|
|
|
case "begincidrange":
|
2014-03-15 03:22:02 +09:00
|
|
|
result.body.push({
|
|
|
|
type: 3,
|
|
|
|
items: lines.map(function (line) {
|
2021-03-14 02:30:24 +09:00
|
|
|
const m2 = /<(\w+)>\s+<(\w+)>\s+(\d+)/.exec(line);
|
|
|
|
return { start: m2[1], end: m2[2], code: +m2[3] };
|
2021-03-14 02:29:00 +09:00
|
|
|
}),
|
2014-03-15 03:22:02 +09:00
|
|
|
});
|
|
|
|
break;
|
2021-03-14 02:29:00 +09:00
|
|
|
case "beginbfchar":
|
2014-03-15 03:22:02 +09:00
|
|
|
result.body.push({
|
|
|
|
type: 4,
|
|
|
|
items: lines.map(function (line) {
|
2021-03-14 02:30:24 +09:00
|
|
|
const m2 = /<(\w+)>\s+<(\w+)>/.exec(line);
|
|
|
|
return { char: m2[1], code: m2[2] };
|
2021-03-14 02:29:00 +09:00
|
|
|
}),
|
2014-03-15 03:22:02 +09:00
|
|
|
});
|
|
|
|
break;
|
2021-03-14 02:29:00 +09:00
|
|
|
case "beginbfrange":
|
2014-03-15 03:22:02 +09:00
|
|
|
result.body.push({
|
|
|
|
type: 5,
|
|
|
|
items: lines.map(function (line) {
|
2021-03-14 02:30:24 +09:00
|
|
|
const m2 = /<(\w+)>\s+<(\w+)>\s+<(\w+)>/.exec(line);
|
|
|
|
return { start: m2[1], end: m2[2], code: m2[3] };
|
2021-03-14 02:29:00 +09:00
|
|
|
}),
|
2014-03-15 03:22:02 +09:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2023-07-08 21:35:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export { parseAdobeCMap };
|