@@ -326,32 +326,59 @@ export class WatermarkAction extends BaseImageAction {
326
326
}
327
327
328
328
calculateTextSize ( text : string , fontSize : number ) : WatermarkTextOpts {
329
+ // any better way?
329
330
let cWidth = 0 ;
330
331
for ( let v of text ) {
331
332
const charCode = v . charCodeAt ( 0 ) ;
332
333
if ( charCode > 256 ) {
333
334
cWidth += fontSize ;
334
335
} 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 ;
336
361
} else {
337
- cWidth += fontSize * 0.8 ;
362
+ // comma and other punctuation is smaller
363
+ cWidth += fontSize * 0.25 ;
338
364
}
339
365
}
340
366
return {
341
367
width : Math . round ( cWidth + margin ) ,
342
368
height : Math . round ( fontSize * 1.2 ) ,
343
369
} ;
344
370
}
371
+
372
+
345
373
textSvgStr ( opt : WatermarkOpts , textOpt : WatermarkTextOpts , applyOpacity : boolean = true , shadow : number = 0 ) : string {
346
- const xOffset = Math . round ( textOpt . width / 2 ) ;
347
374
const yOffset = Math . round ( textOpt . height * 0.8 ) ;
348
375
const color = `#${ opt . color } ` ;
349
376
const opacity = applyOpacity ? opt . t / 100 : 1 ;
350
377
// https://gitlab.gnome.org/GNOME/librsvg/-/blob/main/FEATURES.md
351
378
// https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/529
352
379
// 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>
355
382
</svg>` ;
356
383
return svg ;
357
384
}
0 commit comments