optimize the speed
This commit is contained in:
parent
2b1a59e963
commit
9548c51651
5 changed files with 194 additions and 132 deletions
BIN
output.pdf
BIN
output.pdf
Binary file not shown.
|
@ -1,13 +1,4 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
||||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
||||||
return new (P || (P = Promise))(function (resolve, reject) {
|
|
||||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
||||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
||||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
||||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
||||||
});
|
|
||||||
};
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.BreakLineAlgorithm = void 0;
|
exports.BreakLineAlgorithm = void 0;
|
||||||
/**
|
/**
|
||||||
|
@ -81,7 +72,6 @@ class BreakLineAlgorithm {
|
||||||
* check all the total cost of paragraphes of the segnemt
|
* check all the total cost of paragraphes of the segnemt
|
||||||
*/
|
*/
|
||||||
totalCost(items, lineWidth) {
|
totalCost(items, lineWidth) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
|
||||||
let lineWidthFixed = lineWidth * 0.75;
|
let lineWidthFixed = lineWidth * 0.75;
|
||||||
let itemsLength = items.length;
|
let itemsLength = items.length;
|
||||||
this.lineCostStorage = Array(itemsLength);
|
this.lineCostStorage = Array(itemsLength);
|
||||||
|
@ -91,17 +81,14 @@ class BreakLineAlgorithm {
|
||||||
}
|
}
|
||||||
this.totalCostAuxStorage = Array(itemsLength).fill(null);
|
this.totalCostAuxStorage = Array(itemsLength).fill(null);
|
||||||
let a = Infinity;
|
let a = Infinity;
|
||||||
for (var k = itemsLength - 2; (yield this.lineCost(items, k + 1, itemsLength - 1, lineWidthFixed)) < Infinity; k--) {
|
for (var k = itemsLength - 2; this.lineCost(items, k + 1, itemsLength - 1, lineWidthFixed) < Infinity; k--) {
|
||||||
let tmp = yield this.totalCostAux(items, k, lineWidthFixed);
|
let tmp = this.totalCostAux(items, k, lineWidthFixed);
|
||||||
if (a > tmp) {
|
if (a > tmp) {
|
||||||
this.prevNodes[itemsLength - 1] = k;
|
this.prevNodes[itemsLength - 1] = k;
|
||||||
a = tmp;
|
a = tmp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log("~~~", lineWidth);
|
|
||||||
console.log(items[itemsLength - 2]);
|
|
||||||
return a;
|
return a;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* check the total cost item[0..j].
|
* check the total cost item[0..j].
|
||||||
|
@ -110,11 +97,10 @@ class BreakLineAlgorithm {
|
||||||
* @param lineWidth
|
* @param lineWidth
|
||||||
*/
|
*/
|
||||||
totalCostAux(items, j, lineWidth) {
|
totalCostAux(items, j, lineWidth) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
|
||||||
if (this.totalCostAuxStorage[j] !== null) {
|
if (this.totalCostAuxStorage[j] !== null) {
|
||||||
return this.totalCostAuxStorage[j];
|
return this.totalCostAuxStorage[j];
|
||||||
}
|
}
|
||||||
let rawLineCost = yield this.lineCost(items, 0, j, lineWidth);
|
let rawLineCost = this.lineCost(items, 0, j, lineWidth);
|
||||||
if (rawLineCost != Infinity) {
|
if (rawLineCost != Infinity) {
|
||||||
this.totalCostAuxStorage[j] = rawLineCost ** 3.0;
|
this.totalCostAuxStorage[j] = rawLineCost ** 3.0;
|
||||||
return rawLineCost ** 3.0;
|
return rawLineCost ** 3.0;
|
||||||
|
@ -122,8 +108,7 @@ class BreakLineAlgorithm {
|
||||||
else {
|
else {
|
||||||
var returnCost = Infinity;
|
var returnCost = Infinity;
|
||||||
for (var k = 0; k < j; k++) {
|
for (var k = 0; k < j; k++) {
|
||||||
let tmp1 = yield Promise.all([this.totalCostAux(items, k, lineWidth), this.lineCost(items, k + 1, j, lineWidth)]);
|
let tmp = this.totalCostAux(items, k, lineWidth) + this.lineCost(items, k + 1, j, lineWidth) ** 3.0;
|
||||||
let tmp = tmp1[0] + tmp1[1] ** 3;
|
|
||||||
if (returnCost > tmp) {
|
if (returnCost > tmp) {
|
||||||
this.prevNodes[j] = k;
|
this.prevNodes[j] = k;
|
||||||
returnCost = tmp;
|
returnCost = tmp;
|
||||||
|
@ -132,7 +117,6 @@ class BreakLineAlgorithm {
|
||||||
this.totalCostAuxStorage[j] = returnCost;
|
this.totalCostAuxStorage[j] = returnCost;
|
||||||
return returnCost;
|
return returnCost;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* check the line cost of a line containing items[i..j]
|
* check the line cost of a line containing items[i..j]
|
||||||
|
@ -142,8 +126,8 @@ class BreakLineAlgorithm {
|
||||||
* @param lineWidth line width
|
* @param lineWidth line width
|
||||||
*/
|
*/
|
||||||
lineCost(items, i, j, lineWidth) {
|
lineCost(items, i, j, lineWidth) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
if (this.lineCostStorage[i][j] !== null) {
|
||||||
if (this.lineCostStorage[i] !== null && this.lineCostStorage[i][j] !== null) {
|
console.log("AA");
|
||||||
return this.lineCostStorage[i][j];
|
return this.lineCostStorage[i][j];
|
||||||
}
|
}
|
||||||
if (!this.isBreakPoint(items[j])) {
|
if (!this.isBreakPoint(items[j])) {
|
||||||
|
@ -166,7 +150,6 @@ class BreakLineAlgorithm {
|
||||||
return returnValue;
|
return returnValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.BreakLineAlgorithm = BreakLineAlgorithm;
|
exports.BreakLineAlgorithm = BreakLineAlgorithm;
|
||||||
|
|
|
@ -51,6 +51,7 @@ export class BreakLineAlgorithm {
|
||||||
}
|
}
|
||||||
|
|
||||||
segmentedNodes(items : BoxesItem[], lineWidth : number) : BoxesItem[][]{
|
segmentedNodes(items : BoxesItem[], lineWidth : number) : BoxesItem[][]{
|
||||||
|
|
||||||
let lineWidthFixed = lineWidth;
|
let lineWidthFixed = lineWidth;
|
||||||
this.totalCost(items ,lineWidthFixed);
|
this.totalCost(items ,lineWidthFixed);
|
||||||
let nodeList = this.generateBreakLineNodeList();
|
let nodeList = this.generateBreakLineNodeList();
|
||||||
|
@ -64,6 +65,8 @@ export class BreakLineAlgorithm {
|
||||||
up = nodeList[i+1];
|
up = nodeList[i+1];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,16 +108,15 @@ export class BreakLineAlgorithm {
|
||||||
|
|
||||||
let a = Infinity;
|
let a = Infinity;
|
||||||
for(var k=itemsLength-2; this.lineCost(items, k+1,itemsLength-1, lineWidthFixed) < Infinity; k--){
|
for(var k=itemsLength-2; this.lineCost(items, k+1,itemsLength-1, lineWidthFixed) < Infinity; k--){
|
||||||
|
|
||||||
let tmp = this.totalCostAux(items, k, lineWidthFixed);
|
let tmp = this.totalCostAux(items, k, lineWidthFixed);
|
||||||
|
|
||||||
if (a > tmp){
|
if (a > tmp){
|
||||||
this.prevNodes[itemsLength-1] = k
|
this.prevNodes[itemsLength-1] = k
|
||||||
a = tmp;
|
a = tmp;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
console.log("~~~", lineWidth);
|
}
|
||||||
console.log((<CharBox>items[itemsLength-2]));
|
|
||||||
return a;
|
return a;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -163,7 +165,8 @@ export class BreakLineAlgorithm {
|
||||||
* @param lineWidth line width
|
* @param lineWidth line width
|
||||||
*/
|
*/
|
||||||
lineCost(items : BoxesItem[], i : number, j : number, lineWidth: number) : number{
|
lineCost(items : BoxesItem[], i : number, j : number, lineWidth: number) : number{
|
||||||
if (this.lineCostStorage[i] !== null && this.lineCostStorage[i][j] !== null){
|
if (this.lineCostStorage[i][j] !== null){
|
||||||
|
console.log("AA")
|
||||||
return this.lineCostStorage[i][j];
|
return this.lineCostStorage[i][j];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -245,8 +245,13 @@ exports.hyphenTkTree = hyphenTkTree;
|
||||||
function calculateTextWidthHeight(element, style) {
|
function calculateTextWidthHeight(element, style) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
var res = [];
|
var res = [];
|
||||||
|
var styleCache = {};
|
||||||
|
var fontCache = {};
|
||||||
for (var i = 0; i < element.length; i++) {
|
for (var i = 0; i < element.length; i++) {
|
||||||
res.push(yield calculateTextWidthHeightAux(element[i], style));
|
let item = yield calculateTextWidthHeightAux(element[i], style, styleCache, fontCache);
|
||||||
|
styleCache = item[1];
|
||||||
|
fontCache = item[2];
|
||||||
|
res.push(item[0]);
|
||||||
}
|
}
|
||||||
res = res.flat();
|
res = res.flat();
|
||||||
return res;
|
return res;
|
||||||
|
@ -258,15 +263,25 @@ exports.calculateTextWidthHeight = calculateTextWidthHeight;
|
||||||
* @param preprocessed
|
* @param preprocessed
|
||||||
* @param defaultFontStyle
|
* @param defaultFontStyle
|
||||||
*/
|
*/
|
||||||
function calculateTextWidthHeightAux(element, style) {
|
function calculateTextWidthHeightAux(element, style, styleCache, fontCache) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
var result = [];
|
var result = [];
|
||||||
let fontPair = (0, canva_1.fontStyleTofont)(style);
|
var font;
|
||||||
if (fontPair.path.match(/\.ttc$/)) {
|
if (style === styleCache) {
|
||||||
var font = yield fontkit.openSync(fontPair.path, fontPair.psName);
|
font = fontCache;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var font = yield fontkit.openSync(fontPair.path);
|
let fontPair = (0, canva_1.fontStyleTofont)(style);
|
||||||
|
if (fontPair.path.match(/\.ttc$/)) {
|
||||||
|
font = yield fontkit.openSync(fontPair.path, fontPair.psName);
|
||||||
|
styleCache = style;
|
||||||
|
fontCache = font;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
font = yield fontkit.openSync(fontPair.path);
|
||||||
|
styleCache = style;
|
||||||
|
fontCache = font;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!Array.isArray(element)) {
|
if (!Array.isArray(element)) {
|
||||||
var run = font.layout(element, undefined, undefined, undefined, "ltr");
|
var run = font.layout(element, undefined, undefined, undefined, "ltr");
|
||||||
|
@ -287,14 +302,14 @@ function calculateTextWidthHeightAux(element, style) {
|
||||||
};
|
};
|
||||||
result.push(item);
|
result.push(item);
|
||||||
}
|
}
|
||||||
return result;
|
return [result, styleCache, fontCache];
|
||||||
}
|
}
|
||||||
else if (element[0] == "bp") {
|
else if (element[0] == "bp") {
|
||||||
var beforeNewLine = yield calculateTextWidthHeightAux(element[1], style);
|
var beforeNewLine = (yield calculateTextWidthHeightAux(element[1], style, styleCache, fontCache))[0];
|
||||||
if (Array.isArray(beforeNewLine)) {
|
if (Array.isArray(beforeNewLine)) {
|
||||||
beforeNewLine = beforeNewLine.flat();
|
beforeNewLine = beforeNewLine.flat();
|
||||||
}
|
}
|
||||||
let afterNewLine = yield calculateTextWidthHeightAux(element[2], style);
|
let afterNewLine = (yield calculateTextWidthHeightAux(element[2], style, styleCache, fontCache))[0];
|
||||||
if (Array.isArray(afterNewLine)) {
|
if (Array.isArray(afterNewLine)) {
|
||||||
afterNewLine = afterNewLine.flat();
|
afterNewLine = afterNewLine.flat();
|
||||||
}
|
}
|
||||||
|
@ -302,14 +317,14 @@ function calculateTextWidthHeightAux(element, style) {
|
||||||
original: beforeNewLine,
|
original: beforeNewLine,
|
||||||
newLined: afterNewLine,
|
newLined: afterNewLine,
|
||||||
};
|
};
|
||||||
return breakPointNode;
|
return [breakPointNode, styleCache, fontCache];
|
||||||
}
|
}
|
||||||
else if (element[0] == "hglue" && !Array.isArray(element[1])) {
|
else if (element[0] == "hglue" && !Array.isArray(element[1])) {
|
||||||
let hGlue = { stretchFactor: parseFloat(element[1]) };
|
let hGlue = { stretchFactor: parseFloat(element[1]) };
|
||||||
return hGlue;
|
return [hGlue, styleCache, fontCache];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return calculateTextWidthHeight(element, style);
|
return [yield calculateTextWidthHeight(element, style), styleCache, fontCache];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -362,47 +377,58 @@ class Clo {
|
||||||
let defaultFontStyle = this.attrs.defaultFrameStyle.textStyle;
|
let defaultFontStyle = this.attrs.defaultFrameStyle.textStyle;
|
||||||
let a = yield calculateTextWidthHeight(preprocessed, defaultFontStyle);
|
let a = yield calculateTextWidthHeight(preprocessed, defaultFontStyle);
|
||||||
let breakLineAlgorithms = new breakLines.BreakLineAlgorithm();
|
let breakLineAlgorithms = new breakLines.BreakLineAlgorithm();
|
||||||
// TODO
|
|
||||||
//console.log(breakLineAlgorithms.totalCost(a,70));
|
|
||||||
let segmentedNodes = breakLineAlgorithms.segmentedNodes(a, this.attrs.defaultFrameStyle.width);
|
let segmentedNodes = breakLineAlgorithms.segmentedNodes(a, this.attrs.defaultFrameStyle.width);
|
||||||
let segmentedNodesToBox = this.segmentedNodesToFrameBox(segmentedNodes, this.attrs.defaultFrameStyle);
|
let segmentedNodesToBox = this.segmentedNodesToFrameBox(segmentedNodes, this.attrs.defaultFrameStyle);
|
||||||
let boxesFixed = this.fixenBoxesPosition(segmentedNodesToBox);
|
let boxesFixed = this.fixenBoxesPosition(segmentedNodesToBox);
|
||||||
// generate pdf7
|
// generate pdf
|
||||||
const doc = new PDFDocument({ size: 'A4' });
|
const doc = new PDFDocument({ size: 'A4' });
|
||||||
doc.pipe(fs.createWriteStream('output.pdf'));
|
doc.pipe(fs.createWriteStream('output.pdf'));
|
||||||
this.grid(doc);
|
this.grid(doc);
|
||||||
yield this.putText(doc, boxesFixed);
|
let styleCache = {};
|
||||||
|
let fontPairCache = { path: "", psName: "" };
|
||||||
|
yield this.putText(doc, boxesFixed, styleCache, fontPairCache);
|
||||||
// putChar
|
// putChar
|
||||||
doc.end();
|
doc.end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
putText(doc, box) {
|
putText(doc, box, styleCache, fontPairCache) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
var fontPair;
|
||||||
if (box.textStyle !== null) {
|
if (box.textStyle !== null) {
|
||||||
let fontInfo = (0, canva_1.fontStyleTofont)(box.textStyle);
|
if (box.textStyle == styleCache) {
|
||||||
if (fontInfo.path.match(/\.ttc$/g)) {
|
fontPair = fontPairCache;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fontPair = (0, canva_1.fontStyleTofont)(box.textStyle);
|
||||||
|
styleCache = box.textStyle;
|
||||||
|
fontPairCache = fontPair;
|
||||||
|
if (fontPair.path.match(/\.ttc$/g)) {
|
||||||
doc
|
doc
|
||||||
.font(fontInfo.path, fontInfo.psName)
|
.font(fontPair.path, fontPair.psName)
|
||||||
.fontSize(box.textStyle.size * 0.75);
|
.fontSize(box.textStyle.size * 0.75);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
doc
|
doc
|
||||||
.font(fontInfo.path)
|
.font(fontPair.path)
|
||||||
.fontSize(box.textStyle.size * 0.75); // 0.75 must added!
|
.fontSize(box.textStyle.size * 0.75); // 0.75 must added!
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (box.textStyle.color !== undefined) {
|
if (box.textStyle.color !== undefined) {
|
||||||
doc.fill(box.textStyle.color);
|
doc.fill(box.textStyle.color);
|
||||||
}
|
}
|
||||||
if (Array.isArray(box.content)) {
|
if (Array.isArray(box.content)) {
|
||||||
for (var k = 0; k < box.content.length; k++) {
|
for (var k = 0; k < box.content.length; k++) {
|
||||||
doc = yield this.putText(doc, box.content[k]);
|
let tmp = yield this.putText(doc, box.content[k], styleCache, fontPairCache);
|
||||||
|
doc = tmp[0];
|
||||||
|
styleCache = tmp[1];
|
||||||
|
fontPairCache = tmp[2];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (box.content !== null) {
|
else if (box.content !== null) {
|
||||||
yield doc.text(box.content, (box.x !== null ? box.x : undefined), (box.y !== null ? box.y : undefined));
|
yield doc.text(box.content, (box.x !== null ? box.x : undefined), (box.y !== null ? box.y : undefined));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return doc;
|
return [doc, styleCache, fontPairCache];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import {tkTree} from "../parser";
|
import {tkTree} from "../parser";
|
||||||
import {FontStyle, TextStyle, TextWeight, fontStyleTofont} from "../canva";
|
import {FontStyle, TextStyle, TextWeight, fontStyleTofont, fontPathPSNamePair} from "../canva";
|
||||||
import * as fontkit from "fontkit";
|
import * as fontkit from "fontkit";
|
||||||
import * as breakLines from "./breakLines";
|
import * as breakLines from "./breakLines";
|
||||||
const PDFDocument = require('pdfkit');
|
const PDFDocument = require('pdfkit');
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
|
import { Style } from "util";
|
||||||
|
import { time } from "console";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -279,6 +281,8 @@ export function hyphenTkTree(arr : tkTree, lang: string) : tkTree{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* calculate the text width and Height with a given `TextStyle`
|
* calculate the text width and Height with a given `TextStyle`
|
||||||
* @param preprocessed
|
* @param preprocessed
|
||||||
|
@ -286,9 +290,14 @@ export function hyphenTkTree(arr : tkTree, lang: string) : tkTree{
|
||||||
*/
|
*/
|
||||||
export async function calculateTextWidthHeight(element : tkTree, style : TextStyle): Promise<BoxesItem[]> {
|
export async function calculateTextWidthHeight(element : tkTree, style : TextStyle): Promise<BoxesItem[]> {
|
||||||
var res = [];
|
var res = [];
|
||||||
|
var styleCache = {};
|
||||||
|
var fontCache = {};
|
||||||
|
|
||||||
for (var i=0; i<element.length; i++){
|
for (var i=0; i<element.length; i++){
|
||||||
res.push(await calculateTextWidthHeightAux(element[i], style));
|
let item = await calculateTextWidthHeightAux(element[i], style, <TextStyle>styleCache, <fontkit.Font>fontCache);
|
||||||
|
styleCache = item[1];
|
||||||
|
fontCache = item[2];
|
||||||
|
res.push(item[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
res = res.flat();
|
res = res.flat();
|
||||||
|
@ -302,18 +311,37 @@ export async function calculateTextWidthHeight(element : tkTree, style : TextSty
|
||||||
* @param preprocessed
|
* @param preprocessed
|
||||||
* @param defaultFontStyle
|
* @param defaultFontStyle
|
||||||
*/
|
*/
|
||||||
export async function calculateTextWidthHeightAux(element : tkTree, style : TextStyle): Promise<BoxesItem> {
|
export async function calculateTextWidthHeightAux(element : tkTree,
|
||||||
|
style : TextStyle,
|
||||||
|
styleCache : TextStyle,
|
||||||
|
fontCache : fontkit.Font): Promise<[BoxesItem, TextStyle, fontkit.Font] > {
|
||||||
var result : BoxesItem = [];
|
var result : BoxesItem = [];
|
||||||
|
var font;
|
||||||
|
|
||||||
|
if (style === styleCache){
|
||||||
|
font = fontCache;
|
||||||
|
}else {
|
||||||
|
|
||||||
|
|
||||||
let fontPair = fontStyleTofont(style);
|
let fontPair = fontStyleTofont(style);
|
||||||
|
|
||||||
if (fontPair.path.match(/\.ttc$/)){
|
if (fontPair.path.match(/\.ttc$/)){
|
||||||
var font = await fontkit.openSync(fontPair.path, fontPair.psName);
|
font = await fontkit.openSync(fontPair.path, fontPair.psName);
|
||||||
|
styleCache = style;
|
||||||
|
fontCache = font;
|
||||||
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
var font = await fontkit.openSync(fontPair.path);
|
font = await fontkit.openSync(fontPair.path);
|
||||||
|
styleCache = style;
|
||||||
|
fontCache = font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (!Array.isArray(element)){
|
if (!Array.isArray(element)){
|
||||||
var run = font.layout(element, undefined, undefined, undefined, "ltr");
|
var run = font.layout(element, undefined, undefined, undefined, "ltr");
|
||||||
|
|
||||||
|
@ -340,19 +368,20 @@ export async function calculateTextWidthHeightAux(element : tkTree, style : Text
|
||||||
result.push(item);
|
result.push(item);
|
||||||
|
|
||||||
}
|
}
|
||||||
return result;
|
return [result, styleCache, fontCache];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}else if(element[0] == "bp"){
|
}else if(element[0] == "bp"){
|
||||||
|
|
||||||
var beforeNewLine = await calculateTextWidthHeightAux(element[1], style);
|
|
||||||
|
var beforeNewLine = (await calculateTextWidthHeightAux(element[1], style, styleCache, fontCache))[0];
|
||||||
if (Array.isArray(beforeNewLine)){
|
if (Array.isArray(beforeNewLine)){
|
||||||
beforeNewLine = beforeNewLine.flat();
|
beforeNewLine = beforeNewLine.flat();
|
||||||
}
|
}
|
||||||
|
|
||||||
let afterNewLine = await calculateTextWidthHeightAux(element[2], style);
|
let afterNewLine = (await calculateTextWidthHeightAux(element[2], style, styleCache, fontCache))[0];
|
||||||
if (Array.isArray(afterNewLine)){
|
if (Array.isArray(afterNewLine)){
|
||||||
afterNewLine = afterNewLine.flat();
|
afterNewLine = afterNewLine.flat();
|
||||||
}
|
}
|
||||||
|
@ -362,13 +391,13 @@ export async function calculateTextWidthHeightAux(element : tkTree, style : Text
|
||||||
newLined : afterNewLine,
|
newLined : afterNewLine,
|
||||||
}
|
}
|
||||||
|
|
||||||
return breakPointNode;
|
return [breakPointNode, styleCache, fontCache];
|
||||||
}else if(element[0] == "hglue" && !Array.isArray(element[1])){
|
}else if(element[0] == "hglue" && !Array.isArray(element[1])){
|
||||||
let hGlue : HGlue = {stretchFactor : parseFloat(element[1])}
|
let hGlue : HGlue = {stretchFactor : parseFloat(element[1])}
|
||||||
return hGlue;
|
return [hGlue, styleCache, fontCache];
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
return calculateTextWidthHeight(element, style);
|
return [await calculateTextWidthHeight(element, style), styleCache, fontCache];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -428,56 +457,72 @@ export class Clo{
|
||||||
}
|
}
|
||||||
|
|
||||||
public async generatePdf(){
|
public async generatePdf(){
|
||||||
|
|
||||||
// preprocessed
|
// preprocessed
|
||||||
var preprocessed = this.mainStream;
|
var preprocessed = this.mainStream;
|
||||||
for (var i = 0; i<this.preprocessors.length; i++){
|
for (var i = 0; i<this.preprocessors.length; i++){
|
||||||
preprocessed = this.preprocessors[i](preprocessed, this);
|
preprocessed = this.preprocessors[i](preprocessed, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate the width and height of the stream
|
// generate the width and height of the stream
|
||||||
|
|
||||||
let defaultFontStyle : TextStyle = this.attrs.defaultFrameStyle.textStyle;
|
let defaultFontStyle : TextStyle = this.attrs.defaultFrameStyle.textStyle;
|
||||||
|
|
||||||
|
|
||||||
let a = await calculateTextWidthHeight(preprocessed, defaultFontStyle);
|
let a = await calculateTextWidthHeight(preprocessed, defaultFontStyle);
|
||||||
|
|
||||||
let breakLineAlgorithms = new breakLines.BreakLineAlgorithm();
|
let breakLineAlgorithms = new breakLines.BreakLineAlgorithm();
|
||||||
// TODO
|
|
||||||
//console.log(breakLineAlgorithms.totalCost(a,70));
|
|
||||||
let segmentedNodes = breakLineAlgorithms.segmentedNodes(a, this.attrs.defaultFrameStyle.width);
|
let segmentedNodes = breakLineAlgorithms.segmentedNodes(a, this.attrs.defaultFrameStyle.width);
|
||||||
|
|
||||||
let segmentedNodesToBox =
|
let segmentedNodesToBox =
|
||||||
this.segmentedNodesToFrameBox(segmentedNodes, <FrameBox>this.attrs.defaultFrameStyle);
|
this.segmentedNodesToFrameBox(segmentedNodes, <FrameBox>this.attrs.defaultFrameStyle);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let boxesFixed = this.fixenBoxesPosition(segmentedNodesToBox);
|
let boxesFixed = this.fixenBoxesPosition(segmentedNodesToBox);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// generate pdf7
|
// generate pdf
|
||||||
const doc = new PDFDocument({size: 'A4'});
|
const doc = new PDFDocument({size: 'A4'});
|
||||||
doc.pipe(fs.createWriteStream('output.pdf'));
|
doc.pipe(fs.createWriteStream('output.pdf'));
|
||||||
this.grid(doc);
|
this.grid(doc);
|
||||||
|
|
||||||
await this.putText(doc, boxesFixed);
|
let styleCache : any = {};
|
||||||
|
let fontPairCache : fontPathPSNamePair = {path : "", psName : ""};
|
||||||
|
await this.putText(doc, boxesFixed, <TextStyle>styleCache, fontPairCache);
|
||||||
// putChar
|
// putChar
|
||||||
doc.end();
|
doc.end();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async putText(doc : PDFKit.PDFDocument, box : Box): Promise<PDFKit.PDFDocument>{
|
async putText(doc : PDFKit.PDFDocument, box : Box, styleCache : TextStyle,
|
||||||
|
fontPairCache : fontPathPSNamePair):
|
||||||
|
Promise<[PDFKit.PDFDocument, TextStyle, fontPathPSNamePair]>{
|
||||||
|
var fontPair;
|
||||||
|
|
||||||
|
|
||||||
if (box.textStyle !== null){
|
if (box.textStyle !== null){
|
||||||
let fontInfo = fontStyleTofont(box.textStyle);
|
|
||||||
|
|
||||||
if (fontInfo.path.match(/\.ttc$/g)){
|
if(box.textStyle == styleCache){
|
||||||
|
fontPair = fontPairCache;
|
||||||
|
}else{
|
||||||
|
fontPair = fontStyleTofont(box.textStyle);
|
||||||
|
styleCache = box.textStyle;
|
||||||
|
fontPairCache = fontPair;
|
||||||
|
|
||||||
|
if (fontPair.path.match(/\.ttc$/g)){
|
||||||
doc
|
doc
|
||||||
.font(fontInfo.path, fontInfo.psName)
|
.font(fontPair.path, fontPair.psName)
|
||||||
.fontSize(box.textStyle.size * 0.75);}
|
.fontSize(box.textStyle.size * 0.75);}
|
||||||
else{
|
else{
|
||||||
doc
|
doc
|
||||||
.font(fontInfo.path)
|
.font(fontPair.path)
|
||||||
.fontSize(box.textStyle.size * 0.75); // 0.75 must added!
|
.fontSize(box.textStyle.size * 0.75); // 0.75 must added!
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (box.textStyle.color !== undefined){
|
if (box.textStyle.color !== undefined){
|
||||||
doc.fill(box.textStyle.color);
|
doc.fill(box.textStyle.color);
|
||||||
|
@ -486,7 +531,10 @@ export class Clo{
|
||||||
if (Array.isArray(box.content)){
|
if (Array.isArray(box.content)){
|
||||||
for (var k=0; k<box.content.length; k++){
|
for (var k=0; k<box.content.length; k++){
|
||||||
|
|
||||||
doc = await this.putText(doc, box.content[k]);
|
let tmp = await this.putText(doc, box.content[k], styleCache, fontPairCache);
|
||||||
|
doc = tmp[0];
|
||||||
|
styleCache = tmp[1];
|
||||||
|
fontPairCache = tmp[2];
|
||||||
}
|
}
|
||||||
}else if (box.content !== null){
|
}else if (box.content !== null){
|
||||||
await doc.text(box.content,
|
await doc.text(box.content,
|
||||||
|
@ -495,7 +543,9 @@ export class Clo{
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return doc;
|
|
||||||
|
|
||||||
|
return [doc, styleCache, fontPairCache];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue