@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.
@@ -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 new PDF information, or retrieve existing PDF information.
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
- const infoDict = copyCtx
37
- .getSourceDocumentParser()
38
- .queryDictionaryObject(
39
- copyCtx.getSourceDocumentParser().getTrailer(),
40
- "Info",
41
- );
45
+ try {
46
+ const infoDict = copyCtx
47
+ .getSourceDocumentParser()
48
+ .queryDictionaryObject(
49
+ copyCtx.getSourceDocumentParser().getTrailer(),
50
+ "Info",
51
+ );
42
52
 
43
- const oldInfo =
44
- infoDict && infoDict.toJSObject ? infoDict.toJSObject() : null;
53
+ const oldInfo =
54
+ infoDict && infoDict.toJSObject ? infoDict.toJSObject() : null;
45
55
 
46
- if (oldInfo) {
47
- this.infoDictionary = {};
48
- Object.getOwnPropertyNames(oldInfo).forEach((key) => {
49
- if (!oldInfo[key]) {
50
- return;
51
- }
52
- const oldInforSrc = this._parseObjectByType(oldInfo[key]);
53
- if (!oldInforSrc) {
54
- return;
55
- }
56
- switch (key) {
57
- case "Trapped":
58
- if (oldInforSrc && oldInforSrc.value) {
59
- this.infoDictionary.trapped = oldInforSrc.value;
60
- }
61
- break;
62
- case "CreationDate":
63
- if (oldInforSrc && oldInforSrc.value) {
64
- this.infoDictionary.creationDate = oldInforSrc.value;
65
- }
66
- break;
67
- case "ModDate":
68
- if (oldInforSrc && oldInforSrc.value) {
69
- this.infoDictionary.modDate = oldInforSrc.value;
70
- }
71
- break;
72
- case "Creator":
73
- if (oldInforSrc && oldInforSrc.toText) {
74
- this.infoDictionary.creator = oldInforSrc.toText();
75
- }
76
- break;
77
- case "Producer":
78
- if (oldInforSrc && oldInforSrc.toText) {
79
- this.infoDictionary.producer = oldInforSrc.toText();
80
- }
81
- break;
82
- default:
83
- if (oldInforSrc && oldInforSrc.toText) {
84
- this.infoDictionary[key.toLowerCase()] = oldInforSrc.toText();
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
- const fields = [
106
- {
107
- key: "author",
108
- type: "string",
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
- "Hummus-Recipe (https://github.com/chunyenHuang/hummusRecipe)";
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.pdfReader;
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.pdfReader;
332
+ const pdfReader = this._getReader();
335
333
  const type = inObject.getType();
336
334
  const label = muhammara.getTypeLabel(type);
337
335
  const saveToObject = this.pdfStructure || {};
@@ -1,6 +1,6 @@
1
1
  const muhammara = require("../muhammara");
2
2
  const fs = require("fs");
3
- const hummusUtils = require("./utils");
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
- this.needToInsertPages = true;
24
- if (pdfSrc && srcPageNumber) {
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
- hummusUtils.appendPDFPagesFromPDFWithAnnotations(pdfWriter, tmp, {
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
- hummusUtils.appendPDFPagesFromPDFWithAnnotations(
69
- pdfWriter,
70
- info.pdfSrc,
71
- { specificRanges },
72
- );
73
+ utils.appendPDFPagesFromPDFWithAnnotations(pdfWriter, info.pdfSrc, {
74
+ specificRanges,
75
+ });
73
76
  });
74
77
  }
75
78
  });