javascript - Get the exact font of a rendered text in the browser, maybe with a browser extension -


this question has answer here:

i know can font-family value window.getcomputedstyle() that's not font used browser render. example, if given text contains (multi-lingual) text font family not carry, browser renders text partially system font.

if take @ built-in web developer tool, either in chrome or firefox, both have little area display (rendered fonts pane on chrome or fonts tab on firefox) exact fonts used. firefox, guess this code used , seems calling internal api.

i'm looking dom-compliant (or vendor-specific) way exact font javascript land or else. if means writing browser extension/add-on provide api/inject info/whatever in-page code access, that's worst case, acceptable.

you can hack : main idea compare size of inline elements given fonts. 1 should use complete font-family value, single font-family. here's a proof of concept work pretty well.

// @ fiddle full working code function createfonttester(fontfamily) {   var container = document.createelement('div');   var tester = document.createelement('div');    container.style.position = 'absolute';   container.style.overflow = 'auto';   container.style.visibility = 'hidden';    tester.style.fontfamily = fontfamily;   tester.style.display = 'inline';   tester.style.visibility = 'hidden';    // size should big enough make visual difference   tester.style.fontsize = '100px';     // reset , prevent line breaks   tester.style.fontweight = 'normal';   tester.style.fontstyle = 'normal';   tester.style.letterspacing = 'normal';   tester.style.lineheight = 'normal';   tester.style.whitespace = 'nowrap';    document.body.appendchild(container);   container.appendchild(tester);    return tester; } 

note fonts similar characters take same space. take helvetica , arial instance : character width same, height different used large font-size.

this method not bullet-proof work every font-families think of.

update : i've made this little library on github handle more use cases.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -