/
rune.go
97 lines (79 loc) · 3.02 KB
/
rune.go
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
// Copyright (c) 2018, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package paint
import (
"errors"
"image"
"cogentcore.org/core/math32"
"cogentcore.org/core/styles"
"golang.org/x/image/font"
)
// Rune contains fully explicit data needed for rendering a single rune
// -- Face and Color can be nil after first element, in which case the last
// non-nil is used -- likely slightly more efficient to avoid setting all
// those pointers -- float32 values used to support better accuracy when
// transforming points
type Rune struct {
// fully-specified font rendering info, includes fully computed font size.
// This is exactly what will be drawn, with no further transforms.
Face font.Face `json:"-"`
// Color is the color to draw characters in
Color image.Image `json:"-"`
// background color to fill background of color, for highlighting,
// <mark> tag, etc. Unlike Face, Color, this must be non-nil for every case
// that uses it, as nil is also used for default transparent background.
Background image.Image `json:"-"`
// dditional decoration to apply: underline, strike-through, etc.
// Also used for encoding a few special layout hints to pass info
// from styling tags to separate layout algorithms (e.g., <P> vs <BR>)
Deco styles.TextDecorations
// relative position from start of Text for the lower-left baseline
// rendering position of the font character
RelPos math32.Vector2
// size of the rune itself, exclusive of spacing that might surround it
Size math32.Vector2
// rotation in radians for this character, relative to its lower-left
// baseline rendering position
RotRad float32
// scaling of the X dimension, in case of non-uniform scaling, 0 = no separate scaling
ScaleX float32
}
// HasNil returns error if any of the key info (face, color) is nil -- only
// the first element must be non-nil
func (rr *Rune) HasNil() error {
if rr.Face == nil {
return errors.New("core.Rune: Face is nil")
}
if rr.Color == nil {
return errors.New("core.Rune: Color is nil")
}
// note: BackgroundColor can be nil -- transparent
return nil
}
// CurFace is convenience for updating current font face if non-nil
func (rr *Rune) CurFace(curFace font.Face) font.Face {
if rr.Face != nil {
return rr.Face
}
return curFace
}
// CurColor is convenience for updating current color if non-nil
func (rr *Rune) CurColor(curColor image.Image) image.Image {
if rr.Color != nil {
return rr.Color
}
return curColor
}
// RelPosAfterLR returns the relative position after given rune for LR order: RelPos.X + Size.X
func (rr *Rune) RelPosAfterLR() float32 {
return rr.RelPos.X + rr.Size.X
}
// RelPosAfterRL returns the relative position after given rune for RL order: RelPos.X - Size.X
func (rr *Rune) RelPosAfterRL() float32 {
return rr.RelPos.X - rr.Size.X
}
// RelPosAfterTB returns the relative position after given rune for TB order: RelPos.Y + Size.Y
func (rr *Rune) RelPosAfterTB() float32 {
return rr.RelPos.Y + rr.Size.Y
}