英文:
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 := "#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) {
?
}
...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
}
输出结果:
英文:
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 (
"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\"?>\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>"
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论