Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Fail to render with OTF font #88

@nobe4

Description

@nobe4

Hi 👋

I couldn't make a simple program working with an OTF font.

I used: https://velvetyne.fr/download/?font=karrik

Code sample:

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:

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:

fpdf/fpdf.go

Line 3077 in 9e977fe

l += float64(cw[int(c)])

(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.

fpdf/fpdf.go

Line 3028 in 9e977fe

cw := f.currentFont.Cw

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions