Changes to get creator working better.

- textencoding.RuneToGlyph always returns a value
- Encode empty /Difference correctly
- NewParagraph sets a custom encoding that matches its text
This commit is contained in:
Peter Williams 2018-08-22 14:44:16 +10:00
parent cc4f64fa98
commit e5ec5406f6
4 changed files with 19 additions and 7 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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`.

View File

@ -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()