Page 1 of 1

DrawText changing fonts, fontsize and colors

Posted: Mon Mar 03, 2008 11:20 am
by einander
DrawText changing fonts, fontsizes and colors

Code: Select all

;By einander
;PB 4.20 Beta 2 - march 2008

Global _DRAWING

Procedure AlignText(X,YAlign,Text.s,Font,RGB=-1,BackRGB=-1)
     ; ;Without  RGB Param or RGB=-1, keeps previous RGB. Default=#Black
     ; ;Without BackRGB Param or BackRGB=-1, keeps previous BackRGB. Default=#White
     Static X1,Y1,OldRGB,OldBACK
     FontID=FontID(Font)
     DrawingFont(FontID)
     If RGB=-1 : RGB=OldRGB: EndIf
     If BackRGB=-1
          If OldBACK=RGB:BackRGB=#White
          Else :  BackRGB=OldBACK
          EndIf
     EndIf
     If X=-1
          If X1=0:X=0
          Else:X=X1
          EndIf
     EndIf
     If YAlign=-1
          If Y1=0:YAlign=0
          Else:YAlign=Y1
          EndIf
     EndIf
     GetTextMetrics_(_DRAWING, TM.NEWTEXTMETRIC)  ;to change text bottom position
     
     
     X1=DrawText(X,YAlign-TM\tmAscent,Text,RGB,BackRGB)
     Y1=YAlign
     
     OldRGB=RGB :  OldBACK=BackRGB
     ProcedureReturn X1 ; x position for the next string
EndProcedure
   
     ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;Test it
