How to make canvas font same as string gadget font?

Windows specific forum
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

How to make canvas font same as string gadget font?

Post by ozzie »

I'm trying to display canvas gadgets with the same font as nearby string and other gadgets, but the fonts display differently. Here's a test program:

Code: Select all

If OpenWindow(0, 0, 0, 322, 50, "Font Comparison", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 8, 10, 120, 21, "StringGadget", #PB_Text_Right)
  CanvasGadget(1, 140, 10, 120, 21)
  If StartDrawing(CanvasOutput(1))
    DrawingFont(#PB_Default)
    DrawingMode(#PB_2DDrawing_Outlined)
    Box(0,0,OutputWidth(),OutputHeight(),RGB(122,122,122))
    DrawingMode(#PB_2DDrawing_Default)
    nTop = (OutputHeight() - TextHeight("CanvasGadget")) >> 1
    DrawText(6, nTop, "CanvasGadget", RGB(0,0,0), RGB(255,255,255))
    StopDrawing()
  EndIf
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
And here's the result:
Image

Any suggestions on how to make the font of the text in the canvas gadget match that of the string gadget?
TassyJim
Enthusiast
Enthusiast
Posts: 151
Joined: Sun Jun 16, 2013 6:27 am
Location: Tasmania (Australia)

Re: How to make canvas font same as string gadget font?

Post by TassyJim »

Instead of relying on the system font, define the font used for the string gadget as well as the canvas

Code: Select all

If OpenWindow(0, 0, 0, 322, 50, "Font Comparison", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 8, 10, 120, 21, "StringGadget", #PB_Text_Right)
  Font1 = LoadFont(#PB_Any, "Arial"  ,  10)
SetGadgetFont(0, FontID(Font1))
  CanvasGadget(1, 140, 10, 120, 21)
  If StartDrawing(CanvasOutput(1))
    DrawingFont(FontID(Font1))
    DrawingMode(#PB_2DDrawing_Outlined)
    Box(0,0,OutputWidth(),OutputHeight(),RGB(122,122,122))
    DrawingMode(#PB_2DDrawing_Default)
    nTop = (OutputHeight() - TextHeight("CanvasGadget")) >> 1
    DrawText(6, nTop, "CanvasGadget", RGB(0,0,0), RGB(255,255,255))
    StopDrawing()
  EndIf
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
breeze4me
Enthusiast
Enthusiast
Posts: 511
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: How to make canvas font same as string gadget font?

Post by breeze4me »

There is GetGadgetFont() function.

Code: Select all

If OpenWindow(0, 0, 0, 322, 50, "Font Comparison", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 8, 10, 120, 21, "StringGadget", #PB_Text_Right)
  CanvasGadget(1, 140, 10, 120, 21)
  
  ;hFont = GetGadgetFont(0)
  hFont = GetGadgetFont(#PB_Default)
  
  If StartDrawing(CanvasOutput(1))
    DrawingFont(hFont)
    DrawingMode(#PB_2DDrawing_Outlined)
    Box(0,0,OutputWidth(),OutputHeight(),RGB(122,122,122))
    DrawingMode(#PB_2DDrawing_Default)
    nTop = (OutputHeight() - TextHeight("CanvasGadget")) >> 1
    DrawText(6, nTop, "CanvasGadget", RGB(0,0,0), RGB(255,255,255))
    StopDrawing()
  EndIf
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: How to make canvas font same as string gadget font?

Post by ozzie »

Brilliant! Many thanks to you both. Using GetGadgetFont() of an associated String gadget and using that as the Canvas gadget's DrawingFont() gives the correct result. :D
Post Reply