Spaces:
Sleeping
Sleeping
File size: 7,467 Bytes
84e1e28 |
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
const express = require("express");
const cors = require("cors");
const { createCanvas, registerFont } = require("canvas");
const path = require("path");
const fs = require("fs");
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.static("public"));
// Create fonts directory if it doesn't exist
const fontsDir = path.join(__dirname, "fonts");
if (!fs.existsSync(fontsDir)) {
fs.mkdirSync(fontsDir);
}
// Store available fonts and their variations
const availableFonts = new Map();
function initializeFonts() {
if (!fs.existsSync(fontsDir)) {
console.log(
"Created fonts directory. Please add font files (.ttf or .otf) to the fonts folder."
);
return;
}
const fontFiles = fs
.readdirSync(fontsDir)
.filter(
(file) =>
file.toLowerCase().endsWith(".ttf") ||
file.toLowerCase().endsWith(".otf")
);
fontFiles.forEach((file) => {
const fontPath = path.join(fontsDir, file);
const fontName = file.replace(/\.(ttf|otf)$/i, "");
// Parse font variations (Regular, Bold, Italic, etc.)
let weight = "normal";
let style = "normal";
const lowerFontName = fontName.toLowerCase();
if (lowerFontName.includes("bold")) weight = "bold";
if (lowerFontName.includes("light")) weight = "light";
if (lowerFontName.includes("medium")) weight = "medium";
if (lowerFontName.includes("italic")) style = "italic";
// Register font with canvas
registerFont(fontPath, {
family: fontName.split("-")[0], // Get base font name
weight,
style,
});
// Store font info
const familyName = fontName.split("-")[0];
if (!availableFonts.has(familyName)) {
availableFonts.set(familyName, []);
}
availableFonts.get(familyName).push({
fullName: fontName,
weight,
style,
});
});
console.log("Available font families:", Array.from(availableFonts.keys()));
}
// Initialize fonts
initializeFonts();
// Store requests history
let requestsHistory = [];
function generateQuoteImage(ctx, canvas, data) {
const {
text,
author,
bgColor,
barColor,
textColor,
authorColor,
quoteFontFamily,
quoteFontWeight,
quoteFontStyle,
authorFontFamily,
authorFontWeight,
authorFontStyle,
barWidth = 4,
} = data;
// Constants for layout
const margin = 80;
const quoteMarkSize = 120;
const padding = 30;
const lineHeight = 50;
// Clear canvas
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Draw bars
ctx.fillStyle = barColor;
ctx.fillRect(margin, margin, barWidth, canvas.height - margin * 2);
ctx.fillRect(
canvas.width - margin - barWidth,
margin,
barWidth,
canvas.height - margin * 2
);
// Set up quote font
ctx.fillStyle = barColor;
const quoteMarkFont = [];
if (quoteFontStyle === "italic") quoteMarkFont.push("italic");
quoteMarkFont.push(quoteFontWeight);
quoteMarkFont.push(`${quoteMarkSize}px`);
quoteMarkFont.push(`"${quoteFontFamily}"`);
ctx.font = quoteMarkFont.join(" ");
ctx.textBaseline = "top";
// Calculate quote mark metrics
const quoteMarkWidth = ctx.measureText('"').width;
const textStartX = margin + barWidth + padding + quoteMarkWidth + padding;
const textEndX = canvas.width - margin - barWidth - padding * 2;
const maxWidth = textEndX - textStartX;
// Set up quote text font
ctx.fillStyle = textColor;
const quoteFont = [];
if (quoteFontStyle === "italic") quoteFont.push("italic");
quoteFont.push(quoteFontWeight);
quoteFont.push("36px");
quoteFont.push(`"${quoteFontFamily}"`);
ctx.font = quoteFont.join(" ");
// Word wrap text
const words = text.split(" ");
const lines = [];
let currentLine = "";
for (let word of words) {
const testLine = currentLine + (currentLine ? " " : "") + word;
const metrics = ctx.measureText(testLine);
if (metrics.width > maxWidth) {
if (currentLine) {
lines.push(currentLine);
currentLine = word;
} else {
currentLine = word;
}
} else {
currentLine = testLine;
}
}
if (currentLine) {
lines.push(currentLine);
}
// Calculate total height of text block
const totalTextHeight = lines.length * lineHeight;
const authorHeight = 60; // Space reserved for author
const availableHeight = canvas.height - margin * 2;
// Calculate starting Y position to center text block
let startY =
margin + (availableHeight - (totalTextHeight + authorHeight)) / 2;
// Draw quote mark at the same vertical position as first line
ctx.fillStyle = barColor;
ctx.font = quoteMarkFont.join(" ");
ctx.fillText('"', margin + barWidth + padding, startY);
// Draw quote lines
ctx.fillStyle = textColor;
ctx.font = quoteFont.join(" ");
lines.forEach((line, index) => {
ctx.fillText(line.trim(), textStartX, startY + index * lineHeight);
});
// Draw author below the quote
ctx.fillStyle = authorColor;
const authorFont = [];
if (authorFontStyle === "italic") authorFont.push("italic");
authorFont.push(authorFontWeight);
authorFont.push("28px");
authorFont.push(`"${authorFontFamily}"`);
ctx.font = authorFont.join(" ");
// Ensure author doesn't overflow
let authorText = `- ${author}`;
let authorMetrics = ctx.measureText(authorText);
if (authorMetrics.width > maxWidth) {
const ellipsis = "...";
while (authorMetrics.width > maxWidth && author.length > 0) {
author = author.slice(0, -1);
authorText = `- ${author}${ellipsis}`;
authorMetrics = ctx.measureText(authorText);
}
}
// Position author text below quote with spacing
const authorY = startY + totalTextHeight + 40;
ctx.fillText(authorText, textStartX, authorY);
}
// API Endpoints
app.get("/api/fonts", (req, res) => {
const fontDetails = Array.from(availableFonts.entries()).map(
([family, variations]) => ({
family,
variations: variations.map((v) => ({
weight: v.weight,
style: v.style,
fullName: v.fullName,
})),
})
);
res.json(fontDetails);
});
app.post("/api/generate-quote", (req, res) => {
try {
const data = req.body;
// Validate fonts exist
if (!availableFonts.has(data.quoteFontFamily)) {
throw new Error("Selected quote font is not available");
}
if (!availableFonts.has(data.authorFontFamily)) {
throw new Error("Selected author font is not available");
}
// Store request
requestsHistory.unshift({
timestamp: new Date(),
request: data,
});
// Keep only last 10 requests
requestsHistory = requestsHistory.slice(0, 10);
// Create canvas
const canvas = createCanvas(1200, 675); // 16:9 ratio
const ctx = canvas.getContext("2d");
// Generate quote image
generateQuoteImage(ctx, canvas, data);
// Send response
res.json({
success: true,
imageUrl: canvas.toDataURL(),
timestamp: new Date().toISOString(),
});
} catch (error) {
console.error("Error generating quote:", error);
res.status(500).json({
success: false,
error: error.message,
});
}
});
app.get("/api/requests-history", (req, res) => {
res.json(requestsHistory);
});
// Start server
const PORT = process.env.PORT || 7860;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`View the application at http://localhost:${PORT}`);
});
|