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 { func NewParagraph(text string) *Paragraph {
p := &Paragraph{} p := &Paragraph{}
p.text = text 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.fontSize = 10
p.lineHeight = 1.0 p.lineHeight = 1.0
@ -206,7 +213,7 @@ func (p *Paragraph) getTextWidth() float64 {
metrics, found := p.textFont.GetGlyphCharMetrics(glyph) metrics, found := p.textFont.GetGlyphCharMetrics(glyph)
if !found { 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. return -1 // XXX/FIXME: return error.
} }
w += p.fontSize * metrics.Wx w += p.fontSize * metrics.Wx

View File

@ -107,7 +107,7 @@ func NewStandard14FontWithEncoding(basefont string, alphabet map[rune]int) (*Pdf
} }
slots = append(slots, slots1...) 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{} glyphs := []string{}
for _, r := range sortedAlphabet(alphabet) { for _, r := range sortedAlphabet(alphabet) {
glyph, ok := textencoding.RuneToGlyph(r) glyph, ok := textencoding.RuneToGlyph(r)
@ -116,7 +116,7 @@ func NewStandard14FontWithEncoding(basefont string, alphabet map[rune]int) (*Pdf
continue continue
} }
if _, ok = std.fontMetrics[glyph]; !ok { 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 continue
} }
if len(glyphs) >= 255 { if len(glyphs) >= 255 {

View File

@ -10,6 +10,7 @@
package textencoding package textencoding
import ( import (
"fmt"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -66,9 +67,13 @@ func GlyphToRune(glyph string) (rune, bool) {
} }
// RuneToGlyph is the reverse of the table lookups in GlyphToRune. // 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) { func RuneToGlyph(r rune) (string, bool) {
glyph, ok := glyphlistRuneToGlyphMap[r] 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`. // 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 // ToPdfObject returns `se` as a PdfObject
func (se SimpleEncoder) ToPdfObject() core.PdfObject { func (se SimpleEncoder) ToPdfObject() core.PdfObject {
if se.differences == nil { if se.differences == nil || len(se.differences) == 0 {
return core.MakeName(se.baseName) return core.MakeName(se.baseName)
} }
dict := core.MakeDict() dict := core.MakeDict()