copied code

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

copied code

Post by Saki »

Or so :wink:

Image

Code updated 01_01_2021
DPI aware supported for high quality text outputs on all desktop scalings - Razor knife sharp ! :D
Clipping added.
Code simplifications and enhancements for easy using.

Code updated 02_01_2021
Special new FontWizzard_Direct_BF Module with OpenGLGadget support created
for all OS and added here directly runnable :wink:
Have fun :D

(Linux can not grab the PB Logo from the PB Program path, but it works also fine)
In the row : MacOS - Linux - Windows
Image

Code: Select all

; FontWizzard_Direct_BF

DeclareModule FontWizzard_Direct_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
  
  ; 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)
  
  Declare OpenGLGadgetFlipBuffers_FW(gadget_ID) ; Flip Buffers OpenGLGadget
  
  Declare SetColorOpenGLGadget_FW(background_color) ; OpenGLGadget color
  
  ; DrawText_FW create as output a image - You must deactivate again
  ; 1 create a image
  ; 2 create a vertical mirrored image
  ; 3 create a horizontal mirrored image
  ; 4 create a horizontal and vertical mirrored image
  Declare CreateImage_FW(CreateImage_FW_=0)
  
  ; You can also add images to the text output
  ; The additional image size is not limited and can be larger than the text and the background
  ; If CreateImage_FW activated, the parameter output_ID is ignored
  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)
  
  ; Create and output a DrawTextImage_FW on a OpenGLGadget
  ; Set CreateImage_FW above for mirrored outputs
  Declare CreateImage_FW(CreateImage_FW_=0)
  
  Declare DrawGLTextImage_FW(gadget_ID,           ; Gadget ID
                             font$,               ; Font
                             font_flag,           ; Font flag
                             output_x,            ; Output x
                             output_y,            ; Output y
                             output_width,        ; Output width
                             output_height,       ; Output height
                             text_color=#White,   ; Text color - Alpha channel sensitive
                             text_adjustment=1,   ; Text adjustment - 0=center - 1=left - 2=right
                             background_color=-1, ; Background color - Alpha channel sensitive
                             text$="",            ; Text
                             padding_x=0,         ; Padding x
                             padding_y=0,         ; Padding y
                             resize_factor.f=1.0) ; Resize factor - float
  
  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
  
EndDeclareModule

