Skip to content
New issue

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

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

Already on GitHub? # to your account

feat: parse open type features #317

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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
28 changes: 27 additions & 1 deletion text/harfbuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package text

import (
"bytes"
"fmt"
"os"
"strings"

"github.com/go-text/typesetting/harfbuzz"
"github.com/go-text/typesetting/language"
Expand Down Expand Up @@ -52,7 +55,7 @@ func (s Shaper) Shape(text string, ppem uint16, direction Direction, script Scri
buf.Props.Script = language.Script(script)
buf.Props.Language = language.NewLanguage(lang)
buf.GuessSegmentProperties() // only sets direction, script, and language if unset
buf.Shape(s.font, nil)
buf.Shape(s.font, parseFeatures(features))

runeMap := make([]int, len(rtext)+1)
j := 0
Expand All @@ -77,6 +80,29 @@ func (s Shaper) Shape(text string, ppem uint16, direction Direction, script Scri
return glyphs
}

func parseFeatures(input string) []harfbuzz.Feature {
if input == "" {
return nil
}

features := []harfbuzz.Feature{}
for _, part := range strings.Split(input, ",") {
if part == "" {
continue
}

feature, err := harfbuzz.ParseFeature(part)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing feature '%s': %v\n", part, err)
continue
}

features = append(features, feature)
}

return features
}

func LookupScript(r rune) Script {
return Script(language.LookupScript(r))
}
Expand Down
Loading