Skip to content

Commit 4f75807

Browse files
authored
Merge pull request #144 from tingxin/fix_watermark_bug2
Fix watermark bug2
2 parents 9830f69 + 7b875ba commit 4f75807

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

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

+32-5
Original file line numberDiff line numberDiff line change
@@ -326,32 +326,59 @@ export class WatermarkAction extends BaseImageAction {
326326
}
327327

328328
calculateTextSize(text: string, fontSize: number): WatermarkTextOpts {
329+
// any better way?
329330
let cWidth = 0;
330331
for (let v of text) {
331332
const charCode = v.charCodeAt(0);
332333
if (charCode > 256) {
333334
cWidth += fontSize;
334335
} else if (charCode > 97) {
335-
cWidth += fontSize / 2;
336+
// i, j,l
337+
if (charCode === 105 || charCode === 106 || charCode === 108) {
338+
cWidth += fontSize * 0.22;
339+
} else if (charCode === 102) {
340+
// f
341+
cWidth += fontSize * 0.28;
342+
} else {
343+
cWidth += fontSize * 0.55;
344+
}
345+
} else if (charCode > 59 && charCode < 91) {
346+
if (charCode === 87) {
347+
// char W is bigger than others
348+
cWidth += fontSize * 0.95;
349+
} else if (charCode === 77) {
350+
// char M is bigger than others
351+
cWidth += fontSize * 0.85;
352+
} else if (charCode === 73 || charCode === 74) {
353+
// I,J
354+
cWidth += fontSize * 0.6;
355+
} else {
356+
cWidth += fontSize * 0.75;
357+
}
358+
} else if (charCode > 47 && charCode < 58) {
359+
// number
360+
cWidth += fontSize * 0.55;
336361
} else {
337-
cWidth += fontSize * 0.8;
362+
// comma and other punctuation is smaller
363+
cWidth += fontSize * 0.25;
338364
}
339365
}
340366
return {
341367
width: Math.round(cWidth + margin),
342368
height: Math.round(fontSize * 1.2),
343369
};
344370
}
371+
372+
345373
textSvgStr(opt: WatermarkOpts, textOpt: WatermarkTextOpts, applyOpacity: boolean = true, shadow: number = 0): string {
346-
const xOffset = Math.round(textOpt.width / 2);
347374
const yOffset = Math.round(textOpt.height * 0.8);
348375
const color = `#${opt.color}`;
349376
const opacity = applyOpacity ? opt.t / 100 : 1;
350377
// https://gitlab.gnome.org/GNOME/librsvg/-/blob/main/FEATURES.md
351378
// https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/529
352379
// 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>
380+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${textOpt.width} ${textOpt.height}">
381+
<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>
355382
</svg>`;
356383
return svg;
357384
}

0 commit comments

Comments
 (0)