add cjk splitter
This commit is contained in:
		
							parent
							
								
									d8b33fabf7
								
							
						
					
					
						commit
						796e0c20c7
					
				
					 3 changed files with 220 additions and 16 deletions
				
			
		| 
						 | 
					@ -29,6 +29,8 @@ License: MIT
 | 
				
			||||||
 - 20231012: clo->js converter successfully (maybe.)
 | 
					 - 20231012: clo->js converter successfully (maybe.)
 | 
				
			||||||
 - 20231016:basic font guessing and `putText` function
 | 
					 - 20231016:basic font guessing and `putText` function
 | 
				
			||||||
 - 20231023-24:fix .ttc bug.
 | 
					 - 20231023-24:fix .ttc bug.
 | 
				
			||||||
 | 
					 - 20231026-27 : clo basic interface, preprocessor of stream of text,
 | 
				
			||||||
 | 
					  add cjk-english splitter, etc.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 ## 之後的做法
 | 
					 ## 之後的做法
 | 
				
			||||||
  - 先做一個前處理註冊器,註冊下列的前處理
 | 
					  - 先做一個前處理註冊器,註冊下列的前處理
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,20 +1,102 @@
 | 
				
			||||||
"use strict";
 | 
					"use strict";
 | 
				
			||||||
Object.defineProperty(exports, "__esModule", { value: true });
 | 
					Object.defineProperty(exports, "__esModule", { value: true });
 | 
				
			||||||
exports.a = exports.Clo = void 0;
 | 
					exports.a = exports.Clo = void 0;
 | 
				
			||||||
