From ef5029596657de75a312ed095fd29ec7aacee058 Mon Sep 17 00:00:00 2001 From: Kurt Date: Fri, 18 Aug 2017 14:06:29 -0400 Subject: Return 'do nothing' function for Unicode translator rather than nil if document is in error state --- util.go | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'util.go') diff --git a/util.go b/util.go index 2c8c5bb..dbdaa7c 100644 --- a/util.go +++ b/util.go @@ -153,6 +153,11 @@ func strIf(cnd bool, aStr, bStr string) string { return bStr } +// doNothing returns the passed string with no translation. +func doNothing(s string) string { + return s +} + // Dump the internals of the specified values // func dump(fileStr string, a ...interface{}) { // fl, err := os.OpenFile(fileStr, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600) @@ -219,9 +224,7 @@ func UnicodeTranslator(r io.Reader) (f func(string) string, err error) { if err == nil { f = repClosure(m) } else { - f = func(s string) string { - return s - } + f = doNothing } return } @@ -241,9 +244,7 @@ func UnicodeTranslatorFromFile(fileStr string) (f func(string) string, err error f, err = UnicodeTranslator(fl) fl.Close() } else { - f = func(s string) string { - return s - } + f = doNothing } return } @@ -260,21 +261,22 @@ func UnicodeTranslatorFromFile(fileStr string) (f func(string) string, err error // If an error occurs reading the descriptor, the returned function is valid // but does not perform any rune translation. // -// The CellFormat (4) example demonstrates this method. +// The CellFormat_codepage example demonstrates this method. func (f *Fpdf) UnicodeTranslatorFromDescriptor(cpStr string) (rep func(string) string) { var str string var ok bool - if f.err != nil { - return - } - if len(cpStr) == 0 { - cpStr = "cp1252" - } - str, ok = embeddedMapList[cpStr] - if ok { - rep, f.err = UnicodeTranslator(strings.NewReader(str)) + if f.err == nil { + if len(cpStr) == 0 { + cpStr = "cp1252" + } + str, ok = embeddedMapList[cpStr] + if ok { + rep, f.err = UnicodeTranslator(strings.NewReader(str)) + } else { + rep, f.err = UnicodeTranslatorFromFile(filepath.Join(f.fontpath, cpStr) + ".map") + } } else { - rep, f.err = UnicodeTranslatorFromFile(filepath.Join(f.fontpath, cpStr) + ".map") + rep = doNothing } return } -- cgit v1.2.1-24-ge1ad