@@ -606,4 +606,79 @@ class FontInfo {
606606 }
607607}
608608
609- export { CssFontInfo , FontInfo , SystemFontInfo } ;
609+ class FontPathInfo {
610+ static write ( path ) {
611+ let lengthEstimate = 0 ;
612+ const commands = [ ] ;
613+ for ( const cmd of path ) {
614+ const code = cmd . charCodeAt ( 0 ) ;
615+ let args = null ;
616+ if ( code === 67 || code === 76 || code === 77 ) {
617+ args = cmd . slice ( 1 ) . split ( " " ) ;
618+ lengthEstimate += 1 + args . length * 8 ;
619+ } else if ( code === 90 ) {
620+ lengthEstimate += 1 ;
621+ } else {
622+ throw new Error ( `Invalid path command: ${ cmd } ` ) ;
623+ }
624+ commands . push ( { code, args } ) ;
625+ }
626+
627+ const buffer = new ArrayBuffer ( 4 + lengthEstimate ) ;
628+ const view = new DataView ( buffer ) ;
629+ let offset = 0 ;
630+
631+ view . setUint32 ( offset , commands . length ) ;
632+ offset += 4 ;
633+ for ( const { code, args } of commands ) {
634+ view . setUint8 ( offset , code ) ;
635+ offset += 1 ;
636+ if ( args ) {
637+ for ( const arg of args ) {
638+ view . setFloat64 ( offset , parseFloat ( arg ) , true ) ;
639+ offset += 8 ;
640+ }
641+ }
642+ }
643+
644+ assert ( offset === buffer . byteLength , "FontPathInfo.write: Buffer overflow" ) ;
645+ return buffer ;
646+ }
647+
648+ #buffer;
649+
650+ #view;
651+
652+ constructor ( buffer ) {
653+ this . #buffer = buffer ;
654+ this . #view = new DataView ( this . #buffer) ;
655+ }
656+
657+ getSVG ( ) {
658+ const length = this . #view. getUint32 ( 0 ) ;
659+ const cmds = [ ] ;
660+ let offset = 4 ;
661+ for ( let i = 0 ; i < length ; i ++ ) {
662+ const code = String . fromCharCode ( this . #view. getUint8 ( offset ) ) ;
663+ offset += 1 ;
664+ // eslint-disable-next-line no-nested-ternary
665+ const numArgs = code === "M" || code === "L" ? 2 : code === "C" ? 6 : 0 ;
666+ let args = null ;
667+ if ( numArgs > 0 ) {
668+ args = [ ] ;
669+ for ( let j = 0 ; j < numArgs ; j ++ ) {
670+ args . push ( this . #view. getFloat64 ( offset , true ) ) ;
671+ offset += 8 ;
672+ }
673+ }
674+ cmds . push ( code + ( args ? args . join ( " " ) : "" ) ) ;
675+ }
676+ assert (
677+ offset === this . #buffer. byteLength ,
678+ "FontPathInfo.toString: Buffer overflow"
679+ ) ;
680+ return cmds . join ( "" ) ;
681+ }
682+ }
683+
684+ export { CssFontInfo , FontInfo , FontPathInfo , SystemFontInfo } ;
0 commit comments