Compare commits
2 commits
99e86cde50
...
bcb643ee78
Author | SHA1 | Date | |
---|---|---|---|
bcb643ee78 | |||
11c9d9498b |
2 changed files with 55 additions and 12 deletions
24
emoji.go
24
emoji.go
|
@ -1,4 +1,4 @@
|
||||||
package freetype
|
package emoji
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -9,12 +9,12 @@ import (
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Emoji is a node of an EmojiTable
|
// Emoji is a node of a Table
|
||||||
type Emoji struct {
|
type Emoji struct {
|
||||||
Codepoint []rune
|
Codepoint []rune
|
||||||
IsEmoji bool
|
IsEmoji bool
|
||||||
Path string
|
Path string
|
||||||
Sub EmojiTable
|
Sub Table
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Emoji) String() (str string) {
|
func (e Emoji) String() (str string) {
|
||||||
|
@ -41,11 +41,11 @@ func (e Emoji) Length() int {
|
||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
// EmojiTable is a table of detected Unicode codepoints sequences for which an emoticon is available
|
// Table is a table of detected Unicode codepoints sequences for which an emoticon is available
|
||||||
type EmojiTable map[rune]Emoji
|
type Table map[rune]Emoji
|
||||||
|
|
||||||
// Find checks if a given strings begins with an emoji made by one or more sequential runes
|
// Find checks if a given strings begins with an emoji made by one or more sequential runes
|
||||||
func (em EmojiTable) Find(str string) *Emoji {
|
func (em Table) Find(str string) *Emoji {
|
||||||
for i, r := range str {
|
for i, r := range str {
|
||||||
e, ok := em[r]
|
e, ok := em[r]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -67,12 +67,12 @@ func (em EmojiTable) Find(str string) *Emoji {
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEmoji checks whether the given rune is an emoji
|
// IsEmoji checks whether the given rune is an emoji
|
||||||
func (em EmojiTable) IsEmoji(cp rune) bool {
|
func (em Table) IsEmoji(cp rune) bool {
|
||||||
_, ok := em[cp]
|
_, ok := em[cp]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (em EmojiTable) tostring(indent string, nomarker bool) (str string) {
|
func (em Table) tostring(indent string, nomarker bool) (str string) {
|
||||||
counter := len(em)
|
counter := len(em)
|
||||||
marker := "│ "
|
marker := "│ "
|
||||||
if nomarker {
|
if nomarker {
|
||||||
|
@ -96,7 +96,7 @@ func (em EmojiTable) tostring(indent string, nomarker bool) (str string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (em EmojiTable) String() string {
|
func (em Table) String() string {
|
||||||
return "Emoji table\n" + em.tostring("", true)
|
return "Emoji table\n" + em.tostring("", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,8 +105,8 @@ func (em EmojiTable) String() string {
|
||||||
// emoji_uXXXX_XXXX.png
|
// emoji_uXXXX_XXXX.png
|
||||||
// where XXXX are Unicode codepoints
|
// where XXXX are Unicode codepoints
|
||||||
// See <https://github.com/googlei18n/noto-emoji/tree/master/png/128>
|
// See <https://github.com/googlei18n/noto-emoji/tree/master/png/128>
|
||||||
func ScanEmojiDirectory(emojipath string) (tab EmojiTable, err error) {
|
func ScanEmojiDirectory(emojipath string) (tab Table, err error) {
|
||||||
tab = make(EmojiTable)
|
tab = make(Table)
|
||||||
filepath.Walk(emojipath, func(path string, info os.FileInfo, err error) error {
|
filepath.Walk(emojipath, func(path string, info os.FileInfo, err error) error {
|
||||||
// Ignore non-images
|
// Ignore non-images
|
||||||
if !strings.HasSuffix(strings.ToLower(path), ".png") {
|
if !strings.HasSuffix(strings.ToLower(path), ".png") {
|
||||||
|
@ -143,7 +143,7 @@ func ScanEmojiDirectory(emojipath string) (tab EmojiTable, err error) {
|
||||||
} else {
|
} else {
|
||||||
// Add sub-entry if not existant
|
// Add sub-entry if not existant
|
||||||
if newemo.Sub == nil {
|
if newemo.Sub == nil {
|
||||||
newemo.Sub = make(EmojiTable)
|
newemo.Sub = make(Table)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(*curtab)[cprune] = newemo
|
(*curtab)[cprune] = newemo
|
||||||
|
|
43
iterator.go
Normal file
43
iterator.go
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package emoji
|
||||||
|
|
||||||
|
// Fragment is either a rune or an emoji
|
||||||
|
type Fragment struct {
|
||||||
|
Offset int
|
||||||
|
IsEmoji bool
|
||||||
|
Emoji Emoji
|
||||||
|
Rune rune
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate iterates through a string and returns its runes (for characters) and emojis
|
||||||
|
func (em Table) Iterate(str string) <-chan Fragment {
|
||||||
|
c := make(chan Fragment)
|
||||||
|
go func() {
|
||||||
|
nextchar := 0
|
||||||
|
for index, r := range str {
|
||||||
|
// Check if we need to skip entries
|
||||||
|
if nextchar > index {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Check if rune is an emoji
|
||||||
|
if em.IsEmoji(r) {
|
||||||
|
icon := em.Find(str[index:])
|
||||||
|
if icon != nil {
|
||||||
|
nextchar = index + icon.Length() - 1
|
||||||
|
c <- Fragment{
|
||||||
|
IsEmoji: true,
|
||||||
|
Offset: index,
|
||||||
|
Emoji: *icon,
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c <- Fragment{
|
||||||
|
IsEmoji: false,
|
||||||
|
Offset: index,
|
||||||
|
Rune: r,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return c
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue