Skip to content

Commit

Permalink
Merge pull request #195 from sjaakieb/186-migrate-to-muhammara
Browse files Browse the repository at this point in the history
Migrated van hummus to muhammara
  • Loading branch information
chunyenHuang authored Aug 11, 2020
2 parents a265f1d + 549797c commit a1c9b05
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 437 deletions.
30 changes: 15 additions & 15 deletions lib/Recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hummus = require('hummus');
const muhammara = require('muhammara');
const path = require('path');
const fs = require('fs');
const streams = require('memory-streams');
Expand Down Expand Up @@ -42,8 +42,8 @@ class Recipe {
this.filename = path.basename(this.src);
}
}
this.hummus = hummus;
this.logFile = 'hummus-error.log';
this.muhammara = muhammara;
this.logFile = 'muhammara-error.log';

this.textMarkupAnnotations = [
'Highlight', 'Underline', 'StrikeOut', 'Squiggly'
Expand All @@ -69,7 +69,7 @@ class Recipe {

_createWriter() {
if (this.isNewPDF) {
this.writer = hummus.createWriter(this.output,
this.writer = muhammara.createWriter(this.output,
Object.assign( {}, this.encryptOptions, {
version: this._getVersion(this.options.version)
})
Expand All @@ -78,15 +78,15 @@ class Recipe {
this.read();
try {
if (this.isBufferSrc) {
this.writer = hummus.createWriterToModify(
new hummus.PDFRStreamForBuffer(this.src),
new hummus.PDFStreamForResponse(this.outStream),
this.writer = muhammara.createWriterToModify(
new muhammara.PDFRStreamForBuffer(this.src),
new muhammara.PDFStreamForResponse(this.outStream),
Object.assign( {}, this.encryptOptions, {
log: this.logFile
})
);
} else {
this.writer = hummus.createWriterToModify(this.src,
this.writer = muhammara.createWriterToModify(this.src,
Object.assign( {}, this.encryptOptions, {
modifiedFilePath: this.output,
log: this.logFile
Expand All @@ -108,7 +108,7 @@ class Recipe {
if (!supportedVersions.includes(version)) {
version = 1.7;
}
version = hummus[`ePDFVersion${version * 10}`];
version = muhammara[`ePDFVersion${version * 10}`];

return version;
}
Expand All @@ -129,9 +129,9 @@ class Recipe {
try {
let src = (isForExternal) ? inSrc : this.src;
if (this.isBufferSrc) {
src = new hummus.PDFRStreamForBuffer(this.src);
src = new muhammara.PDFRStreamForBuffer(this.src);
}
const pdfReader = hummus.createReader(src, this.encryptOptions);
const pdfReader = muhammara.createReader(src, this.encryptOptions);
const pages = pdfReader.getPagesCount();
if (pages == 0) {
// broken or modify password protected
Expand Down Expand Up @@ -215,15 +215,15 @@ class Recipe {
const oldStream = this.outStream;
this.outStream = new streams.WritableStream();

this.writer = hummus.createWriterToModify(
new hummus.PDFRStreamForBuffer(oldStream.toBuffer()),
new hummus.PDFStreamForResponse(this.outStream),
this.writer = muhammara.createWriterToModify(
new muhammara.PDFRStreamForBuffer(oldStream.toBuffer()),
new muhammara.PDFStreamForResponse(this.outStream),
Object.assign( {}, this.encryptOptions, {
log: this.logFile
})
);
} else {
this.writer = hummus.createWriterToModify(this.output,
this.writer = muhammara.createWriterToModify(this.output,
Object.assign( {}, this.encryptOptions, {
modifiedFilePath: this.output,
log: this.logFile
Expand Down
6 changes: 3 additions & 3 deletions lib/appendPage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hummus = require('hummus');
const muhammara = require('muhammara');
const hummusUtils = require('./utils');

/**
Expand All @@ -14,8 +14,8 @@ exports.appendPage = function appendPage(pdfSrc, pages = []) {
pages = [pages];
}
// Using stream so it can be closed to release reader resource (Issue #61)
const instream = new hummus.PDFRStreamForFile(pdfSrc);
const pdfReader = hummus.createReader(instream);
const instream = new muhammara.PDFRStreamForFile(pdfSrc);
const pdfReader = muhammara.createReader(instream);
const pageCount = pdfReader.getPagesCount();
instream.close();

Expand Down
4 changes: 2 additions & 2 deletions lib/colors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hummus = require('hummus');
const muhammara = require('muhammara');
const fs = require('fs');

this.knownColors = { // knownColors.colorspace.colorName = value
Expand Down Expand Up @@ -117,7 +117,7 @@ function createColorSpaces(self, colorName, color) {
.writeName(colorName)
.writeName(altCS)
.writeIndirectObjectReference(transformFunction)
.endArray(hummus.eTokenSeparatorEndLine)
.endArray(muhammara.eTokenSeparatorEndLine)
.endIndirectObject();
self.resumeContext();
this.colorSpaces[altCS][colorName] = colorSpaceID;
Expand Down
4 changes: 2 additions & 2 deletions lib/encrypt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hummus = require('hummus');
const muhammara = require('muhammara');
const fs = require('fs');


Expand Down Expand Up @@ -101,6 +101,6 @@ exports._encrypt = function _encrypt() {

const tmp = this.output + '.tmp.pdf';
fs.renameSync(this.output, tmp);
hummus.recrypt(tmp, this.output, this.encryption_);
muhammara.recrypt(tmp, this.output, this.encryption_);
fs.unlinkSync(tmp);
};
30 changes: 15 additions & 15 deletions lib/info.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs');
const hummus = require('hummus');
const muhammara = require('muhammara');
/**
* @name info
* @desc Add new PDF information, or retrieve existing PDF information.
Expand Down Expand Up @@ -29,7 +29,7 @@ exports.info = function info(options) {

exports._readInfo = function _readInfo() {
if (!this.isNewPDF && !this.infoDictionary) {
const copyFrom = this.isBufferSrc ? new hummus.PDFRStreamForBuffer(this.src) : this.src;
const copyFrom = this.isBufferSrc ? new muhammara.PDFRStreamForBuffer(this.src) : this.src;
const copyCtx = this.writer.createPDFCopyingContext(copyFrom);
const infoDict = copyCtx.getSourceDocumentParser().queryDictionaryObject(
copyCtx.getSourceDocumentParser().getTrailer(), 'Info'
Expand Down Expand Up @@ -144,7 +144,7 @@ exports._writeInfo = function _writeInfo() {
infoDictionary.setCreationDate(new Date());
}
infoDictionary.setModDate(new Date());
infoDictionary.producer = 'PDFHummus (https://github.com/galkahana/HummusJS)';
infoDictionary.producer = 'MuhammaraJS (https://github.com/julianhille/MuhammaraJS)';
infoDictionary.creator = 'Hummus-Recipe (https://github.com/chunyenHuang/hummusRecipe)';

fields.forEach((item) => {
Expand Down Expand Up @@ -192,7 +192,7 @@ exports.structure = function structure(output) {
// PDF file format http://lotabout.me/orgwiki/pdf.html
// const outputFileType = path.extname(output);
const outputFile = fs.openSync(output, 'w');
const hummus = this.hummus;
const muhammara = this.muhammara;
const pdfReader = this.pdfReader;

const tabWidth = ' ';
Expand Down Expand Up @@ -224,12 +224,12 @@ exports.structure = function structure(output) {

const iterateObjectTypes = (inObject) => {
const type = inObject.getType();
const label = hummus.getTypeLabel(type);
const label = muhammara.getTypeLabel(type);
let output = '';
let objectID, jsArray, aDictionary, keys;

switch (type) {
case hummus.ePDFObjectIndirectObjectReference:
case muhammara.ePDFObjectIndirectObjectReference:
++mTabLevel;
objectID = inObject.toPDFIndirectObjectReference().getObjectID();
output += `Indirect object reference (${objectID}): `;
Expand All @@ -243,7 +243,7 @@ exports.structure = function structure(output) {
}
--mTabLevel;
return;
case hummus.ePDFObjectArray:
case muhammara.ePDFObjectArray:
jsArray = inObject.toPDFArray().toJSArray();
output += `- ${label} [${jsArray.length}]`;
logToFile(output);
Expand All @@ -253,7 +253,7 @@ exports.structure = function structure(output) {
});
--mTabLevel;
break;
case hummus.ePDFObjectDictionary:
case muhammara.ePDFObjectDictionary:
aDictionary = inObject.toPDFDictionary().toJSObject();
keys = Object.getOwnPropertyNames(aDictionary).join(', ');
output += `- ${label} {${keys}}`;
Expand All @@ -265,7 +265,7 @@ exports.structure = function structure(output) {
});
--mTabLevel;
break;
case hummus.ePDFObjectStream:
case muhammara.ePDFObjectStream:
output += 'Stream . iterating stream dictionary:';
logToFile(output);
iterateObjectTypes(inObject.toPDFStream().getDictionary());
Expand Down Expand Up @@ -293,29 +293,29 @@ exports._parseObjectByType = function _parseObjectByType(inObject) {
if (!inObject) {
return;
}
const hummus = this.hummus;
const muhammara = this.muhammara;
const pdfReader = this.pdfReader;
const type = inObject.getType();
const label = hummus.getTypeLabel(type);
const label = muhammara.getTypeLabel(type);
const saveToObject = this.pdfStructure || {};
let objectID, parsed, dictionaryObject, dictionary;
switch (type) {
case hummus.ePDFObjectIndirectObjectReference:
case muhammara.ePDFObjectIndirectObjectReference:
objectID = inObject.toPDFIndirectObjectReference().getObjectID();
parsed = pdfReader.parseNewObject(objectID);
return this._parseObjectByType(parsed);
case hummus.ePDFObjectArray:
case muhammara.ePDFObjectArray:
inObject.toPDFArray().toJSArray().forEach((element) => {
this._parseObjectByType(element);
});
break;
case hummus.ePDFObjectDictionary:
case muhammara.ePDFObjectDictionary:
dictionaryObject = inObject.toPDFDictionary().toJSObject();
Object.getOwnPropertyNames(dictionaryObject).forEach((element) => {
this._parseObjectByType(dictionaryObject[element]);
});
break;
case hummus.ePDFObjectStream:
case muhammara.ePDFObjectStream:
dictionary = inObject.toPDFStream().getDictionary();
return this._parseObjectByType(dictionary);
default:
Expand Down
4 changes: 2 additions & 2 deletions lib/insertPage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hummus = require('hummus');
const muhammara = require('muhammara');
const fs = require('fs');
const hummusUtils = require('./utils');
/**
Expand Down Expand Up @@ -40,7 +40,7 @@ exports._insertPages = function _insertPages() {
unlinkList.push(tmp);
fs.renameSync(this.output, tmp);

const pdfWriter = hummus.createWriter(this.output);
const pdfWriter = muhammara.createWriter(this.output);
let lastInsertedOriginal = 0;
pagesForInsert.forEach((pageNumber) => {
const toAppendPage = pageNumber - 1;
Expand Down
4 changes: 2 additions & 2 deletions lib/page.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hummus = require('hummus');
const muhammara = require('muhammara');

/**
* Create a new page, specifying either actual width and height, or the name
Expand Down Expand Up @@ -106,7 +106,7 @@ exports.endPage = function endPage() {
exports.editPage = function editPage(pageNumber) {
const pdfWriter = this.writer;
const pageIndex = pageNumber - 1;
const pageModifier = new hummus.PDFPageModifier(pdfWriter, pageIndex, true);
const pageModifier = new muhammara.PDFPageModifier(pdfWriter, pageIndex, true);
this.page = pageModifier;
this.pageNumber = pageNumber;
this.pageContext = pageModifier.startContext().getContext();
Expand Down
4 changes: 2 additions & 2 deletions lib/split.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hummus = require('hummus');
const muhammara = require('muhammara');
const path = require('path');
const hummusUtils = require('./utils');

Expand All @@ -14,7 +14,7 @@ exports.split = function split(outputDir = '', prefix) {
prefix = prefix || this.filename;
for (let i = 0; i < this.metadata.pages; i++) {
const newPdf = path.join(outputDir, `${prefix}-${i+1}.pdf`);
const pdfWriter = hummus.createWriter(newPdf);
const pdfWriter = muhammara.createWriter(newPdf);
hummusUtils.appendPDFPageFromPDFWithAnnotations(pdfWriter, this.pdfReader, i);
pdfWriter.end();
}
Expand Down
Loading

0 comments on commit a1c9b05

Please sign in to comment.