@@ -838,97 +838,12 @@ function locateTableInBuffer(buffer, sfntOffset) {
838838 return result ;
839839}
840840
841- function locateWoffTableInBuffer ( buffer ) {
842- const view = new DataView ( buffer ) ;
843- if ( buffer . byteLength < 48 ) return { } ;
844- const numTables = view . getUint16 ( 12 , false ) ;
845- const result = { } ;
846- for ( let i = 0 ; i < numTables ; i ++ ) {
847- const pos = 44 + i * 20 ;
848- if ( pos + 20 > buffer . byteLength ) break ;
849- const tag = String . fromCharCode (
850- view . getUint8 ( pos ) , view . getUint8 ( pos + 1 ) ,
851- view . getUint8 ( pos + 2 ) , view . getUint8 ( pos + 3 )
852- ) ;
853- const offset = view . getUint32 ( pos + 4 , false ) ;
854- const compLength = view . getUint32 ( pos + 8 , false ) ;
855- const origLength = view . getUint32 ( pos + 12 , false ) ;
856- result [ tag ] = { offset, compLength, origLength } ;
857- }
858- return result ;
859- }
860-
861- function extractWoffTable ( buffer , entry ) {
862- const { offset, compLength, origLength } = entry ;
863- const raw = new Uint8Array ( buffer , offset , compLength ) ;
864- if ( compLength === origLength ) return raw . buffer . slice ( offset , offset + origLength ) ;
865- const inflated = new Uint8Array ( origLength ) ;
866- let si = 0 , di = 0 ;
867- while ( si < raw . length ) {
868- const bfinal = raw [ si ] & 1 ;
869- const btype = ( raw [ si ] >> 1 ) & 3 ;
870- si ++ ;
871- if ( btype === 0 ) {
872- si = ( si + 3 ) & ~ 3 ;
873- if ( si + 4 > raw . length ) break ;
874- const len = raw [ si ] | ( raw [ si + 1 ] << 8 ) ;
875- si += 4 ;
876- for ( let k = 0 ; k < len && si < raw . length && di < origLength ; k ++ ) inflated [ di ++ ] = raw [ si ++ ] ;
877- } else {
878- break ;
879- }
880- if ( bfinal ) break ;
881- }
882- const tmp = new ArrayBuffer ( origLength ) ;
883- new Uint8Array ( tmp ) . set ( inflated ) ;
884- return tmp ;
885- }
886-
887841function parseFontMetaFromBuffer ( buffer , sfntOffset ) {
888842 const view = new DataView ( buffer ) ;
889843 const tables = locateTableInBuffer ( buffer , sfntOffset ) ;
890844 return extractMetaFromTables ( buffer , tables , false ) ;
891845}
892846
893- function parseFontMetaFromWoff ( buffer ) {
894- const woffTables = locateWoffTableInBuffer ( buffer ) ;
895- const nameEntry = woffTables [ 'name' ] ;
896- const os2Entry = woffTables [ 'OS/2' ] ;
897-
898- const syntheticBuffer = buffer ;
899- const allNames = new Set ( ) ;
900- const familyNames = new Set ( ) ;
901- let weight = 400 , isItalic = false , version = '' , subfamilyName = '' , description = '' ;
902-
903- if ( nameEntry ) {
904- const nameBuf = extractWoffTable ( buffer , nameEntry ) ;
905- const raw = readNameTableRaw ( nameBuf , 0 ) ;
906- if ( raw ) {
907- extractNamesFromRaw ( nameBuf , raw . records , raw . strOff , allNames , familyNames ,
908- ( v ) => { if ( ! version ) version = v ; } ,
909- ( v ) => { if ( ! subfamilyName ) subfamilyName = v ; } ,
910- ( v ) => { if ( ! description ) description = v ; }
911- ) ;
912- }
913- }
914-
915- if ( os2Entry ) {
916- const os2Buf = extractWoffTable ( buffer , os2Entry ) ;
917- const os2View = new DataView ( os2Buf ) ;
918- if ( os2Buf . byteLength >= 64 ) {
919- const wc = os2View . getUint16 ( 4 , false ) ;
920- if ( wc ) weight = wc ;
921- const fsSel = os2View . getUint16 ( 62 , false ) ;
922- isItalic = ! ! ( fsSel & 1 ) ;
923- }
924- }
925-
926- applySubfamilyFallbacks ( subfamilyName , weight , isItalic ,
927- ( w ) => { weight = w ; } , ( it ) => { isItalic = it ; } ) ;
928-
929- return { allNames, familyNames, weight, isItalic, version, subfamilyName, description } ;
930- }
931-
932847function extractNamesFromRaw ( buffer , records , strOff , allNames , familyNames , onVersion , onSubfamily , onDescription ) {
933848 const FAMILY_IDS = new Set ( [ NAME_ID_FAMILY , NAME_ID_FULL , NAME_ID_PREF_FAMILY ] ) ;
934849 const INCLUDE_IDS = new Set ( [
@@ -1021,14 +936,14 @@ function readFontDescriptionRaw(buffer) {
1021936 if ( buffer . byteLength < 4 ) return '' ;
1022937 const view = new DataView ( buffer ) ;
1023938 const magic = view . getUint32 ( 0 , false ) ;
1024- if ( magic === MAGIC_WOFF2 ) return '' ;
1025- if ( magic === MAGIC_WOFF ) {
1026- const woffTables = locateWoffTableInBuffer ( buffer ) ;
1027- if ( ! woffTables [ 'name' ] ) return '' ;
1028- const nameBuf = extractWoffTable ( buffer , woffTables [ 'name' ] ) ;
1029- const raw = readNameTableRaw ( nameBuf , 0 ) ;
1030- if ( ! raw ) return '' ;
1031- return bestNameValue ( raw . records , raw . strOff , nameBuf , NAME_ID_DESCRIPTION ) ;
939+ if ( magic === MAGIC_WOFF2 || magic === MAGIC_WOFF ) {
940+ try {
941+ const fontObj = opentype . parse ( buffer ) ;
942+ const desc = fontObj . names ?. description ?. en || Object . values ( fontObj . names ?. description || { } ) [ 0 ] ;
943+ return typeof desc === 'string' ? desc : '' ;
944+ } catch ( e ) {
945+ return '' ;
946+ }
1032947 }
1033948 const isTTC = magic === MAGIC_TTC ;
1034949 const sfntOffset = isTTC ? view . getUint32 ( 12 , false ) : 0 ;
@@ -1073,7 +988,7 @@ function matchFontBuffer(buffer, requiredFonts, id) {
1073988 } ;
1074989
1075990 try {
1076- if ( magic === MAGIC_WOFF2 ) {
991+ if ( magic === MAGIC_WOFF2 || magic === MAGIC_WOFF ) {
1077992 const fontObj = opentype . parse ( buffer ) ;
1078993 const allNames = new Set ( ) ;
1079994 const familyNames = new Set ( ) ;
@@ -1101,8 +1016,6 @@ function matchFontBuffer(buffer, requiredFonts, id) {
11011016 const sub = ( fontObj . names ?. preferredSubfamily ?. en || fontObj . names ?. fontSubfamily ?. en || '' ) . trim ( ) ;
11021017 const ver = ( fontObj . names ?. version ?. en || '' ) . trim ( ) ;
11031018 commitResult ( { allNames, familyNames, weight, isItalic, version : ver , subfamilyName : sub } , - 1 ) ;
1104- } else if ( magic === MAGIC_WOFF ) {
1105- commitResult ( parseFontMetaFromWoff ( buffer ) , - 1 ) ;
11061019 } else if ( magic === MAGIC_TTC ) {
11071020 const numFonts = view . getUint32 ( 8 , false ) ;
11081021 for ( let i = 0 ; i < numFonts ; i ++ ) {
0 commit comments