import PDFAcroTerminal from 'src/core/acroform/PDFAcroTerminal' ; import PDFHexString from 'src/core/objects/PDFHexString' ; import PDFString from 'src/core/objects/PDFString' ; import PDFArray from 'src/core/objects/PDFArray' ; import PDFName from 'src/core/objects/PDFName' ; import { AcroChoiceFlags } from 'src/core/acroform/flags' ; import { InvalidAcroFieldValueError , MultiSelectValueError , } from 'src/core/errors' ; class PDFAcroChoice extends PDFAcroTerminal { setValues ( values ) { if ( this . hasFlag ( AcroChoiceFlags . Combo ) && ! this . hasFlag ( AcroChoiceFlags . Edit ) && ! this . valuesAreValid ( values ) ) { throw new InvalidAcroFieldValueError ( ) ; } if ( values . length === 0 ) { this . dict . delete ( PDFName . of ( 'V' ) ) ; } if ( values . length === 1 ) { this . dict . set ( PDFName . of ( 'V' ) , values [ 0 ] ) ; } if ( values . length > 1 ) { if ( ! this . hasFlag ( AcroChoiceFlags . MultiSelect ) ) { throw new MultiSelectValueError ( ) ; } this . dict . set ( PDFName . of ( 'V' ) , this . dict . context . obj ( values ) ) ; } this . updateSelectedIndices ( values ) ; } valuesAreValid ( values ) { const options = this . getOptions ( ) ; for ( let idx = 0 , len = values . length ; idx < len ; idx ++ ) { const val = values [ idx ] . decodeText ( ) ; if ( ! options . find ( ( o ) => val === ( o . display || o . value ) . decodeText ( ) ) ) { return false ; } } return true ; } updateSelectedIndices ( values ) { if ( values . length > 1 ) { const indices = new Array < number > ( values . length ) ; const options = this . getOptions ( ) ; for ( let idx = 0 , len = values . length ; idx < len ; idx ++ ) { const val = values [ idx ] . decodeText ( ) ; indices [ idx ] = options . findIndex ( ( o ) => val === ( o . display || o . value ) . decodeText ( ) ) ; } this . dict . set ( PDFName . of ( 'I' ) , this . dict . context . obj ( indices . sort ( ) ) ) ; } else { this . dict . delete ( PDFName . of ( 'I' ) ) ; } } getValues ( ) { const v = this . V ( ) ; if ( v instanceof PDFString || v instanceof PDFHexString ) return [ v ] ; if ( v instanceof PDFArray ) { const values = [ ] ; for ( let idx = 0 , len = v . size ( ) ; idx < len ; idx ++ ) { const value = v . lookup ( idx ) ; if ( value instanceof PDFString || value instanceof PDFHexString ) { values . push ( value ) ; } } return values ; } return [ ] ; } Opt ( ) { return this . dict . lookupMaybe ( PDFName . of ( 'Opt' ) , PDFString , PDFHexString , PDFArray ) ; } setOptions ( options ) { const newOpt = new Array < PDFArray > ( options . length ) ; for ( let idx = 0 , len = options . length ; idx < len ; idx ++ ) { const { value , display } = options [ idx ] ; newOpt [ idx ] = this . dict . context . obj ( [ value , display || value ] ) ; } this . dict . set ( PDFName . of ( 'Opt' ) , this . dict . context . obj ( newOpt ) ) ; } getOptions ( ) { const Opt = this . Opt ( ) ; if ( Opt instanceof PDFString || Opt instanceof PDFHexString ) { return [ { value : Opt , display : Opt } ] ; } if ( Opt instanceof PDFArray ) { const res = [ ] ; for ( let idx = 0 , len = Opt . size ( ) ; idx < len ; idx ++ ) { const item = Opt . lookup ( idx ) ; if ( item instanceof PDFString || item instanceof PDFHexString ) { res . push ( { value : item , display : item } ) ; } if ( item instanceof PDFArray ) { if ( item . size ( ) > 0 ) { const first = item . lookup ( 0 , PDFString , PDFHexString ) ; const second = item . lookupMaybe ( 1 , PDFString , PDFHexString ) ; res . push ( { value : first , display : second || first } ) ; } } } return res ; } return [ ] ; } } export default PDFAcroChoice ;