From: Tan Kian-ting Date: Wed, 15 Nov 2023 04:37:21 +0000 (+0800) Subject: add grid X-Git-Url: https://git.kianting.info/?a=commitdiff_plain;h=134485a6d637ce5b422098c08bccb274c73bca86;p=clo add grid --- diff --git a/blank.pdf b/blank.pdf new file mode 100644 index 0000000..92353b2 Binary files /dev/null and b/blank.pdf differ diff --git a/package-lock.json b/package-lock.json index 4883791..52593f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "jsdom": "^22.1.0", "minimist": "^1.2.8", "npx": "^10.2.2", + "pdf-lib": "^1.17.1", "pdfkit": "^0.13.0", "typescript-parsec": "^0.3.4" }, @@ -741,6 +742,22 @@ "node": ">= 8" } }, + "node_modules/@pdf-lib/standard-fonts": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz", + "integrity": "sha512-hU30BK9IUN/su0Mn9VdlVKsWBS6GyhVfqjwl1FjZN4TxP6cCw0jP2w7V3Hf5uX7M0AZJ16vey9yE0ny7Sa59ZA==", + "dependencies": { + "pako": "^1.0.6" + } + }, + "node_modules/@pdf-lib/upng": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@pdf-lib/upng/-/upng-1.0.1.tgz", + "integrity": "sha512-dQK2FUMQtowVP00mtIksrlZhdFXQZPC+taih1q4CvPZ5vqdxR/LKBaFg0oAfzd1GlHZXXSPdQfzQnt+ViGvEIQ==", + "dependencies": { + "pako": "^1.0.10" + } + }, "node_modules/@swc/helpers": { "version": "0.4.36", "resolved": "https://registry.npmjs.org/@swc/helpers/-/helpers-0.4.36.tgz", @@ -9569,6 +9586,11 @@ "node": ">=8" } }, + "node_modules/pako": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==" + }, "node_modules/parent-module": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz", @@ -9642,6 +9664,17 @@ "node": "*" } }, + "node_modules/pdf-lib": { + "version": "1.17.1", + "resolved": "https://registry.npmjs.org/pdf-lib/-/pdf-lib-1.17.1.tgz", + "integrity": "sha512-V/mpyJAoTsN4cnP31vc0wfNA1+p20evqqnap0KLoRUN0Yk/p3wN52DOEsL4oBFcLdb76hlpKPtzJIgo67j/XLw==", + "dependencies": { + "@pdf-lib/standard-fonts": "^1.0.0", + "@pdf-lib/upng": "^1.0.1", + "pako": "^1.0.11", + "tslib": "^1.11.1" + } + }, "node_modules/pdfkit": { "version": "0.13.0", "resolved": "https://registry.npmjs.org/pdfkit/-/pdfkit-0.13.0.tgz", @@ -10515,8 +10548,7 @@ "node_modules/tslib": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", - "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", - "dev": true + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" }, "node_modules/tslint": { "version": "6.1.3", @@ -11736,6 +11768,22 @@ "fastq": "^1.6.0" } }, + "@pdf-lib/standard-fonts": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz", + "integrity": "sha512-hU30BK9IUN/su0Mn9VdlVKsWBS6GyhVfqjwl1FjZN4TxP6cCw0jP2w7V3Hf5uX7M0AZJ16vey9yE0ny7Sa59ZA==", + "requires": { + "pako": "^1.0.6" + } + }, + "@pdf-lib/upng": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@pdf-lib/upng/-/upng-1.0.1.tgz", + "integrity": "sha512-dQK2FUMQtowVP00mtIksrlZhdFXQZPC+taih1q4CvPZ5vqdxR/LKBaFg0oAfzd1GlHZXXSPdQfzQnt+ViGvEIQ==", + "requires": { + "pako": "^1.0.10" + } + }, "@swc/helpers": { "version": "0.4.36", "resolved": "https://registry.npmjs.org/@swc/helpers/-/helpers-0.4.36.tgz", @@ -18319,6 +18367,11 @@ "release-zalgo": "^1.0.0" } }, + "pako": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==" + }, "parent-module": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz", @@ -18371,6 +18424,17 @@ "integrity": "sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==", "dev": true }, + "pdf-lib": { + "version": "1.17.1", + "resolved": "https://registry.npmjs.org/pdf-lib/-/pdf-lib-1.17.1.tgz", + "integrity": "sha512-V/mpyJAoTsN4cnP31vc0wfNA1+p20evqqnap0KLoRUN0Yk/p3wN52DOEsL4oBFcLdb76hlpKPtzJIgo67j/XLw==", + "requires": { + "@pdf-lib/standard-fonts": "^1.0.0", + "@pdf-lib/upng": "^1.0.1", + "pako": "^1.0.11", + "tslib": "^1.11.1" + } + }, "pdfkit": { "version": "0.13.0", "resolved": "https://registry.npmjs.org/pdfkit/-/pdfkit-0.13.0.tgz", @@ -19001,8 +19065,7 @@ "tslib": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", - "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", - "dev": true + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" }, "tslint": { "version": "6.1.3", diff --git a/package.json b/package.json index 1f75e5c..90711f6 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "jsdom": "^22.1.0", "minimist": "^1.2.8", "npx": "^10.2.2", + "pdf-lib": "^1.17.1", "pdfkit": "^0.13.0", "typescript-parsec": "^0.3.4" } diff --git a/src/libclo/breakLines.js b/src/libclo/breakLines.js index e5d0865..b94d123 100644 --- a/src/libclo/breakLines.js +++ b/src/libclo/breakLines.js @@ -14,7 +14,7 @@ class BreakLineAlgorithm { isBreakPoint(item) { return item.newLined !== undefined; } - /**check if a boeitem is BreakPoint Type */ + /**check if a boeitem is HGlue Type */ isHGlue(item) { return item.stretchFactor !== undefined; } diff --git a/src/libclo/index.js b/src/libclo/index.js index c2b873e..17b36b4 100644 --- a/src/libclo/index.js +++ b/src/libclo/index.js @@ -36,6 +36,9 @@ exports.Clo = exports.calculateTextWidthHeightAux = exports.calculateTextWidthHe const canva_1 = require("../canva"); const fontkit = __importStar(require("fontkit")); const breakLines = __importStar(require("./breakLines")); +require("pdfkit"); +const pdf_lib_1 = require("pdf-lib"); +const fs = __importStar(require("fs")); /** * TYPES */ @@ -364,6 +367,37 @@ class Clo { //console.log(breakLineAlgorithms.totalCost(a,70)); let segmentedNodes = breakLineAlgorithms.segmentedNodes(a, 70); console.log(this.segmentedNodesToFrameBox(segmentedNodes, this.attrs["defaultFrameStyle"])); + // generate pdf + const pdfDoc = yield pdf_lib_1.PDFDocument.create(); + var page = pdfDoc.addPage(); + page.drawText('You can create PDFs!'); + for (var j = 0; j < 1000; j += 5) { + if (j % 50 == 0) { + page.drawText(i.toString(), { x: 50, y: j }); + } + page.drawLine({ + start: { x: 0, y: j }, + end: { x: 1000, y: j }, + thickness: 0.5, + color: (0, pdf_lib_1.rgb)(0.75, 0.2, 0.2), + opacity: 0.20, + }); + } + for (var i = 0; i < 1000; i += 5) { + if (i % 50 == 0) { + page.drawText(i.toString(), { x: i, y: 50 }); + } + page.drawLine({ + start: { x: i, y: 0 }, + end: { x: i, y: 1000 }, + thickness: 0.5, + color: (0, pdf_lib_1.rgb)(0.75, 0.2, 0.2), + opacity: 0.20, + }); + } + pdfDoc.save(); + const pdfBytes = yield pdfDoc.save(); + fs.writeFileSync("blank.pdf", pdfBytes); }); } segmentedNodesToFrameBox(segmentedNodes, frame) { diff --git a/src/libclo/index.ts b/src/libclo/index.ts index b64a819..bcd6a46 100644 --- a/src/libclo/index.ts +++ b/src/libclo/index.ts @@ -5,6 +5,10 @@ import { JSDOM } from "jsdom"; import * as fontkit from "fontkit"; import * as util from "node:util"; import * as breakLines from "./breakLines"; +import "pdfkit"; +import PDFKitPage from "pdfkit/js/page"; +import { ColorTypes, PDFDocument, rgb } from "pdf-lib"; +import * as fs from "fs"; /** * TYPES @@ -443,7 +447,44 @@ export class Clo{ //console.log(breakLineAlgorithms.totalCost(a,70)); let segmentedNodes = breakLineAlgorithms.segmentedNodes(a, 70); - console.log(this.segmentedNodesToFrameBox(segmentedNodes, this.attrs["defaultFrameStyle"])); + console.log( + this.segmentedNodesToFrameBox(segmentedNodes, this.attrs["defaultFrameStyle"])); + + // generate pdf + const pdfDoc = await PDFDocument.create(); + var page = pdfDoc.addPage(); + page.drawText('You can create PDFs!'); + + for (var j = 0; j<1000; j+=5){ + if (j %50 == 0){ + page.drawText(i.toString(), {x: 50, y: j}); + } + + page.drawLine({ + start: { x: 0, y: j }, + end: { x: 1000, y: j }, + thickness: 0.5, + color: rgb(0.75, 0.2, 0.2), + opacity: 0.20, + }); + } + + for (var i = 0; i<1000; i+=5){ + if (i % 50 == 0){ + page.drawText(i.toString(), {x: i, y: 50}); + } + page.drawLine({ + start: { x: i, y: 0 }, + end: { x: i, y: 1000 }, + thickness: 0.5, + color: rgb(0.75, 0.2, 0.2), + opacity: 0.20, + }); + } + pdfDoc.save(); + + const pdfBytes = await pdfDoc.save(); + fs.writeFileSync("blank.pdf", pdfBytes); } segmentedNodesToFrameBox(segmentedNodes : BoxesItem[][], frame : FrameBox) : Box{