Skip to content

More examples about Fonts #100

Open
Open

Description

@zstyblik

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.

Activity

  1. llgcode commented on Apr 25, 2016

    @llgcode
    Owner

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions