feat: add text annotation tool
This commit is contained in:
@@ -8,8 +8,15 @@ import (
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
xfont "golang.org/x/image/font"
|
||||
"golang.org/x/image/font/basicfont"
|
||||
"golang.org/x/image/font/opentype"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
// Annotation describes a user-drawn mark relative to the selected screenshot.
|
||||
@@ -18,6 +25,7 @@ type Annotation struct {
|
||||
Tool string `json:"tool"`
|
||||
Color string `json:"color"`
|
||||
Points []Point `json:"points"`
|
||||
Text string `json:"text,omitempty"`
|
||||
}
|
||||
|
||||
type Point struct {
|
||||
@@ -55,6 +63,8 @@ func ApplyAnnotations(pngBytes []byte, annotations []Annotation, scale float64)
|
||||
drawRectOutline(dst, ann.Points, scale, width, c)
|
||||
case "ellipse":
|
||||
drawEllipseOutline(dst, ann.Points, scale, width, c)
|
||||
case "text":
|
||||
drawTextAnnotation(dst, ann, scale, c)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,3 +190,90 @@ func ordered(a, b float64) (float64, float64) {
|
||||
}
|
||||
return b, a
|
||||
}
|
||||
|
||||
func drawTextAnnotation(img *image.RGBA, ann Annotation, scale float64, c color.RGBA) {
|
||||
if len(ann.Points) == 0 {
|
||||
return
|
||||
}
|
||||
text := strings.TrimSpace(ann.Text)
|
||||
if text == "" {
|
||||
return
|
||||
}
|
||||
face := annotationFontFace(20 * scale)
|
||||
if face == nil {
|
||||
face = basicfont.Face7x13
|
||||
}
|
||||
|
||||
origin := scalePoint(ann.Points[0], scale)
|
||||
metrics := face.Metrics()
|
||||
lineHeight := metrics.Height
|
||||
if lineHeight <= 0 {
|
||||
lineHeight = fixed.I(int(math.Ceil(24 * scale)))
|
||||
}
|
||||
d := &xfont.Drawer{
|
||||
Dst: img,
|
||||
Src: image.NewUniform(c),
|
||||
Face: face,
|
||||
}
|
||||
baselineY := fixed.I(int(math.Round(origin.Y))) + metrics.Ascent
|
||||
x := fixed.I(int(math.Round(origin.X)))
|
||||
for _, line := range strings.Split(text, "\n") {
|
||||
line = strings.TrimRight(line, "\r")
|
||||
if line != "" {
|
||||
d.Dot = fixed.Point26_6{X: x, Y: baselineY}
|
||||
d.DrawString(line)
|
||||
}
|
||||
baselineY += lineHeight
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
annotationFontOnce sync.Once
|
||||
annotationFont *opentype.Font
|
||||
)
|
||||
|
||||
func annotationFontFace(size float64) xfont.Face {
|
||||
if size <= 0 {
|
||||
size = 20
|
||||
}
|
||||
annotationFontOnce.Do(func() {
|
||||
annotationFont = loadAnnotationFont()
|
||||
})
|
||||
if annotationFont == nil {
|
||||
return basicfont.Face7x13
|
||||
}
|
||||
face, err := opentype.NewFace(annotationFont, &opentype.FaceOptions{
|
||||
Size: size,
|
||||
DPI: 72,
|
||||
Hinting: xfont.HintingFull,
|
||||
})
|
||||
if err != nil {
|
||||
return basicfont.Face7x13
|
||||
}
|
||||
return face
|
||||
}
|
||||
|
||||
func loadAnnotationFont() *opentype.Font {
|
||||
paths := []string{
|
||||
"/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
|
||||
"/Library/Fonts/Arial Unicode.ttf",
|
||||
"/System/Library/Fonts/Hiragino Sans GB.ttc",
|
||||
"/System/Library/Fonts/PingFang.ttc",
|
||||
"/System/Library/Fonts/Helvetica.ttc",
|
||||
}
|
||||
for _, path := range paths {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if collection, err := opentype.ParseCollection(data); err == nil && collection.NumFonts() > 0 {
|
||||
if font, err := collection.Font(0); err == nil {
|
||||
return font
|
||||
}
|
||||
}
|
||||
if font, err := opentype.Parse(data); err == nil {
|
||||
return font
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -51,3 +51,45 @@ func TestApplyAnnotationsNoAnnotationsReturnsOriginalBytes(t *testing.T) {
|
||||
t.Fatalf("expected original bytes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyAnnotationsDrawsTextIntoPNG(t *testing.T) {
|
||||
src := image.NewRGBA(image.Rect(0, 0, 120, 80))
|
||||
draw.Draw(src, src.Bounds(), &image.Uniform{C: color.White}, image.Point{}, draw.Src)
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := png.Encode(&buf, src); err != nil {
|
||||
t.Fatalf("encode source: %v", err)
|
||||
}
|
||||
|
||||
out, err := ApplyAnnotations(buf.Bytes(), []Annotation{
|
||||
{
|
||||
Tool: "text",
|
||||
Color: "#ef4444",
|
||||
Points: []Point{
|
||||
{X: 10, Y: 10},
|
||||
},
|
||||
Text: "T",
|
||||
},
|
||||
}, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("apply annotations: %v", err)
|
||||
}
|
||||
|
||||
img, err := png.Decode(bytes.NewReader(out))
|
||||
if err != nil {
|
||||
t.Fatalf("decode output: %v", err)
|
||||
}
|
||||
found := false
|
||||
for y := 8; y < 40 && !found; y++ {
|
||||
for x := 8; x < 40; x++ {
|
||||
got := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
|
||||
if got.R > 180 && got.G < 180 && got.B < 180 {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("expected red text pixels near annotation point")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user