问题描述
给定 image.RGBA
,坐标和一行文字,如何添加一个简单的标签和任何普通的固定字体?例如。 Face7x13
来自。包主
image
image / color
image / png
os
)
func main (){
img:= image.NewRGBA(image.Rect(0,0,320,240))
x,y:= 100,100
addLabel(img,x,y, Test123)
png.Encode(os.Stdout,img)
}
func addLabel(img * image.RGBA,x,y int,label string){
col:= color.Black
//现在是什么?
对齐并不重要,但是如果我能写上面的标签,
我想避免像字体这样的外部可加载依赖项。
解决方案包只是定义字体的接口和图像上的文本。
你可以使用Go实现Freetype字体光栅化器:。
关键的类型是,它具有您需要的所有方法。
看看这个文件:。这个例子加载一个字体文件,创建并配置
freetype.Context
,在图像上绘制文本并将结果图像保存到文件中。
假设你已经加载了字体文件,并且配置了一个c
上下文(请参阅示例如何操作)。然后你的addLabel()
函数看起来像这样:
func addLabel (img * image.RGBA,x,y int,标签字符串){
c.SetDst(img)
size:= 12.0 //字体大小(像素)
pt:= freetype.Pt (x,y + int(c.PointToFixed(size)>> 6))
if _,err:= c.DrawString(label,pt); err!= nil {
//处理错误
}
}
包和外部字体文件争执,软件包包含一个名为
freetype的基本字体Face7x13
其图形数据是完全独立的。这是你如何使用它:import(
golang.org/x/image/font
golang.org/x/image/font/basicfont
golang.org/x/image/math/fixed
image
image / color
)
func addLabel(img * image.RGBA,x,y int,label string){
col:= color.RGBA {200,100,0,255}
point:= fixed.Point26_6 {fixed.Int26_6(x * 64),fixed.Int26_6(y * 64)}
d:=& font.Drawer {
Dst: img,
源:image.NewUniform(col),
面:basicfont.Face7x13,
点:点,
}
d.DrawString(标号)
$ b $ p
$ b $ p $addLabel()函数:下面的代码创建一个新的图像,在其上绘制
Hello Go
文本并保存在名为hello-go.png
:
$ bfunc main(){
img:= image .NewRGBA(image.Rect(0,0,300,100))
addLabel(img, 20,30,Hello Go)
$ bf,err:= os.Create(hello-go.png)
if err!= nil {
panic(err )
}
推迟f.Close()
if err:= png.Encode(f,img); err!= nil {
panic(err)
}
}
image / png
另请注意,给定的
y
坐标将成为底线的文字。所以如果你想画一条线到左上角,你必须使用x = 0
和y = 13
(13是这个Face7x13
字体的高度)。如果你愿意的话,你可以通过从中减去
坐标,以便传递的13
,把它建立到addLabel()
> yy
坐标将成为绘制文本的顶部坐标。
使用常规和粗体样式的包,只需要指定不同的中的https://godoc.org/golang.org/x/image/font#Facerel =noreferrer>
c> addLabel()
:importgolang.org/x/image/font/inconsolata
//使用Regular Inconsolata字体family:
面值:inconsolata.Regular8x16,
//要使用bold Inconsolata字体family:
面值:inconsolata.Bold8x16,
Given
image.RGBA
, coordinates, and a line of text, how do I add a simple label with any plain fixed font? E.g.Face7x13
fromfont/basicfont
.package main import ( "image" "image/color" "image/png" "os" ) func main() { img := image.NewRGBA(image.Rect(0, 0, 320, 240)) x, y := 100, 100 addLabel(img, x, y, "Test123") png.Encode(os.Stdout, img) } func addLabel(img *image.RGBA, x, y int, label string) { col := color.Black // now what? }
Alignment doesn't really matter, but best if I could write the label above a line which starts at the coordinates.
And I would like to avoid external loadable dependencies like fonts.
解决方案The
golang.org/x/image/font
package just defines interfaces for font faces and drawing text on images.You may use the Go implementation of Freetype font rasterizer:
github.com/golang/freetype
.The key type is
freetype.Context
, it has all the methods you need.For a complete example, check out this file:
example/freetype/main.go
. This example loads a font file, creates and configuresfreetype.Context
, draws text on image and saves the result image to file.Let's assume you already have the font file loaded, and a
c
context configured (see the example how to do that). Then youraddLabel()
function could look like this:func addLabel(img *image.RGBA, x, y int, label string) { c.SetDst(img) size := 12.0 // font size in pixels pt := freetype.Pt(x, y+int(c.PointToFixed(size)>>6)) if _, err := c.DrawString(label, pt); err != nil { // handle error } }
If you don't want to hassle with the
freetype
package and external font files, thefont/basicfont
package contains a basic font namedFace7x13
whose graphical data is entirely self-contained. This is how you could use that:import ( "golang.org/x/image/font" "golang.org/x/image/font/basicfont" "golang.org/x/image/math/fixed" "image" "image/color" ) func addLabel(img *image.RGBA, x, y int, label string) { col := color.RGBA{200, 100, 0, 255} point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)} d := &font.Drawer{ Dst: img, Src: image.NewUniform(col), Face: basicfont.Face7x13, Dot: point, } d.DrawString(label) }
This is how this
addLabel()
function can be used: the code below creates a new image, draws the"Hello Go"
text on it and saves it in a file namedhello-go.png
:func main() { img := image.NewRGBA(image.Rect(0, 0, 300, 100)) addLabel(img, 20, 30, "Hello Go") f, err := os.Create("hello-go.png") if err != nil { panic(err) } defer f.Close() if err := png.Encode(f, img); err != nil { panic(err) } }
Note the above code also requires the
"image/png"
package import.Also note that the
y
coordinate given will be the bottom line of the text. So if you want to draw a line to the top left corner, you have to usex = 0
andy = 13
(13 is the height of thisFace7x13
font). If you wish, you could build this into theaddLabel()
function by subtracting13
from they
coordinate, so that the passedy
coordinate would be the top coordinate at which the text will be drawn.There is also an additional self-contained font in the
golang.org/x/image/font/inconsolata
package with regular and bold style, to use them, you only need to specify a differentFace
inaddLabel()
:import "golang.org/x/image/font/inconsolata" // To use regular Inconsolata font family: Face: inconsolata.Regular8x16, // To use bold Inconsolata font family: Face: inconsolata.Bold8x16,
这篇关于如何在Go中添加一个简单的文本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!