github.com/unidoc/unidoc@v2.2.0+incompatible/pdf/contentstream/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 contentstream
     7  
     8  import (
     9  	"errors"
    10  
    11  	"github.com/unidoc/unidoc/common"
    12  	"github.com/unidoc/unidoc/pdf/core"
    13  	"github.com/unidoc/unidoc/pdf/model"
    14  )
    15  
    16  func makeParamsFromFloats(vals []float64) []core.PdfObject {
    17  	params := []core.PdfObject{}
    18  	for _, val := range vals {
    19  		params = append(params, core.MakeFloat(val))
    20  	}
    21  	return params
    22  }
    23  
    24  func makeParamsFromNames(vals []core.PdfObjectName) []core.PdfObject {
    25  	params := []core.PdfObject{}
    26  	for _, val := range vals {
    27  		params = append(params, core.MakeName(string(val)))
    28  	}
    29  	return params
    30  }
    31  
    32  func makeParamsFromStrings(vals []core.PdfObjectString) []core.PdfObject {
    33  	params := []core.PdfObject{}
    34  	for _, val := range vals {
    35  		params = append(params, core.MakeString(string(val)))
    36  	}
    37  	return params
    38  }
    39  
    40  func makeParamsFromInts(vals []int64) []core.PdfObject {
    41  	params := []core.PdfObject{}
    42  	for _, val := range vals {
    43  		params = append(params, core.MakeInteger(val))
    44  	}
    45  	return params
    46  }
    47  
    48  func newIndexedColorspaceFromPdfObject(obj core.PdfObject) (model.PdfColorspace, error) {
    49  	arr, ok := obj.(*core.PdfObjectArray)
    50  	if !ok {
    51  		common.Log.Debug("Error: Invalid indexed cs not in array (%#v)", obj)
    52  		return nil, errors.New("Type check error")
    53  	}
    54  
    55  	if len(*arr) != 4 {
    56  		common.Log.Debug("Error: Invalid cs array, length != 4 (%d)", len(*arr))
    57  		return nil, errors.New("Range check error")
    58  	}
    59  
    60  	// Format is [/I base 255 bytes], where base = /G,/RGB,/CMYK
    61  	name, ok := (*arr)[0].(*core.PdfObjectName)
    62  	if !ok {
    63  		common.Log.Debug("Error: Invalid cs array first element not a name (array: %#v)", *arr)
    64  		return nil, errors.New("Type check error")
    65  	}
    66  	if *name != "I" && *name != "Indexed" {
    67  		common.Log.Debug("Error: Invalid cs array first element != I (got: %v)", *name)
    68  		return nil, errors.New("Range check error")
    69  	}
    70  
    71  	// Check base
    72  	name, ok = (*arr)[1].(*core.PdfObjectName)
    73  	if !ok {
    74  		common.Log.Debug("Error: Invalid cs array 2nd element not a name (array: %#v)", *arr)
    75  		return nil, errors.New("Type check error")
    76  	}
    77  	if *name != "G" && *name != "RGB" && *name != "CMYK" && *name != "DeviceGray" && *name != "DeviceRGB" && *name != "DeviceCMYK" {
    78  		common.Log.Debug("Error: Invalid cs array 2nd element != G/RGB/CMYK (got: %v)", *name)
    79  		return nil, errors.New("Range check error")
    80  	}
    81  	basename := ""
    82  	switch *name {
    83  	case "G", "DeviceGray":
    84  		basename = "DeviceGray"
    85  	case "RGB", "DeviceRGB":
    86  		basename = "DeviceRGB"
    87  	case "CMYK", "DeviceCMYK":
    88  		basename = "DeviceCMYK"
    89  	}
    90  
    91  	// Prepare to a format that can be loaded by model's newPdfColorspaceFromPdfObject.
    92  	csArr := core.MakeArray(core.MakeName("Indexed"), core.MakeName(basename), (*arr)[2], (*arr)[3])
    93  
    94  	return model.NewPdfColorspaceFromPdfObject(csArr)
    95  }