mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
Add NewCompositePdfFontFromTTF to load composite TTF from memory
This commit is contained in:
parent
11f3a6e7a2
commit
d23d4b8c79
@ -9,6 +9,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
@ -569,17 +570,27 @@ func parseCIDFontWidthsArray(w core.PdfObject) (map[textencoding.CharCode]float6
|
|||||||
|
|
||||||
// NewCompositePdfFontFromTTFFile loads a composite font from a TTF font file. Composite fonts can
|
// 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
|
// 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.
|
// 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.
|
// 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) {
|
func NewCompositePdfFontFromTTFFile(filePath string) (*PdfFont, error) {
|
||||||
// Load the truetype font data.
|
|
||||||
ttfBytes, err := ioutil.ReadFile(filePath)
|
ttfBytes, err := ioutil.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.Log.Debug("ERROR: while reading ttf font: %v", err)
|
common.Log.Debug("ERROR: while reading ttf font: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ttf, err := fonts.TtfParse(bytes.NewReader(ttfBytes))
|
return NewCompositePdfFontFromTTF(bytes.NewReader(ttfBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(rs io.ReadSeeker) (*PdfFont, error) {
|
||||||
|
ttf, err := fonts.TtfParse(rs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.Log.Debug("ERROR: while loading ttf font: %v", err)
|
common.Log.Debug("ERROR: while loading ttf font: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -385,6 +385,7 @@ func (font *pdfFontSimple) ToPdfObject() core.PdfObject {
|
|||||||
// NewPdfFontFromTTFFile loads a TTF font file and returns a PdfFont type
|
// NewPdfFontFromTTFFile loads a TTF font file and returns a PdfFont type
|
||||||
// that can be used in text styling functions.
|
// that can be used in text styling functions.
|
||||||
// Uses a WinAnsiTextEncoder and loads only character codes 32-255.
|
// 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) {
|
func NewPdfFontFromTTFFile(filePath string) (*PdfFont, error) {
|
||||||
f, err := os.Open(filePath)
|
f, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -399,6 +400,7 @@ func NewPdfFontFromTTFFile(filePath string) (*PdfFont, error) {
|
|||||||
// NewPdfFontFromTTF loads a TTF font and returns a PdfFont type that can be
|
// NewPdfFontFromTTF loads a TTF font and returns a PdfFont type that can be
|
||||||
// used in text styling functions.
|
// used in text styling functions.
|
||||||
// Uses a WinAnsiTextEncoder and loads only character codes 32-255.
|
// 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) {
|
func NewPdfFontFromTTF(r io.ReadSeeker) (*PdfFont, error) {
|
||||||
const minCode = textencoding.CharCode(32)
|
const minCode = textencoding.CharCode(32)
|
||||||
const maxCode = textencoding.CharCode(255)
|
const maxCode = textencoding.CharCode(255)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user