Skip to content
Permalink
Browse files

play more with font parsers

  • Loading branch information...
dmitshur committed Jul 18, 2019
1 parent 74778ef commit e1a31538bbdcfad00f00a9b9efcf4c331238a29b
Showing with 42 additions and 10 deletions.
  1. +42 −10 236/main.go
@@ -10,6 +10,8 @@ import (
"github.com/gopherjs/gopherjs/js"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/math/fixed"
"honnef.co/go/js/dom"
)
@@ -37,28 +39,58 @@ func main() {

func paint(ctx *dom.CanvasRenderingContext2D, width, height, scale int) error {
// Load font from TTF data.
f, err := truetype.Parse(goregular.TTF)
if err != nil {
return err
var face font.Face
switch 0 {
case 0:
f, err := truetype.Parse(goregular.TTF)
if err != nil {
return err
}
face = truetype.NewFace(f, &truetype.Options{
Size: 300,
DPI: 72 * float64(scale),
//Hinting: font.HintingVertical,
})
case 1:
f, err := sfnt.Parse(goregular.TTF)
if err != nil {
return err
}
face, err = opentype.NewFace(f, nil)
if err != nil {
return err
}
}
face := truetype.NewFace(f, &truetype.Options{
Size: 300,
DPI: 72 * float64(scale),
//Hinting: font.HintingVertical,
})

m := ctx.CreateImageData(width, height)

// Draw text on image.
fd := font.Drawer{
Dst: m,
Src: image.Black,
Face: face,
Face: DebugFace{face},
Dot: fixed.P(100*scale, height-100*scale),
}
fd.DrawString("Hello.")
fd.DrawString(fmt.Sprintf("Hello. @%dx Can you read this? This is some text that is kinda hard to read because it's small, but it's still important.", scale))

// Output image to a context.
ctx.PutImageData(m, 0, 0)
return nil
}

type DebugFace struct {
font.Face
}

func (df DebugFace) Glyph(dot fixed.Point26_6, r rune) (dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
fmt.Println("Glyph:", dot, r)
dr, mask, maskp, advance, ok = df.Face.Glyph(dot, r)
if r == 'H' {
fmt.Println("dr:", dr)
fmt.Println("mask:", mask.Bounds(), mask.ColorModel())
fmt.Println("maskp:", maskp)
fmt.Println("advance:", advance)
fmt.Println("ok:", ok)
}
return dr, mask, maskp, advance, ok
}

0 comments on commit e1a3153

Please sign in to comment.
You can’t perform that action at this time.