Load SVG with TÖVE [x64]

Share your advanced PureBasic knowledge/code with the community.
punak
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Sep 07, 2021 12:08 pm

Load SVG with TÖVE [x64]

Post by punak »

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

Re: Load SVG with TÖVE [x64]

Post by idle »

looking at the source it uses nanosvg. didn't you already try that?
punak
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Sep 07, 2021 12:08 pm

Re: Load SVG with TÖVE [x64]

Post by punak »

idle wrote: Fri Oct 10, 2025 3:04 am looking at the source it uses nanosvg. didn't you already try that?
I don't remember personally using it in a project before. I don't know about others.
This dll is for the LÖVE 2Denvironment.
User avatar
idle
Always Here
Always Here
Posts: 6095
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Load SVG with TÖVE [x64]

Post by idle »

punak wrote: Fri Oct 10, 2025 6:57 am
idle wrote: Fri Oct 10, 2025 3:04 am looking at the source it uses nanosvg. didn't you already try that?
I don't remember personally using it in a project before. I don't know about others.
This dll is for the LÖVE 2Denvironment.
I just remember someone tried nanosvg recently and had some issues with it.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5012
Joined: Sun Apr 12, 2009 6:27 am

Re: Load SVG with TÖVE [x64]

Post by RASHAD »

Hi idle
I am not the one you remembered :D
But yes nanosvg has limitations
For example it don't load svg saved using PB or inkscape and more
Egypt my love
Post Reply