1- import * as fontkit from 'fontkit' ;
2- import createStandardFont from './font/standard' ;
3- import createEmbeddedFont from './font/embedded' ;
4-
5- export class PDFFont {
6- static open ( document , src , family , id ) {
7- let font ;
8-
9- if ( typeof src === 'string' ) {
10- if ( StandardFont . isStandardFont ( src ) ) {
11- return new StandardFont ( document , src , id ) ;
12- }
13-
14- if ( ! BROWSER ) {
15- font = fontkit . openSync ( src , family ) ;
16- } else {
17- throw new Error ( `Can't open ${ src } in browser build` ) ;
18- }
19- } else if ( src instanceof Uint8Array ) {
20- font = fontkit . create ( src , family ) ;
21- } else if ( src instanceof ArrayBuffer ) {
22- font = fontkit . create ( new Uint8Array ( src ) , family ) ;
23- } else if ( typeof src === 'object' ) {
24- font = src ;
25- }
26-
27- if ( font == null ) {
28- throw new Error ( 'Not a supported font format or standard PDF font.' ) ;
29- }
30-
31- return new EmbeddedFont ( document , font , id ) ;
32- }
1+ class PDFFont {
2+ constructor ( ) { }
333
344 encode ( ) {
355 throw new Error ( 'Must be implemented by subclasses' ) ;
@@ -51,23 +21,17 @@ export class PDFFont {
5121 }
5222
5323 this . embed ( ) ;
54- return ( this . embedded = true ) ;
24+ this . embedded = true ;
5525 }
5626
5727 embed ( ) {
5828 throw new Error ( 'Must be implemented by subclasses' ) ;
5929 }
6030
61- lineHeight ( size , includeGap ) {
62- if ( includeGap == null ) {
63- includeGap = false ;
64- }
31+ lineHeight ( size , includeGap = false ) {
6532 const gap = includeGap ? this . lineGap : 0 ;
6633 return ( ( this . ascender + gap - this . descender ) / 1000 ) * size ;
6734 }
6835}
6936
70- export const StandardFont = createStandardFont ( PDFFont ) ;
71- export const EmbeddedFont = createEmbeddedFont ( PDFFont ) ;
72-
7337export default PDFFont ;
0 commit comments