text overlay positions preset

This commit is contained in:
Dmitry Sergeev
2026-09-16 09:55:24 +03:00
parent 8727a9a357
commit 12cdad0805
2 changed files with 55 additions and 7 deletions
+32 -3
View File
@@ -8,6 +8,7 @@ import (
"fmt"
"image"
"os"
"slices"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
@@ -22,6 +23,8 @@ type TextOverlay struct {
blocks []byte // pre-packed v210 tile: h rows x (w/6) blocks x 16 bytes
}
var TextPosAvailable []string = []string{"tl", "tc", "tr", "cc", "bl", "bc", "br"}
// LoadFace parses a TTF/OTF file and builds a render-ready face at the given
// pixel size (DPI 72, full hinting for crisp video text). Call once and
// reuse for every overlay.
@@ -48,7 +51,7 @@ func LoadFace(path string, size float64) (font.Face, error) {
// NewTextOverlay rasterizes text (white, anti-aliased) onto a black backing box.
// Box width and x are multiples of 6 so the box
// covers whole v210 blocks and the per-frame copy is word-aligned.
func NewTextOverlay(text string, frameW, frameH, posX, posY int, face font.Face) (*TextOverlay, error) {
func NewTextOverlay(text string, frameW, frameH, posX, posY int, textPos string, face font.Face) (*TextOverlay, error) {
if text == "" {
return nil, fmt.Errorf("text: empty string")
}
@@ -89,8 +92,34 @@ func NewTextOverlay(text string, frameW, frameH, posX, posY int, face font.Face)
return nil, fmt.Errorf("text: box %dpx does not fit frame height %dpx", topMargin+h, frameH)
}
// posX = ((frameW - w) / 2 / 6) * 6
// posY = topMargin
if textPos != "" {
if !slices.Contains(TextPosAvailable, textPos) {
return nil, fmt.Errorf("text: unknown text position preset '%s'", textPos)
}
switch textPos {
case "tl":
posX = 0
posY = 0
case "tc":
posX = ((frameW - w) / 2 / 6) * 6
posY = 0
case "tr":
posX = frameW - w
posY = 0
case "cc":
posX = ((frameW - w) / 2 / 6) * 6
posY = (frameH - h) / 2
case "bl":
posX = 0
posY = frameH - h
case "bc":
posX = ((frameW - w) / 2 / 6) * 6
posY = frameH - h
case "br":
posX = frameW - w
posY = frameH - h
}
}
o := &TextOverlay{
frameW: frameW,
x: posX,