Draw a text with multiple fonts with VectorDrawing.

Share your advanced PureBasic knowledge/code with the community.
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Draw a text with multiple fonts with VectorDrawing.

Post by ShadowStorm »

Hi, here is a code to create multi font text with the VectorDrawing library.

Code: Select all

; Code created by Dieppedalle David on 23/08/2021.
; Current Version: 1.00.01.

; First version: 1.00.00 on 23/08/2021.
; Version: 1.00.01 on 24/08/2021 > Adds code to free the fonts used to draw the text.

EnableExplicit

; Structure qui contient les propriétées de tous les mots.
Structure Text
  
  Text.s
  Font.s
  FontID.i
  Style.i
  Size.i
  Color.i
  ColorBackround.i
  X.i
  Y.i
  Width.i
  WidthVectorTextVisible.i
  Height.i
  HeightVectorTextVisible.i
  HeightVectorTextBaseline.i
  
EndStructure

; Structure qui contient les propriétées de chaque ligne du texte.
Structure Line
  
  LineX.i
  LineY.i
  NextPositionX.i
  NextPositionY.i
  LineWidth.i
  LineHeight.i
  Array Location.Text(0)
  
EndStructure

; Structure qui contient les propriétées du texte.
Structure MyText
  Array Line.Line(0)
  Interline.i
EndStructure

; Tableau qui contient tous les Textes, chaque élément est un Texte.
Global Dim VectorTextGadget.MyText(0)

; Image de test déstiner pour les oppérations de calcules du Texte.
Global ImageTest.i = CreateImage(#PB_Any, 1000, 1000, 32, #PB_Image_Transparent)

; VectorTextNumber.i commence à partir de 1.
; VectorTextNumber.i ne dois pas être supérieur au nombre d'élément +1 dans le tableau.
; Utilisez #PB_Any pour automatiquement augmenter de +1 le nombre d'élément dans le tableau.
; Retourne si tous c'est bien passé, VectorTextNumber + 1, Sinon 0.
;
; VectorTextNumber.i starts from 1.
; VectorTextNumber.i must Not be greater than the number of elements +1 in the Array.
; Use #PB_Any To automatically increase by +1 the number of elements in the Array.
; Return If all went well, VectorTextNumber + 1, Else 0.
Procedure.i AddVectorText(VectorTextNumber.i, Text.s, Font.s = "", Style.i = 0, Size.i = 12, Color.i = -16777216, ColorBackround.i = -1)
  
  ; Vériffications diverses.
  If VectorTextNumber.i = #PB_Any
    
    If ArraySize(VectorTextGadget()) = 0
      VectorTextNumber.i = 1
      
    ElseIf ArraySize(VectorTextGadget()) > 0
      VectorTextNumber.i = ArraySize(VectorTextGadget()) + 1
      
    EndIf
    
    ReDim VectorTextGadget(ArraySize(VectorTextGadget()) + 1)
    
  ElseIf VectorTextNumber.i <= 0
    Debug "AddVectorText >>> VectorTextNumber.i est inférieur ou égale à 0, dois être 1 minimum !"
    ProcedureReturn #False
    
  ElseIf VectorTextNumber.i = ArraySize(VectorTextGadget()) + 1
    VectorTextNumber.i = ArraySize(VectorTextGadget()) + 1
    ReDim VectorTextGadget(ArraySize(VectorTextGadget()) + 1)
    
  ElseIf VectorTextNumber.i > ArraySize(VectorTextGadget()) + 1
    Debug "AddVectorText >>> VectorTextNumber.i est supérieur au nombre d'élément +1 dans le tableau, le tableau contient (" + Str(ArraySize(VectorTextGadget()) + 1) + " élément(s)) !"
    ProcedureReturn #False
    
  EndIf
  
  If Size.i < 6
    Size.i = 6
    
  ElseIf Size.i > 500
    Size.i = 500
    
  EndIf
  
  ; Chargement de la police.
  Define FontID.i = LoadFont(#PB_Any, Font.s, Size.i, Style.i | #PB_Font_HighQuality)
  
  ; Récuperer le nombre de lignes et de mots que contienent les tableaux Line() et Location().
  Define LineNumber.i = ArraySize(VectorTextGadget(VectorTextNumber.i)\Line())
  Define LocationNumber.i = ArraySize(VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\Location())
  
  ; Racourcis.
  With VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\Location(LocationNumber.i)
    
    ; Si l'image est initialisé.
    If IsImage(ImageTest.i)
      
      ; Si ont peux dessiné sur l'image.
      If StartVectorDrawing(ImageVectorOutput(ImageTest.i))
        
        ; Utilise le Font pour écrire le texte.
        VectorFont(FontID(FontID.i))
        
        ; Enregistrement de la largeur et hauteur du texte.
        \Width = Round(VectorTextWidth(Text.s), #PB_Round_Up) ; Largeur
        \Height = Round(VectorTextHeight(Text.s), #PB_Round_Up) ; Hauteur
        
        ; Enregistrement de la largeur et de la hauteur du texte visible ainsi que la hauteur de la base du texte.
        \WidthVectorTextVisible = Round(VectorTextWidth(Text.s, #PB_VectorText_Visible), #PB_Round_Up) ; Largeur Visible.
        \HeightVectorTextVisible = Round(VectorTextHeight(Text.s, #PB_VectorText_Visible), #PB_Round_Up) ; Hauteur Visible
        \HeightVectorTextBaseline = Round(VectorTextHeight(Text.s, #PB_VectorText_Baseline), #PB_Round_Up) ; Hauteur depuis la base de la ligne du texte.
        
        ; Si la hauteur de la ligne du texte est plus petite que la hauteur du texte
        ; Alors définie et enregistre la hauteur de la ligne depuis la hauteur du texte.
        If VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\LineHeight < \Height
          VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\LineHeight = \Height + 2.00000000
        EndIf
        
      EndIf
      
      ; Sinon si l'image n'est pas initialisé.
    ElseIf Not IsImage(ImageTest.i)
      Debug "ImageTest n'est pas initialisé !"
      ProcedureReturn #False
      
    EndIf
    
    \X = VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\NextPositionX ; Enregistre la prochaine position X du mot depuis NextPositionX.i.
    
    ; NextPositionX.i va sauvegarder la position en X, ce sera très utile pour écrire les prochain mots à la suite des autres.
    VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\NextPositionX + \Width ; Ajoute dans NextPositionX.i, la largeur du mot.
    
    ; Si la couleur de texte est égale à 0.
    If Color.i  = 0
      \Color = RGBA(0, 0, 0, 255) ; Alors ont la change et ont enregistre la couleur en noir opaque, car 0 est égale à noir totalement transparent.
    Else
      \Color = Color.i ; Sinon ont ce contente d'enregistrer la couleur du texte.
    EndIf
    
    \Text = Text.s ; Le texte du mots.
    \Font = Font.s ; Le nom de la police du texte du mots.
    \FontID = FontID.i ; Le Font utiliser pour le texte du mots.
    \Style = Style.i   ; Le texte du mots.
    \ColorBackround = ColorBackround.i ; La couleur d'arrière plant du texte du mots.
    \Size = Size.i                     ; La taille du texte du mots.
    
    ; Ont ajoute un élément dans le tableau Location() pour le prochain mot.
    ReDim VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\Location(LocationNumber.i + 1)
    
  EndWith
  
  ProcedureReturn VectorTextNumber.i + 1
  
EndProcedure

; VectorTextNumber.i commence à partir de 1.
; VectorTextNumber.i ne dois pas être supérieur au nombre d'élément +1 dans le tableau.
; Va sur la prochaine ligne (Ajoute une nouvelle ligne).
; Retourne si tous c'est bien passé, le nombre de ligne totale (Avec la nouvelle), Sinon 0..
;
; VectorTextNumber.i starts from 1.
; VectorTextNumber.i must Not be greater than the number of element +1 in the Array.
; Go To the Next Line (Add a new line).
; Return If all is well, the number of total Line (With the new one), Else 0.
Procedure.i AddVectorTextNewLine(VectorTextNumber.i)
  
  ; Vériffications diverses.
  If VectorTextNumber.i <= 0
    Debug "CreateImageVectorText >>> VectorTextNumber.i est inférieur ou égale à 0, dois être 1 minimum !"
    ProcedureReturn #False
    
  ElseIf VectorTextNumber.i = ArraySize(VectorTextGadget()) + 1
    Debug "CreateImageVectorText >>> VectorTextNumber.i est le dernier élément du tableau qui est vide, le tableau contient (" + Str(ArraySize(VectorTextGadget()) + 1) + " élément(s)) !"
    ProcedureReturn #False
    
  ElseIf VectorTextNumber.i > ArraySize(VectorTextGadget()) + 1
    Debug "CreateImageVectorText >>> VectorTextNumber.i est supérieur au nombre d'élément +1 dans le tableau, le tableau contient (" + Str(ArraySize(VectorTextGadget()) + 1) + " élément(s)) !"
    ProcedureReturn #False
    
  EndIf
  
  ; Récuperer le nombre de lignes que contient le tableau Line().
  Define LineNumber.i = ArraySize(VectorTextGadget(VectorTextNumber.i)\Line())
  
  ; Ont ajoute un élément dans le tableau Line() pour la prochaine ligne.
  ReDim VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i + 1)
  
  ; Définie et enregistre la position Y de la prochaine ligne, depuis la position Y et la hauteur de la ligne actuel.
  VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i + 1)\LineY = VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\LineY + VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\LineHeight + VectorTextGadget(VectorTextNumber.i)\Interline
  
  ; Définie et enregistre la hauteur de la prochaine ligne.
  VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i + 1)\LineHeight = 10
  
;   ; Initialise les valeur pour le premier mot de la ligne.
;   With VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i + 1)\Location(0)
;     
;     \Color.i  = 0 ; La couleur du mot.
;     \Text = " "   ; Le texte du mots.
;     \Font = ""    ; Le nom de la police du texte du mots.
;     \Style = 0    ; Le style du texte du mots.
;     \FontID = LoadFont(#PB_Any, "", 10, #PB_Font_HighQuality) ; Le Font utiliser pour le texte du mots.
;     \ColorBackround = RGBA(255, 255, 255, 255)                ; La couleur d'arrière plant du texte du mots.
;     \Size = 10                                                ; La taille du texte du mots.
;     
;     ; Si l'image est initialisé.
;     If IsImage(ImageTest.i)
;       
;       ; Si ont peux dessiné sur l'image.
;       If StartVectorDrawing(ImageVectorOutput(ImageTest.i))
;         
;         ; Utilise le Font pour écrire le texte.
;         VectorFont(FontID(\FontID))
;         
;         ; Enregistrement de la largeur et hauteur du texte.
;         \Width = Round(VectorTextWidth(" "), #PB_Round_Up) ; Largeur
;         \Height = Round(VectorTextHeight(" "), #PB_Round_Up) ; Hauteur
;         
;         ; Enregistrement de la largeur et de la hauteur du texte visible ainsi que la hauteur de la base du texte.
;         \WidthVectorTextVisible = Round(VectorTextWidth(" ", #PB_VectorText_Visible), #PB_Round_Up) ; Largeur Visible.
;         \HeightVectorTextVisible = Round(VectorTextHeight(" ", #PB_VectorText_Visible), #PB_Round_Up) ; Hauteur Visible
;         \HeightVectorTextBaseline = Round(VectorTextHeight(" ", #PB_VectorText_Baseline), #PB_Round_Up) ; Hauteur depuis la base de la ligne du texte.
;         
;       EndIf
;       
;       ; Sinon si l'image n'est pas initialisé.
;     ElseIf Not IsImage(ImageTest.i)
;       Debug "ImageTest n'est pas initialisé !"
;       ProcedureReturn #False
;       
;     EndIf
;     
;   EndWith
  
  ; Réinitialise NextPositionX.i à 0 car nous changeont de ligne !
  VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\NextPositionX.i = 0
  
  ; Ajoute dans NextPositionY.i la hauteur de la ligne actuel.
  VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\NextPositionY.i + VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\LineHeight + VectorTextGadget(VectorTextNumber.i)\Interline
  
  
  
  ProcedureReturn LineNumber.i + 1
  
EndProcedure

; VectorTextNumber.i commence à partir de 1.
; VectorTextNumber.i ne dois pas être supérieur au nombre d'élément +1 dans le tableau.
; Après avoir ajouter du texte, créer et renvoie l'image du texte construit, Sinon 0..
;
; VectorTextNumber.i starts from 1.
; VectorTextNumber.i must Not be greater than the number of element +1 in the Array.
; After adding text, create And Return the image of the constructed text, Else 0.
Procedure.i CreateImageVectorText(VectorTextNumber.i)
  
  Define IndexLineArray.i ; Index de ligne.
  Define IndexLocationArray.i ; Index de mots.
  
  Define LineWidth.i ; La largeur de la ligne en court d'inspection.
  Define LineLargest.i ; La ligne de texte la plus grande.
  Define TextHeight.i  ; La hauteur totale du texte.
  
  ; Vériffications diverses.
  If VectorTextNumber.i <= 0
    Debug "CreateImageVectorText >>> VectorTextNumber.i est inférieur ou égale à 0, dois être 1 minimum !"
    ProcedureReturn #False
    
  ElseIf VectorTextNumber.i = ArraySize(VectorTextGadget()) + 1
    Debug "CreateImageVectorText >>> VectorTextNumber.i est le dernier élément du tableau qui est vide, le tableau contient (" + Str(ArraySize(VectorTextGadget()) + 1) + " élément(s)) !"
    ProcedureReturn #False
    
  ElseIf VectorTextNumber.i > ArraySize(VectorTextGadget()) + 1
    Debug "CreateImageVectorText >>> VectorTextNumber.i est supérieur au nombre d'élément +1 dans le tableau, le tableau contient (" + Str(ArraySize(VectorTextGadget()) + 1) + " élément(s)) !"
    ProcedureReturn #False
    
  EndIf
  
  ; Récuperer le nombre de lignes que contienent le tableaux Line().
  Define LineNumber.i = ArraySize(VectorTextGadget(VectorTextNumber.i)\Line())
  Define LocationNumber.i = 0 ; Variable pour compter le nombre de mots que contienent le tableau Location() de la ligne actuellement inspecté.
  
  ; Variables pour calculer la position Y de justification du mot.
  Define LocationHeightTextBaselineMaxi.i ; La hauteur maximal des mots depuis leur base, seule le mot le plus grand en hauteur sera retenue.
  
  ; Racourcis
  With VectorTextGadget(VectorTextNumber.i)\Line(IndexLineArray.i)\Location(IndexLocationArray.i)
    
    ; Boucle For pour chaque ligne.
    ; Calcule et Ajuste la position Y des mots.
    For IndexLineArray.i = 0 To LineNumber.i
      
      ; Récuperer le nombre de mots que contienent le tableau Location() de la ligne actuel.
      LocationNumber.i = ArraySize(VectorTextGadget(VectorTextNumber.i)\Line(IndexLineArray.i)\Location())
      LocationHeightTextBaselineMaxi.i = 0 ; Réinitialise la hauteur du mot maximal à chaque nouvelle ligne.
      
      ; Boucle For pour chaque mot de la ligne.
      ; Récuperation des valeurs de hauteur du mot le plus grand en hauteur.
      For IndexLocationArray.i = 0 To LocationNumber.i - 1
        
        ; Récupere la hauteur maximal des mots depuis leur base.
        If LocationHeightTextBaselineMaxi.i < \HeightVectorTextBaseline
          LocationHeightTextBaselineMaxi.i = \HeightVectorTextBaseline
        EndIf
        
      Next
      
      ; Boucle For pour chaque mot de la ligne.
      ; Corrige la position Y des mots pour les alignier par apport à leur base.
      For IndexLocationArray.i = 0 To LocationNumber.i - 1
        
        ; Si la hauteur de la base du mot actuel est plus petite que la base du mot le plus grand enregistré.
        If \HeightVectorTextBaseline < LocationHeightTextBaselineMaxi.i
          ; Ajuste la position Y du mot actuel pour l'aligné par apport au mot le plus grand en hauteur.
          \Y = (LocationHeightTextBaselineMaxi.i - \HeightVectorTextBaseline)
        EndIf
        
      Next
      
    Next
    
    ; Boucle For pour chaque ligne.
    ; Récupere la ligne la plus grande.
    For IndexLineArray.i = 0 To LineNumber.i
      
      ; Récuperer le nombre de mots que contienent le tableau Location() de la ligne actuel.
      LocationNumber.i = ArraySize(VectorTextGadget(VectorTextNumber.i)\Line(IndexLineArray.i)\Location())
      
      ; Boucle For pour chaque mot de la ligne.
      ; Calcule de la largeur de la ligne.
      For IndexLocationArray.i = 0 To LocationNumber.i - 1
        LineWidth.i = \X + \Width ; LineWidth.i sera ingrémenté en taille à chaque mot.
      Next
      
      ; Si la largeur totale de la ligne est plus petite que la valeur enregistré.
      If LineLargest.i < LineWidth.i
        LineLargest.i = LineWidth.i ; Enregistre la largeur de la ligne.
      EndIf
      
    Next
    
    ; Récupere la position ainsi que la taille de la dernière ligne + l'interligne.
    TextHeight.i = VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\LineY + VectorTextGadget(VectorTextNumber.i)\Line(LineNumber.i)\LineHeight + VectorTextGadget(VectorTextNumber.i)\Interline
    
    ; Création de l'image du texte.
    Define ImageTexte.i = CreateImage(#PB_Any, LineLargest.i, TextHeight.i, 32, #PB_Image_Transparent)
    
    ;VectorTextGadget(VectorTextNumber.i)\Line(IndexLineArray.i)\Location(IndexLocationArray.i)
    
    ; Si l'image est initialisé.
    If IsImage(ImageTexte.i)
      
      Define DrawNextPositionX.i = 0
      Define DrawNextPositionY.i = 0
      
      ; Boucle For pour chaque ligne.
      For IndexLineArray.i = 0 To LineNumber.i
        
        DrawNextPositionX.i = VectorTextGadget(VectorTextNumber.i)\Line(IndexLineArray.i)\LineX
        DrawNextPositionY.i = VectorTextGadget(VectorTextNumber.i)\Line(IndexLineArray.i)\LineY
        
        ; Récuperer le nombre de mots que contienent le tableau Location() de la ligne actuel.
        LocationNumber.i = ArraySize(VectorTextGadget(VectorTextNumber.i)\Line(IndexLineArray.i)\Location())
        
        ; Boucle For pour chaque mot de la ligne actuel.
        For IndexLocationArray.i = 0 To LocationNumber.i - 1
          
          If \Text > ""
            
            ; Si ont peux dessiner sur l'image.
            If StartVectorDrawing(ImageVectorOutput(ImageTexte.i))
              
              ; Utilise le font.
              VectorFont(FontID(\FontID))
              
              ; Définie la couleur du font du texte.
              VectorSourceColor(\ColorBackround)
              
              ; Dessine le font du texte avec la couleur du font du texte.
              AddPathBox(DrawNextPositionX.i + \X, DrawNextPositionY.i, \Width, VectorTextGadget(VectorTextNumber.i)\Line(IndexLineArray.i)\LineHeight)
              FillPath(#PB_Path_Preserve)
              
              ; Dessine le texte.
              MovePathCursor(DrawNextPositionX.i + \X, DrawNextPositionY.i + \Y + 0.5 + 2.00000000)
              VectorSourceColor(\Color)
              DrawVectorText(\Text)
              
              StopVectorDrawing()
              
            EndIf
            
          EndIf
          
        Next
        
      Next
      
      ; Sinon si l'image n'est pas initialié.
    Else
      Debug "Imag n'est pas initialisé !"
      ProcedureReturn #False
      
    EndIf
    
  Define FreeFont.i = 0
  
  For IndexLineArray.i = 0 To LineNumber.i
    
    For IndexLocationArray.i = 0 To LocationNumber.i - 1
      
      If IsFont(\FontID)
        FreeFont(\FontID)
      EndIf
      
    Next
    
  Next
  
  EndWith
  
  ProcedureReturn ImageTexte.i
  
EndProcedure

DisableExplicit

; --------------------------Exemple----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Enumeration 1
  #Window
  #MyCanvasGadget
EndEnumeration

Enumeration 1
  #MyText1
  #MyText2
  #MyText3
EndEnumeration

If OpenWindow(#Window, 0, 0, 800, 600, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(#MyCanvasGadget, 0, 0, 800, 600)
  
  ;{ Create Text
  AddVectorText(#MyText1, "This ", "Arial", 0, 12, 0, RGBA(0, 255, 0, 255))
  AddVectorText(#MyText1, "Is ", "Cambria", #PB_Font_Underline, 20, RGBA(255, 0, 0, 255), RGBA(255, 200, 0, 255))
  AddVectorText(#MyText1, "A ", "Times New Roman", #PB_Font_StrikeOut, 40, RGBA(255, 0, 255, 255), RGBA(0, 0, 255, 255))
  AddVectorText(#MyText1, "Text ", "Courier New", #PB_Font_Italic | #PB_Font_Bold, 16, RGBA(0, 0, 255, 255), RGBA(200, 200, 200, 255)) ; Manorly
  AddVectorText(#MyText1, "multi-content", "Verdana", #PB_Font_Underline | #PB_Font_StrikeOut | #PB_Font_Italic, 24, RGBA(255, 200, 0, 255), RGBA(0, 0, 0, 255)) ; Manorly
  
  MyImageVectorTexte1.i = CreateImageVectorText(#MyText1)
  
  AddVectorText(#MyText2, "The fox", "Arial", #PB_Font_Bold | #PB_Font_Underline, 17, RGBA(255, 130, 0, 255), RGBA(255, 255, 255, 255))
  
  AddVectorTextNewLine(#MyText2)
  AddVectorTextNewLine(#MyText2)
  
  AddVectorText(#MyText2, "Fox ", "Cambria", #PB_Font_Bold, 12, RGBA(255, 0, 0, 255), RGBA(255, 255, 255, 255))
  AddVectorText(#MyText2, "is an ambiguous term that most often refers to ", "Cambria", 0, 10, RGBA(0, 0, 0, 255), RGBA(255, 255, 255, 255))
  AddVectorText(#MyText2, "Canids", "Cambria", #PB_Font_Underline, 10, RGBA(0, 0, 255, 255), RGBA(255, 255, 255, 255))
  AddVectorText(#MyText2, " of the", "Cambria", 0, 10, RGBA(0, 0, 0, 255), RGBA(255, 255, 255, 255))
  AddVectorTextNewLine(#MyText2)
  
  AddVectorText(#MyText2, "genus Vulpes, the most common being the ", "Cambria", 0, 10, RGBA(0, 0, 0, 255), RGBA(255, 255, 255, 255))
  AddVectorText(#MyText2, "Red Fox ", "Cambria", #PB_Font_Italic, 10, RGBA(255, 0, 0, 255), RGBA(255, 255, 255, 255))
  AddVectorText(#MyText2, "(Vulpes vulpes).", "Cambria", 0, 10, RGBA(0, 0, 0, 255), RGBA(255, 255, 255, 255))
  
  AddVectorTextNewLine(#MyText2)
  AddVectorTextNewLine(#MyText2)
  
  AddVectorText(#MyText2, "However, by physical similarity, the term is also used to", "Cambria", 0, 10, RGBA(0, 0, 0, 255), 0)
  
  AddVectorTextNewLine(#MyText2)
  
  AddVectorText(#MyText2, "designate ", "Cambria", 0, 10, RGBA(0, 0, 0, 255), 0)
  AddVectorText(#MyText2, "Canids ", "Cambria", #PB_Font_Underline, 10, RGBA(0, 0, 255, 255), 0)
  AddVectorText(#MyText2, "belonging to other genera such As:", "Cambria", 0, 10, RGBA(0, 0, 0, 255), 0)
  
  AddVectorTextNewLine(#MyText2)
  
  AddVectorText(#MyText2, "Atelocynus", "Cambria", #PB_Font_Underline, 10, RGBA(0, 0, 255, 255), 0)
  AddVectorText(#MyText2, ", ", "Cambria", 0, 10, RGBA(0, 0, 0, 255), 0)
  
  AddVectorText(#MyText2, "Cerdocyon", "Cambria", #PB_Font_Underline, 10, RGBA(0, 0, 255, 255), 0)
  AddVectorText(#MyText2, ", ", "Cambria", 0, 10, RGBA(0, 0, 0, 255), 0)
  
  AddVectorText(#MyText2, "Dusicyon", "Cambria", #PB_Font_Underline, 10, RGBA(0, 0, 255, 255), 0)
  AddVectorText(#MyText2, ", ", "Cambria", 0, 10, RGBA(0, 0, 0, 255), 0)
  
  AddVectorText(#MyText2, "Otocyon", "Cambria", #PB_Font_Underline, 10, RGBA(0, 0, 255, 255), 0)
  AddVectorText(#MyText2, ", ", "Cambria", 0, 10, RGBA(0, 0, 0, 255), 0)
  
  AddVectorText(#MyText2, "Lycalopex", "Cambria", #PB_Font_Underline, 10, RGBA(0, 0, 255, 255), 0)
  AddVectorText(#MyText2, " And, ", "Cambria", 0, 10, RGBA(0, 0, 0, 255), 0)
  
  AddVectorText(#MyText2, "Urocyon.", "Cambria", #PB_Font_Underline, 10, RGBA(0, 0, 255, 255), 0)
  
  AddVectorTextNewLine(#MyText2)
  AddVectorTextNewLine(#MyText2)
  
  AddVectorText(#MyText2, "                      You will not see this text because it is invisible !                    ", "Cambria", #PB_Font_Underline, 10, RGBA(0, 0, 255, 0), RGBA(255, 200, 0, 255))
  
  MyImageVectorTexte2.i = CreateImageVectorText(#MyText2)
  
  For Line.i = 1 To 14
    For Letter.i = 1 To 100
      AddVectorText(#MyText3, Chr(Random(122, 97)), "Belwe Lt BT", 0, Random(12, 8), RGBA(Random(255), Random(255), Random(255), 255), RGBA(255, 255, 255, 255))
    Next
    
    If Line.i < 14
      AddVectorTextNewLine(#MyText3)
    EndIf
    
  Next
  
  MyImageVectorTexte3.i = CreateImageVectorText(#MyText3)
  
  ;}
  
  ;{ Draw Text
  If StartVectorDrawing(CanvasVectorOutput(#MyCanvasGadget))
    AddPathBox(0, 0, GadgetWidth(#MyCanvasGadget), GadgetHeight(#MyCanvasGadget))
    VectorSourceColor(RGBA(255, 150, 0, 255))
    FillPath()
    StopVectorDrawing()
  EndIf
  
  If IsImage(MyImageVectorTexte1.i)
    
    If StartVectorDrawing(CanvasVectorOutput(#MyCanvasGadget))
      
      AddPathBox(10.5, 10.5, ImageWidth(MyImageVectorTexte1.i) + 10, ImageHeight(MyImageVectorTexte1.i) + 10)
      VectorSourceColor(RGBA(0, 0, 0, 255))
      StrokePath(1)
      
      MovePathCursor(15, 15)
      DrawVectorImage(ImageID(MyImageVectorTexte1.i), 255)
      StopVectorDrawing()
      
    EndIf
    
  Else
    Debug "Miss !"
    
  EndIf
  
  If IsImage(MyImageVectorTexte2.i)
    
    If StartVectorDrawing(CanvasVectorOutput(#MyCanvasGadget))
      
      AddPathBox(10.5, 90.5, ImageWidth(MyImageVectorTexte2.i) + 10, ImageHeight(MyImageVectorTexte2.i) + 10)
      VectorSourceColor(RGBA(0, 0, 0, 255))
      StrokePath(1)
      
      MovePathCursor(15, 95)
      DrawVectorImage(ImageID(MyImageVectorTexte2.i), 255)
      StopVectorDrawing()
    EndIf
    
  Else
    Debug "Miss !"
    
  EndIf
  
  If IsImage(MyImageVectorTexte3.i)
    
    If StartVectorDrawing(CanvasVectorOutput(#MyCanvasGadget))
      
      AddPathBox(10.5, 278.5, ImageWidth(MyImageVectorTexte3.i) + 10, ImageHeight(MyImageVectorTexte3.i) + 10)
      VectorSourceColor(RGBA(0, 0, 0, 255))
      StrokePath(1)
      
      MovePathCursor(15, 283)
      DrawVectorImage(ImageID(MyImageVectorTexte3.i), 255)
      StopVectorDrawing()
    EndIf
    
  Else
    Debug "Miss !"
    
  EndIf
  
  ;}
  
  Repeat
    
    Event = WaitWindowEvent()
    
  Until Event = #PB_Event_CloseWindow
  
EndIf
Last edited by ShadowStorm on Tue Aug 24, 2021 1:08 am, edited 2 times in total.
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Draw a text with multiple fonts with VectorDrawing.

Post by jacdelad »

Great job! :D

But...I see no FreeFont. In a worst case scenario this can fill up your RAM.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Re: Draw a text with multiple fonts with VectorDrawing.

Post by ShadowStorm »

In fact, I think if a policy already exists, it won't reload, right?
This was a tough job but I did it, thank you.
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Draw a text with multiple fonts with VectorDrawing.

Post by jacdelad »

Nope, it will every time you use LoadFont.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Re: Draw a text with multiple fonts with VectorDrawing.

Post by ShadowStorm »

Unless it is the same font number.
I'll change that thanks.
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Draw a text with multiple fonts with VectorDrawing.

Post by jacdelad »

Yes, but no. You use #PB_Any, so it loads it anew every time. If you use fixed IDs I'm not entirely sure, but I would expect it to keep the old font and load it anew too (you just can't access the old one anymore). But I'm not entirely sure.

Also I saw you use arrays. Redimming arrays is "slow", I would have used a LinkedList. I know this won't change much, but it should be faster, especially when adding more and more items.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Re: Draw a text with multiple fonts with VectorDrawing.

Post by ShadowStorm »

I made changes, no, tables are faster than lists because I can access an item instantly.
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Draw a text with multiple fonts with VectorDrawing.

Post by jacdelad »

Yes, access. But redimming is fairly slow. Again, we're talking here about fractions of fractions of seconds, but the more items you have the more redims you use, which eventually will slow down your program.
You could dim it bigger in the beginning, let's say 10 items. If you need more items don't add just one, add another 10. So you have to deal with much less redims. You just have to keep track of how many items are actually used.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Re: Draw a text with multiple fonts with VectorDrawing.

Post by ShadowStorm »

I take note, but it is complicated after...
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Draw a text with multiple fonts with VectorDrawing.

Post by jacdelad »

Just wanted to tell you. It will also only affect if you us it extensively.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Re: Draw a text with multiple fonts with VectorDrawing.

Post by ShadowStorm »

This is just a start, I think I'll improve, thanks!
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Re: Draw a text with multiple fonts with VectorDrawing.

Post by ShadowStorm »

After test, totally useless, at 10 000 elements, it takes the same time, at my place 1 millisecond for each, it's at 100 000 that it starts to change, and 1 000 000 it's obvious !

In my situation, it is totally useless !

Code: Select all

Dim Abc(0)
NewList Def()

a = ElapsedMilliseconds()

For i = 1 To 100000
  ReDim Abc(i)
Next

b = ElapsedMilliseconds()

Debug b - a

a = ElapsedMilliseconds()

For i = 1 To 100000
  AddElement(Def())
Next

b = ElapsedMilliseconds()

Debug b - a
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Draw a text with multiple fonts with VectorDrawing.

Post by Mijikai »

ShadowStorm wrote: Tue Aug 24, 2021 4:31 pm ...
In my situation, it is totally useless !

Code: Select all

...
Debug b - a
Your testcode makes no sense - disable the debugger.
User avatar
jacdelad
Addict
Addict
Posts: 1418
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Draw a text with multiple fonts with VectorDrawing.

Post by jacdelad »

Code: Select all

DisableDebugger
Dim Abc(0)
NewList Def()
a = ElapsedMilliseconds()
For i = 1 To 1000000
  ReDim Abc(i)
Next
b = ElapsedMilliseconds()
For i = 1 To 1000000
  AddElement(Def())
Next
c = ElapsedMilliseconds()
MessageRequester("","Array: "+Str(b-a)+#CRLF$+"List: "+Str(c-b),0)
Array: 2818ms, List: 35ms.

This is for one million items and just for redimming/addelementing. I'm sure nobody will need so many text items. Anyway, I wanted to tell you that a LinkedList is maybe faster when adding more items to your cache before drawing. It also depends on what else is done in the procedures; array access may be faster.
Anyway, I guess there won't be a noticeable difference whether you use arrays or lists, at least in most cases. But you should consider this in your future projects.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Re: Draw a text with multiple fonts with VectorDrawing.

Post by ShadowStorm »

delete this message (Here) thank you.
Last edited by ShadowStorm on Mon Aug 30, 2021 2:22 am, edited 4 times in total.
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
Post Reply