Skip to content

Commit c715810

Browse files
committed
fix text size error
1 parent 9830f69 commit c715810

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

source/new-image-handler/src/processor/image/watermark.ts

+19-5
Original file line numberDiff line numberDiff line change
@@ -332,26 +332,40 @@ export class WatermarkAction extends BaseImageAction {
332332
if (charCode > 256) {
333333
cWidth += fontSize;
334334
} else if (charCode > 97) {
335-
cWidth += fontSize / 2;
335+
cWidth += fontSize * 0.55;
336+
} else if (charCode === 87) {
337+
// char W is bigger than others
338+
cWidth += fontSize * 0.95;
339+
} else if (charCode === 77) {
340+
// char M is bigger than others
341+
cWidth += fontSize * 0.85;
342+
343+
} else if (charCode > 59 && charCode < 91) {
344+
cWidth += fontSize * 0.75;
345+
} else if (charCode > 47 && charCode < 58) {
346+
// number
347+
cWidth += fontSize * 0.55;
336348
} else {
337-
cWidth += fontSize * 0.8;
349+
// comma and other punctuation is smaller
350+
cWidth += fontSize * 0.25;
338351
}
339352
}
340353
return {
341354
width: Math.round(cWidth + margin),
342355
height: Math.round(fontSize * 1.2),
343356
};
344357
}
358+
359+
345360
textSvgStr(opt: WatermarkOpts, textOpt: WatermarkTextOpts, applyOpacity: boolean = true, shadow: number = 0): string {
346-
const xOffset = Math.round(textOpt.width / 2);
347361
const yOffset = Math.round(textOpt.height * 0.8);
348362
const color = `#${opt.color}`;
349363
const opacity = applyOpacity ? opt.t / 100 : 1;
350364
// https://gitlab.gnome.org/GNOME/librsvg/-/blob/main/FEATURES.md
351365
// https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/529
352366
// https://github.com/lovell/sharp/issues/1490#issuecomment-1162760143
353-
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${textOpt.width} ${textOpt.height}" text-anchor="middle">
354-
<text filter="drop-shadow(rgba(0,0,0,${shadow}) 2px 0px 2px)" font-size="${opt.size}" x="${xOffset}" y="${yOffset}" fill="${color}" opacity="${opacity}" font-family="${opt.type}">${opt.text}</text>
367+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${textOpt.width} ${textOpt.height}">
368+
<text filter="drop-shadow(rgba(0,0,0,${shadow}) 2px 0px 2px)" font-size="${opt.size}" text-anchor="start" x="0" y="${yOffset}" fill="${color}" opacity="${opacity}" font-family="${opt.type}">${opt.text}</text>
355369
</svg>`;
356370
return svg;
357371
}

0 commit comments

Comments
 (0)