Hi 👋
I couldn't make a simple program working with an OTF font.
I used: https://velvetyne.fr/download/?font=karrik
Code sample:
packagemainimport("fmt""github.com/go-pdf/fpdf")funcmain(){pdf:=fpdf.New("P","mm","A4",".")pdf.AddUTF8Font("f","B","./Karrik-Regular.otf")pdf.AddPage()pdf.SetFont("f","B",20)pdf.Write(10,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")err:=pdf.OutputFileAndClose("hello.pdf")iferr!=nil{fmt.Printf("Error: %#v\n",err)}}
I get the following error:
panic:runtimeerror:indexoutofrange[65]withlength0goroutine1[running]:github.com/go-pdf/fpdf.(*Fpdf).write(0xc00013c008,0x4024000000000000,{0x573ba3?,0x5a4ee0?},0x0,{0x0,0x0})/home/n/go/pkg/mod/github.com/go-pdf/fpdf@v0.9.0/fpdf.go:3074+0xb9fgithub.com/go-pdf/fpdf.(*Fpdf).Write(...)/home/n/go/pkg/mod/github.com/go-pdf/fpdf@v0.9.0/fpdf.go:3136main.main()/home/n/dev/fontly/main.go:15+0xf8exitstatus2shellreturned1
Which corresponds to: github.com/go-pdf/fpdf@9e977fe339/fpdf.go (L3077)
(the line number is different locally, but it's this one)
Using delve to step through the program, it seems that arriving here, the cw is empty:
(dlv) l
> [Breakpoint 1] github.com/go-pdf/fpdf.(*Fpdf).write() /home/n/go/pkg/mod/github.com/go-pdf/fpdf@v0.9.0/fpdf.go:3074 (hits goroutine(1):1 total:1) (PC: 0x58c1f8)
3069: continue
3070: }
3071: if c == ' ' {
3072: sep = i
3073: }
=>3074: l += float64(cw[int(c)])
3075: if l > wmax {
3076: // Automatic line break
3077: if sep == -1 {
3078: if f.x > f.lMargin {
3079: // Move to next line
(dlv) p c
65
(dlv) p cw
[]int len: 0, cap: 0, nil
cw is just a copy of f.currentFont.Cw, so the current font character width.
github.com/go-pdf/fpdf@9e977fe339/fpdf.go (L3028)
If I instead use pdf.SetFont("Arial", "B", 16) or a random TTF font:
(dlv) p cw
[]int len: 256, cap: 256, [278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,333,474,556,556,889,722,238,333,333,389,584,278,333,278,278,556,556,556,556,556,556,556,556,556,556,333,333,584,584,584,611,...+192 more]
Can you have a look as to why this (and other OTF) font aren't loaded correctly?
Hi 👋
I couldn't make a simple program working with an OTF font.
I used: https://velvetyne.fr/download/?font=karrik
Code sample:
```go
package main
import (
"fmt"
"github.com/go-pdf/fpdf"
)
func main() {
pdf := fpdf.New("P", "mm", "A4", ".")
pdf.AddUTF8Font("f", "B", "./Karrik-Regular.otf")
pdf.AddPage()
pdf.SetFont("f", "B", 20)
pdf.Write(10, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
err := pdf.OutputFileAndClose("hello.pdf")
if err != nil {
fmt.Printf("Error: %#v\n", err)
}
}
```
I get the following error:
```go
panic: runtime error: index out of range [65] with length 0
goroutine 1 [running]:
github.com/go-pdf/fpdf.(*Fpdf).write(0xc00013c008, 0x4024000000000000, {0x573ba3?, 0x5a4ee0?}, 0x0
, {0x0, 0x0})
/home/n/go/pkg/mod/github.com/go-pdf/fpdf@v0.9.0/fpdf.go:3074 +0xb9f
github.com/go-pdf/fpdf.(*Fpdf).Write(...)
/home/n/go/pkg/mod/github.com/go-pdf/fpdf@v0.9.0/fpdf.go:3136
main.main()
/home/n/dev/fontly/main.go:15 +0xf8
exit status 2
shell returned 1
```
Which corresponds to: https://github.com/go-pdf/fpdf/blob/9e977fe3398a7a7fba11b52463f048b9a00e943c/fpdf.go#L3077
(the line number is different locally, but it's this one)
Using [delve](https://github.com/go-delve/delve) to step through the program, it seems that arriving here, the `cw` is empty:
```
(dlv) l
> [Breakpoint 1] github.com/go-pdf/fpdf.(*Fpdf).write() /home/n/go/pkg/mod/github.com/go-pdf/fpdf@v0.9.0/fpdf.go:3074 (hits goroutine(1):1 total:1) (PC: 0x58c1f8)
3069: continue
3070: }
3071: if c == ' ' {
3072: sep = i
3073: }
=>3074: l += float64(cw[int(c)])
3075: if l > wmax {
3076: // Automatic line break
3077: if sep == -1 {
3078: if f.x > f.lMargin {
3079: // Move to next line
(dlv) p c
65
(dlv) p cw
[]int len: 0, cap: 0, nil
```
`cw` is just a copy of `f.currentFont.Cw`, so the current font character width.
https://github.com/go-pdf/fpdf/blob/9e977fe3398a7a7fba11b52463f048b9a00e943c/fpdf.go#L3028
If I instead use `pdf.SetFont("Arial", "B", 16)` or a random TTF font:
```shell
(dlv) p cw
[]int len: 256, cap: 256, [278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,333,474,556,556,889,722,238,333,333,389,584,278,333,278,278,556,556,556,556,556,556,556,556,556,556,333,333,584,584,584,611,...+192 more]
```
Can you have a look as to why this (and other OTF) font aren't loaded correctly?