From 12536bbc2139e1a8cfe4128211d9c6a653a2d97d Mon Sep 17 00:00:00 2001 From: Michal Szczepanski Date: Tue, 23 Jul 2019 00:44:49 +0200 Subject: [PATCH] Text elements move to separate files --- lib/pdf/Extract.js | 2 +- lib/pdf/formatter/FormatterJSON.js | 8 +++ lib/pdf/{ => model}/FontObject.js | 0 lib/pdf/{ => model}/PdfObject.js | 0 lib/pdf/model/TextObject.js | 29 +++++++++ lib/pdf/model/text/TextFont.js | 38 ++++++++++++ lib/pdf/{Text.js => model/text/TextLine.js} | 65 ++------------------- lib/pdf/visitor/VisitorText.js | 4 +- 8 files changed, 84 insertions(+), 62 deletions(-) rename lib/pdf/{ => model}/FontObject.js (100%) rename lib/pdf/{ => model}/PdfObject.js (100%) create mode 100644 lib/pdf/model/TextObject.js create mode 100644 lib/pdf/model/text/TextFont.js rename lib/pdf/{Text.js => model/text/TextLine.js} (53%) diff --git a/lib/pdf/Extract.js b/lib/pdf/Extract.js index f85b517..5480ce5 100644 --- a/lib/pdf/Extract.js +++ b/lib/pdf/Extract.js @@ -1,4 +1,4 @@ -const FontObject = require('./FontObject'); +const FontObject = require('./model/FontObject'); const Constraints = require('./Constraints'); /** diff --git a/lib/pdf/formatter/FormatterJSON.js b/lib/pdf/formatter/FormatterJSON.js index 08808ac..cc3e662 100644 --- a/lib/pdf/formatter/FormatterJSON.js +++ b/lib/pdf/formatter/FormatterJSON.js @@ -1,3 +1,6 @@ +/** + * Format PDF into json data + */ class FormatterJSON { start(doc, metadata) { const meta = JSON.stringify(metadata) @@ -8,6 +11,11 @@ class FormatterJSON { ` } + /** + * Formats text object + * @param textObject + * @returns {{lines: Array, x: *, y: *}} + */ formatTextObject(textObject) { const txtObjOut = {lines: [], x: textObject.x, y: textObject.y}; textObject.getData().forEach(textLine => { diff --git a/lib/pdf/FontObject.js b/lib/pdf/model/FontObject.js similarity index 100% rename from lib/pdf/FontObject.js rename to lib/pdf/model/FontObject.js diff --git a/lib/pdf/PdfObject.js b/lib/pdf/model/PdfObject.js similarity index 100% rename from lib/pdf/PdfObject.js rename to lib/pdf/model/PdfObject.js diff --git a/lib/pdf/model/TextObject.js b/lib/pdf/model/TextObject.js new file mode 100644 index 0000000..14071f7 --- /dev/null +++ b/lib/pdf/model/TextObject.js @@ -0,0 +1,29 @@ +const PdfObject = require('./PdfObject'); +const TextLine = require('./text/TextLine'); + +/** + * Represents text fragment + * with multiple lines in pdf document + */ +class TextObject extends PdfObject { + constructor() { + super(); + this._textLines = []; + } + + newLine() { + const t = new TextLine(); + this._textLines.push(t); + return t; + } + + getLine() { + return this._textLines[this._textLines.length -1] + } + + getData() { + return this._textLines; + } +} + +module.exports = TextObject; diff --git a/lib/pdf/model/text/TextFont.js b/lib/pdf/model/text/TextFont.js new file mode 100644 index 0000000..da232b7 --- /dev/null +++ b/lib/pdf/model/text/TextFont.js @@ -0,0 +1,38 @@ +const PdfObject = require('./../PdfObject'); + +/** + * Represents Font information in pdf file + */ +class TextFont extends PdfObject { + constructor() { + super(); + this._font = null; + this._text = ""; + this.charSpacing = 0; + this.wordSpacing = 0; + } + + getFont() { + return this._font; + } + + setFont(font) { + this._font = font; + } + + setText(text) { + this._text = text; + } + + getText() { + return this._text; + } + + equals(font) { + return this.font === font.font + && this.charSpacing === font.charSpacing + && this.wordSpacing === font.wordSpacing; + } +} + +module.exports = TextFont; \ No newline at end of file diff --git a/lib/pdf/Text.js b/lib/pdf/model/text/TextLine.js similarity index 53% rename from lib/pdf/Text.js rename to lib/pdf/model/text/TextLine.js index 935e7ae..846755f 100644 --- a/lib/pdf/Text.js +++ b/lib/pdf/model/text/TextLine.js @@ -1,27 +1,9 @@ -const PdfObject = require('./PdfObject'); - -class TextObject extends PdfObject { - constructor() { - super(); - this._textLines = []; - } - - newLine() { - const t = new TextLine(); - this._textLines.push(t); - return t; - } - - getLine() { - return this._textLines[this._textLines.length -1] - } - - getData() { - return this._textLines; - } -} - +const PdfObject = require('./../PdfObject'); +const TextFont = require('./../text/TextFont'); +/** + * Represents text line in pdf file + */ class TextLine extends PdfObject { constructor() { super(); @@ -81,39 +63,4 @@ class TextLine extends PdfObject { } } -class TextFont extends PdfObject { - constructor() { - super(); - this._font = null; - this._text = ""; - this.charSpacing = 0; - this.wordSpacing = 0; - } - - getFont() { - return this._font; - } - - setFont(font) { - this._font = font; - } - - setText(text) { - this._text = text; - } - - getText() { - return this._text; - } - - equals(font) { - return this.font === font.font - && this.charSpacing === font.charSpacing - && this.wordSpacing === font.wordSpacing; - } -} - -module.exports = { - TextObject, - TextLine, -}; +module.exports = TextLine; diff --git a/lib/pdf/visitor/VisitorText.js b/lib/pdf/visitor/VisitorText.js index 0b552f3..462cbce 100644 --- a/lib/pdf/visitor/VisitorText.js +++ b/lib/pdf/visitor/VisitorText.js @@ -1,5 +1,5 @@ const Extract = require('./../Extract'); -const Text = require('./../Text'); +const TextObject = require('./../model/TextObject'); class VisitorText { @@ -18,7 +18,7 @@ class VisitorText { beginText(args, page, dependencies) { if (this.debug) console.log('beginText'); if (this.config.skip) return; - this.currentObject = new Text.TextObject(); + this.currentObject = new TextObject(); this.currentObject.newLine(); this.objectList.push(this.currentObject); }