function foo(arr) {
 | 
					const canva_1 = require("../canva");
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * TYPES
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * text direction
 | 
				
			||||||
 | 
					 * LTR - left to right
 | 
				
			||||||
 | 
					 * TTB - top to bottom
 | 
				
			||||||
 | 
					 * etc.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					var Direction;
 | 
				
			||||||
 | 
					(function (Direction) {
 | 
				
			||||||
 | 
					    Direction[Direction["LTR"] = 0] = "LTR";
 | 
				
			||||||
 | 
					    Direction[Direction["RTL"] = 1] = "RTL";
 | 
				
			||||||
 | 
					    Direction[Direction["TTB"] = 2] = "TTB";
 | 
				
			||||||
 | 
					    Direction[Direction["BTT"] = 3] = "BTT";
 | 
				
			||||||
 | 
					})(Direction || (Direction = {}));
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * DEFAULT CONST PART
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					const A4_IN_PX = { "width": 793.7,
 | 
				
			||||||
 | 
					    "height": 1122.5 };
 | 
				
			||||||
 | 
					const defaultTextStyle = {
 | 
				
			||||||
 | 
					    family: "FreeSans",
 | 
				
			||||||
 | 
					    size: 12,
 | 
				
			||||||
 | 
					    textWeight: canva_1.TextWeight.REGULAR,
 | 
				
			||||||
 | 
					    textStyle: canva_1.TextStyle.ITALIC,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					const defaultFrameStyle = {
 | 
				
			||||||
 | 
					    directionInsideLine: Direction.LTR,
 | 
				
			||||||
 | 
					    direction: Direction.TTB,
 | 
				
			||||||
 | 
					    baseLineskip: ptToPx(15),
 | 
				
			||||||
 | 
					    fontStyle: 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,
 | 
				
			||||||
 | 
					    content: null,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					const cjkvBlocksInRegex = ["Hani"];
 | 
				
			||||||
 | 
					const cjkvRegexPattern = new RegExp("((?:" +
 | 
				
			||||||
 | 
					    cjkvBlocksInRegex.map((x) => "\\p{Script_Extensions=" + x + "}").join("|") + ")+)", "gu");
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * FUNCTION PART
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * convert from ptToPx
 | 
				
			||||||
 | 
					 * @param pt pt size value
 | 
				
			||||||
 | 
					 * @returns the corresponding px value
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function ptToPx(pt) {
 | 
				
			||||||
 | 
					    return pt * 4 / 3.0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 *  REGISTER PART
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * split CJKV and non-CJKV
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @param arr : input tkTree
 | 
				
			||||||
 | 
					 * @returns
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function splitCJKV(arr) {
 | 
				
			||||||
 | 
					    console.log(arr);
 | 
				
			||||||
 | 
					    var result = [];
 | 
				
			||||||
    for (let i = 0; i < arr.length; i++) {
 | 
					    for (let i = 0; i < arr.length; i++) {
 | 
				
			||||||
 | 
					        var item = arr[i];
 | 
				
			||||||
 | 
					        if (!Array.isArray(item)) {
 | 
				
			||||||
 | 
					            console.log(item.split(cjkvRegexPattern));
 | 
				
			||||||
 | 
					            result = result.concat(item.split(cjkvRegexPattern));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    if (Array.isArray(arr)) {
 | 
					        else {
 | 
				
			||||||
        arr.push("balabala");
 | 
					            result.push(item);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    return arr;
 | 
					    }
 | 
				
			||||||
 | 
					    console.log(result);
 | 
				
			||||||
 | 
					    return result;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
class Clo {
 | 
					class Clo {
 | 
				
			||||||
    constructor() {
 | 
					    constructor() {
 | 
				
			||||||
        this.preprocessors = [];
 | 
					        this.preprocessors = [];
 | 
				
			||||||
        this.mainStream = [];
 | 
					        this.mainStream = [];
 | 
				
			||||||
        this.attributes = { "page": [793.7, 1122.5] };
 | 
					        this.attributes = { "page": A4_IN_PX };
 | 
				
			||||||
        this.preprocessorRegister(foo);
 | 
					        // register the precessor functions
 | 
				
			||||||
 | 
					        this.preprocessorRegister(splitCJKV);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    setAttr(attr, val) {
 | 
				
			||||||
 | 
					        Object.assign(this.attributes, attr, val);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    getAttr(attr) {
 | 
				
			||||||
 | 
					        if (Object.keys(this.attributes).length === 0) {
 | 
				
			||||||
 | 
					            return this.attributes[attr];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else {
 | 
				
			||||||
 | 
					            return undefined;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * register a function of preprocessor
 | 
					     * register a function of preprocessor
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,27 +1,147 @@
 | 
				
			||||||
 | 
					import { isKeyObject, isStringObject } from "util/types";
 | 
				
			||||||
import {tkTree} from "../parser";
 | 
					import {tkTree} from "../parser";
 | 
				
			||||||
 | 
					import {TextStyle, FontStyle, TextWeight} from "../canva";
 | 
				
			||||||
 | 
					import { isString } from "util";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * TYPES
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * text direction
 | 
				
			||||||
 | 
					 * LTR - left to right
 | 
				
			||||||
 | 
					 * TTB - top to bottom
 | 
				
			||||||
 | 
					 * etc.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					enum Direction{
 | 
				
			||||||
 | 
					    LTR,
 | 
				
			||||||
 | 
					    RTL,
 | 
				
			||||||
 | 
					    TTB,
 | 
				
			||||||
 | 
					    BTT,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * frame box is a subclass of box
 | 
				
			||||||
 | 
					 * - directionInsideLine : text direction inside a line
 | 
				
			||||||
 | 
					 * - baselineskip : the distance between baselines in px
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					interface FrameBox extends Box{
 | 
				
			||||||
 | 
					    directionInsideLine : Direction,
 | 
				
			||||||
 | 
					    baseLineskip : number | null,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * a basic Box
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					interface Box{
 | 
				
			||||||
 | 
					    x : number | null,
 | 
				
			||||||
 | 
					    y : number | null,
 | 
				
			||||||
 | 
					    fontStyle : FontStyle | null,
 | 
				
			||||||
 | 
					    direction : Direction,
 | 
				
			||||||
 | 
					    width : number,
 | 
				
			||||||
 | 
					    height : number,
 | 
				
			||||||
 | 
					    content : string | Box[] | null,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function foo(arr : tkTree): tkTree{
 | 
					/**
 | 
				
			||||||
 | 
					 * DEFAULT CONST PART
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					const A4_IN_PX = {"width" : 793.7,
 | 
				
			||||||
 | 
					                  "height" : 1122.5};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const defaultTextStyle : FontStyle = {
 | 
				
			||||||
 | 
					        family : "FreeSans",
 | 
				
			||||||
 | 
					        size : 12,
 | 
				
			||||||
 | 
					        textWeight : TextWeight.REGULAR,
 | 
				
			||||||
 | 
					        textStyle : TextStyle.ITALIC,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const defaultFrameStyle : FrameBox = {
 | 
				
			||||||
 | 
					    directionInsideLine : Direction.LTR,
 | 
				
			||||||
 | 
					    direction : Direction.TTB,
 | 
				
			||||||
 | 
					    baseLineskip : ptToPx(15),
 | 
				
			||||||
 | 
					    fontStyle : 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,
 | 
				
			||||||
 | 
					    content : null,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const cjkvBlocksInRegex = ["Hani"];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const cjkvRegexPattern = new RegExp("((?:" +
 | 
				
			||||||
 | 
					    cjkvBlocksInRegex.map((x)=>"\\p{Script_Extensions="+x+"}").join("|") + ")+)", "gu");
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * FUNCTION PART
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * convert from ptToPx
 | 
				
			||||||
 | 
					 * @param pt pt size value
 | 
				
			||||||
 | 
					 * @returns the corresponding px value
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function ptToPx(pt : number) : number{
 | 
				
			||||||
 | 
					    return pt * 4 / 3.0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 *  REGISTER PART
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * split CJKV and non-CJKV
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @param arr : input tkTree
 | 
				
			||||||
 | 
					 * @returns 
 | 
				
			||||||
 | 
					 */ 
 | 
				
			||||||
 | 
					function splitCJKV(arr : tkTree): tkTree{
 | 
				
			||||||
 | 
					    var result : tkTree = [];
 | 
				
			||||||
    for (let i = 0; i < arr.length; i++) {
 | 
					    for (let i = 0; i < arr.length; i++) {
 | 
				
			||||||
 | 
					        var item = arr[i];
 | 
				
			||||||
 | 
					        if (!Array.isArray(item)){
 | 
				
			||||||
 | 
					            console.log(item.split(cjkvRegexPattern));
 | 
				
			||||||
 | 
					            result = result.concat(item.split(cjkvRegexPattern));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else{
 | 
				
			||||||
 | 
					            result.push(item);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    return result;
 | 
				
			||||||
    if (Array.isArray(arr)){
 | 
					 | 
				
			||||||
        arr.push("balabala");
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return arr;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export class Clo{
 | 
					export class Clo{
 | 
				
			||||||
    mainStream : Array<string>;
 | 
					    mainStream : Array<string>;
 | 
				
			||||||
    preprocessors : Array<Function>;
 | 
					    preprocessors : Array<Function>;
 | 
				
			||||||
    attributes: object ; // a4 size(x,y)
 | 
					    attributes: {[index: string]:any} ; // a4 size(x,y)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    constructor(){
 | 
					    constructor(){
 | 
				
			||||||
        this.preprocessors = [];
 | 
					        this.preprocessors = [];
 | 
				
			||||||
        this.mainStream = [];
 | 
					        this.mainStream = [];
 | 
				
			||||||
        this.attributes = {"page" : [793.7, 1122.5]};
 | 
					        this.attributes = {"page" : A4_IN_PX};
 | 
				
			||||||
        this.preprocessorRegister(foo);
 | 
					
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // register the precessor functions
 | 
				
			||||||
 | 
					        this.preprocessorRegister(splitCJKV);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public setAttr(attr : string, val : any):void{
 | 
				
			||||||
 | 
					        Object.assign(this.attributes, attr, val);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public getAttr(attr:string) : any{
 | 
				
			||||||
 | 
					        if (Object.keys(this.attributes).length === 0){
 | 
				
			||||||
 | 
					            return this.attributes[attr];
 | 
				
			||||||
 | 
					        }else{
 | 
				
			||||||
 | 
					            return undefined;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue