github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/image/font/plan9font/example_test.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package plan9font_test 6 7 import ( 8 "image" 9 "image/draw" 10 "io/ioutil" 11 "log" 12 "os" 13 "path" 14 "path/filepath" 15 16 "golang.org/x/image/font" 17 "golang.org/x/image/font/plan9font" 18 "golang.org/x/image/math/fixed" 19 ) 20 21 func ExampleParseFont() { 22 readFile := func(name string) ([]byte, error) { 23 return ioutil.ReadFile(filepath.FromSlash(path.Join("../testdata/fixed", name))) 24 } 25 fontData, err := readFile("unicode.7x13.font") 26 if err != nil { 27 log.Fatal(err) 28 } 29 face, err := plan9font.ParseFont(fontData, readFile) 30 if err != nil { 31 log.Fatal(err) 32 } 33 // TODO: derive the ascent from the face's metrics. 34 const ascent = 11 35 36 dst := image.NewRGBA(image.Rect(0, 0, 4*7, 13)) 37 draw.Draw(dst, dst.Bounds(), image.Black, image.Point{}, draw.Src) 38 d := &font.Drawer{ 39 Dst: dst, 40 Src: image.White, 41 Face: face, 42 Dot: fixed.P(0, ascent), 43 } 44 // Draw: 45 // - U+0053 LATIN CAPITAL LETTER S 46 // - U+03A3 GREEK CAPITAL LETTER SIGMA 47 // - U+222B INTEGRAL 48 // - U+3055 HIRAGANA LETTER SA 49 // The testdata does not contain the CJK subfont files, so U+3055 HIRAGANA 50 // LETTER SA (さ) should be rendered as U+FFFD REPLACEMENT CHARACTER (�). 51 // 52 // The missing subfont file will trigger an "open 53 // ../testdata/shinonome/k12.3000: no such file or directory" log message. 54 // This is expected and can be ignored. 55 d.DrawString("SΣ∫さ") 56 57 // Convert the dst image to ASCII art. 58 var out []byte 59 b := dst.Bounds() 60 for y := b.Min.Y; y < b.Max.Y; y++ { 61 out = append(out, '0'+byte(y%10), ' ') 62 for x := b.Min.X; x < b.Max.X; x++ { 63 if dst.RGBAAt(x, y).R > 0 { 64 out = append(out, 'X') 65 } else { 66 out = append(out, '.') 67 } 68 } 69 // Highlight the last row before the baseline. Glyphs like 'S' without 70 // descenders should not affect any pixels whose Y coordinate is >= the 71 // baseline. 72 if y == ascent-1 { 73 out = append(out, '_') 74 } 75 out = append(out, '\n') 76 } 77 os.Stdout.Write(out) 78 79 // Output: 80 // 0 ..................X......... 81 // 1 .................X.X........ 82 // 2 .XXXX..XXXXXX....X.....XXX.. 83 // 3 X....X.X.........X....XX.XX. 84 // 4 X.......X........X....X.X.X. 85 // 5 X........X.......X....XXX.X. 86 // 6 .XXXX.....X......X....XX.XX. 87 // 7 .....X...X.......X....XX.XX. 88 // 8 .....X..X........X....XXXXX. 89 // 9 X....X.X.........X....XX.XX. 90 // 0 .XXXX..XXXXXX....X.....XXX.._ 91 // 1 ...............X.X.......... 92 // 2 ................X........... 93 }