-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtitle.go
59 lines (47 loc) · 1.43 KB
/
subtitle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"image"
"image/color"
"image/draw"
"github.com/boombuler/barcode"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
func subtitleBarcode(bc barcode.Barcode) image.Image {
fontFace := basicfont.Face7x13
fontColor := color.RGBA{0x0, 0x0, 0x0, 0xff}
margin := 5 // Space between barcode and text
// Get the bounds of the string
bounds, _ := font.BoundString(fontFace, bc.Content())
widthTxt := int((bounds.Max.X - bounds.Min.X) / 64)
heightTxt := int((bounds.Max.Y - bounds.Min.Y) / 64)
// calc width and height
width := widthTxt
if bc.Bounds().Dx() > width {
width = bc.Bounds().Dx()
}
height := heightTxt + bc.Bounds().Dy() + margin
// create result img
img := image.NewRGBA(image.Rect(0, 0, width, height))
// draw white background
whiteBG := color.RGBA{0xff, 0xff, 0xff, 0xff}
draw.Draw(img, image.Rect(0, 0, width, height), &image.Uniform{whiteBG}, bc.Bounds().Min, draw.Over)
// draw the barcode
draw.Draw(img, image.Rect(0, 0, bc.Bounds().Dx(), bc.Bounds().Dy()), bc, bc.Bounds().Min, draw.Over)
// TextPt
offsetY := bc.Bounds().Dy() + margin - int(bounds.Min.Y/64)
offsetX := (width - widthTxt) / 2
point := fixed.Point26_6{
X: fixed.Int26_6(offsetX * 64),
Y: fixed.Int26_6(offsetY * 64),
}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(fontColor),
Face: fontFace,
Dot: point,
}
d.DrawString(bc.Content())
return img
}