Page 1 of 3

UseSVGImageDecoder/Encoder

Posted: Fri Sep 19, 2025 11:37 am
by punak
Hi
Given the usefulness of SVG files, it would be great if they were added to image plugins.

Code: Select all

UseSVGImageDecoder()
If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 0, 0, 400, 200)
  
  LoadImage(0,"Test.svg")
  
  If StartVectorDrawing(CanvasVectorOutput(0))
    
    MovePathCursor(50, 50)
    DrawVectorImage(ImageID(0), 255,50,60)
    
    StopVectorDrawing()
  EndIf
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf


Re: UseSVGImageDecoder/Encoder

Posted: Fri Sep 19, 2025 1:48 pm
by acreis
+1

Re: UseSVGImageDecoder/Encoder

Posted: Fri Sep 19, 2025 11:07 pm
by Little John
+1

Re: UseSVGImageDecoder/Encoder

Posted: Sat Sep 20, 2025 5:16 am
by Amitris_de
+1

Re: UseSVGImageDecoder/Encoder

Posted: Sat Sep 20, 2025 11:52 pm
by idle

Re: UseSVGImageDecoder/Encoder

Posted: Sun Sep 21, 2025 6:57 am
by punak
idle wrote: Sat Sep 20, 2025 11:52 pm mit libs
https://github.com/sammycage/plutosvg ;based on
https://github.com/sammycage/plutovg

and there's also this.
https://github.com/plutoprint/plutobook
what do you mean by these links you posted?
what does that have to do with pb?

Re: UseSVGImageDecoder/Encoder

Posted: Sun Sep 21, 2025 7:12 am
by idle
punak wrote: Sun Sep 21, 2025 6:57 am
idle wrote: Sat Sep 20, 2025 11:52 pm mit libs
https://github.com/sammycage/plutosvg ;based on
https://github.com/sammycage/plutovg

and there's also this.
https://github.com/plutoprint/plutobook
what do you mean by these links you posted?
what does that have to do with pb?
Is that's a rhetorical question. What do libs have to do with PB? Every command comes from a lib and the source is c/c++
So the links are there for Fred or anyone else to look at build and test them out. It no good asking for feature xyz without doing a little background or you'll be waiting a long time.

Re: UseSVGImageDecoder/Encoder

Posted: Sun Sep 21, 2025 10:09 am
by punak
idle wrote: Sun Sep 21, 2025 7:12 am
punak wrote: Sun Sep 21, 2025 6:57 am
idle wrote: Sat Sep 20, 2025 11:52 pm mit libs
https://github.com/sammycage/plutosvg ;based on
https://github.com/sammycage/plutovg

and there's also this.
https://github.com/plutoprint/plutobook
what do you mean by these links you posted?
what does that have to do with pb?
Is that's a rhetorical question. What do libs have to do with PB? Every command comes from a lib and the source is c/c++
So the links are there for Fred or anyone else to look at build and test them out. It no good asking for feature xyz without doing a little background or you'll be waiting a long time.
I'm not a skilled programmer. I didn't even know about the connection between C libraries and PureBasic until now.so I thought it was irrelevant. :D
Thanks for letting Fred know.

Re: UseSVGImageDecoder/Encoder

Posted: Tue Sep 23, 2025 7:55 pm
by punak

Re: UseSVGImageDecoder/Encoder

Posted: Tue Sep 23, 2025 10:11 pm
by Little John
punak wrote: Tue Sep 23, 2025 7:55 pm The best
https://github.com/linebender/resvg
The description of that project sounds very promising. Thanks for posting the link!

Re: UseSVGImageDecoder/Encoder

Posted: Wed Sep 24, 2025 2:24 am
by idle
I built plutosvg.dll for windows x64

https://github.com/idle-PB/plutosvg

example

Code: Select all

UsePNGImageDecoder()
  
  Procedure Smile() 
    
    width = 150;
    height = 150;
    
    center_x.f = width / 2.0
    center_y.f = height / 2.0
    face_radius.f = 70
    mouth_radius.f = 50
    eye_radius.f = 10
    eye_offset_x.f = 25
    eye_offset_y.f = 20
    eye_x.f = center_x - eye_offset_x
    eye_y.f = center_y - eye_offset_y
    
    surface = plutovg_surface_create(width, height)
    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)
    
    img = plutovg_surface_write_to_img(surface,#PB_Any) 
    If img 
      OpenWindow(0,0,0,ImageWidth(img),ImageHeight(img),"test") 
      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),"test") 
          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()) + "camera.svg" 
  Global topng.s = GetPathPart(ProgramFilename()) + "mycamera.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),"test") 
      ImageGadget(0,0,0,ImageWidth(cam),ImageHeight(cam),ImageID(cam)) 
      Repeat 
      Until WaitWindowEvent() = #PB_Event_CloseWindow 
    Else 
      MessageRequester("oops","cant find image")
    EndIf   
    
  EndIf 

Re: UseSVGImageDecoder/Encoder

Posted: Wed Sep 24, 2025 6:55 am
by punak
@idle:Thank you for your effort,
I found a very powerful source that supports almost 99% of SVG features. https://github.com/linebender/resvg
Its only limitations are this:
No animations
There are no plans on implementing them either.
No native text rendering
resvg doesn't rely on any system libraries, which implies that we cannot use native text rendering. Nevertheless, native text rendering is optimized for small horizontal text, which is not that common in SVG.
Unicode-only
It's the 21st century. Text files that aren't UTF-8 encoded are no longer relevant.
since you can easily communicate between C and PureBasic code , Is there a way to use this library within PureBasic vector code?

Re: UseSVGImageDecoder/Encoder

Posted: Wed Sep 24, 2025 7:30 am
by idle
plutosvg supports SVG 1.1 and SVG 1.2 Tiny specifications without animation AFAK
also I don't know rust

Re: UseSVGImageDecoder/Encoder

Posted: Fri Sep 26, 2025 10:41 am
by acreis
I dont know rust, but folowing copilot I was able to compile a resvg.dll.

Seems very powerfull.


But it only works on the same machine I compile it.

Some sort of dependency maybe.

Purebasic native rendering should solve all issues

Re: UseSVGImageDecoder/Encoder

Posted: Fri Sep 26, 2025 11:38 am
by idle
acreis wrote: Fri Sep 26, 2025 10:41 am I dont know rust, but folowing copilot I was able to compile a resvg.dll.

Seems very powerfull.


But it only works on the same machine I compile it.

Some sort of dependency maybe.

Purebasic native rendering should solve all issues
How large is the dll?
I'm still having issues with plutosvg, I can't get it to load a svg with text that's generated by PB and I don't know if it's supposed to do that or not. So I've gone around in circles manually initializing freetype but I can't set the hooks back to the dll which might be a dll boundary issue.