-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using pdfkit #1
Comments
@techtonik Thanks, this looks good, but I am not sure, how about rendering speed. Do you have any experience with speed? It looks like pdfkit is using its own rendering, that could be fast (faster than rendering into headless browser). |
No, I haven't measured performance. Was looking for alternative library without PDF in its name. |
You can use the library PDFKit uses internally: https://github.com/foliojs/fontkit Code for measuring string width would be as follows: var fontkit = require('fontkit');
var font = fontkit.openSync('/path/to/font.ttf');
function getStringWidth(str, useKerning) {
if (useKerning) {
return font.layout(text).positions.reduce((widthSum, glyphPosition) => widthSum + glyphPosition.xAdvance, 0);
}
return font.glyphsForString(str).reduce((widthSum, glyph) => widthSum + glyph.advanceWidth, 0));
} |
Thanks for your advice. Will look at this. |
It doesn't use PDF, it is the library for font loading only. I don't know how you would get the glyphs width in pixels exactly though, maybe you can get it from the pdfkit's implementation. |
Wow, sounds very promising! |
Make sure to checkout badges/shields#1379 for performance discussion. |
Maybe this will help to get width in different fonts. https://github.com/badges/shields/blob/105e383d93a4a333c2a7ee8038a492c8071f14a5/lib/measure-text.js
UPDATE (20180901): Update link to
measure-text.js
with archived copy. The code was replaced in badges/shields@cc9a6db853beThe text was updated successfully, but these errors were encountered: