Drawing fonts

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
matthewburk
Prole
Posts: 15
Joined: Fri Mar 05, 2010 6:56 am

Drawing fonts

Post by matthewburk »

Since this is my first time posting, let me just say I love love.

Now down to business. When drawing a font with graphics.print() x and y seem aren't top left, but seem to be the pen origin, so all of my text is not where i want it exactly. I couldn't find any font metrics api so that i could compenstate, is there a way to draw a font with x,y as top left?
User avatar
rhezalouis
Party member
Posts: 100
Joined: Mon Dec 07, 2009 10:27 am
Location: Indonesia
Contact:

[response]Pen Origin?

Post by rhezalouis »

Eh? I'm sorry I don't get what do you mean with pen origin. :surprised:I thought the origins for both the X and Y axes are always at the top-left, like:

Code: Select all

0 1 2 3 4 5
1
2
3
4
5
so, could you show an example of your work?

I think to invert the position, you might set the variables as something like this

Code: Select all

x = love.graphics.getWidth() - xInMyReference;
y = love.graphics.getHeight() - yInMyReference;
and use them as

Code: Select all

love.graphics.print(myText, x, y);
, eh?
Aargh, I am wasting my posts! My citizenshiiiip... :o
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Drawing fonts

Post by Robin »

matthewburk wrote:When drawing a font with graphics.print() x and y seem aren't top left, but seem to be the pen origin, so all of my text is not where i want it exactly. I couldn't find any font metrics api so that i could compenstate, is there a way to draw a font with x,y as top left?
That (x,y is the bottom-left location instead of top-left) is a known problem, and is fixed in the latest development version.

As a drop-in fix, you can try this:

Code: Select all

do
    local prnt = love.graphics.print
    function love.graphics.print(text, x, y)
        prnt(text, x, y - love.graphics.getFont():getHeight(text))
    end
end
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Drawing fonts

Post by bartbes »

I'd recommend you store it though, this is pretty inefficient.
matthewburk
Prole
Posts: 15
Joined: Fri Mar 05, 2010 6:56 am

Re: Drawing fonts

Post by matthewburk »

Robin wrote: That (x,y is the bottom-left location instead of top-left) is a known problem, and is fixed in the latest development version.

As a drop-in fix, you can try this:

Code: Select all

do
    local prnt = love.graphics.print
    function love.graphics.print(text, x, y)
        prnt(text, x, y - love.graphics.getFont():getHeight(text))
    end
end
I tried this last night, what happens in that case is any part of a letter that goes below the baseline is cut-off, for example letters like p, y, g. By cut-off i mean i am first clipping to the rectangle of x, y, font:getWidth(string) font:getHeight(string)
and then drawing it as you suggested. Which is why I say it is drawing at the pen origin. Of course it could be that getHeight() is just retruning the ascent.

I'll post an example .love showing what i mean when i get home tonight.
matthewburk
Prole
Posts: 15
Joined: Fri Mar 05, 2010 6:56 am

Re: Drawing fonts

Post by matthewburk »

Ok, i checked out the source, looks like its from the NEHE tutorial, which i also based my font drawing off of, for another project. Your text drawing is from the pens position, not lower left. Which means that text will draw below the y value, (and to the left of the x value for some fonts) passed into the draw function. I have attached some code that does basically the same thing (see glfont_draw_string) but there is a also a function in there to draw from top-left (glfont_draw_label) which can be changed to bottom-left if you don't substract font->height. You'll notice i have to measure the string to get the bounding box to adjust it correctly for a top-left origin.
Attachments
glfont.c
(11.13 KiB) Downloaded 81 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 213 guests