First download x64-TÖVE.dll
Now, with the following code, you can easily load the entire svg file into an image.
Code: Select all
; === Load TÖVE DLL and define prototypes ===
Global toveDLL = OpenLibrary(#PB_Any, "libTove.dll")
If toveDLL = 0
MessageRequester("Error", "Failed to load libTove.dll.")
End
EndIf
PrototypeC.i proto_NewGraphics(svg.p-ascii, units.p-ascii, dpi.f)
PrototypeC proto_GraphicsRasterize(shape.i, pixels.i, width.i, height.i, stride.i, tx.f, ty.f, scale.f, settings.i)
Global NewGraphics.proto_NewGraphics = GetFunction(toveDLL, "NewGraphics")
Global GraphicsRasterize.proto_GraphicsRasterize = GetFunction(toveDLL, "GraphicsRasterize")
If NewGraphics = 0 Or GraphicsRasterize = 0
MessageRequester("Error", "Failed to resolve required functions from libTove.dll.")
CloseLibrary(toveDLL)
End
EndIf
; === Rasterize SVG file into PB image ===
Procedure.i RasterizeSVGToImage(svgFile.s, width.i, height.i)
Protected svg.s, line.s, graphics.i
Protected *buffer, image.i, i.i, r.b, b.b
; --- Load SVG content ---
If ReadFile(0, svgFile)
While Not Eof(0)
line = ReadString(0)
svg + line + #LF$
Wend
CloseFile(0)
Else
MessageRequester("Error", "Failed to read SVG file: " + svgFile)
ProcedureReturn 0
EndIf
; --- Create TÖVE graphics object ---
graphics = NewGraphics(svg, "px", 72.0)
If graphics = 0
MessageRequester("Error", "Failed to parse SVG.")
ProcedureReturn 0
EndIf
; --- Allocate RGBA buffer ---
*buffer = AllocateMemory(width * height * 4)
If *buffer = 0
MessageRequester("Error", "Failed to allocate memory.")
ProcedureReturn 0
EndIf
; --- Rasterize SVG into buffer ---
GraphicsRasterize(graphics, *buffer, width, height, width * 4, 0.0, 0.0, 0.5, 0)
; --- Swap R and B channels (BGRA → RGBA) ---
For i = 0 To width * height - 1
r = PeekB(*buffer + i * 4 + 0)
b = PeekB(*buffer + i * 4 + 2)
PokeB(*buffer + i * 4 + 0, b)
PokeB(*buffer + i * 4 + 2, r)
Next
; --- Create PB image and copy pixel data ---
image = CreateImage(#PB_Any, width, height, 32)
If image
StartDrawing(ImageOutput(image))
For y = 0 To height - 1
CopyMemory(*buffer + (height - 1 - y) * width * 4, DrawingBuffer() + y * width * 4, width * 4)
Next
StopDrawing()
Else
MessageRequester("Error", "Failed to create PB image.")
EndIf
FreeMemory(*buffer)
ProcedureReturn image
EndProcedure
; === Display rasterized image ===
Define img = RasterizeSVGToImage("1.svg", 400, 400)
If img
If OpenWindow(0, 0, 0, 450, 450, "SVG Rasterized", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ImageGadget(0, 25, 25, 400, 400, ImageID(img))
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
EndIf


