From cae7d4739e815a170819d84c5361b05306b2f019 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 25 Sep 2019 08:16:44 -0400 Subject: Merge from v2: Escape spaces in font family string to comply with PDF standard --- fpdf.go | 12 +++++++----- util.go | 8 ++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/fpdf.go b/fpdf.go index 7be0117..b6861b6 100644 --- a/fpdf.go +++ b/fpdf.go @@ -1629,7 +1629,7 @@ func (f *Fpdf) ClipEnd() { // definition file to be added. The file will be loaded from the font directory // specified in the call to New() or SetFontLocation(). func (f *Fpdf) AddFont(familyStr, styleStr, fileStr string) { - f.addFont(familyStr, styleStr, fileStr, false) + f.addFont(fontFamilyEscape(familyStr), styleStr, fileStr, false) } // AddUTF8Font imports a TrueType font with utf-8 symbols and makes it available. @@ -1653,7 +1653,7 @@ func (f *Fpdf) AddFont(familyStr, styleStr, fileStr string) { // definition file to be added. The file will be loaded from the font directory // specified in the call to New() or SetFontLocation(). func (f *Fpdf) AddUTF8Font(familyStr, styleStr, fileStr string) { - f.addFont(familyStr, styleStr, fileStr, true) + f.addFont(fontFamilyEscape(familyStr), styleStr, fileStr, true) } func (f *Fpdf) addFont(familyStr, styleStr, fileStr string, isUTF8 bool) { @@ -1779,7 +1779,7 @@ func makeSubsetRange(end int) map[int]int { // // zFileBytes contain all bytes of Z file. func (f *Fpdf) AddFontFromBytes(familyStr, styleStr string, jsonFileBytes, zFileBytes []byte) { - f.addFontFromBytes(familyStr, styleStr, jsonFileBytes, zFileBytes, nil) + f.addFontFromBytes(fontFamilyEscape(familyStr), styleStr, jsonFileBytes, zFileBytes, nil) } // AddUTF8FontFromBytes imports a TrueType font with utf-8 symbols from static @@ -1798,7 +1798,7 @@ func (f *Fpdf) AddFontFromBytes(familyStr, styleStr string, jsonFileBytes, zFile // // zFileBytes contain all bytes of Z file. func (f *Fpdf) AddUTF8FontFromBytes(familyStr, styleStr string, utf8Bytes []byte) { - f.addFontFromBytes(familyStr, styleStr, nil, nil, utf8Bytes) + f.addFontFromBytes(fontFamilyEscape(familyStr), styleStr, nil, nil, utf8Bytes) } func (f *Fpdf) addFontFromBytes(familyStr, styleStr string, jsonFileBytes, zFileBytes, utf8Bytes []byte) { @@ -1937,6 +1937,7 @@ func (f *Fpdf) AddFontFromReader(familyStr, styleStr string, r io.Reader) { return } // dbg("Adding family [%s], style [%s]", familyStr, styleStr) + familyStr = fontFamilyEscape(familyStr) var ok bool fontkey := getFontKey(familyStr, styleStr) _, ok = f.fonts[fontkey] @@ -1985,7 +1986,7 @@ func (f *Fpdf) GetFontDesc(familyStr, styleStr string) FontDescType { if familyStr == "" { return f.currentFont.Desc } - return f.fonts[getFontKey(familyStr, styleStr)].Desc + return f.fonts[getFontKey(fontFamilyEscape(familyStr), styleStr)].Desc } // SetFont sets the font used to print character strings. It is mandatory to @@ -2022,6 +2023,7 @@ func (f *Fpdf) SetFont(familyStr, styleStr string, size float64) { return } // dbg("SetFont") + familyStr = fontFamilyEscape(familyStr) var ok bool if familyStr == "" { familyStr = f.fontFamily diff --git a/util.go b/util.go index 99a1ba5..22e5f36 100644 --- a/util.go +++ b/util.go @@ -452,3 +452,11 @@ func isChinese(rune2 rune) bool { } return false } + +// Condition font family string to PDF name compliance. See section 5.3 (Names) +// in https://resources.infosecinstitute.com/pdf-file-format-basic-structure/ +func fontFamilyEscape(familyStr string) (escStr string) { + escStr = strings.ReplaceAll(familyStr, " ", "#20") + // Additional replacements can take place here + return +} -- cgit v1.2.1-24-ge1ad