diff --git a/pdf/creator/paragraph.go b/pdf/creator/paragraph.go index de2ddf57..311c443d 100644 --- a/pdf/creator/paragraph.go +++ b/pdf/creator/paragraph.go @@ -66,8 +66,15 @@ type Paragraph struct { func NewParagraph(text string) *Paragraph { p := &Paragraph{} p.text = text - p.textFont, _ = model.NewStandard14Font("Helvetica") - p.SetEncoder(textencoding.NewWinAnsiTextEncoder()) + + font, encoder, err := model.NewStandard14FontWithEncoding("Helvetica", model.GetAlphabet(text)) + if err != nil { + common.Log.Debug("ERROR: NewStandard14FontWithEncoding failed err=%v. Falling back.", err) + p.textFont = model.DefaultFont() + } + p.textFont = font + p.SetEncoder(encoder) + p.fontSize = 10 p.lineHeight = 1.0 @@ -206,7 +213,7 @@ func (p *Paragraph) getTextWidth() float64 { metrics, found := p.textFont.GetGlyphCharMetrics(glyph) if !found { - common.Log.Debug("Glyph char metrics not found! %q (rune 0x04x=%c)", glyph, r, r) + common.Log.Debug("ERROR: Glyph char metrics not found! %q (rune 0x%04x=%c)", glyph, r, r) return -1 // XXX/FIXME: return error. } w += p.fontSize * metrics.Wx diff --git a/pdf/model/font.go b/pdf/model/font.go index 53d957bb..802a39ad 100644 --- a/pdf/model/font.go +++ b/pdf/model/font.go @@ -107,7 +107,7 @@ func NewStandard14FontWithEncoding(basefont string, alphabet map[rune]int) (*Pdf } slots = append(slots, slots1...) - // glyphs are the font glyphs that we need to encode + // `glyphs` are the font glyphs that we need to encode. glyphs := []string{} for _, r := range sortedAlphabet(alphabet) { glyph, ok := textencoding.RuneToGlyph(r) @@ -116,7 +116,7 @@ func NewStandard14FontWithEncoding(basefont string, alphabet map[rune]int) (*Pdf continue } if _, ok = std.fontMetrics[glyph]; !ok { - common.Log.Debug("Glyph %q not in font", glyph) + common.Log.Trace("Glyph %q (0x%04x=%c)not in font", glyph, r, r) continue } if len(glyphs) >= 255 { diff --git a/pdf/model/textencoding/glyphs_glyphlist.go b/pdf/model/textencoding/glyphs_glyphlist.go index 8fa796b0..82489eff 100644 --- a/pdf/model/textencoding/glyphs_glyphlist.go +++ b/pdf/model/textencoding/glyphs_glyphlist.go @@ -10,6 +10,7 @@ package textencoding import ( + "fmt" "regexp" "strconv" "strings" @@ -66,9 +67,13 @@ func GlyphToRune(glyph string) (rune, bool) { } // RuneToGlyph is the reverse of the table lookups in GlyphToRune. +// XXX:TODO Remove bool return as function always returns a value. func RuneToGlyph(r rune) (string, bool) { glyph, ok := glyphlistRuneToGlyphMap[r] - return glyph, ok + if !ok { + glyph = fmt.Sprintf("uni%04x", r) + } + return glyph, true } // RuneToString converts rune `r` to a string. It unpacks `ligatures`. diff --git a/pdf/model/textencoding/simple.go b/pdf/model/textencoding/simple.go index 434c7b76..5d05cc91 100644 --- a/pdf/model/textencoding/simple.go +++ b/pdf/model/textencoding/simple.go @@ -170,7 +170,7 @@ func (se SimpleEncoder) GlyphToRune(glyph string) (rune, bool) { // ToPdfObject returns `se` as a PdfObject func (se SimpleEncoder) ToPdfObject() core.PdfObject { - if se.differences == nil { + if se.differences == nil || len(se.differences) == 0 { return core.MakeName(se.baseName) } dict := core.MakeDict()