add clo library initially with register of prepocessor
This commit is contained in:
parent
a59e167307
commit
d8b33fabf7
7 changed files with 152 additions and 10 deletions
26
README.md
26
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.
|
||||
- 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 高度 內容)
|
||||
```
|
4
b.clo
Normal file
4
b.clo
Normal file
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
many臺中daylight
|
||||
|
||||
aaa collee
|
18
b.js
Normal file
18
b.js
Normal file
|
@ -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*/
|
|
@ -1,4 +1,5 @@
|
|||
{
|
||||
"type": "commonjs",
|
||||
"name": "clo",
|
||||
"version": "0.0.1",
|
||||
"description": "a little typesetting engine in TypeScript",
|
||||
|
|
38
src/libclo/index.js
Normal file
38
src/libclo/index.js
Normal file
|
@ -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;
|
49
src/libclo/index.ts
Normal file
49
src/libclo/index.ts
Normal file
|
@ -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<string>;
|
||||
preprocessors : Array<Function>;
|
||||
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<this.preprocessors.length; i++){
|
||||
prepro = this.preprocessors[i](prepro);
|
||||
}
|
||||
// TODO
|
||||
console.log("test"+prepro);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
export let a = new Clo();
|
||||
export default a;
|
|
@ -31,7 +31,7 @@ export function tkTreeToSExp(t: tkTree): string{
|
|||
return str;
|
||||
}*/
|
||||
|
||||
type tkTree = string | tkTree[];
|
||||
export type tkTree = string | tkTree[];
|
||||
|
||||
enum TokenKind {
|
||||
Seperator, // ---
|
||||
|
@ -99,6 +99,10 @@ function applyParts(first: tkTree,
|
|||
return ["%clo", first , second[1]];
|
||||
}
|
||||
|
||||
function applyPartsWithoutImport(parsed: [Token<TokenKind>, tkTree]):tkTree {
|
||||
return ["%clo", "" , parsed[1]];
|
||||
}
|
||||
|
||||
|
||||
function applyComment(value: Token<TokenKind.Comment>): 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("\`","\\\`");
|
||||
|
|
Loading…
Reference in a new issue