go-text / typesetting

High quality text shaping in pure Go.
Other
88 stars 11 forks source link

harfbuzz: shaping with Unifont font panics #140

Closed dominikh closed 3 months ago

dominikh commented 3 months ago

Input program:

package main

import (
    "log"

    "github.com/go-text/typesetting/fontscan"
    "github.com/go-text/typesetting/harfbuzz"
    "github.com/go-text/typesetting/language"
)

func main() {
    fmap := fontscan.NewFontMap(log.Default())
    if err := fmap.UseSystemFonts(""); err != nil {
        log.Fatalln("couldn't load system fonts:", err)
    }
    s := []rune("a")
    fmap.SetQuery(fontscan.Query{Families: []string{"unifont"}})
    face := fmap.ResolveFace(s[0])
    if face == nil {
        log.Fatal("couldn't find any font")
    }

    family, _ := fmap.FontMetadata(face.Font)
    if family != "unifont" {
        log.Fatalf("loaded %q instead of %q", family, "unifont")
    }

    buf := harfbuzz.NewBuffer()
    buf.Props.Language = "en-us"
    buf.Props.Script = language.Latin
    buf.Props.Direction = harfbuzz.LeftToRight
    buf.AddRunes(s, 0, 1)
    font := harfbuzz.NewFont(face)
    buf.Shape(font, nil)
}

Output when running:

2024/03/14 02:30:59 fontconfig referenced path "local.conf", but it could not be resolved to a real path
2024/03/14 02:30:59 missing fontconfig include /home/dominikh/.config/fontconfig/fonts.conf: skipping
2024/03/14 02:30:59 missing fontconfig include /home/dominikh/.fonts.conf.d: skipping
2024/03/14 02:30:59 missing fontconfig include /home/dominikh/.fonts.conf: skipping
2024/03/14 02:30:59 missing fontconfig include /nix/store/40nlkb4c2sjn588dh761yh6qbxazvlhg-home-manager-path/etc/fonts/conf.d: skipping
2024/03/14 02:30:59 missing fontconfig include /nix/store/40nlkb4c2sjn588dh761yh6qbxazvlhg-home-manager-path/etc/fonts/fonts.conf: skipping
2024/03/14 02:30:59 using system font dirs ["/home/dominikh/.local/share/fonts" "/home/dominikh/.nix-profile/share/fonts" "/nix/store/2ghx00bszxscdn0zfvx7ydyjvzarpgm0-freefont-ttf-20120503" "/nix/store/3sn4sdl59hnk6diaqfqzfwvc3qdpmv3j-gyre-fonts-2.005" "/nix/store/40nlkb4c2sjn588dh761yh6qbxazvlhg-home-manager-path/share/fonts" "/nix/store/57ccgyl8sgz9nrcsag4f79zwn74v6hq3-terminus-font-4.49.1" "/nix/store/5b87c7ia6m76kys0nmin80839p3b74f0-input-fonts-1.2" "/nix/store/6zgfqxdvl8yki176mfagxa08q3m5wr8x-fira-code-6.2" "/nix/store/73p9v3qr7l1h8vs8fi5ji36krrychkkj-dejavu-fonts-minimal-2.37" "/nix/store/74yd8mhw1vbca13ylp9y8ihq13rf35mr-corefonts-1" "/nix/store/axwfqgf7fpw3s9hnlrv591sk2180zd7x-emacs-all-the-icons-fonts-5.0.0" "/nix/store/c123p9zmnl33ql4n5pvwlgml8d49k0kq-noto-fonts-color-emoji-2.042" "/nix/store/d3j1yyq8cz9apc082j2cvs48khhi34ma-unifont-15.1.04" "/nix/store/fs58cb7bxzjnyifp43bmn3b2p233a9w9-font-xfree86-type1-1.0.5" "/nix/store/jqkd5vajcdlsg93nnnx4rwhy3jqbq230-dejavu-fonts-2.37" "/nix/store/nwn963src6wrx9v7ivm6lq2y3j73gwnq-noto-fonts-24.2.1" "/nix/store/q8ny4mjzqsfwi34c2592ahg7qbdcadpc-uw-ttyp0-1.3" "/nix/store/rp7kjp0w7j4lrhrcb26innhh4nn1rwlx-font-misc-misc-1.1.3" "/nix/store/w8lrxpj7nb5qsmk6vgr5s25qc49d9f43-liberation-fonts-2.1.5"]
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/go-text/typesetting/harfbuzz.getRequiredFeature(...)
    /home/dominikh/prj/go-text/typesetting/harfbuzz/ot_layout.go:247
github.com/go-text/typesetting/harfbuzz.(*otMapBuilder).compile(0x0?, 0x1646e6f6d?, {0xffffffffffffffff, 0xffffffffffffffff})
    /home/dominikh/prj/go-text/typesetting/harfbuzz/ot_map.go:146 +0x100
github.com/go-text/typesetting/harfbuzz.(*otShapePlanner).compile(0xc00012c000, 0xc000004710, {0xffffffffffffffff, 0xffffffffffffffff})
    /home/dominikh/prj/go-text/typesetting/harfbuzz/ot_shaper.go:64 +0xac
github.com/go-text/typesetting/harfbuzz.(*otShapePlan).init0(0xc000004710, 0x0?, {{0x61d0e9?, 0x0?}, 0xca000?, 0xc0?}, {0x0, 0x0, 0x0}, {0xffffffffffffffff, ...})
    /home/dominikh/prj/go-text/typesetting/harfbuzz/ot_shaper.go:173 +0x7d
github.com/go-text/typesetting/harfbuzz.(*shaperOpentype).compile(...)
    /home/dominikh/prj/go-text/typesetting/harfbuzz/ot_shaper.go:777
github.com/go-text/typesetting/harfbuzz.newShapePlan(0xc0000d61e0, {{0x61d0e9?, 0x0?}, 0x41da8?, 0xc0?}, {0x0, 0x0, 0x0}, {0x0, 0x0, ...})
    /home/dominikh/prj/go-text/typesetting/harfbuzz/shape.go:109 +0xe6
github.com/go-text/typesetting/harfbuzz.(*Buffer).newShapePlanCached(0xc0000e6410, 0xc0000d61e0, {{0x61d0e9?, 0x0?}, 0x61?, 0x0?}, {0x0, 0x0, 0x0}, {0x0, ...})
    /home/dominikh/prj/go-text/typesetting/harfbuzz/shape.go:146 +0x369
github.com/go-text/typesetting/harfbuzz.(*Buffer).Shape(0xc0000e6410, 0xc0000d61e0, {0x0, 0x0, 0x0})
    /home/dominikh/prj/go-text/typesetting/harfbuzz/shape.go:30 +0x7f
main.main()
    /home/dominikh/prj/src/honnef.co/go/gutter/paint/demo2.go:36 +0x359
exit status 2

This is on linux/amd64. Unifont is a OTF bitmap font.

benoitkugler commented 3 months ago

Thank you for the bug report. It seems that the font is somewhat corrupted, but we should not crash.