jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

getTextDimensions return wrong measure when blanck spaces inside text ...

Open themacboy opened this issue 5 years ago • 10 comments
trafficstars

Hello,

I have noticed that when Im using getTextDimensions () to know final size of a text, the returnin result is wrong if your text contains "blank spaces".

When you use plain text without "blank spaces" width measure is usualy a little bit higger than real size.

But if you use "blank spaces" then witdh measure is always lower thab reak size.

that make that you miss positioning next text, and they ovelap.

themacboy avatar Feb 20 '20 10:02 themacboy

more "blank spaces", more accumaltive error.

And probably it happends with other special characters too, like " · ", "-", "(", ....

themacboy avatar Feb 20 '20 10:02 themacboy

could you please post an example which shows the incident in live preview?

bwl21 avatar Feb 20 '20 10:02 bwl21

ohhh! Finally found how to reproduce the error.

It happends after using text with multiple lines and justify alignment. After that getTextDimension return worng values in next calls if they use special characters or blank spaces.

Please check that code to reproduce the error:

themacboy avatar Feb 21 '20 18:02 themacboy

`

<head>
	<meta charset="UTF-8">
	<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

	<meta name="description" content="jsPDF gettextDimension test"/>
	<title>jsPDF gettextDimension test</title>
	<script src="jspdf.min.js"></script>
	<script src="test.js"></script>

</head>

<body>
</body>
`

`(function () { let doc = new jsPDF ({ orientation: 'portrait', unit: 'mm', format: "a4", });

doc.setFont("Helvetica", "normal");
doc.setFontSize (11);

let lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
let text1 = "Hello beautiful world and all your little citizens. ";
let text2 = "Hellobeautifulworldandallyourlittlecitizens. ";
let final_text = "My name is Oscar !";
	
let text_1_width = doc.getTextDimensions (text1);
let text_2_width = doc.getTextDimensions (text2);

console.log (text_1_width.w)
console.log (text_2_width.w)

doc.text (lorem, 20, 20, {align: "justify", lineHeightFactor: 1.15, maxWidth: doc.internal.pageSize.width - (20 * 2)  });


doc.text (text1, 20, 60, {align: "left", lineHeightFactor: 1.15, maxWidth: doc.internal.pageSize.width - (20 * 2)  });
doc.text (final_text, (20 + text_1_width.w), 60, { align: "left", lineHeightFactor: 1.15, maxWidth: (doc.internal.pageSize.width - (20 * 2)) });

doc.text (text2, 20, 80, {align: "left", lineHeightFactor: 1.15, maxWidth: doc.internal.pageSize.width - (20 * 2)  });
doc.text (final_text, (20 + text_2_width.w), 80, { align: "left", lineHeightFactor: 1.15, maxWidth: (doc.internal.pageSize.width - (20 * 2)) });
doc.save('dataurlnewwindow.pdf');

})()`

themacboy avatar Feb 21 '20 18:02 themacboy

Here a screen shot of final result and the overlap problem:

imatge

themacboy avatar Feb 21 '20 18:02 themacboy

Here a real example of how it make fail my warper function to manage X,Y position.

imatge

themacboy avatar Feb 21 '20 18:02 themacboy

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

github-actions[bot] avatar Jul 03 '20 01:07 github-actions[bot]

i supose the problem is still alive.

Im sorry but im not skilled to give help to correct it, and think this a really important function to imprve your pdf designs.

I hope someone can check it.

themacboy avatar Jul 08 '20 03:07 themacboy

save issue for me i use chinese font render the pdf file. but the getTextWidth and getTextDimensions api return me a wrong result.

MarioJames avatar Mar 04 '22 09:03 MarioJames

Unfortunately, I'll avoid using this lib because of this bug. Honestly, library needs a standard way of measuring any kind of characters and get rid of language-specific parsers like the one for Arabic.

I'd suggest that library uses opentype.js

CatchABus avatar Nov 18 '23 15:11 CatchABus