import PDFArray from 'src/core/objects/PDFArray' ; import PDFBool from 'src/core/objects/PDFBool' ; import PDFDict from 'src/core/objects/PDFDict' ; import PDFName from 'src/core/objects/PDFName' ; import PDFNumber from 'src/core/objects/PDFNumber' ; import PDFContext from 'src/core/PDFContext' ; import { assertEachIs , assertInteger , assertIsOneOf , assertRange , } from 'src/utils' ; const asEnum = < T extends string | number , U extends { [ key ] : T ; } > ( rawValue , enumType ) : U [ keyof U ] | undefined => { if ( rawValue === undefined ) return undefined ; return enumType [ rawValue ] ; } ; export enum NonFullScreenPageMode { /**
     * After exiting FullScreen mode, neither the document outline nor thumbnail
     * images should be visible.
     */ UseNone = 'UseNone' , /** After exiting FullScreen mode, the document outline should be visible. */ UseOutlines = 'UseOutlines' , /** After exiting FullScreen mode, thumbnail images should be visible. */ UseThumbs = 'UseThumbs' , /**
     * After exiting FullScreen mode, the optional content group panel should be
     * visible.
     */ UseOC = 'UseOC' } export enum ReadingDirection { /** The predominant reading order is Left to Right. */ L2R = 'L2R' , /**
     * The predominant reading order is Right to left (including vertical writing
     * systems, such as Chinese, Japanese and Korean).
     */ R2L = 'R2L' } export enum PrintScaling { /** No page scaling. */ None = 'None' , AppDefault = 'AppDefault' } export enum Duplex { /** The PDF reader should print single-sided. */ Simplex = 'Simplex' , /**
     * The PDF reader should print double sided and flip on the short edge of the
     * sheet.
     */ DuplexFlipShortEdge = 'DuplexFlipShortEdge' , /**
     * The PDF reader should print double sided and flip on the long edge of the
     * sheet.
     */ DuplexFlipLongEdge = 'DuplexFlipLongEdge' } type BoolViewerPrefKey = 'HideToolbar' | 'HideMenubar' | 'HideWindowUI' | 'FitWindow' | 'CenterWindow' | 'DisplayDocTitle' | 'PickTrayByPDFSize' ; type NameViewerPrefKey = 'NonFullScreenPageMode' | 'Direction' | 'PrintScaling' | 'Duplex' ; interface PageRange { start : number ; end : number ; } class ViewerPreferences { ignore readonly dict : PDFDict ; ignore static fromDict = ( dict ) : ViewerPreferences => new ViewerPreferences ( dict ) ; ignore static create = ( context ) => { const dict = context . obj ( { } ) ; return new ViewerPreferences ( dict ) ; } ; ignore protected constructor ( dict ) { this . dict = dict ; } protected lookupBool ( key ) { const returnObj = this . dict . lookup ( PDFName . of ( key ) ) ; if ( returnObj instanceof PDFBool ) return returnObj ; return undefined ; } protected lookupName ( key ) { const returnObj = this . dict . lookup ( PDFName . of ( key ) ) ; if ( returnObj instanceof PDFName ) return returnObj ; return undefined ; } ignore HideToolbar ( ) { return this . lookupBool ( 'HideToolbar' ) ; } ignore HideMenubar ( ) { return this . lookupBool ( 'HideMenubar' ) ; } ignore HideWindowUI ( ) { return this . lookupBool ( 'HideWindowUI' ) ; } ignore FitWindow ( ) { return this . lookupBool ( 'FitWindow' ) ; } ignore CenterWindow ( ) { return this . lookupBool ( 'CenterWindow' ) ; } ignore DisplayDocTitle ( ) { return this . lookupBool ( 'DisplayDocTitle' ) ; } ignore NonFullScreenPageMode ( ) { return this . lookupName ( 'NonFullScreenPageMode' ) ; } ignore Direction ( ) { return this . lookupName ( 'Direction' ) ; } ignore PrintScaling ( ) { return this . lookupName ( 'PrintScaling' ) ; } ignore Duplex ( ) { return this . lookupName ( 'Duplex' ) ; } ignore PickTrayByPDFSize ( ) { return this . lookupBool ( 'PickTrayByPDFSize' ) ; } ignore PrintPageRange ( ) { const PrintPageRange = this . dict . lookup ( PDFName . of ( 'PrintPageRange' ) ) ; if ( PrintPageRange instanceof PDFArray ) return PrintPageRange ; return undefined ; } ignore NumCopies ( ) { const NumCopies = this . dict . lookup ( PDFName . of ( 'NumCopies' ) ) ; if ( NumCopies instanceof PDFNumber ) return NumCopies ; return undefined ; } returns getHideToolbar ( ) { return this . HideToolbar ( ) ?. asBoolean ( ) ?? false ; } returns getHideMenubar ( ) { return this . HideMenubar ( ) ?. asBoolean ( ) ?? false ; } returns getHideWindowUI ( ) { return this . HideWindowUI ( ) ?. asBoolean ( ) ?? false ; } returns getFitWindow ( ) { return this . FitWindow ( ) ?. asBoolean ( ) ?? false ; } returns getCenterWindow ( ) { return this . CenterWindow ( ) ?. asBoolean ( ) ?? false ; } returns getDisplayDocTitle ( ) { return this . DisplayDocTitle ( ) ?. asBoolean ( ) ?? false ; } returns getNonFullScreenPageMode ( ) { const mode = this . NonFullScreenPageMode ( ) ?. decodeText ( ) ; return asEnum ( mode , NonFullScreenPageMode ) ?? NonFullScreenPageMode . UseNone ; } returns getReadingDirection ( ) { const direction = this . Direction ( ) ?. decodeText ( ) ; return asEnum ( direction , ReadingDirection ) ?? ReadingDirection . L2R ; } returns getPrintScaling ( ) { const scaling = this . PrintScaling ( ) ?. decodeText ( ) ; return asEnum ( scaling , PrintScaling ) ?? PrintScaling . AppDefault ; } returns getDuplex ( ) { const duplex = this . Duplex ( ) ?. decodeText ( ) ; return asEnum ( duplex , Duplex ) ; } returns getPickTrayByPDFSize ( ) { return this . PickTrayByPDFSize ( ) ?. asBoolean ( ) ; } returns getPrintPageRange ( ) { const rng = this . PrintPageRange ( ) ; if ( ! rng ) return [ ] ; const pageRanges = [ ] ; for ( let i = 0 ; i < rng . size ( ) ; i += 2 ) { const start = rng . lookup ( i , PDFNumber ) . asNumber ( ) ; const end = rng . lookup ( i + 1 , PDFNumber ) . asNumber ( ) ; pageRanges . push ( { start , end } ) ; } return pageRanges ; } returns getNumCopies ( ) { return this . NumCopies ( ) ?. asNumber ( ) ?? 1 ; } param hideToolbar setHideToolbar ( hideToolbar ) { const HideToolbar = this . dict . context . obj ( hideToolbar ) ; this . dict . set ( PDFName . of ( 'HideToolbar' ) , HideToolbar ) ; } param hideMenubar setHideMenubar ( hideMenubar ) { const HideMenubar = this . dict . context . obj ( hideMenubar ) ; this . dict . set ( PDFName . of ( 'HideMenubar' ) , HideMenubar ) ; } param hideWindowUI setHideWindowUI ( hideWindowUI ) { const HideWindowUI = this . dict . context . obj ( hideWindowUI ) ; this . dict . set ( PDFName . of ( 'HideWindowUI' ) , HideWindowUI ) ; } param fitWindow setFitWindow ( fitWindow ) { const FitWindow = this . dict . context . obj ( fitWindow ) ; this . dict . set ( PDFName . of ( 'FitWindow' ) , FitWindow ) ; } param centerWindow setCenterWindow ( centerWindow ) { const CenterWindow = this . dict . context . obj ( centerWindow ) ; this . dict . set ( PDFName . of ( 'CenterWindow' ) , CenterWindow ) ; } param displayTitle setDisplayDocTitle ( displayTitle ) { const DisplayDocTitle = this . dict . context . obj ( displayTitle ) ; this . dict . set ( PDFName . of ( 'DisplayDocTitle' ) , DisplayDocTitle ) ; } param nonFullScreenPageMode setNonFullScreenPageMode ( nonFullScreenPageMode ) { assertIsOneOf ( nonFullScreenPageMode , 'nonFullScreenPageMode' , NonFullScreenPageMode ) ; const mode = PDFName . of ( nonFullScreenPageMode ) ; this . dict . set ( PDFName . of ( 'NonFullScreenPageMode' ) , mode ) ; } param readingDirection setReadingDirection ( readingDirection ) { assertIsOneOf ( readingDirection , 'readingDirection' , ReadingDirection ) ; const direction = PDFName . of ( readingDirection ) ; this . dict . set ( PDFName . of ( 'Direction' ) , direction ) ; } param printScaling setPrintScaling ( printScaling ) { assertIsOneOf ( printScaling , 'printScaling' , PrintScaling ) ; const scaling = PDFName . of ( printScaling ) ; this . dict . set ( PDFName . of ( 'PrintScaling' ) , scaling ) ; } param duplex setDuplex ( duplex ) { assertIsOneOf ( duplex , 'duplex' , Duplex ) ; const dup = PDFName . of ( duplex ) ; this . dict . set ( PDFName . of ( 'Duplex' ) , dup ) ; } param pickTrayByPDFSize setPickTrayByPDFSize ( pickTrayByPDFSize ) { const PickTrayByPDFSize = this . dict . context . obj ( pickTrayByPDFSize ) ; this . dict . set ( PDFName . of ( 'PickTrayByPDFSize' ) , PickTrayByPDFSize ) ; } param printPageRange setPrintPageRange ( printPageRange ) { if ( ! Array . isArray ( printPageRange ) ) printPageRange = [ printPageRange ] ; const flatRange = [ ] ; for ( let idx = 0 , len = printPageRange . length ; idx < len ; idx ++ ) { flatRange . push ( printPageRange [ idx ] . start ) ; flatRange . push ( printPageRange [ idx ] . end ) ; } assertEachIs ( flatRange , 'printPageRange' , [ 'number' ] ) ; const pageRanges = this . dict . context . obj ( flatRange ) ; this . dict . set ( PDFName . of ( 'PrintPageRange' ) , pageRanges ) ; } param numCopies setNumCopies ( numCopies ) { assertRange ( numCopies , 'numCopies' , 1 , Number . MAX_VALUE ) ; assertInteger ( numCopies , 'numCopies' ) ; const NumCopies = this . dict . context . obj ( numCopies ) ; this . dict . set ( PDFName . of ( 'NumCopies' ) , NumCopies ) ; } } export default ViewerPreferences ;