import PDFBool from 'src/core/objects/PDFBool' ; import PDFDict from 'src/core/objects/PDFDict' ; import PDFHexString from 'src/core/objects/PDFHexString' ; import PDFName from 'src/core/objects/PDFName' ; import PDFNull from 'src/core/objects/PDFNull' ; import PDFNumber from 'src/core/objects/PDFNumber' ; import PDFObject from 'src/core/objects/PDFObject' ; import PDFRef from 'src/core/objects/PDFRef' ; import PDFStream from 'src/core/objects/PDFStream' ; import PDFString from 'src/core/objects/PDFString' ; import PDFContext from 'src/core/PDFContext' ; import CharCodes from 'src/core/syntax/CharCodes' ; import { PDFArrayIsNotRectangleError } from 'src/core/errors' ; import PDFRawStream from 'src/core/objects/PDFRawStream' ; class PDFArray extends PDFObject { static withContext = ( context ) => new PDFArray ( context ) ; private readonly array : PDFObject [ ] ; private readonly context : PDFContext ; private constructor ( context ) { super ( ) ; this . array = [ ] ; this . context = context ; } size ( ) { return this . array . length ; } push ( object ) { this . array . push ( object ) ; } insert ( index , object ) { this . array . splice ( index , 0 , object ) ; } indexOf ( object ) { const index = this . array . indexOf ( object ) ; return index === - 1 ? undefined : index ; } remove ( index ) { this . array . splice ( index , 1 ) ; } set ( idx , object ) { this . array [ idx ] = object ; } get ( index ) { return this . array [ index ] ; } lookupMaybe ( index , type ) ; lookupMaybe ( index , type ) ; lookupMaybe ( index , type ) ; lookupMaybe ( index , type ) ; lookupMaybe ( index , type ) ; lookupMaybe ( index , type ) ; lookupMaybe ( index , type ) ; lookupMaybe ( index , type ) ; lookupMaybe ( index , type ) ; lookupMaybe ( index , type ) ; lookupMaybe ( index , type ) ; lookupMaybe ( index , type1 , type2 ) ; lookupMaybe ( index , ... types ) { return this . context . lookupMaybe ( this . get ( index ) , ... types ) as any ; } lookup ( index ) ; lookup ( index , type ) ; lookup ( index , type ) ; lookup ( index , type ) ; lookup ( index , type ) ; lookup ( index , type ) ; lookup ( index , type ) ; lookup ( index , type ) ; lookup ( index , type ) ; lookup ( index , type ) ; lookup ( index , type ) ; lookup ( index , type ) ; lookup ( index , type1 , type2 ) ; lookup ( index , ... types ) { return this . context . lookup ( this . get ( index ) , ... types ) as any ; } asRectangle ( ) { if ( this . size ( ) !== 4 ) throw new PDFArrayIsNotRectangleError ( this . size ( ) ) ; const lowerLeftX = this . lookup ( 0 , PDFNumber ) . asNumber ( ) ; const lowerLeftY = this . lookup ( 1 , PDFNumber ) . asNumber ( ) ; const upperRightX = this . lookup ( 2 , PDFNumber ) . asNumber ( ) ; const upperRightY = this . lookup ( 3 , PDFNumber ) . asNumber ( ) ; const x = lowerLeftX ; const y = lowerLeftY ; const width = upperRightX - lowerLeftX ; const height = upperRightY - lowerLeftY ; return { x , y , width , height } ; } asArray ( ) { return this . array . slice ( ) ; } clone ( context ? ) { const clone = PDFArray . withContext ( context || this . context ) ; for ( let idx = 0 , len = this . size ( ) ; idx < len ; idx ++ ) { clone . push ( this . array [ idx ] ) ; } return clone ; } toString ( ) { let arrayString = '[ ' ; for ( let idx = 0 , len = this . size ( ) ; idx < len ; idx ++ ) { arrayString += this . get ( idx ) . toString ( ) ; arrayString += ' ' ; } arrayString += ']' ; return arrayString ; } sizeInBytes ( ) { let size = 3 ; for ( let idx = 0 , len = this . size ( ) ; idx < len ; idx ++ ) { size += this . get ( idx ) . sizeInBytes ( ) + 1 ; } return size ; } copyBytesInto ( buffer , offset ) { const initialOffset = offset ; buffer [ offset ++ ] = CharCodes . LeftSquareBracket ; buffer [ offset ++ ] = CharCodes . Space ; for ( let idx = 0 , len = this . size ( ) ; idx < len ; idx ++ ) { offset += this . get ( idx ) . copyBytesInto ( buffer , offset ) ; buffer [ offset ++ ] = CharCodes . Space ; } buffer [ offset ++ ] = CharCodes . RightSquareBracket ; return offset - initialOffset ; } scalePDFNumbers ( x , y ) { for ( let idx = 0 , len = this . size ( ) ; idx < len ; idx ++ ) { const el = this . lookup ( idx ) ; if ( el instanceof PDFNumber ) { const factor = idx % 2 === 0 ? x : y ; this . set ( idx , PDFNumber . of ( el . asNumber ( ) * factor ) ) ; } } } } export default PDFArray ;