Everything should be working decently now!

This commit is contained in:
Hamcha 2018-11-16 10:58:44 +01:00
parent 33cbf9f25f
commit cbc74ad845
Signed by: hamcha
GPG key ID: A40413D21021EAEE
3 changed files with 40 additions and 8 deletions

View file

@ -2,11 +2,17 @@ package freetype
import (
"fmt"
"image"
"os"
"path/filepath"
"strconv"
"strings"
"unicode/utf8"
_ "image/png" // Add PNG file loading support
"golang.org/x/image/draw"
"golang.org/x/image/math/fixed"
)
type emoji struct {
@ -143,3 +149,24 @@ func scanEmojiDirectory(emojipath string) (tab emojiTable, err error) {
})
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
}

View file

@ -31,7 +31,7 @@ var (
dpi = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font")
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)")
wonb = flag.Bool("whiteonblack", false, "white text on a black background")
emojidir = flag.String("emojidir", "../../noto_emojis", "Path to emojis")
@ -96,14 +96,12 @@ 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

@ -10,7 +10,6 @@ package freetype // import "git.fromouter.space/crunchy-rocks/freetype"
import (
"errors"
"fmt"
"image"
"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
if c.emojis != nil && c.emojis.IsEmoji(r) {
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())
if icon != nil {
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)