Page 1 of 1

PlutoSVG

Posted: Wed Sep 24, 2025 3:09 am
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)

Re: PlutoSVG

Posted: Wed Sep 24, 2025 8:24 am
by Fred
Good work, as always !

Re: PlutoSVG

Posted: Wed Sep 24, 2025 8:45 am
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.

Re: PlutoSVG

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

Windows 10 64b, pb 6.30b2 64b.

M.

Re: PlutoSVG

Posted: Thu Sep 25, 2025 6:52 pm
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.

Re: PlutoSVG

Posted: Thu Sep 25, 2025 9:52 pm
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.

Re: PlutoSVG

Posted: Fri Sep 26, 2025 7:28 am
by pjay
Great stuff Idle, examples working well here. 8)

Re: PlutoSVG

Posted: Fri Sep 26, 2025 7:39 am
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?

Re: PlutoSVG

Posted: Fri Sep 26, 2025 8:43 am
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