Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language constructor #157

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion font/opentype/tables/aat_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,5 +415,5 @@ func TestParseLtag(t *testing.T) {
tu.AssertNoErr(t, err)

tu.Assert(t, len(ltag.tagRange) == 1)
tu.Assert(t, ltag.Language(0) == "pl")
tu.Assert(t, ltag.Language(0).String() == "pl")
}
7 changes: 4 additions & 3 deletions fontscan/fontmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/go-text/typesetting/font"
ot "github.com/go-text/typesetting/font/opentype"
"github.com/go-text/typesetting/language"
"github.com/go-text/typesetting/shaping"
tu "github.com/go-text/typesetting/testutils"
)
Expand Down Expand Up @@ -61,7 +62,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 @@ -113,7 +114,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 @@ -139,7 +140,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