Debian Codegraph @

shiny/text/example_test.go

raw symbols SCIP
1 // Copyright 2016 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 text_test
6
7 import (
8 "fmt"
9 "image"
10 "os"
11
12 "golang.org/x/exp/shiny/text"
13 "golang.org/x/image/font"
14 "golang.org/x/image/math/fixed"
15 )
16
17 // toyFace implements the font.Face interface by measuring every rune's width
18 // as 1 pixel.
19 type toyFace struct{}
20
21 func (toyFace) Close() error {
22 return nil
23 }
24
25 func (toyFace) Glyph(dot fixed.Point26_6, r rune) (image.Rectangle, image.Image, image.Point, fixed.Int26_6, bool) {
26 panic("unimplemented")
27 }
28
29 func (toyFace) GlyphBounds(r rune) (fixed.Rectangle26_6, fixed.Int26_6, bool) {
30 panic("unimplemented")
31 }
32
33 func (toyFace) GlyphAdvance(r rune) (fixed.Int26_6, bool) {
34 return fixed.I(1), true
35 }
36
37 func (toyFace) Kern(r0, r1 rune) fixed.Int26_6 {
38 return 0
39 }
40
41 func (toyFace) Metrics() font.Metrics {
42 return font.Metrics{}
43 }
44
45 func printFrame(f *text.Frame, softReturnsOnly bool) {
46 for p := f.FirstParagraph(); p != nil; p = p.Next(f) {
47 for l := p.FirstLine(f); l != nil; l = l.Next(f) {
48 for b := l.FirstBox(f); b != nil; b = b.Next(f) {
49 if softReturnsOnly {
50 os.Stdout.Write(b.TrimmedText(f))
51 } else {
52 os.Stdout.Write(b.Text(f))
53 }
54 }
55 if softReturnsOnly {
56 fmt.Println()
57 }
58 }
59 }
60 }
61
62 func Example() {
63 var f text.Frame
64 f.SetFace(toyFace{})
65 f.SetMaxWidth(fixed.I(60))
66
67 c := f.NewCaret()
68 c.WriteString(mobyDick)
69 c.Close()
70
71 fmt.Println("====")
72 printFrame(&f, false)
73 fmt.Println("====")
74 fmt.Println("123456789_123456789_123456789_123456789_123456789_123456789_")
75 printFrame(&f, true)
76 fmt.Println("====")
77
78 // Output:
79 // ====
80 // CHAPTER 1. Loomings.
81 // Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world...
82 // ====
83 // 123456789_123456789_123456789_123456789_123456789_123456789_
84 // CHAPTER 1. Loomings.
85 // Call me Ishmael. Some years ago—never mind how long
86 // precisely—having little or no money in my purse, and nothing
87 // particular to interest me on shore, I thought I would sail
88 // about a little and see the watery part of the world...
89 //
90 // ====
91 }
92
93 const mobyDick = "CHAPTER 1. Loomings.\nCall me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world...\n"