Browse Source

Use built in CMaps and unify the glyph mapping.

Brendan Dahl 11 years ago
parent
commit
b5b94a4af3
  1. 5
      make.js
  2. 433
      src/core/cmap.js
  3. 118
      src/core/evaluator.js
  4. 1346
      src/core/fonts.js
  5. 2
      src/core/worker.js
  6. 8
      src/display/api.js
  7. 51
      src/display/canvas.js
  8. 1
      test/driver.js
  9. 4
      test/font/font_fpgm_spec.js
  10. 6
      test/font/font_os2_spec.js
  11. 7
      test/font/font_post_spec.js
  12. 1
      test/font/font_test.html
  13. 5
      test/test.py
  14. 19
      test/unit/cmap_spec.js
  15. 2
      test/unit/font_spec.js
  16. 5
      web/viewer.js

5
make.js

@ -105,6 +105,7 @@ target.generic = function() {
copy: [ copy: [
[COMMON_WEB_FILES, GENERIC_DIR + '/web'], [COMMON_WEB_FILES, GENERIC_DIR + '/web'],
['external/webL10n/l10n.js', GENERIC_DIR + '/web'], ['external/webL10n/l10n.js', GENERIC_DIR + '/web'],
['external/cmaps/', GENERIC_DIR + '/web/cmaps'],
['web/viewer.css', GENERIC_DIR + '/web'], ['web/viewer.css', GENERIC_DIR + '/web'],
['web/compatibility.js', GENERIC_DIR + '/web'], ['web/compatibility.js', GENERIC_DIR + '/web'],
['web/compressed.tracemonkey-pldi-09.pdf', GENERIC_DIR + '/web'], ['web/compressed.tracemonkey-pldi-09.pdf', GENERIC_DIR + '/web'],
@ -489,6 +490,7 @@ target.firefox = function() {
defines: defines, defines: defines,
copy: [ copy: [
[COMMON_WEB_FILES, FIREFOX_BUILD_CONTENT_DIR + '/web'], [COMMON_WEB_FILES, FIREFOX_BUILD_CONTENT_DIR + '/web'],
['external/cmaps/', FIREFOX_BUILD_CONTENT_DIR + '/web/cmaps'],
[FIREFOX_EXTENSION_DIR + 'tools/l10n.js', [FIREFOX_EXTENSION_DIR + 'tools/l10n.js',
FIREFOX_BUILD_CONTENT_DIR + '/web'], FIREFOX_BUILD_CONTENT_DIR + '/web'],
['web/default_preferences.js', FIREFOX_BUILD_CONTENT_DIR] ['web/default_preferences.js', FIREFOX_BUILD_CONTENT_DIR]
@ -604,6 +606,7 @@ target.mozcentral = function() {
defines: defines, defines: defines,
copy: [ copy: [
[COMMON_WEB_FILES, MOZCENTRAL_CONTENT_DIR + '/web'], [COMMON_WEB_FILES, MOZCENTRAL_CONTENT_DIR + '/web'],
['external/cmaps/', MOZCENTRAL_CONTENT_DIR + '/web/cmaps'],
['extensions/firefox/tools/l10n.js', MOZCENTRAL_CONTENT_DIR + '/web'], ['extensions/firefox/tools/l10n.js', MOZCENTRAL_CONTENT_DIR + '/web'],
['web/default_preferences.js', MOZCENTRAL_CONTENT_DIR] ['web/default_preferences.js', MOZCENTRAL_CONTENT_DIR]
], ],
@ -673,6 +676,7 @@ target.b2g = function() {
var setup = { var setup = {
defines: defines, defines: defines,
copy: [ copy: [
['external/cmaps/', B2G_BUILD_CONTENT_DIR + '/web/cmaps'],
['extensions/b2g/images', B2G_BUILD_CONTENT_DIR + '/web'], ['extensions/b2g/images', B2G_BUILD_CONTENT_DIR + '/web'],
['extensions/b2g/viewer.html', B2G_BUILD_CONTENT_DIR + '/web'], ['extensions/b2g/viewer.html', B2G_BUILD_CONTENT_DIR + '/web'],
['extensions/b2g/viewer.css', B2G_BUILD_CONTENT_DIR + '/web'], ['extensions/b2g/viewer.css', B2G_BUILD_CONTENT_DIR + '/web'],
@ -713,6 +717,7 @@ target.chromium = function() {
var setup = { var setup = {
defines: defines, defines: defines,
copy: [ copy: [
['external/cmaps/', CHROME_BUILD_CONTENT_DIR + '/web/cmaps'],
[COMMON_WEB_FILES, CHROME_BUILD_CONTENT_DIR + '/web'], [COMMON_WEB_FILES, CHROME_BUILD_CONTENT_DIR + '/web'],
[['extensions/chromium/*.json', [['extensions/chromium/*.json',
'extensions/chromium/*.html', 'extensions/chromium/*.html',

433
src/core/cmap.js

@ -15,184 +15,199 @@
* limitations under the License. * limitations under the License.
*/ */
/* globals Util, isString, isInt, warn, error, isCmd, isEOF, isName, Lexer, /* globals Util, isString, isInt, warn, error, isCmd, isEOF, isName, Lexer,
isStream */ isStream, StringStream */
'use strict'; 'use strict';
var CMAP_CODESPACES = { var BUILT_IN_CMAPS = [
'Adobe-CNS1-0': [[], [0, 14335]], // << Start unicode maps.
'Adobe-CNS1-1': [[], [0, 17407]], 'Adobe-GB1-UCS2',
'Adobe-CNS1-2': [[], [0, 17663]], 'Adobe-CNS1-UCS2',
'Adobe-CNS1-3': [[], [0, 18943]], 'Adobe-Japan1-UCS2',
'Adobe-CNS1-4': [[], [0, 19199]], 'Adobe-Korea1-UCS2',
'Adobe-CNS1-5': [[], [0, 19199]], // >> End unicode maps.
'Adobe-CNS1-6': [[], [0, 19199]], '78-EUC-H',
'Adobe-CNS1-UCS2': [[], [0, 65535]], '78-EUC-V',
'B5-H': [[0, 128], [41280, 65278]], '78-H',
'B5-V': [[0, 128], [41280, 65278]], '78-RKSJ-H',
'B5pc-H': [[0, 128, 253, 255], [41280, 64766]], '78-RKSJ-V',
'B5pc-V': [[0, 128, 253, 255], [41280, 64766]], '78-V',
'CNS-EUC-H': [[0, 128], [41377, 65278], [], '78ms-RKSJ-H',
[2392957345, 2392981246, 2393022881, 2393046782, 2393088417, 2393112318]], '78ms-RKSJ-V',
'CNS-EUC-V': [[0, 128], [41377, 65278], [], '83pv-RKSJ-H',
[2392957345, 2392981246, 2393022881, 2393046782, 2393088417, 2393112318]], '90ms-RKSJ-H',
'CNS1-H': [[], [8481, 32382]], '90ms-RKSJ-V',
'CNS1-V': [[], [8481, 32382]], '90msp-RKSJ-H',
'CNS2-H': [[], [8481, 32382]], '90msp-RKSJ-V',
'CNS2-V': [[], [8481, 32382]], '90pv-RKSJ-H',
'ETen-B5-H': [[0, 128], [41280, 65278]], '90pv-RKSJ-V',
'ETen-B5-V': [[0, 128], [41280, 65278]], 'Add-H',
'ETenms-B5-H': [[0, 128], [41280, 65278]], 'Add-RKSJ-H',
'ETenms-B5-V': [[0, 128], [41280, 65278]], 'Add-RKSJ-V',
'ETHK-B5-H': [[0, 128], [34624, 65278]], 'Add-V',
'ETHK-B5-V': [[0, 128], [34624, 65278]], 'Adobe-CNS1-0',
'HKdla-B5-H': [[0, 128], [41280, 65278]], 'Adobe-CNS1-1',
'HKdla-B5-V': [[0, 128], [41280, 65278]], 'Adobe-CNS1-2',
'HKdlb-B5-H': [[0, 128], [36416, 65278]], 'Adobe-CNS1-3',
'HKdlb-B5-V': [[0, 128], [36416, 65278]], 'Adobe-CNS1-4',
'HKgccs-B5-H': [[0, 128], [35392, 65278]], 'Adobe-CNS1-5',
'HKgccs-B5-V': [[0, 128], [35392, 65278]], 'Adobe-CNS1-6',
'HKm314-B5-H': [[0, 128], [41280, 65278]], 'Adobe-GB1-0',
'HKm314-B5-V': [[0, 128], [41280, 65278]], 'Adobe-GB1-1',
'HKm471-B5-H': [[0, 128], [41280, 65278]], 'Adobe-GB1-2',
'HKm471-B5-V': [[0, 128], [41280, 65278]], 'Adobe-GB1-3',
'HKscs-B5-H': [[0, 128], [34624, 65278]], 'Adobe-GB1-4',
'HKscs-B5-V': [[0, 128], [34624, 65278]], 'Adobe-GB1-5',
'UniCNS-UCS2-H': [[], [0, 55295, 57344, 65535]], 'Adobe-Japan1-0',
'UniCNS-UCS2-V': [[], [0, 55295, 57344, 65535]], 'Adobe-Japan1-1',
'UniCNS-UTF16-H': [[], [0, 55295, 57344, 65535], [], 'Adobe-Japan1-2',
[3623934976, 3690979327]], 'Adobe-Japan1-3',
'UniCNS-UTF16-V': [[], [0, 55295, 57344, 65535], [], 'Adobe-Japan1-4',
[3623934976, 3690979327]], 'Adobe-Japan1-5',
'Adobe-GB1-0': [[], [0, 7935]], 'Adobe-Japan1-6',
'Adobe-GB1-1': [[], [0, 9983]], 'Adobe-Korea1-0',
'Adobe-GB1-2': [[], [0, 22271]], 'Adobe-Korea1-1',
'Adobe-GB1-3': [[], [0, 22527]], 'Adobe-Korea1-2',
'Adobe-GB1-4': [[], [0, 29183]], 'B5-H',
'Adobe-GB1-5': [[], [0, 30463]], 'B5-V',
'Adobe-GB1-UCS2': [[], [0, 65535]], 'B5pc-H',
'GB-EUC-H': [[0, 128], [41377, 65278]], 'B5pc-V',
'GB-EUC-V': [[0, 128], [41377, 65278]], 'CNS-EUC-H',
'GB-H': [[], [8481, 32382]], 'CNS-EUC-V',
'GB-V': [[], [8481, 32382]], 'CNS1-H',
'GBK-EUC-H': [[0, 128], [33088, 65278]], 'CNS1-V',
'GBK-EUC-V': [[0, 128], [33088, 65278]], 'CNS2-H',
'GBK2K-H': [[0, 127], [33088, 65278], [], [2167439664, 4265213497]], 'CNS2-V',
'GBK2K-V': [[0, 127], [33088, 65278], [], [2167439664, 4265213497]], 'ETHK-B5-H',
'GBKp-EUC-H': [[0, 128], [33088, 65278]], 'ETHK-B5-V',
'GBKp-EUC-V': [[0, 128], [33088, 65278]], 'ETen-B5-H',
'GBpc-EUC-H': [[0, 128, 253, 255], [41377, 64766]], 'ETen-B5-V',
'GBpc-EUC-V': [[0, 128, 253, 255], [41377, 64766]], 'ETenms-B5-H',
'GBT-EUC-H': [[0, 128], [41377, 65278]], 'ETenms-B5-V',
'GBT-EUC-V': [[0, 128], [41377, 65278]], 'EUC-H',
'GBT-H': [[], [8481, 32382]], 'EUC-V',
'GBT-V': [[], [8481, 32382]], 'Ext-H',
'GBTpc-EUC-H': [[0, 128, 253, 255], [41377, 64766]], 'Ext-RKSJ-H',
'GBTpc-EUC-V': [[0, 128, 253, 255], [41377, 64766]], 'Ext-RKSJ-V',
'UniGB-UCS2-H': [[], [0, 55295, 57344, 65535]], 'Ext-V',
'UniGB-UCS2-V': [[], [0, 55295, 57344, 65535]], 'GB-EUC-H',
'UniGB-UTF16-H': [[], [0, 55295, 57344, 65535], [], [3623934976, 3690979327]], 'GB-EUC-V',
'UniGB-UTF16-V': [[], [0, 55295, 57344, 65535], [], [3623934976, 3690979327]], 'GB-H',
'78-EUC-H': [[0, 128], [36512, 36575, 41377, 65278]], 'GB-V',
'78-EUC-V': [[0, 128], [36512, 36575, 41377, 65278]], 'GBK-EUC-H',
'78-H': [[], [8481, 32382]], 'GBK-EUC-V',
'78-RKSJ-H': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'GBK2K-H',
'78-RKSJ-V': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'GBK2K-V',
'78-V': [[], [8481, 32382]], 'GBKp-EUC-H',
'78ms-RKSJ-H': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'GBKp-EUC-V',
'78ms-RKSJ-V': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'GBT-EUC-H',
'83pv-RKSJ-H': [[0, 128, 160, 223, 253, 255], [33088, 40956, 57408, 64764]], 'GBT-EUC-V',
'90ms-RKSJ-H': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'GBT-H',
'90ms-RKSJ-V': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'GBT-V',
'90msp-RKSJ-H': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'GBTpc-EUC-H',
'90msp-RKSJ-V': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'GBTpc-EUC-V',
'90pv-RKSJ-H': [[0, 128, 160, 223, 253, 255], [33088, 40956, 57408, 64764]], 'GBpc-EUC-H',
'90pv-RKSJ-V': [[0, 128, 160, 223, 253, 255], [33088, 40956, 57408, 64764]], 'GBpc-EUC-V',
'Add-H': [[], [8481, 32382]], 'H',
'Add-RKSJ-H': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'HKdla-B5-H',
'Add-RKSJ-V': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'HKdla-B5-V',
'Add-V': [[], [8481, 32382]], 'HKdlb-B5-H',
'Adobe-Japan1-0': [[], [0, 8447]], 'HKdlb-B5-V',
'Adobe-Japan1-1': [[], [0, 8447]], 'HKgccs-B5-H',
'Adobe-Japan1-2': [[], [0, 8959]], 'HKgccs-B5-V',
'Adobe-Japan1-3': [[], [0, 9471]], 'HKm314-B5-H',
'Adobe-Japan1-4': [[], [0, 15615]], 'HKm314-B5-V',
'Adobe-Japan1-5': [[], [0, 20479]], 'HKm471-B5-H',
'Adobe-Japan1-6': [[], [0, 23295]], 'HKm471-B5-V',
'Adobe-Japan1-UCS2': [[], [0, 65535]], 'HKscs-B5-H',
'Adobe-Japan2-0': [[], [0, 6143]], 'HKscs-B5-V',
'EUC-H': [[0, 128], [36512, 36575, 41377, 65278]], 'Hankaku',
'EUC-V': [[0, 128], [36512, 36575, 41377, 65278]], 'Hiragana',
'Ext-H': [[], [8481, 32382]], 'KSC-EUC-H',
'Ext-RKSJ-H': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'KSC-EUC-V',
'Ext-RKSJ-V': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'KSC-H',
'Ext-V': [[], [8481, 32382]], 'KSC-Johab-H',
'H': [[], [8481, 32382]], 'KSC-Johab-V',
'Hankaku': [[0, 255], []], 'KSC-V',
'Hiragana': [[0, 255], []], 'KSCms-UHC-H',
'Hojo-EUC-H': [[], [], [9413025, 9436926], []], 'KSCms-UHC-HW-H',
'Hojo-EUC-V': [[], [], [9413025, 9436926], []], 'KSCms-UHC-HW-V',
'Hojo-H': [[], [8481, 32382]], 'KSCms-UHC-V',
'Hojo-V': [[], [8481, 32382]], 'KSCpc-EUC-H',
'Katakana': [[0, 255], []], 'KSCpc-EUC-V',
'NWP-H': [[], [8481, 32382]], 'Katakana',
'NWP-V': [[], [8481, 32382]], 'NWP-H',
'RKSJ-H': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'NWP-V',
'RKSJ-V': [[0, 128, 160, 223], [33088, 40956, 57408, 64764]], 'RKSJ-H',
'Roman': [[0, 255], []], 'RKSJ-V',
'UniHojo-UCS2-H': [[], [0, 55295, 57344, 65535]], 'Roman',
'UniHojo-UCS2-V': [[], [0, 55295, 57344, 65535]], 'UniCNS-UCS2-H',
'UniHojo-UTF16-H': [[], [0, 55295, 57344, 65535], [], 'UniCNS-UCS2-V',
[3623934976, 3690979327]], 'UniCNS-UTF16-H',
'UniHojo-UTF16-V': [[], [0, 55295, 57344, 65535], [], 'UniCNS-UTF16-V',
[3623934976, 3690979327]], 'UniCNS-UTF32-H',
'UniJIS-UCS2-H': [[], [0, 55295, 57344, 65535]], 'UniCNS-UTF32-V',
'UniJIS-UCS2-HW-H': [[], [0, 55295, 57344, 65535]], 'UniCNS-UTF8-H',
'UniJIS-UCS2-HW-V': [[], [0, 55295, 57344, 65535]], 'UniCNS-UTF8-V',
'UniJIS-UCS2-V': [[], [0, 55295, 57344, 65535]], 'UniGB-UCS2-H',
'UniJIS-UTF16-H': [[], [0, 55295, 57344, 65535], [], 'UniGB-UCS2-V',
[3623934976, 3690979327]], 'UniGB-UTF16-H',
'UniJIS-UTF16-V': [[], [0, 55295, 57344, 65535], [], 'UniGB-UTF16-V',
[3623934976, 3690979327]], 'UniGB-UTF32-H',
'UniJISPro-UCS2-HW-V': [[], [0, 55295, 57344, 65535]], 'UniGB-UTF32-V',
'UniJISPro-UCS2-V': [[], [0, 55295, 57344, 65535]], 'UniGB-UTF8-H',
'V': [[], [8481, 32382]], 'UniGB-UTF8-V',
'WP-Symbol': [[0, 255], []], 'UniJIS-UCS2-H',
'Adobe-Korea1-0': [[], [0, 9471]], 'UniJIS-UCS2-HW-H',
'Adobe-Korea1-1': [[], [0, 18175]], 'UniJIS-UCS2-HW-V',
'Adobe-Korea1-2': [[], [0, 18431]], 'UniJIS-UCS2-V',
'Adobe-Korea1-UCS2': [[], [0, 65535]], 'UniJIS-UTF16-H',
'KSC-EUC-H': [[0, 128], [41377, 65278]], 'UniJIS-UTF16-V',
'KSC-EUC-V': [[0, 128], [41377, 65278]], 'UniJIS-UTF32-H',
'KSC-H': [[], [8481, 32382]], 'UniJIS-UTF32-V',
'KSC-Johab-H': [[0, 128], [33857, 54270, 55345, 57086, 57393, 63998]], 'UniJIS-UTF8-H',
'KSC-Johab-V': [[0, 128], [33857, 54270, 55345, 57086, 57393, 63998]], 'UniJIS-UTF8-V',
'KSC-V': [[], [8481, 32382]], 'UniJIS2004-UTF16-H',
'KSCms-UHC-H': [[0, 128], [33089, 65278]], 'UniJIS2004-UTF16-V',
'KSCms-UHC-HW-H': [[0, 128], [33089, 65278]], 'UniJIS2004-UTF32-H',
'KSCms-UHC-HW-V': [[0, 128], [33089, 65278]], 'UniJIS2004-UTF32-V',
'KSCms-UHC-V': [[0, 128], [33089, 65278]], 'UniJIS2004-UTF8-H',
'KSCpc-EUC-H': [[0, 132, 254, 255], [41281, 65022]], 'UniJIS2004-UTF8-V',
'KSCpc-EUC-V': [[0, 132, 254, 255], [41281, 65022]], 'UniJISPro-UCS2-HW-V',
'UniKS-UCS2-H': [[], [0, 55295, 57344, 65535]], 'UniJISPro-UCS2-V',
'UniKS-UCS2-V': [[], [0, 55295, 57344, 65535]], 'UniJISPro-UTF8-V',
'UniKS-UTF16-H': [[], [0, 55295, 57344, 65535], [], [3623934976, 3690979327]], 'UniJISX0213-UTF32-H',
'UniKS-UTF16-V': [[], [0, 55295, 57344, 65535], [], [3623934976, 3690979327]] 'UniJISX0213-UTF32-V',
}; 'UniJISX02132004-UTF32-H',
'UniJISX02132004-UTF32-V',
'UniKS-UCS2-H',
'UniKS-UCS2-V',
'UniKS-UTF16-H',
'UniKS-UTF16-V',
'UniKS-UTF32-H',
'UniKS-UTF32-V',
'UniKS-UTF8-H',
'UniKS-UTF8-V',
'V',
'WP-Symbol'];
// CMap, not to be confused with TrueType's cmap. // CMap, not to be confused with TrueType's cmap.
var CMap = (function CMapClosure() { var CMap = (function CMapClosure() {
function CMap() { function CMap(builtInCMap) {
// Codespace ranges are stored as follows: // Codespace ranges are stored as follows:
// [[1BytePairs], [2BytePairs], [3BytePairs], [4BytePairs]] // [[1BytePairs], [2BytePairs], [3BytePairs], [4BytePairs]]
// where nBytePairs are ranges e.g. [low1, high1, low2, high2, ...] // where nBytePairs are ranges e.g. [low1, high1, low2, high2, ...]
this.codespaceRanges = [[], [], [], []]; this.codespaceRanges = [[], [], [], []];
this.numCodespaceRanges = 0;
this.map = []; this.map = [];
this.vertical = false; this.vertical = false;
this.useCMap = null;
this.builtInCMap = builtInCMap;
} }
CMap.prototype = { CMap.prototype = {
addCodespaceRange: function(n, low, high) { addCodespaceRange: function(n, low, high) {
this.codespaceRanges[n - 1].push(low, high); this.codespaceRanges[n - 1].push(low, high);
this.numCodespaceRanges++;
}, },
mapRange: function(low, high, dstLow) { mapRange: function(low, high, dstLow) {
@ -395,17 +410,33 @@ var CMapFactory = (function CMapFactoryClosure() {
error('Invalid codespace range.'); error('Invalid codespace range.');
} }
function parseCmap(cMap, lexer) { function parseWMode(cMap, lexer) {
var obj = lexer.getObj();
if (isInt(obj)) {
cMap.vertical = !!obj;
}
}
function parseCMap(cMap, lexer, builtInCMapUrl, useCMap) {
var previous;
var embededUseCMap;
objLoop: while (true) { objLoop: while (true) {
var obj = lexer.getObj(); var obj = lexer.getObj();
if (isEOF(obj)) { if (isEOF(obj)) {
break; break;
} else if (isName(obj)) {
if (obj.name === 'WMode') {
parseWMode(cMap, lexer);
}
previous = obj;
} else if (isCmd(obj)) { } else if (isCmd(obj)) {
switch (obj.cmd) { switch (obj.cmd) {
case 'endcMap': case 'endcmap':
break objLoop; break objLoop;
case 'usecMap': case 'usecmap':
// TODO if (isName(previous)) {
embededUseCMap = previous.name;
}
break; break;
case 'begincodespacerange': case 'begincodespacerange':
parseCodespaceRange(cMap, lexer); parseCodespaceRange(cMap, lexer);
@ -425,30 +456,66 @@ var CMapFactory = (function CMapFactoryClosure() {
} }
} }
} }
if (!useCMap && embededUseCMap) {
// Load the usecmap definition from the file only if there wasn't one
// specified.
useCMap = embededUseCMap;
}
if (useCMap) {
cMap.useCMap = createBuiltInCMap(useCMap, builtInCMapUrl);
// If there aren't any code space ranges defined clone all the parent ones
// into this cMap.
if (cMap.numCodespaceRanges === 0) {
var useCodespaceRanges = cMap.useCMap.codespaceRanges;
for (var i = 0; i < useCodespaceRanges.length; i++) {
cMap.codespaceRanges[i] = useCodespaceRanges[i].slice();
}
cMap.numCodespaceRanges = cMap.useCMap.numCodespaceRanges;
}
// Merge the map into the current one, making sure not to override
// any previously defined entries.
for (var key in cMap.useCMap.map) {
if (key in cMap.map) {
continue;
}
cMap.map[key] = cMap.useCMap.map[key];
}
}
}
function createBuiltInCMap(name, builtInCMapUrl) {
if (name === 'Identity-H') {
return new IdentityCMap(false, 2);
} else if (name === 'Identity-V') {
return new IdentityCMap(true, 2);
}
if (BUILT_IN_CMAPS.indexOf(name) === -1) {
error('Unknown cMap name: ' + name);
}
var request = new XMLHttpRequest();
var url = builtInCMapUrl + name;
request.open('GET', url, false);
request.send(null);
if (request.status === 0 && /^https?:/i.test(url)) {
error('Unable to get cMap at: ' + url);
}
var cMap = new CMap(true);
var lexer = new Lexer(new StringStream(request.responseText));
parseCMap(cMap, lexer, builtInCMapUrl, null);
return cMap;
} }
return { return {
create: function (encoding) { create: function (encoding, builtInCMapUrl, useCMap) {
if (isName(encoding)) { if (isName(encoding)) {
switch (encoding.name) { return createBuiltInCMap(encoding.name, builtInCMapUrl);
case 'Identity-H':
return new IdentityCMap(false, 2);
case 'Identity-V':
return new IdentityCMap(true, 2);
default:
if (encoding.name in CMAP_CODESPACES) {
// XXX: Temporary hack so the correct amount of bytes are read in
// CMap.readCharCode.
var cMap = new CMap();
cMap.codespaceRanges = CMAP_CODESPACES[encoding.name];
return cMap;
}
return null;
}
} else if (isStream(encoding)) { } else if (isStream(encoding)) {
var cMap = new CMap(); var cMap = new CMap();
var lexer = new Lexer(encoding); var lexer = new Lexer(encoding);
try { try {
parseCmap(cMap, lexer); parseCMap(cMap, lexer, builtInCMapUrl, useCMap);
} catch (e) { } catch (e) {
warn('Invalid CMap data. ' + e); warn('Invalid CMap data. ' + e);
} }

118
src/core/evaluator.js

@ -828,41 +828,21 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
} }
// Based on 9.6.6 of the spec the encoding can come from multiple places // Based on 9.6.6 of the spec the encoding can come from multiple places
// but should be prioritized in the following order: // and depends on the font type. The base encoding and differences are
// 1. Encoding dictionary // read here, but the encoding that is actually used is chosen during
// 2. Encoding within font file (Type1 or Type1C) // glyph mapping in the font.
// 3. Default (depends on font type) // TODO: Loading the built in encoding in the font would allow the
// Differences applied to the above. // differences to be merged in here not require us to hold on to it.
// Note: we don't fill in the encoding from the font file(2) here but use
// the flag overridableEncoding to signal that the font can override the
// encoding if it has one built in.
var overridableEncoding = true;
var hasEncoding = false;
var flags = properties.flags;
var differences = []; var differences = [];
var baseEncoding = properties.type === 'TrueType' ? var baseEncodingName = null;
Encodings.WinAnsiEncoding :
Encodings.StandardEncoding;
// The Symbolic attribute can be misused for regular fonts
// Heuristic: we have to check if the font is a standard one and has
// Symbolic font name
if (!!(flags & FontFlags.Symbolic)) {
baseEncoding = !properties.file && /Symbol/i.test(properties.name) ?
Encodings.SymbolSetEncoding : Encodings.MacRomanEncoding;
}
if (dict.has('Encoding')) { if (dict.has('Encoding')) {
var encoding = dict.get('Encoding'); var encoding = dict.get('Encoding');
if (isDict(encoding)) { if (isDict(encoding)) {
var baseName = encoding.get('BaseEncoding'); baseEncodingName = encoding.get('BaseEncoding');
if (baseName) { baseEncodingName = isName(baseEncodingName) ? baseEncodingName.name :
overridableEncoding = false; null;
hasEncoding = true;
baseEncoding = Encodings[baseName.name];
}
// Load the differences between the base and original // Load the differences between the base and original
if (encoding.has('Differences')) { if (encoding.has('Differences')) {
hasEncoding = true;
var diffEncoding = encoding.get('Differences'); var diffEncoding = encoding.get('Differences');
var index = 0; var index = 0;
for (var j = 0, jj = diffEncoding.length; j < jj; j++) { for (var j = 0, jj = diffEncoding.length; j < jj; j++) {
@ -874,38 +854,44 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
} }
} }
} else if (isName(encoding)) { } else if (isName(encoding)) {
overridableEncoding = false; baseEncodingName = encoding.name;
hasEncoding = true;
var currentEncoding = Encodings[encoding.name];
// Some bad PDF files contain fonts whose encoding name is not among
// the predefined encodings, causing baseEncoding to be undefined.
// In this case, fallback to using the baseEncoding as defined above
// and let the font override the encoding if one is available.
if (currentEncoding) {
baseEncoding = currentEncoding;
} else {
overridableEncoding = true;
}
} else { } else {
error('Encoding is not a Name nor a Dict'); error('Encoding is not a Name nor a Dict');
} }
// According to table 114 if the encoding is a named encoding it must be
// one of these predefined encodings.
if ((baseEncodingName !== 'MacRomanEncoding' &&
baseEncodingName !== 'MacExpertEncoding' &&
baseEncodingName !== 'WinAnsiEncoding')) {
baseEncodingName = null;
}
}
if (baseEncodingName) {
properties.defaultEncoding = Encodings[baseEncodingName].slice();
} else {
var encoding = properties.type === 'TrueType' ?
Encodings.WinAnsiEncoding :
Encodings.StandardEncoding;
// The Symbolic attribute can be misused for regular fonts
// Heuristic: we have to check if the font is a standard one also
if (!!(properties.flags & FontFlags.Symbolic)) {
encoding = !properties.file && /Symbol/i.test(properties.name) ?
Encodings.SymbolSetEncoding : Encodings.MacRomanEncoding;
}
properties.defaultEncoding = encoding;
} }
properties.differences = differences; properties.differences = differences;
properties.baseEncoding = baseEncoding; properties.baseEncodingName = baseEncodingName;
properties.hasEncoding = hasEncoding; properties.dict = dict;
properties.overridableEncoding = overridableEncoding;
}, },
readToUnicode: function PartialEvaluator_readToUnicode(toUnicode, xref, readToUnicode: function PartialEvaluator_readToUnicode(toUnicode) {
properties) {
var cmapObj = toUnicode; var cmapObj = toUnicode;
var charToUnicode = []; var charToUnicode = [];
if (isName(cmapObj)) { if (isName(cmapObj)) {
var isIdentityMap = cmapObj.name.substr(0, 9) == 'Identity-'; return CMapFactory.create(cmapObj).map;
if (!isIdentityMap)
error('ToUnicode file cmap translation not implemented');
} else if (isStream(cmapObj)) { } else if (isStream(cmapObj)) {
var cmap = CMapFactory.create(cmapObj).map; var cmap = CMapFactory.create(cmapObj).map;
// Convert UTF-16BE // Convert UTF-16BE
@ -927,7 +913,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
}); });
return cmap; return cmap;
} }
return charToUnicode; return null;
}, },
readCidToGidMap: function PartialEvaluator_readCidToGidMap(cidToGidStream) { readCidToGidMap: function PartialEvaluator_readCidToGidMap(cidToGidStream) {
// Extract the encoding from the CIDToGIDMap // Extract the encoding from the CIDToGIDMap
@ -1006,7 +992,8 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
if (isName(baseFontName)) { if (isName(baseFontName)) {
var metrics = this.getBaseFontMetrics(baseFontName.name); var metrics = this.getBaseFontMetrics(baseFontName.name);
glyphsWidths = metrics.widths; glyphsWidths = this.buildCharCodeToWidth(metrics.widths,
properties);
defaultWidth = metrics.defaultWidth; defaultWidth = metrics.defaultWidth;
} }
} }
@ -1074,6 +1061,25 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
}; };
}, },
buildCharCodeToWidth: function PartialEvaluator_bulildCharCodeToWidth(
widthsByGlyphName, properties) {
var widths = Object.create(null);
var differences = properties.differences;
var encoding = properties.defaultEncoding;
for (var charCode = 0; charCode < 256; charCode++) {
if (charCode in differences &&
widthsByGlyphName[differences[charCode]]) {
widths[charCode] = widthsByGlyphName[differences[charCode]];
continue;
}
if (charCode in encoding && widthsByGlyphName[encoding[charCode]]) {
widths[charCode] = widthsByGlyphName[encoding[charCode]];
continue;
}
}
return widths;
},
translateFont: function PartialEvaluator_translateFont(dict, translateFont: function PartialEvaluator_translateFont(dict,
xref) { xref) {
var baseDict = dict; var baseDict = dict;
@ -1135,6 +1141,8 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
lastChar: maxCharIndex lastChar: maxCharIndex
}; };
this.extractDataStructures(dict, dict, xref, properties); this.extractDataStructures(dict, dict, xref, properties);
properties.widths = this.buildCharCodeToWidth(metrics.widths,
properties);
return new Font(baseFontName, null, properties); return new Font(baseFontName, null, properties);
} }
@ -1212,12 +1220,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var cidEncoding = baseDict.get('Encoding'); var cidEncoding = baseDict.get('Encoding');
if (isName(cidEncoding)) { if (isName(cidEncoding)) {
properties.cidEncoding = cidEncoding.name; properties.cidEncoding = cidEncoding.name;
properties.vertical = /-V$/.test(cidEncoding.name);
} }
properties.cmap = CMapFactory.create(cidEncoding); properties.cMap = CMapFactory.create(cidEncoding, PDFJS.cMapUrl, null);
properties.vertical = properties.cMap.vertical;
} }
this.extractWidths(dict, xref, descriptor, properties);
this.extractDataStructures(dict, baseDict, xref, properties); this.extractDataStructures(dict, baseDict, xref, properties);
this.extractWidths(dict, xref, descriptor, properties);
if (type.name === 'Type3') { if (type.name === 'Type3') {
properties.coded = true; properties.coded = true;

1346
src/core/fonts.js

File diff suppressed because it is too large Load Diff

2
src/core/worker.js

@ -239,6 +239,8 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
PDFJS.disableFontFace = data.disableFontFace; PDFJS.disableFontFace = data.disableFontFace;
PDFJS.disableCreateObjectURL = data.disableCreateObjectURL; PDFJS.disableCreateObjectURL = data.disableCreateObjectURL;
PDFJS.verbosity = data.verbosity; PDFJS.verbosity = data.verbosity;
PDFJS.cMapUrl = data.cMapUrl === undefined ?
null : data.cMapUrl;
getPdfManager(data).then(function () { getPdfManager(data).then(function () {
pdfManager.onLoadedStream().then(function(stream) { pdfManager.onLoadedStream().then(function(stream) {

8
src/display/api.js

@ -30,6 +30,13 @@
PDFJS.maxImageSize = PDFJS.maxImageSize === undefined ? -1 : PDFJS.maxImageSize; PDFJS.maxImageSize = PDFJS.maxImageSize === undefined ? -1 : PDFJS.maxImageSize;
/** /**
* The url of where the predefined Adobe CMaps are located. Include trailing
* slash.
* @var {string}
*/
PDFJS.cMapUrl = PDFJS.cMapUrl === undefined ? null : PDFJS.cMapUrl;
/*
* By default fonts are converted to OpenType fonts and loaded via font face * By default fonts are converted to OpenType fonts and loaded via font face
* rules. If disabled, the font will be rendered using a built in font renderer * rules. If disabled, the font will be rendered using a built in font renderer
* that constructs the glyphs with primitive path commands. * that constructs the glyphs with primitive path commands.
@ -917,6 +924,7 @@ var WorkerTransport = (function WorkerTransportClosure() {
source: source, source: source,
disableRange: PDFJS.disableRange, disableRange: PDFJS.disableRange,
maxImageSize: PDFJS.maxImageSize, maxImageSize: PDFJS.maxImageSize,
cMapUrl: PDFJS.cMapUrl,
disableFontFace: PDFJS.disableFontFace, disableFontFace: PDFJS.disableFontFace,
disableCreateObjectURL: PDFJS.disableCreateObjectURL, disableCreateObjectURL: PDFJS.disableCreateObjectURL,
verbosity: PDFJS.verbosity verbosity: PDFJS.verbosity

51
src/display/canvas.js

@ -1383,38 +1383,37 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var accent = glyph.accent; var accent = glyph.accent;
var scaledX, scaledY, scaledAccentX, scaledAccentY; var scaledX, scaledY, scaledAccentX, scaledAccentY;
if (!glyph.disabled) {
if (vertical) {
scaledX = vx / fontSizeScale;
scaledY = (x + vy) / fontSizeScale;
} else {
scaledX = x / fontSizeScale;
scaledY = 0;
}
if (font.remeasure && width > 0 && this.isFontSubpixelAAEnabled) { if (vertical) {
// some standard fonts may not have the exact width, trying to scaledX = vx / fontSizeScale;
// rescale per character scaledY = (x + vy) / fontSizeScale;
var measuredWidth = ctx.measureText(character).width * 1000 / } else {
current.fontSize * current.fontSizeScale; scaledX = x / fontSizeScale;
var characterScaleX = width / measuredWidth; scaledY = 0;
restoreNeeded = true; }
ctx.save();
ctx.scale(characterScaleX, 1);
scaledX /= characterScaleX;
if (accent) {
scaledAccentX /= characterScaleX;
}
}
this.paintChar(character, scaledX, scaledY); if (font.remeasure && width > 0 && this.isFontSubpixelAAEnabled) {
// some standard fonts may not have the exact width, trying to
// rescale per character
var measuredWidth = ctx.measureText(character).width * 1000 /
current.fontSize * current.fontSizeScale;
var characterScaleX = width / measuredWidth;
restoreNeeded = true;
ctx.save();
ctx.scale(characterScaleX, 1);
scaledX /= characterScaleX;
if (accent) { if (accent) {
scaledAccentX = scaledX + accent.offset.x / fontSizeScale; scaledAccentX /= characterScaleX;
scaledAccentY = scaledY - accent.offset.y / fontSizeScale;
this.paintChar(accent.fontChar, scaledAccentX, scaledAccentY);
} }
} }
this.paintChar(character, scaledX, scaledY);
if (accent) {
scaledAccentX = scaledX + accent.offset.x / fontSizeScale;
scaledAccentY = scaledY - accent.offset.y / fontSizeScale;
this.paintChar(accent.fontChar, scaledAccentX, scaledAccentY);
}
x += charWidth; x += charWidth;
canvasWidth += charWidth; canvasWidth += charWidth;

1
test/driver.js

@ -28,6 +28,7 @@
// "firefox-bin: Fatal IO error 12 (Cannot allocate memory) on X server :1." // "firefox-bin: Fatal IO error 12 (Cannot allocate memory) on X server :1."
// PDFJS.disableWorker = true; // PDFJS.disableWorker = true;
PDFJS.enableStats = true; PDFJS.enableStats = true;
PDFJS.cMapUrl = '../external/cmaps/';
var appPath, masterMode, browser, canvas, dummyCanvas, currentTaskIdx, var appPath, masterMode, browser, canvas, dummyCanvas, currentTaskIdx,
manifest, stdout; manifest, stdout;

4
test/font/font_fpgm_spec.js

@ -9,7 +9,9 @@ describe('font_fpgm', function() {
var font = new Font("font", new Stream(font2324), { var font = new Font("font", new Stream(font2324), {
loadedName: 'font', loadedName: 'font',
type: 'CIDFontType2', type: 'CIDFontType2',
differences: [] differences: [],
defaultEncoding: [],
cMap: CMapFactory.create(new Name('Identity-H'))
}); });
ttx(font.data, function(result) { output = result; }); ttx(font.data, function(result) { output = result; });
runs(function() { runs(function() {

6
test/font/font_os2_spec.js

@ -11,7 +11,7 @@ describe('font_post', function() {
loadedName: 'font', loadedName: 'font',
type: 'TrueType', type: 'TrueType',
differences: [], differences: [],
baseEncoding: [] defaultEncoding: []
}); });
ttx(font.data, function(result) { output = result; }); ttx(font.data, function(result) { output = result; });
runs(function() { runs(function() {
@ -26,7 +26,9 @@ describe('font_post', function() {
var font = new Font("font", new Stream(font1282), { var font = new Font("font", new Stream(font1282), {
loadedName: 'font', loadedName: 'font',
type: 'CIDFontType2', type: 'CIDFontType2',
differences: [] differences: [],
defaultEncoding: [],
cMap: CMapFactory.create(new Name('Identity-H'))
}); });
ttx(font.data, function(result) { output = result; }); ttx(font.data, function(result) { output = result; });
runs(function() { runs(function() {

7
test/font/font_post_spec.js

@ -12,7 +12,8 @@ describe('font_post', function() {
loadedName: 'font', loadedName: 'font',
type: 'CIDFontType2', type: 'CIDFontType2',
differences: [], differences: [],
baseEncoding: [] defaultEncoding: [],
cMap: CMapFactory.create(new Name('Identity-H'))
}); });
ttx(font.data, function(result) { output = result; }); ttx(font.data, function(result) { output = result; });
runs(function() { runs(function() {
@ -28,7 +29,7 @@ describe('font_post', function() {
loadedName: 'font', loadedName: 'font',
type: 'TrueType', type: 'TrueType',
differences: [], differences: [],
baseEncoding: [] defaultEncoding: []
}); });
ttx(font.data, function(result) { output = result; }); ttx(font.data, function(result) { output = result; });
runs(function() { runs(function() {
@ -44,7 +45,7 @@ describe('font_post', function() {
loadedName: 'font', loadedName: 'font',
type: 'TrueType', type: 'TrueType',
differences: [], differences: [],
baseEncoding: [] defaultEncoding: []
}); });
ttx(font.data, function(result) { output = result; }); ttx(font.data, function(result) { output = result; });
runs(function() { runs(function() {

1
test/font/font_test.html

@ -28,6 +28,7 @@
<script type="text/javascript" src="../../src/core/crypto.js"></script> <script type="text/javascript" src="../../src/core/crypto.js"></script>
<script type="text/javascript" src="../../src/core/pattern.js"></script> <script type="text/javascript" src="../../src/core/pattern.js"></script>
<script type="text/javascript" src="../../src/core/evaluator.js"></script> <script type="text/javascript" src="../../src/core/evaluator.js"></script>
<script type="text/javascript" src="../../src/core/cmap.js"></script>
<script type="text/javascript" src="../../src/core/fonts.js"></script> <script type="text/javascript" src="../../src/core/fonts.js"></script>
<script type="text/javascript" src="../../src/core/glyphlist.js"></script> <script type="text/javascript" src="../../src/core/glyphlist.js"></script>
<script type="text/javascript" src="../../src/core/image.js"></script> <script type="text/javascript" src="../../src/core/image.js"></script>

5
test/test.py

@ -220,6 +220,11 @@ class TestHandlerBase(BaseHTTPRequestHandler):
self.sendIndex(url.path, url.query) self.sendIndex(url.path, url.query)
return return
pieces = path.split(os.sep);
if pieces[len(pieces) - 2] == 'cmaps':
self.sendFile(path, '.properties');
return
if not (prefix == DOC_ROOT if not (prefix == DOC_ROOT
and os.path.isfile(path) and os.path.isfile(path)
and ext in MIMEs): and ext in MIMEs):

19
test/unit/cmap_spec.js

@ -1,6 +1,6 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* globals expect, it, describe, StringStream, Lexer, CMapFactory */ /* globals expect, it, describe, StringStream, Lexer, CMapFactory, Name */
'use strict'; 'use strict';
@ -82,5 +82,22 @@ describe('cmap', function() {
expect(c[0]).toEqual(0x8EA1A1A1); expect(c[0]).toEqual(0x8EA1A1A1);
expect(c[1]).toEqual(4); expect(c[1]).toEqual(4);
}); });
it('read usecmap', function() {
var str = '/Adobe-Japan1-1 usecmap\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream, null, '../../external/cmaps/');
expect(cmap.useCMap).toBeDefined();
});
it('parses wmode', function() {
var str = '/WMode 1 def\n';
var stream = new StringStream(str);
var cmap = CMapFactory.create(stream);
expect(cmap.vertical).toEqual(true);
});
it('loads built in cmap', function() {
var cmap = CMapFactory.create(new Name('Adobe-Japan1-1'),
'../../external/cmaps/',
null);
});
}); });

2
test/unit/font_spec.js

@ -386,7 +386,7 @@ describe('font', function() {
var parser = new Type1Parser(stream); var parser = new Type1Parser(stream);
var props = { overridableEncoding: true }; var props = { overridableEncoding: true };
var program = parser.extractFontHeader(props); var program = parser.extractFontHeader(props);
expect(props.baseEncoding[33]).toEqual('arrowright'); expect(props.builtInEncoding[33]).toEqual('arrowright');
}); });
}); });
}); });

5
web/viewer.js

@ -62,6 +62,11 @@ PDFJS.imageResourcesPath = './images/';
//#if (FIREFOX || MOZCENTRAL || B2G || GENERIC || CHROME) //#if (FIREFOX || MOZCENTRAL || B2G || GENERIC || CHROME)
//PDFJS.workerSrc = '../build/pdf.worker.js'; //PDFJS.workerSrc = '../build/pdf.worker.js';
//#endif //#endif
//#if !PRODUCTION
PDFJS.cMapUrl = '../external/cmaps/';
//#else
//PDFJS.cMapUrl = '../web/cmaps/';
//#endif
var mozL10n = document.mozL10n || document.webL10n; var mozL10n = document.mozL10n || document.webL10n;

Loading…
Cancel
Save