Compare commits
No commits in common. "bcb643ee7855775f9f21987d24b09b3f6207097a" and "99e86cde5040126963958a67d5b25e1ed3114492" have entirely different histories.
bcb643ee78
...
99e86cde50
2 changed files with 12 additions and 55 deletions
24
emoji.go
24
emoji.go
|
@ -1,4 +1,4 @@
|
||||||
package emoji
|
package freetype
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -9,12 +9,12 @@ import (
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Emoji is a node of a Table
|
// Emoji is a node of an EmojiTable
|
||||||
type Emoji struct {
|
type Emoji struct {
|
||||||
Codepoint []rune
|
Codepoint []rune
|
||||||
IsEmoji bool
|
IsEmoji bool
|
||||||
Path string
|
Path string
|
||||||
Sub Table
|
Sub EmojiTable
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Table is a table of detected Unicode codepoints sequences for which an emoticon is available
|
// EmojiTable is a table of detected Unicode codepoints sequences for which an emoticon is available
|
||||||
type Table map[rune]Emoji
|
type EmojiTable 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 Table) Find(str string) *Emoji {
|
func (em EmojiTable) 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 Table) Find(str string) *Emoji {
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEmoji checks whether the given rune is an emoji
|
// IsEmoji checks whether the given rune is an emoji
|
||||||
func (em Table) IsEmoji(cp rune) bool {
|
func (em EmojiTable) IsEmoji(cp rune) bool {
|
||||||
_, ok := em[cp]
|
_, ok := em[cp]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (em Table) tostring(indent string, nomarker bool) (str string) {
|
func (em EmojiTable) 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 Table) tostring(indent string, nomarker bool) (str string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (em Table) String() string {
|
func (em EmojiTable) String() string {
|
||||||
return "Emoji table\n" + em.tostring("", true)
|
return "Emoji table\n" + em.tostring("", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,8 +105,8 @@ func (em Table) 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 Table, err error) {
|
func ScanEmojiDirectory(emojipath string) (tab EmojiTable, err error) {
|
||||||
tab = make(Table)
|
tab = make(EmojiTable)
|
||||||
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 Table, 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(Table)
|
newemo.Sub = make(EmojiTable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(*curtab)[cprune] = newemo
|
(*curtab)[cprune] = newemo
|
||||||
|
|
43
iterator.go
43
iterator.go
|
@ -1,43 +0,0 @@
|
||||||
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