From d8b33fabf7ffab673f4cd6a8201aadacf3624b41 Mon Sep 17 00:00:00 2001 From: Tan Kian-ting Date: Thu, 26 Oct 2023 23:36:20 +0800 Subject: [PATCH] add clo library initially with register of prepocessor --- README.md | 26 +++++++++++++++++++++++- b.clo | 4 ++++ b.js | 18 +++++++++++++++++ package.json | 1 + src/libclo/index.js | 38 +++++++++++++++++++++++++++++++++++ src/libclo/index.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/parser.ts | 26 +++++++++++++++--------- 7 files changed, 152 insertions(+), 10 deletions(-) create mode 100644 b.clo create mode 100644 b.js create mode 100644 src/libclo/index.js create mode 100644 src/libclo/index.ts diff --git a/README.md b/README.md index 1256c9d..4ae71cf 100644 --- a/README.md +++ b/README.md @@ -28,4 +28,28 @@ License: MIT - 20231010: 初步完成tsit ê階段ê Parser`。 - 20231012: clo->js converter successfully (maybe.) - 20231016:basic font guessing and `putText` function - - 20231023-24:fix .ttc bug. \ No newline at end of file + - 20231023-24:fix .ttc bug. + + ## 之後的做法 + - 先做一個前處理註冊器,註冊下列的前處理 + - 中英文間距 + - 換行點 + - 空白轉為 [glue] + - 前處理完成字串後,必須要: + - 算出字元的Box + - 利用 frame/box 資訊分行、分頁 + - 然後算出每個Box的x, y, page + - 最後納入排版 + +## 排版語法: + +使用lisp表示,但其實是陣列 +```lisp + (hglue 寬度 伸展值) + (vglue 高度 伸展值) + (breakpoint 原始模式 斷行模式) + (em 數字) + (ex 數字) + (span {"font-family" : "Noto Sans" , "font-size" : 16 }) + (vbox 高度 內容) +``` \ No newline at end of file diff --git a/b.clo b/b.clo new file mode 100644 index 0000000..73bb578 --- /dev/null +++ b/b.clo @@ -0,0 +1,4 @@ +--- +many臺中daylight + +aaa collee \ No newline at end of file diff --git a/b.js b/b.js new file mode 100644 index 0000000..9fe1c9f --- /dev/null +++ b/b.js @@ -0,0 +1,18 @@ + +/* clo, a typesetting engine, generated JS file*/ +/* CLO: beginning of head*/ + +let cloLib = require("./src/libclo/index.js"); +let clo = new cloLib.Clo(); + +/* CLO: end of head*/ + +/* CLO: beginning of middle part*/ +clo.mainStream = /* CLO: end of middle part*/ +[` +`, `many臺中daylight`, ` + +`, `aaa`, ` `, `collee`]; +/* CLO: beginning of end part*/ +clo.generatePdf(); +/*CLO : end of end part*/ diff --git a/package.json b/package.json index f252c0f..3f68bda 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,5 @@ { + "type": "commonjs", "name": "clo", "version": "0.0.1", "description": "a little typesetting engine in TypeScript", diff --git a/src/libclo/index.js b/src/libclo/index.js new file mode 100644 index 0000000..5bcc3f1 --- /dev/null +++ b/src/libclo/index.js @@ -0,0 +1,38 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.a = exports.Clo = void 0; +function foo(arr) { + for (let i = 0; i < arr.length; i++) { + } + if (Array.isArray(arr)) { + arr.push("balabala"); + } + return arr; +} +class Clo { + constructor() { + this.preprocessors = []; + this.mainStream = []; + this.attributes = { "page": [793.7, 1122.5] }; + this.preprocessorRegister(foo); + } + /** + * register a function of preprocessor + * @param f a function + */ + preprocessorRegister(f) { + this.preprocessors.push(f); + } + generatePdf() { + // preprocessed + var prepro = this.mainStream; + for (var i = 0; i < this.preprocessors.length; i++) { + prepro = this.preprocessors[i](prepro); + } + // TODO + console.log("test" + prepro); + } +} +exports.Clo = Clo; +exports.a = new Clo(); +exports.default = exports.a; diff --git a/src/libclo/index.ts b/src/libclo/index.ts new file mode 100644 index 0000000..b83b050 --- /dev/null +++ b/src/libclo/index.ts @@ -0,0 +1,49 @@ +import {tkTree} from "../parser"; + + +function foo(arr : tkTree): tkTree{ + for (let i = 0; i < arr.length; i++) { + + } + if (Array.isArray(arr)){ + arr.push("balabala"); + } + return arr; +} + +export class Clo{ + mainStream : Array; + preprocessors : Array; + attributes: object ; // a4 size(x,y) + + + constructor(){ + this.preprocessors = []; + this.mainStream = []; + this.attributes = {"page" : [793.7, 1122.5]}; + this.preprocessorRegister(foo); + } + + /** + * register a function of preprocessor + * @param f a function + */ + public preprocessorRegister(f : Function){ + this.preprocessors.push(f); + } + + public generatePdf(){ + // preprocessed + var prepro = this.mainStream; + for (var i = 0; i, tkTree]):tkTree { +return ["%clo", "" , parsed[1]]; +} + function applyComment(value: Token): tkTree[]{ return [value.text]; @@ -171,10 +175,12 @@ let NOT_AT = p.alt(p.tok(TokenKind.Seperator), ); /** - * PROG : IMPORTS '---' CONTENT; + * PROG : IMPORTS '---' CONTENT | '---' CONTNENT */ PROG.setPattern( - p.lrec_sc(IMPORTS, p.seq(p.str('---'), CONTENT), applyParts) + p.alt( + p.lrec_sc(IMPORTS, p.seq(p.str('---'), CONTENT), applyParts), + p.apply(p.seq(p.str('---'), CONTENT), applyPartsWithoutImport)) ) @@ -239,9 +245,10 @@ CONTENT.setPattern( let outputHead = ` /* clo, a typesetting engine, generated JS file*/ /* CLO: beginning of head*/ -import * as clo from "clo"; -cl = clo.initClo(); +let cloLib = require("./src/libclo/index.js"); +let clo = new cloLib.Clo(); + /* CLO: end of head*/\n` /** @@ -249,11 +256,11 @@ cl = clo.initClo(); */ let outputMiddle =` /* CLO: beginning of middle part*/ -cl.mainText = /* CLO: end of middle part*/ +clo.mainStream = /* CLO: end of middle part*/ ` let outputEnd =` /* CLO: beginning of end part*/ -cl.generatePdf(); +clo.generatePdf(); /*CLO : end of end part*/ ` @@ -261,6 +268,7 @@ cl.generatePdf(); * Convert `tree` (ASTTree; `tkTree`) to JS Code. */ export function treeToJS(tree : tkTree): string{ + let head = tree[0]; if (head == "%clo"){ let totalResult = outputHead + treeToJS(tree[1]) + @@ -290,7 +298,7 @@ export function treeToJS(tree : tkTree): string{ let tail = tree[1]; if (Array.isArray(tail)){ if (tail.length == 1){ - return treeToJS(tail); + return tail.map((x)=>treeToJS(x)).join("').concat('")+ ";"; } let tailStrings = tail.map((x)=>treeToJS(x)); return "(" + tailStrings.join(').concat(') + ");"; @@ -304,7 +312,7 @@ export function treeToJS(tree : tkTree): string{ let decoratedArray = textContents .flatMap(x=>String(x)) .map(x=>x.replace("\`","\\\`")); - + return "[`" + decoratedArray.join("\`, \`") + "`]"; }else{ let decorated = textContents.replace("\`","\\\`"); -- 2.39.2