Skip to content

Add cmap table format 6 #800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/glyph-inspector.html
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,12 @@ <h1>Free Software</h1>
hline('yMin', font.tables.head.yMin);
hline('Ascender', font.tables.hhea.ascender);
hline('Descender', font.tables.hhea.descender);
hline('Typo Ascender', font.tables.os2.sTypoAscender);
hline('Typo Descender', font.tables.os2.sTypoDescender);

// Some PDF-embedded fonts might not have the OS/2 table
if (font.tables.os2) {
hline('Typo Ascender', font.tables.os2.sTypoAscender);
hline('Typo Descender', font.tables.os2.sTypoDescender);
}
}

window.redraw = function(options = { withColors: true, withVariations: true }) {
Expand Down
2 changes: 1 addition & 1 deletion src/encoding.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function addGlyphNamesAll(font) {
glyph = font.glyphs.get(i);
if (font.cffEncoding) {
glyph.name = font.cffEncoding.charset[i];
} else if (font.glyphNames.names) {
} else if (font.glyphNames && font.glyphNames.names) {
glyph.name = font.glyphNames.glyphIndexToName(i);
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/tables/cmap.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ function parseCmapTableFormat4(cmap, p, data, start, offset) {
}
}

function parseCmapTableFormat6(cmap, p) {
cmap.length = p.parseUShort();
cmap.language = p.parseUShort();

cmap.firstCode = p.parseUShort();
cmap.entryCount = p.parseUShort();
cmap.glyphIndexMap = {};

for (let i = 0; i < cmap.entryCount; i += 1) {
cmap.glyphIndexMap[cmap.firstCode + i] = p.parseUShort();
}
}

function parseCmapTableFormat14(cmap, p) {
const varSelectorList = {};

Expand Down Expand Up @@ -216,6 +229,8 @@ function parseCmapTable(data, start) {
parseCmapTableFormat12or13(cmap, p, cmap.format);
} else if (cmap.format === 4) {
parseCmapTableFormat4(cmap, p, data, start, offset);
} else if (cmap.format === 6) {
parseCmapTableFormat6(cmap, p);
} else {
throw new Error(
'Only format 0 (platformId 1, encodingId 0), 4, 12 and 14 cmap tables are supported ' +
Expand Down Expand Up @@ -410,4 +425,4 @@ function makeCmapTable(glyphs) {

export default { parse: parseCmapTable, make: makeCmapTable };

export { parseCmapTableFormat0, parseCmapTableFormat14 };
export { parseCmapTableFormat0, parseCmapTableFormat6, parseCmapTableFormat14 };
Binary file added test/fonts/PDF-Embedded-Calibri-Subset.ttf
Binary file not shown.
16 changes: 15 additions & 1 deletion test/tables/cmap.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import { unhex } from '../testutil.mjs';
import { Parser } from '../../src/parse.mjs';
import { parseCmapTableFormat14, parseCmapTableFormat0 } from '../../src/tables/cmap.mjs';
import { parseCmapTableFormat14, parseCmapTableFormat6, parseCmapTableFormat0 } from '../../src/tables/cmap.mjs';
import { parse } from '../../src/opentype.mjs';
import { readFileSync } from 'fs';
const loadSync = (url, opt) => parse(readFileSync(url), opt);
Expand Down Expand Up @@ -82,4 +82,18 @@ describe('tables/cmap.mjs', function() {
const expectedGlyphIds = [1,2,3,4];
assert.deepEqual(glyphIds, expectedGlyphIds);
});

it('can parse CMAP table format 6', function() {
let font;
assert.doesNotThrow(function() {
font = loadSync('./test/fonts/PDF-Embedded-Calibri-Subset.ttf');
});

// The embedded char IDs for the string "Living Room" in the PDF embedded font
const testString = '!"#"$%&\'(()';
const glyphIds = font.stringToGlyphIndexes(testString);

const expectedGlyphIds = [3, 13, 21, 13, 16, 11, 1, 5, 17, 17, 15];
assert.deepEqual(glyphIds, expectedGlyphIds);
});
});