PlutoSVG

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
idle
Always Here
Always Here
Posts: 6011
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

PlutoSVG

Post by idle »

A build of PlutoSVG windows x64
Note PlutoSVG doesn't support Text.
Instead use SVGModule
viewtopic.php?p=645872#p645872

PlutoSVG is a compact and efficient SVG rendering library written in C. It is specifically designed for parsing and rendering SVG documents embedded in OpenType fonts, providing an optimal balance between speed and minimal memory usage. It is also suitable for rendering scalable icons.
including PlutoVG
PlutoVG is a standalone 2D vector graphics library
Features
Path Filling, Stroking and Dashing
Solid, Gradient and Texture Paints
Fonts and Texts
Clipping and Compositing
Transformations
Images
https://github.com/idle-PB/plutosvg

Example

Code: Select all

UsePNGImageDecoder()
  
  Procedure Smile() 
    
    Protected width = 150;
    Protected height = 150;
    
    Protected center_x.f = width / 2.0
    Protected center_y.f = height / 2.0
    Protected face_radius.f = 70
    Protected mouth_radius.f = 50
    Protected eye_radius.f = 10
    Protected eye_offset_x.f = 25
    Protected eye_offset_y.f = 20
    Protected eye_x.f = center_x - eye_offset_x
    Protected eye_y.f = center_y - eye_offset_y
    
    Protected surface = plutovg_surface_create(width, height)
    Protected canvas = plutovg_canvas_create(surface)
    
    plutovg_canvas_save(canvas);
    plutovg_canvas_arc(canvas, center_x, center_y, face_radius, 0, #PLUTOVG_TWO_PI, 0)
    plutovg_canvas_set_rgb(canvas, 1, 1, 0)
    plutovg_canvas_fill_preserve(canvas)
    plutovg_canvas_set_rgb(canvas, 0, 0, 0)
    plutovg_canvas_set_line_width(canvas, 5)
    plutovg_canvas_stroke(canvas)
    plutovg_canvas_restore(canvas)
    
    plutovg_canvas_save(canvas)
    plutovg_canvas_arc(canvas, eye_x, eye_y, eye_radius, 0, #PLUTOVG_TWO_PI, 0)
    plutovg_canvas_arc(canvas, center_x + eye_offset_x, eye_y, eye_radius, 0, #PLUTOVG_TWO_PI, 0)
    plutovg_canvas_set_rgb(canvas, 0, 0, 0)
    plutovg_canvas_fill(canvas)
    plutovg_canvas_restore(canvas)
    
    plutovg_canvas_save(canvas)
    plutovg_canvas_arc(canvas, center_x, center_y, mouth_radius, 0, #PLUTOVG_PI, 0)
    plutovg_canvas_set_rgb(canvas, 0, 0, 0)
    plutovg_canvas_set_line_width(canvas, 5)
    plutovg_canvas_stroke(canvas)
    plutovg_canvas_restore(canvas)
    
    Protected font.s = "C:\Windows\fonts\Arial.ttf"
    Protected text.s = "Smile PB" 
    
    If plutovg_canvas_add_font_file(canvas, "Arial",1,0,font,0)
      
      plutovg_canvas_select_font_face(canvas,"Arial",1,0) 
      Protected rect.plutovg_rect 
      plutovg_canvas_save(canvas)   
      plutovg_canvas_set_rgb(canvas, 0, 0, 1)
      plutovg_canvas_set_font_size(canvas,24)
      plutovg_canvas_text_extents(canvas,@text,-1,#PLUTOVG_TEXT_ENCODING_UTF16,@rect)
      Plutovg_canvas_stroke_text(canvas,text,-1,#PLUTOVG_TEXT_ENCODING_UTF16, center_x-(rect\w*0.5),rect\h*2.0)
      plutovg_canvas_restore(canvas)
      
    EndIf   
        
    Protected img = plutovg_surface_write_to_img(surface,#PB_Any) 
    If img 
      OpenWindow(0,0,0,ImageWidth(img),ImageHeight(img),"vector test",#PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
      ImageGadget(0,0,0,ImageWidth(img),ImageHeight(img),ImageID(img)) 
      Repeat 
      Until WaitWindowEvent() = #PB_Event_CloseWindow 
    EndIf 
    plutovg_canvas_destroy(canvas)
    plutovg_surface_destroy(surface)
       
    
  EndProcedure   
  
  
  Procedure TestSVGtoImgandPNG(svg.s,topng.s, width.f = -1, height.f = -1)
    Protected document.i
    Protected surface.i 
    
    document = plutosvg_document_load_from_file(svg, width, height)
    If document
      Debug "SVG loaded successfully"
      Debug "Width: " + StrF(plutosvg_document_get_width(document))
      Debug "Height: " + StrF(plutosvg_document_get_height(document))
      
      surface = plutosvg_document_render_to_surface(document,0, -1, -1, #Null, #Null, #Null);
      If surface 
        
        img = plutovg_surface_write_to_img(surface,#PB_Any) 
        If img 
          OpenWindow(0,0,0,ImageWidth(img),ImageHeight(img),"SVG to img",#PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
          ImageGadget(0,0,0,ImageWidth(img),ImageHeight(img),ImageID(img)) 
          Repeat 
          Until WaitWindowEvent() = #PB_Event_CloseWindow 
        EndIf 
                
        plutovg_surface_write_to_png(surface, topng) 
        plutovg_surface_destroy(surface)
        plutosvg_document_destroy(document)
        ProcedureReturn 1 
      Else 
        Debug "Failed to create surface" 
      EndIf 
      
    Else
      Debug "Failed to load SVG file: " + svg
    EndIf 
  EndProcedure
  
  Debug "PlutoSVG Version: " + PeekS(plutosvg_version_string(),-1,#PB_UTF8) 
  Debug "Version Code: " + Str(plutosvg_version())
  
  Global svg.s = GetPathPart(ProgramFilename()) + "tiger.svg" 
  Global topng.s = GetPathPart(ProgramFilename()) + "tiger.png"
  
  smile() ;draw smile to image and open window
    
  If TestSVGtoImgandPNG(svg.s,topng.s)
    
    cam = LoadImage(-1,topng)
    If cam  
      OpenWindow(0,0,0,ImageWidth(cam),ImageHeight(cam),"SVG to PNG",#PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
      ImageGadget(0,0,0,ImageWidth(cam),ImageHeight(cam),ImageID(cam)) 
      Repeat 
      Until WaitWindowEvent() = #PB_Event_CloseWindow 
    Else 
      MessageRequester("oops","cant find image")
    EndIf   
    
  EndIf 

added
plutovg_surface_write_to_img(*surface, imageNumber)
Fred
Administrator
Administrator
Posts: 18343
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PlutoSVG

Post by Fred »

Good work, as always !
User avatar
idle
Always Here
Always Here
Posts: 6011
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PlutoSVG

Post by idle »

Fred wrote: Wed Sep 24, 2025 8:24 am Good work, as always !
Not bad for 3/4 of a days work!
unfortunately Claude spat the dummy writing the markdown documentation from the headers so it got truncated.
Mesa
Enthusiast
Enthusiast
Posts: 445
Joined: Fri Feb 24, 2012 10:19 am

Re: PlutoSVG

Post by Mesa »

I've got this message "libwinpthread-1.dll is missing".

Windows 10 64b, pb 6.30b2 64b.

M.
punak
Enthusiast
Enthusiast
Posts: 113
Joined: Tue Sep 07, 2021 12:08 pm

Re: PlutoSVG

Post by punak »

Mesa wrote: Thu Sep 25, 2025 4:38 pm I've got this message "libwinpthread-1.dll is missing".

Windows 10 64b, pb 6.30b2 64b.

M.
I receive this message too.
User avatar
idle
Always Here
Always Here
Posts: 6011
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PlutoSVG

Post by idle »

Mesa wrote: Thu Sep 25, 2025 4:38 pm I've got this message "libwinpthread-1.dll is missing".

Windows 10 64b, pb 6.30b2 64b.

M.
I rebuilt the dll, maybe it's only adding the import lib to the dll, I explicitly added libpwinthread.a but I've included the dll as objdump still referenced the dll.

There's also still an issue with fonts in svg trying to work that one out.
pjay
Enthusiast
Enthusiast
Posts: 277
Joined: Thu Mar 30, 2006 11:14 am

Re: PlutoSVG

Post by pjay »

Great stuff Idle, examples working well here. 8)
punak
Enthusiast
Enthusiast
Posts: 113
Joined: Tue Sep 07, 2021 12:08 pm

Re: PlutoSVG

Post by punak »

@idle: can we use this library inside pb vector drawing codes?
For example, I want to load and draw an SVG image inside the vector drawing codes?
User avatar
idle
Always Here
Always Here
Posts: 6011
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PlutoSVG

Post by idle »

You can use pb vectors to svg output which writes to file then load it with the lib but the fonts aren't working from svg and I don't really understand why.
I may need to get some clarification from the author.

I'm curretly trying to get it working statically linking freetype instead of loading it dynamicly but I'm getting an error on the ft_new_library function
Post Reply