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

feat: parse open type features #317

Merged
merged 1 commit into from
Oct 16, 2024
Merged

Conversation

nobe4
Copy link
Contributor

@nobe4 nobe4 commented Oct 16, 2024

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.

E.g.

func main() {
	c := canvas.New(100, 100)
	ctx := canvas.NewContext(c)
	font, err := canvas.LoadFontFile("./examples/font/Fira/FiraSans-Regular.ttf", canvas.FontRegular)
	if err != nil {
		panic(err)
	}

	f, err := os.Create("out.pdf")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	pdf := pdf.New(f, 100, 100, &pdf.Options{})

	features := []string{
		"",
		"subs",
		"tnum",
		"zero",
		"smcp, zero",
	}

	y := 90.0
	for _, feature := range features {
		font.SetFeatures(feature)
		face := font.Face(20.0, canvas.Black)
		txt := canvas.NewTextLine(face, "Aabc ff 3.14 0O "+feature, canvas.Left)
		ctx.DrawText(0, y, txt)
		y -= 10.0
	}

	c.RenderTo(pdf)

	if err := pdf.Close(); err != nil {
		panic(err)
	}
}

Test font used: https://fonts.google.com/specimen/Fira+Sans

image

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.
@tdewolff
Copy link
Owner

Awesome, thank you!

@tdewolff tdewolff merged commit e8e42bb into tdewolff:master Oct 16, 2024
1 of 2 checks passed
nobe4 added a commit to nobe4/canvas that referenced this pull request Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants