Mini refactor though everything is still broken

This commit is contained in:
Hamcha 2018-11-15 18:20:46 +01:00
parent 591c93aefb
commit b32820410d
Signed by: hamcha
GPG Key ID: A40413D21021EAEE
3 changed files with 24 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import (
"path/filepath"
"strconv"
"strings"
"unicode/utf8"
)
type emoji struct {
@ -30,10 +31,17 @@ func (e emoji) String() (str string) {
return str + "\n"
}
func (e emoji) Length() int {
total := 0
for _, r := range e.Codepoint {
total += utf8.RuneLen(r)
}
return total
}
type emojiTable map[rune]emoji
func (em emojiTable) Find(str string) (emj emoji, length int) {
length = 0
func (em emojiTable) Find(str string) *emoji {
for i, r := range str {
e, ok := em[r]
if !ok {
@ -41,19 +49,16 @@ func (em emojiTable) Find(str string) (emj emoji, length int) {
}
// Check if there are more bytes to check
if len(str) > i && e.Sub != nil {
newemj, len := e.Sub.Find(str[i:])
if len > 0 {
length += len
emj = newemj
return
newemj := e.Sub.Find(str[i:])
if newemj != nil {
return newemj
}
}
if e.IsEmoji {
emj = e
return &e
}
length++
}
return
return nil
}
func (em emojiTable) IsEmoji(cp rune) bool {

View File

@ -96,12 +96,14 @@ func main() {
// Draw the text.
pt := freetype.Pt(10, 10+int(c.PointToFixed(*size)>>6))
for _, s := range text {
fmt.Println(s)
_, err = c.DrawString(s, pt)
if err != nil {
log.Println(err)
return
}
pt.Y += c.PointToFixed(*size * *spacing)
fmt.Println()
}
// Save that RGBA image to disk.

View File

@ -243,9 +243,13 @@ func (c *Context) DrawString(s string, p fixed.Point26_6) (fixed.Point26_6, erro
}
// Check if rune is an emoji
if c.emojis != nil && c.emojis.IsEmoji(r) {
icon, length := c.emojis.Find(s[index:])
fmt.Printf("Found emoji at position #%d: (len %d)\n %s", index, length, icon.String())
nextchar = index + length
icon := c.emojis.Find(s[index:])
if icon == nil {
fmt.Printf("Nil emoji at position #%d\n", index)
} else {
fmt.Printf("Found emoji at position #%d:\n %s", index, icon.String())
nextchar = index + icon.Length() - 1
}
}
index := c.f.Index(r)
if hasPrev {