fix .ttc import bug
This commit is contained in:
parent
a37fd632a7
commit
a59e167307
7 changed files with 1982 additions and 221 deletions
|
@ -28,4 +28,4 @@ License: MIT
|
|||
- 20231010: 初步完成tsit ê階段ê Parser`。
|
||||
- 20231012: clo->js converter successfully (maybe.)
|
||||
- 20231016:basic font guessing and `putText` function
|
||||
- issues : the CJK font can't be in .ttc format. (upstream-problem)
|
||||
- 20231023-24:fix .ttc bug.
|
2035
package-lock.json
generated
2035
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -23,6 +23,7 @@
|
|||
"@types/chai": "^4.3.5",
|
||||
"@types/mocha": "^10.0.1",
|
||||
"@types/node": "^20.8.4",
|
||||
"@types/pdfkit": "^0.13.1",
|
||||
"@typescript-eslint/eslint-plugin": "^6.5.0",
|
||||
"chai": "^4.3.8",
|
||||
"eslint": "^8.48.0",
|
||||
|
@ -37,12 +38,10 @@
|
|||
"typescript": "^5.2.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pdf-lib/fontkit": "^1.1.1",
|
||||
"harfbuzzjs": "^0.3.3",
|
||||
"js-tokens": "^8.0.2",
|
||||
"minimist": "^1.2.8",
|
||||
"npx": "^10.2.2",
|
||||
"pdf-lib": "^1.17.1",
|
||||
"pdfkit": "^0.13.0",
|
||||
"typescript-parsec": "^0.3.4"
|
||||
}
|
||||
}
|
||||
|
|
111
src/canva.ts
111
src/canva.ts
|
@ -1,7 +1,6 @@
|
|||
const { execSync } = require('child_process');
|
||||
import { PDFDocument, RGB, ColorTypes } from "pdf-lib";
|
||||
import { readFileSync, writeFileSync } from "fs";
|
||||
import fontkit from '@pdf-lib/fontkit';
|
||||
import "pdfkit";
|
||||
|
||||
|
||||
export interface CloCommand {
|
||||
|
@ -9,27 +8,28 @@ export interface CloCommand {
|
|||
args : TextStreamUnit[],
|
||||
}
|
||||
|
||||
export type TextStreamUnit = string | CloCommand;
|
||||
|
||||
export type TextStreamUnit = string | CloCommand;
|
||||
export type PDFDocument = PDFKit.PDFDocument;
|
||||
/**
|
||||
* a clo document
|
||||
*/
|
||||
export interface Clo{
|
||||
mainText : TextStreamUnit[],
|
||||
mainFontStyle? : FontStyle,
|
||||
PDFCanvas : PDFDocument,
|
||||
PDFCanvas : PDFDocument;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Font Style Interface
|
||||
* name : eg. "FreeSans"
|
||||
* family : eg. "FreeSans"
|
||||
* size : in px, not in pt.
|
||||
* textWeight : TextWeight.REGULAR ,etc
|
||||
* textWeight : TextStyle.ITALIC ,etc
|
||||
*/
|
||||
export interface FontStyle{
|
||||
name : string,
|
||||
family : string,
|
||||
size : number,
|
||||
textWeight : TextWeight,
|
||||
textStyle : TextStyle,
|
||||
|
@ -47,59 +47,41 @@ export enum TextStyle{
|
|||
OBLIQUE,
|
||||
};
|
||||
|
||||
export interface fontPathPSNamePair{
|
||||
path : string,
|
||||
psName : string,
|
||||
}
|
||||
|
||||
/**
|
||||
* guess the font path of a font style with fontconfig's commands
|
||||
* guess the font path and postscript name of a font style with fontconfig's commands
|
||||
* @param style the font style
|
||||
* @returns the font path in string, if found none or .ttc, return a empty string.
|
||||
* @returns pair of the font path and postscript name.
|
||||
*/
|
||||
export function fontStyleTofontPath(style : FontStyle) : string{
|
||||
export function fontStyleTofont(style : FontStyle) : fontPathPSNamePair{
|
||||
try {
|
||||
let fcMatchOut = execSync(
|
||||
`fc-match "${style.name}":${TextWeight[style.textWeight]}:`+
|
||||
`${TextStyle[style.textStyle]}`);
|
||||
|
||||
let fontFileName : string = fcMatchOut.toString().match(/^[^:]+/g)[0];
|
||||
let fcMatchCommand = `fc-match "${style.family}":${TextWeight[style.textWeight]}:`+
|
||||
`${TextStyle[style.textStyle]}` +` postscriptname file`;
|
||||
|
||||
let fcMatchOut = execSync(fcMatchCommand);
|
||||
let matched = fcMatchOut
|
||||
.toString()
|
||||
.match(/\:file=(.+):postscriptname=(.+)\n/);
|
||||
|
||||
let fontPath : string = matched[1];
|
||||
let psName : string = matched[2];
|
||||
|
||||
return {path: fontPath, psName : psName};
|
||||
|
||||
if (fontFileName.match(/[.]ttc$/g)){
|
||||
console.log("WARNING: the program doesn't support .ttc font format!\n"+
|
||||
"Font file name: "+
|
||||
fontFileName);
|
||||
return "";
|
||||
}
|
||||
|
||||
let fcListOut = execSync(
|
||||
`fc-list | grep ${fontFileName}`);
|
||||
let fontPath : string = fcListOut.toString().match(/^[^:]+/g)[0];
|
||||
return fontPath;
|
||||
} catch (error) {
|
||||
console.log("WARNING: You should install `fontconfig` to select the font.");
|
||||
return "";
|
||||
console.log(`WARNING: You should install "fontconfig" to select the font.
|
||||
Detail of the error:` + error);
|
||||
|
||||
return {path: "", psName : ""};
|
||||
}
|
||||
};
|
||||
|
||||
export function hexToRGB(hex : string) : RGB{
|
||||
let matched = hex.match(/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/);
|
||||
|
||||
var result : RGB ;
|
||||
if (!matched){
|
||||
console.log("WARNING: the hex color code is not valid. set to #000000")
|
||||
result = {
|
||||
type : ColorTypes.RGB,
|
||||
red: 0,
|
||||
green: 0,
|
||||
blue: 0,
|
||||
}
|
||||
}else{
|
||||
result = {
|
||||
type : ColorTypes.RGB,
|
||||
red: parseInt(matched[1], 16),
|
||||
green: parseInt(matched[2], 16),
|
||||
blue: parseInt(matched[3], 16)
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* put text in a clo canva.
|
||||
|
@ -113,29 +95,26 @@ export function hexToRGB(hex : string) : RGB{
|
|||
*/
|
||||
export async function putText(clo : Clo, str : string, sty : FontStyle,
|
||||
pageNo : number, x : number, y : number): Promise<Clo>{
|
||||
|
||||
clo.PDFCanvas.registerFontkit(fontkit);
|
||||
let canvaPage = clo.PDFCanvas.getPage(pageNo);
|
||||
|
||||
let fontInfo = fontStyleTofont(sty);
|
||||
|
||||
const fontBytes = readFileSync(fontStyleTofontPath(sty));
|
||||
const fontEmbed = await clo.PDFCanvas.embedFont(fontBytes);
|
||||
|
||||
var textColor : RGB;
|
||||
if (sty.color === undefined){
|
||||
textColor = hexToRGB("#000000");
|
||||
}else{
|
||||
textColor = hexToRGB(sty.color);
|
||||
if (fontInfo.path.match(/\.ttc$/g)){
|
||||
var middle = clo.PDFCanvas
|
||||
.font(fontInfo.path, fontInfo.psName)
|
||||
.fontSize(sty.size);}
|
||||
else{
|
||||
var middle = clo.PDFCanvas
|
||||
.font(fontInfo.path)
|
||||
.fontSize(sty.size);
|
||||
}
|
||||
|
||||
let drawTextOptions = {
|
||||
x : x,
|
||||
y : canvaPage.getHeight() - y,
|
||||
font : fontEmbed,
|
||||
size : sty.size,
|
||||
color : textColor};
|
||||
if (sty.color !== undefined){
|
||||
middle.fill(sty.color);
|
||||
}
|
||||
|
||||
middle.text(str, x, y);
|
||||
|
||||
canvaPage.drawText(str, drawTextOptions);
|
||||
clo.PDFCanvas = middle;
|
||||
|
||||
return clo;
|
||||
};
|
|
@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|||
exports.pdfGenerate = void 0;
|
||||
const fs_1 = require("fs");
|
||||
const pdf_lib_1 = require("pdf-lib");
|
||||
var fontkit = require('@pdf-lib/fontkit');
|
||||
var fontkit = require('pdf-fontkit');
|
||||
function pdfGenerate() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const pdfDoc = yield pdf_lib_1.PDFDocument.create();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { readFileSync, writeFileSync } from "fs";
|
||||
import { PDFDocument } from "pdf-lib";
|
||||
var fontkit = require('@pdf-lib/fontkit');
|
||||
var fontkit = require('pdf-fontkit');
|
||||
|
||||
export async function pdfGenerate(){
|
||||
|
||||
|
|
|
@ -1,35 +1,51 @@
|
|||
import * as canva from "../src/canva.js";
|
||||
import { PDFDocument } from "pdf-lib";
|
||||
var fontkit = require('@pdf-lib/fontkit');
|
||||
import {writeFileSync} from 'fs';
|
||||
import {createWriteStream} from 'fs';
|
||||
import PDFDocument from 'pdfkit';
|
||||
|
||||
let hanziFont = {
|
||||
name : "思源黑體",
|
||||
family : "Noto Sans CJK TC",
|
||||
size : 12,
|
||||
textWeight : canva.TextWeight.REGULAR,
|
||||
textStyle : canva.TextStyle.ITALIC,
|
||||
}
|
||||
|
||||
let romanFont = {
|
||||
family : "FreeSans",
|
||||
size : 15,
|
||||
textWeight : canva.TextWeight.BOLD,
|
||||
textStyle : canva.TextStyle.ITALIC,
|
||||
}
|
||||
|
||||
let arabicFont = {
|
||||
family : "noto sans arabic",
|
||||
size : 16,
|
||||
textWeight : canva.TextWeight.REGULAR,
|
||||
textStyle : canva.TextStyle.NORMAL,
|
||||
}
|
||||
|
||||
|
||||
|
||||
async function foo (){
|
||||
|
||||
let c = await PDFDocument.create();
|
||||
const doc = new PDFDocument();
|
||||
|
||||
let clo = await {
|
||||
mainText : ["123"],
|
||||
mainFontStyle : hanziFont,
|
||||
PDFCanvas : c,
|
||||
|
||||
}
|
||||
let clo = await {
|
||||
mainText : ["123 一隻貓跑過來"],
|
||||
mainFontStyle : hanziFont,
|
||||
PDFCanvas : doc,
|
||||
|
||||
clo.PDFCanvas.registerFontkit(fontkit);
|
||||
const page = clo.PDFCanvas.addPage();
|
||||
}
|
||||
clo.PDFCanvas.pipe(createWriteStream('/tmp/output.pdf'));
|
||||
|
||||
await canva.putText(clo, clo.mainText[0],hanziFont, 0, 100, 200);
|
||||
|
||||
const pdfBytes = await clo.PDFCanvas.save();
|
||||
|
||||
writeFileSync('/tmp/test.pdf', pdfBytes);
|
||||
await canva.putText(clo, clo.mainText[0],hanziFont, 0, 100, 200);
|
||||
await canva.putText(clo, "ag téastáil" ,romanFont, 0, 100, 300);
|
||||
await canva.putText(clo, "اَلْعَرَبِيَّةُ" ,arabicFont, 0, 100, 350);
|
||||
|
||||
|
||||
doc.end();
|
||||
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue