@muhammara/native-core 7.0.0-alpha.1 → 7.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/index.js +45 -0
- package/lib/Recipe.js +163 -56
- package/lib/recipe/annotation.js +53 -4
- package/lib/recipe/appendPage.js +38 -16
- package/lib/recipe/graphics-state.js +20 -0
- package/lib/recipe/image.js +4 -0
- package/lib/recipe/info.js +71 -73
- package/lib/recipe/insertPage.js +20 -17
- package/lib/recipe/page.js +808 -11
- package/lib/recipe/parameters.js +2 -0
- package/lib/recipe/replaceText.js +66 -0
- package/lib/recipe/shapes.js +22 -1
- package/lib/recipe/split.js +2 -6
- package/lib/recipe/text.helper.js +1 -1
- package/lib/recipe/text.js +106 -8
- package/lib/recipe/utils.js +31 -27
- package/lib/recipe/vector-polygon.js +11 -0
- package/lib/recipe/vector.helper.js +15 -3
- package/lib/recipe/vector.js +108 -10
- package/lib/recipe-info.js +10 -0
- package/muhammara.d.ts +543 -89
- package/package.json +1 -1
package/lib/recipe/info.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
2
|
const muhammara = require("../muhammara");
|
|
3
|
+
var { recipeInfoKeys, standardInfoKeys } = require("../recipe-info");
|
|
3
4
|
/**
|
|
4
5
|
* @name info
|
|
5
|
-
* @desc Add
|
|
6
|
+
* @desc Add standard and custom PDF information, or retrieve existing PDF information. Custom keys retain their spelling when written; array values are joined with a comma and space.
|
|
6
7
|
* @memberof Recipe#
|
|
7
8
|
* @function
|
|
8
9
|
* @param {Object} [options] - The options (when missing obtains existing PDF information)
|
|
@@ -21,6 +22,14 @@ exports.info = function info(options) {
|
|
|
21
22
|
} else {
|
|
22
23
|
this.toWriteInfo_ = this.toWriteInfo_ || {};
|
|
23
24
|
Object.assign(this.toWriteInfo_, options);
|
|
25
|
+
Object.entries(options).forEach(([key, value]) => {
|
|
26
|
+
if (!standardInfoKeys.includes(key)) {
|
|
27
|
+
this.custom(
|
|
28
|
+
key,
|
|
29
|
+
Array.isArray(value) ? value.join(", ") : String(value),
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
24
33
|
result = this;
|
|
25
34
|
}
|
|
26
35
|
|
|
@@ -33,60 +42,63 @@ exports._readInfo = function _readInfo() {
|
|
|
33
42
|
? new muhammara.PDFRStreamForBuffer(this.src)
|
|
34
43
|
: this.src;
|
|
35
44
|
const copyCtx = this.writer.createPDFCopyingContext(copyFrom);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
try {
|
|
46
|
+
const infoDict = copyCtx
|
|
47
|
+
.getSourceDocumentParser()
|
|
48
|
+
.queryDictionaryObject(
|
|
49
|
+
copyCtx.getSourceDocumentParser().getTrailer(),
|
|
50
|
+
"Info",
|
|
51
|
+
);
|
|
42
52
|
|
|
43
|
-
|
|
44
|
-
|
|
53
|
+
const oldInfo =
|
|
54
|
+
infoDict && infoDict.toJSObject ? infoDict.toJSObject() : null;
|
|
45
55
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
56
|
+
if (oldInfo) {
|
|
57
|
+
this.infoDictionary = {};
|
|
58
|
+
Object.getOwnPropertyNames(oldInfo).forEach((key) => {
|
|
59
|
+
if (!oldInfo[key]) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const oldInforSrc = this._parseObjectByType(oldInfo[key]);
|
|
63
|
+
if (!oldInforSrc) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
switch (key) {
|
|
67
|
+
case "Trapped":
|
|
68
|
+
if (oldInforSrc && oldInforSrc.value) {
|
|
69
|
+
this.infoDictionary.trapped = oldInforSrc.value;
|
|
70
|
+
}
|
|
71
|
+
break;
|
|
72
|
+
case "CreationDate":
|
|
73
|
+
if (oldInforSrc && oldInforSrc.value) {
|
|
74
|
+
this.infoDictionary.creationDate = oldInforSrc.value;
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
case "ModDate":
|
|
78
|
+
if (oldInforSrc && oldInforSrc.value) {
|
|
79
|
+
this.infoDictionary.modDate = oldInforSrc.value;
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
case "Creator":
|
|
83
|
+
if (oldInforSrc && oldInforSrc.toText) {
|
|
84
|
+
this.infoDictionary.creator = oldInforSrc.toText();
|
|
85
|
+
}
|
|
86
|
+
break;
|
|
87
|
+
case "Producer":
|
|
88
|
+
if (oldInforSrc && oldInforSrc.toText) {
|
|
89
|
+
this.infoDictionary.producer = oldInforSrc.toText();
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
default:
|
|
93
|
+
if (oldInforSrc && oldInforSrc.toText) {
|
|
94
|
+
this.infoDictionary[key.toLowerCase()] = oldInforSrc.toText();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
} finally {
|
|
100
|
+
copyCtx.end();
|
|
88
101
|
}
|
|
89
|
-
copyCtx.end();
|
|
90
102
|
}
|
|
91
103
|
|
|
92
104
|
return this.infoDictionary;
|
|
@@ -102,24 +114,10 @@ exports._writeInfo = function _writeInfo() {
|
|
|
102
114
|
*/
|
|
103
115
|
|
|
104
116
|
const infoDictionary = this.writer.getDocumentContext().getInfoDictionary();
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
key: "title",
|
|
112
|
-
type: "string",
|
|
113
|
-
},
|
|
114
|
-
{
|
|
115
|
-
key: "subject",
|
|
116
|
-
type: "string",
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
key: "keywords",
|
|
120
|
-
type: "array",
|
|
121
|
-
},
|
|
122
|
-
];
|
|
117
|
+
var fields = standardInfoKeys.map((key) => ({
|
|
118
|
+
key,
|
|
119
|
+
type: key === recipeInfoKeys.keywords ? "array" : "string",
|
|
120
|
+
}));
|
|
123
121
|
// const ignores = [
|
|
124
122
|
// 'CreationDate', 'Creator', 'ModDate', 'Producer'
|
|
125
123
|
// ];
|
|
@@ -168,7 +166,7 @@ exports._writeInfo = function _writeInfo() {
|
|
|
168
166
|
infoDictionary.producer =
|
|
169
167
|
"MuhammaraJS (https://github.com/julianhille/MuhammaraJS)";
|
|
170
168
|
infoDictionary.creator =
|
|
171
|
-
"
|
|
169
|
+
"Muhammara-Recipe (https://github.com/julianhille/MuhammaraJS)";
|
|
172
170
|
|
|
173
171
|
fields.forEach((item) => {
|
|
174
172
|
let value = options[item.key];
|
|
@@ -225,7 +223,7 @@ exports.structure = function structure(output) {
|
|
|
225
223
|
// const outputFileType = path.extname(output);
|
|
226
224
|
const outputFile = fs.openSync(output, "w");
|
|
227
225
|
const muhammara = this.muhammara;
|
|
228
|
-
const pdfReader = this.
|
|
226
|
+
const pdfReader = this._getReader();
|
|
229
227
|
|
|
230
228
|
const tabWidth = " ";
|
|
231
229
|
const structures = [
|
|
@@ -331,7 +329,7 @@ exports._parseObjectByType = function _parseObjectByType(inObject) {
|
|
|
331
329
|
return;
|
|
332
330
|
}
|
|
333
331
|
const muhammara = this.muhammara;
|
|
334
|
-
const pdfReader = this.
|
|
332
|
+
const pdfReader = this._getReader();
|
|
335
333
|
const type = inObject.getType();
|
|
336
334
|
const label = muhammara.getTypeLabel(type);
|
|
337
335
|
const saveToObject = this.pdfStructure || {};
|
package/lib/recipe/insertPage.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const muhammara = require("../muhammara");
|
|
2
2
|
const fs = require("fs");
|
|
3
|
-
const
|
|
3
|
+
const utils = require("./utils");
|
|
4
4
|
/**
|
|
5
5
|
* Insert a page from the other pdf
|
|
6
6
|
* @name insertPage
|
|
@@ -11,26 +11,31 @@ const hummusUtils = require("./utils");
|
|
|
11
11
|
* @param {number} srcPageNumber - The page number to be insterted from the other pdf.
|
|
12
12
|
* @returns {Recipe} The recipe instance.
|
|
13
13
|
* @throws {Error} If afterPageNumber is not a number.
|
|
14
|
+
* @throws {TypeError} If pdfSrc or srcPageNumber is missing.
|
|
14
15
|
*/
|
|
15
16
|
exports.insertPage = function insertPage(
|
|
16
17
|
afterPageNumber,
|
|
17
18
|
pdfSrc,
|
|
18
19
|
srcPageNumber,
|
|
19
20
|
) {
|
|
21
|
+
if (this.deletedPages?.size) {
|
|
22
|
+
throw new Error("insertPage cannot be combined with deletePage");
|
|
23
|
+
}
|
|
20
24
|
if (isNaN(afterPageNumber)) {
|
|
21
25
|
throw new Error("The afterPageNumber is inValid.");
|
|
22
26
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.insertInformation = this.insertInformation || {};
|
|
26
|
-
this.insertInformation[afterPageNumber] =
|
|
27
|
-
this.insertInformation[afterPageNumber] || [];
|
|
28
|
-
this.insertInformation[afterPageNumber].push({
|
|
29
|
-
afterPageNumber,
|
|
30
|
-
pdfSrc,
|
|
31
|
-
srcPageNumber,
|
|
32
|
-
});
|
|
27
|
+
if (!pdfSrc || !srcPageNumber) {
|
|
28
|
+
throw new TypeError("insertPage requires pdfSrc and srcPageNumber");
|
|
33
29
|
}
|
|
30
|
+
this.insertInformation = this.insertInformation || {};
|
|
31
|
+
this.insertInformation[afterPageNumber] =
|
|
32
|
+
this.insertInformation[afterPageNumber] || [];
|
|
33
|
+
this.insertInformation[afterPageNumber].push({
|
|
34
|
+
afterPageNumber,
|
|
35
|
+
pdfSrc,
|
|
36
|
+
srcPageNumber,
|
|
37
|
+
});
|
|
38
|
+
this.needToInsertPages = true;
|
|
34
39
|
return this;
|
|
35
40
|
};
|
|
36
41
|
|
|
@@ -53,7 +58,7 @@ exports._insertPages = function _insertPages() {
|
|
|
53
58
|
const toAppendPage = pageNumber - 1;
|
|
54
59
|
if (toAppendPage >= 0) {
|
|
55
60
|
const specificRanges = [[lastInsertedOriginal, toAppendPage]];
|
|
56
|
-
|
|
61
|
+
utils.appendPDFPagesFromPDFWithAnnotations(pdfWriter, tmp, {
|
|
57
62
|
specificRanges,
|
|
58
63
|
});
|
|
59
64
|
}
|
|
@@ -65,11 +70,9 @@ exports._insertPages = function _insertPages() {
|
|
|
65
70
|
const specificRanges = [
|
|
66
71
|
[info.srcPageNumber - 1, info.srcPageNumber - 1],
|
|
67
72
|
];
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
{ specificRanges },
|
|
72
|
-
);
|
|
73
|
+
utils.appendPDFPagesFromPDFWithAnnotations(pdfWriter, info.pdfSrc, {
|
|
74
|
+
specificRanges,
|
|
75
|
+
});
|
|
73
76
|
});
|
|
74
77
|
}
|
|
75
78
|
});
|