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

View File

@ -96,12 +96,14 @@ func main() {
// Draw the text. // Draw the text.
pt := freetype.Pt(10, 10+int(c.PointToFixed(*size)>>6)) pt := freetype.Pt(10, 10+int(c.PointToFixed(*size)>>6))
for _, s := range text { for _, s := range text {
fmt.Println(s)
_, err = c.DrawString(s, pt) _, err = c.DrawString(s, pt)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
} }
pt.Y += c.PointToFixed(*size * *spacing) pt.Y += c.PointToFixed(*size * *spacing)
fmt.Println()
} }
// Save that RGBA image to disk. // 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 // Check if rune is an emoji
if c.emojis != nil && c.emojis.IsEmoji(r) { if c.emojis != nil && c.emojis.IsEmoji(r) {
icon, length := c.emojis.Find(s[index:]) icon := c.emojis.Find(s[index:])
fmt.Printf("Found emoji at position #%d: (len %d)\n %s", index, length, icon.String()) if icon == nil {
nextchar = index + length 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) index := c.f.Index(r)
if hasPrev { if hasPrev {