From 5212485284df2c6ad88f2af101af38e3852c1004 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Fri, 22 Aug 2014 20:02:52 -0400 Subject: Embed core font definition files and the 1252 code page in the application. Only if a non-core font or non-default code page is used does an actual font directory need to be specified when creating an Fpdf instance. --- util.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'util.go') diff --git a/util.go b/util.go index aaca0da..d3b114f 100644 --- a/util.go +++ b/util.go @@ -26,6 +26,7 @@ import ( "math" "os" "path/filepath" + "strings" ) func round(f float64) int { @@ -183,7 +184,8 @@ func repClosure(m map[rune]byte) func(string) string { // map files are packaged with the gfpdf library in the font directory. // // An error occurs only if a line is read that does not conform to the expected -// format. +// format. In this case, the returned function is valid but does not perform +// any rune translation. func UnicodeTranslator(r io.Reader) (f func(string) string, err error) { m := make(map[rune]byte) var uPos, cPos uint32 @@ -191,6 +193,7 @@ func UnicodeTranslator(r io.Reader) (f func(string) string, err error) { sc := bufio.NewScanner(r) for sc.Scan() { lineStr = sc.Text() + lineStr = strings.TrimSpace(lineStr) if len(lineStr) > 0 { _, err = fmt.Sscanf(lineStr, "!%2X U+%4X %s", &cPos, &uPos, &nameStr) if err == nil { @@ -202,6 +205,10 @@ 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 + } } return } @@ -211,12 +218,19 @@ func UnicodeTranslator(r io.Reader) (f func(string) string, err error) { // specified code page. See UnicodeTranslator for more details. // // fileStr identifies a font descriptor file that maps glyph positions to names. +// +// If an error occurs reading the file, the returned function is valid but does +// not perform any rune translation. func UnicodeTranslatorFromFile(fileStr string) (f func(string) string, err error) { var fl *os.File fl, err = os.Open(fileStr) if err == nil { f, err = UnicodeTranslator(fl) fl.Close() + } else { + f = func(s string) string { + return s + } } return } @@ -230,14 +244,24 @@ func UnicodeTranslatorFromFile(fileStr string) (f func(string) string, err error // plus the extension ".map". If cpStr is empty, it will be replaced with // "cp1252", the gofpdf code page default. // +// If an error occurs reading the descriptor, the returned function is valid +// but does not perform any rune translation. +// // See tutorial 23 for an example of this function. 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" } - rep, f.err = UnicodeTranslatorFromFile(filepath.Join(f.fontpath, cpStr) + ".map") + str, ok = embeddedMapList[cpStr] + if ok { + rep, f.err = UnicodeTranslator(strings.NewReader(str)) + } else { + rep, f.err = UnicodeTranslatorFromFile(filepath.Join(f.fontpath, cpStr) + ".map") + } return } -- cgit v1.2.1-24-ge1ad