Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions model/font_composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"sort"

"github.com/unidoc/unipdf/v3/common"
Expand Down Expand Up @@ -569,16 +571,33 @@ func parseCIDFontWidthsArray(w core.PdfObject) (map[textencoding.CharCode]float6

// NewCompositePdfFontFromTTFFile loads a composite font from a TTF font file. Composite fonts can
// be used to represent unicode fonts which can have multi-byte character codes, representing a wide
// range of values.
// range of values. They are often used for symbolic languages, including Chinese, Japanese and Korean.
// It is represented by a Type0 Font with an underlying CIDFontType2 and an Identity-H encoding map.
// TODO: May be extended in the future to support a larger variety of CMaps and vertical fonts.
// NOTE: For simple fonts, use NewPdfFontFromTTFFile.
func NewCompositePdfFontFromTTFFile(filePath string) (*PdfFont, error) {
// Load the truetype font data.
ttfBytes, err := ioutil.ReadFile(filePath)
f, err := os.Open(filePath)
if err != nil {
common.Log.Debug("ERROR: while reading ttf font: %v", err)
common.Log.Debug("ERROR: opening file: %v", err)
return nil, err
}
defer f.Close()
return NewCompositePdfFontFromTTF(f)
}

// NewCompositePdfFontFromTTF loads a composite TTF font. Composite fonts can
// be used to represent unicode fonts which can have multi-byte character codes, representing a wide
// range of values. They are often used for symbolic languages, including Chinese, Japanese and Korean.
// It is represented by a Type0 Font with an underlying CIDFontType2 and an Identity-H encoding map.
// TODO: May be extended in the future to support a larger variety of CMaps and vertical fonts.
// NOTE: For simple fonts, use NewPdfFontFromTTF.
func NewCompositePdfFontFromTTF(r io.ReadSeeker) (*PdfFont, error) {
ttfBytes, err := ioutil.ReadAll(r)
if err != nil {
common.Log.Debug("ERROR: Unable to read font contents: %v", err)
return nil, err
}

ttf, err := fonts.TtfParse(bytes.NewReader(ttfBytes))
if err != nil {
common.Log.Debug("ERROR: while loading ttf font: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions model/font_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ func (font *pdfFontSimple) ToPdfObject() core.PdfObject {
// NewPdfFontFromTTFFile loads a TTF font file and returns a PdfFont type
// that can be used in text styling functions.
// Uses a WinAnsiTextEncoder and loads only character codes 32-255.
// NOTE: For composite fonts such as used in symbolic languages, use NewCompositePdfFontFromTTFFile.
func NewPdfFontFromTTFFile(filePath string) (*PdfFont, error) {
f, err := os.Open(filePath)
if err != nil {
Expand All @@ -399,6 +400,7 @@ func NewPdfFontFromTTFFile(filePath string) (*PdfFont, error) {
// NewPdfFontFromTTF loads a TTF font and returns a PdfFont type that can be
// used in text styling functions.
// Uses a WinAnsiTextEncoder and loads only character codes 32-255.
// NOTE: For composite fonts such as used in symbolic languages, use NewCompositePdfFontFromTTF.
func NewPdfFontFromTTF(r io.ReadSeeker) (*PdfFont, error) {
const minCode = textencoding.CharCode(32)
const maxCode = textencoding.CharCode(255)
Expand Down