text/v2: add (*GoTextFaceSource).Metadata

Updates #2454
This commit is contained in:
Hajime Hoshi 2023-11-25 21:53:46 +09:00
parent 6f5fab47aa
commit 989d749475
2 changed files with 99 additions and 7 deletions

View File

@ -22,6 +22,8 @@ import (
"github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/language"
"github.com/go-text/typesetting/opentype/api"
ofont "github.com/go-text/typesetting/opentype/api/font"
"github.com/go-text/typesetting/opentype/loader"
"github.com/go-text/typesetting/shaping"
"golang.org/x/image/math/fixed"
@ -62,6 +64,7 @@ type goTextGlyphImageCacheKey struct {
// GoTextFaceSource is a source of a GoTextFace. This can be shared by multiple GoTextFace objects.
type GoTextFaceSource struct {
f font.Face
metadata Metadata
outputCache map[goTextOutputCacheKey]*goTextOutputCacheValue
glyphImageCache map[float64]*glyphImageCache[goTextGlyphImageCacheKey]
@ -96,15 +99,22 @@ func NewGoTextFaceSource(source io.ReadSeeker) (*GoTextFaceSource, error) {
return nil, err
}
f, err := font.ParseTTF(src)
l, err := loader.NewLoader(src)
if err != nil {
return nil, err
}
ft, err := ofont.NewFont(l)
if err != nil {
return nil, err
}
s := &GoTextFaceSource{
f: f,
f: &ofont.Face{Font: ft},
}
s.addr = s
s.metadata = metadataFromLoader(l)
return s, nil
}
@ -115,17 +125,22 @@ func NewGoTextFaceSourcesFromCollection(source io.ReadSeeker) ([]*GoTextFaceSour
return nil, err
}
fs, err := font.ParseTTC(src)
ls, err := loader.NewLoaders(src)
if err != nil {
return nil, err
}
sources := make([]*GoTextFaceSource, len(fs))
for i, f := range fs {
sources := make([]*GoTextFaceSource, len(ls))
for i, l := range ls {
ft, err := ofont.NewFont(l)
if err != nil {
return nil, err
}
s := &GoTextFaceSource{
f: f,
f: &ofont.Face{Font: ft},
}
s.addr = s
s.metadata = metadataFromLoader(l)
sources[i] = s
}
return sources, nil
@ -137,6 +152,11 @@ func (g *GoTextFaceSource) copyCheck() {
}
}
// Metadata returns its metadata.
func (g *GoTextFaceSource) Metadata() Metadata {
return g.metadata
}
func (g *GoTextFaceSource) shape(text string, face *GoTextFace) (shaping.Output, []glyph) {
g.copyCheck()

72
text/v2/metadata.go Normal file
View File

@ -0,0 +1,72 @@
// Copyright 2023 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package text
import (
"github.com/go-text/typesetting/opentype/api/metadata"
"github.com/go-text/typesetting/opentype/loader"
)
// Metadata represents a font face's metadata.
type Metadata struct {
Family string
Style Style
Weight Weight
Stretch Stretch
Monospace bool
}
func metadataFromLoader(l *loader.Loader) Metadata {
d := metadata.Metadata(l)
return Metadata{
Family: d.Family,
Monospace: d.IsMonospace,
}
}
type Style uint8
const (
StyleNormal Style = Style(metadata.StyleNormal)
StyleItalic Style = Style(metadata.StyleItalic)
)
type Weight float32
const (
WeightThin Weight = Weight(metadata.WeightThin)
WeightExtraLight Weight = Weight(metadata.WeightExtraLight)
WeightLight Weight = Weight(metadata.WeightLight)
WeightNormal Weight = Weight(metadata.WeightNormal)
WeightMedium Weight = Weight(metadata.WeightMedium)
WeightSemibold Weight = Weight(metadata.WeightSemibold)
WeightBold Weight = Weight(metadata.WeightBold)
WeightExtraBold Weight = Weight(metadata.WeightExtraBold)
WeightBlack Weight = Weight(metadata.WeightBlack)
)
type Stretch float32
const (
StretchUltraCondensed Stretch = Stretch(metadata.StretchUltraCondensed)
StretchExtraCondensed Stretch = Stretch(metadata.StretchExtraCondensed)
StretchCondensed Stretch = Stretch(metadata.StretchCondensed)
StretchSemiCondensed Stretch = Stretch(metadata.StretchSemiCondensed)
StretchNormal Stretch = Stretch(metadata.StretchNormal)
StretchSemiExpanded Stretch = Stretch(metadata.StretchSemiExpanded)
StretchExpanded Stretch = Stretch(metadata.StretchExpanded)
StretchExtraExpanded Stretch = Stretch(metadata.StretchExtraExpanded)
StretchUltraExpanded Stretch = Stretch(metadata.StretchUltraExpanded)
)