Text elements move to separate files

This commit is contained in:
Michal Szczepanski 2019-07-23 00:44:49 +02:00
parent ac54beceba
commit 12536bbc21
8 changed files with 84 additions and 62 deletions

@ -1,4 +1,4 @@
const FontObject = require('./FontObject');
const FontObject = require('./model/FontObject');
const Constraints = require('./Constraints');
/**

@ -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 => {

@ -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;

@ -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;

@ -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;

@ -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);
}