X-Git-Url: https://git.kianting.info/?a=blobdiff_plain;f=src%2Flibclo%2Findex.ts;h=d074e9c91ce127b4d03f560ae25a6f8c4c74b9aa;hb=714bbb4a64531268d17ca8f7ae60800ec2046a27;hp=917cf68afbb1f8061105241b729ee808e85ea543;hpb=c3dc58d74afa6b298d84bad90d63c027a32a954a;p=clo
diff --git a/src/libclo/index.ts b/src/libclo/index.ts
index 917cf68..d074e9c 100644
--- a/src/libclo/index.ts
+++ b/src/libclo/index.ts
@@ -1,7 +1,13 @@
-import { isKeyObject, isStringObject } from "util/types";
import {tkTree} from "../parser";
-import {FontStyle, TextStyle, TextWeight} from "../canva";
-import { JSDOM } from "jsdom";
+import {FontStyle, TextStyle, TextWeight, fontStyleTofont, fontPathPSNamePair} from "../canva";
+import * as fontkit from "fontkit";
+import * as breakLines from "./breakLines";
+const PDFDocument = require('pdfkit');
+import * as fs from "fs";
+import { Style } from "util";
+import { time } from "console";
+import {memfs} from "memfs";
+
/**
* TYPES
@@ -19,6 +25,31 @@ export enum Direction{
TTB,
BTT,
}
+/**
+ * Horizonal glue.
+ * - stretchFactor : the stretch factor in float
+ */
+export interface HGlue{
+ isHorizonalGlue : true,
+ stretchFactor: number
+}
+
+export interface VGlue{
+ isHorizonalGlue : false,
+ stretchFactor: number
+}
+
+export interface BreakPoint{
+ original : BoxesItem,
+ newLined : BoxesItem
+}
+
+/** BR is like html br */
+export interface BR extends BreakPoint{
+ isBR : true;
+}
+
+export type BoxesItem = HGlue | Box | BreakPoint | BoxesItem[] ;
/**
* frame box is a subclass of box
@@ -30,13 +61,21 @@ export interface FrameBox extends Box{
baseLineskip : number | null,
}
+export interface CharBox extends Box{
+ minX: number,
+ maxX: number,
+ minY: number,
+ maxY: number,
+
+}
+
/**
* a basic Box
- * - x :
- * - y :
+ * - x : pt
+ * - y : pt
* - textStyle :
* - direction :
- * - width :
+ * - width : x_advance pt
* - content :
*/
export interface Box{
@@ -57,7 +96,7 @@ export const A4_IN_PX = {"width" : 793.7,
"height" : 1122.5};
export const defaultTextStyle : TextStyle = {
- family : "FreeSerif",
+ family : "Noto Sans CJK TC",
size : ptToPx(12),
textWeight : TextWeight.REGULAR,
fontStyle : FontStyle.ITALIC,
@@ -68,10 +107,10 @@ export const defaultFrameStyle : FrameBox = {
direction : Direction.TTB,
baseLineskip : ptToPx(15),
textStyle : defaultTextStyle,
- x : A4_IN_PX.width * 0.10,
- y : A4_IN_PX.height * 0.10,
- width : A4_IN_PX.width * 0.80,
- height : A4_IN_PX.height * 0.80,
+ x : A4_IN_PX.width * 0.10 ,
+ y : A4_IN_PX.height * 0.10 ,
+ width : A4_IN_PX.width * 0.80 ,
+ height : A4_IN_PX.height * 0.80 ,
content : null,
};
@@ -106,7 +145,7 @@ export function ptToPx(pt : number) : number{
*/
/**
- * convert '\n\n' to newline command ["nl"]
+ * convert '\n\n' to new paragraph command ["br"]
* @param arr the input `tkTree`
* @param clo the `Clo` object
* @returns the input tktree
@@ -128,7 +167,7 @@ export function twoReturnsToNewline(arr : tkTree, clo : Clo): tkTree{
for (let j = 0; j < middle.length; j++){
var item = middle[j];
if (!Array.isArray(item) && item == "\n\n"){
- result.push(["nl"]); // push a newline command to the result `tkTree`
+ result.push(["br"]); // push a newline command to the result `tkTree`
}
else{
result.push(middle[j]);
@@ -188,7 +227,8 @@ export function spacesToBreakpoint(arr : tkTree, clo : Clo) : tkTree{
for (let i = 0; i < arr.length; i++){
var item = arr[i];
if (!Array.isArray(item) && item.match(spacePattern)){
- result.push([ 'bp', item, "" ]); // push a newline command to the result `tkTree`
+ // push a breakpoint command to the result `tkTree`
+ result.push([ 'bp', [["hglue", "0.1"], item] , "" ]);
}
else{
result.push(item);
@@ -252,54 +292,176 @@ export function hyphenTkTree(arr : tkTree, lang: string) : tkTree{
return result;
}
+
+
/**
* calculate the text width and Height with a given `TextStyle`
* @param preprocessed
* @param defaultFontStyle
*/
-export function calculateTextWidthHeight(preprocessed : tkTree, style : TextStyle): void {
- var dom = new JSDOM(`
- `);
+export async function calculateTextWidthHeight(element : tkTree, style : TextStyle): Promise {
+ var res = [];
+ var styleCache = {};
+ var fontCache = {};
- try {
- let canvas = dom.window.document.getElementById("canvas");
- console.log(canvas);
+ for (var i=0; istyleCache, fontCache);
+ styleCache = item[1];
+ fontCache = item[2];
+ res.push(item[0]);
+ }
- /*if (!(canvas instanceof HTMLElement)){
- throw new Error('the