github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/model/utils.go (about)

     1  /*
     2   * This file is subject to the terms and conditions defined in
     3   * file 'LICENSE.md', which is part of this source code package.
     4   */
     5  
     6  package model
     7  
     8  import (
     9  	"errors"
    10  
    11  	"github.com/unidoc/unidoc/common"
    12  	. "github.com/unidoc/unidoc/pdf/core"
    13  )
    14  
    15  func getUniDocVersion() string {
    16  	return common.Version
    17  }
    18  
    19  func getNumberAsFloat(obj PdfObject) (float64, error) {
    20  	if fObj, ok := obj.(*PdfObjectFloat); ok {
    21  		return float64(*fObj), nil
    22  	}
    23  
    24  	if iObj, ok := obj.(*PdfObjectInteger); ok {
    25  		return float64(*iObj), nil
    26  	}
    27  
    28  	return 0, errors.New("Not a number")
    29  }
    30  
    31  func isNullObject(obj PdfObject) bool {
    32  	if _, isNull := obj.(*PdfObjectNull); isNull {
    33  		return true
    34  	} else {
    35  		return false
    36  	}
    37  }
    38  
    39  // Convert a list of pdf objects representing floats or integers to a slice of float64 values.
    40  func getNumbersAsFloat(objects []PdfObject) ([]float64, error) {
    41  	floats := []float64{}
    42  	for _, obj := range objects {
    43  		val, err := getNumberAsFloat(obj)
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  		floats = append(floats, val)
    48  
    49  	}
    50  	return floats, nil
    51  }
    52  
    53  // Cases where expecting an integer, but some implementations actually
    54  // store the number in a floating point format.
    55  func getNumberAsInt64(obj PdfObject) (int64, error) {
    56  	if iObj, ok := obj.(*PdfObjectInteger); ok {
    57  		return int64(*iObj), nil
    58  	}
    59  
    60  	if fObj, ok := obj.(*PdfObjectFloat); ok {
    61  		common.Log.Debug("Number expected as integer was stored as float (type casting used)")
    62  		return int64(*fObj), nil
    63  	}
    64  
    65  	return 0, errors.New("Not a number")
    66  }
    67  
    68  func getNumberAsFloatOrNull(obj PdfObject) (*float64, error) {
    69  	if fObj, ok := obj.(*PdfObjectFloat); ok {
    70  		num := float64(*fObj)
    71  		return &num, nil
    72  	}
    73  
    74  	if iObj, ok := obj.(*PdfObjectInteger); ok {
    75  		num := float64(*iObj)
    76  		return &num, nil
    77  	}
    78  	if _, ok := obj.(*PdfObjectNull); ok {
    79  		return nil, nil
    80  	}
    81  
    82  	return nil, errors.New("Not a number")
    83  }
    84  
    85  // Handy function for debugging in development.
    86  func debugObject(obj PdfObject) {
    87  	common.Log.Debug("obj: %T %s", obj, obj.String())
    88  
    89  	if stream, is := obj.(*PdfObjectStream); is {
    90  		decoded, err := DecodeStream(stream)
    91  		if err != nil {
    92  			common.Log.Debug("Error: %v", err)
    93  			return
    94  		}
    95  		common.Log.Debug("Decoded: %s", decoded)
    96  	} else if indObj, is := obj.(*PdfIndirectObject); is {
    97  		common.Log.Debug("%T %v", indObj.PdfObject, indObj.PdfObject)
    98  		common.Log.Debug("%s", indObj.PdfObject.String())
    99  	}
   100  }