Using different font style with DrawText()

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Using different font style with DrawText()

Post by RASHAD »

Maybe it will be useful for someone

Code: Select all

CreateImage(0, 200, 200)

Procedure UnderL(Text$,XX,YY,ZZ)
      StartDrawing(ImageOutput(0))
      DrawingMode(#PB_2DDrawing_Transparent)
      For i = 1 To Len(Text$)
      If i = ZZ
        DrawingFont(FontID(0))
        Color = $0102FE
      Else
        DrawingFont(FontID(1))
        Color = 0 
      EndIf 
        Result$ = Mid(Text$, i,1)
        XX = DrawText(XX,YY, Result$, Color)
      Next i
      StopDrawing()
EndProcedure 
  
  
    LoadFont(0, "Broadway",12,#PB_Font_Underline)
    LoadFont(1, "Arial",12)
  
  If OpenWindow(0, 0, 0, 200, 200, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

    StartDrawing(ImageOutput(0))
      Box(0, 0, 200, 200, $FFFFFF)
    StopDrawing()
    
    UnderL("Hello World!",10,10,7)
    UnderL("How about that",10,30,11)
    UnderL("OK",10,50,1)    

    ImageGadget(0, 0, 0, 200, 200, ImageID(0))
    EndIf
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
Egypt my love
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: Using different font style with DrawText()

Post by kernadec »

Hi RASHAD
Why not have fun with a marker :lol:
thank the sharing

good night

Code: Select all

CreateImage(0, 200, 200)
Procedure ThickLineXY(X1.i, Y1.i, X2.i, Y2.i, Thickness.i, Color.i) 
  ; Procedure épaisseur de : STARGATE  forum Purebasic Allemand
  Protected Length.i = Sqr((X2-X1)*(X2-X1)+(Y2-Y1)*(Y2-Y1)) 
  Protected I, DeltaX.i, DeltaY.i 
  If Length = 0 
    Circle(X1, Y1, Thickness/2, Color) 
  Else 
    For I = 0 To Length 
      DeltaX = (X2-X1)*I/Length 
      DeltaY = (Y2-Y1)*I/Length 
      Circle(X1+DeltaX, Y1+DeltaY, Thickness/2, Color) 
    Next 
  EndIf
EndProcedure 
Procedure UnderL(Text$,XX,YY,ZZ)
  StartDrawing(ImageOutput(0))
  DrawingMode(#PB_2DDrawing_Transparent)
  For i = 1 To Len(Text$)
    If i = ZZ
      DrawingFont(FontID(0))
      Color = $0102FE
    Else
      DrawingFont(FontID(1))
      Color = 0 
    EndIf 
    Result$ = Mid(Text$, i,1)
    XX = DrawText(XX,YY, Result$, Color)
  Next i
  StopDrawing()
EndProcedure 


LoadFont(0, "Broadway",12,#PB_Font_Underline)
LoadFont(1, "Arial",12)

If OpenWindow(0, 0, 0, 200, 200, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  StartDrawing(ImageOutput(0))
  Box(0, 0, 200, 200, $FFFFFF)
  DrawingMode(#PB_2DDrawing_AlphaBlend) 
  ThickLineXY(5, 20,100, 20, 20, RGBA(255,255,0,20)) 
  ThickLineXY(5, 60,40, 60, 20, RGBA(0,255,0,20)) 
  StopDrawing()
  
  UnderL("Hello World!",10,10,7)
  UnderL("How about that",10,30,11)
  UnderL("OK",10,50,1)   
  
  
  
  
  ImageGadget(0, 0, 0, 200, 200, ImageID(0))
EndIf

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Using different font style with DrawText()

Post by STARGÅTE »

omg
DrawText() wrote:Return value:
Returns the new x position of the text cursor (ie the location just after the printed text).
thank you for opening my eyes, it is easier then:

Code: Select all

DrawtText(X, Y, Text$)
X + TextWidth(Text$)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Using different font style with DrawText()

Post by RASHAD »

Thanks guys

@STARGÅTE
Yes mate that is the point
This tip is specially for Michael Vogel :mrgreen:
Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2808
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Using different font style with DrawText()

Post by Michael Vogel »

RASHAD wrote: This tip is specially for Michael Vogel :mrgreen:
Got it, Rashad, didn't know this trick either, your tip will be very usable... (I am already searching for 'TextWidth' in my codes) :D

The textwidth command will be still an option for me in certain cases :wink: -- anyhow centered text (see button example) and some other stuff (word wrapping) can be done now with different approaches, thanks :)

Michael
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Using different font style with DrawText()

Post by RASHAD »

You are welcome MV

It is much much perfect than TextWidth(Text$)
TextWidth(Text$) is for non-proportional font
Egypt my love
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: Using different font style with DrawText()

Post by kenmo »

:shock: Such a simple trick, but I never realized that DrawText returned anything either!

But RASHAD, why the cryptic parameter names? XX and YY but especially ZZ?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Using different font style with DrawText()

Post by RASHAD »

Hi kenmo
I do not know :P
But at least it is easier than MV naming scheme
Who can use one line to define a variable :P :P

Next some extra staff

Using TabbedTextOut_()

Code: Select all

; LoadClearTypeFont() By STARGÅTE

CreateImage(0, 200, 200)

Procedure LoadClearTypeFont(Name$, Size.i, Style=0)
  Protected LogFont.LOGFONT
  With LogFont
   \lfHeight         = Size
   \lfWidth          = 0
   \lfEscapement     = 0
   \lfOrientation    = 0
   If Style & #PB_Font_Bold       : \lfWeight = 700 : Else : \lfWeight = 400 : EndIf
   If Style & #PB_Font_Italic     : \lfItalic = #True : Else : \lfItalic = #False : EndIf 
   If Style & #PB_Font_Underline  : \lfUnderline = #True : Else : \lfUnderline = #False : EndIf 
   If Style & #PB_Font_StrikeOut  : \lfStrikeOut = #True : Else : \lfStrikeOut = #False : EndIf 
   \lfCharSet        = #DEFAULT_CHARSET
   \lfOutPrecision   = #OUT_DEFAULT_PRECIS   
   \lfClipPrecision  = #CLIP_DEFAULT_PRECIS
   \lfQuality        = #CLEARTYPE_NATURAL_QUALITY
   \lfPitchAndFamily = #DEFAULT_PITCH | #FF_DONTCARE
   PokeS(@\lfFaceName[0], Name$)
  EndWith
  ProcedureReturn CreateFontIndirect_(@LogFont)
EndProcedure

Procedure TabbedText()
      x = 10
      hDC = StartDrawing(ImageOutput(0))
      SelectObject_(hDC,FontID(1))
      ;SelectObject_(hDC,LoadClearTypeFont("Broadway", 20))
      For i = 1 To Len("Hello")
          Result$ = Mid("Hello", i,1)
          xx = TabbedTextOut_(hDC, x, 10, @Result$, 1, 0, 0, 0)
          ;(xx >> 16 & $FFFF)   ;Height of text
          ;xx & $FFFF           ;Width of Text   
          x = x + (xx & $FFFF)
      Next
   
      StopDrawing()
EndProcedure
 
  
    LoadFont(0, "Broadway",12,#PB_Font_Underline)
    LoadFont(1, "Arial",12,#PB_Font_Bold)
 
  If OpenWindow(0, 0, 0, 200, 200, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

    StartDrawing(ImageOutput(0))
      Box(0, 0, 200, 200, $FFFFFF)
    StopDrawing()
   
    TabbedText()

    ImageGadget(0, 0, 0, 200, 200, ImageID(0))
    EndIf
   
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow

Adding align (Left - Center - Right) & Differnt font size

Code: Select all

CreateImage(0, 200, 100)
CopyImage(0,1)

Procedure TextOut(Text$,x,y,schar,Align)
      xs = x
      ys = y
      StartDrawing(ImageOutput(1))
      DrawingMode(#PB_2DDrawing_Transparent)
      For i = 1 To Len(Text$)
        If i = schar
          DrawingFont(FontID(0))
          Color = $0102FE
          y = ys
        Else
          DrawingFont(FontID(1))
          Color = 0
          y = ys + 6
        EndIf
       Result$ = Mid(Text$, i,1)         
       x = DrawText(x,y, Result$, Color)
      Next i
      StopDrawing()
     
      If align = 0
        x = 2
      ElseIf align = 1
        x = (ImageWidth(0) - x + xs)/2
      ElseIf align = 2
        x = ImageWidth(0) - x + xs - 2
      EndIf 
           
      StartDrawing(ImageOutput(0))
      DrawingMode(#PB_2DDrawing_Transparent)
      For i = 1 To Len(Text$)
        If i = schar
          DrawingFont(FontID(0))
          Color = $0102FE
          y = ys
        Else
          DrawingFont(FontID(1))
          Color = 0          
          y = ys + 6
        EndIf
       Result$ = Mid(Text$, i,1)         
       x = DrawText(x,y, Result$, Color)
      Next i     
      StopDrawing()
EndProcedure


    LoadFont(0, "Arial",18,#PB_Font_Underline)
    LoadFont(1, "Arial",12)

  If OpenWindow(0, 0, 0, 400, 200, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

    StartDrawing(ImageOutput(0))
      Box(0, 0, 200, 100, $FFFFFF)
    StopDrawing()
   
    TextOut("Hello World!",10,5,7,0)   ;0 = Left Aligned - 1 = Center Aligned - 2 = Right Aligned
    TextOut("How about That",10,35,11,1)
    TextOut("Is it OK ?",10,65,1,2)
    FreeImage(1)   

    ImageGadget(0, 100,50, 200, 200, ImageID(0),#PB_Image_Border)
    EndIf
   
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
Egypt my love
Post Reply