It was previously explained here (http://stackoverflow.com/questions/118241/calculate-text-width-with-javascript/21015393#21015393) how to compute a text string length in pixels, with a solid reference to http://jsfiddle.net/eNzjZ/34/.
In the jsFiddle example a font
parameter string is passed to getTextWidth
. This parameter string includes the font size in pixels. The returned result in pixels is logged to the console.
Assuming that I want to convert the result into another units (em, pt…), I need retrieve from the parameter string the original font size in pixels in order to make a conversion. Is there a way to do it from within getTextWidth
using a Canvas property ?
In other words, how can I straightforwardly retrieve or extract the font size from the context.font property?
EDITING : MORE CLUES
typeof (context.font)
returnq a string including the correct font-size value. The topic of parsing a CSS string in shorthand format has been dealt with here (How to parse CSS font shorthand format). But I am so far unable to extract a specific value from the string without implementing a parse algorithm of my own… There must be a fast lane.