display-test/drawer/font.go
2024-09-30 21:56:45 +03:00

355 lines
11 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package drawer
import (
"log"
"golang.org/x/text/encoding/charmap"
)
const (
CharGap = 1
LineGap = 1
FontCharW = 5
FontCharH = 7
LineH = FontCharH + LineGap
)
// Standard ASCII 5x7 font
var stdFont = []byte{
0x0, 0x0, 0x0, 0x0, 0x0, // Empty Cell
0x3e, 0x5b, 0x4f, 0x5b, 0x3e, // Sad Smiley
0x3e, 0x6b, 0x4f, 0x6b, 0x3e, // Happy Smiley
0x1c, 0x3e, 0x7c, 0x3e, 0x1c, // Heart
0x18, 0x3c, 0x7e, 0x3c, 0x18, // Diamond
0x1c, 0x57, 0x7d, 0x57, 0x1c, // Clubs
0x1c, 0x5e, 0x7f, 0x5e, 0x1c, // Spades
0x18, 0x3c, 0x18, 0x0, 0x0, // Bullet Point
0xff, 0xe7, 0xc3, 0xe7, 0xff, // Rev Bullet Point
0x18, 0x24, 0x18, 0x0, 0x0, // Hollow Bullet Point
0xff, 0xe7, 0xdb, 0xe7, 0xff, // Rev Hollow BP
0x30, 0x48, 0x3a, 0x6, 0xe, // Male
0x26, 0x29, 0x79, 0x29, 0x26, // Female
0x40, 0x7f, 0x5, 0x5, 0x7, // Music Note 1
0x40, 0x7f, 0x5, 0x25, 0x3f, // Music Note 2
0x5a, 0x3c, 0xe7, 0x3c, 0x5a, // Snowflake
0x7f, 0x3e, 0x1c, 0x1c, 0x8, // Right Pointer
0x8, 0x1c, 0x1c, 0x3e, 0x7f, // Left Pointer
0x14, 0x22, 0x7f, 0x22, 0x14, // UpDown Arrows
0x5f, 0x5f, 0x0, 0x5f, 0x5f, // Double Exclamation
0x6, 0x9, 0x7f, 0x1, 0x7f, // Paragraph Mark
0x66, 0x89, 0x95, 0x6a, 0x0, // Section Mark
0x60, 0x60, 0x60, 0x60, 0x60, // Double Underline
0x94, 0xa2, 0xff, 0xa2, 0x94, // UpDown Underlined
0x8, 0x4, 0x7e, 0x4, 0x8, // Up Arrow
0x10, 0x20, 0x7e, 0x20, 0x10, // Down Arrow
0x8, 0x8, 0x2a, 0x1c, 0x8, // Right Arrow
0x8, 0x1c, 0x2a, 0x8, 0x8, // Left Arrow
0x1e, 0x10, 0x10, 0x10, 0x10, // Angled
0xc, 0x1e, 0xc, 0x1e, 0xc, // Squashed #
0x30, 0x38, 0x3e, 0x38, 0x30, // Up Pointer
0x6, 0xe, 0x3e, 0xe, 0x6, // Down Pointer
0x0, 0x0, 0x0, 0x0, 0x0, // Space
0x5f, 0x0, 0x0, 0x0, 0x0, // !
0x3, 0x0, 0x3, 0x0, 0x0, // "
0x14, 0x3e, 0x14, 0x3e, 0x14, // #
0x24, 0x6a, 0x2b, 0x12, 0x0, // $
0x63, 0x13, 0x8, 0x64, 0x63, // %
0x36, 0x49, 0x56, 0x20, 0x50, // &
0x3, 0x0, 0x0, 0x0, 0x0, // '
0x1c, 0x22, 0x41, 0x0, 0x0, // (
0x41, 0x22, 0x1c, 0x0, 0x0, // )
0x28, 0x18, 0xe, 0x18, 0x28, // *
0x8, 0x8, 0x3e, 0x8, 0x8, // +
0xb0, 0x70, 0x0, 0x0, 0x0, // ,
0x8, 0x8, 0x8, 0x8, 0x0, // -
0x60, 0x60, 0x0, 0x0, 0x0, // .
0x60, 0x18, 0x6, 0x1, 0x0, // /
0x3e, 0x41, 0x41, 0x3e, 0x0, // 0
0x42, 0x7f, 0x40, 0x0, 0x0, // 1
0x62, 0x51, 0x49, 0x46, 0x0, // 2
0x22, 0x41, 0x49, 0x36, 0x0, // 3
0x18, 0x14, 0x12, 0x7f, 0x0, // 4
0x27, 0x45, 0x45, 0x39, 0x0, // 5
0x3e, 0x49, 0x49, 0x30, 0x0, // 6
0x61, 0x11, 0x9, 0x7, 0x0, // 7
0x36, 0x49, 0x49, 0x36, 0x0, // 8
0x6, 0x49, 0x49, 0x3e, 0x0, // 9
0x00, 0x0, 0x22, 0x0, 0x0, // :
0x80, 0x50, 0x0, 0x0, 0x0, // ;
0x10, 0x28, 0x44, 0x0, 0x0, // <
0x14, 0x14, 0x14, 0x0, 0x0, // =
0x44, 0x28, 0x10, 0x0, 0x0, // >
0x2, 0x59, 0x9, 0x6, 0x0, // ?
0x3e, 0x49, 0x55, 0x5d, 0xe, // @
0x7e, 0x11, 0x11, 0x7e, 0x0, // A
0x7f, 0x49, 0x49, 0x36, 0x0, // B
0x3e, 0x41, 0x41, 0x22, 0x0, // C
0x7f, 0x41, 0x41, 0x3e, 0x0, // D
0x7f, 0x49, 0x49, 0x41, 0x0, // E
0x7f, 0x9, 0x9, 0x1, 0x0, // F
0x3e, 0x41, 0x49, 0x7a, 0x0, // G
0x7f, 0x8, 0x8, 0x7f, 0x0, // H
0x41, 0x7f, 0x41, 0x0, 0x0, // I
0x30, 0x40, 0x41, 0x3f, 0x0, // J
0x7f, 0x8, 0x14, 0x63, 0x0, // K
0x7f, 0x40, 0x40, 0x40, 0x0, // L
0x7f, 0x2, 0xc, 0x2, 0x7f, // M
0x7f, 0x4, 0x8, 0x10, 0x7f, // N
0x3e, 0x41, 0x41, 0x3e, 0x0, // O
0x7f, 0x9, 0x9, 0x6, 0x0, // P
0x3e, 0x41, 0x41, 0xbe, 0x0, // Q
0x7f, 0x9, 0x9, 0x76, 0x0, // R
0x46, 0x49, 0x49, 0x32, 0x0, // S
0x1, 0x1, 0x7f, 0x1, 0x1, // T
0x3f, 0x40, 0x40, 0x3f, 0x0, // U
0xf, 0x30, 0x40, 0x30, 0xf, // V
0x3f, 0x40, 0x38, 0x40, 0x3f, // W
0x63, 0x14, 0x8, 0x14, 0x63, // X
0x7, 0x8, 0x70, 0x8, 0x7, // Y
0x61, 0x51, 0x49, 0x47, 0x0, // Z
0x7f, 0x41, 0x0, 0x0, 0x0, // [
0x1, 0x6, 0x18, 0x60, 0x0, //
0x41, 0x7f, 0x0, 0x0, 0x0, // ]
0x2, 0x1, 0x2, 0x0, 0x0, // ^
0x40, 0x40, 0x40, 0x40, 0x0, // _
0x1, 0x2, 0x0, 0x0, 0x0, // `
0x20, 0x54, 0x54, 0x78, 0x0, // a
0x7f, 0x44, 0x44, 0x38, 0x0, // b
0x38, 0x44, 0x44, 0x28, 0x0, // c
0x38, 0x44, 0x44, 0x7f, 0x0, // d
0x38, 0x54, 0x54, 0x18, 0x0, // e
0x4, 0x7e, 0x5, 0x0, 0x0, // f
0x98, 0xa4, 0xa4, 0x78, 0x0, // g
0x7f, 0x4, 0x4, 0x78, 0x0, // h
0x44, 0x7d, 0x40, 0x0, 0x0, // i
0x40, 0x80, 0x84, 0x7d, 0x0, // j
0x7f, 0x10, 0x28, 0x44, 0x0, // k
0x41, 0x7f, 0x40, 0x0, 0x0, // l
0x7c, 0x4, 0x7c, 0x4, 0x78, // m
0x7c, 0x4, 0x4, 0x78, 0x0, // n
0x38, 0x44, 0x44, 0x38, 0x0, // o
0xfc, 0x24, 0x24, 0x18, 0x0, // p
0x18, 0x24, 0x24, 0xfc, 0x0, // q
0x7c, 0x8, 0x4, 0x4, 0x0, // r
0x48, 0x54, 0x54, 0x24, 0x0, // s
0x4, 0x3f, 0x44, 0x0, 0x0, // t
0x3c, 0x40, 0x40, 0x7c, 0x0, // u
0x1c, 0x20, 0x40, 0x20, 0x1c, // v
0x3c, 0x40, 0x3c, 0x40, 0x3c, // w
0x44, 0x28, 0x10, 0x28, 0x44, // x
0x9c, 0xa0, 0xa0, 0x7c, 0x0, // y
0x64, 0x54, 0x4c, 0x0, 0x0, // z
0x8, 0x36, 0x41, 0x0, 0x0, // {
0x7f, 0x0, 0x0, 0x0, 0x0, // |
0x41, 0x36, 0x8, 0x0, 0x0, // }
0x8, 0x4, 0x8, 0x4, 0x0, // ~
0x0, 0x0, 0x0, 0x0, 0x0, // Hollow Up Arrow
0x7f, 0x55, 0x49, 0x55, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x03, 0x03, 0x00, 0x00, 0x00, // °
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7f, 0x41, 0x41, 0x41, 0x7f, // Empty
0x7e, 0x11, 0x11, 0x7e, 0x0, // А
0x7f, 0x49, 0x49, 0x31, 0x0, // Б
0x7f, 0x49, 0x49, 0x36, 0x0, // В
0x7f, 0x1, 0x1, 0x1, 0x0, // Г
0xc0, 0x7e, 0x41, 0x7f, 0xc0, // Д
0x7f, 0x49, 0x49, 0x41, 0x0, // Е
0x77, 0x8, 0x7f, 0x8, 0x77, // Ж
0x22, 0x41, 0x49, 0x36, 0x0, // З
0x7f, 0x20, 0x10, 0x7f, 0x0, // И
0x7f, 0x20, 0x11, 0x7d, 0x0, // Й
0x7f, 0x8, 0x14, 0x63, 0x0, // К
0x40, 0x3c, 0x2, 0x7f, 0x0, // Л
0x7f, 0x2, 0xc, 0x2, 0x7f, // М
0x7f, 0x8, 0x8, 0x7f, 0x0, // Н
0x3e, 0x41, 0x41, 0x3e, 0x0, // О
0x7f, 0x1, 0x1, 0x7f, 0x0, // П
0x7f, 0x9, 0x9, 0x6, 0x0, // Р
0x3e, 0x41, 0x41, 0x22, 0x0, // С
0x1, 0x1, 0x7f, 0x1, 0x1, // Т
0x47, 0x48, 0x48, 0x3f, 0x0, // У
0xe, 0x11, 0x7f, 0x11, 0xe, // Ф
0x63, 0x14, 0x8, 0x14, 0x63, // Х
0x7f, 0x40, 0x40, 0x7f, 0xc0, // Ц
0xf, 0x8, 0x8, 0x7f, 0x0, // Ч
0x7f, 0x40, 0x7c, 0x40, 0x7f, // Ш
0x7f, 0x40, 0x7c, 0x40, 0xff, // Щ
0x1, 0x7f, 0x48, 0x48, 0x30, // Ъ
0x7f, 0x48, 0x30, 0x0, 0x7f, // Ы
0x7f, 0x48, 0x48, 0x30, 0x0, // Ь
0x41, 0x49, 0x49, 0x3e, 0x0, // Э
0x7f, 0x8, 0x3e, 0x41, 0x3e, // Ю
0x46, 0x29, 0x19, 0x7f, 0x0, // Я
0x20, 0x54, 0x54, 0x78, 0x0, // а
0x3c, 0x4a, 0x4a, 0x31, 0x0, // б
0x7e, 0x59, 0x56, 0x20, 0x0, // в
0x7c, 0x4, 0x4, 0x4, 0x0, // г
0xc0, 0x78, 0x44, 0x7c, 0xc0, // д
0x38, 0x54, 0x54, 0x18, 0x0, // е
0x6c, 0x10, 0x7c, 0x10, 0x6c, // ж
0x48, 0x84, 0x94, 0x68, 0x0, // з
0x7c, 0x20, 0x10, 0x7c, 0x0, // и
0x7c, 0x21, 0x11, 0x7c, 0x0, // й
0x7c, 0x10, 0x28, 0x44, 0x0, // к
0x40, 0x38, 0x4, 0x7c, 0x0, // л
0x7c, 0x8, 0x10, 0x8, 0x7c, // м
0x7c, 0x10, 0x10, 0x7c, 0x0, // н
0x38, 0x44, 0x44, 0x38, 0x0, // о
0x7c, 0x4, 0x4, 0x7c, 0x0, // п
0xfc, 0x24, 0x24, 0x18, 0x0, // р
0x38, 0x44, 0x44, 0x28, 0x0, // с
0x4, 0x7c, 0x4, 0x0, 0x0, // т
0x9c, 0xa0, 0xa0, 0x7c, 0x0, // у
0x18, 0x24, 0xfc, 0x24, 0x18, // ф
0x44, 0x28, 0x10, 0x28, 0x44, // х
0x7c, 0x40, 0x40, 0x7c, 0xc0, // ц
0x1c, 0x10, 0x10, 0x7c, 0x0, // ч
0x7c, 0x40, 0x70, 0x40, 0x7c, // ш
0x7c, 0x40, 0x70, 0x40, 0xfc, // щ
0x4, 0x7c, 0x48, 0x48, 0x30, // ъ
0x7c, 0x48, 0x30, 0x0, 0x7c, // ы
0x7c, 0x48, 0x48, 0x30, 0x0, // ь
0x44, 0x54, 0x54, 0x38, 0x0, // э
0x7c, 0x10, 0x38, 0x44, 0x38, // ю
0x48, 0x34, 0x14, 0x7c, 0x0, // я
}
func IntMin(a, b int) int {
if a < b {
return a
}
return b
}
func IntMax(a, b int) int {
if a > b {
return a
}
return b
}
func (d *drawer) putChar(x0, y0 int, c byte) {
if int(c) > len(stdFont)/5 {
return
}
//if x0+7 < 0 || y0+5 < 0 || y0 >= d.img.Rect.Dy() || x0 >= d.img.Rect.Dx() {
// return
//}
minX := IntMax(x0, 0) - x0
minY := IntMax(y0, 0) - y0
maxY := IntMin(y0+7, d.img.Rect.Dy()) - y0
maxX := IntMin(x0+5, d.img.Rect.Dx()) - x0
for yi := minY; yi < maxY; yi++ {
lineaddr := (y0+yi)*d.img.Stride + x0
for xi := minX; xi < maxX; xi++ {
d.img.Pix[lineaddr+xi] = ((stdFont[int(c)*5+xi] >> yi) & 1) * 255
}
}
}
func convertStr(str string) []byte {
out, err := charmap.Windows1251.NewEncoder().Bytes([]byte(str))
if err != nil {
log.Println("ENCODE ERROR: ", err.Error())
}
return out
}
func (d *drawer) PutText(x0, y0 int, text string) {
d.dev.LockImg()
defer d.dev.UnlockImg()
asciiStr := convertStr(text)
//asciiStr := []byte(text)
// Create font drawer
x := x0
for _, c := range asciiStr {
d.putChar(x, y0, c)
x += CharGap + FontCharW
}
// Calculate update mask
mask := uint32(0)
// Implementation dependent !!!
// Now for mt12232
my0 := min(3, max(0, int((y0)/8)))
my1 := min(3, max(0, int((y0+FontCharH-1)/8)))
mx0 := min(1, max(0, int(x0/61)))
mx1 := min(1, max(0, int((x0+FontCharW*len(text)-1)/61)))
//log.Println(x0, y0, x1, y1)
for y := my0; y <= my1; y++ {
for x := mx0; x <= mx1; x++ {
mask |= (1 << (x*4 + y))
}
}
d.dev.FlushByMask(mask)
}