Everything should be working decently now!
This commit is contained in:
parent
33cbf9f25f
commit
cbc74ad845
27
emoji.go
27
emoji.go
|
@ -2,11 +2,17 @@ package freetype
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
_ "image/png" // Add PNG file loading support
|
||||||
|
|
||||||
|
"golang.org/x/image/draw"
|
||||||
|
"golang.org/x/image/math/fixed"
|
||||||
)
|
)
|
||||||
|
|
||||||
type emoji struct {
|
type emoji struct {
|
||||||
|
@ -143,3 +149,24 @@ func scanEmojiDirectory(emojipath string) (tab emojiTable, err error) {
|
||||||
})
|
})
|
||||||
return tab, err
|
return tab, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const emojiScale = fixed.Int26_6(100)
|
||||||
|
|
||||||
|
func loadIconAtSize(path string, size fixed.Int26_6) (image.Image, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
img, _, err := image.Decode(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
scale := size.Mul(emojiScale).Round()
|
||||||
|
scaled := image.NewRGBA(image.Rect(0, 0, scale, scale))
|
||||||
|
draw.BiLinear.Scale(scaled, scaled.Bounds(), img, img.Bounds(), draw.Over, nil)
|
||||||
|
|
||||||
|
return scaled, nil
|
||||||
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ var (
|
||||||
dpi = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
|
dpi = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
|
||||||
fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font")
|
fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font")
|
||||||
hinting = flag.String("hinting", "none", "none | full")
|
hinting = flag.String("hinting", "none", "none | full")
|
||||||
size = flag.Float64("size", 20, "font size in points")
|
size = flag.Float64("size", 30, "font size in points")
|
||||||
spacing = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
|
spacing = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
|
||||||
wonb = flag.Bool("whiteonblack", false, "white text on a black background")
|
wonb = flag.Bool("whiteonblack", false, "white text on a black background")
|
||||||
emojidir = flag.String("emojidir", "../../noto_emojis", "Path to emojis")
|
emojidir = flag.String("emojidir", "../../noto_emojis", "Path to emojis")
|
||||||
|
@ -96,14 +96,12 @@ 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.
|
||||||
|
|
17
freetype.go
17
freetype.go
|
@ -10,7 +10,6 @@ package freetype // import "git.fromouter.space/crunchy-rocks/freetype"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"image"
|
"image"
|
||||||
"image/draw"
|
"image/draw"
|
||||||
|
|
||||||
|
@ -244,11 +243,19 @@ 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 := c.emojis.Find(s[index:])
|
icon := c.emojis.Find(s[index:])
|
||||||
if icon == nil {
|
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
|
nextchar = index + icon.Length() - 1
|
||||||
|
img, err := loadIconAtSize(icon.Path, c.scale)
|
||||||
|
h := emojiScale * c.scale / 90
|
||||||
|
w := emojiScale * c.scale / 70
|
||||||
|
if err == nil {
|
||||||
|
// Draw pic
|
||||||
|
ix, iy := int(p.X>>6), int((p.Y-h)>>6)
|
||||||
|
draw.Draw(c.dst, c.dst.Bounds(), img, image.Point{-ix, -iy}, draw.Over)
|
||||||
|
// Set some kerning variables
|
||||||
|
p.X += w
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
index := c.f.Index(r)
|
index := c.f.Index(r)
|
||||||
|
|
Loading…
Reference in New Issue