diff options
author | Kurt <kurt.w.jung@gmail.com> | 2019-09-25 08:16:44 -0400 |
---|---|---|
committer | Kurt <kurt.w.jung@gmail.com> | 2019-09-25 08:16:44 -0400 |
commit | cae7d4739e815a170819d84c5361b05306b2f019 (patch) | |
tree | 94aeb9ae213fdea7f01dc7fcd6c6423865203d1a | |
parent | 07f2797b1523f4a406a919883c018ca52233a8a0 (diff) |
Merge from v2: Escape spaces in font family string to comply with PDF standard
-rw-r--r-- | fpdf.go | 12 | ||||
-rw-r--r-- | util.go | 8 |
2 files changed, 15 insertions, 5 deletions
@@ -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 @@ -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 +} |