It is possible to calculate fonts and text for any standard gadget or output, on the fly.

This tool can also turn any CanvasGadget, ImageGadget, ButtonImageGadget, into a ColorButton with multiline text.
You can write text in a image, screen, CanvasGadget, ImageGadget, ButtonImageGadget,
in exactly the same way with a simple function, as in a ButtonGadget,
but automatically fitted, colored, centred, left, right, with offsets, adjustable padding,
min text size, max text size, presetted text size, semi transparency for text.
Additional frames for any text.
Images, icons and background images are supported, with transparency and semi-transparency

With automatic font management.
Existing code does not need to be modified.
Simple new Set...FW commands do everything that is necessary.
Works also with a dynamic GUI on demand.
You found here :
viewtopic.php?f=12&t=76347&p=562434#p562434

This is a feature reduced basic version for the forum, the full feature version is to large.
The full featured version is now with all also enhanced demo codes,
inclusive the FontWizzard_BF enhanced Rabbit File Crypter, in the GFX_Wizzard_BF archive included.
The codes have simply become too large and maintaining different multiple instances too costly.
viewtopic.php?f=12&t=66927
Code: Select all
DeclareModule Font_Wizzard_BF
EnableExplicit
Declare GetTextWidth_FW(text$, font_ID=-1) ; Get text width - Multiline supported
Declare GetTextHeight_FW(text$, font_ID=-1) ; Get text width - Multiline supported
Declare GetTextLineHeight_BF(font_ID=-1) ; Get text line height
Declare GetTextLinesAmount_BF(text$) ; Get amount text lines
Declare Calculate_fitted_Font_FW(text$,
width_rectangle,
height_rectangle,
font$,
font_flag,
padding_x=0,
padding_y=0,
resize_factor.f=1)
Declare SetGadgetText_FW(gadget_ID,
Font$, ; The necessary fonts are managed automatically
font_flag,
text$=" ",
padding_x=0,
padding_y=0,
resize_factor.f=1)
Declare SetGadget_FW(gadget_ID,
Font$, ; The necessary fonts are managed automatically
font_flag,
output_x,
output_y,
gadget_width,
gadget_height,
text$=" ",
padding_x=0,
padding_y=0,
resize_factor.f=1)
Declare SetImageBasedGadgetText_FW(gadget_ID, ; Works at the same with CanvasGadget, ImageGadget, ButtonImageGadget
Font$,
font_flag,
text_color=#Black, ; Alpha channel sensitive - Sample $8000FFFF
text_adjustment=0, ; Text adjustment - 0=center - 1=left - 2=right
background_color=-1, ; Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$=" ",
padding_x=0,
padding_y=0,
resize_factor.f=1)
Declare SetImageBasedGadget_FW(gadget_ID, ; Works at the same with CanvasGadget, ImageGadget, ButtonImageGadget
Font$,
font_flag,
output_x,
output_y,
gadget_width,
gadget_height,
text_color=#Black, ; Alpha channel sensitive - Sample $8000FFFF
text_adjustment=0, ; Text adjustment - 0=center - 1=left - 2=right
background_color=-1, ; Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$=" ",
padding_x=0,
padding_y=0,
resize_factor.f=1)
; Text output position for ImageBasedGadget_FW - You must deactivate again
; This function move the text cursor to create space for an image on a ImageBasedGadget_FW
Declare SetGadgetCursor_FW(SetGadgetCursor_X_FW_=0, SetGadgetCursor_Y_FW_=0)
; Get the actual text output position X for DrawText_FW
Declare GetCursor_X_FW()
; Get the actual text output position Y for DrawText_FW
Declare GetCursor_Y_FW()
; Set the actual text output position X for DrawText_FW
Declare SetCursor_X_FW(GetCursor_X_FW_)
; Set the actual text output position Y for DrawText_FW
Declare SetCursor_Y_FW(GetCursor_Y_FW_)
; Insert an image into the ImageBasedGadget - You must deactivate again
; To deactivate simply set the image ID to -1
Declare AddImage_1_FW(Image_1_ID_FW_=-1,
Image_1_x_FW_=0,
Image_1_y_FW_=0,
Image_1_width_FW_=0,
Image_1_height_FW_=0,
Image_1_alpha_FW_=255)
; Insert an image into the ImageBasedGadget - You must deactivate again
; To deactivate simply set the image ID to -1
Declare AddImage_2_FW(Image_2_ID_FW_=-1,
Image_2_x_FW_=0,
Image_2_y_FW_=0,
Image_2_width_FW_=0,
Image_2_height_FW_=0,
Image_2_alpha_FW_=255)
; Insert an image into the ImageBasedGadget - You must deactivate again
; To deactivate simply set the image ID to -1
Declare AddImage_3_FW(Image_3_ID_FW_=-1,
Image_3_x_FW_=0,
Image_3_y_FW_=0,
Image_3_width_FW_=0,
Image_3_height_FW_=0,
Image_3_alpha_FW_=255)
; Add a frame to all outputs - Padding is automatically adjusted - Alpha channel sensitive - Sample $8000FFFF
Declare AddFrame_FW(AddFrame_Size_FW_=0, AddFrame_Color_FW_=0)
; Activate screen output for DrawText_FW - You must deactivate again
Declare ActivateScreenOutput_FW(screen_output_=0)
; You can also add images to the text output
; - The image size is not limited and can be larger than the text and the background
Declare DrawText_FW(output_ID,
Font$,
font_flag,
output_x,
output_y,
output_width,
output_height,
text_color=#Black,
text_adjustment=0,
background_color=-1,
text$=" ",
padding_x=0,
padding_y=0,
resize_factor.f=1)
Declare PresetFontsize_FW(PresetFontsize_FW_=0) ; Preset a fontsize - You must deactivate again
Declare PresetMinFontsize_FW(PresetMinFontsize_FW_=0) ; Set the smallest font size - You must deactivate again
Declare PresetMaxFontsize_FW(PresetMaxFontsize_FW_=0) ; Sets the largest font size - You must deactivate again
Declare FreeGadget_FW(gadget_ID) ; Free the actual used gadget_FW, font and cached image for this gadget
; Only recommended when the font is no longer needed
Declare FreeAllGadgets_FW(mode=0) ; Free all Gadgets_FW, fonts and cached images used
; Only recommended when the fonts is no longer needed
; mode=1 returns the number of fonts created by the tool
; Otherwise the function returns the number of deleted fonts
EndDeclareModule
Module Font_Wizzard_BF
; Font_Wizzard_BF by Saki
; GFX_Wizzard_BF - For PureBasic
; Author : W. Albus © 2018 - 2020
; Copyright text and references may not be removed
; The use of the GFX-Wizzard_BF is free, but the distribution is reserved for me
; Exceptions require my written permission
; Padding can also be used to create text zoom effects
; The padding can also be specified negatively for very small buttons
; Set a little Padding, this pervent the OS word wrap on multi line ButtonGadgets
#min_padding_x=25
CompilerIf #PB_Compiler_OS=#PB_OS_Linux And Subsystem("gtk2")
#min_padding_y=9
CompilerElse
#min_padding_y=5
CompilerEndIf
Structure gadget_bf
gadget_ID.i
font_ID.i
image_ID.i
EndStructure
Global NewList gadget_bf.gadget_bf()
Global screen_output, PresetFontsize_FW, PresetMinFontsize_FW, PresetMaxFontsize_FW
Global SetGadgetCursor_X_FW, SetGadgetCursor_Y_FW, GetCursor_X_FW, GetCursor_Y_FW
Global Image_1_ID_FW, Image_1_x_FW, Image_1_y_FW, Image_1_width_FW, Image_1_height_FW, Image_1_alpha_FW
Global Image_2_ID_FW, Image_2_x_FW, Image_2_y_FW, Image_2_width_FW, Image_2_height_FW, Image_2_alpha_FW
Global Image_3_ID_FW, Image_3_x_FW, Image_3_y_FW, Image_3_width_FW, Image_3_height_FW, Image_3_alpha_FW
Global AddFrame_Size_FW, AddFrame_Color_FW
Global Rx.d=DesktopResolutionX()
Global Ry.d=DesktopResolutionY()
If Not InitSprite() : MessageRequester(" ERROR", "InitSprite fails"+#LF$+#LF$+"The tool is terminated") : End : EndIf
Procedure.s Replace_LineBreaks_to_LF(text$)
If FindString(text$, #CR$)
text$=ReplaceString(text$, #CRLF$, #LF$)
text$=ReplaceString(text$, #LFCR$, #LF$)
ReplaceString(text$, #CR$, #LF$, #PB_String_InPlace)
EndIf
ProcedureReturn text$
EndProcedure
Procedure GetTextWidth_FW(text$, font_ID=-1)
Protected lf, i, ii, iii
Static image_ID
If Not IsImage(image_ID)
image_ID=CreateImage(#PB_Any, 16, 16)
If Not image_ID : ProcedureReturn -6
EndIf
EndIf
text$=Replace_LineBreaks_to_LF(text$)
lf=CountString(text$, #LF$)
If Not StartDrawing(ImageOutput(image_ID))
ProcedureReturn -4
FreeImage(image_ID)
EndIf
If IsFont(font_ID)
DrawingFont(FontID(font_ID))
EndIf
If lf
lf+1
For i=1 To lf
ii=TextWidth(StringField(text$, i, #LF$))
If ii>iii : iii=ii : EndIf
Next i
Else
iii=TextWidth(text$)
EndIf
StopDrawing()
ProcedureReturn iii
EndProcedure
Procedure GetTextHeight_FW(text$, font_ID=-1)
Protected lf, i
Static image_ID
If Not IsImage(image_ID)
image_ID=CreateImage(#PB_Any, 16, 16)
If Not image_ID : ProcedureReturn -6
EndIf
EndIf
text$=Replace_LineBreaks_to_LF(text$)
lf=CountString(text$, #LF$)+1
If Not StartDrawing(ImageOutput(image_ID))
ProcedureReturn -4
FreeImage(image_ID)
EndIf
If IsFont(font_ID)
DrawingFont(FontID(font_ID))
EndIf
i=TextHeight(" ")*lf
StopDrawing()
ProcedureReturn i
EndProcedure
Procedure GetTextLineHeight_BF(font_ID=-1)
Protected result
Static image_ID
If Not IsImage(image_ID)
image_ID=CreateImage(#PB_Any, 16, 16)
If Not image_ID : ProcedureReturn -6
EndIf
EndIf
If Not StartDrawing(ImageOutput(image_ID))
ProcedureReturn -4
FreeImage(image_ID)
EndIf
If IsFont(font_ID)
DrawingFont(FontID(font_ID))
EndIf
result=TextHeight(" ")
StopDrawing()
ProcedureReturn result
EndProcedure
Procedure GetTextLinesAmount_BF(text$)
text$=Replace_LineBreaks_to_LF(text$)
ProcedureReturn CountString(text$, #LF$)+1
EndProcedure
Procedure Calculate_fitted_Font_FW(text$,
width_rectangle,
height_rectangle,
font$,
font_flag,
padding_x=0,
padding_y=0,
resize_factor.f=1)
If resize_factor<=0 : resize_factor=1 : EndIf
If text$="" : ProcedureReturn 0 : EndIf
Macro common_calculate_fitted_font
font_ID=LoadFont(#PB_Any, font$, max_needed_font_size, font_flag)
text_width=GetTextWidth_FW(text$, font_ID)*resize_factor
text_height=GetTextHeight_FW(text$, font_ID)*resize_factor
EndMacro
Protected font_ID, max_needed_font_size, text_width, text_height
If PresetMinFontsize_FW>1
max_needed_font_size=PresetMinFontsize_FW
EndIf
If PresetFontsize_FW>0
font_ID=LoadFont(#PB_Any, font$, PresetFontsize_FW, font_flag)
Else
Repeat
max_needed_font_size+1
font_ID=LoadFont(#PB_Any, font$, max_needed_font_size, font_flag)
text_width=GetTextWidth_FW(text$, font_ID)*resize_factor
text_height=GetTextHeight_FW(text$, font_ID)*resize_factor
If IsFont(font_ID)
FreeFont(font_ID)
EndIf
If PresetMaxFontsize_FW>0
If max_needed_font_size>PresetMaxFontsize_FW
Break
EndIf
EndIf
If text_width>width_rectangle-padding_x Or text_height>height_rectangle-padding_y
Break
EndIf
ForEver
If max_needed_font_size>1
max_needed_font_size-1
EndIf
font_ID=LoadFont(#PB_Any, font$, max_needed_font_size, font_flag)
EndIf
ProcedureReturn font_ID
EndProcedure
Procedure SetGadget_FW(gadget_ID,
Font$,
font_flag,
output_x,
output_y,
gadget_width,
gadget_height,
text$=" ",
padding_x=0,
padding_y=0,
resize_factor.f=1)
If Not IsGadget(gadget_ID) : ProcedureReturn -25 : EndIf
If resize_factor<=0 : resize_factor=1 : EndIf
If output_x=-1 : output_x=GadgetX(gadget_ID) : EndIf
If output_y=-1 : output_y=GadgetY(gadget_ID) : EndIf
If gadget_width<0 : gadget_width=GadgetWidth(gadget_ID) : EndIf
If gadget_height<0 : gadget_height=GadgetHeight(gadget_ID) : EndIf
If text$="" : text$=" " : EndIf
If gadget_width>0 And gadget_height>0
Protected gadget_width_=gadget_width*Rx
Protected gadget_height_=gadget_height*Ry
padding_x=(padding_x+#min_padding_x)*Rx
padding_y=(padding_y+#min_padding_y)*Ry
Else
gadget_width_=gadget_width
gadget_height_=gadget_height
EndIf
ForEach(gadget_bf())
If gadget_bf()\gadget_ID=gadget_ID
If IsFont(gadget_bf()\font_ID)
FreeFont(gadget_bf()\font_ID)
EndIf
DeleteElement(gadget_bf())
Break
EndIf
Next
Protected font_ID=Calculate_fitted_Font_FW(text$,
gadget_width_,
gadget_height_,
font$,
font_flag,
padding_x,
padding_y,
resize_factor)
If Not IsFont(font_ID) : ProcedureReturn -9 : EndIf
AddElement(gadget_bf())
gadget_bf()\gadget_ID=gadget_ID
gadget_bf()\font_ID=font_ID
ResizeGadget(gadget_ID, output_x, output_y, gadget_width, gadget_height)
SetGadgetFont(gadget_ID, FontID(font_ID))
SetGadgetText(gadget_ID, text$)
ProcedureReturn font_ID
EndProcedure
Procedure SetGadgetText_FW(gadget_ID,
Font$,
font_flag,
text$=" ",
padding_x=0,
padding_y=0,
resize_factor.f=1)
ProcedureReturn SetGadget_FW(gadget_ID,
Font$,
font_flag,
-1,
-1,
-1,
-1,
text$,
padding_x,
padding_y,
resize_factor.f)
EndProcedure
Procedure SetImageBasedGadget_FW(gadget_ID,
Font$,
font_flag,
output_x,
output_y,
gadget_width,
gadget_height,
text_color=#Black,
text_adjustment=0,
background_color=-1,
text$=" ",
padding_x=0,
padding_y=0,
resize_factor.f=1)
If Not IsGadget(gadget_ID) : ProcedureReturn -25 : EndIf
If resize_factor<=0 : resize_factor=1 : EndIf
If output_x=-1 : output_x=GadgetX(gadget_ID) : EndIf
If output_y=-1 : output_y=GadgetY(gadget_ID) : EndIf
If gadget_width<0 : gadget_width=GadgetWidth(gadget_ID) : EndIf
If gadget_height<0 : gadget_height=GadgetHeight(gadget_ID) : EndIf
If text$="" : text$=" " : EndIf
If gadget_width>0 And gadget_height>0
Protected gadget_width_=gadget_width*Rx
Protected gadget_height_=gadget_height*Ry
padding_x=(padding_x+#min_padding_x)*Rx
padding_y=(padding_y+#min_padding_y)*Ry
Else
gadget_width_=gadget_width
gadget_height_=gadget_height
EndIf
If gadget_width_<1 : gadget_width_=1 : EndIf
If gadget_height_<1 : gadget_height_=1 : EndIf
If Alpha(background_color) And GadgetType(gadget_ID)<>#PB_GadgetType_Canvas
Protected image_ID=CreateImage(#PB_Any, gadget_width_, gadget_height_, 32, background_color)
Else
If background_color=-1
image_ID=CreateImage(#PB_Any, gadget_width_, gadget_height_, 24, $FFFFFF)
Else
image_ID=CreateImage(#PB_Any, gadget_width_, gadget_height_, 24, background_color)
EndIf
EndIf
If Not image_ID : ProcedureReturn -6 : EndIf
ForEach(gadget_bf())
If gadget_bf()\gadget_ID=gadget_ID
If IsFont(gadget_bf()\font_ID)
FreeFont(gadget_bf()\font_ID)
EndIf
If IsImage(gadget_bf()\image_ID)
FreeImage(gadget_bf()\image_ID)
EndIf
DeleteElement(gadget_bf())
Break
EndIf
Next
If AddFrame_Size_FW>0
padding_x+AddFrame_Size_FW*2*Rx
padding_y+AddFrame_Size_FW*2*Ry
EndIf
Protected font_ID=Calculate_fitted_Font_FW(text$,
gadget_width_,
gadget_height_,
font$,
font_flag,
padding_x,
padding_y,
resize_factor)
If Not IsFont(font_ID) : ProcedureReturn -9 : EndIf
If resize_factor<=0 : resize_factor=1 : EndIf
AddElement(gadget_bf())
gadget_bf()\gadget_ID=gadget_ID
gadget_bf()\font_ID=font_ID
gadget_bf()\image_ID=image_ID
ResizeGadget(gadget_ID, output_x, output_y, gadget_width, gadget_height)
Protected i, ii, iii, line_width, string_field$
Protected lf=CountString(Replace_LineBreaks_to_LF(text$), #LF$)
Protected text_height=GetTextHeight_FW(text$, font_ID)
If text_adjustment
Protected text_width=GetTextWidth_FW(text$, font_ID)
EndIf
Protected GetCursor_X_1_FW
Protected GetCursor_Y_1_FW
Macro text_adjustment_1(text_line)
Select text_adjustment
Case 0 ; centered
GetCursor_X_1_FW=(gadget_width_/2-line_width/2)+SetGadgetCursor_X_FW
Case 1 ; Left
GetCursor_X_1_FW=(gadget_width_/2-text_width/2)+SetGadgetCursor_X_FW
Case 2 ; Right
GetCursor_X_1_FW=(gadget_width_/2+text_width/2-line_width)+SetGadgetCursor_X_FW
EndSelect
GetCursor_Y_1_FW=((gadget_height_-text_height)/2+ii)+SetGadgetCursor_Y_FW
DrawText(GetCursor_X_1_FW,
GetCursor_Y_1_FW,
text_line,
text_color)
EndMacro
Protected temp_image_1_ID
Macro add_image_1_fw
If Image_1_Width_FW>0 And Image_1_Height_FW>0
If IsImage(Image_1_ID_FW)
temp_image_1_ID=CopyImage(Image_1_ID_FW, #PB_Any)
If Not temp_image_1_ID
FreeImage(image_ID)
ProcedureReturn-6
EndIf
ResizeImage(temp_image_1_ID,
Image_1_Width_FW*Rx,
Image_1_Height_FW*Ry)
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawAlphaImage(ImageID(temp_image_1_ID),
Image_1_x_FW*Rx,
Image_1_y_FW*Ry,
Image_1_alpha_FW)
FreeImage(temp_image_1_ID)
EndIf
EndIf
EndMacro
Protected temp_image_2_ID
Macro add_image_2_fw
If Image_2_Width_FW>0 And Image_2_Height_FW>0
If IsImage(Image_2_ID_FW)
temp_image_2_ID=CopyImage(Image_2_ID_FW, #PB_Any)
If Not temp_image_2_ID
FreeImage(image_ID)
ProcedureReturn-6
EndIf
ResizeImage(temp_image_2_ID,
Image_2_Width_FW*Rx,
Image_2_Height_FW*Ry)
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawAlphaImage(ImageID(temp_image_2_ID),
Image_2_x_FW*Rx,
Image_2_y_FW*Ry,
Image_2_alpha_FW)
FreeImage(temp_image_2_ID)
EndIf
EndIf
EndMacro
Protected temp_image_3_ID
Macro add_image_3_fw
If Image_3_Width_FW>0 And Image_3_Height_FW>0
If IsImage(Image_3_ID_FW)
temp_image_3_ID=CopyImage(Image_3_ID_FW, #PB_Any)
If Not temp_image_3_ID
FreeImage(image_ID)
ProcedureReturn-6
EndIf
ResizeImage(temp_image_3_ID,
Image_3_Width_FW*Rx,
Image_3_Height_FW*Ry)
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawAlphaImage(ImageID(temp_image_3_ID),
Image_3_x_FW*Rx,
Image_3_y_FW*Ry,
Image_3_alpha_FW)
FreeImage(temp_image_3_ID)
EndIf
EndIf
EndMacro
If Not Alpha(text_color)
text_color|$FF000000
EndIf
If Not StartDrawing(ImageOutput(image_ID))
FreeFont(font_ID)
FreeImage(image_ID)
ProcedureReturn -5
EndIf
If Alpha(background_color) And background_color<>-1
If GadgetType(gadget_ID)<>#PB_GadgetType_Canvas
DrawingMode(#PB_2DDrawing_AllChannels)
Else
DrawingMode(#PB_2DDrawing_AlphaBlend)
EndIf
EndIf
If background_color<>-1
Box(0, 0, gadget_width_, gadget_height_, background_color)
EndIf
add_image_1_fw
add_image_2_fw
add_image_3_fw
If AddFrame_Size_FW
If Alpha(AddFrame_Color_FW)
DrawingMode(#PB_2DDrawing_Outlined|#PB_2DDrawing_AlphaBlend)
Else
DrawingMode(#PB_2DDrawing_Outlined)
EndIf
For iii=0 To (AddFrame_Size_FW-1)*Rx
Box(iii, iii, gadget_width_-iii*2, gadget_height_-iii*2, AddFrame_Color_FW)
Next iii
EndIf
DrawingFont(FontID(font_ID))
DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
Protected line_height=TextHeight(" ")
If lf
lf+1
For i=1 To lf
string_field$=StringField(text$, i, #LF$)
line_width=TextWidth(string_field$)
text_adjustment_1(string_field$)
ii+line_height
Next i
Else
text_adjustment=0
line_width=TextWidth(text$)
text_adjustment_1(text$)
EndIf
StopDrawing()
If GadgetType(gadget_ID)=#PB_GadgetType_Image
SetGadgetState(gadget_ID, ImageID(image_ID)) ; ImageGadget
Else
SetGadgetAttribute(gadget_ID, #PB_Button_Image, ImageID(image_ID)) ; ButtonImageGadget, CanvasGadget
EndIf
ProcedureReturn font_ID
EndProcedure
Procedure SetImageBasedGadgetText_FW(gadget_ID,
Font$,
font_flag,
text_color=#Black,
text_adjustment=0,
background_color=-1,
text$=" ",
padding_x=0,
padding_y=0,
resize_factor.f=1)
ProcedureReturn SetImageBasedGadget_FW(gadget_ID,
Font$,
font_flag,
-1,
-1,
-1,
-1,
text_color,
text_adjustment,
background_color,
text$,
padding_x,
padding_y,
resize_factor.f)
EndProcedure
Procedure AddFrame_FW(AddFrame_Size_FW_=0, AddFrame_Color_FW_=0)
AddFrame_Size_FW=AddFrame_Size_FW_
AddFrame_Color_FW=AddFrame_Color_FW_
ProcedureReturn 1
EndProcedure
Procedure ActivateScreenOutput_FW(screen_output_=0)
screen_output=screen_output_
EndProcedure
Procedure DrawText_FW(output_ID,
Font$,
font_flag,
output_x,
output_y,
output_width,
output_height,
text_color=#Black,
text_adjustment=0,
background_color=-1,
text$=" ",
padding_x=0,
padding_y=0,
resize_factor.f=1)
If Not screen_output
If IsImage(output_ID) ; Image
Protected image=1
ElseIf IsSprite(output_ID) ; PB sprite
Protected sprite=1
ElseIf IsGadget(output_ID) ; Canvas
Protected canvas=1
ElseIf IsWindow(output_ID) ; Window
Protected window=1
Else
ProcedureReturn -11
EndIf
EndIf
If resize_factor<=0 : resize_factor=1 : EndIf
If text$="" : text$=" " : EndIf
If output_width<1 : output_width=1 : EndIf
If output_height<1 : output_height=1 : EndIf
padding_x+#min_padding_x
padding_y+#min_padding_y
If canvas
padding_x*Rx
padding_y*Ry
EndIf
If AddFrame_Size_FW>0
padding_x+AddFrame_Size_FW*2
padding_y+AddFrame_Size_FW*2
EndIf
Protected font_ID=Calculate_fitted_Font_FW(text$,
output_width,
output_height,
font$,
font_flag,
padding_x,
padding_y,
resize_factor)
If Not IsFont(font_ID) : ProcedureReturn -9 : EndIf
Protected i, ii, iii, line_width, string_field$
Protected lf=CountString(Replace_LineBreaks_to_LF(text$), #LF$)
Protected text_height=GetTextHeight_FW(text$, font_ID)
If text_adjustment
Protected text_width=GetTextWidth_FW(text$, font_ID)
EndIf
Protected GetCursor_X
Protected GetCursor_Y
Macro text_adjustment_2(text_line)
Select text_adjustment
Case 0 ; centered
GetCursor_X=(output_width/2-line_width/2)+output_x
Case 1 ; Left
GetCursor_X=(output_width/2-text_width/2)+output_x
Case 2 ; Right
GetCursor_X=(output_width/2+text_width/2-line_width)+output_x
EndSelect
GetCursor_Y=((output_height-text_height)/2+ii)+output_y
If Not init
init=1
GetCursor_X_FW=GetCursor_X+text_width
GetCursor_Y_FW=GetCursor_Y+text_height
EndIf
DrawText(GetCursor_X,
GetCursor_Y,
text_line,
text_color)
EndMacro
Protected temp_image_1_ID
Macro add_image_1_fw_1
If Image_1_Width_FW>0 And Image_1_Height_FW>0
If IsImage(Image_1_ID_FW)
temp_image_1_ID=CopyImage(Image_1_ID_FW, #PB_Any)
If Not temp_image_1_ID
ProcedureReturn-6
EndIf
ResizeImage(temp_image_1_ID,
Image_1_Width_FW,
Image_1_Height_FW)
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawAlphaImage(ImageID(temp_image_1_ID),
Image_1_x_FW+output_x,
Image_1_y_FW+output_y,
Image_1_alpha_FW)
FreeImage(temp_image_1_ID)
EndIf
EndIf
EndMacro
Protected temp_image_2_ID
Macro add_image_2_fw_1
If Image_2_Width_FW>0 And Image_2_Height_FW>0
If IsImage(Image_2_ID_FW)
temp_image_2_ID=CopyImage(Image_2_ID_FW, #PB_Any)
If Not temp_image_2_ID
ProcedureReturn-6
EndIf
ResizeImage(temp_image_2_ID,
Image_2_Width_FW,
Image_2_Height_FW)
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawAlphaImage(ImageID(temp_image_2_ID),
Image_2_x_FW+output_x,
Image_2_y_FW+output_y,
Image_2_alpha_FW)
FreeImage(temp_image_2_ID)
EndIf
EndIf
EndMacro
Protected temp_image_3_ID
Macro add_image_3_fw_1
If Image_3_Width_FW>0 And Image_3_Height_FW>0
If IsImage(Image_3_ID_FW)
temp_image_3_ID=CopyImage(Image_3_ID_FW, #PB_Any)
If Not temp_image_3_ID
ProcedureReturn-6
EndIf
ResizeImage(temp_image_3_ID,
Image_3_Width_FW,
Image_3_Height_FW)
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawAlphaImage(ImageID(temp_image_3_ID),
Image_3_x_FW+output_x,
Image_3_y_FW+output_y,
Image_3_alpha_FW)
FreeImage(temp_image_3_ID)
EndIf
EndIf
EndMacro
If screen_output
Protected result=StartDrawing(ScreenOutput())
Else
If Not Alpha(text_color)
text_color|$FF000000
EndIf
If image
result=StartDrawing(ImageOutput(output_ID))
ElseIf sprite
result=StartDrawing(SpriteOutput(output_ID))
ElseIf canvas
result=StartDrawing(CanvasOutput(output_ID))
Else
result=StartDrawing(WindowOutput(output_ID))
EndIf
EndIf
If Not result
FreeFont(font_ID)
ProcedureReturn -11
EndIf
If Not screen_output
If Alpha(background_color) And background_color<>-1
DrawingMode(#PB_2DDrawing_AllChannels)
EndIf
EndIf
If background_color<>-1
Box(output_x, output_y, output_width, output_height, background_color)
EndIf
add_image_1_fw_1
add_image_2_fw_1
add_image_3_fw_1
If AddFrame_Size_FW
If Alpha(AddFrame_Color_FW)
DrawingMode(#PB_2DDrawing_Outlined|#PB_2DDrawing_AlphaBlend)
Else
DrawingMode(#PB_2DDrawing_Outlined)
EndIf
For iii=0 To (AddFrame_Size_FW-1)
Box(output_x+iii,output_y+iii, output_width-iii*2, output_height-iii*2, AddFrame_Color_FW)
Next iii
EndIf
DrawingFont(FontID(font_ID))
DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
Protected line_height=TextHeight(" ")
Protected init
If lf
lf+1
For i=1 To lf
string_field$=StringField(text$, i, #LF$)
line_width=TextWidth(string_field$)
text_adjustment_2(string_field$)
ii+line_height
Next i
Else
text_adjustment=0
line_width=TextWidth(text$)
text_adjustment_2(text$)
EndIf
StopDrawing()
FreeFont(font_ID)
ProcedureReturn 1
EndProcedure
Procedure SetGadgetCursor_FW(SetGadgetCursor_X_FW_=0, SetGadgetCursor_Y_FW_=0)
SetGadgetCursor_X_FW=SetGadgetCursor_X_FW_
SetGadgetCursor_Y_FW=SetGadgetCursor_Y_FW_
ProcedureReturn 1
EndProcedure
Procedure GetCursor_X_FW()
ProcedureReturn GetCursor_X_FW
EndProcedure
Procedure GetCursor_Y_FW()
ProcedureReturn GetCursor_Y_FW
EndProcedure
Procedure SetCursor_X_FW(GetCursor_X_FW_)
GetCursor_X_FW=GetCursor_X_FW_
ProcedureReturn GetCursor_X_FW
EndProcedure
Procedure SetCursor_Y_FW(GetCursor_Y_FW_)
GetCursor_Y_FW=GetCursor_Y_FW_
ProcedureReturn GetCursor_Y_FW
EndProcedure
Procedure AddImage_1_FW(Image_1_ID_FW_=-1,
Image_1_x_FW_=0,
Image_1_y_FW_=0,
Image_1_width_FW_=0,
Image_1_height_FW_=0,
Image_1_alpha_FW_=255)
Image_1_ID_FW=Image_1_ID_FW_
Image_1_x_FW=Image_1_x_FW_
Image_1_y_FW=Image_1_y_FW_
Image_1_width_FW=Image_1_width_FW_
Image_1_height_FW=Image_1_height_FW_
Image_1_alpha_FW=Image_1_alpha_FW_
ProcedureReturn Image_1_ID_FW
EndProcedure
Procedure AddImage_2_FW(Image_2_ID_FW_=-1,
Image_2_x_FW_=0,
Image_2_y_FW_=0,
Image_2_width_FW_=0,
Image_2_height_FW_=0,
Image_2_alpha_FW_=255)
Image_2_ID_FW=Image_2_ID_FW_
Image_2_x_FW=Image_2_x_FW_
Image_2_y_FW=Image_2_y_FW_
Image_2_width_FW=Image_2_width_FW_
Image_2_height_FW=Image_2_height_FW_
Image_2_alpha_FW=Image_2_alpha_FW_
ProcedureReturn Image_1_ID_FW
EndProcedure
Procedure AddImage_3_FW(Image_3_ID_FW_=-1,
Image_3_x_FW_=0,
Image_3_y_FW_=0,
Image_3_width_FW_=0,
Image_3_height_FW_=0,
Image_3_alpha_FW_=255)
Image_3_ID_FW=Image_3_ID_FW_
Image_3_x_FW=Image_3_x_FW_
Image_3_y_FW=Image_3_y_FW_
Image_3_width_FW=Image_3_width_FW_
Image_3_height_FW=Image_3_height_FW_
Image_3_alpha_FW=Image_3_alpha_FW_
ProcedureReturn Image_3_ID_FW
EndProcedure
Procedure PresetFontsize_FW(PresetFontsize_FW_=0)
PresetFontsize_FW=PresetFontsize_FW_
ProcedureReturn PresetFontsize_FW
EndProcedure
Procedure PresetMinFontsize_FW(PresetMinFontsize_FW_=0)
PresetMinFontsize_FW=PresetMinFontsize_FW_
ProcedureReturn PresetMinFontsize_FW
EndProcedure
Procedure PresetMaxFontsize_FW(PresetMaxFontsize_FW_=0)
PresetMaxFontsize_FW=PresetMaxFontsize_FW_
ProcedureReturn PresetMaxFontsize_FW
EndProcedure
Procedure FreeGadget_FW(gadget_ID)
ForEach(gadget_bf())
If (gadget_bf()\gadget_ID)=gadget_ID
If IsFont(gadget_bf()\font_ID)
FreeFont(gadget_bf()\font_ID)
EndIf
If IsImage(gadget_bf()\image_ID)
FreeImage(gadget_bf()\image_ID)
EndIf
If IsGadget(gadget_bf()\gadget_ID)
FreeGadget(gadget_bf()\gadget_ID)
EndIf
DeleteElement(gadget_bf())
ProcedureReturn 1
EndIf
Next
ProcedureReturn 0
EndProcedure
Procedure FreeAllGadgets_FW(mode=0)
Protected result
If Not mode
ForEach(gadget_BF())
If IsFont(gadget_bf()\font_ID)
FreeFont(gadget_bf()\font_ID)
EndIf
If IsImage(gadget_bf()\image_ID)
FreeImage(gadget_bf()\image_ID)
EndIf
If IsGadget(gadget_bf()\gadget_ID)
FreeGadget(gadget_bf()\gadget_ID)
result+1
EndIf
Next
ClearList(gadget_bf())
ProcedureReturn result
ElseIf mode=1
ProcedureReturn ListSize(gadget_bf())
EndIf
EndProcedure
EndModule
UseModule Font_Wizzard_BF
; ################################# Demo part #####################################
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
UsePNGImageDecoder()
UseJPEGImageDecoder()
Define image_1_ID=LoadImage(#PB_Any, #PB_Compiler_Home+"Examples/3D/Data/Water/Foam.png")
Define image_2_ID=LoadImage(#PB_Any, #PB_Compiler_Home+"Examples/3D/Data/Textures/grass1.png")
Define window_ID=OpenWindow(#PB_Any, 0, 0, 960, 470,
"Font_Wizzard_BF",
#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_Invisible)
Define text$="Font Wizzard_BF"+#LF$+"This is a ButtonGadget"+#LF$+"Try any text"
Define button_1_ID=ButtonGadget(#PB_Any, 20, 20, 280, 100, text$, #PB_Button_MultiLine)
SetGadgetText_FW(button_1_ID,
"Georgia",
#PB_Font_Bold,
text$) ; text$
Define text$="Hello World"+#LF$+"Automatically fitted text"+#LF$+"This is a ButtonImageGadget"+#LF$+"Try any text"
Define button_2_ID=ButtonImageGadget(#PB_Any, 330, 20, 400, 140, 0)
SetImageBasedGadgetText_FW(button_2_ID,
"Tahoma",
#PB_Font_Bold,
#Red, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
-1, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
5, ; Padding x
5) ; Padding y
Define text$="Hello World"+#LF$+"Automatically fitted text"+#LF$+"This is a little ButtonGadget"
Define button_3_ID=ButtonGadget(#PB_Any, 0, 0, 0, 0, "", #PB_Button_MultiLine)
SetGadget_FW(button_3_ID,
"Arial",
#PB_Font_Bold,
60, ; output_x
140, ; output_y
160, ; gadget_width
50, ; gadget_height
text$) ; text$
Define text$="Hello World"+#LF$+"Automatically fitted text"+#LF$+"This is a ButtonImageGadget"
Define button_4_ID=ButtonImageGadget(#PB_Any, 20, 210, 300, 90, 0)
SetImageBasedGadgetText_FW(button_4_ID,
"Tahoma",
#PB_Font_Bold,
#Blue, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
$8000FFFF, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
0, ; Padding x
5) ; Padding y
Define text$="Hello World"+#LF$+"This is a ImageGadget"+#LF$+"Try any text"
Define imagegadget_1_ID=ImageGadget(#PB_Any, 0, 0, 0, 0, 0) ; , #PB_Image_Border)
AddImage_2_FW(image_2_ID,
-30, ; x pos
0, ; y pos
100, ; Width
100, ; Height
255) ; Alpha
AddImage_3_FW(image_2_ID,
300,
20,
50,
50,
255)
AddFrame_FW(3, #Red) ; Add a frame
SetImageBasedGadget_FW(imagegadget_1_ID,
"Arial",
#PB_Font_Bold,
360, ; output_x
190, ; output_y
350, ; gadget_width
100, ; gadget_height
#Red, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
#Yellow, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$) ; text$
AddFrame_FW() ; Disable the frame
AddImage_2_FW() ; Disable the images
AddImage_3_FW()
Define text$=#LF$+"Hello World"+#LF$+"Automatically fitted text"+#LF$+"This is a CanvasGadget"
Define canvas_1_ID=CanvasGadget(#PB_Any,
10, ; output_x
310, ; output_y
330, ; gadget_width
140, ; gadget_height
#PB_Canvas_Border) ; Border
SetImageBasedGadgetText_FW(canvas_1_ID,
"",
#PB_Font_Bold,
#Blue, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
#White, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
0, ; padding_x
0) ; padding_y
AddImage_3_FW(image_2_ID,
275*DesktopResolutionX(), ; x pos
15*DesktopResolutionY(), ; y pos
50*DesktopResolutionX(), ; Width
50*DesktopResolutionY(), ; Height
255) ; Alpha
AddFrame_FW(2*DesktopResolutionX(), #Blue) ; Add a frame
text$="Created"+#LF$+"with DrawText_FW"
DrawText_FW(canvas_1_ID,
"", ; Font
#PB_Font_Bold, ; Font flag
5*DesktopResolutionX(), ; Output X
5*DesktopResolutionY(), ; Output Y
100*DesktopResolutionX(), ; Text width
30*DesktopResolutionY(), ; Text height
#Blue, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
#Green, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
-5*DesktopResolutionX(), ; padding_x
0*DesktopResolutionX()) ; padding_y
AddImage_3_FW() ; Disable again
AddFrame_FW() ; Disable the frame
DrawText_FW(canvas_1_ID,
"", ; Font
#PB_Font_Bold, ; Font flag
(GadgetWidth(canvas_1_ID)-108)*DesktopResolutionX(), ; Output X
5*DesktopResolutionY(), ; Output Y
100*DesktopResolutionX(), ; Text width
30*DesktopResolutionY(), ; Text height
#Red, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
-1, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
0, ; padding_x
0) ; padding_y
AddImage_3_FW(image_2_ID,
320, ; x pos
30, ; y pos
50, ; Width
50, ; Height
255) ; Alpha
Define text$="Hello World"+#LF$+#LF$+"ButtonImageGadget"+#LF$+"Also with included images"
Define button_5_ID=ButtonImageGadget(#PB_Any, 360, 320, 370, 130, 0)
SetImageBasedGadgetText_FW(button_5_ID,
"Georgia",
#PB_Font_Bold,
#Blue, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
-1, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
30, ; Padding x
10) ; Padding y
AddImage_3_FW() ; Disable again
Define output_ID=CanvasGadget(#PB_Any,
750, ; Output X
20, ; Output Y
190, ; gadget_width
430) ; gadget_height
AddImage_1_FW(image_1_ID,
0*DesktopResolutionX(), ; Offset X
0*DesktopResolutionY(), ; Offset Y
GadgetWidth(button_5_ID)*DesktopResolutionX(), ; Width
GadgetHeight(button_5_ID)*DesktopResolutionY(), ; Height
50)
AddImage_3_FW(image_2_ID,
-30*DesktopResolutionX(), ; Offset X
0*DesktopResolutionY(), ; Offset Y
GadgetWidth(button_5_ID)/3*DesktopResolutionX(), ; Width
GadgetHeight(button_5_ID)*DesktopResolutionY(), ; Height
255)
text$="DrawText_FW"
DrawText_FW(output_ID,
"", ; Font
#PB_Font_Bold, ; Font flag
0*DesktopResolutionX(), ; Output X
0*DesktopResolutionY(), ; Output Y
190*DesktopResolutionX(), ; Text width
60*DesktopResolutionY(), ; Text height
#Blue, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
-1, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
0, ; padding_x
0, ; padding_y
1) ; Resize Factor
AddImage_1_FW() ; Free the images
AddImage_2_FW()
AddImage_3_FW()
text$="Cool"+#LF$+
"fitted text"+#LF$+
"All channels"+#LF$+
"Inheritable cursor"+#LF$
DrawText_FW(output_ID,
"", ; Font
#PB_Font_Bold, ; Font flag
0, ; Output X
GetCursor_Y_FW(), ; Output Y
190*DesktopResolutionX(), ; Text width
100*DesktopResolutionY(), ; Text height
#Blue, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
-1, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
35, ; padding_x
0, ; padding_y
1) ; Resize Factor
text$="It's cool man"+#LF$+"This is DrawText_FW"
Define i, ii
For i=0 To 1
ii=GetCursor_Y_FW()
DrawText_FW(output_ID,
"", ; Font
#PB_Font_Bold, ; Font flag
0, ; Output X
ii, ; Output Y
190*DesktopResolutionX(), ; Text width
35*DesktopResolutionY(), ; Text height
#Red, ; Text color - Alpha channel sensitive - Sample $8000FFFF
Bool(i%2), ; Text adjustment - 0=center - 1=left - 2=right
#Green, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
10, ; padding_x
0) ; padding_y
Next i
AddFrame_FW(2*DesktopResolutionX(), #Blue) ; Add a frame
For i=0 To 1
ii=GetCursor_Y_FW()+10*DesktopResolutionX()
DrawText_FW(output_ID,
"", ; Font
#PB_Font_Bold, ; Font flag
0, ; Output X
ii, ; Output Y
190*DesktopResolutionX(), ; Text width
40*DesktopResolutionY(), ; Text height
#Blue, ; Text color - Alpha channel sensitive - Sample $8000FFFF
Bool(i%2)+1, ; Text adjustment - 0=center - 1=left - 2=right
#Red, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
10, ; padding_x
0) ; padding_y
Next i
AddFrame_FW() ; Disable the frame
text$="This is a "+#LF$+"CanvasGadget"+#LF$+"Works also on screen,"+#LF$+"images, sprites, window"
ii=GetCursor_Y_FW()
DrawText_FW(output_ID,
"", ; Font
#PB_Font_Bold, ; Font flag
0*DesktopResolutionX(), ; Output X
10*DesktopResolutionY()+GetCursor_Y_FW(), ; Output Y
190*DesktopResolutionX(), ; Text width
120*DesktopResolutionY(), ; Text height
#Blue, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
-1, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
0, ; padding_x
0) ; padding_y
AddImage_1_FW(image_1_ID,
0, ; x pos
0, ; y pos
GadgetWidth(button_5_ID), ; Width
GadgetHeight(button_5_ID), ; Height
50)
AddImage_2_FW(image_2_ID,
-30,
0,
GadgetWidth(button_5_ID)/3,
GadgetHeight(button_5_ID),
255)
AddImage_3_FW(image_2_ID,
300,
50,
GadgetWidth(button_5_ID)/8,
GadgetHeight(button_5_ID)/3,
255)
text$="This is a "+#LF$+"ButtonImageGadget"+#LF$+"Works also on screen,"+#LF$+"images, sprites, window"
SetImageBasedGadgetText_FW(button_5_ID,
"Georgia",
#PB_Font_Bold,
#Blue, ; Text color - Alpha channel sensitive - Sample $8000FFFF
0, ; Text adjustment - 0=center - 1=left - 2=right
-1, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
text$, ; text$
30, ; Padding x
10) ; Padding y
HideWindow(window_ID, 0)
Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
CompilerEndIf