PDA

View Full Version : unit conversion (font pt to opengl units)



noeska
06-10-2009, 05:21 PM
How would i convert the size of a font that is give in pt ot opengl unit. I guess i have to use px as an intbetween step. Thanks for your answers in advance.

User137
07-10-2009, 05:55 PM
In ortho mode 1 unit is 1 pixel, but pixels start from -0.5 to 0.5.

noeska
07-10-2009, 06:04 PM
This is not enterely true i think as i specify a window of 1024x768 and yet specify 640x480 in glortho a pixel would not be equals to an opengl unit in that case.

on the delphi forum i got the answer that 1pt = 1/72 inch. Together with some more researche i made up the following:

e.g if i specyify 640x480 in glortho mode and my real screen size is 1024x768. How do i get the unit size? Posible Solution: divide real screensize with glortho screensize? Does that give pixels per opengl unit?

but a third size comes into play EM . e.g. a font is stored as units per em and 1 em being the current font size ?
a ttf font is supposed to have 2048 units per em. http://www.w3.org/TR/SVG/fonts.html

Using google i found the following table:
http://sureshjain.wordpress.com/2007/07/06/53/
stating that:
12pt = 16px = 1em = 100%
and 12/72 gives about 0,16666 so that's 16px.

Now how do get from the 2048 unit per em to a font that is drawn at the correct size.

768/480 give 1,6 so an opengl unit would be 1,6 pixel?
assuming i want to draw a font at 12pt i would need to draw something at 16px heigh.
16 / 1,6 states to me i should draw something at 10 opengl units heigh.
i now assume that 10 opengl units is 1em and thus equals the 2048 units per em. So 2048 = 10.
Now i can calculate from a font point to opengl point.

I should try to write some code for this. To see if my theory works in practice.

Let me know if the above makes sense or not. Thanks for your answers in advance.

User137
07-10-2009, 07:59 PM
If you want to make pixel-perfect text which i assume, your OpenGL window must be same resolution as Ortho view. If you try draw 800x600 image on 1024x768 it gets stretched and you have to either scale some pixels to double size or draw them with linear blending.

I did tests with this before and took screenshots etc to further prove that 1 pixel = 1 unit. I did a 256x128 sized texture and window with same size, draw in ortho to 0,0 and it fills exactly the screen.

DarkBow
08-10-2009, 11:25 AM
On first impression the reasoning seems to be logical to me. :)



Using google i found the following table:
http://sureshjain.wordpress.com/2007/07/06/53/
stating that:
12pt = 16px = 1em = 100%
and 12/72 gives about 0,16666 so that's 16px.


Anyway, I am curious about if the rounding applied is the "correct" one. Usually (but not always) when you round a number in the IT world the rule applied says a fraction of:

- 0,5 or more goes to 1
- less than 0,5 goes to 0

but in this example the fractional part is cut off. ???

noeska
11-10-2009, 10:12 AM
Until i hit a wall i dont care about the calculation being 100% correct. ;)

Using 1000 for the units per em, and asuming all screens dpi are 72, and asuming 1px is 1unit in opengl. I can now draw some fonts at what seems to be the correct size.

To finetune things i need to be certaint that the font saving routine makes the font 1000 units per em.

Need to find a way to read out screen dpi and pass it the glvg font class.
In the case of glortho and real screen size are not equal to each other i have to pass that info also to the glvg font class.

User137
12-10-2009, 05:42 AM
Do you need dpi in your calculations really? :) As far as i know everything in computer graphics can be done with just pixels, where OpenGL is also directly plotting via graphics card. There is no real meaning to the term i can think of...