3
25
Fork
You've already forked fpdf
10

Fail to render with OTF font #88

Open
opened 2024年10月10日 09:22:21 +02:00 by nobe4 · 2 comments
nobe4 commented 2024年10月10日 09:22:21 +02:00 (Migrated from github.com)
Copy link

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?
nobe4 commented 2024年10月10日 09:42:06 +02:00 (Migrated from github.com)
Copy link

Ah, after looking at the error, it seems that I get a generic message:

pdf.AddUTF8Font("f","B","./Karrik-Regular.otf")ifpdf.Error()!=nil{fmt.Printf("Error: %#v\n",pdf.Error())}

Error:

Error: &errors.errorString{s:"not supported\n "}

🤔

Does that mean that I need first to run makefont before I load an OTF font?

Ah, after looking at the error, it seems that I get a generic message: ```go pdf.AddUTF8Font("f", "B", "./Karrik-Regular.otf") if pdf.Error() != nil { fmt.Printf("Error: %#v\n", pdf.Error()) } ``` Error: ``` Error: &errors.errorString{s:"not supported\n "} ``` 🤔 Does that mean that I need first to run `makefont` before I load an OTF font?
nobe4 commented 2024年10月10日 09:58:08 +02:00 (Migrated from github.com)
Copy link

New try with the cp1252.map found in this repo:

funcmain(){err:=fpdf.MakeFont("./Karrik-Regular.otf","./cp1252.map","./font-out",os.Stdout,false)iferr!=nil{panic(err)}}

result:

panic: fonts based on PostScript outlines are not supported
New try with the [`cp1252.map`](https://github.com/go-pdf/fpdf/blob/main/font/cp1252.map) found in this repo: ```go func main() { err := fpdf.MakeFont("./Karrik-Regular.otf", "./cp1252.map", "./font-out", os.Stdout, false) if err != nil { panic(err) } } ``` result: ``` panic: fonts based on PostScript outlines are not supported ```
Sign in to join this conversation.
No Branch/Tag specified
main
v0.12.0
v0.11.1
v0.10.0
v0.11.0
v0.9.0
v0.8.0
v0.7.0
v0.6.0
v0.5.0
v1.4.3
v0.4.1
v0.4.0
v0.3.1
v0.3.0
v0.2.0
v0.1.0
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
go-pdf/fpdf#88
Reference in a new issue
go-pdf/fpdf
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?