Add font support to correctly display text in generated images for social media

Integrate DejaVu Sans font by updating Dockerfile, registering the font in imageProcessor.ts, and referencing it in canvas font styles to resolve text rendering issues on Facebook stories.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: ae4037a0-2a6f-4530-9bac-79b543286bda
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Event-Id: ded0e79e-eb39-4fe2-9aad-3cb0b8722e22
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/397bca8c-984f-43ff-841a-10897aeb8140/ae4037a0-2a6f-4530-9bac-79b543286bda/EMt8Koz
This commit is contained in:
michaelschal committed 2025-11-18 07:15:11 +00:00
1 parent f949640ad4
commit 0b70e00834
3 files changed
+14 -3

No files matched your search

+3 -1
View File
@@ -19,7 +19,9 @@ RUN apk add --no-cache \
giflib \ giflib \
pixman \ pixman \
vips-dev \ vips-dev \
pkgconfig pkgconfig \
ttf-dejavu \
fontconfig
# Copier les fichiers de configuration # Copier les fichiers de configuration
COPY package*.json ./ COPY package*.json ./
Binary file not shown.
+11 -2
View File
@@ -21,6 +21,15 @@ const CROP_FORMATS = {
instagramStory: { width: 1080, height: 1920 } as CropDimensions, instagramStory: { width: 1080, height: 1920 } as CropDimensions,
}; };
// Register DejaVu Sans font for proper character rendering
try {
const fontPath = path.join(process.cwd(), "server", "fonts", "DejaVuSans.ttf");
registerFont(fontPath, { family: 'DejaVu Sans' });
console.log('✓ Font DejaVu Sans enregistrée avec succès');
} catch (error) {
console.error('Erreur lors de l\'enregistrement de la police:', error);
}
export class ImageProcessor { export class ImageProcessor {
private uploadsDir: string; private uploadsDir: string;
@@ -150,7 +159,7 @@ export class ImageProcessor {
let totalTextHeight = 0; let totalTextHeight = 0;
while (fontSize >= MIN_FONT_SIZE) { while (fontSize >= MIN_FONT_SIZE) {
ctx.font = `bold ${fontSize}px Arial, sans-serif`; ctx.font = `bold ${fontSize}px "DejaVu Sans", Arial, sans-serif`;
lines = this.wrapText(ctx, text, maxTextWidth); lines = this.wrapText(ctx, text, maxTextWidth);
const lineHeight = fontSize * LINE_HEIGHT_MULTIPLIER; const lineHeight = fontSize * LINE_HEIGHT_MULTIPLIER;
@@ -165,7 +174,7 @@ export class ImageProcessor {
if (fontSize < MIN_FONT_SIZE) { if (fontSize < MIN_FONT_SIZE) {
fontSize = MIN_FONT_SIZE; fontSize = MIN_FONT_SIZE;
ctx.font = `bold ${fontSize}px Arial, sans-serif`; ctx.font = `bold ${fontSize}px "DejaVu Sans", Arial, sans-serif`;
lines = this.wrapText(ctx, text, maxTextWidth); lines = this.wrapText(ctx, text, maxTextWidth);
const lineHeight = fontSize * LINE_HEIGHT_MULTIPLIER; const lineHeight = fontSize * LINE_HEIGHT_MULTIPLIER;
totalTextHeight = lines.length * lineHeight; totalTextHeight = lines.length * lineHeight;