Merge pull request #4354 from nnethercote/Name-cache

Use a cache to minimize the number of Name objects.
This commit is contained in:
Yury Delendik 2014-03-02 18:44:29 -06:00
commit ad4eb9a21d
8 changed files with 18 additions and 14 deletions

View File

@ -548,7 +548,7 @@ var CipherTransformFactory = (function CipherTransformFactoryClosure() {
return userPassword; return userPassword;
} }
var identityName = new Name('Identity'); var identityName = Name.get('Identity');
function CipherTransformFactory(dict, fileId, password) { function CipherTransformFactory(dict, fileId, password) {
var filter = dict.get('Filter'); var filter = dict.get('Filter');

View File

@ -1098,7 +1098,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
// FontDescriptor is only required for Type3 fonts when the document // FontDescriptor is only required for Type3 fonts when the document
// is a tagged pdf. Create a barbebones one to get by. // is a tagged pdf. Create a barbebones one to get by.
descriptor = new Dict(); descriptor = new Dict();
descriptor.set('FontName', new Name(type.name)); descriptor.set('FontName', Name.get(type.name));
} else { } else {
// Before PDF 1.5 if the font was one of the base 14 fonts, having a // Before PDF 1.5 if the font was one of the base 14 fonts, having a
// FontDescriptor was not required. // FontDescriptor was not required.
@ -1146,10 +1146,10 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var baseFont = dict.get('BaseFont'); var baseFont = dict.get('BaseFont');
// Some bad pdf's have a string as the font name. // Some bad pdf's have a string as the font name.
if (isString(fontName)) { if (isString(fontName)) {
fontName = new Name(fontName); fontName = Name.get(fontName);
} }
if (isString(baseFont)) { if (isString(baseFont)) {
baseFont = new Name(baseFont); baseFont = Name.get(baseFont);
} }
if (type.name !== 'Type3') { if (type.name !== 'Type3') {

View File

@ -88,7 +88,7 @@ var PDFImage = (function PDFImageClosure() {
var colorSpace = dict.get('ColorSpace', 'CS'); var colorSpace = dict.get('ColorSpace', 'CS');
if (!colorSpace) { if (!colorSpace) {
warn('JPX images (which don"t require color spaces'); warn('JPX images (which don"t require color spaces');
colorSpace = new Name('DeviceRGB'); colorSpace = Name.get('DeviceRGB');
} }
this.colorSpace = ColorSpace.parse(colorSpace, xref, res); this.colorSpace = ColorSpace.parse(colorSpace, xref, res);
this.numComps = this.colorSpace.numComps; this.numComps = this.colorSpace.numComps;

View File

@ -30,6 +30,13 @@ var Name = (function NameClosure() {
Name.prototype = {}; Name.prototype = {};
var nameCache = {};
Name.get = function Name_get(name) {
var nameValue = nameCache[name];
return nameValue ? nameValue : (nameCache[name] = new Name(name));
};
return Name; return Name;
})(); })();
@ -44,10 +51,7 @@ var Cmd = (function CmdClosure() {
Cmd.get = function Cmd_get(cmd) { Cmd.get = function Cmd_get(cmd) {
var cmdValue = cmdCache[cmd]; var cmdValue = cmdCache[cmd];
if (cmdValue) return cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd));
return cmdValue;
return cmdCache[cmd] = new Cmd(cmd);
}; };
return Cmd; return Cmd;

View File

@ -586,7 +586,7 @@ var Lexer = (function LexerClosure() {
error('Warning: name token is longer than allowed by the spec: ' + error('Warning: name token is longer than allowed by the spec: ' +
strBuf.length); strBuf.length);
} }
return new Name(strBuf.join('')); return Name.get(strBuf.join(''));
}, },
getHexString: function Lexer_getHexString() { getHexString: function Lexer_getHexString() {
var strBuf = this.strBuf; var strBuf = this.strBuf;

View File

@ -198,7 +198,7 @@ describe('CipherTransformFactory', function() {
}; };
var map1 = { var map1 = {
Filter: new Name('Standard'), Filter: Name.get('Standard'),
V: 2, V: 2,
Length: 128, Length: 128,
O: unescape('%80%C3%04%96%91o%20sl%3A%E6%1B%13T%91%F2%0DV%12%E3%FF%5E%BB%' + O: unescape('%80%C3%04%96%91o%20sl%3A%E6%1B%13T%91%F2%0DV%12%E3%FF%5E%BB%' +
@ -211,7 +211,7 @@ describe('CipherTransformFactory', function() {
var fileID1 = unescape('%F6%C6%AF%17%F3rR%8DRM%9A%80%D1%EF%DF%18'); var fileID1 = unescape('%F6%C6%AF%17%F3rR%8DRM%9A%80%D1%EF%DF%18');
var map2 = { var map2 = {
Filter: new Name('Standard'), Filter: Name.get('Standard'),
V: 4, V: 4,
Length: 128, Length: 128,
O: unescape('sF%14v.y5%27%DB%97%0A5%22%B3%E1%D4%AD%BD%9B%3C%B4%A5%89u%15%' + O: unescape('sF%14v.y5%27%DB%97%0A5%22%B3%E1%D4%AD%BD%9B%3C%B4%A5%89u%15%' +

View File

@ -10,7 +10,7 @@ describe('obj', function() {
describe('Name', function() { describe('Name', function() {
it('should retain the given name', function() { it('should retain the given name', function() {
var givenName = 'Font'; var givenName = 'Font';
var name = new Name(givenName); var name = Name.get(givenName);
expect(name.name).toEqual(givenName); expect(name.name).toEqual(givenName);
}); });
}); });

View File

@ -72,7 +72,7 @@ describe('util', function() {
it('handles dictionaries with type check', function() { it('handles dictionaries with type check', function() {
var dict = new Dict(); var dict = new Dict();
dict.set('Type', new Name('Page')); dict.set('Type', Name.get('Page'));
expect(isDict(dict, 'Page')).toEqual(true); expect(isDict(dict, 'Page')).toEqual(true);
}); });
}); });