Skip to content

Commit

Permalink
[opentype/metadata] properly use the OS/2 table to fetch aspect
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitkugler committed Nov 21, 2023
1 parent af78120 commit 9ced926
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
13 changes: 9 additions & 4 deletions opentype/api/metadata/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ type fontDescriptor struct {
head tables.Head
}

// Describe provides access to family and aspect.
// 'buffer' may be provided to reduce allocations.
func Describe(ld *loader.Loader, buffer []byte) (family string, aspect Aspect, _ []byte) {
func newFontDescriptor(ld *loader.Loader, buffer []byte) (fontDescriptor, []byte) {
var desc fontDescriptor

// load tables, all considered optional
buffer, _ = ld.RawTableTo(loader.MustNewTag("OS/2"), buffer)
if os2, _, err := tables.ParseOs2(buffer); err != nil {
if os2, _, err := tables.ParseOs2(buffer); err == nil {
desc.os2 = &os2Table{
USWeightClass: os2.USWeightClass,
USWidthClass: os2.USWidthClass,
Expand All @@ -50,6 +48,13 @@ func Describe(ld *loader.Loader, buffer []byte) (family string, aspect Aspect, _
buffer, _ = ld.RawTableTo(loader.MustNewTag("name"), buffer)
desc.names, _, _ = tables.ParseName(buffer)

return desc, buffer
}

// Describe provides access to family and aspect.
// 'buffer' may be provided to reduce allocations.
func Describe(ld *loader.Loader, buffer []byte) (family string, aspect Aspect, _ []byte) {
desc, buffer := newFontDescriptor(ld, buffer)
return desc.Family(), desc.Aspect(), buffer
}

Expand Down
24 changes: 24 additions & 0 deletions opentype/api/metadata/descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func TestMetadata(t *testing.T) {
Aspect{StyleNormal, WeightNormal, StretchNormal},
"Noto Sans Arabic",
},
{
"common/DejaVuSans.ttf",
Aspect{StyleNormal, WeightNormal, StretchNormal},
"DejaVu Sans",
},
}

for _, test := range tests {
Expand Down Expand Up @@ -88,3 +93,22 @@ func TestAspect_inferFromStyle(t *testing.T) {
tu.AssertC(t, as == tt.want, tt.args)
}
}

func TestAspectFromOS2(t *testing.T) {
// This font has two different weight values :
// 400, in the OS/2 table and 380, in the style description
f, err := td.Files.ReadFile("common/DejaVuSans.ttf")
tu.AssertNoErr(t, err)

ld, err := loader.NewLoader(bytes.NewReader(f))
tu.AssertNoErr(t, err)

fd, _ := newFontDescriptor(ld, nil)

raw := fd.rawAspect()
tu.Assert(t, raw.Weight == WeightNormal)

var inferred Aspect
inferred.inferFromStyle(fd.additionalStyle())
tu.Assert(t, inferred.Weight == 380)
}

0 comments on commit 9ced926

Please sign in to comment.