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

[pdf] TTF font displayed as dots on iOS and Adobe Reader (OTF equivalent is fine) #60

Closed
oliverpool opened this issue Aug 15, 2020 · 2 comments

Comments

@oliverpool
Copy link
Contributor

oliverpool commented Aug 15, 2020

I generated the following pdf by running this program:
bug.pdf

package main

import (
	"image/color"
	"os"

	"github.com/tdewolff/canvas"
	"github.com/tdewolff/canvas/pdf"
)

func fontTTF() *canvas.FontFamily {
	fontFamily := canvas.NewFontFamily("SourceSerifPro")
	fontFamily.Use(canvas.CommonLigatures)
	err := fontFamily.LoadFontFile("SourceSerifPro-Regular.ttf", canvas.FontRegular)
	if err != nil {
		panic(err)
	}
	return fontFamily
}

func fontOTF() *canvas.FontFamily {
	fontFamily := canvas.NewFontFamily("SourceSerifPro")
	fontFamily.Use(canvas.CommonLigatures)
	err := fontFamily.LoadFontFile("SourceSerifPro-Regular.otf", canvas.FontRegular)
	if err != nil {
		panic(err)
	}
	return fontFamily
}
func main() {
	f, err := os.Create("bug.pdf")
	if err != nil {
		panic(nil)
	}
	defer f.Close()
	pdf := pdf.New(f, 100, 100)

	////////////////
	face := fontOTF().Face(16, color.Black, canvas.FontRegular, canvas.FontNormal)
	txt := canvas.NewTextLine(face, "OTF font is fine", canvas.Left)
	pdf.RenderText(txt, canvas.Identity.Translate(0, 50))
	////////////////
	face = fontTTF().Face(16, color.Black, canvas.FontRegular, canvas.FontNormal)
	txt = canvas.NewTextLine(face, "Only dots are displayed for TTF", canvas.Left)
	pdf.RenderText(txt, canvas.Identity.Translate(0, 40))
	////////////////

	err = pdf.Close()
	if err != nil {
		panic(nil)
	}
}

(you can get the OTF and TTF font at https://github.com/adobe-fonts/source-serif-pro)

It produces the following pdf:
bug.pdf

It displays fine on all PDF reader that I could test on linux (evince, firefox, chromium), but the second line (the one using the TTF font) displays only dots on Adobe Reader and iOS:
image

Do you have any idea what might be wrong ? (or at least in which direction should I search?)

@tdewolff
Copy link
Owner

tdewolff commented Aug 31, 2020

Someone e-mailed me personally with the same issue, this was my response:

It looks like dots are displayed when the font file cannot be found or the character is not defined in the PDF. This is strange as it loads fine in a variety of applications on my Linux computer. It might be related to the use of UTF-8 characters in the text (the double quotes in this case). To be fair, text embedding in PDFs could be improved in general as there is a bug of copy pasting text from the PDF not representing the written text. The latter might be related to your issue.

Can you output a PDF of https://github.com/tdewolff/canvas/tree/master/examples/document and see what happens? That is, does this happen when there is a UTF-8 character used, or also when just using ASCII. In general, at https://github.com/tdewolff/canvas/blob/master/pdf/renderer.go#L490 I'm wrapping the font program in a couple of layers which seems to be unnecessary and might be part of the problem. For example, output from Inkscape is much more concise.

In particular, this could be fixed with a GID to codepoint table as defined in the PDF specification, but I think it could be simpler by more directly embedding the font program and specifying the right encoding. In the future it would be desireable to create the TTF/OTF font (instead of copying the original font file) so that we can tweak the GID=>codepoint table to match one of PDFs encodings and so we can subset the file and remove unnecessary tables.

@tdewolff
Copy link
Owner

tdewolff commented Feb 15, 2021

This should be fixed in the develop branch, see the roadmap for more information: #74

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

2 participants