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

truetype: parse TTF failed with "bad TTF version" #76

Open
sbinet opened this issue May 23, 2020 · 9 comments
Open

truetype: parse TTF failed with "bad TTF version" #76

sbinet opened this issue May 23, 2020 · 9 comments

Comments

@sbinet
Copy link
Member

sbinet commented May 23, 2020

the following program:

package main

import (
	"log"

	"github.com/go-fonts/latin-modern/lmroman12regular"
	"github.com/golang/freetype/truetype"
)

func main() {
	_, err := truetype.Parse(lmroman12regular.TTF)
	if err != nil {
		log.Fatalf("could not parse TTF data: %+v", err)
	}
}

fails with:

$> go run ./main.go
2020/05/23 20:34:03 could not parse TTF data: freetype: invalid TrueType format: bad TTF version
exit status 1
@sbinet
Copy link
Member Author

sbinet commented May 23, 2020

the equivalent program, using x/image/font/sfnt yields:

2020/05/23 21:10:19 num-glyphs: 821
package main

import (
	"log"

	"github.com/go-fonts/latin-modern/lmroman12regular"
	"golang.org/x/image/font/sfnt"
)

func main() {
	fnt, err := sfnt.Parse(lmroman12regular.TTF)
	if err != nil {
		log.Fatalf("could not parse TTF data: %+v", err)
	}
	log.Printf("num-glyphs: %d", fnt.NumGlyphs())
}

@pelejorwa123
Copy link

did you find a good solution for this? I have to use free-type library to draw text onto image, but I failed loading ttf file with exception“invalid TrueType format: bad TTF version”

@sbinet
Copy link
Member Author

sbinet commented Dec 17, 2020

I've migrated to using golang.org/x/image/font/opentype now.
(the latest version of x/image/font/opentype implements the x/image/font.Face interface so I am good.)

@lz1998
Copy link

lz1998 commented Aug 20, 2021

I've migrated to using golang.org/x/image/font/opentype now.
(the latest version of x/image/font/opentype implements the x/image/font.Face interface so I am good.)

.ttc is not supported by opentype. Is there a good solution for this?

@sbinet
Copy link
Member Author

sbinet commented Aug 20, 2021

.ttc is not supported by opentype. Is there a good solution for this?

hum... last time I tried, opentype was able to parse .ttc collections. (one needs to use opentype.ParseCollection though).

@lz1998
Copy link

lz1998 commented Aug 20, 2021

.ttc is not supported by opentype. Is there a good solution for this?

hum... last time I tried, opentype was able to parse .ttc collections. (one needs to use opentype.ParseCollection though).

How to convert a collection to a font face?

@sbinet
Copy link
Member Author

sbinet commented Aug 20, 2021

something like that should work:

package main

import (
	"fmt"
	"log"

	"golang.org/x/image/font"
	"golang.org/x/image/font/opentype"
)

func main() {
	fmt.Println("Hello, playground")
	var raw []byte // = ... some TTC collection
	ttc, err := opentype.ParseCollection(raw)
	if err != nil {
		log.Fatalf("could not parse collection: %+v", err)
	}
	fmt.Printf("# of fonts in collection: %d\n", ttc.NumFonts())

	fnt, err := ttc.Font(0) // get first font.
	if err != nil {
		log.Fatalf("could not open font #0: %+v", err)
	}

	face, err := opentype.NewFace(fnt, &opentype.FaceOptions{
		Size:    32,
		DPI:     72,
		Hinting: font.HintingNone,
	})
	if err != nil {
		log.Fatalf("could not open face #0: %+v", err)
	}
	defer face.Close()
	fmt.Printf("face: %+v\n", face)
}

https://play.golang.org/p/8YxgSHnS-qa

@lz1998
Copy link

lz1998 commented Aug 20, 2021

something like that should work:

package main

import (
	"fmt"
	"log"

	"golang.org/x/image/font"
	"golang.org/x/image/font/opentype"
)

func main() {
	fmt.Println("Hello, playground")
	var raw []byte // = ... some TTC collection
	ttc, err := opentype.ParseCollection(raw)
	if err != nil {
		log.Fatalf("could not parse collection: %+v", err)
	}
	fmt.Printf("# of fonts in collection: %d\n", ttc.NumFonts())

	fnt, err := ttc.Font(0) // get first font.
	if err != nil {
		log.Fatalf("could not open font #0: %+v", err)
	}

	face, err := opentype.NewFace(fnt, &opentype.FaceOptions{
		Size:    32,
		DPI:     72,
		Hinting: font.HintingNone,
	})
	if err != nil {
		log.Fatalf("could not open face #0: %+v", err)
	}
	defer face.Close()
	fmt.Printf("face: %+v\n", face)
}

https://play.golang.org/p/8YxgSHnS-qa

Is it possible to use all fonts?
There are several language in the collection (Chinese, Japanese and Korean)

@sbinet
Copy link
Member Author

sbinet commented Aug 20, 2021

of course.
one font.Face (with a matching font.Face.Close() when finished) per font you want to use.
(as much as physical memory can serve)

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

3 participants