chore(v2): vendor dependencies for offline/China builds
go mod vendor pins onnxruntime_go v1.12.1, Gio and the rest into v2/vendor so go run/build work without hitting proxy.golang.org (blocked/slow in China). Verified: CGO_ENABLED=1 go build -mod=vendor ./internal/spike and GOOS=windows go build -mod=vendor ./internal/ui both pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+126
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
Package font provides type describing font faces attributes.
|
||||
*/
|
||||
package font
|
||||
|
||||
import (
|
||||
"github.com/go-text/typesetting/font"
|
||||
)
|
||||
|
||||
// A FontFace is a Font and a matching Face.
|
||||
type FontFace struct {
|
||||
Font Font
|
||||
Face Face
|
||||
}
|
||||
|
||||
// Style is the font style.
|
||||
type Style int
|
||||
|
||||
// Weight is a font weight, in CSS units subtracted 400 so the zero value
|
||||
// is normal text weight.
|
||||
type Weight int
|
||||
|
||||
// Font specify a particular typeface variant, style and weight.
|
||||
type Font struct {
|
||||
// Typeface specifies the name(s) of the the font faces to try. See [Typeface]
|
||||
// for details.
|
||||
Typeface Typeface
|
||||
// Style specifies the kind of text style.
|
||||
Style Style
|
||||
// Weight is the text weight.
|
||||
Weight Weight
|
||||
}
|
||||
|
||||
// Face is an opaque handle to a typeface. The concrete implementation depends
|
||||
// upon the kind of font and shaper in use.
|
||||
type Face interface {
|
||||
Face() *font.Face
|
||||
}
|
||||
|
||||
// Typeface identifies a list of font families to attempt to use for displaying
|
||||
// a string. The syntax is a comma-delimited list of family names. In order to
|
||||
// allow for the remote possibility of needing to express a font family name
|
||||
// containing a comma, name entries may be quoted using either single or double
|
||||
// quotes. Within quotes, a literal quotation mark can be expressed by escaping
|
||||
// it with `\`. A literal backslash may be expressed by escaping it with another
|
||||
// `\`.
|
||||
//
|
||||
// Here's an example Typeface:
|
||||
//
|
||||
// Times New Roman, Georgia, serif
|
||||
//
|
||||
// This is equivalent to the above:
|
||||
//
|
||||
// "Times New Roman", 'Georgia', serif
|
||||
//
|
||||
// Here are some valid uses of escape sequences:
|
||||
//
|
||||
// "Contains a literal \" doublequote", 'Literal \' Singlequote', "\\ Literal backslash", '\\ another'
|
||||
//
|
||||
// This syntax has the happy side effect that most CSS "font-family" rules are
|
||||
// valid Typefaces (without the trailing semicolon).
|
||||
//
|
||||
// Generic CSS font families are supported, and are automatically expanded to lists
|
||||
// of known font families with a matching style. The supported generic families are:
|
||||
//
|
||||
// - fantasy
|
||||
// - math
|
||||
// - emoji
|
||||
// - serif
|
||||
// - sans-serif
|
||||
// - cursive
|
||||
// - monospace
|
||||
type Typeface string
|
||||
|
||||
const (
|
||||
Regular Style = iota
|
||||
Italic
|
||||
)
|
||||
|
||||
const (
|
||||
Thin Weight = -300
|
||||
ExtraLight Weight = -200
|
||||
Light Weight = -100
|
||||
Normal Weight = 0
|
||||
Medium Weight = 100
|
||||
SemiBold Weight = 200
|
||||
Bold Weight = 300
|
||||
ExtraBold Weight = 400
|
||||
Black Weight = 500
|
||||
)
|
||||
|
||||
func (s Style) String() string {
|
||||
switch s {
|
||||
case Regular:
|
||||
return "Regular"
|
||||
case Italic:
|
||||
return "Italic"
|
||||
default:
|
||||
panic("invalid Style")
|
||||
}
|
||||
}
|
||||
|
||||
func (w Weight) String() string {
|
||||
switch w {
|
||||
case Thin:
|
||||
return "Thin"
|
||||
case ExtraLight:
|
||||
return "ExtraLight"
|
||||
case Light:
|
||||
return "Light"
|
||||
case Normal:
|
||||
return "Normal"
|
||||
case Medium:
|
||||
return "Medium"
|
||||
case SemiBold:
|
||||
return "SemiBold"
|
||||
case Bold:
|
||||
return "Bold"
|
||||
case ExtraBold:
|
||||
return "ExtraBold"
|
||||
case Black:
|
||||
return "Black"
|
||||
default:
|
||||
panic("invalid Weight")
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
// Package gofont exports the Go fonts as a text.Collection.
|
||||
//
|
||||
// See https://blog.golang.org/go-fonts for a description of the
|
||||
// fonts, and the golang.org/x/image/font/gofont packages for the
|
||||
// font data.
|
||||
package gofont
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"golang.org/x/image/font/gofont/gobold"
|
||||
"golang.org/x/image/font/gofont/gobolditalic"
|
||||
"golang.org/x/image/font/gofont/goitalic"
|
||||
"golang.org/x/image/font/gofont/gomedium"
|
||||
"golang.org/x/image/font/gofont/gomediumitalic"
|
||||
"golang.org/x/image/font/gofont/gomono"
|
||||
"golang.org/x/image/font/gofont/gomonobold"
|
||||
"golang.org/x/image/font/gofont/gomonobolditalic"
|
||||
"golang.org/x/image/font/gofont/gomonoitalic"
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
"golang.org/x/image/font/gofont/gosmallcaps"
|
||||
"golang.org/x/image/font/gofont/gosmallcapsitalic"
|
||||
|
||||
"gioui.org/font"
|
||||
"gioui.org/font/opentype"
|
||||
)
|
||||
|
||||
var (
|
||||
regOnce sync.Once
|
||||
reg []font.FontFace
|
||||
once sync.Once
|
||||
collection []font.FontFace
|
||||
)
|
||||
|
||||
func loadRegular() {
|
||||
regOnce.Do(func() {
|
||||
faces, err := opentype.ParseCollection(goregular.TTF)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to parse font: %v", err))
|
||||
}
|
||||
reg = faces
|
||||
collection = append(collection, reg[0])
|
||||
})
|
||||
}
|
||||
|
||||
// Regular returns a collection of only the Go regular font face.
|
||||
func Regular() []font.FontFace {
|
||||
loadRegular()
|
||||
return reg
|
||||
}
|
||||
|
||||
// Regular returns a collection of all available Go font faces.
|
||||
func Collection() []font.FontFace {
|
||||
loadRegular()
|
||||
once.Do(func() {
|
||||
register(goitalic.TTF)
|
||||
register(gobold.TTF)
|
||||
register(gobolditalic.TTF)
|
||||
register(gomedium.TTF)
|
||||
register(gomediumitalic.TTF)
|
||||
register(gomono.TTF)
|
||||
register(gomonobold.TTF)
|
||||
register(gomonobolditalic.TTF)
|
||||
register(gomonoitalic.TTF)
|
||||
register(gosmallcaps.TTF)
|
||||
register(gosmallcapsitalic.TTF)
|
||||
// Ensure that any outside appends will not reuse the backing store.
|
||||
n := len(collection)
|
||||
collection = collection[:n:n]
|
||||
})
|
||||
return collection
|
||||
}
|
||||
|
||||
func register(ttf []byte) {
|
||||
faces, err := opentype.ParseCollection(ttf)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to parse font: %v", err))
|
||||
}
|
||||
collection = append(collection, faces[0])
|
||||
}
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
// Package opentype implements text layout and shaping for OpenType
|
||||
// files.
|
||||
//
|
||||
// NOTE: the OpenType specification allows for fonts to include bitmap images
|
||||
// in a variety of formats. In the interest of small binary sizes, the opentype
|
||||
// package only automatically imports the PNG image decoder. If you have a font
|
||||
// with glyphs in JPEG or TIFF formats, register those decoders with the image
|
||||
// package in order to ensure those glyphs are visible in text.
|
||||
package opentype
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
_ "image/png"
|
||||
|
||||
giofont "gioui.org/font"
|
||||
fontapi "github.com/go-text/typesetting/font"
|
||||
"github.com/go-text/typesetting/font/opentype"
|
||||
)
|
||||
|
||||
// Face is a thread-safe representation of a loaded font. For efficiency, applications
|
||||
// should construct a face for any given font file once, reusing it across different
|
||||
// text shapers.
|
||||
type Face struct {
|
||||
face *fontapi.Font
|
||||
font giofont.Font
|
||||
}
|
||||
|
||||
// Parse constructs a Face from source bytes.
|
||||
func Parse(src []byte) (Face, error) {
|
||||
ld, err := opentype.NewLoader(bytes.NewReader(src))
|
||||
if err != nil {
|
||||
return Face{}, err
|
||||
}
|
||||
font, md, err := parseLoader(ld)
|
||||
if err != nil {
|
||||
return Face{}, fmt.Errorf("failed parsing truetype font: %w", err)
|
||||
}
|
||||
return Face{
|
||||
face: font,
|
||||
font: md,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ParseCollection parse an Opentype font file, with support for collections.
|
||||
// Single font files are supported, returning a slice with length 1.
|
||||
// The returned fonts are automatically wrapped in a text.FontFace with
|
||||
// inferred font font.
|
||||
// BUG(whereswaldon): the only Variant that can be detected automatically is
|
||||
// "Mono".
|
||||
func ParseCollection(src []byte) ([]giofont.FontFace, error) {
|
||||
lds, err := opentype.NewLoaders(bytes.NewReader(src))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]giofont.FontFace, len(lds))
|
||||
for i, ld := range lds {
|
||||
face, md, err := parseLoader(ld)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading font %d of collection: %s", i, err)
|
||||
}
|
||||
ff := Face{
|
||||
face: face,
|
||||
font: md,
|
||||
}
|
||||
out[i] = giofont.FontFace{
|
||||
Face: ff,
|
||||
Font: ff.Font(),
|
||||
}
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func DescriptionToFont(md fontapi.Description) giofont.Font {
|
||||
return giofont.Font{
|
||||
Typeface: giofont.Typeface(md.Family),
|
||||
Style: gioStyle(md.Aspect.Style),
|
||||
Weight: gioWeight(md.Aspect.Weight),
|
||||
}
|
||||
}
|
||||
|
||||
func FontToDescription(font giofont.Font) fontapi.Description {
|
||||
return fontapi.Description{
|
||||
Family: string(font.Typeface),
|
||||
Aspect: fontapi.Aspect{
|
||||
Style: mdStyle(font.Style),
|
||||
Weight: mdWeight(font.Weight),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// parseLoader parses the contents of the loader into a face and its font.
|
||||
func parseLoader(ld *opentype.Loader) (*fontapi.Font, giofont.Font, error) {
|
||||
ft, err := fontapi.NewFont(ld)
|
||||
if err != nil {
|
||||
return nil, giofont.Font{}, err
|
||||
}
|
||||
data := DescriptionToFont(ft.Describe())
|
||||
return ft, data, nil
|
||||
}
|
||||
|
||||
// Face returns a thread-unsafe wrapper for this Face suitable for use by a single shaper.
|
||||
// Face many be invoked any number of times and is safe so long as each return value is
|
||||
// only used by one goroutine.
|
||||
func (f Face) Face() *fontapi.Face {
|
||||
return fontapi.NewFace(f.face)
|
||||
}
|
||||
|
||||
// FontFace returns a text.Font with populated font metadata for the
|
||||
// font.
|
||||
// BUG(whereswaldon): the only Variant that can be detected automatically is
|
||||
// "Mono".
|
||||
func (f Face) Font() giofont.Font {
|
||||
return f.font
|
||||
}
|
||||
|
||||
func gioStyle(s fontapi.Style) giofont.Style {
|
||||
switch s {
|
||||
case fontapi.StyleItalic:
|
||||
return giofont.Italic
|
||||
case fontapi.StyleNormal:
|
||||
fallthrough
|
||||
default:
|
||||
return giofont.Regular
|
||||
}
|
||||
}
|
||||
|
||||
func mdStyle(g giofont.Style) fontapi.Style {
|
||||
switch g {
|
||||
case giofont.Italic:
|
||||
return fontapi.StyleItalic
|
||||
case giofont.Regular:
|
||||
fallthrough
|
||||
default:
|
||||
return fontapi.StyleNormal
|
||||
}
|
||||
}
|
||||
|
||||
func gioWeight(w fontapi.Weight) giofont.Weight {
|
||||
switch w {
|
||||
case fontapi.WeightThin:
|
||||
return giofont.Thin
|
||||
case fontapi.WeightExtraLight:
|
||||
return giofont.ExtraLight
|
||||
case fontapi.WeightLight:
|
||||
return giofont.Light
|
||||
case fontapi.WeightNormal:
|
||||
return giofont.Normal
|
||||
case fontapi.WeightMedium:
|
||||
return giofont.Medium
|
||||
case fontapi.WeightSemibold:
|
||||
return giofont.SemiBold
|
||||
case fontapi.WeightBold:
|
||||
return giofont.Bold
|
||||
case fontapi.WeightExtraBold:
|
||||
return giofont.ExtraBold
|
||||
case fontapi.WeightBlack:
|
||||
return giofont.Black
|
||||
default:
|
||||
return giofont.Normal
|
||||
}
|
||||
}
|
||||
|
||||
func mdWeight(g giofont.Weight) fontapi.Weight {
|
||||
switch g {
|
||||
case giofont.Thin:
|
||||
return fontapi.WeightThin
|
||||
case giofont.ExtraLight:
|
||||
return fontapi.WeightExtraLight
|
||||
case giofont.Light:
|
||||
return fontapi.WeightLight
|
||||
case giofont.Normal:
|
||||
return fontapi.WeightNormal
|
||||
case giofont.Medium:
|
||||
return fontapi.WeightMedium
|
||||
case giofont.SemiBold:
|
||||
return fontapi.WeightSemibold
|
||||
case giofont.Bold:
|
||||
return fontapi.WeightBold
|
||||
case giofont.ExtraBold:
|
||||
return fontapi.WeightExtraBold
|
||||
case giofont.Black:
|
||||
return fontapi.WeightBlack
|
||||
default:
|
||||
return fontapi.WeightNormal
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user