Change "DrawText Size"

Just starting out? Need help? Post your questions and find answers here.
pfaber11
Enthusiast
Enthusiast
Posts: 145
Joined: Sat Apr 13, 2019 12:17 pm

Change "DrawText Size"

Post by pfaber11 »

Hi when using DrawText how do I change the size of the text ? something like set text size maybe .
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Change "DrawText Size"

Post by netmaestro »

Load the font you want in the desired size and after your StartDrawing() command, use DrawingFont(FontID(#font)) and you're good to go. Your DrawText() line comes after you've selected the font.

Example:

Code: Select all

OpenWindow(0,0,0,400,120,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CanvasGadget(0,0,0,400,120)
LoadFont(0, "Arial", 48, #PB_Font_Bold)
StartDrawing(CanvasOutput(0))
  DrawingFont(FontID(0))
  DrawText(15,20,"Hello World!",#Black,#White)
StopDrawing()
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
Last edited by netmaestro on Fri May 27, 2022 11:34 pm, edited 1 time in total.
BERESHEIT
User avatar
idle
Always Here
Always Here
Posts: 5050
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Change "DrawText Size"

Post by idle »

The size comes from LoadFont an example with sprites

Code: Select all

EnableExplicit 

InitSprite()
InitKeyboard() 

Global font = LoadFont(#PB_Any,"arial",72,#PB_Font_HighQuality)

Procedure DisplayMessageCenter(window,msg.s,color=255,scale.f=1.00)
  
  Protected spriteNumber,tempImage,fontsTextWidth,fontsTextHeight,cx,cy
  
  tempImage = CreateImage(#PB_Any,1,1)    ;if you need to get the size of a font in pixels         
  
  If tempImage
    If StartDrawing(ImageOutput(tempImage)) ;draw to the temp image 
      DrawingFont(FontID(font))             ;with the selected font  
      fontsTextWidth = TextWidth(msg)       ;get the width and height in pixles     
      fontsTextHeight = TextHeight(msg)  
      StopDrawing()  
      spriteNumber = CreateSprite(#PB_Any,fontsTextWidth,fontsTextHeight) ;create the sprite of required size 
      If spriteNumber 
        If StartDrawing(SpriteOutput(spriteNumber))   ;now you can draw the text to the sprite 
          DrawingFont(FontID(font)) 
          DrawText(0,0,msg,color)
          StopDrawing()
          TransparentSpriteColor(spriteNumber,0)
          cx= (WindowWidth(window) - (fontsTextWidth*scale)) / 2   
          cy = (WindowHeight(window)- (fontsTextHeight*scale)) / 2        
          ZoomSprite(spriteNumber,fontsTextWidth*scale,fontsTextHeight*scale)  
          DisplayTransparentSprite(spriteNumber,cx,cy)
        EndIf   
        FreeSprite(spriteNumber)
      EndIf  
    EndIf  
    FreeImage(tempImage)      
  EndIf 
  
EndProcedure 

Global event,ct,sc

OpenWindow(0,0,0,800,600,"Putin de merde clock",#PB_Window_ScreenCentered | #PB_Window_SystemMenu) 
OpenWindowedScreen(WindowID(0),0,0,800,600) 

Repeat
  Repeat
    event=WindowEvent()
    If event=#PB_Event_CloseWindow
      End
    EndIf
  Until event=0
  
  ExamineKeyboard() 
  ClearScreen((ct%64)*2)
  DisplayMessageCenter(0,FormatDate("%hh:%ii:%ss", Date()-ct),RGB(0,192,0),0.025*sc)
  ct+1
  sc+1 
  sc%60
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)     
pfaber11
Enthusiast
Enthusiast
Posts: 145
Joined: Sat Apr 13, 2019 12:17 pm

Re: Change "DrawText Size"

Post by pfaber11 »

Thanks for the replies , I've got it down now .
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Change "DrawText Size"

Post by Distorted Pixel »

If I use LoadFont() 2 times for the same font because I want to change font size on the second one, I'm guessing the font is actually loaded twice and takes up more memory? Because all I want to do is change the font size, I don't want 2 instances of the font in memory, that is bad coding in my opinion. I can't just change the text size in a text gadget because I'm drawing text with DrawText()

Code: Select all

Procedure EnterSurName()
OpenWindow(0, 0, 0, 1024, 768, "Ultimate Football Manager", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered)
InitSprite() : InitKeyboard()

If OpenWindowedScreen(WindowID(0), 0, 0, 322, 50)
    StringGadget(1, 8,  10, 306, 20, "")

If LoadImage(0, "Images/EnterNameBackground.png")
  LoadFont(0, "Media/nevis.ttf", 20, #PB_Font_Bold)

  If StartDrawing(ImageOutput(0))
    DrawingMode(1)        ; transparent
    DrawingFont(FontID(0))
    Box(18, 48, 107, 35, $000000)
    DrawText(20, 50, "Player", $00C3C3, $000000)
    DrawText(110, 50, "1", $00C3C3, $000000)
    StopDrawing()
  EndIf
  
  LoadFont(0, "Media/nevis.ttf", 15);, #PB_Font_Bold)
  
  If StartDrawing(ImageOutput(0))
    DrawingMode(1)      ; transparent
    DrawingFont(FontID(0))
    Box(392, 333, 195, 20, $000000)
    DrawText(393, 331, "Please enter surname", $00C3C3, $000000)
  EndIf
  StopDrawing()
  
  ImageGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), ImageID(0))
  DisableGadget(0, #True)
  
  
  Global stringpattern.l, bkswitch.l = 0 

  CreateImage(1, 320, 240, 32) 
  StartDrawing(ImageOutput(1)) 
    For i = 0 To 231 Step 16 
      For j = 0 To 311 Step 16 
        Box(j, i, 8, 8, RGB(230, 230, 255)) 
        Box(j + 8, i, 8, 8, #White) 
        Box(j, i + 8, 8, 8, #White) 
        Box(j + 8, i + 8, 8, 8, RGB(230, 230, 255))      
      Next 
    Next 
  StopDrawing() 

  ImageGadget(1, 0, 0, 0, 0, ImageID(0)) 
  DisableGadget(1, #True) 
  StringGadget(1, 390, 355, 200, 25, PlayerName$)
  GrabImage(1, 1, GadgetX(1) + 2, GadgetY(1) + 2, GadgetWidth(1) - 2, GadgetHeight(1) - 2) 
  stringpattern = CreatePatternBrush_(ImageID(1)) 
  SetWindowCallback(@WinProc()) 


DeleteObject_(stringpattern) 
  
Repeat
  
      ExamineKeyboard()
      PlayerName = GetGadgetText(1)
     
      If KeyboardPushed(#PB_Key_Return) And return_key_down = #False
        Debug PlayerName
        return_key_down = #True
      ElseIf KeyboardReleased(#PB_Key_Return) And return_key_down = #True
        return_key_down = #False
      EndIf
  
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
EndIf

EndProcedure
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
jacdelad
Addict
Addict
Posts: 1436
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Change "DrawText Size"

Post by jacdelad »

If you use LoadFont multiple times, each loaded font needs space, even if you always load the same font with the same parameters. But nowadays it shouldn't be a problem to load a bunch of fonts?!
BUT in your example you use a fixed ID, so the second time LoadFont is called the first font is automatically freed before the second font is loaded and assigned to this ID.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4637
Joined: Sun Apr 12, 2009 6:27 am

Re: Change "DrawText Size"

Post by RASHAD »

Just for fun :)

Code: Select all

Procedure newtext(id,size.f,text.s,bcolor,fcolor)
  StartDrawing(WindowOutput(0))
    DrawingFont(FontID(0))
    w = TextWidth(text)
    h = TextHeight(text)
  StopDrawing()
  CreateImage(id,w,h,24,bcolor)
  StartDrawing(ImageOutput(id))
    DrawingFont(FontID(0))
    DrawText(0,0,text,fcolor,bcolor)
  StopDrawing()
  ResizeImage(id,w*size,h*size)
EndProcedure

LoadFont(0, "Arial", 24, #PB_Font_Bold)
OpenWindow(0,0,0,400,120,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
newtext(0,0.4,"Hi there!",$ffffff,$0)
newtext(1,1,"Hello World!",$FFFFFF,$0000FF)
newtext(2,0.6,"RASHAD",$FFFFFF,$FF0000)
CanvasGadget(0,0,0,400,120)
StartDrawing(CanvasOutput(0))
  DrawImage(ImageID(0),15,2)
  DrawImage(ImageID(1),15,30)
  DrawImage(ImageID(2),15,80)
StopDrawing()
FreeFont(0)
For img = 0 To 2
  FreeImage(img)
Next
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
Egypt my love
User avatar
mk-soft
Always Here
Always Here
Posts: 5336
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Change "DrawText Size"

Post by mk-soft »

With VectorDrawing can define with VectorFont the size of font with one loaded font
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Change "DrawText Size"

Post by Distorted Pixel »

RASHAD wrote: Sat Nov 12, 2022 11:14 am Just for fun :)

Code: Select all

Procedure newtext(id,size.f,text.s,bcolor,fcolor)
  StartDrawing(WindowOutput(0))
    DrawingFont(FontID(0))
    w = TextWidth(text)
    h = TextHeight(text)
  StopDrawing()
  CreateImage(id,w,h,24,bcolor)
  StartDrawing(ImageOutput(id))
    DrawingFont(FontID(0))
    DrawText(0,0,text,fcolor,bcolor)
  StopDrawing()
  ResizeImage(id,w*size,h*size)
EndProcedure

LoadFont(0, "Arial", 24, #PB_Font_Bold)
OpenWindow(0,0,0,400,120,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
newtext(0,0.4,"Hi there!",$ffffff,$0)
newtext(1,1,"Hello World!",$FFFFFF,$0000FF)
newtext(2,0.6,"RASHAD",$FFFFFF,$FF0000)
CanvasGadget(0,0,0,400,120)
StartDrawing(CanvasOutput(0))
  DrawImage(ImageID(0),15,2)
  DrawImage(ImageID(1),15,30)
  DrawImage(ImageID(2),15,80)
StopDrawing()
FreeFont(0)
For img = 0 To 2
  FreeImage(img)
Next
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
Thank you for your example. I will play around with it and learn from it.
jacdelad wrote: Sat Nov 12, 2022 4:35 am If you use LoadFont multiple times, each loaded font needs space, even if you always load the same font with the same parameters. But nowadays it shouldn't be a problem to load a bunch of fonts?!
BUT in your example you use a fixed ID, so the second time LoadFont is called the first font is automatically freed before the second font is loaded and assigned to this ID.
I totally forgot it frees the first one. Thank you for reminding me

So even since I changed font size and kept the same FontID, it frees the first one?
Last edited by Distorted Pixel on Sun Nov 13, 2022 5:23 pm, edited 1 time in total.
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
User avatar
Caronte3D
Addict
Addict
Posts: 1029
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Change "DrawText Size"

Post by Caronte3D »

Yes
Each LoadFont with the same number frees the previous font with that number
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Change "DrawText Size"

Post by Distorted Pixel »

Caronte3D wrote: Sun Nov 13, 2022 5:22 pm Yes
Each LoadFont with the same number frees the previous font with that number
Ok, Thank you
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar? :shock:

No one cares how much you know until they know how much you care
Post Reply