Skip to content

Commit

Permalink
[language] Use a constructor to force correct canonication
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitkugler committed Apr 3, 2024
1 parent ced1d68 commit 1cd9336
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 309 deletions.
7 changes: 4 additions & 3 deletions fontscan/fontmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/language"
fontapi "github.com/go-text/typesetting/opentype/api/font"
meta "github.com/go-text/typesetting/opentype/api/metadata"
"github.com/go-text/typesetting/opentype/loader"
Expand Down Expand Up @@ -63,7 +64,7 @@ func ExampleFontMap_AddFace() {
var _ shaping.Fontmap = (*FontMap)(nil)

func TestResolveFont(t *testing.T) {
en, _ := NewLangID("en")
en, _ := NewLangID(language.NewLanguage("en"))

var logOutput bytes.Buffer
logger := log.New(&logOutput, "", 0)
Expand Down Expand Up @@ -115,7 +116,7 @@ func TestResolveForLang(t *testing.T) {
fm.SetQuery(Query{Families: []string{"helvetica"}})

// all system fonts should have support for english
en, _ := NewLangID("en")
en, _ := NewLangID(language.NewLanguage("en"))
face := fm.ResolveFaceForLang(en)
tu.AssertC(t, face != nil, "expected EN to be supported by system fonts")
}
Expand All @@ -141,7 +142,7 @@ func TestResolveFallbackManual(t *testing.T) {
face := fm.ResolveFace('c')
tu.Assert(t, fm.FontLocation(face.Font).File == "user:Amiri")

en, _ := NewLangID("en")
en, _ := NewLangID(language.NewLanguage("en"))
face = fm.ResolveFaceForLang(en)
tu.Assert(t, face != nil && fm.FontLocation(face.Font).File == "user:Amiri")
}
Expand Down
9 changes: 5 additions & 4 deletions fontscan/langset.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ type LangID uint16
// Derived languages not exactly supported are mapped to their primary part : for instance,
// 'fr-be' is mapped to 'fr'
func NewLangID(l language.Language) (LangID, bool) {
la := l.String()
const N = len(languagesRunes)
// binary search
i, j := 0, N
for i < j {
h := i + (j-i)/2
entry := languagesRunes[h]
if l < entry.lang {
if la < entry.lang {
j = h
} else if entry.lang < l {
} else if entry.lang < la {
i = h + 1
} else {
// extact match
Expand All @@ -35,7 +36,7 @@ func NewLangID(l language.Language) (LangID, bool) {
}
// i is the index where l should be :
// try to match the primary part
root := l.Primary()
root := l.Primary().String()
for ; i >= 0; i-- {
entry := languagesRunes[i]
if entry.lang > root { // keep going
Expand Down Expand Up @@ -76,7 +77,7 @@ func (ls LangSet) String() string {
for bit := 0; bit < 64; bit++ {
if page&(1<<bit) != 0 {
id := pageN<<6 | bit
chunks = append(chunks, string(languagesRunes[id].lang))
chunks = append(chunks, languagesRunes[id].lang)
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions fontscan/langset_gen.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package fontscan

import "github.com/go-text/typesetting/language"

// Copyright © 2002 Keith Packard
//
// Permission to use, copy, modify, distribute, and sell this software and its
Expand All @@ -24,7 +22,7 @@ import "github.com/go-text/typesetting/language"

// languagesRunes stores the runes commonly used to write a language
var languagesRunes = [...]struct {
lang language.Language
lang string
runes RuneSet // sorted, inclusive ranges
}{
{ /** index: 0 */
Expand Down
Loading

0 comments on commit 1cd9336

Please sign in to comment.