hwnd=OpenWindow(0, 100, 100,700,500 ,"Draw text aligned changing font sizes and colors",  #WS_OVERLAPPEDWINDOW | #WS_MAXIMIZE)
CreateGadgetList(hwnd)
Wi=WindowWidth(0):He=WindowHeight(0)
ImGad=ImageGadget(-1,0,0,0,0,0)
IMG=CreateImage(-1,Wi,He,32)

Dim Font(5)
     
Font(0)=LoadFont(-1,"Arial",26)
Font(1)=LoadFont(-1,"Comic sans MS",30)
Font(2)=LoadFont(-1,"Times new Roman",22)
Font(3)=LoadFont(-1,"WingDings",18)
Font(4)=LoadFont(-1,"Courier New",12)
Font(5)=LoadFont(-1,"Courier New",24)
     
_DRAWING=StartDrawing(ImageOutput(IMG))
     X=10 :YAlign=100
     AlignText(X,YAlign,"Draw text aligned",Font(2),#Magenta)
     AlignText(-1,-1, " changing",Font(1),#Yellow)
     AlignText(-1,-1," font sizes and c",Font(2),#Red)
     AlignText(-1,-1,"o",Font(0),#Green)
     AlignText(-1,-1,"l",Font(1),#Magenta)
     AlignText(-1,-1,"o",Font(5),#Cyan)
     AlignText(-1,-1,"r",Font(0),#Yellow)
     AlignText(-1,-1,"s",Font(4),#White)
     
     X=10:YAlign=200  ; new line
     AlignText(X,YAlign,"Arial 10",Font(0),#Red)
     AlignText(-1,-1," Comic Sans MS 30",Font(1),$FFFCC2)
     X1=AlignText(-1,-1," Times New Roman 22",Font(2),#Yellow)
     LineXY(10,YAlign,X1,YAlign,$AABBCC)
     
     YAlign=300  ; new line
     
     AlignText(10,YAlign,"WingDings 18",Font(3))
     AlignText(-1,-1," Courier new 12",Font(4),#Red,#Cyan)
     X1=AlignText(-1,-1," Courier New 24",Font(5),0)
     LineXY(0,YAlign,X1,YAlign,#Blue)
StopDrawing():_DRAWING=0
SetGadgetState(ImGad,ImageID(IMG))
     
Repeat
     If GetAsyncKeyState_(#VK_ESCAPE):Break:EndIf
     Ev=WaitWindowEvent()
Until Ev=#WM_CLOSE
For i = 0 To 5
     FreeFont(Font(i))
Next
End 
Have fun!

Posted: Mon Mar 03, 2008 11:35 am
by srod
Nice! :)

Posted: Mon Mar 03, 2008 2:26 pm
by einander
Thanks Srod!

Posted: Mon Mar 03, 2008 6:12 pm
by Kwai chang caine
Yes, it's great and very nice 8)

You give me an idea to your turn, is it possible to insert a little jpg picture, (Like the size of the characters), Between your characters, As if the photo was an character ?? :roll:

Posted: Mon Mar 03, 2008 6:26 pm
by einander
Yes this can be achieved.
Each block of text on the example is treated like a rectangle, so it's matter of replace a text block with an image.
Ill try it.

The same code with image insert

Posted: Mon Mar 03, 2008 6:56 pm
by einander

Code: Select all

;By einander
;idea by Kwaï chang caïne
;PB 4.20 Beta 2 - march 2008
Global _DRAWING


Procedure AlignText(X,YAlign,Text.s,Font,wParam=-1,lParam=-1,IMG=-1)
     ; for text, wParam=frontcolor, lParam=BackColor
     ; ;Without  wParam or wParam=-1, keeps previous FrontColor. Default=#Black
     ; ;Without lParam or lParam=-1, keeps previous BackColor. Default=#White
     
     ; for image, wParam=ImageWidth:lParam=ImageHeight
     Static X1,Y1,OldRGB,OldBACK
     
     If X=-1
          If X1=0:X=0
          Else:X=X1
          EndIf
     EndIf
     If YAlign=-1
          If Y1=0:YAlign=0
          Else:YAlign=Y1
          EndIf
     EndIf
     If IMG>-1
          DrawImage(ImageID(IMG),X,YAlign-lParam,wParam,lParam)
          X1= X+wParam
     Else
          FontID=FontID(Font)
          DrawingFont(FontID)
          If wParam=-1 : wParam=OldRGB: EndIf
          If lParam=-1
               If OldBACK=wParam:lParam=#White
               Else :  lParam=OldBACK
               EndIf
          EndIf
          GetTextMetrics_(_DRAWING, TM.NEWTEXTMETRIC)  ;to change text bottom position
          
          X1=DrawText(X,YAlign-TM\tmAscent,Text,wParam,lParam)
          Y1=YAlign
          OldRGB=wParam :  OldBACK=lParam
     EndIf
     ProcedureReturn X1 ; x position for the next string
EndProcedure
   
     ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;Test it
hwnd=OpenWindow(0, 100, 100,700,500 ,"Draw text and images changing font sizes and colors",  #WS_OVERLAPPEDWINDOW | #WS_MAXIMIZE)
CreateGadgetList(hwnd)
Wi=WindowWidth(0):He=WindowHeight(0)
ImGad=ImageGadget(-1,0,0,0,0,0)
IMG=CreateImage(-1,Wi,He,32)
Img1=LoadImage(-1,"c:\test3.bmp") ;here put your image <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Dim Font(5)
     
Font(0)=LoadFont(-1,"Arial",26)
Font(1)=LoadFont(-1,"Comic sans MS",30)
Font(2)=LoadFont(-1,"Times new Roman",22)
Font(3)=LoadFont(-1,"WingDings",18)
Font(4)=LoadFont(-1,"Courier New",12)
Font(5)=LoadFont(-1,"Courier New",24)
     
_DRAWING=StartDrawing(ImageOutput(IMG))
     X=10 :YAlign=100
     AlignText(X,YAlign,"Draw text and images ",Font(2),#Magenta)
     AlignText(-1,-1,"",0,50,50,Img1)   ; this line can be moved as you like <<<<<<<<<<<<<<<<<<<<<
     AlignText(-1,-1, " changing",Font(1),#Yellow)
     AlignText(-1,-1," font sizes and c",Font(2),#Red)
     AlignText(-1,-1,"o",Font(0),#Green)
     AlignText(-1,-1,"l",Font(1),#Magenta)
     AlignText(-1,-1,"o",Font(5),#Cyan)
     AlignText(-1,-1,"r",Font(0),#Yellow)
     AlignText(-1,-1,"s",Font(4),#White)
     X=10:YAlign=200  ; new line
     AlignText(X,YAlign,"Arial 10",Font(0),#Red)
     AlignText(-1,-1," Comic Sans MS 30",Font(1),$FFFCC2)
     X1=AlignText(-1,-1," Times New Roman 22",Font(2),#Yellow)
     LineXY(10,YAlign,X1,YAlign,$AABBCC)
     
     YAlign=300  ; new line
     
     AlignText(10,YAlign,"WingDings 18",Font(3))
     AlignText(-1,-1," Courier new 12",Font(4),#Red,#Cyan)
     X1=AlignText(-1,-1," Courier New 24",Font(5),0)
     LineXY(0,YAlign,X1,YAlign,#Blue)
StopDrawing():_DRAWING=0
SetGadgetState(ImGad,ImageID(IMG))
     
Repeat
     If GetAsyncKeyState_(#VK_ESCAPE):Break:EndIf
     Ev=WaitWindowEvent()
Until Ev=#WM_CLOSE
For i = 0 To 5
     FreeFont(Font(i))
Next
End

Posted: Mon Mar 03, 2008 10:58 pm
by Kwai chang caine
Yeeeeessss !!!!

It's super cool !!!!! :D
More, the picture auto stretching 8)

I have put a big picture and she appears more little 8)
Exactly what i want, without ask you this option, thanks again.

This is a big beginner for make approximatly the same effect that the code of SROD and NICO with a EditorGadget, but in more simple, i think 8)
http://www.purebasic.fr/english/viewtop ... jectaddref
Just add the keyboard management and this is a clone :D

Code: Select all

;By einander 
;idea by Kwaï chang caïne 
;PB 4.20 Beta 2 - march 2008 
Global _DRAWING 
Thank you very much for this little word in begin of your code 8)
I do not deserve this honour :oops:

I wish you a good night

Posted: Mon Mar 03, 2008 11:35 pm
by einander
You're welcome :)

Posted: Mon Mar 03, 2008 11:47 pm
by Kwai chang caine
:wink:

Posted: Tue Mar 04, 2008 11:26 am
by DoubleDutch
einander: You can replace...

Code: Select all

DrawText(X,YAlign-TM\tmAscent,Text,RGB,BackRGB) 
X1=X+TextWidth(Text) : Y1=YAlign 
with...

Code: Select all

X1=DrawText(X,YAlign-TM\tmAscent,Text,RGB,BackRGB)
Y1=YAlign

Posted: Tue Mar 04, 2008 2:24 pm
by einander
Thanks DoubleDutch!

Oops! Surprise :oops:
From the new PB Help:
The new x position of the text cursor (ie: just after the last printed character) is returned in 'Result' to allow easy text concatenation, if required.
Changed on both examples

Posted: Tue Mar 04, 2008 2:39 pm
by DoubleDutch
I requested it on an alpha or beta ages ago and Fred put it in. :)

Posted: Tue Mar 04, 2008 5:43 pm
by Psychophanta
Nice code.
By the way, you can download lots of text fonts at:
http://www.hackingballz.com/fuentes
even i don't know all of it is legal :?

Posted: Tue Mar 04, 2008 10:24 pm
by einander
Thanks Albert.
Some nice fonts on that site.

Re: DrawText changing fonts, fontsize and colors

Posted: Tue Dec 31, 2013 3:13 am
by Michel_k17
Code was not working for me (using PB 5.10), which was driving me nuts. It looks like older versions of PB might have selected the font object as part of the DrawingFont function.

At any rate, here is the modified code that works.

Note: although not clear in the subject line, this code vertically aligns text of different fonts/sizes to the Baseline of the font rather than the top.

Code: Select all

;By einander
;PB 4.20 Beta 2 - march 2008
;PB 5.10 Dec 2013 modified by Michel K-17 (added SelectObject_ so that it would work)

Global _DRAWING

Procedure AlignText(X,YAlign,Text.s,Font,RGB=-1,BackRGB=-1)
     ; ;Without  RGB Param or RGB=-1, keeps previous RGB. Default=#Black
     ; ;Without BackRGB Param or BackRGB=-1, keeps previous BackRGB. Default=#White
     Static X1,Y1,OldRGB,OldBACK
     FontID=FontID(Font)
     DrawingFont(FontID)
     If RGB=-1 : RGB=OldRGB: EndIf
     If BackRGB=-1
          If OldBACK=RGB:BackRGB=#White
          Else :  BackRGB=OldBACK
          EndIf
     EndIf
     If X=-1
          If X1=0:X=0
          Else:X=X1
          EndIf
     EndIf
     If YAlign=-1
          If Y1=0:YAlign=0
          Else:YAlign=Y1
          EndIf
     EndIf

     ; Select the font, Get the Font Metrics, and then reselect the current object
     hOld.l = SelectObject_(_DRAWING,FontID)
     GetTextMetrics_(_DRAWING, TM.NEWTEXTMETRIC)  ;to change text bottom position
     If hOld <> 0 : SelectObject_(_DRAWING,hOld):EndIf
     
     
     
     X1=DrawText(X,YAlign-TM\tmAscent,Text,RGB,BackRGB)
     Y1=YAlign
     
     OldRGB=RGB :  OldBACK=BackRGB
     ProcedureReturn X1 ; x position for the next string
EndProcedure
   
     ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;Test it
hwnd=OpenWindow(0, 100, 100,700,500 ,"Draw text aligned changing font sizes and colors",  #WS_OVERLAPPEDWINDOW | #WS_MAXIMIZE)
CreateGadgetList(hwnd)
Wi=WindowWidth(0):He=WindowHeight(0)
ImGad=ImageGadget(-1,0,0,0,0,0)
IMG=CreateImage(-1,Wi,He,32)

Dim Font(5)
     
Font(0)=LoadFont(-1,"Arial",26)
Font(1)=LoadFont(-1,"Comic sans MS",30)
Font(2)=LoadFont(-1,"Times new Roman",22)
Font(3)=LoadFont(-1,"WingDings",18)
Font(4)=LoadFont(-1,"Courier New",12)
Font(5)=LoadFont(-1,"Courier New",24)
     
_DRAWING=StartDrawing(ImageOutput(IMG))
     X=10 :YAlign=100
     AlignText(X,YAlign,"Draw text aligned",Font(2),#Magenta)
     AlignText(-1,-1, " changing",Font(1),#Yellow)
     AlignText(-1,-1," font sizes and c",Font(2),#Red)
     AlignText(-1,-1,"o",Font(0),#Green)
     AlignText(-1,-1,"l",Font(1),#Magenta)
     AlignText(-1,-1,"o",Font(5),#Cyan)
     AlignText(-1,-1,"r",Font(0),#Yellow)
     AlignText(-1,-1,"s",Font(4),#White)
     LineXY(10,YAlign,700,YAlign,$AABBCC)
     
     X=10:YAlign=200  ; new line
     AlignText(X,YAlign,"Arial 10",Font(0),#Red)
     AlignText(-1,-1," Comic Sans MS 30",Font(1),$FFFCC2)
     X1=AlignText(-1,-1," Times New Roman 22",Font(2),#Yellow)
     LineXY(10,YAlign,X1,YAlign,$AABBCC)
     
     YAlign=300  ; new line
     
     AlignText(10,YAlign,"WingDings 18",Font(3))
     AlignText(-1,-1," Courier new 12",Font(4),#Red,#Cyan)
     X1=AlignText(-1,-1," Courier New 24",Font(5),0)
     LineXY(0,YAlign,X1,YAlign,#Blue)
StopDrawing():_DRAWING=0
SetGadgetState(ImGad,ImageID(IMG))
     
Repeat
     If GetAsyncKeyState_(#VK_ESCAPE):Break:EndIf
     Ev=WaitWindowEvent()
Until Ev=#WM_CLOSE
For i = 0 To 5
     FreeFont(Font(i))
Next
End