Skip to content

Commit 50cc4ad

Browse files
Merge pull request #20535 from calixteman/issue18062
Use CIDToGIDMap when the font is a type 2 with an OpenType font
2 parents 1990fa7 + 5518c8a commit 50cc4ad

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

src/core/cff_parser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ class CFFParser {
255255
const charStringOffset = topDict.getByName("CharStrings");
256256
const charStringIndex = this.parseIndex(charStringOffset).obj;
257257

258+
cff.charStringCount = charStringIndex.count;
259+
258260
const fontMatrix = topDict.getByName("FontMatrix");
259261
if (fontMatrix) {
260262
properties.fontMatrix = fontMatrix;
@@ -1005,6 +1007,7 @@ class CFF {
10051007
this.fdSelect = null;
10061008

10071009
this.isCIDFont = false;
1010+
this.charStringCount = 0;
10081011
}
10091012

10101013
duplicateFirstGlyph() {

src/core/fonts.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ import { CFFFont } from "./cff_font.js";
6060
import { FontRendererFactory } from "./font_renderer.js";
6161
import { getFontBasicMetrics } from "./metrics.js";
6262
import { GlyfTable } from "./glyf.js";
63-
import { IdentityCMap } from "./cmap.js";
6463
import { OpenTypeFileBuilder } from "./opentype_file_builder.js";
6564
import { readUint32 } from "./core_utils.js";
6665
import { 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 = {

test/pdfs/issue18062.pdf.link

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/mozilla/pdf.js/files/15274065/1.pdf

test/test_manifest.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13123,5 +13123,13 @@
1312313123
"rounds": 1,
1312413124
"link": true,
1312513125
"type": "eq"
13126+
},
13127+
{
13128+
"id": "issue18062",
13129+
"file": "pdfs/issue18062.pdf",
13130+
"md5": "b8fb6d5622f0a2f45be8e26da7de969c",
13131+
"rounds": 1,
13132+
"link": true,
13133+
"type": "eq"
1312613134
}
1312713135
]

0 commit comments

Comments
 (0)