2018-11-14 17:08:36 +00:00
|
|
|
package freetype
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-11-16 09:58:44 +00:00
|
|
|
"image"
|
2018-11-15 16:57:41 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2018-11-14 17:08:36 +00:00
|
|
|
"strings"
|
2018-11-15 17:20:46 +00:00
|
|
|
"unicode/utf8"
|
2018-11-16 09:58:44 +00:00
|
|
|
|
|
|
|
_ "image/png" // Add PNG file loading support
|
|
|
|
|
|
|
|
"golang.org/x/image/draw"
|
|
|
|
"golang.org/x/image/math/fixed"
|
2018-11-14 17:08:36 +00:00
|
|
|
)
|
|
|
|
|
2018-11-15 16:57:41 +00:00
|
|
|
type emoji struct {
|
|
|
|
Codepoint []rune
|
|
|
|
IsEmoji bool
|
|
|
|
Path string
|
|
|
|
Sub emojiTable
|
2018-11-14 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 16:57:41 +00:00
|
|
|
func (e emoji) String() (str string) {
|
|
|
|
str = "Emoji("
|
|
|
|
for _, cprune := range e.Codepoint {
|
|
|
|
str += fmt.Sprintf("%U ", cprune)
|
|
|
|
}
|
|
|
|
str = strings.TrimRight(str, " ") + ")"
|
|
|
|
if e.Sub != nil {
|
|
|
|
str += "+"
|
|
|
|
}
|
|
|
|
if e.IsEmoji {
|
|
|
|
str += fmt.Sprintf("\n └ Path: %s", e.Path)
|
|
|
|
}
|
|
|
|
return str + "\n"
|
2018-11-14 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:20:46 +00:00
|
|
|
func (e emoji) Length() int {
|
|
|
|
total := 0
|
|
|
|
for _, r := range e.Codepoint {
|
|
|
|
total += utf8.RuneLen(r)
|
|
|
|
}
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
2018-11-15 16:57:41 +00:00
|
|
|
type emojiTable map[rune]emoji
|
|
|
|
|
2018-11-15 17:20:46 +00:00
|
|
|
func (em emojiTable) Find(str string) *emoji {
|
2018-11-15 16:57:41 +00:00
|
|
|
for i, r := range str {
|
|
|
|
e, ok := em[r]
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Check if there are more bytes to check
|
|
|
|
if len(str) > i && e.Sub != nil {
|
2018-11-15 17:37:49 +00:00
|
|
|
rlen := utf8.RuneLen(r)
|
|
|
|
newemj := e.Sub.Find(str[i+rlen:])
|
2018-11-15 17:20:46 +00:00
|
|
|
if newemj != nil {
|
|
|
|
return newemj
|
2018-11-15 16:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if e.IsEmoji {
|
2018-11-15 17:20:46 +00:00
|
|
|
return &e
|
2018-11-15 16:57:41 +00:00
|
|
|
}
|
2018-11-14 17:08:36 +00:00
|
|
|
}
|
2018-11-15 17:20:46 +00:00
|
|
|
return nil
|
2018-11-14 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 16:57:41 +00:00
|
|
|
func (em emojiTable) IsEmoji(cp rune) bool {
|
|
|
|
_, ok := em[cp]
|
|
|
|
return ok
|
2018-11-14 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 16:57:41 +00:00
|
|
|
func (em emojiTable) tostring(indent string, nomarker bool) (str string) {
|
|
|
|
counter := len(em)
|
|
|
|
marker := "│ "
|
|
|
|
if nomarker {
|
|
|
|
marker = " "
|
|
|
|
}
|
|
|
|
for r, emo := range em {
|
|
|
|
listr := "├"
|
|
|
|
if counter == 1 {
|
|
|
|
listr = "└"
|
|
|
|
}
|
|
|
|
str += fmt.Sprintf(indent+"%s%s %U", marker, listr, r)
|
|
|
|
if emo.IsEmoji {
|
|
|
|
str += fmt.Sprintf(": %s", emo.Path)
|
|
|
|
}
|
|
|
|
str += "\n"
|
|
|
|
if emo.Sub != nil {
|
|
|
|
str += emo.Sub.tostring(indent+marker, counter == 1)
|
|
|
|
}
|
|
|
|
counter--
|
2018-11-14 17:08:36 +00:00
|
|
|
}
|
2018-11-15 16:57:41 +00:00
|
|
|
return
|
|
|
|
}
|
2018-11-14 17:08:36 +00:00
|
|
|
|
2018-11-15 16:57:41 +00:00
|
|
|
func (em emojiTable) String() string {
|
|
|
|
return "Emoji table\n" + em.tostring("", true)
|
|
|
|
}
|
2018-11-14 17:08:36 +00:00
|
|
|
|
2018-11-15 16:57:41 +00:00
|
|
|
func scanEmojiDirectory(emojipath string) (tab emojiTable, err error) {
|
|
|
|
tab = make(emojiTable)
|
|
|
|
filepath.Walk(emojipath, func(path string, info os.FileInfo, err error) error {
|
|
|
|
// Ignore non-images
|
|
|
|
if !strings.HasSuffix(strings.ToLower(path), ".png") {
|
|
|
|
return nil
|
2018-11-14 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 16:57:41 +00:00
|
|
|
// Get icon filename
|
|
|
|
emojiname := filepath.Base(path)
|
2018-11-14 17:08:36 +00:00
|
|
|
|
2018-11-15 16:57:41 +00:00
|
|
|
// Strip prefix and suffix
|
|
|
|
extsep := strings.LastIndexByte(emojiname, '.')
|
|
|
|
basesep := strings.IndexByte(emojiname, '_')
|
|
|
|
codepointstr := emojiname[basesep+2 : extsep]
|
2018-11-14 17:08:36 +00:00
|
|
|
|
2018-11-15 16:57:41 +00:00
|
|
|
// Split codepoints by separator (_)
|
|
|
|
codepointlist := strings.Split(codepointstr, "_")
|
|
|
|
|
|
|
|
// Parse codepoints to runes
|
|
|
|
curtab := &tab
|
|
|
|
codepoints := []rune{}
|
|
|
|
for cpi, cpstr := range codepointlist {
|
|
|
|
num, err := strconv.ParseInt(cpstr, 16, 32)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("malformed icon filename: %s (codepoints are not valid int32)", emojiname)
|
|
|
|
}
|
|
|
|
cprune := rune(num)
|
|
|
|
newemo := (*curtab)[cprune]
|
|
|
|
codepoints = append(codepoints, cprune)
|
|
|
|
newemo.Codepoint = codepoints[:]
|
|
|
|
if len(codepointlist) < cpi+2 {
|
|
|
|
// Set as emoji
|
|
|
|
newemo.IsEmoji = true
|
|
|
|
newemo.Path = path
|
|
|
|
} else {
|
2018-11-15 17:37:49 +00:00
|
|
|
// Add sub-entry if not existant
|
|
|
|
if newemo.Sub == nil {
|
|
|
|
newemo.Sub = make(emojiTable)
|
|
|
|
}
|
2018-11-15 16:57:41 +00:00
|
|
|
}
|
|
|
|
(*curtab)[cprune] = newemo
|
|
|
|
curtab = &newemo.Sub
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return tab, err
|
2018-11-14 17:08:36 +00:00
|
|
|
}
|
2018-11-16 09:58:44 +00:00
|
|
|
|
|
|
|
const emojiScale = fixed.Int26_6(100)
|
|
|
|
|
|
|
|
func loadIconAtSize(path string, size fixed.Int26_6) (image.Image, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
img, _, err := image.Decode(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
scale := size.Mul(emojiScale).Round()
|
|
|
|
scaled := image.NewRGBA(image.Rect(0, 0, scale, scale))
|
|
|
|
draw.BiLinear.Scale(scaled, scaled.Bounds(), img, img.Bounds(), draw.Over, nil)
|
|
|
|
|
|
|
|
return scaled, nil
|
|
|
|
}
|