@@ -60,7 +60,6 @@ import { CFFFont } from "./cff_font.js";
6060import { FontRendererFactory } from "./font_renderer.js" ;
6161import { getFontBasicMetrics } from "./metrics.js" ;
6262import { GlyfTable } from "./glyf.js" ;
63- import { IdentityCMap } from "./cmap.js" ;
6463import { OpenTypeFileBuilder } from "./opentype_file_builder.js" ;
6564import { readUint32 } from "./core_utils.js" ;
6665import { Stream } from "./stream.js" ;
@@ -2638,27 +2637,23 @@ class Font {
26382637 header = readOpenTypeHeader ( font ) ;
26392638 tables = readTables ( font , header . numTables ) ;
26402639 }
2641- let cff , cffFile ;
26422640
26432641 const isTrueType = ! tables [ "CFF " ] ;
26442642 if ( ! isTrueType ) {
2645- const isComposite =
2646- properties . composite &&
2647- ( properties . cidToGidMap ?. length > 0 ||
2648- ! ( properties . cMap instanceof IdentityCMap ) ) ;
26492643 // OpenType font (skip composite fonts with non-default glyph mapping).
26502644 if (
2651- ( header . version === "OTTO" && ! isComposite ) ||
2645+ ( header . version === "OTTO" && ! properties . composite ) ||
26522646 ! tables . head ||
26532647 ! tables . hhea ||
26542648 ! tables . maxp ||
26552649 ! tables . post
26562650 ) {
26572651 // No major tables: throwing everything at `CFFFont`.
2658- cffFile = new Stream ( tables [ "CFF " ] . data ) ;
2659- cff = new CFFFont ( cffFile , properties ) ;
2660-
2661- return this . convert ( name , cff , properties ) ;
2652+ return this . convert (
2653+ name ,
2654+ new CFFFont ( new Stream ( tables [ "CFF " ] . data ) , properties ) ,
2655+ properties
2656+ ) ;
26622657 }
26632658
26642659 delete tables . glyf ;
@@ -2686,9 +2681,32 @@ class Font {
26862681 throw new FormatError ( 'Required "maxp" table is not found' ) ;
26872682 }
26882683
2684+ let numGlyphsFromCFF ;
2685+ if ( ! isTrueType ) {
2686+ try {
2687+ // Trying to repair CFF file
2688+ const parser = new CFFParser (
2689+ new Stream ( tables [ "CFF " ] . data ) ,
2690+ properties ,
2691+ SEAC_ANALYSIS_ENABLED
2692+ ) ;
2693+ const cff = parser . parse ( ) ;
2694+ cff . duplicateFirstGlyph ( ) ;
2695+ const compiler = new CFFCompiler ( cff ) ;
2696+ tables [ "CFF " ] . data = compiler . compile ( ) ;
2697+ numGlyphsFromCFF = cff . charStringCount ;
2698+ } catch {
2699+ warn ( "Failed to compile font " + properties . loadedName ) ;
2700+ }
2701+ }
2702+
26892703 font . pos = ( font . start || 0 ) + tables . maxp . offset ;
26902704 let version = font . getInt32 ( ) ;
2691- const numGlyphs = font . getUint16 ( ) ;
2705+ const numGlyphs = numGlyphsFromCFF ?? font . getUint16 ( ) ;
2706+ if ( version === 0x00005000 && tables . maxp . length !== 6 ) {
2707+ tables . maxp . data = tables . maxp . data . subarray ( 0 , 6 ) ;
2708+ tables . maxp . length = 6 ;
2709+ }
26922710
26932711 if ( version !== 0x00010000 && version !== 0x00005000 ) {
26942712 // https://learn.microsoft.com/en-us/typography/opentype/spec/maxp
@@ -3099,24 +3117,6 @@ class Font {
30993117 }
31003118 }
31013119
3102- if ( ! isTrueType ) {
3103- try {
3104- // Trying to repair CFF file
3105- cffFile = new Stream ( tables [ "CFF " ] . data ) ;
3106- const parser = new CFFParser (
3107- cffFile ,
3108- properties ,
3109- SEAC_ANALYSIS_ENABLED
3110- ) ;
3111- cff = parser . parse ( ) ;
3112- cff . duplicateFirstGlyph ( ) ;
3113- const compiler = new CFFCompiler ( cff ) ;
3114- tables [ "CFF " ] . data = compiler . compile ( ) ;
3115- } catch {
3116- warn ( "Failed to compile font " + properties . loadedName ) ;
3117- }
3118- }
3119-
31203120 // Re-creating 'name' table
31213121 if ( ! tables . name ) {
31223122 tables . name = {
0 commit comments