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

     1  package gopdf
     2  
     3  import "bytes"
     4  
     5  type listCacheContent struct {
     6  	caches []iCacheContent
     7  }
     8  
     9  func (l *listCacheContent) last() iCacheContent {
    10  	max := len(l.caches)
    11  	if max > 0 {
    12  		return l.caches[max-1]
    13  	}
    14  	return nil
    15  }
    16  
    17  func (l *listCacheContent) append(cache iCacheContent) {
    18  	l.caches = append(l.caches, cache)
    19  }
    20  
    21  func (l *listCacheContent) appendContentText(cache cacheContentText, text string) (float64, float64, error) {
    22  
    23  	x := cache.x
    24  	y := cache.y
    25  
    26  	mustMakeNewCache := true
    27  	var cacheFont *cacheContentText
    28  	var ok bool
    29  	last := l.last()
    30  	if cacheFont, ok = last.(*cacheContentText); ok {
    31  		if cacheFont != nil {
    32  			if cacheFont.isSame(cache) {
    33  				mustMakeNewCache = false
    34  			}
    35  		}
    36  	}
    37  
    38  	if mustMakeNewCache { //make new cell
    39  		l.caches = append(l.caches, &cache)
    40  		cacheFont = &cache
    41  	}
    42  
    43  	//start add text
    44  	_, err := cacheFont.text.WriteString(text)
    45  	if err != nil {
    46  		return x, y, err
    47  	}
    48  
    49  	//re-create contnet
    50  	textWidthPdfUnit, textHeightPdfUnit, err := cacheFont.createContent()
    51  	if err != nil {
    52  		return x, y, err
    53  	}
    54  
    55  	if cacheFont.cellOpt.Float == 0 || cacheFont.cellOpt.Float&Right == Right || cacheFont.contentType == ContentTypeText {
    56  		x += textWidthPdfUnit
    57  	}
    58  	if cacheFont.cellOpt.Float&Bottom == Bottom {
    59  		y += textHeightPdfUnit
    60  	}
    61  
    62  	return x, y, nil
    63  }
    64  
    65  func (l *listCacheContent) toStream(protection *PDFProtection) (*bytes.Buffer, error) {
    66  	var buff bytes.Buffer
    67  	for _, cache := range l.caches {
    68  		stream, err := cache.toStream(protection)
    69  		if err != nil {
    70  			return nil, err
    71  		}
    72  		_, err = stream.WriteTo(&buff)
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  	}
    77  	return &buff, nil
    78  }
    79  
    80  /*
    81  func (l *listCacheContent) debug() string {
    82  	var buff bytes.Buffer
    83  	for _, cache := range l.caches {
    84  		buff.WriteString(cache.text.String())
    85  		buff.WriteString("\n")
    86  	}
    87  	return buff.String()
    88  }*/