tmOverhang GetTextMetrics()

Just starting out? Need help? Post your questions and find answers here.
Chirantha
User
User
Posts: 54
Joined: Sat Jul 16, 2005 7:22 pm

tmOverhang GetTextMetrics()

Post by Chirantha »

Hi,

I'm creating a window that fits my text which is drawn using 2D functions. but when the text is italic the text width is incorrect.

Here is the code:

Code: Select all

OpenWindow(0,10,10,100,100,"Test")
LoadFont(1,"Lucida Handwriting",25,#PB_Font_Bold|#PB_Font_Italic)
StartDrawing(WindowOutput(0))
DrawingFont(FontID(1))
height=TextHeight("OMG HI! :D")
width=TextWidth("OMG HI! :D")
ResizeWindow(0,10,10,width,height)
DrawText(0,0,"OMG HI! :D",1)
StopDrawing()

Repeat

 Event=WaitWindowEvent()

Until Event=#PB_Event_CloseWindow
Now I ran into the tmOverhang value which does calculate the extra part of the italic text which TextWidth misses. But I have no idea what to set the DC to in GetTextMetrics() :(

Can anyone please help? :(
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Code: Select all

hdc = StartDrawing(WindowOutput(0)) 
I may look like a mule, but I'm not a complete ass.
Chirantha
User
User
Posts: 54
Joined: Sat Jul 16, 2005 7:22 pm

Post by Chirantha »

But for some reason tmOverhang is returning 0 :( how do I fix this? :(
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

It works fine here. Try a courier font for example. Make sure you place The GetTextMetrics_() function after the DrawingFont() command.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Ah, your original code is not resizing the window correctly because it is attempting to size it to a width less than the minimum possible for a window of that type!!! Use a bigger string and you'll see it all works fine - in fact I do not believe you need the GetTextMetrics_() function at all!

Try the following to see that your code runs fine as is :

Code: Select all

OpenWindow(0,10,10,100,100,"Test", #pb_window_borderless) 
LoadFont(1,"Lucida Handwriting",25,#PB_Font_Bold|#PB_Font_Italic) 
StartDrawing(WindowOutput(0)) 
DrawingFont(FontID(1)) 
height=TextHeight("OMG HI! :D") 
width=TextWidth("OMG HI! :D") 
ResizeWindow(0,10,10,width,height) 
DrawText(0,0,"OMG HI! :D",1) 
StopDrawing() 

Repeat 

 Event=WaitWindowEvent() 

Until Event=#PB_Event_CloseWindow
I may look like a mule, but I'm not a complete ass.
Chirantha
User
User
Posts: 54
Joined: Sat Jul 16, 2005 7:22 pm

Post by Chirantha »

I don't see any difference :S.. Part of the D is missing plus.. that value is returning 0 :(
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Chirantha wrote:I don't see any difference :S.. Part of the D is missing plus.. that value is returning 0 :(
I don't see any difference either, both versions work fine and show all of the text.

@Edit: Didn't have the font installed...
Last edited by Demivec on Sat Aug 16, 2008 11:25 pm, edited 1 time in total.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

here is also a part cut off...

Image

WinXPpro sp3 classic skin
oh... and have a nice day.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Okay, the tmOverhang field of the TEXTMETRIC structure is only valid for raster fonts (not true-type). For TT fonts you need to use GetCharABCWidths_().

The following code is a quick hack and would be far improved by using proper checks for a TT font. I couldn't be bothered though and put in a cheap and cheerful check! :wink:

Code: Select all

a$ = "OMG HI! :D"

If OpenWindow(0,10,10,100,100,"Test", #PB_Window_BorderLess|#PB_Window_ScreenCentered) 
  LoadFont(1,"Australian sunrise",85,#PB_Font_Italic|#PB_Font_Bold) 
  hdc = StartDrawing(WindowOutput(0)) 
  If hdc
    DrawingFont(FontID(1)) 

    height=TextHeight(a$) 
    width=TextWidth(a$) 

    ;Adjust the overhang.
      GetTextMetrics_(hdc, tm.TEXTMETRIC)
      If tm\tmOverhang
        width + tm\tmOverhang
      Else
        char = Asc(Right(a$,1))
        GetCharABCWidths_(hdc, char, char, abc.ABC)  
        overhang = abc\abcC
        If overHang < 0
          width - overHang
        EndIf
      EndIf

    ResizeWindow(0,10,10,width,height) 
    DrawText(0,0,a$) 
    StopDrawing() 
  EndIf

  Repeat 
    Event=WaitWindowEvent() 
  Until Event=#PB_Event_CloseWindow 

EndIf
I may look like a mule, but I'm not a complete ass.
Post Reply