gioui.org/ui@v0.0.0-20190926171558-ce74bc0cbaea/measure/opentype.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package measure 4 5 import ( 6 "golang.org/x/image/font" 7 "golang.org/x/image/font/sfnt" 8 "golang.org/x/image/math/fixed" 9 ) 10 11 type opentype struct { 12 Font *sfnt.Font 13 Hinting font.Hinting 14 15 buf sfnt.Buffer 16 } 17 18 func (f *opentype) GlyphAdvance(ppem fixed.Int26_6, r rune) (advance fixed.Int26_6, ok bool) { 19 g, err := f.Font.GlyphIndex(&f.buf, r) 20 if err != nil { 21 return 0, false 22 } 23 adv, err := f.Font.GlyphAdvance(&f.buf, g, ppem, f.Hinting) 24 return adv, err == nil 25 } 26 27 func (f *opentype) Kern(ppem fixed.Int26_6, r0, r1 rune) fixed.Int26_6 { 28 g0, err := f.Font.GlyphIndex(&f.buf, r0) 29 if err != nil { 30 return 0 31 } 32 g1, err := f.Font.GlyphIndex(&f.buf, r1) 33 if err != nil { 34 return 0 35 } 36 adv, err := f.Font.Kern(&f.buf, g0, g1, ppem, f.Hinting) 37 if err != nil { 38 return 0 39 } 40 return adv 41 } 42 43 func (f *opentype) Metrics(ppem fixed.Int26_6) font.Metrics { 44 m, _ := f.Font.Metrics(&f.buf, ppem, f.Hinting) 45 return m 46 } 47 48 func (f *opentype) Bounds(ppem fixed.Int26_6) fixed.Rectangle26_6 { 49 r, _ := f.Font.Bounds(&f.buf, ppem, f.Hinting) 50 return r 51 } 52 53 func (f *opentype) LoadGlyph(ppem fixed.Int26_6, r rune) ([]sfnt.Segment, bool) { 54 g, err := f.Font.GlyphIndex(&f.buf, r) 55 if err != nil { 56 return nil, false 57 } 58 segs, err := f.Font.LoadGlyph(&f.buf, g, ppem, nil) 59 if err != nil { 60 return nil, false 61 } 62 return segs, true 63 }