-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfont-loader.js
More file actions
74 lines (61 loc) · 2.38 KB
/
font-loader.js
File metadata and controls
74 lines (61 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const https = require('https');
const fs = require('fs');
const path = require('path');
const FONTS_DIR = path.join(__dirname, 'fonts');
// Ensure fonts directory exists
if (!fs.existsSync(FONTS_DIR)) {
fs.mkdirSync(FONTS_DIR);
}
/**
* Downloads a Google Font if it doesn't already exist.
* @param {string} fontName - The name of the font (e.g., "Roboto").
* @param {string} weight - The weight (e.g., "400").
* @returns {Promise<boolean>} - Success status.
*/
async function downloadFont(fontName, weight = '400') {
if (!fontName) return true;
const fileName = `${fontName.replace(/\s+/g, '-')}-${weight}.ttf`;
const filePath = path.join(FONTS_DIR, fileName);
if (fs.existsSync(filePath)) {
return true; // Already cached
}
console.log(`Downloading font: ${fontName} (${weight})...`);
try {
const cssUrl = `https://fonts.googleapis.com/css2?family=${fontName.replace(/\s+/g, '+')}:wght@${weight}`;
const css = await fetchContent(cssUrl, {
'User-Agent': 'Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'
});
const fontUrlMatch = css.match(/src:\s*url\(([^)]+)\)/);
if (!fontUrlMatch) {
console.error(`Could not find font URL in CSS for ${fontName}`);
return false;
}
const fontUrl = fontUrlMatch[1].replace(/['"]/g, '');
const fontData = await fetchContentBuffer(fontUrl);
fs.writeFileSync(filePath, fontData);
console.log(`Successfully downloaded and cached: ${fileName}`);
return true;
} catch (err) {
console.error(`Failed to download font ${fontName}:`, err.message);
return false;
}
}
function fetchContent(url, headers = {}) {
return new Promise((resolve, reject) => {
https.get(url, { headers }, (res) => {
let data = '';
res.on('data', (chunk) => data += chunk);
res.on('end', () => resolve(data));
}).on('error', reject);
});
}
function fetchContentBuffer(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
const chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => resolve(Buffer.concat(chunks)));
}).on('error', reject);
});
}
module.exports = { downloadFont };