import Embeddable from 'src/api//Embeddable' ; import PDFDocument from 'src/api/PDFDocument' ; import { CustomFontEmbedder , PDFHexString , PDFRef , StandardFontEmbedder , } from 'src/core' ; import { assertIs , assertOrUndefined } from 'src/utils' ; export type FontEmbedder = CustomFontEmbedder | StandardFontEmbedder ; /**
 * Represents a font that has been embedded in a [[PDFDocument]].
 */ export default class PDFFont implements Embeddable { param ref param doc param embedder static of = ( ref , doc , embedder ) => new PDFFont ( ref , doc , embedder ) ; /** The unique reference assigned to this font within the document. */ readonly ref : PDFRef ; /** The document to which this font belongs. */ readonly doc : PDFDocument ; /** The name of this font. */ readonly name : string ; private modified = true ; private readonly embedder : FontEmbedder ; private constructor ( ref , doc , embedder ) { assertIs ( ref , 'ref' , [ [ PDFRef , 'PDFRef' ] ] ) ; assertIs ( doc , 'doc' , [ [ PDFDocument , 'PDFDocument' ] ] ) ; assertIs ( embedder , 'embedder' , [ [ CustomFontEmbedder , 'CustomFontEmbedder' ] , [ StandardFontEmbedder , 'StandardFontEmbedder' ] , ] ) ; this . ref = ref ; this . doc = doc ; this . name = embedder . fontName ; this . embedder = embedder ; } param text returns encodeText ( text ) { assertIs ( text , 'text' , [ 'string' ] ) ; this . modified = true ; return this . embedder . encodeText ( text ) ; } param text param size returns widthOfTextAtSize ( text , size ) { assertIs ( text , 'text' , [ 'string' ] ) ; assertIs ( size , 'size' , [ 'number' ] ) ; return this . embedder . widthOfTextAtSize ( text , size ) ; } param size param options returns heightAtSize ( size , options ? ) { assertIs ( size , 'size' , [ 'number' ] ) ; assertOrUndefined ( options ?. descender , 'options.descender' , [ 'boolean' ] ) ; return this . embedder . heightOfFontAtSize ( size , { descender : options ?. descender ?? true , } ) ; } param height returns sizeAtHeight ( height ) { assertIs ( height , 'height' , [ 'number' ] ) ; return this . embedder . sizeOfFontAtHeight ( height ) ; } returns getCharacterSet ( ) { if ( this . embedder instanceof StandardFontEmbedder ) { return this . embedder . encoding . supportedCodePoints ; } else { return this . embedder . font . characterSet ; } } returns async embed ( ) { if ( this . modified ) { await this . embedder . embedIntoContext ( this . doc . context , this . ref ) ; this . modified = false ; } } }