问题描述
给定 image.RGBA
、坐标和一行文本,如何添加带有任何普通固定字体的简单标签?例如.Face7x13
来自 font/basicfont
.
Given image.RGBA
, coordinates, and a line of text, how do I add a simple label with any plain fixed font? E.g. Face7x13
from font/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.
推荐答案
golang.org/x/image/font
包只定义了字体界面和在图像上绘制文本的接口.
The golang.org/x/image/font
package just defines interfaces for font faces and drawing text on images.
您可以使用 Freetype 字体光栅化器的 Go 实现:github.com/golang/freetype
.
You may use the Go implementation of Freetype font rasterizer: github.com/golang/freetype
.
密钥类型是freetype.Context
,它有你需要的所有方法.
The key type is freetype.Context
, it has all the methods you need.
有关完整示例,请查看此文件:example/freetype/main.go
.此示例加载字体文件,创建和配置 freetype.Context
,在图像上绘制文本并将结果图像保存到文件中.
For a complete example, check out this file: example/freetype/main.go
. This example loads a font file, creates and configures freetype.Context
, draws text on image and saves the result image to file.
假设您已经加载了字体文件,并配置了 c
上下文(请参阅示例如何执行此操作).那么您的 addLabel()
函数可能如下所示:
Let's assume you already have the font file loaded, and a c
context configured (see the example how to do that). Then your addLabel()
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
}
}
如果您不想麻烦 freetype
包和外部字体文件,font/basicfont
包包含一个名为 Face7x13
的基本字体,其图形数据完全独立.你可以这样使用它:
If you don't want to hassle with the freetype
package and external font files, the font/basicfont
package contains a basic font named Face7x13
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)
}
addLabel()
函数的用法如下:下面的代码创建一个新图像,在其上绘制 "Hello Go"
文本并将其保存在一个名为 hello-go.png
的文件:
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 named hello-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)
}
}
注意上面的代码还需要"image/png"
包导入.
Note the above code also requires the "image/png"
package import.
另请注意,给定的 y
坐标将是文本的底线.所以如果你想画一条线到左上角,你必须使用x = 0
和y = 13
(13是这个Face7x13的高度
字体).如果您愿意,可以通过从 y
坐标中减去 13
将其构建到 addLabel()
函数中,以便传递的 y
坐标将是绘制文本的顶部坐标.
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 use x = 0
and y = 13
(13 is the height of this Face7x13
font). If you wish, you could build this into the addLabel()
function by subtracting 13
from the y
coordinate, so that the passed y
coordinate would be the top coordinate at which the text will be drawn.
在 golang 中还有一个额外的自包含字体.org/x/image/font/inconsolata
包具有常规和粗体样式,要使用它们,您只需要指定不同的 :
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 different Face
in addLabel()
:
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 中为图像添加简单的文本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!