Dessiner un texte avec différentes polices...

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Shadow
Messages : 1413
Inscription : mer. 04/nov./2015 17:39

Dessiner un texte avec différentes polices...

Message par Shadow »

Salut, je ressors de mon piti troutrou !

Savez vous si il existe déjà un code pour faire ce genre de chose en graphique, drawing, vector:
https://drive.google.com/file/d/1GTFWVX ... sp=sharing
Processeur: Intel Core I7-4790 - 4 Cœurs - 8 Thread: 3.60 Ghz.
Ram: 32 GB.
Disque: C: SDD 250 GB, D: 3 TB.
Vidéo: NVIDIA GeForce GTX 960: 2 GB DDR5.
Écran: Asus VX248 24 Pouces: 1920 x 1080.
Système: Windows 7 64 Bits.

PureBasic: 5.60 x64 Bits.
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

Re: Dessiner un texte avec différentes polices...

Message par blendman »

Tu peux le faire facilement avec la lib 2D drawing ou la lib vector, peu importe.

Avec la lib vector, tu as l'avantage de pouvoir connaitre la taille (width, height) du texte, donc c'est peut être plus facile de placer les mots bout à bout.

Code : Tout sélectionner


Procedure AddtextVector(text$,x,y,fontID.a,size.w,color.i)
  VectorFont(FontID(fontID),size)
  VectorSourceColor(color)
  h = VectorTextHeight(text$,#PB_VectorText_Baseline)
  MovePathCursor(x, y-h)
  DrawVectorText(Text$) 
EndProcedure



If OpenWindow(0, 0, 0, 800, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 0, 0, 800, 200)
    LoadFont(0, "Impact", 20, #PB_Font_Bold)
    LoadFont(1, "arial", 30)
    LoadFont(2, "comic sans ms", 15, #PB_Font_Italic)
    
    If StartVectorDrawing(CanvasVectorOutput(0))
      y = 40
      x= 10
      AddtextVector("Le rapide renard ",x,y,0,20,RGBA(100, 0, 0, 255))
      x+VectorTextWidth("Le rapide renard ")
      AddtextVector("brun sauta par ",x,y,1,30,RGBA(255, 0, 0, 200))
      x+VectorTextWidth("brun sauta par ")
      AddtextVector("dessus le chien paresseux.",x,y,2,15,RGBA(255, 0, 255, 200))
      StopVectorDrawing()
    EndIf
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
  EndIf


PS : j'ai vu que Sur le forum anglais on t'a répondu. C'est d'ailleurs peut -être la même technique, je vais regarder ;).
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

Re: Dessiner un texte avec différentes polices...

Message par blendman »

avec une procédure et une list(), tu peux en faire ensuite ce que tu veux ;)

Code : Tout sélectionner

Structure sText
  text$
  x.w
  y.w
  h.w
  w.w
  size.w
  color.i
  bgcolor.i
  FondId.a
  fontname$
  fontflag.i
EndStructure
Global NewList Text.sText()


Procedure AddText(text$,x,y,fontname$,size.w,color.i, fontflag,bgcolor=-1)
  AddElement(text())
  text()\text$ = text$
  text()\x = x
  text()\y = y
  text()\size = size
  text()\fontname$ = fontname$
  text()\color = color
  text()\fontflag = fontflag
  text()\bgcolor = bgcolor
EndProcedure  

Procedure AddtextVector(text$,x,y)
  With text()
    If \fontflag>-1
      LoadFont(0, \fontname$, \size, \fontflag)
    Else
      LoadFont(0, \fontname$, \size)
    EndIf
    
    id = ListIndex(text()) 
    
    If ListSize(text()) >0 And id >0
      For i=0 To id-1
        SelectElement(text(),i)
        w + text()\w
      Next
      SelectElement(text(),id)
    EndIf
    
    VectorFont(FontID(0),\size)
    
    h = VectorTextHeight(\text$,#PB_VectorText_Baseline)
    ; h2 = VectorTextHeight(\text$,#PB_VectorText_Visible)
    \h = h
    \w = VectorTextWidth(\text$)
    x1 = x+\x+w
    y1 = y+\y-h
    
    ; draw box
    If \bgcolor<> -1
      AddPathBox(x1,y1,\w,h)
      VectorSourceColor(\bgcolor)
      FillPath()
    EndIf
    
    ; draw text
    VectorSourceColor(\color)
    MovePathCursor(x1, y1)
    DrawVectorText(\Text$) 
    
    FreeFont(0)
  EndWith
EndProcedure

Procedure DrawTextVector(x,y)
  
  ForEach text()
    AddtextVector(text$,x,y)
  Next
  
EndProcedure


If OpenWindow(0, 0, 0, 800, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 0, 0, 800, 200)
  
  y = 0
  x = 0
  AddText("Le rapide renard ",x,y, "Impact",30,RGBA(100, 0, 0, 255),#PB_Font_Bold,RGBA(0,0,0,80))
  AddText("brun sauta par ",x,y, "arial",20,RGBA(255, 0, 0, 255),-1,RGBA(0,255,0,80))
  AddText("dessus le chien paresseux.",x,y, "comic sans ms",15,RGBA(255, 0, 255, 255),#PB_Font_Italic,RGBA(255,255,0,255))
  
  If StartVectorDrawing(CanvasVectorOutput(0))
    DrawTextVector(10,40)
    StopVectorDrawing()
  EndIf
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf


Shadow
Messages : 1413
Inscription : mer. 04/nov./2015 17:39

Re: Dessiner un texte avec différentes polices...

Message par Shadow »

Merci pour le code :)
Processeur: Intel Core I7-4790 - 4 Cœurs - 8 Thread: 3.60 Ghz.
Ram: 32 GB.
Disque: C: SDD 250 GB, D: 3 TB.
Vidéo: NVIDIA GeForce GTX 960: 2 GB DDR5.
Écran: Asus VX248 24 Pouces: 1920 x 1080.
Système: Windows 7 64 Bits.

PureBasic: 5.60 x64 Bits.
Répondre