~eliasnaur/gio

ref: b331407e8145648c6804788a41e89fe77c9a2a2d gio/text/text.go -rw-r--r-- 2.0 KiB
b331407eElias Naur text: add io.Reader Layout method to Shaper 9 months ago
                                                                                
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
// SPDX-License-Identifier: Unlicense OR MIT

package text

import (
	"io"

	"gioui.org/op"
	"gioui.org/unit"
	"golang.org/x/image/font"
	"golang.org/x/image/math/fixed"
)

// A Line contains the measurements of a line of text.
type Line struct {
	Layout []Glyph
	// Len is the length in UTF8 bytes of the line.
	Len int
	// Width is the width of the line.
	Width fixed.Int26_6
	// Ascent is the height above the baseline.
	Ascent fixed.Int26_6
	// Descent is the height below the baseline, including
	// the line gap.
	Descent fixed.Int26_6
	// Bounds is the visible bounds of the line.
	Bounds fixed.Rectangle26_6
}

type Glyph struct {
	Rune    rune
	Advance fixed.Int26_6
}

// LayoutOptions specify the constraints of a text layout.
type LayoutOptions struct {
	// MaxWidth is the available width of the layout.
	MaxWidth int
}

// Style is the font style.
type Style int

// Weight is a font weight, in CSS units.
type Weight int

// Font specify a particular typeface, style and size.
type Font struct {
	Typeface Typeface
	Variant  Variant
	Size     unit.Value
	Style    Style
	// Weight is the text weight. If zero, Normal is used instead.
	Weight Weight
}

// Face implements text layout and shaping for a particular font.
type Face interface {
	Layout(ppem fixed.Int26_6, txt io.Reader, opts LayoutOptions) ([]Line, error)
	Shape(ppem fixed.Int26_6, str []Glyph) op.CallOp
	Metrics(ppem fixed.Int26_6) font.Metrics
}

// Typeface identifies a particular typeface design. The empty
// string denotes the default typeface.
type Typeface string

// Variant denotes a typeface variant such as "Mono" or "Smallcaps".
type Variant string

type Alignment uint8

const (
	Start Alignment = iota
	End
	Middle
)

const (
	Regular Style = iota
	Italic
)

const (
	Normal Weight = 400
	Medium Weight = 500
	Bold   Weight = 600
)

func (a Alignment) String() string {
	switch a {
	case Start:
		return "Start"
	case End:
		return "End"
	case Middle:
		return "Middle"
	default:
		panic("unreachable")
	}
}