File: example_test.go

package info (click to toggle)
golang-golang-x-image 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 18,044 kB
  • sloc: makefile: 2
file content (113 lines) | stat: -rw-r--r-- 4,399 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package opentype_test

import (
	"fmt"
	"image"
	"image/color"
	"log"
	"os"

	"golang.org/x/image/font"
	"golang.org/x/image/font/gofont/goitalic"
	"golang.org/x/image/font/opentype"
	"golang.org/x/image/math/fixed"
)

func ExampleNewFace() {
	const (
		width        = 72
		height       = 36
		startingDotX = 6
		startingDotY = 28
	)

	f, err := opentype.Parse(goitalic.TTF)
	if err != nil {
		log.Fatalf("Parse: %v", err)
	}
	face, err := opentype.NewFace(f, &opentype.FaceOptions{
		Size:    32,
		DPI:     72,
		Hinting: font.HintingNone,
	})
	if err != nil {
		log.Fatalf("NewFace: %v", err)
	}

	dst := image.NewGray(image.Rect(0, 0, width, height))
	d := font.Drawer{
		Dst:  dst,
		Src:  image.White,
		Face: face,
		Dot:  fixed.P(startingDotX, startingDotY),
	}
	fmt.Printf("The dot is at %v\n", d.Dot)
	d.DrawString("jel")
	fmt.Printf("The dot is at %v\n", d.Dot)
	d.Src = image.NewUniform(color.Gray{0x7F})
	d.DrawString("ly")
	fmt.Printf("The dot is at %v\n", d.Dot)

	const asciiArt = ".++8"
	buf := make([]byte, 0, height*(width+1))
	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			c := asciiArt[dst.GrayAt(x, y).Y>>6]
			if c != '.' {
				// No-op.
			} else if x == startingDotX-1 {
				c = ']'
			} else if y == startingDotY-1 {
				c = '_'
			}
			buf = append(buf, c)
		}
		buf = append(buf, '\n')
	}
	os.Stdout.Write(buf)

	// Output:
	// The dot is at {6:00 28:00}
	// The dot is at {41:32 28:00}
	// The dot is at {66:48 28:00}
	// .....]..................................................................
	// .....]..................................................................
	// .....]..................................................................
	// .....]..................................+++......+++....................
	// .....]........+++.......................888......+++....................
	// .....].......+88+......................+88+......+++....................
	// .....].......888+......................+88+.....+++.....................
	// .....].......888+......................+88+.....+++.....................
	// .....].................................888......+++.....................
	// .....].................................888......+++.....................
	// .....]....................++..........+88+......+++.....................
	// .....]......+88+.......+888888+.......+88+.....+++....+++..........++...
	// .....]......888......+888888888+......+88+.....+++....++++........+++...
	// .....]......888.....+888+...+888......888......+++.....+++........++....
	// .....].....+888....+888......+88+.....888......+++.....+++.......+++....
	// .....].....+88+....888.......+88+....+88+......+++.....+++......+++.....
	// .....].....+88+...+888.......+88+....+88+.....+++......+++......+++.....
	// .....].....888....888+++++++++88+....+88+.....+++......+++.....+++......
	// .....].....888....88888888888888+....888......+++......++++....++.......
	// .....]....+888...+88888888888888.....888......+++.......+++...+++.......
	// .....]....+88+...+888...............+888......+++.......+++..+++........
	// .....]....+88+...+888...............+88+.....+++........+++..+++........
	// .....]....888....+888...............+88+.....+++........+++.+++.........
	// .....]....888....+888...............888......+++........++++++..........
	// .....]...+888.....888+..............888......+++........++++++..........
	// .....]...+88+.....+8888+....++8.....888+.....++++........++++...........
	// .....]...+88+......+8888888888+.....+8888....+++++.......++++...........
	// _____]___888________+88888888++______+888_____++++_______+++____________
	// .....]...888...........+++.............++................+++............
	// .....]..+88+............................................+++.............
	// .....]..+88+...........................................+++..............
	// .....].+888............................................+++..............
	// ....888888............................................+++...............
	// ....88888............................................++++...............
	// ....+++.................................................................
	// .....]..................................................................
}