github.com/signintech/pdft@v0.5.0/pdf_font_data.go (about)

     1  package pdft
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  
     7  	gopdf "github.com/signintech/pdft/minigopdf"
     8  )
     9  
    10  // PDFFontData font data
    11  type PDFFontData struct {
    12  	fontname  string
    13  	fontindex int
    14  	//gopdf obj
    15  	font       gopdf.SubsetFontObj
    16  	cid        gopdf.CIDFontObj
    17  	unicodeMap gopdf.UnicodeMap
    18  	fontDesc   gopdf.SubfontDescriptorObj
    19  	dictionary gopdf.PdfDictionaryObj
    20  	//IDs
    21  	startID                                               int
    22  	fontID, cidID, unicodeMapID, fontDescID, dictionaryID int
    23  	//stream
    24  	fontStream       *bytes.Buffer
    25  	cidStream        *bytes.Buffer
    26  	unicodeMapStream *bytes.Buffer
    27  	fontDescStream   *bytes.Buffer
    28  	dictionaryStream *bytes.Buffer
    29  }
    30  
    31  func (p *PDFFontData) init() {
    32  	p.font.CharacterToGlyphIndex = gopdf.NewMapOfCharacterToGlyphIndex() //make(map[rune]uint)
    33  }
    34  
    35  func (p *PDFFontData) setFontName(name string) {
    36  	p.font.SetFamily(name)
    37  	p.fontname = name
    38  }
    39  
    40  func (p *PDFFontData) setFontIndex(fontindex int) {
    41  	p.fontindex = fontindex
    42  }
    43  
    44  func (p *PDFFontData) fontIndex() int {
    45  	return p.fontindex
    46  }
    47  
    48  func (p *PDFFontData) fontName() string {
    49  	return p.fontname
    50  }
    51  
    52  func (p *PDFFontData) setTTFPath(path string) error {
    53  	return p.font.SetTTFByPath(path)
    54  }
    55  
    56  func (p *PDFFontData) setTTFReader(reader io.Reader) error {
    57  	return p.font.SetTTFByReader(reader)
    58  }
    59  
    60  func (p *PDFFontData) addChars(text string) error {
    61  	return p.font.AddChars(text)
    62  }
    63  
    64  func (p *PDFFontData) build() (int, error) {
    65  
    66  	p.fontID = p.startID + 1
    67  	p.cidID = p.startID + 2
    68  	p.unicodeMapID = p.startID + 3
    69  	p.fontDescID = p.startID + 4
    70  	p.dictionaryID = p.startID + 5
    71  	newMaxID := p.dictionaryID
    72  
    73  	//font
    74  	p.font.SetIndexObjCIDFont(p.cidID - 1)
    75  	p.font.SetIndexObjUnicodeMap(p.unicodeMapID - 1)
    76  
    77  	//cid
    78  	p.cid.SetPtrToSubsetFontObj(&p.font)
    79  	p.cid.SetIndexObjSubfontDescriptor(p.fontDescID - 1)
    80  
    81  	//unicode
    82  	p.unicodeMap.SetPtrToSubsetFontObj(&p.font)
    83  
    84  	//font descriptor
    85  	p.fontDesc.SetPtrToSubsetFontObj(&p.font)
    86  	p.fontDesc.SetIndexObjPdfDictionary(p.dictionaryID - 1)
    87  
    88  	//dictionary
    89  	p.dictionary.SetPtrToSubsetFontObj(&p.font)
    90  
    91  	var err error
    92  	err = p.font.Build(p.fontID)
    93  	if err != nil {
    94  		return 0, err
    95  	}
    96  
    97  	err = p.cid.Build(p.cidID)
    98  	if err != nil {
    99  		return 0, err
   100  	}
   101  
   102  	err = p.unicodeMap.Build(p.unicodeMapID)
   103  	if err != nil {
   104  		return 0, err
   105  	}
   106  
   107  	err = p.fontDesc.Build(p.fontDescID)
   108  	if err != nil {
   109  		return 0, err
   110  	}
   111  
   112  	err = p.dictionary.Build(p.dictionaryID)
   113  	if err != nil {
   114  		return 0, err
   115  	}
   116  
   117  	p.fontStream = p.font.GetObjBuff()
   118  	p.cidStream = p.cid.GetObjBuff()
   119  	p.unicodeMapStream = p.unicodeMap.GetObjBuff()
   120  	p.fontDescStream = p.fontDesc.GetObjBuff()
   121  	p.dictionaryStream = p.dictionary.GetObjBuff()
   122  
   123  	return newMaxID, nil
   124  }
   125  
   126  func (p *PDFFontData) setStartID(id int) {
   127  	p.startID = id
   128  }