Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning text in golang with Truetype

Tags:

go

truetype

I am trying to render some text on a png in a Golang project using freetype/truetype. As you can see from the attachment, I am trying to render 4 letters in columns - each letter centered in the column. Have used the truetype api to get bounds and widths of the glyphs but have been unable to convert these to give me an accurate offset for each glyph. For example with the O glyph, given the font I using. I get the following dimensions:

Hmetric {AdvanceWidth:543 LeftSideBearing:36} Bounds {XMin:0 YMin:-64 XMax:512 YMax:704} Advance width: 512

With the last dimension being returned from GlyphBuf.

I rendered it using the following:

size := 125.00 tileOffset := (int(tileWidth) * i) + int(tileWidth/2) pt := freetype.Pt(tileOffset, (imgH-newCharHeight)-int(size))

How can I use the glyph dimensions returned by truetype to offset the letters correctly? I have tried using the AdvanceWidth as detailed in this plotinum code (line 160) but that does not give me a consistent result across all glyphs.

Example of rendering

like image 409
technix Avatar asked Mar 17 '15 17:03

technix


1 Answers

As suggested by Simon the correct solution is to use AdvanceWidth:

enter image description here

Crude example:

package main

import (
    "flag"
    "fmt"
    "io/ioutil"
    "log"
    "image"
    "bufio"
    "image/draw"
    "image/png"
    "image/color"
    "github.com/golang/freetype/truetype"
    "golang.org/x/image/font"
    "github.com/golang/freetype"
    "os"
)

var (
    dpi      = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
    fontfile = flag.String("fontfile", "/usr/share/fonts/liberation/LiberationSerif-Regular.ttf", "filename of the ttf font")
    hinting  = flag.String("hinting", "none", "none | full")
    size     = flag.Float64("size", 125, "font size in points")
    spacing  = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
    wonb     = flag.Bool("whiteonblack", false, "white text on a black background")
    text     = string("JOJO")
)


func main() {
    flag.Parse()
    fmt.Printf("Loading fontfile %q\n", *fontfile)
    b, err := ioutil.ReadFile(*fontfile)
    if err != nil {
        log.Println(err)
        return
    }
    f, err := truetype.Parse(b)
    if err != nil {
        log.Println(err)
        return
    }

    // Freetype context
    fg, bg := image.Black, image.White
    rgba := image.NewRGBA(image.Rect(0, 0, 1000, 200))
    draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
    c := freetype.NewContext()
    c.SetDPI(*dpi)
    c.SetFont(f)
    c.SetFontSize(*size)
    c.SetClip(rgba.Bounds())
    c.SetDst(rgba)
    c.SetSrc(fg)
    switch *hinting {
    default:
        c.SetHinting(font.HintingNone)
    case "full":
        c.SetHinting(font.HintingFull)
    }

    // Make some background

    // Draw the guidelines.
    ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
    for rcount := 0; rcount < 4; rcount ++ {
        for i := 0; i < 200; i++ {
            rgba.Set(250*rcount, i, ruler)
        }
    }

    // Truetype stuff
    opts := truetype.Options{}
    opts.Size = 125.0
    face := truetype.NewFace(f, &opts)


    // Calculate the widths and print to image
    for i, x := range(text) {
        awidth, ok := face.GlyphAdvance(rune(x))
        if ok != true {
            log.Println(err)
            return
        }
        iwidthf := int(float64(awidth) / 64)
        fmt.Printf("%+v\n", iwidthf)

        pt := freetype.Pt(i*250+(125-iwidthf/2), 128)
        c.DrawString(string(x), pt)
        fmt.Printf("%+v\n", awidth)
    }


    // Save that RGBA image to disk.
    outFile, err := os.Create("out.png")
    if err != nil {
        log.Println(err)
        os.Exit(1)
    }
    defer outFile.Close()
    bf := bufio.NewWriter(outFile)
    err = png.Encode(bf, rgba)
    if err != nil {
        log.Println(err)
        os.Exit(1)
    }
    err = bf.Flush()
    if err != nil {
        log.Println(err)
        os.Exit(1)
    }
    fmt.Println("Wrote out.png OK.")


}
like image 156
user918176 Avatar answered Nov 09 '22 09:11

user918176