import Embeddable from 'src/api/Embeddable' ; import PDFDocument from 'src/api/PDFDocument' ; import { JpegEmbedder , PDFRef , PngEmbedder } from 'src/core' ; import { assertIs } from 'src/utils' ; export type ImageEmbedder = JpegEmbedder | PngEmbedder ; /**
 * Represents an image that has been embedded in a [[PDFDocument]].
 */ export default class PDFImage implements Embeddable { param ref param doc param embedder static of = ( ref , doc , embedder ) => new PDFImage ( ref , doc , embedder ) ; /** The unique reference assigned to this image within the document. */ readonly ref : PDFRef ; /** The document to which this image belongs. */ readonly doc : PDFDocument ; /** The width of this image in pixels. */ readonly width : number ; /** The height of this image in pixels. */ readonly height : number ; private embedder : ImageEmbedder | undefined ; private embedTask : Promise < PDFRef > | undefined ; private constructor ( ref , doc , embedder ) { assertIs ( ref , 'ref' , [ [ PDFRef , 'PDFRef' ] ] ) ; assertIs ( doc , 'doc' , [ [ PDFDocument , 'PDFDocument' ] ] ) ; assertIs ( embedder , 'embedder' , [ [ JpegEmbedder , 'JpegEmbedder' ] , [ PngEmbedder , 'PngEmbedder' ] , ] ) ; this . ref = ref ; this . doc = doc ; this . width = embedder . width ; this . height = embedder . height ; this . embedder = embedder ; } param factor returns scale ( factor ) { assertIs ( factor , 'factor' , [ 'number' ] ) ; return { width : this . width * factor , height : this . height * factor } ; } param width param height returns scaleToFit ( width , height ) { assertIs ( width , 'width' , [ 'number' ] ) ; assertIs ( height , 'height' , [ 'number' ] ) ; const imgWidthScale = width / this . width ; const imgHeightScale = height / this . height ; const scale = Math . min ( imgWidthScale , imgHeightScale ) ; return this . scale ( scale ) ; } returns size ( ) { return this . scale ( 1 ) ; } returns async embed ( ) { if ( ! this . embedder ) return ; if ( ! this . embedTask ) { const { doc , ref } = this ; this . embedTask = this . embedder . embedIntoContext ( doc . context , ref ) ; } await this . embedTask ; this . embedder = undefined ; } }