X-Git-Url: https://git.kianting.info/?a=blobdiff_plain;f=src%2Flibclo%2Findex.ts;h=f48f448d9bcabebaeaba7d0b21a60923bf4baf8b;hb=9548c51651d010ec8fd8fbf0213a2d7c60e051ce;hp=917cf68afbb1f8061105241b729ee808e85ea543;hpb=c3dc58d74afa6b298d84bad90d63c027a32a954a;p=clo
diff --git a/src/libclo/index.ts b/src/libclo/index.ts
index 917cf68..f48f448 100644
--- a/src/libclo/index.ts
+++ b/src/libclo/index.ts
@@ -1,7 +1,12 @@
-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";
+
/**
* TYPES
@@ -20,6 +25,21 @@ export enum Direction{
BTT,
}
+/**
+ * Horizonal glue.
+ * - stretchFactor : the stretch factor in float
+ */
+export interface HGlue{
+ stretchFactor: number
+}
+
+export interface BreakPoint{
+ original : BoxesItem,
+ newLined : BoxesItem
+}
+
+export type BoxesItem = HGlue | Box | BreakPoint | BoxesItem[] ;
+
/**
* frame box is a subclass of box
* - directionInsideLine : text direction inside a line
@@ -30,13 +50,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{
@@ -68,10 +96,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,
};
@@ -188,7 +216,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,50 +281,124 @@ 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