Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.
I am able to parse out all of the other information I need about the text, but I can't figure out the correct way to get the font size.
I have extended CSCharacterParser, and overridden onCharacterFound.
Here is my current code in the onCharacterFound function to get the font size:
//1 glyph width is 1/72 of a pt. textstate.fontSize scales the font.
//the + .5 makes the cast a round to nearest integer operation
fontSize = (int)(glyphs.getWidth() / 72.0f * textState.fontSize + .5);
One problem with using glyphs.width() is that the value varies for each character, being larger for wide letters and smaller for thing letters. I am doing it this way because I can't figure out any other way to get the font size in pt. Can anyone help me figure this out?
fontSize = textState.fontSize
textState.fontSize represents the latest font size requested by the graphics stream
glyp width is expressed in 1/1000 text space unit (== 1/72000 pt)
But textState.fontSize is always 1.0. The fonts I am parsing should be around 8-12.
The code I posted initially gives nearly the correct font, but is always off by 1 or 2 pt.
Well, then this is by hazard. When you divide a character that will be between 500 and 1000 units wide by 72, you will get quite close to the range you're expecting. Nevertheless this is a fault.
textState.fontSIze is set in the method "textSetFontFont" - break on it to find out what your graphics stream sets. I bet your fontSize will be 1… (at least that's what Spock would suggest from your input).
But, there's hope. There are some clever creator applications that always set the font size to 1 and scale the text transformation up (which is in textState.transform). You can safely assume for this type of PDF that the scale is your font size.
Alright, thanks for the help.
How exactly would I scale the font size up with textState.transform? I'm familiar with transform matrices…. just not in how they relate to pdf text.
well, hoping that your transformation is a proportional scale only you could
fontSize = textState.fontSize * textState.transform.getScaleX();
If you have rotation and the like, dig for the math book…
Awesome, that did the trick. Thanks!
For the sake of robustness, how would I check if there has been rotation? There doesn't seem to be a rotation attribute in textState.transform.
Really, you should cover some ground here…
The most robust approach may be to transform a Vector of length 1 using the text transformation and then measure the new length using Pythagoras.
length = root(square(x') + square(y'))
This is the net transformation scale.