summaryrefslogtreecommitdiff
path: root/cmd/orca/display.go
blob: 7eaf778340595a2c1611788b893203d654d47e89 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main

import (
	"embed"
	"image"
	"image/color"
	"image/draw"
	_ "image/png"
	"io"
	"io/fs"
	"os"

	"golang.org/x/image/font"
	"golang.org/x/image/font/opentype"
	"golang.org/x/image/font/sfnt"
	"golang.org/x/image/math/fixed"
)

// openSFNT opens the specified font file
// It keeps the file open indefinitely for lazy loading when the font is used.
// It never attempts to close the file and is meant only for loading fonts permanently at init time.
func openSFNT(fs fs.FS, path string) *sfnt.Font {
	f, err := fs.Open(path)
	if err != nil {
		panic(err)
	}
	font, err := sfnt.ParseReaderAt(f.(io.ReaderAt))
	if err != nil {
		panic(err)
	}
	return font
}

func openImage(fs fs.FS, path string) image.Image {
	f, err := fs.Open(path)
	if err != nil {
		panic(err)
	}
	img, _, err := image.Decode(f)
	if err != nil {
		panic(err)
	}
	return img
}

//go:embed *.ttf *.png
var embedFS embed.FS
var qtFonts = os.DirFS("/usr/lib/fonts")

var (
	dejaVuSans       = openSFNT(qtFonts, "DejaVuSans.ttf")
	notoSans_Regular = openSFNT(embedFS, "NotoSans-Regular.ttf")
	notoSans_Bold    = openSFNT(embedFS, "NotoSans-Bold.ttf")
)

var (
	orcaImg  = openImage(embedFS, "orca.png")
	belowImg = openImage(embedFS, "below.png")
)

type justify int

const (
	_ justify = iota
	justifyLeft
	justifyCenter
	justifyRight
)

func drawText(img draw.Image, sfnt *sfnt.Font, size float64, just justify, c color.Color, dot fixed.Point26_6, text string) {
	face, err := opentype.NewFace(sfnt, &opentype.FaceOptions{
		Size:    size,
		DPI:     72,
		Hinting: font.HintingNone,
	})
	if err != nil {
		panic(err)
	}
	defer face.Close()
	drawer := font.Drawer{
		Dst:  img,
		Src:  &image.Uniform{c},
		Face: face,
		Dot:  dot,
	}
	_, adv := drawer.BoundString(text)
	var offset fixed.Int26_6
	switch just {
	case justifyCenter:
		offset = adv / 2
	case justifyRight:
		offset = adv
	}
	drawer.Dot.X -= offset
	drawer.DrawString(text)
}

func drawDisplay(img draw.Image, s displayState) {
	draw.Draw(img, img.Bounds(), &image.Uniform{color.RGBA{34, 31, 32, 255}}, image.Point{}, draw.Src)
	drawText(img, dejaVuSans, 20, justifyLeft, color.White, fixed.P(50, 25), s.now.Format("Jan 02, 2006"))
	drawText(img, dejaVuSans, 20, justifyRight, color.White, fixed.P(750, 25), s.now.Format("3:04 pm"))
	drawText(img, notoSans_Regular, 16, justifyCenter, color.White, fixed.P(400, 25), "V1234567-D98765")
	draw.Draw(img, orcaImg.Bounds().Add(image.Point{400 - orcaImg.Bounds().Dx()/2, 75}), orcaImg, image.Point{}, draw.Over)
	if s.cvend.ready {
		if !s.cvend.hasCard {
			drawText(img, notoSans_Bold, 48, justifyCenter, color.White, fixed.P(400, 330), "Tap below")
		}
		draw.Draw(img, belowImg.Bounds().Add(image.Point{400 - belowImg.Bounds().Dx()/2, 380}), belowImg, image.Point{}, draw.Over)
	}
}