forked from joejulian/gio
f77bf9a42c
This commit adds back support for loading font collections, which we
lost when switching to the harfbuzz-based shaper last January. In
addition, this commit takes advantage of our new font loading library's
metadata facilities to automatically construct text.FontFaces for all
fonts within a collection. This is significantly more ergonomic for
users, and can be used to load single fonts with automatic metadata
detection as well.
I've exposed a opentype.Face.Font() method that can be used to get the
font metadata for a given face as well, though you have to type assert to
see it:
var myFace text.Face
if asOpentype, ok := myFace.(opentype.Face); ok {
myFont := asOpentype.Font()
}
The one problem with this approach is that the font variant field always
be automatically populated. Mono font detection is supported, but
other variants like SmallCaps are more complicated and may need to be
expressed differently in the future (smallcaps is a feature that any font
file can have, not necessarily a separate font file). See this [0] upstream
issue for details.
Additionally, in order to avoid import cycles, I've moved the declarations
of font attributes to package font. You can fix your code automatically to
refer to the new definitions by running the following:
gofmt -w -r 'text.FontFace -> font.FontFace' .
gofmt -w -r 'text.Variant -> font.Variant' .
gofmt -w -r 'text.Style -> font.Style' .
gofmt -w -r 'text.Typeface -> font.Typeface' .
gofmt -w -r 'text.Font -> font.Font' .
gofmt -w -r 'text.Regular -> font.Regular' .
gofmt -w -r 'text.Italic -> font.Italic' .
gofmt -w -r 'text.Thin -> font.Thin' .
gofmt -w -r 'text.ExtraLight -> font.ExtraLight' .
gofmt -w -r 'text.Light -> font.Light' .
gofmt -w -r 'text.Normal -> font.Normal' .
gofmt -w -r 'text.Medium -> font.Medium' .
gofmt -w -r 'text.SemiBold -> font.SemiBold' .
gofmt -w -r 'text.Bold -> font.Bold' .
gofmt -w -r 'text.ExtraBold -> font.ExtraBold' .
gofmt -w -r 'text.Black -> font.Black' .
gofmt -w -r 'text.Hairline -> font.Thin' .
gofmt -w -r 'text.UltraLight -> font.ExtraLight' .
gofmt -w -r 'text.DemiBold -> font.SemiBold' .
gofmt -w -r 'text.UltraBold -> font.ExtraBold' .
gofmt -w -r 'text.Heavy -> font.Black' .
gofmt -w -r 'text.ExtraBlack -> font.Black+50' .
gofmt -w -r 'text.UltraBlack -> font.ExtraBlack' .
Make sure each affected file imports gioui.org/font.
[0] https://github.com/go-text/typesetting/issues/57
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
92 lines
2.0 KiB
Go
92 lines
2.0 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image/color"
|
|
|
|
"golang.org/x/exp/shiny/materialdesign/icons"
|
|
|
|
"gioui.org/font"
|
|
"gioui.org/text"
|
|
"gioui.org/unit"
|
|
"gioui.org/widget"
|
|
)
|
|
|
|
// Palette contains the minimal set of colors that a widget may need to
|
|
// draw itself.
|
|
type Palette struct {
|
|
// Bg is the background color atop which content is currently being
|
|
// drawn.
|
|
Bg color.NRGBA
|
|
|
|
// Fg is a color suitable for drawing on top of Bg.
|
|
Fg color.NRGBA
|
|
|
|
// ContrastBg is a color used to draw attention to active,
|
|
// important, interactive widgets such as buttons.
|
|
ContrastBg color.NRGBA
|
|
|
|
// ContrastFg is a color suitable for content drawn on top of
|
|
// ContrastBg.
|
|
ContrastFg color.NRGBA
|
|
}
|
|
|
|
type Theme struct {
|
|
Shaper *text.Shaper
|
|
Palette
|
|
TextSize unit.Sp
|
|
Icon struct {
|
|
CheckBoxChecked *widget.Icon
|
|
CheckBoxUnchecked *widget.Icon
|
|
RadioChecked *widget.Icon
|
|
RadioUnchecked *widget.Icon
|
|
}
|
|
|
|
// FingerSize is the minimum touch target size.
|
|
FingerSize unit.Dp
|
|
}
|
|
|
|
func NewTheme(fontCollection []font.FontFace) *Theme {
|
|
t := &Theme{
|
|
Shaper: text.NewShaper(fontCollection),
|
|
}
|
|
t.Palette = Palette{
|
|
Fg: rgb(0x000000),
|
|
Bg: rgb(0xffffff),
|
|
ContrastBg: rgb(0x3f51b5),
|
|
ContrastFg: rgb(0xffffff),
|
|
}
|
|
t.TextSize = 16
|
|
|
|
t.Icon.CheckBoxChecked = mustIcon(widget.NewIcon(icons.ToggleCheckBox))
|
|
t.Icon.CheckBoxUnchecked = mustIcon(widget.NewIcon(icons.ToggleCheckBoxOutlineBlank))
|
|
t.Icon.RadioChecked = mustIcon(widget.NewIcon(icons.ToggleRadioButtonChecked))
|
|
t.Icon.RadioUnchecked = mustIcon(widget.NewIcon(icons.ToggleRadioButtonUnchecked))
|
|
|
|
// 38dp is on the lower end of possible finger size.
|
|
t.FingerSize = 38
|
|
|
|
return t
|
|
}
|
|
|
|
func (t Theme) WithPalette(p Palette) Theme {
|
|
t.Palette = p
|
|
return t
|
|
}
|
|
|
|
func mustIcon(ic *widget.Icon, err error) *widget.Icon {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return ic
|
|
}
|
|
|
|
func rgb(c uint32) color.NRGBA {
|
|
return argb(0xff000000 | c)
|
|
}
|
|
|
|
func argb(c uint32) color.NRGBA {
|
|
return color.NRGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)}
|
|
}
|