Page 1 of 1

tmOverhang GetTextMetrics()

Posted: Sat Aug 16, 2008 8:25 pm
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? :(

Posted: Sat Aug 16, 2008 9:00 pm
by srod

Code: Select all

hdc = StartDrawing(WindowOutput(0)) 

Posted: Sat Aug 16, 2008 9:20 pm
by Chirantha
But for some reason tmOverhang is returning 0 :( how do I fix this? :(

Posted: Sat Aug 16, 2008 9:25 pm
by srod
It works fine here. Try a courier font for example. Make sure you place The GetTextMetrics_() function after the DrawingFont() command.

Posted: Sat Aug 16, 2008 9:32 pm
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

Posted: Sat Aug 16, 2008 10:19 pm
by Chirantha
I don't see any difference :S.. Part of the D is missing plus.. that value is returning 0 :(

Posted: Sat Aug 16, 2008 11:09 pm
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...

Posted: Sat Aug 16, 2008 11:13 pm
by Kaeru Gaman
here is also a part cut off...

Image

WinXPpro sp3 classic skin

Posted: Sun Aug 17, 2008 12:10 pm
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