Skip to content

How to draw several strings of different size and load font data only once? #141

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

Open
lz1998 opened this issue Aug 17, 2021 · 1 comment

Comments

@lz1998
Copy link

lz1998 commented Aug 17, 2021

No description provided.

@HeCorr
Copy link

HeCorr commented Sep 2, 2021

You can use Scale/ScaleAbout but if you scale it too much the text might become pixelated.
(Choosing a bigger font size and scaling it down as opposed to up might avoid that)

Now, if you want to load the font face once and change the font size later, I don't think that's possible.

The best you can do is read the font file once and define the font faces with the different sizes manually.

This code should work but I didn't test it:

(Edit: it seems that SetFontFace is currently broken/gives different results from LoadFontFace. see #76)

// read font file
fontBytes, err := ioutil.ReadFile(fontFilePath)
if err != nil {
	// handle error
}
// parse font
f, err := truetype.Parse(fontBytes)
if err != nil {
	// handle error
}

// define new font face and set it on the context
dc.SetFontFace(truetype.NewFace(f, &truetype.Options{Size: 60}))
dc.DrawString("big", 0, 0)

// again, but with a different size
dc.SetFontFace(truetype.NewFace(f, &truetype.Options{Size: 30}))
dc.DrawString("small", 0, 0)

(Parts of this code were taken from here)

I recommend you create separate functions for loading the font file and defining the font face so your code ends up cleaner. :)

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

No branches or pull requests

2 participants