如何使用GOTK3和librsvg在Go中加载内联的SVG?

huangapple go评论64阅读模式
英文:

How to load an inlined SVG in Go using GOTK3 and librsvg?

问题

我正在使用Go编写的应用程序。它使用GOTK3,已安装librsvg,并且我可以使用gtk.ImageNewFromFile(file-path-to-svg)创建(然后显示)SVG图像。

现在,我想通过源代码中内联的SVG来以编程方式创建图像...就像这样:

color := "#FF0000"
redSvg := 
  "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n" +
  "<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\" width=\"32px\" height=\"32px\" viewBox=\"0 0 32 32\" enable-background=\"new 0 0 32 32\" xml:space=\"preserve\">\n" +
  "    <rect fill=\"" + color + "\" width=\"32\" height=\"32\"/>\n" +
  "</svg>"

image, err := ImageNewFromSvg(redSvg)
.
.
.


func ImageNewFromSvg(svg string) (*gtk.Image, error) {
  ?
}

...我需要一些关于ImageNewFromSvg()实现的帮助。gotk3没有ImageNewFromSvg() API,但它有ImageNewFromPixbuf()

我尝试实现从字符串(带有SVG标记)创建pixbuf,但我还没有成功。

是否可以从内联的SVG中创建图像,即在源代码中定义的SVG?如果可以,ImageNewFromSvg()的实现应该是什么样的?

英文:

I work on an application written in Go. It uses GOTK3, librsvg is installed, and I can create (and then display) SVG images using gtk.ImageNewFromFile(file-path-to-svg).

Now I would like to programmatically create an image from an SVG that's inlined within the source code... something like this:

color := &quot;#FF0000&quot;
redSvg := 
  &quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;utf-8\&quot;?&gt;\n&quot; +
  &quot;&lt;!DOCTYPE svg PUBLIC \&quot;-//W3C//DTD SVG 1.1//EN\&quot; \&quot;http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\&quot;&gt;\n&quot; +
  &quot;&lt;svg version=\&quot;1.1\&quot; id=\&quot;Layer_1\&quot; xmlns=\&quot;http://www.w3.org/2000/svg\&quot; xmlns:xlink=\&quot;http://www.w3.org/1999/xlink\&quot; x=\&quot;0px\&quot; y=\&quot;0px\&quot; width=\&quot;32px\&quot; height=\&quot;32px\&quot; viewBox=\&quot;0 0 32 32\&quot; enable-background=\&quot;new 0 0 32 32\&quot; xml:space=\&quot;preserve\&quot;&gt;\n&quot; +
  &quot;    &lt;rect fill=\&quot;&quot; + color + &quot;\&quot; width=\&quot;32\&quot; height=\&quot;32\&quot;/&gt;\n&quot; +
  &quot;&lt;/svg&gt;&quot;

image, err := ImageNewFromSvg(redSvg)
.
.
.


func ImageNewFromSvg(svg string) (*gtk.Image, error) {
  ?
}

...and I need some help with the implementation of ImageNewFromSvg(). gotk3 doesn't have an ImageNewFromSvg() API, but it does have ImageNewFromPixbuf().

I've tried to implement the creation of a pixbuf from a string (with SVG markup), but I haven't been able to do so.

Is it possible to create an image from an inlined SVG, defined within the source code? If so, what would the implementation of ImageNewFromSvg() be?

答案1

得分: 1

你可以使用gdk.PixbufNewFromDataOnly加载内联的SVG,通过将SVG字符串作为字节切片传递给它,然后从中获取pixbuf,并将其放入gtk.ImageNewFromPixbuf中以获取*gtk.Image,以便在窗口中使用它。

以下是你提到的ImageNewFromSvg函数的正确实现之一:

func ImageNewFromSvg(svg string) (*gtk.Image, error){
  pixbuf, err := gdk.PixbufNewFromDataOnly([]byte(svg))
  if err !=  nil {
  	return nil, err
  }
  
  img, err := gtk.ImageNewFromPixbuf(pixbuf)
  
  if err !=  nil {
  	return nil, err
  }
  
  return img, nil
}

在实际的gtk程序中的示例用法:

package main

import (
	"log"

	"github.com/gotk3/gotk3/gtk"
	"github.com/gotk3/gotk3/gdk"
)

func main() {
	gtk.Init(nil)

	win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
	if err != nil {
		log.Fatal("Unable to create window:", err)
	}
	win.SetTitle("Svg Example")
	win.Connect("destroy", func() {
		gtk.MainQuit()
	})


	color := "#FF0000"
	svgdata := `<?xml version="1.0" encoding="utf-8"?>
	<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
	<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="32px" height="32px" viewBox="0 0 32 32" enable-background="new 0 0 32 32" xml:space="preserve">
	    <rect fill="` + color + `" width="32" height="32"/>
	</svg>`

	svgImage, err  := ImageNewFromSvg(svgdata)
	
	if err != nil {
		log.Fatal(err)
	}
	
	win.Add(svgImage)
	win.ShowAll()


	gtk.Main()
}  

func ImageNewFromSvg(svg string) (*gtk.Image, error){
  pixbuf, err := gdk.PixbufNewFromDataOnly([]byte(svg))
  if err !=  nil {
  	return nil, err
  }
  
  img, err := gtk.ImageNewFromPixbuf(pixbuf)
  
  if err !=  nil {
  	return nil, err
  }
  
  return img, nil
}

输出结果:

如何使用GOTK3和librsvg在Go中加载内联的SVG?

英文:

You can load inlined SVG with gdk.PixbufNewFromDataOnly through passing your svg string as slice of bytes to it then getting pixbuf from it and putting it to gtk.ImageNewFromPixbuf to get *gtk.Image to use it in your window.
And here is one of correct implementation of your mentioned ImageNewFromSvg function:

func ImageNewFromSvg(svg string) (*gtk.Image, error){
  pixbuf, err := gdk.PixbufNewFromDataOnly([]byte(svg))
  if err !=  nil {
  	return nil, err
  }
  
  img, err := gtk.ImageNewFromPixbuf(pixbuf)
  
  if err !=  nil {
  	return nil, err
  }
  
  return img, nil
}

Example usage in actual gtk program:

package main

import (
	&quot;log&quot;

	&quot;github.com/gotk3/gotk3/gtk&quot;
	&quot;github.com/gotk3/gotk3/gdk&quot;
)

func main() {
	gtk.Init(nil)

	win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
	if err != nil {
		log.Fatal(&quot;Unable to create window:&quot;, err)
	}
	win.SetTitle(&quot;Svg Example&quot;)
	win.Connect(&quot;destroy&quot;, func() {
		gtk.MainQuit()
	})


	color := &quot;#FF0000&quot;
	svgdata := &quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;utf-8\&quot;?&gt;\n&quot; +
 			   &quot;&lt;!DOCTYPE svg PUBLIC \&quot;-//W3C//DTD SVG 1.1//EN\&quot; \&quot;http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\&quot;&gt;\n&quot; +
               &quot;&lt;svg version=\&quot;1.1\&quot; id=\&quot;Layer_1\&quot; xmlns=\&quot;http://www.w3.org/2000/svg\&quot; xmlns:xlink=\&quot;http://www.w3.org/1999/xlink\&quot; x=\&quot;0px\&quot; y=\&quot;0px\&quot; width=\&quot;32px\&quot; height=\&quot;32px\&quot; viewBox=\&quot;0 0 32 32\&quot; enable-background=\&quot;new 0 0 32 32\&quot; xml:space=\&quot;preserve\&quot;&gt;\n&quot; +
               &quot;    &lt;rect fill=\&quot;&quot; + color + &quot;\&quot; width=\&quot;32\&quot; height=\&quot;32\&quot;/&gt;\n&quot; +
               &quot;&lt;/svg&gt;&quot;

	svgImage, err  := ImageNewFromSvg(svgdata)
	
	if err != nil {
		log.Fatal(err)
	}
	
	win.Add(svgImage)
	win.ShowAll()


	gtk.Main()
}  

func ImageNewFromSvg(svg string) (*gtk.Image, error){
  pixbuf, err := gdk.PixbufNewFromDataOnly([]byte(svg))
  if err !=  nil {
  	return nil, err
  }
  
  img, err := gtk.ImageNewFromPixbuf(pixbuf)
  
  if err !=  nil {
  	return nil, err
  }
  
  return img, nil
}

And output:
如何使用GOTK3和librsvg在Go中加载内联的SVG?

huangapple
  • 本文由 发表于 2023年6月11日 23:49:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定