"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
-exports.a = exports.Clo = void 0;
+exports.Clo = exports.calculateTextWidthHeight = exports.hyphenTkTree = exports.filterEmptyString = exports.spacesToBreakpoint = exports.hyphenForClo = exports.splitCJKV = exports.twoReturnsToNewline = exports.ptToPx = exports.cjkvRegexPattern = exports.cjkvBlocksInRegex = exports.defaultFrameStyle = exports.defaultTextStyle = exports.A4_IN_PX = exports.Direction = void 0;
const canva_1 = require("../canva");
+const jsdom_1 = require("jsdom");
/**
* TYPES
*/
Direction[Direction["RTL"] = 1] = "RTL";
Direction[Direction["TTB"] = 2] = "TTB";
Direction[Direction["BTT"] = 3] = "BTT";
-})(Direction || (Direction = {}));
+})(Direction || (exports.Direction = Direction = {}));
/**
* DEFAULT CONST PART
*/
-const A4_IN_PX = { "width": 793.7,
+exports.A4_IN_PX = { "width": 793.7,
"height": 1122.5 };
-const defaultTextStyle = {
- family: "FreeSans",
- size: 12,
+exports.defaultTextStyle = {
+ family: "FreeSerif",
+ size: ptToPx(12),
textWeight: canva_1.TextWeight.REGULAR,
- textStyle: canva_1.TextStyle.ITALIC,
+ fontStyle: canva_1.FontStyle.ITALIC,
};
-const defaultFrameStyle = {
+exports.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,
+ textStyle: exports.defaultTextStyle,
+ x: exports.A4_IN_PX.width * 0.10,
+ y: exports.A4_IN_PX.height * 0.10,
+ width: exports.A4_IN_PX.width * 0.80,
+ height: exports.A4_IN_PX.height * 0.80,
content: null,
};
-const cjkvBlocksInRegex = ["Hani"];
-const cjkvRegexPattern = new RegExp("((?:" +
- cjkvBlocksInRegex.map((x) => "\\p{Script_Extensions=" + x + "}").join("|") + ")+)", "gu");
+/**
+ * definition for cjk scripts
+ * - Hani : Han Character
+ * - Hang : Hangul
+ * - Bopo : Bopomofo
+ * - Kana : Katakana
+ * - Hira : Hiragana
+*/
+exports.cjkvBlocksInRegex = ["Hani", "Hang", "Bopo", "Kana", "Hira"];
+exports.cjkvRegexPattern = new RegExp("((?:" +
+ exports.cjkvBlocksInRegex.map((x) => "\\p{Script_Extensions=" + x + "}").join("|") + ")+)", "gu");
/**
* FUNCTION PART
*/
* @returns the corresponding px value
*/
function ptToPx(pt) {
- return pt * 4 / 3.0;
+ return pt * 4.0 / 3.0;
}
+exports.ptToPx = ptToPx;
/**
* REGISTER PART
*/
+/**
+ * convert '\n\n' to newline command ["nl"]
+ * @param arr the input `tkTree`
+ * @param clo the `Clo` object
+ * @returns the input tktree
+ */
+function twoReturnsToNewline(arr, clo) {
+ var middle = [];
+ for (let i = 0; i < arr.length; i++) {
+ var item = arr[i];
+ if (!Array.isArray(item)) {
+ middle = middle.concat(item.split(/(\n\n)/g));
+ }
+ else {
+ middle.push(item);
+ }
+ }
+ var result = [];
+ 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`
+ }
+ else {
+ result.push(middle[j]);
+ }
+ }
+ return result;
+}
+exports.twoReturnsToNewline = twoReturnsToNewline;
/**
* split CJKV and non-CJKV
*
* @param arr : input tkTree
- * @returns
+ * @returns a splitted tkTree (by CJK and NonCJK)
+ * - Examples:
+ * ```
+ * [`many臺中daylight`] => [`many`, `臺中`, `dahylight`]
+ * ```
*/
-function splitCJKV(arr) {
- console.log(arr);
+function splitCJKV(arr, clo) {
var result = [];
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));
+ result = result.concat(item.split(exports.cjkvRegexPattern));
}
else {
result.push(item);
}
}
- console.log(result);
return result;
}
+exports.splitCJKV = splitCJKV;
+/**
+ * hyphenation for a clo document
+ * @param arr the array for a `tkTree`
+ * @param clo the Clo object
+ */
+function hyphenForClo(arr, clo) {
+ let hyphenLanguage = clo.attrs["hyphenLanguage"];
+ let res = hyphenTkTree(arr, hyphenLanguage);
+ return res;
+}
+exports.hyphenForClo = hyphenForClo;
+/**
+ * convert spaces to Breakpoint
+ * \s+ => ["bp" [\s+] ""]
+ * @param arr the tkTree input text stream
+ * @param clo the Clo object
+ * @returns the converted object
+ */
+function spacesToBreakpoint(arr, clo) {
+ let spacePattern = /^([ \t]+)$/g;
+ var result = [];
+ 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`
+ }
+ else {
+ result.push(item);
+ }
+ }
+ return result;
+}
+exports.spacesToBreakpoint = spacesToBreakpoint;
+/**
+ * remove all the `` (empty string) in the arr
+ * @param arr the tkTree to be filtered
+ * @param clo the Clo file
+ */
+function filterEmptyString(arr, clo) {
+ if (Array.isArray(arr)) {
+ arr.filter((x) => { return x != ``; });
+ }
+ return arr;
+}
+exports.filterEmptyString = filterEmptyString;
+/**
+ * OTHER FUNCTIONS
+ */
+/**
+ * hyphenate for a tkTree
+ * - hyphenation => ["bp", "", "-"]
+ * @param arr the tkTree array
+ * @param lang ISO 639 code for the language
+ */
+function hyphenTkTree(arr, lang) {
+ // import corresponding hyphen language data and function
+ let hyphen = require("hyphen/" + lang);
+ let result = [];
+ for (let i = 0; i < arr.length; i++) {
+ let element = arr[i];
+ let splitter = "分"; // a CJKV
+ if (!Array.isArray(element)) {
+ let hyphenatedElement = hyphen.hyphenateSync(element, { hyphenChar: splitter });
+ let hyphenatedSplitted = hyphenatedElement.split(splitter);
+ var newSplitted = [];
+ for (var j = 0; j < hyphenatedSplitted.length - 1; j++) {
+ newSplitted.push(hyphenatedSplitted[j]);
+ // "bp" for breakpoint
+ newSplitted.push(["bp", "", "-"]); //insert a breakable point (bp) mark
+ }
+ newSplitted.push(hyphenatedSplitted[hyphenatedSplitted.length - 1]);
+ result = result.concat(newSplitted);
+ }
+ else {
+ result.push(element);
+ }
+ }
+ return result;
+}
+exports.hyphenTkTree = hyphenTkTree;
+/**
+ * calculate the text width and Height with a given `TextStyle`
+ * @param preprocessed
+ * @param defaultFontStyle
+ */
+function calculateTextWidthHeight(preprocessed, style) {
+ var dom = new jsdom_1.JSDOM(`<!DOCTYPE html><html><head></head>
+ <body><canvas id="canvas"></canvas></body></html>`);
+ try {
+ let canvas = dom.window.document.getElementById("canvas");
+ console.log(canvas);
+ /*if (!(canvas instanceof HTMLElement)){
+ throw new Error('the <canvas="canvas"> in the jsdom\'s DOM is not found.');
+
+ }*/
+ let context = canvas.getContext("2d");
+ console.log(context);
+ if (context == null) {
+ throw new Error('`canvas.getContext("2d");` can\'t be executed.');
+ }
+ context.font = `normal normal 10pt ${style.family}`;
+ console.log(context.font);
+ let txt = `Hello john`;
+ console.log(txt);
+ let measured = context.measureText(txt);
+ let width = measured.width;
+ let height = measured.actualBoundingBoxAscent;
+ let depth = measured.actualBoundingBoxDescent;
+ console.log("width: " + width);
+ console.log("height: " + height);
+ console.log("depth: " + depth);
+ }
+ catch (error) {
+ console.log("Exception " + error);
+ }
+}
+exports.calculateTextWidthHeight = calculateTextWidthHeight;
+/**
+ * whole document-representing class
+ */
class Clo {
constructor() {
this.preprocessors = [];
this.mainStream = [];
- this.attributes = { "page": A4_IN_PX };
+ this.attrs = {
+ "page": exports.A4_IN_PX,
+ "defaultFrameStyle": exports.defaultFrameStyle,
+ "hyphenLanguage": 'en' // hyphenated in the language (in ISO 639)
+ };
// register the precessor functions
this.preprocessorRegister(splitCJKV);
+ this.preprocessorRegister(hyphenForClo);
+ this.preprocessorRegister(twoReturnsToNewline);
+ this.preprocessorRegister(spacesToBreakpoint);
+ this.preprocessorRegister(filterEmptyString);
}
setAttr(attr, val) {
- Object.assign(this.attributes, attr, val);
+ Object.assign(this.attrs, attr, val);
}
getAttr(attr) {
- if (Object.keys(this.attributes).length === 0) {
- return this.attributes[attr];
+ if (Object.keys(this.attrs).length === 0) {
+ return this.attrs[attr];
}
else {
return undefined;
}
generatePdf() {
// preprocessed
- var prepro = this.mainStream;
+ var preprocessed = this.mainStream;
for (var i = 0; i < this.preprocessors.length; i++) {
- prepro = this.preprocessors[i](prepro);
+ preprocessed = this.preprocessors[i](preprocessed, this);
}
+ // generate the width and height of the stream
+ let defaultFontStyle = this.attrs["defaultFrameStyle"].textStyle;
+ calculateTextWidthHeight(preprocessed, defaultFontStyle);
// TODO
- console.log("test" + prepro);
+ console.log(preprocessed);
}
}
exports.Clo = Clo;
-exports.a = new Clo();
-exports.default = exports.a;
+/*
+export let a = new Clo();
+export default a; */