@@ -2702,13 +2702,36 @@ class Font {
27022702 writeUint32 ( tables . maxp . data , 0 , version ) ;
27032703 }
27042704
2705+ let isGlyphLocationsLong = int16 (
2706+ tables . head . data [ 50 ] ,
2707+ tables . head . data [ 51 ]
2708+ ) ;
2709+ if ( tables . loca ) {
2710+ const locaLength = isGlyphLocationsLong
2711+ ? ( numGlyphs + 1 ) * 4
2712+ : ( numGlyphs + 1 ) * 2 ;
2713+ if ( tables . loca . length !== locaLength ) {
2714+ warn ( "Incorrect 'loca' table length -- attempting to fix it." ) ;
2715+ // The length of the loca table is wrong (see #13425), so we check if we
2716+ // have enough space to fix it.
2717+ const sortedTables = Object . values ( tables )
2718+ . filter ( Boolean )
2719+ . sort ( ( a , b ) => a . offset - b . offset ) ;
2720+ const locaIndex = sortedTables . indexOf ( tables . loca ) ;
2721+ const nextTable = sortedTables [ locaIndex + 1 ] || null ;
2722+ if ( nextTable && tables . loca . offset + locaLength < nextTable . offset ) {
2723+ const previousPos = font . pos ;
2724+ font . pos = font . start || 0 ;
2725+ font . skip ( tables . loca . offset ) ;
2726+ tables . loca . data = font . getBytes ( locaLength ) ;
2727+ tables . loca . length = locaLength ;
2728+ font . pos = previousPos ;
2729+ }
2730+ }
2731+ }
2732+
27052733 if ( properties . scaleFactors ?. length === numGlyphs && isTrueType ) {
27062734 const { scaleFactors } = properties ;
2707- const isGlyphLocationsLong = int16 (
2708- tables . head . data [ 50 ] ,
2709- tables . head . data [ 51 ]
2710- ) ;
2711-
27122735 const glyphs = new GlyfTable ( {
27132736 glyfTable : tables . glyf . data ,
27142737 isGlyphLocationsLong,
@@ -2723,7 +2746,7 @@ class Font {
27232746
27242747 if ( isLocationLong !== ! ! isGlyphLocationsLong ) {
27252748 tables . head . data [ 50 ] = 0 ;
2726- tables . head . data [ 51 ] = isLocationLong ? 1 : 0 ;
2749+ isGlyphLocationsLong = tables . head . data [ 51 ] = isLocationLong ? 1 : 0 ;
27272750 }
27282751
27292752 const metrics = tables . hmtx . data ;
@@ -2801,10 +2824,6 @@ class Font {
28012824
28022825 let missingGlyphs = Object . create ( null ) ;
28032826 if ( isTrueType ) {
2804- const isGlyphLocationsLong = int16 (
2805- tables . head . data [ 50 ] ,
2806- tables . head . data [ 51 ]
2807- ) ;
28082827 const glyphsInfo = sanitizeGlyphLocations (
28092828 tables . loca ,
28102829 tables . glyf ,
0 commit comments