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

More examples about Fonts #100

Open
zstyblik opened this issue Apr 23, 2016 · 1 comment
Open

More examples about Fonts #100

zstyblik opened this issue Apr 23, 2016 · 1 comment

Comments

@zstyblik
Copy link
Contributor

zstyblik commented Apr 23, 2016

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.

@llgcode
Copy link
Owner

llgcode commented Apr 25, 2016

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

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