Module FontWizzard_Direct_BF
  ; FontWizzard_Direct_GL_BF - All OS - by Saki
  ; For outputs on screen, images, canvas, OpenGLGadget
  
  ; 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, CreateImage_FW, FromDrawGLTextImage_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 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 OpenGLGadgetFlipBuffers_FW(gadget_ID) ; Flip Buffers OpenGLGadget
    SetGadgetAttribute(gadget_ID, #PB_OpenGL_FlipBuffers, #True)
    ProcedureReturn 1
  EndProcedure
  
  Procedure SetColorOpenGLGadget_FW(background_color) ; OpenGLGadget color
    glEnable_(#GL_BLEND)
    glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
    glClearColor_(Red(background_color)/255, Green(background_color)/255, Blue(background_color)/255, 1)
    glClear_(#GL_COLOR_BUFFER_BIT)
    ProcedureReturn 1
  EndProcedure
  
  Procedure CreateImage_FW(CreateImage_FW_=0)
    CreateImage_FW=CreateImage_FW_
    ProcedureReturn CreateImage_FW
  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 CreateImage_FW
      Protected image=2
    Else
      If Not screen_output
        If IsImage(output_ID) ; Image
          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
    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
      
      Protected image_ID=CreateImage(#PB_Any, output_width, output_height, 32, #PB_Image_Transparent)
      
      If image=2 : output_ID=image_ID : 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
    
    Protected AddFrame_Color_FW_=AddFrame_Color_FW
    If Not Alpha(AddFrame_Color_FW_) : AddFrame_Color_FW_|$FF000000 : EndIf
    CompilerIf #PB_Compiler_OS<>#PB_OS_Windows
      If FromDrawGLTextImage_FW
        text_color=RGBA(Blue(text_color), Green(text_color), Red(text_color), Alpha(text_color))
        background_color=RGBA(Blue(background_color), Green(background_color), Red(background_color), Alpha(background_color))
        AddFrame_Color_FW_=RGBA(Blue(AddFrame_Color_FW_), Green(AddFrame_Color_FW_), Red(AddFrame_Color_FW_), Alpha(AddFrame_Color_FW_))
      EndIf
    CompilerEndIf
    
    If Not Alpha(background_color) : background_color=$FF000000|background_color : EndIf
    
    FromDrawGLTextImage_FW=1
    
    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)
    
    If image=2
      ProcedureReturn image_ID
    EndIf
    ProcedureReturn 1
  EndProcedure
  
  Procedure DrawGLTextImage_FW(gadget_ID,         ; Gadget ID
                               font$,             ; Font
                               font_flag,         ; Font flag
                               output_x,          ; Output x
                               output_y,          ; Output y
                               output_width,      ; Output width
                               output_height,     ; Output height
                               text_color=#White, ; Text color - Alpha channel sensitive
                               text_adjustment=1, ; Text adjustment - 0=center - 1=left - 2=right
                               background_color=-1, ; Background color - Alpha channel sensitive
                               text$="",            ; Text
                               padding_x=0,         ; Padding x
                               padding_y=0,         ; Padding y
                               resize_factor.f=1.0) ; Resize factor - float
    
    If output_x<0 : Protected output_x_=output_x : output_x=0 : EndIf : If output_y<0 : Protected output_y_=output_y : output_y=0 : EndIf
    output_width*DesktopResolutionX() : output_height*DesktopResolutionX()
    
    CompilerIf #PB_Compiler_OS=#PB_OS_Windows
      CreateImage_FW(1)
    CompilerElse
      CreateImage_FW(2)
    CompilerEndIf
    
    If Not Alpha(background_color) : background_color=$FF000000|background_color : EndIf
    
    FromDrawGLTextImage_FW=1
    
    Protected image_ID=DrawText_FW(-1,               ; output_ID 
                                   font$,            ; Font
                                   font_flag,        ; Font flag
                                   output_x_,        ; Output x pos
                                   output_y_,        ; Output y pos
                                   output_width,     ; Output width
                                   output_height,    ; Output height
                                   text_color,       ; Text color
                                   text_adjustment,  ; Text adjustment - 0=center - 1=left - 2=right
                                   background_color, ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
                                   Text$,            ; Text
                                   padding_x,        ; Padding x
                                   padding_y,        ; Padding y
                                   resize_factor)    ; resize_factor.f
    
    FromDrawGLTextImage_FW=1
    
    If Not IsImage(image_ID) : ProcedureReturn 0 : EndIf
    
    Protected temporary_image_ID=CreateImage(#PB_Any, output_width, output_height, 32, #PB_Image_Transparent) 
    If Not temporary_image_ID
      FreeImage(image_ID)
      ProcedureReturn 0
    EndIf
    If Not StartVectorDrawing(ImageVectorOutput(temporary_image_ID))
      FreeImage(image_ID)
      FreeImage(temporary_image_ID)
    EndIf
    Select CreateImage_FW
      Case 2
        FlipCoordinatesY(VectorOutputHeight()/2)
      Case 3
        FlipCoordinatesX(VectorOutputWidth()/2)
      Case 4   
        FlipCoordinatesX(VectorOutputWidth()/2)
        FlipCoordinatesY(VectorOutputHeight()/2)
    EndSelect
    MovePathCursor(0, 0)
    DrawVectorImage(ImageID(image_ID))
    StopVectorDrawing()
    
    If StartDrawing(ImageOutput(temporary_image_ID))
      glPushMatrix_()
      glOrtho_(0, GadgetWidth(gadget_ID), GadgetHeight(gadget_ID), 0, 0, 1)
      glRasterPos2i_(output_x, output_y+output_height/DesktopResolutionX())
      glDrawPixels_(output_width, output_height, #GL_BGRA_EXT, #GL_UNSIGNED_BYTE, DrawingBuffer())
      glPopMatrix_()
      StopDrawing()
    Else
      FreeImage(image_ID)
      FreeImage(temporary_image_ID)
      ProcedureReturn 0
    EndIf
    FreeImage(image_ID)
    FreeImage(temporary_image_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
  
EndModule
UseModule FontWizzard_Direct_BF

; ##############  Simple OpenGLGadget FontWizzard_BF Demo - DPI aware - All OS ############
; The FontWizzard_BF Module - You can found here :
; https://www.purebasic.fr/english/viewtopic.php?f=12&t=76338
EnableExplicit

Define image_1_ID=LoadImage(#PB_Any, #PB_Compiler_Home+"/Examples/Sources/Data/PureBasicLogo.bmp")
Define window_ID=OpenWindow(#PB_Any, 0, 0, 500, 370, "FW Text on a OpenGLGadget", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
Define gadget_ID=OpenGLGadget(#PB_Any, 0, 0, WindowWidth(window_ID), WindowHeight(window_ID))

; ----------------------------------------------------------

Repeat
  
  SetColorOpenGLGadget_FW(#Blue) ; OpenGLGadget color
  
  Define text$="Hello World"+#LF$+"This is a advanced text output"
  ; Add a frame to all outputs - Padding is automatically adjusted - Alpha channel sensitive - Sample $8000FFFF
  AddFrame_FW(2*DesktopResolutionX(), $FF0000FF) ; AddFrame_Size_FW, AddFrame_Color_FW
  
  DrawGLTextImage_FW(gadget_ID,     ; Gadget ID
                     "",            ; Font
                     #PB_Font_Bold, ; Font flag
                     100,           ; Output x pos
                     20,            ; Output y pos
                     300,           ; Output width
                     60,            ; Output height
                     #White,        ; Text color
                     1,             ; Text adjustment - 0=center - 1=left - 2=right
                     -1,            ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
                     text$,         ; Text
                     25,            ; Padding x
                     5,             ; Padding y
                     1.0)           ; Resize factor
  
  text$="Hello World"+#LF$+"This is a automaticaly fitted text output"+#LF$+"Try any Text"
  AddFrame_FW() ; Remove the frame
  DrawGLTextImage_FW(gadget_ID,     ; Gadget ID
                     "Georgia",     ; Font
                     #PB_Font_Bold, ; Font flag
                     10,            ; Output x pos
                     110,           ; Output y pos
                     480,           ; Output width
                     90,            ; Output height
                     #Red,          ; Text color
                     0,             ; Text adjustment - 0=center - 1=left - 2=right
                     -1,            ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
                     text$,         ; Text
                     -25,           ; Padding x
                     5,             ; Padding y
                     1.0)           ; Resize factor
  
  text$+#LF$+#LF$+"Have fun"
  AddFrame_FW(3*DesktopResolutionX(), #Red) ; AddFrame_Size_FW, AddFrame_Color_FW
  AddImage_1_FW(image_1_ID,
                252*DesktopResolutionX(), ; x pos
                88*DesktopResolutionX(),  ; y pos
                140*DesktopResolutionX(), ; Width
                25*DesktopResolutionX(),  ; Height
                255)                      ; Alpha
  
  DrawGLTextImage_FW(gadget_ID,     ; Gadget ID
                     "Georgia",     ; Font
                     #PB_Font_Bold, ; Font flag
                     50,            ; Output x pos
                     230,           ; Output y pos
                     400,           ; Output width
                     120,           ; Output height
                     #Blue,         ; Text color
                     0,             ; Text adjustment - 0=center - 1=left - 2=right
                     #Green,        ; Background_color - Ignore = -1 - Alpha channel sensitive - Sample $8000FFFF
                     text$,         ; Text
                     25,            ; Padding x
                     5,             ; Padding y
                     1.0)           ; Resize factor
  AddImage_1_FW()                   ; Remove additional image 1
  
  OpenGLGadgetFlipBuffers_FW(gadget_ID) ; Flip Buffers OpenGLGadget - Get the output  
  
Until WaitWindowEvent()=#PB_Event_CloseWindow
Last edited by Saki on Sat Jan 02, 2021 11:47 pm, edited 28 times in total.
地球上の平和
User avatar
kernadec
Enthusiast
Enthusiast
Posts: 146
Joined: Tue Jan 05, 2010 10:35 am

Re: OpenGLGadget + txt = impossible [Resolve]

Post by kernadec »

Thanks for sharing Saki :D
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenGLGadget + txt = impossible [Resolve]

Post by infratec »

@Saki
Thank you that you are using my code to show again the 'great BF' stuff. :evil:

It was clear, that when I draw an image, that every image can be drawn in an opengl context.
So it was no need to capture an other thread to bring BF in the mind of the forum users.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: OpenGLGadget + txt = impossible [Resolve]

Post by Saki »

Your code ?
It's not your code any more than it's my code ! :wink:
We usually post our codes so that others should and can use them.
You have strange problems.
It is exactly what the users wanted as a solution.
Or should they use your text output just because you feel pissed because there are better ?
How else can I show them a solution ?
Should I tell them to look into the crystal ball, just because it doesn't suit your ego ?
地球上の平和
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: OpenGLGadget + txt = impossible [Resolve]

Post by infratec »

In my opinion your problem is bigger then mine. :wink:

So use the search function for 'image', 'canvas', 'window', 'screen' and add everywhere a comment about BF.
Maybe afterwards you are satisfied.

And even if I publish a code from me here in the public, it is still my code.
Or you can tell walbus that the BF code is yours, it would be interesting what he gives you as answer.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: OpenGLGadget + txt = impossible [Resolve]

Post by Saki »

@infratec
Be careful that your problem does not eat your soul.
If you would look, you would see that the code
and the creation of the output and also the output itself is quite different.

Thanks to all for posting his GL codes here and in the french forum.
Discussion finished.

@kernadec
Thank you very much, I wish you a lot of joy with it.
Useful hints :
When you use frames, the text automatically recedes in front of them.
Additionally a preset padding works.
If no frames are used this padding is virtual and also reduces the size of the text automaticaly.
For very small text boxes you can make the padding negative, e.g. x=-20, y=-5.
The text always fits automatically into the specified boxes.
You can also specify a smallest and largest font to use, or a font size to use.
The scaling factor can adjust the output to higher desktop scales without changing the coordinates.
地球上の平和
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: OpenGLGadget + txt = impossible [Resolve]

Post by Saki »

FW based text output - Code updated and enhanced.

Now DPI aware is supported for high quality Razor knife sharp outputs on all desktop scalings.
Clipping added for x, y <0.
Code simplifications and enhancements for easy using.

Have fun
地球上の平和
User avatar
Lord
Addict
Addict
Posts: 849
Joined: Tue May 26, 2009 2:11 pm

Re: OpenGLGadget + txt = impossible [Resolve]

Post by Lord »

infratec wrote:@Saki
Thank you that you are using my code to show again the 'great BF' stuff. :evil:

It was clear, that when I draw an image, that every image can be drawn in an opengl context.
So it was no need to capture an other thread to bring BF in the mind of the forum users.
I think that walbus almost always hijacks every graphics thread to promote his program.
Image
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: OpenGLGadget + txt = impossible [Resolve]

Post by Saki »

Again, it's not his code, coloring a GLGadget and outputting an image on it requires a few calls.
If you know another way, please show it here.
But you have neither looked at the code nor do you understand it.

You are free to post your own code here, just like anyone else.
It's not your thread either, yet you post your nonsense here, like so often and always BF bashing.
You are also still free to present your own projects. :!:
But unfortunately there seems to be none.
Therefore you try again and again to get a profile with your nonsense at the expense of others.
Your biggest code should be about a hundred lines long, for many years. :shock:
You better work on it. :!:
If you call yourself a lord, you should also try to behave like a lord.
Create a large project with thousands of hours of work, make it available to PB and the community for free, and you're a lord.
The FontWizzard_BF is a completely independent small module, which has absolutely nothing to do with the GFX_Wizzard_BF.
There are also no dependencies and the module is directly available as code in the forum.
But this doesn't fit into what you want, you want to stink.
Remember once that the Basher and Stanker always shows a mirror image of itself.
I don't discuss with you, write here your better code so that all have something from it and ready.

The FontWizzard_BF is a valuable module,
there is no foreign code in PB that has its capabilities.

Discussion ended.

For all others.
For MacOS and Linux the calls have to be in an event loop.
The event processing is different here.
Also the images to be output must be mirrored vertically.
It is not difficult and who needs it can adapt the codes.
I don't do anything with OpenGL, so I have no ambitions to maintain and extend the code.
But everyone else is welcome to do that, so that everyone gets something out of it.
地球上の平和
User avatar
Lord
Addict
Addict
Posts: 849
Joined: Tue May 26, 2009 2:11 pm

Re: OpenGLGadget + txt = impossible [Resolve]

Post by Lord »

Changing your name didn't change your behaviour:
I think that walbus almost always hijacks every graphics thread to promote his program.
Image
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: OpenGLGadget + txt = impossible [Resolve]

Post by Saki »

Dear OpenGL interested people.

My little Christmas present for you. :D

Attached in the thread above is a small complete directly runnable
new special module, named FontWizzard_Direct_BF for enhanced text output on all OS and all channels.

This module can directly create text outputs, also text as images
and output them to CanvasGadget, OpenGLGadget, Screen, Images,
but it has no support for PB ImageGadget, ButtonImageGadget and ButtonGadget.

This module was created especially for the needs of the OpenGL friends
here to enable a simple but powerful text output.

That was really quite fiddly to implement this stuff multi OS. LOL.

If you look at the code above, you'll see that the code to create this output is only 60 lines and can easily be trimmed down considerably.
Everything else is handled in the module.
It's also completely DPI aware, this is very important !

Just try.

(Linux can not grab the PB Logo from the PB Program path, but it works also fine)
In the row : MacOS - Linux - Windows
Image

Well, to be honest, this thing has pretty much spoiled my desire for OpenGL. :(
地球上の平和
User avatar
Rings
Moderator
Moderator
Posts: 1427
Joined: Sat Apr 26, 2003 1:11 am

my message

Post by Rings »

this is the splitted topic ffrom viewtopic.php?f=13&t=76496
@saki: move your code to the BF Topics.
this will be locked and deleted later
SPAMINATOR NR.1
Locked