git.lucas.co / go_mono
git clone https://git.lucas.co/go_mono.git

font/opentype/example_test.go (4.3K)

  1 // Copyright 2020 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 opentype_test
  6 
  7 import (
  8 	"fmt"
  9 	"image"
 10 	"image/color"
 11 	"log"
 12 	"os"
 13 
 14 	"golang.org/x/image/font"
 15 	"golang.org/x/image/font/gofont/goitalic"
 16 	"golang.org/x/image/font/opentype"
 17 	"golang.org/x/image/math/fixed"
 18 )
 19 
 20 func ExampleNewFace() {
 21 	const (
 22 		width        = 72
 23 		height       = 36
 24 		startingDotX = 6
 25 		startingDotY = 28
 26 	)
 27 
 28 	f, err := opentype.Parse(goitalic.TTF)
 29 	if err != nil {
 30 		log.Fatalf("Parse: %v", err)
 31 	}
 32 	face, err := opentype.NewFace(f, &opentype.FaceOptions{
 33 		Size:    32,
 34 		DPI:     72,
 35 		Hinting: font.HintingNone,
 36 	})
 37 	if err != nil {
 38 		log.Fatalf("NewFace: %v", err)
 39 	}
 40 
 41 	dst := image.NewGray(image.Rect(0, 0, width, height))
 42 	d := font.Drawer{
 43 		Dst:  dst,
 44 		Src:  image.White,
 45 		Face: face,
 46 		Dot:  fixed.P(startingDotX, startingDotY),
 47 	}
 48 	fmt.Printf("The dot is at %v\n", d.Dot)
 49 	d.DrawString("jel")
 50 	fmt.Printf("The dot is at %v\n", d.Dot)
 51 	d.Src = image.NewUniform(color.Gray{0x7F})
 52 	d.DrawString("ly")
 53 	fmt.Printf("The dot is at %v\n", d.Dot)
 54 
 55 	const asciiArt = ".++8"
 56 	buf := make([]byte, 0, height*(width+1))
 57 	for y := 0; y < height; y++ {
 58 		for x := 0; x < width; x++ {
 59 			c := asciiArt[dst.GrayAt(x, y).Y>>6]
 60 			if c != '.' {
 61 				// No-op.
 62 			} else if x == startingDotX-1 {
 63 				c = ']'
 64 			} else if y == startingDotY-1 {
 65 				c = '_'
 66 			}
 67 			buf = append(buf, c)
 68 		}
 69 		buf = append(buf, '\n')
 70 	}
 71 	os.Stdout.Write(buf)
 72 
 73 	// Output:
 74 	// The dot is at {6:00 28:00}
 75 	// The dot is at {41:32 28:00}
 76 	// The dot is at {66:48 28:00}
 77 	// .....]..................................................................
 78 	// .....]..................................................................
 79 	// .....]..................................................................
 80 	// .....]..................................+++......+++....................
 81 	// .....]........+++.......................888......+++....................
 82 	// .....].......+88+......................+88+......+++....................
 83 	// .....].......888+......................+88+.....+++.....................
 84 	// .....].......888+......................+88+.....+++.....................
 85 	// .....].................................888......+++.....................
 86 	// .....].................................888......+++.....................
 87 	// .....]....................++..........+88+......+++.....................
 88 	// .....]......+88+.......+888888+.......+88+.....+++....+++..........++...
 89 	// .....]......888......+888888888+......+88+.....+++....++++........+++...
 90 	// .....]......888.....+888+...+888......888......+++.....+++........++....
 91 	// .....].....+888....+888......+88+.....888......+++.....+++.......+++....
 92 	// .....].....+88+....888.......+88+....+88+......+++.....+++......+++.....
 93 	// .....].....+88+...+888.......+88+....+88+.....+++......+++......+++.....
 94 	// .....].....888....888+++++++++88+....+88+.....+++......+++.....+++......
 95 	// .....].....888....88888888888888+....888......+++......++++....++.......
 96 	// .....]....+888...+88888888888888.....888......+++.......+++...+++.......
 97 	// .....]....+88+...+888...............+888......+++.......+++..+++........
 98 	// .....]....+88+...+888...............+88+.....+++........+++..+++........
 99 	// .....]....888....+888...............+88+.....+++........+++.+++.........
100 	// .....]....888....+888...............888......+++........++++++..........
101 	// .....]...+888.....888+..............888......+++........++++++..........
102 	// .....]...+88+.....+8888+....++8.....888+.....++++........++++...........
103 	// .....]...+88+......+8888888888+.....+8888....+++++.......++++...........
104 	// _____]___888________+88888888++______+888_____++++_______+++____________
105 	// .....]...888...........+++.............++................+++............
106 	// .....]..+88+............................................+++.............
107 	// .....]..+88+...........................................+++..............
108 	// .....].+888............................................+++..............
109 	// ....888888............................................+++...............
110 	// ....88888............................................++++...............
111 	// ....+++.................................................................
112 	// .....]..................................................................
113 }