PlutoSVG
Posted: Wed Sep 24, 2025 3:09 am
A build of PlutoSVG windows x64
Note PlutoSVG doesn't support Text.
Instead use SVGModule
viewtopic.php?p=645872#p645872
Example
added
plutovg_surface_write_to_img(*surface, imageNumber)
Note PlutoSVG doesn't support Text.
Instead use SVGModule
viewtopic.php?p=645872#p645872
including PlutoVGPlutoSVG 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.
https://github.com/idle-PB/plutosvgPlutoVG 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
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
plutovg_surface_write_to_img(*surface, imageNumber)