Skip to content

Commit

Permalink
feat: parse open type features
Browse files Browse the repository at this point in the history
This adds support for OpenType Font Features. It does a simple parsing
of the input string and uses typesetting/harfbuzz existing method to set
the feature.

This makes the `font.SetFeature` method actually work. TODOs could be
removed after that.
  • Loading branch information
nobe4 committed Oct 16, 2024
1 parent d601972 commit 38f25b7
Showing 1 changed file with 27 additions and 1 deletion.
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

0 comments on commit 38f25b7

Please sign in to comment.