What is the best way to properly scale an svg graphic for use in Gambas IconView? Here's a hack I made to get the desired sharp result and and easier way which unfortunately yields a blurry result:
Public Sub Form_Open()
Dim FilePath As String = "/usr/share/icons/Papirus/32x32/actions/home.svg"
Dim TmpDescription As String = File.Name(FilePath)
' Method A = Sharp
Dim FileAsData As String = FixSvgResolution(FilePath, "64", "64")
Dim Pic_A As Picture = Picture.FromString(FileAsData)
IconView1.Add(0, TmpDescription, Pic_A)
' Method B = Blurry
Dim Pic_B As Picture = Picture.Load(FilePath)
IconView1.Add(1, TmpDescription, Pic_B.Image.Stretch(64, 64).Picture)
End
Private Function FixSvgResolution(SvgPath As String, Optional DesiredWidth As String = "32px", Optional DesiredHeight As String = "32px") As String
Dim FixedSvg As String
Dim doc As New XmlDocument(SvgPath)
Dim mainNode As XmlNode = doc.GetElementsByTagName("svg")[0] 'doc.All[0]
Dim OrigWidth As String = ""
Dim OrigHeight As String = ""
Dim Header As String = Split(mainNode.ToString(), ">")[0]
Dim Attributes As String[] = Split(Header, " ")
Dim i As Integer
For i = 0 To Attributes.Count - 1
If Attributes[i] Like "*width=*" Then
OrigWidth = Split(Attributes[i], String.Chr(34))[1]
Else If Attributes[i] Like "*height=*" Then
OrigHeight = Split(Attributes[i], String.Chr(34))[1]
Endif
If OrigWidth = "" Or OrigHeight = "" Then
'Keep looking
Else
Exit 'For
Endif
Next
If OrigWidth = "" Or OrigHeight = "" Then Error.Raise("File did not contain width and height!")
mainNode.NewAttribute("width", DesiredWidth)
mainNode.NewAttribute("height", DesiredHeight)
' mainNode.NewAttribute("viewBox", "0 0 16 16")
mainNode.NewAttribute("viewBox", "0 0 " & OrigWidth & " " & OrigHeight)
' mainNode.NewAttribute("viewBox", "-0.2 -0.2 " & OrigWidth & ".4 " & OrigHeight & "16.4")
FixedSvg = doc.ToString()
Return FixedSvg
End
PS: DotNewAttribute does an update or replace to the existing attribute. But I could not find a proper way to read the xml attributes. So reading the original attribute values is another hack. I appreciate any advice in this regard aswell.