diff --git a/go.mod b/go.mod index aec5608..db6d212 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/go-text/typesetting go 1.17 require ( - github.com/go-text/typesetting-utils v0.0.0-20231211103740-d9332ae51f04 + github.com/go-text/typesetting-utils v0.0.0-20240317173224-1986cbe96c66 golang.org/x/image v0.3.0 golang.org/x/text v0.9.0 ) diff --git a/go.sum b/go.sum index b1138d0..4534de2 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/go-text/typesetting-utils v0.0.0-20231204162240-fa4dc564ba79 h1:3yBOz github.com/go-text/typesetting-utils v0.0.0-20231204162240-fa4dc564ba79/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o= github.com/go-text/typesetting-utils v0.0.0-20231211103740-d9332ae51f04 h1:zBx+p/W2aQYtNuyZNcTfinWvXBQwYtDfme051PR/lAY= github.com/go-text/typesetting-utils v0.0.0-20231211103740-d9332ae51f04/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o= +github.com/go-text/typesetting-utils v0.0.0-20240317173224-1986cbe96c66 h1:GUrm65PQPlhFSKjLPGOZNPNxLCybjzjYBzjfoBGaDUY= +github.com/go-text/typesetting-utils v0.0.0-20240317173224-1986cbe96c66/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= diff --git a/harfbuzz/fonts_test.go b/harfbuzz/fonts_test.go index 8b7623c..3d12843 100644 --- a/harfbuzz/fonts_test.go +++ b/harfbuzz/fonts_test.go @@ -5,6 +5,7 @@ import ( "reflect" "testing" + "github.com/go-text/typesetting/language" "github.com/go-text/typesetting/opentype/api/font" tu "github.com/go-text/typesetting/opentype/testutils" ) @@ -259,3 +260,16 @@ func TestNames(t *testing.T) { /* beyond last glyph */ tu.Assert(t, ft.GlyphName(2000) == "") } + +func TestUnifont(t *testing.T) { + // https://github.com/go-text/typesetting/issues/140 + ft := openFontFileTT(t, "bitmap/unifont-15.1.05.otf") + + buf := NewBuffer() + buf.Props.Language = "en-us" + buf.Props.Script = language.Latin + buf.Props.Direction = LeftToRight + buf.AddRunes([]rune{'a'}, 0, 1) + font := NewFont(&font.Face{Font: ft}) + buf.Shape(font, nil) // just check for crashes +} diff --git a/opentype/api/font/ot_layout.go b/opentype/api/font/ot_layout.go index 848ab62..43e641d 100644 --- a/opentype/api/font/ot_layout.go +++ b/opentype/api/font/ot_layout.go @@ -12,11 +12,18 @@ type Layout struct { } func newLayout(table tables.Layout) Layout { + fCount := len(table.FeatureList.Features) out := Layout{ Scripts: make([]Script, len(table.ScriptList.Scripts)), - Features: make([]Feature, len(table.FeatureList.Features)), + Features: make([]Feature, fCount), } for i, s := range table.ScriptList.Scripts { + if langSys := s.DefaultLangSys; langSys != nil { + sanitizeLangSys(langSys, fCount) + } + for i := range s.LangSys { + sanitizeLangSys(&s.LangSys[i], fCount) + } out.Scripts[i] = Script{ Script: s, Tag: table.ScriptList.Records[i].Tag, @@ -34,6 +41,13 @@ func newLayout(table tables.Layout) Layout { return out } +func sanitizeLangSys(langSys *tables.LangSys, featuresCount int) { + if int(langSys.RequiredFeatureIndex) >= featuresCount { + // invalid index : replace it by the sentinel value + langSys.RequiredFeatureIndex = 0xFFFF + } +} + type Script struct { tables.Script Tag Tag