emoji/emoji.go

156 lines
3.4 KiB
Go
Raw Permalink Normal View History

2018-11-16 10:39:41 +00:00
package emoji
2018-11-16 10:30:23 +00:00
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"unicode/utf8"
)
2018-11-16 10:39:41 +00:00
// Emoji is a node of a Table
2018-11-16 10:30:23 +00:00
type Emoji struct {
Codepoint []rune
IsEmoji bool
Path string
2018-11-16 10:39:41 +00:00
Sub Table
2018-11-16 10:30:23 +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"
}
// Length returns the lenght of the emoji in bytes
func (e Emoji) Length() int {
total := 0
for _, r := range e.Codepoint {
total += utf8.RuneLen(r)
}
return total
}
2018-11-16 10:39:41 +00:00
// Table is a table of detected Unicode codepoints sequences for which an emoticon is available
type Table map[rune]Emoji
2018-11-16 10:30:23 +00:00
// Find checks if a given strings begins with an emoji made by one or more sequential runes
2018-11-16 10:39:41 +00:00
func (em Table) Find(str string) *Emoji {
2018-11-16 10:30:23 +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 {
rlen := utf8.RuneLen(r)
newemj := e.Sub.Find(str[i+rlen:])
if newemj != nil {
return newemj
}
}
if e.IsEmoji {
return &e
}
}
return nil
}
// IsEmoji checks whether the given rune is an emoji
2018-11-16 10:39:41 +00:00
func (em Table) IsEmoji(cp rune) bool {
2018-11-16 10:30:23 +00:00
_, ok := em[cp]
return ok
}
2018-11-16 10:39:41 +00:00
func (em Table) tostring(indent string, nomarker bool) (str string) {
2018-11-16 10:30:23 +00:00
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--
}
return
}
2018-11-16 10:39:41 +00:00
func (em Table) String() string {
2018-11-16 10:30:23 +00:00
return "Emoji table\n" + em.tostring("", true)
}
// ScanEmojiDirectory scans a directory for images to use as icons
// Pictures must be named in this format:
// emoji_uXXXX_XXXX.png
// where XXXX are Unicode codepoints
// See <https://github.com/googlei18n/noto-emoji/tree/master/png/128>
2018-11-16 10:39:41 +00:00
func ScanEmojiDirectory(emojipath string) (tab Table, err error) {
tab = make(Table)
2018-11-16 14:21:02 +00:00
err = filepath.Walk(emojipath, func(path string, info os.FileInfo, err error) error {
2018-11-16 10:30:23 +00:00
// Ignore non-images
if !strings.HasSuffix(strings.ToLower(path), ".png") {
return nil
}
// Get icon filename
emojiname := filepath.Base(path)
// Strip prefix and suffix
extsep := strings.LastIndexByte(emojiname, '.')
basesep := strings.IndexByte(emojiname, '_')
codepointstr := emojiname[basesep+2 : extsep]
// 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 {
// Add sub-entry if not existant
if newemo.Sub == nil {
2018-11-16 10:39:41 +00:00
newemo.Sub = make(Table)
2018-11-16 10:30:23 +00:00
}
}
(*curtab)[cprune] = newemo
curtab = &newemo.Sub
}
return nil
})
return tab, err
}