The Freetype font rasterizer in the Go programming language.

The Freetype font rasterizer in the Go programming language.

To download and install from source:
$ go get github.com/golang/freetype

It is an incomplete port:
  * It only supports TrueType fonts, and not Type 1 fonts nor bitmap fonts.
  * It only supports the Unicode encoding.

There are also some implementation differences:
  * It uses a 26.6 fixed point co-ordinate system everywhere internally,
    as opposed to the original Freetype's mix of 26.6 (or 10.6 for 16-bit
    systems) in some places, and 24.8 in the "smooth" rasterizer.

Freetype-Go is derived from Freetype, which is written in C. Freetype is
copyright 1996-2010 David Turner, Robert Wilhelm, and Werner Lemberg.
Freetype-Go is copyright The Freetype-Go Authors, who are listed in the
AUTHORS file.

Unless otherwise noted, the Freetype-Go source files are distributed
under the BSD-style license found in the LICENSE file.
Owner
Go
The Go Programming Language
Go
Comments
  • Text extents

    Text extents

    With freetype-go, is there a way to compute the text extents (width and height) 
    of a string given a particular font and size *before* rendering the text?
    
    I've looked around in the source code, and it *appears* that the right 
    information is there, but I can't quite seem to figure out how to do it.
    
    As of right now, if I want an image that "snugly" fits some string, I do the 
    following:
    
    Over estimate the extents by multiplying the pixel size of one em unit by the 
    length of the string. I get the width/height this way (assuming one line of 
    text).
    
    I allocate an image with a rectangle of two points: (0, 0) and (width, height).
    
    After rendering the text to the image using DrawText, I use the point returned 
    from DrawText to take a sub-image of the initial image. (I convert this point 
    to a (x, y) position by simply dividing the X and Y of the point by 256. It 
    seems to work OK, but I have no idea if this is correct.)
    
    --------------
    
    And one last question: would the extents include the entire bounding box of the 
    string? The (x,y) position returned by DrawText doesn't seem to cover the parts 
    of the text the dip below the text's base line. Like the tail in the 'y'. My 
    solution has just been to pad the 'y' with a few pixels, but I know this is not 
    optimal since it will break if the font size changes too much.
    
    (My apologies if my terminology is off. I don't have much experience in font 
    rendering.)
    
    I would happily submit a patch if I was nudged in the right direction :-)
    
    Thanks!
    

    Original issue reported on code.google.com by [email protected] on 2 Jun 2012 at 10:44

  • Add getters for a font's name, style

    Add getters for a font's name, style

    This PR aims to address #10 by adding Name and Style funcs to the public Font API.

    Name and Style are retrieved from the first nameRecord that includes both.

    The luxisr font is the only test case I added because I don't have the optional fonts referenced in TestIndex.

    The tests passed when ran against a handful of those available in ttf-mscorefonts-installer.

    The UTF-16 string check on line 346 is a hack. If you know of a better option, I'll happily update it. :)

  • support postscript table parsing

    support postscript table parsing

    I have added a postscript table parsing which will be used with the Thai languaged rendering problem which mentions in https://github.com/golang/go/issues/27281#issuecomment-750339814

    to be able to achieve these glyph combination rules http://www.bakoma-tex.com/doc/fonts/enc/c90/c90.pdf

    we need to be able to get an index of the glyph from the glyph name

    References

    • https://docs.microsoft.com/en-us/typography/opentype/spec/post
    • https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6post.html
  • RoundCapper seems to be

    RoundCapper seems to be "working in reverse", shape is spiky not round

    out

    package main
    
    import (
        "image"
        "image/png"
        "log"
        "os"
    
        "github.com/golang/freetype/raster"
        "golang.org/x/image/math/fixed"
    )
    
    func main() {
        const (
            w = 400
            h = 400
        )
        var p raster.Path
        p.Start(fixed.P(150, 150))
        p.Add1(fixed.P(200, 250))
        p.Add1(fixed.P(300, 250))
    
        r := raster.NewRasterizer(w, h)
        r.UseNonZeroWinding = true
        r.AddStroke(p, fixed.I(80), raster.RoundCapper, raster.RoundJoiner)
    
        rgba := image.NewRGBA(image.Rect(0, 0, w, h))
        painter := raster.NewRGBAPainter(rgba)
        painter.SetColor(image.Black)
        r.Rasterize(painter)
    
        f, err := os.Create("out.png")
        if err != nil {
            log.Fatalf("cannot create file: %v", err)
        }
        defer f.Close()
        if err := png.Encode(f, rgba); err != nil {
            log.Fatalf("writing png: %v", err)
        }
        if err := f.Close(); err != nil {
            log.Fatalf("closing file: %v", err)
        }
    }
    
  • Combining / Non-spacing characters don't work correctly

    Combining / Non-spacing characters don't work correctly

    When I use this module to print text to an image file, everything seems to work correctly. However, in a special case, I want to underline some characters, I tried to use the Combining / Non-spacing character \u0332 for that, but it just comes out as a separate character. There are quite some combining / non-spacing characters, check: http://www.fileformat.info/info/unicode/category/Mn/list.htm

    Here's the result: selection_988

    (the text on the right should be underlined)

    I tried to look into the code to pinpoint the issue, but the code is a little too advanced for me to understand it in a quick look but I'm pretty sure it's in func (g *GlyphBuf) Load(f *Font, scale fixed.Int26_6, i Index, h font.Hinting) error { in truetype/glyph.go, where it calculates the advanceWidth. The font I'm using is DejaVu Sans Mono and DejaVu Sans Mono Bold.

  • Fix Capper & Joiner bugs introduced by 856a70c

    Fix Capper & Joiner bugs introduced by 856a70c

    @nigeltao Do you take pull requests? Here's a fix. Let me know if anything else needs to be done.

    Some constants were not updated properly when switching from raster.Point to fixed.26_6

    Before the fix:

    before

    After the fix:

    after

  • go get github.com/golang/freetype failed

    go get github.com/golang/freetype failed

    go get github.com/golang/freetype

    package golang.org/x/image/math/fixed: unrecognized import path "golang.org/x/im age/math/fixed" package golang.org/x/image/font: unrecognized import path "golang.org/x/image/fo nt"

  • Add stroke functionality to glyphs

    Add stroke functionality to glyphs

    This adds the ability to set stroke width for font drawing. Drawing a black stroked glyph and then a white normal glyph will give the font a black stroke.

    The only problem is that the rasterizer doesn't allow closing paths. If the start and end point are at the same position, the strokes will overlap and appear thicker (alpha channel overlaps). If the start and end points are at an angle, using ButtCapping will reveal that they do not properly connect. The rasterizer must support path closing for this.

    Secondly, this should be added to github.com/golang/freetype/freetype.go too?

  • Artifacts close to start/end points

    Artifacts close to start/end points

    
    Freetype-go generates artifacts close to start and end path points,
    output differs from Freetype-C.
    
    Font is automatically converted from C to Q by FontForge, no problems detected.
    Rearanged canonical start point to leftmost on the contour produces expected 
    output.
    
    
    

    Original issue reported on code.google.com by [email protected] on 20 Feb 2015 at 9:48

    Attachments:

  • Fix TTC parsing

    Fix TTC parsing

    I have no idea how to submit patches, so forgive me if this is an inappropriate 
    venue.
    
    Attached is a patch fixing TTC parsing.
    The constant 12 was hardcoded instead of using `offset`.
    
    Tested on a TTC v2.0 file, I don't have a v1.0 file to test on but based on 
    what I understand of the file format I don't think there should be any issues 
    with that.
    

    Original issue reported on code.google.com by [email protected] on 10 Sep 2014 at 4:30

    Attachments:

  • request: truetype font Name() API

    request: truetype font Name() API

    I would like to be able to get at the font name(s) stored in the truetype in 
    the font file. 
    

    Original issue reported on code.google.com by [email protected] on 29 May 2014 at 12:39

  • Corruption when using WorkSans-Black

    Corruption when using WorkSans-Black

    Describe the bug: When using the WorkSans-Black font certain cross-elements are corripted. The font shows gaps instead of a solid character, as per images below

    To Reproduce: Steps to reproduce the behaviour:

    • Go to google fonts and download WorkSans-Black.ttf
    • Any code that rasterises using "image/draw" call to DrawMask passing the parsed font (from truetype.Parse)
    • render any label with "t" or "H"

    Notice the holes in the output: 175044562-277cbace-4466-45d3-9b86-3b658bab9d97

    The same does not happen when you download the font from https://www.1001fonts.com/work-sans-font.html...

  • Failing to rasterize non-latin character correctly

    Failing to rasterize non-latin character correctly

    Go 1.17.6 Windows 11 amd64 github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0

    I'm trying to use the library to draw some text in transparent background, but it doesn't matter what font I use, it fails to correctly rasterize non-latin characters:

    out

    Here is a screenshot of Notepad using the same font:

    image

  • prefer full/UCS4 sub-tables over BMP/UCS2 ones

    prefer full/UCS4 sub-tables over BMP/UCS2 ones

    Some ttf files contain several sub-tables. For example, a ttf file may contain one unicodeEncodingBMPOnly table, one microsoftUCS2Encoding table, and one microsoftUCS4Encoding table. In this case, we choose microsoftUCS4Encoding as the best one. Since there are most valid codes in it.

  • range bug

    range bug

    panic: runtime error: index out of range [104] with length 104
    
    goroutine 319 [running]:
    github.com/golang/freetype/truetype.(*GlyphBuf).loadSimple(0x17e9590, 0x1583df0, 0x68, 0x75cb5, 0x1, 0x1583ce6, 0x0, 0x75dbf)
    	C:/gowork/pkg/mod/github.com/golang/[email protected]/truetype/glyph.go:331 +0x590
    github.com/golang/freetype/truetype.(*GlyphBuf).load(0x17e9590, 0x0, 0x10012, 0x0, 0x0)
    	C:/gowork/pkg/mod/github.com/golang/[email protected]/truetype/glyph.go:229 +0x264
    github.com/golang/freetype/truetype.(*GlyphBuf).Load(0x17e9590, 0x10d6100, 0x900, 0x12, 0x0, 0x0, 0xd)
    	C:/gowork/pkg/mod/github.com/golang/[email protected]/truetype/glyph.go:102 +0x90
    github.com/golang/freetype/truetype.(*face).GlyphAdvance(0x17e8000, 0x33, 0x33, 0x0)
    	C:/gowork/pkg/mod/github.com/golang/[email protected]/truetype/face.go:345 +0x58
    golang.org/x/image/font.MeasureString(0x61f42c, 0x17e8000, 0x1530da0, 0xc, 0x0)
    	C:/gowork/pkg/mod/golang.org/x/[email protected]/font/font.go:301 +0x80
    golang.org/x/image/font.(*Drawer).MeasureString(...)
    	C:/gowork/pkg/mod/golang.org/x/[email protected]/font/font.go:215
    
  • Round scale to nearest 26.6 fixed point.

    Round scale to nearest 26.6 fixed point.

    With this change, the computation of the scale factor becomes identical across the freetype and truetype packages, removing deviations in the font metrics that are derived from scale.

    Apply the same change to the Context.PointToFixed method to keep it in sync.

    The rounding computation is newer; it was introduced when the truetype.Face type was added in commit 6deea2414309d03c665a7b.

    Fixes #85.

  • discrepancy in scale factor conversion between freetype and truetype packages

    discrepancy in scale factor conversion between freetype and truetype packages

    I noticed there's sometimes a tiny discrepancy in font metrics as computed by freetype.Context and truetype.NewFace.

    As a reproducible example, using the Go Mono font of size 86.4 exactly, at 72.0 DPI, the advance width for the glyph 'H' differs by 1/64 (the smallest value a fixed.Int26_6 can represent).

    See the complete program on the Go Playground. Its output:

    advance width of 'H' via truetype: 51:55
    advance width of 'H' via freetype: 51:54
    

    I've tracked it down and found the root cause. When computing the scale factor, the float64 → fixed 26.6 conversion is done differently between those two packages. In truetype, it rounds to the nearest 26.6 fixed point value:

    scale:      fixed.Int26_6(0.5 + (opts.size() * opts.dpi() * 64 / 72)),
    

    But in freetype, it uses the floor:

    c.scale = fixed.Int26_6(c.fontSize * c.dpi * (64.0 / 72.0))
    

    Between those two, it seems taking the nearest value is the better behavior, so I'll send a PR that adjusts freetype to fix this discrepancy. CC @nigeltao.

String i18n utilities for the Go Programming Language

About polyglot polyglot is a String translation package and tool for Go. Setup Make sure you have a working Go installation. See Getting Started Now r

Dec 22, 2022
A simple programming language with emojis only

MOGEE mogee (pronounced ēˈmōjē) is an extremely simple interpreted programming language that consists solely of emojis. Similar to Brainfuck, mogee is

Nov 15, 2021
Interpreted Programming Language built in Go. Lexer, Parser, AST, VM.

Gago | Programming Language Built in Go if you are looking for the docs, go here Gago is a interpreted programming language. It is fully written in Go

May 6, 2022
Lingo - Literate Programming with Go + Markdown

lingo: literate programming with Go + Markdown lingo is a simple tool for litera

Mar 22, 2022
Go library for the TOML language

go-toml Go library for the TOML format. This library supports TOML version v1.0.0-rc.3 Features Go-toml provides the following features for using data

Dec 27, 2022
Parses the Graphviz DOT language in golang

Parses the Graphviz DOT language and creates an interface, in golang, with which to easily create new and manipulate existing graphs which can be writ

Dec 25, 2022
Guess the natural language of a text in Go

guesslanguage This is a Go version of python guess-language. guesslanguage provides a simple way to detect the natural language of unicode string and

Dec 26, 2022
Unified text diffing in Go (copy of the internal diffing packages the officlal Go language server uses)

gotextdiff - unified text diffing in Go This is a copy of the Go text diffing packages that the official Go language server gopls uses internally to g

Dec 26, 2022
Simple HCL (HashiCorp Configuration Language) parser for your vars.

HCL to Markdown About To write a good documentation for terraform module, quite often we just need to print all our input variables as a fancy table.

Dec 14, 2021
An anthology of a variety of tools for the Persian language in Golang
An anthology of a variety of tools for the Persian language in Golang

Persian tools An anthology of a variety of tools for the Persian language in Golang Todos Bill calculator Digits Validate Bank card number. Find Bank'

Nov 22, 2022
Frongo is a Golang package to create HTML/CSS components using only the Go language.

Frongo Frongo is a Go tool to make HTML/CSS document out of Golang code. It was designed with readability and usability in mind, so HTML objects are c

Jul 29, 2021
👄 The most accurate natural language detection library in the Go ecosystem, suitable for long and short text alike
👄 The most accurate natural language detection library in the Go ecosystem, suitable for long and short text alike

?? The most accurate natural language detection library in the Go ecosystem, suitable for long and short text alike

Dec 25, 2022
Toy scripting language with a syntax similar to Rust.

Dust - toy scripting language Toy scripting language with a syntax similar to Rust. ?? Syntax similar to Rust ?? Loose JSON parsing ?? Calling host fu

Sep 28, 2022
Build "Dictionary of the Old Norwegian Language" into easier-to-use data formats

Old Norwegian Dictionary Builder Build "Dictionary of the Old Norwegian Language" into easier-to-use data formats. Available formats: JSON DSL XML Usa

Oct 11, 2022
Rasterx is an SVG 2.0 path compliant rasterizer that can use either the golang vector or a derivative of the freetype anti-aliaser.
Rasterx is an SVG 2.0 path compliant rasterizer that can use either the golang vector or a derivative of the freetype anti-aliaser.

rasterx Rasterx is a golang rasterizer that implements path stroking functions capable of SVG 2.0 compliant 'arc' joins and explicit loop closing. Pat

Nov 1, 2022
Fonts is a package that provides helpers to access font details and easily retrive font bytes with ZERO dependencies

Fonts Fonts is a package that provides helpers to access font details and easily retrieve font bytes. This package has ZERO 3rd-party dependencies. Fo

Mar 3, 2022
tigrfont is a commandline tool for creating bitmap font sheets for TIGR from TTF or BDF font files.
tigrfont is a commandline tool for creating bitmap font sheets for TIGR from TTF or BDF font files.

tigrfont - bitmap font sheet generator for TIGR tigrfont is a commandline tool for creating bitmap font sheets for TIGR from TTF or BDF font files. TI

Dec 5, 2022
Floppa programming language inspired by the brainf*ck programming language. Created just for fun and you can convert your brainf*ck code to floppa code.

Floppa Programming Language Created just for fun. But if you want to contribute, why not? Floppa p.l. inspired by the brainf*ck programming language.

Oct 20, 2022
T# Programming Language. Something like Porth, Forth but written in Go. Stack-oriented programming language.

The T# Programming Language WARNING! THIS LANGUAGE IS A WORK IN PROGRESS! ANYTHING CAN CHANGE AT ANY MOMENT WITHOUT ANY NOTICE! Something like Forth a

Jun 29, 2022
Yayx programming language is begginer friendly programming language.
Yayx programming language is begginer friendly programming language.

Yayx Yayx programming language is begginer friendly programming language. What have yayx: Easy syntax Dynamic types Can be compiled to outhers program

Dec 27, 2021