@@ -332,26 +332,40 @@ export class WatermarkAction extends BaseImageAction {
332
332
if ( charCode > 256 ) {
333
333
cWidth += fontSize ;
334
334
} 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 ;
336
348
} else {
337
- cWidth += fontSize * 0.8 ;
349
+ // comma and other punctuation is smaller
350
+ cWidth += fontSize * 0.25 ;
338
351
}
339
352
}
340
353
return {
341
354
width : Math . round ( cWidth + margin ) ,
342
355
height : Math . round ( fontSize * 1.2 ) ,
343
356
} ;
344
357
}
358
+
359
+
345
360
textSvgStr ( opt : WatermarkOpts , textOpt : WatermarkTextOpts , applyOpacity : boolean = true , shadow : number = 0 ) : string {
346
- const xOffset = Math . round ( textOpt . width / 2 ) ;
347
361
const yOffset = Math . round ( textOpt . height * 0.8 ) ;
348
362
const color = `#${ opt . color } ` ;
349
363
const opacity = applyOpacity ? opt . t / 100 : 1 ;
350
364
// https://gitlab.gnome.org/GNOME/librsvg/-/blob/main/FEATURES.md
351
365
// https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/529
352
366
// 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>
355
369
</svg>` ;
356
370
return svg ;
357
371
}
0 commit comments