diff options
author | Kurt <kurt.w.jung@gmail.com> | 2016-07-30 11:06:04 -0400 |
---|---|---|
committer | Kurt <kurt.w.jung@gmail.com> | 2016-07-30 11:06:04 -0400 |
commit | 9d37149a8b21e4566cca0c844d25a80769dbd302 (patch) | |
tree | ce1128a9020335884c2700eea5d502e69f47bf77 /fpdf.go | |
parent | 11f22e6f7d4f331772e7999a14bb7ade23028f19 (diff) | |
parent | a7290bf6598b103a0fcdb5ce25182e83c00e881b (diff) |
Merge branch 'prsolucoes-master'
Diffstat (limited to 'fpdf.go')
-rw-r--r-- | fpdf.go | 99 |
1 files changed, 95 insertions, 4 deletions
@@ -1405,6 +1405,88 @@ func (f *Fpdf) AddFont(familyStr, styleStr, fileStr string) { f.AddFontFromReader(familyStr, styleStr, file) } +// AddFont imports a TrueType, OpenType or Type1 font and makes it available +// for use in the generated document. +// +// family specifies the font family. The name can be chosen arbitrarily. If it +// is a standard family name, it will override the corresponding font. This +// string is used to subsequently set the font with the SetFont method. +// +// style specifies the font style. Acceptable values are (case insensitive) the +// empty string for regular style, "B" for bold, "I" for italic, or "BI" or +// "IB" for bold and italic combined. +// +// jsonFileBytes contain all bytes of JSON file. + +// zFileBytes contain all bytes of Z file. +func (f *Fpdf) AddFontFromBytes(familyStr string, styleStr string, jsonFileBytes []byte, zFileBytes []byte) { + if f.err != nil { + return + } + + // load font key + var ok bool + fontkey := getFontKey(familyStr, styleStr) + _, ok = f.fonts[fontkey] + + if ok { + return + } + + // load font definitions + var info fontDefType + err := json.Unmarshal(jsonFileBytes, &info) + + if err != nil { + f.err = err + } + + if f.err != nil { + return + } + + // search existing encodings + info.I = len(f.fonts) + + if len(info.Diff) > 0 { + n := -1 + + for j, str := range f.diffs { + if str == info.Diff { + n = j + 1 + break + } + } + + if n < 0 { + f.diffs = append(f.diffs, info.Diff) + n = len(f.diffs) + } + + info.DiffN = n + } + + // embed font + if len(info.File) > 0 { + if info.Tp == "TrueType" { + f.fontFiles[info.File] = fontFileType{ + length1: int64(info.OriginalSize), + embedded: true, + content: zFileBytes, + } + } else { + f.fontFiles[info.File] = fontFileType{ + length1: int64(info.Size1), + length2: int64(info.Size2), + embedded: true, + content: zFileBytes, + } + } + } + + f.fonts[fontkey] = info +} + // getFontKey is used by AddFontFromReader and GetFontDesc func getFontKey(familyStr, styleStr string) string { familyStr = strings.ToLower(familyStr) @@ -3237,11 +3319,20 @@ func (f *Fpdf) putfonts() { f.newobj() info.n = f.n f.fontFiles[file] = info - font, err := f.loadFontFile(file) - if err != nil { - f.err = err - return + + var font []byte + + if info.embedded { + font = info.content + } else { + var err error + font, err = f.loadFontFile(file) + if err != nil { + f.err = err + return + } } + // dbg("font file [%s], ext [%s]", file, file[len(file)-2:]) compressed := file[len(file)-2:] == ".z" if !compressed && info.length2 > 0 { |