...

Source file src/golang.org/x/image/font/plan9font/example_test.go

Documentation: golang.org/x/image/font/plan9font

     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  	ascent := face.Metrics().Ascent.Ceil()
    34  
    35  	dst := image.NewRGBA(image.Rect(0, 0, 4*7, 13))
    36  	draw.Draw(dst, dst.Bounds(), image.Black, image.Point{}, draw.Src)
    37  	d := &font.Drawer{
    38  		Dst:  dst,
    39  		Src:  image.White,
    40  		Face: face,
    41  		Dot:  fixed.P(0, ascent),
    42  	}
    43  	// Draw:
    44  	// 	- U+0053 LATIN CAPITAL LETTER S
    45  	//	- U+03A3 GREEK CAPITAL LETTER SIGMA
    46  	//	- U+222B INTEGRAL
    47  	//	- U+3055 HIRAGANA LETTER SA
    48  	// The testdata does not contain the CJK subfont files, so U+3055 HIRAGANA
    49  	// LETTER SA (さ) should be rendered as U+FFFD REPLACEMENT CHARACTER (�).
    50  	//
    51  	// The missing subfont file will trigger an "open
    52  	// ../testdata/shinonome/k12.3000: no such file or directory" log message.
    53  	// This is expected and can be ignored.
    54  	d.DrawString("SΣ∫さ")
    55  
    56  	// Convert the dst image to ASCII art.
    57  	var out []byte
    58  	b := dst.Bounds()
    59  	for y := b.Min.Y; y < b.Max.Y; y++ {
    60  		out = append(out, '0'+byte(y%10), ' ')
    61  		for x := b.Min.X; x < b.Max.X; x++ {
    62  			if dst.RGBAAt(x, y).R > 0 {
    63  				out = append(out, 'X')
    64  			} else {
    65  				out = append(out, '.')
    66  			}
    67  		}
    68  		// Highlight the last row before the baseline. Glyphs like 'S' without
    69  		// descenders should not affect any pixels whose Y coordinate is >= the
    70  		// baseline.
    71  		if y == ascent-1 {
    72  			out = append(out, '_')
    73  		}
    74  		out = append(out, '\n')
    75  	}
    76  	os.Stdout.Write(out)
    77  
    78  	// Output:
    79  	// 0 ..................X.........
    80  	// 1 .................X.X........
    81  	// 2 .XXXX..XXXXXX....X.....XXX..
    82  	// 3 X....X.X.........X....XX.XX.
    83  	// 4 X.......X........X....X.X.X.
    84  	// 5 X........X.......X....XXX.X.
    85  	// 6 .XXXX.....X......X....XX.XX.
    86  	// 7 .....X...X.......X....XX.XX.
    87  	// 8 .....X..X........X....XXXXX.
    88  	// 9 X....X.X.........X....XX.XX.
    89  	// 0 .XXXX..XXXXXX....X.....XXX.._
    90  	// 1 ...............X.X..........
    91  	// 2 ................X...........
    92  }
    93  

View as plain text