]>
git.kianting.info Git - clo/blob - src/canva.ts
e88d8fdf2f180a4265cabb4321dfb89aa4cf9fdc
1 const { execSync
} = require('child_process');
2 import { readFileSync
, writeFileSync
} from
"fs";
6 export interface CloCommand
{
8 args
: TextStreamUnit
[],
12 export type TextStreamUnit
= string | CloCommand
;
13 export type PDFDocument
= PDFKit
.PDFDocument
;
18 mainText
: TextStreamUnit
[],
19 mainFontStyle
? : FontStyle
,
20 PDFCanvas
: PDFDocument
;
25 * Font Style Interface
26 * family : eg. "FreeSans"
27 * size : in px, not in pt.
28 * textWeight : TextWeight.REGULAR ,etc
29 * textWeight : TextStyle.ITALIC ,etc
31 export interface FontStyle
{
34 textWeight
: TextWeight
,
35 textStyle
: TextStyle
,
39 export enum TextWeight
{
44 export enum TextStyle
{
50 export interface fontPathPSNamePair
{
56 * guess the font path and postscript name of a font style with fontconfig's commands
57 * @param style the font style
58 * @returns pair of the font path and postscript name.
60 export function fontStyleTofont(style
: FontStyle
) : fontPathPSNamePair
{
62 let fcMatchCommand
= `fc-match "${style.family}":${TextWeight[style.textWeight]}:`+
63 `${TextStyle[style.textStyle]}` +` postscriptname file`;
65 let fcMatchOut
= execSync(fcMatchCommand
);
66 let matched
= fcMatchOut
68 .match(/\
:file
=(.+):postscriptname
=(.+)\n/);
70 let fontPath
: string = matched
[1];
71 let psName
: string = matched
[2];
73 return {path
: fontPath
, psName
: psName
};
77 console
.log(`WARNING: You should install "fontconfig" to select the font.
78 Detail of the error:` + error
);
80 return {path
: "", psName
: ""};
87 * put text in a clo canva.
88 * @param clo : the clo object
89 * @param str input string
90 * @param sty input fontstyle
91 * @param PageNo : the input page, 0-indexed.
92 * @param x base x-point from left
93 * @param y base y-point from top
94 * @returns a new updated clo object
96 export async function putText(clo
: Clo
, str
: string, sty
: FontStyle
,
97 pageNo
: number, x
: number, y
: number): Promise
<Clo
>{
99 let fontInfo
= fontStyleTofont(sty
);
101 if (fontInfo
.path
.match(/\
.ttc$
/g
)){
102 var middle
= clo
.PDFCanvas
103 .font(fontInfo
.path
, fontInfo
.psName
)
104 .fontSize(sty
.size
);}
106 var middle
= clo
.PDFCanvas
111 if (sty
.color
!== undefined){
112 middle
.fill(sty
.color
);
115 middle
.text(str
, x
, y
);
117 clo
.PDFCanvas
= middle
;