draw2d
draw2d copied to clipboard
More examples about Fonts
Hello,
I believe more examples about Fonts and Font configuration would be nice, although it's possible it's my fault.
I've tried to draw a string on GNU/Linux and kept failing. This is because of two things. I don't have a luxi
font and FontFileName()
makes platform/OS(?) specific assumptions about TTF font filenames. But looking at the filenames at my GNU/Linux in /usr/share/fonts/truetype/freefont
, there is no predictable way to determine filenames. Therefore, it seems to me like specific FontFileName()
might be needed ... a lot.
So, here is an example for my specific case:
package main
import (
"image"
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dimg"
)
func MyFontFileName(fontData draw2d.FontData) string {
fontFileName := fontData.Name
switch fontData.Family {
case draw2d.FontFamilySans:
fontFileName += "Sans"
case draw2d.FontFamilySerif:
fontFileName += ""
case draw2d.FontFamilyMono:
fontFileName += "Mono"
}
if fontData.Style&draw2d.FontStyleBold != 0 {
fontFileName += "Bold"
} else {
fontFileName += ""
}
if fontData.Style&draw2d.FontStyleItalic != 0 {
fontFileName += "Italic"
}
fontFileName += ".ttf"
return fontFileName
}
func main() {
// Initialize the graphic context on an RGBA image
dest := image.NewRGBA(image.Rect(0, 0, 297, 210))
gc := draw2dimg.NewGraphicContext(dest)
// This is to get FreeMonoBold.ttf
fontData := draw2d.FontData{
Name: "Free",
Family: draw2d.FontFamilyMono,
Style: draw2d.FontStyleBold,
}
draw2d.SetFontFolder("/usr/share/fonts/truetype/freefont")
draw2d.SetFontNamer(MyFontFileName)
gc.SetFontData(fontData)
gc.FillStringAt("Hello world", 10, 40)
draw2dimg.SaveToPngFile("/dev/shm/hello.png", dest)
}
As I've said, it might be me I haven't figured it out immediately. But it might help to have it in examples or documentation.
Thanks.
You're right it would be nice to have more doc on it this one seems to be a good one. We have already a PR on FontCache that can have impact on this #94