UseSVGImageDecoder/Encoder

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
punak
Enthusiast
Enthusiast
Posts: 106
Joined: Tue Sep 07, 2021 12:08 pm

UseSVGImageDecoder/Encoder

Post 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

acreis
Enthusiast
Enthusiast
Posts: 220
Joined: Fri Jun 01, 2012 12:20 am

Re: UseSVGImageDecoder/Encoder

Post by acreis »

+1
Little John
Addict
Addict
Posts: 4798
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: UseSVGImageDecoder/Encoder

Post by Little John »

+1
Amitris_de
User
User
Posts: 32
Joined: Wed Jan 06, 2021 2:53 pm

Re: UseSVGImageDecoder/Encoder

Post by Amitris_de »

+1
User avatar
idle
Always Here
Always Here
Posts: 5964
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UseSVGImageDecoder/Encoder

Post by idle »

punak
Enthusiast
Enthusiast
Posts: 106
Joined: Tue Sep 07, 2021 12:08 pm

Re: UseSVGImageDecoder/Encoder

Post 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?
User avatar
idle
Always Here
Always Here
Posts: 5964
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UseSVGImageDecoder/Encoder

Post 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.
punak
Enthusiast
Enthusiast
Posts: 106
Joined: Tue Sep 07, 2021 12:08 pm

Re: UseSVGImageDecoder/Encoder

Post 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.
punak
Enthusiast
Enthusiast
Posts: 106
Joined: Tue Sep 07, 2021 12:08 pm

Re: UseSVGImageDecoder/Encoder

Post by punak »

Little John
Addict
Addict
Posts: 4798
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: UseSVGImageDecoder/Encoder

Post 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!
User avatar
idle
Always Here
Always Here
Posts: 5964
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UseSVGImageDecoder/Encoder

Post 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 
punak
Enthusiast
Enthusiast
Posts: 106
Joined: Tue Sep 07, 2021 12:08 pm

Re: UseSVGImageDecoder/Encoder

Post 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?
User avatar
idle
Always Here
Always Here
Posts: 5964
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UseSVGImageDecoder/Encoder

Post by idle »

plutosvg supports SVG 1.1 and SVG 1.2 Tiny specifications without animation AFAK
also I don't know rust
acreis
Enthusiast
Enthusiast
Posts: 220
Joined: Fri Jun 01, 2012 12:20 am

Re: UseSVGImageDecoder/Encoder

Post 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
User avatar
idle
Always Here
Always Here
Posts: 5964
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UseSVGImageDecoder/Encoder

Post 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.
Post Reply