Skip to content

Commit 7b875ba

Browse files
committed
case by case to get some char width
1 parent c715810 commit 7b875ba

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

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

+22-9
Original file line numberDiff line numberDiff line change
@@ -326,22 +326,35 @@ 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 * 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-
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+
}
343345
} else if (charCode > 59 && charCode < 91) {
344-
cWidth += fontSize * 0.75;
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+
}
345358
} else if (charCode > 47 && charCode < 58) {
346359
// number
347360
cWidth += fontSize * 0.55;

0 commit comments

Comments
 (0)