Fix json formatter / add width calculation

This commit is contained in:
Michal Szczepanski 2019-07-27 10:09:37 +02:00
parent 5c83ef37d6
commit 4fd9b6024c
3 changed files with 17 additions and 13 deletions

@ -34,8 +34,7 @@ class ExtractText {
x += line.font.direction * line.wordSpacing;
continue;
} else if (util.isNum(glyph)) {
const spaceSize = -glyph * line.font.size * 0.001;
x += spaceSize;
x += -glyph * line.font.size * 0.001;
if (!line.font.spaceWidthIsSet && line.isSpace(glyph)) {
partial += " ";
}
@ -65,10 +64,10 @@ class ExtractText {
line.x = page.x;
line.y = page.y;
line.setText(partial);
const isNew = lineList.y !== 0 && Math.abs(line.y - lineList.y) > line.font.size
const isNew = lineList.y !== 0 && Math.abs(line.y - lineList.y) > line.font.size;
if(isNew) {
lineList.printText()
lineList = page.currentObject.newLine()
lineList.printText();
lineList = page.currentObject.newLine();
}
lineList.x = startX;
lineList.y = startY;
@ -85,7 +84,7 @@ class ExtractText {
getFontFamily(name, dependencies) {
for(let i = 0;i<dependencies.length;i++) {
if(dependencies[i].loadedName == name) {
return dependencies[i]
return dependencies[i];
}
}
return null;
@ -98,7 +97,7 @@ class ExtractText {
*/
setFont(details, page) {
const fontObj = page.data.commonObjs.get(details[0]);
const font = new Model.FontObject()
const font = new Model.FontObject();
// calculate space width
let spaceKey = -1
for(let key in fontObj.toUnicode._map) {

@ -23,7 +23,12 @@ class FormatterJSON {
* @returns {object}
*/
formatTextObject(textObject) {
const txtObjOut = {lines: [], x: textObject.x, y: textObject.y};
const txtObjOut = {
lines: [],
x: textObject.x,
y: textObject.y,
textMatrix: textObject.textMatrix,
};
textObject.getData().forEach(textLine => {
const txtLineOut = this.formatTextLine(textLine);
txtObjOut.lines.push(txtLineOut);
@ -41,9 +46,7 @@ class FormatterJSON {
text: [],
x: textLine.x,
y: textLine.y,
w: textLine.w,
h: textLine.h,
textMatrix: textLine.textMatrix,
width: textLine.width,
}
textLine.getData().forEach(textFont => {
const txtFontOut = this.formatTextFont(textFont);
@ -65,6 +68,7 @@ class FormatterJSON {
family: textFont.font.family,
style: textFont.font.style,
weight: textFont.font.weight,
vertical: textFont.font.vertical,
},
text: textFont.getText(),
charSpacing: textFont.charSpacing,

@ -10,9 +10,7 @@ class TextLine extends PdfObject {
super();
this._textFonts = [];
this.width = 0;
this.height = 0;
}
/**
* Adds line with font to text
* @param {TextFont} line
@ -45,6 +43,9 @@ class TextLine extends PdfObject {
* @returns {Array} of {@link TextFont}
*/
getData() {
// this._textFonts.sort((a, b) => a.x >= b.x);
this.x = this._textFonts[0].x;
this.width = this._textFonts[this._textFonts.length - 1].x - this.x;
return this._textFonts;
}
}