Making a Centering function for True Type Fonts

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Making a Centering function for True Type Fonts

Post by DaveUnit »

I'm using Freebasic and the Yagl library(not really necessary for you to know bu whatever. =P), and I want to know how to figure out how to center the text no matter the size of text.
With Old QB and using 8x8 fonts only it's easy to make a centering routine, but it gets complicated when using True Type Fonts.
I can't think of any formula for doing this. If you know the correlation between point size and dimensions in pixels I'd appreciate any help you can offer.
Thanks a lot.

Dave
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Yagl has a nifty function called YagGfxFont_getTextDimensions

You use it as such:
YagGfxFont_getTextDimensions( myFONT, myText$, width, height )

Width and height will now hold the size of that text in pixels, after that it's easy to work out the proper offset to place the text at
I have left this dump.
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

Thanks a lot Z!re, I'm still trying to memorize YAGL's routines so I don't know a lot of the functions.
I reference the sample programs and online documentation but I guess I missed that routine.
Anyway, thanks again Z!re. You sure know YAGL very well. When I heard that you and Nekrophidius were using YAGL for your games I knew YAGL must be great. :D

Later,

Dave
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

Okay... um... FBIDE is giving me some crap so I took a look at yaglwrapper.bi for reference and I see no yaglgfxfont_gettextdimensions...
I'm gonna check to see if there's a newer version of YAGL out.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

The latest is 0.0.7 and it has gettextdimensions

And ya, Yagl is a really cool library =)
I have left this dump.
Macric
Coder
Posts: 34
Joined: Fri Mar 25, 2005 11:11 pm
Location: Mexico

Post by Macric »

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

I have left this dump.
Macric
Coder
Posts: 34
Joined: Fri Mar 25, 2005 11:11 pm
Location: Mexico

Post by Macric »

i am imperfect but this is good
http://forum.qbasicnews.com/viewforum.php?f=22
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Macric wrote:i am imperfect but this is good
http://forum.qbasicnews.com/viewforum.php?f=22
I still dont see the relevance to this topic.
I have left this dump.
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

I don't see the relevance either...
Anyway, I got the newest version of Yagl, it has getTextDimensions so I should be good now.

Thanks again, Z!re.

Dave
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

DaveUnit wrote:I don't see the relevance either...
Anyway, I got the newest version of Yagl, it has getTextDimensions so I should be good now.

Thanks again, Z!re.

Dave
No problem =)
I have left this dump.
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

I feel a little like Dr. Frankenstein bringing this once dead topic back to life but I'm having one wee problem with yaggfxfont_gettextdimensions.

In addition to centering text, I made an option where the text can have a background color rather than being transparent. The background color takes up the correct height, but the width is off. There's got to be something I'm overlooking.

vvvv the important code is in bold vvvv

Code: Select all

    with text_textobjects(text_number)
        .object_name=object_name$
        .text=text$
        .x=x
        .y=y
        .text_color=text_color
        .transparency=transparency
        .font=font
        .active=-1
        [b].shade_color=shade_color
        yaggfxfont_gettextdimensions(.font,.text,width,height)
        .length=width
        .height=height[/b]
    end with
Any help is appreciated.

Dave
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

I cant see anything wrong..
I have left this dump.
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

Me either, lemme look somewhere else..

Code: Select all

yaglgfxdevice_solidbox(.x,.y,.x+.length,.y+.height,.shade_color,.transparency)
That's the only place anything involving text dimensions comes up, do you see anything wrong there?
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Nope... just by chance, try changing the width variable where you get the font dimensions.
I had some problems with variables named: W and H, where my program would crash..
Seems to be a random FB problem.. not sure though..

Changing the variable names to ww and hh, respectively, fixed the problem.
I have left this dump.
DaveUnit
Veteran
Posts: 72
Joined: Sat Oct 29, 2005 10:07 am

Post by DaveUnit »

Could it be because Width is an FB command? I thought about that but since yaglwrapper.bi had that paramater called width also I thought it must work still.

It must be because of that 'cuz changing width to anything else works perfectly.

Thanks for helping with another one of my stupid mistakes Z!re.

Dave
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Heh, no problem, and it's not a misstake.. it's FB screwing up..
I have left this dump.
Post Reply