faj-cli
Version:
FAJ - A powerful CLI resume builder with AI enhancement and multi-format export
145 lines • 5.83 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CJKFontLoader = void 0;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const https = __importStar(require("node:https"));
const Logger_1 = require("../../utils/Logger");
class CJKFontLoader {
logger;
fontDir;
fontUrls = {
// Using smaller subset fonts that support common CJK characters
// These are web-optimized versions with reduced file size
'NotoSansSC-Regular': 'https://cdn.jsdelivr.net/gh/fonts-archive/NotoSansSC/NotoSansSC-Regular.otf',
'NotoSansSC-Bold': 'https://cdn.jsdelivr.net/gh/fonts-archive/NotoSansSC/NotoSansSC-Bold.otf'
};
constructor() {
this.logger = new Logger_1.Logger('CJKFontLoader');
this.fontDir = path.join(process.cwd(), '.fonts');
}
async ensureFonts() {
// Ensure font directory exists
try {
await fs.access(this.fontDir);
}
catch {
await fs.mkdir(this.fontDir, { recursive: true });
}
// Check for cached fonts first
const regularPath = path.join(this.fontDir, 'NotoSansSC-Regular.otf');
const boldPath = path.join(this.fontDir, 'NotoSansSC-Bold.otf');
let regularFont;
let boldFont;
try {
// Try to load cached fonts
regularFont = await fs.readFile(regularPath);
boldFont = await fs.readFile(boldPath);
this.logger.info('Loaded cached CJK fonts');
}
catch {
// Download fonts if not cached
this.logger.info('Downloading CJK fonts...');
try {
regularFont = await this.downloadFont(this.fontUrls['NotoSansSC-Regular']);
boldFont = await this.downloadFont(this.fontUrls['NotoSansSC-Bold']);
// Cache the fonts
await fs.writeFile(regularPath, regularFont);
await fs.writeFile(boldPath, boldFont);
this.logger.success('CJK fonts downloaded and cached');
}
catch (error) {
this.logger.warn('Failed to download CJK fonts, using fallback');
// Return empty buffers - the PDF generator will fall back to standard fonts
return { regular: Buffer.from([]), bold: Buffer.from([]) };
}
}
return { regular: regularFont, bold: boldFont };
}
downloadFont(url) {
return new Promise((resolve, reject) => {
const chunks = [];
https.get(url, (response) => {
// Handle redirects
if (response.statusCode === 301 || response.statusCode === 302) {
const redirectUrl = response.headers.location;
if (redirectUrl) {
this.downloadFont(redirectUrl).then(resolve).catch(reject);
return;
}
}
if (response.statusCode !== 200) {
reject(new Error(`Failed to download font: ${response.statusCode}`));
return;
}
response.on('data', (chunk) => {
chunks.push(chunk);
});
response.on('end', () => {
resolve(Buffer.concat(chunks));
});
response.on('error', reject);
}).on('error', reject);
});
}
async getSystemFontPath() {
// Try to find system fonts that support CJK
const possiblePaths = [
// macOS
'/System/Library/Fonts/PingFang.ttc',
'/Library/Fonts/Arial Unicode.ttf',
// Linux
'/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc',
'/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf',
// Windows (WSL)
'/mnt/c/Windows/Fonts/msyh.ttc',
'/mnt/c/Windows/Fonts/simsun.ttc'
];
for (const fontPath of possiblePaths) {
try {
await fs.access(fontPath);
this.logger.info(`Found system font: ${fontPath}`);
return fontPath;
}
catch {
// Continue checking
}
}
return null;
}
}
exports.CJKFontLoader = CJKFontLoader;
//# sourceMappingURL=CJKFontLoader.js.map
;