]> git.kianting.info Git - clo/commitdiff
add grid
authorTan Kian-ting <chenjt30@gmail.com>
Wed, 15 Nov 2023 04:37:21 +0000 (12:37 +0800)
committerTan Kian-ting <chenjt30@gmail.com>
Wed, 15 Nov 2023 04:37:21 +0000 (12:37 +0800)
blank.pdf [new file with mode: 0644]
package-lock.json
package.json
src/libclo/breakLines.js
src/libclo/index.js
src/libclo/index.ts

diff --git a/blank.pdf b/blank.pdf
new file mode 100644 (file)
index 0000000..92353b2
Binary files /dev/null and b/blank.pdf differ
index 4883791a7544ce88393a31070aad0b788bbdce27..52593f435947d85394ae81ce91b60ccb3af95116 100644 (file)
@@ -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"
       },
         "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",
         "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",
         "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",
     "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",
         "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",
         "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",
       "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",
     "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",
index 1f75e5cb0678bc8418ad983545588d0996ceb128..90711f644e0e05753ad8e8dbd1c2c7f192b2c8f0 100644 (file)
@@ -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"
   }
index e5d086563030a8d88c1c8a252f8db5416191a670..b94d123680be05e16ef7ff3f6650fb4cdc82acba 100644 (file)
@@ -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;
     }
index c2b873e81b8c3175ed5ee532e04bafe0319885f9..17b36b41752e06493d9c785c3002b787ab2be69d 100644 (file)
@@ -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) {
index b64a819c9c719a63d2f7af34d0ec4c73d749fc7a..bcd6a46704d96b05580f4f81eea3e86eef1b214e 100644 (file)
@@ -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, <FrameBox>this.attrs["defaultFrameStyle"]));
+        console.log(
+            this.segmentedNodesToFrameBox(segmentedNodes, <FrameBox>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{