You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed a slight difference when measuring text with CanvasRenderingContext2D.measureText() and @pdf-lib/fontkit's Font.layout(). In the exemple below, the difference is very small, but it tends to increase with the size of the text.
importfontkitfrom"@pdf-lib/fontkit";constfontFamily="Inter";constfontSize=16;consttext="Lorem ipsum dolor sit amet";asyncfunctionfetchFont(url){constresponse=awaitfetch(url);constarrayBuffer=awaitresponse.arrayBuffer();returnnewUint8Array(arrayBuffer);}constfontBytes=awaitfetchFont("https://fonts.gstatic.com/s/inter/v11/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfAZ9hjp-Ek-_EeA.woff");constfontFace=newFontFace(fontFamily,fontBytes);window.document.fonts.add(fontFace);constcanvas=window.document.createElement("canvas");constctx=canvas.getContext("2d");ctx.font=`${fontSize}px ${fontFamily}`;constwidthFromCanvas=ctx.measureText(text).width;constfontkitFont=fontkit.create(fontBytes);const{ advanceWidth }=fontkitFont.layout(text);constwidthFromFontkit=(advanceWidth/fontkitFont.unitsPerEm)*16;console.log("Width from canvas:",widthFromCanvas);// Gives 205.38621520996094console.log("Width from @pdf-lib/fontkit:",widthFromFontkit);// 205.38636363636363
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I noticed a slight difference when measuring text with
CanvasRenderingContext2D.measureText()
and@pdf-lib/fontkit
'sFont.layout()
. In the exemple below, the difference is very small, but it tends to increase with the size of the text.CodeSandbox link
Any idea what can be the reason for this?
Beta Was this translation helpful? Give feedback.
All reactions