@@ -357,21 +357,35 @@ import { jsPDF } from "../jspdf.js";
357
357
* @returns {number } width the width of the text/link
358
358
*/
359
359
jsPDFAPI . textWithLink = function ( text , x , y , options ) {
360
- var width = this . getTextWidth ( text ) ;
361
- var height = this . internal . getLineHeight ( ) / this . internal . scaleFactor ;
360
+ var totalLineWidth = this . getTextWidth ( text ) ;
361
+ var lineHeight = this . internal . getLineHeight ( ) / this . internal . scaleFactor ;
362
+ var linkHeight , linkWidth ;
363
+
364
+ // Checking if maxWidth option is passed to determine lineWidth and number of lines for each line
365
+ if ( options . maxWidth !== undefined ) {
366
+ var { maxWidth } = options ;
367
+ linkWidth = maxWidth ;
368
+ var numOfLines = this . splitTextToSize ( text , linkWidth ) . length ;
369
+ linkHeight = Math . ceil ( lineHeight * numOfLines ) ;
370
+ } else {
371
+ linkWidth = totalLineWidth ;
372
+ linkHeight = lineHeight ;
373
+ }
374
+
362
375
this . text ( text , x , y , options ) ;
376
+
363
377
//TODO We really need the text baseline height to do this correctly.
364
378
// Or ability to draw text on top, bottom, center, or baseline.
365
- y += height * 0.2 ;
379
+ y += lineHeight * 0.2 ;
366
380
//handle x position based on the align option
367
381
if ( options . align === "center" ) {
368
- x = x - width / 2 ; //since starting from center move the x position by half of text width
382
+ x = x - totalLineWidth / 2 ; //since starting from center move the x position by half of text width
369
383
}
370
384
if ( options . align === "right" ) {
371
- x = x - width ;
385
+ x = x - totalLineWidth ;
372
386
}
373
- this . link ( x , y - height , width , height , options ) ;
374
- return width ;
387
+ this . link ( x , y - lineHeight , linkWidth , linkHeight , options ) ;
388
+ return totalLineWidth ;
375
389
} ;
376
390
377
391
//TODO move into external library
0 commit comments