Skip to content

Commit

Permalink
Merge pull request mozilla#4519 from yurydelendik/pdfbugclean
Browse files Browse the repository at this point in the history
Cleaning up the viewer and pdfbug; fixes bugs in Stepper
  • Loading branch information
brendandahl committed Mar 27, 2014
2 parents 4ead351 + ed8ecfd commit a06fca1
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 39 deletions.
97 changes: 82 additions & 15 deletions web/debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ var FontInspector = (function FontInspectorClosure() {
fonts = document.createElement('div');
panel.appendChild(fonts);
},
cleanup: function cleanup() {
fonts.textContent = '';
},
enabled: false,
get active() {
return active;
Expand Down Expand Up @@ -181,6 +184,11 @@ var StepperManager = (function StepperManagerClosure() {
breakPoints = JSON.parse(sessionStorage.getItem('pdfjsBreakPoints'));
}
},
cleanup: function cleanup() {
stepperChooser.textContent = '';
stepperDiv.textContent = '';
steppers = [];
},
enabled: false,
active: false,
// Stepper specific functions.
Expand All @@ -204,7 +212,7 @@ var StepperManager = (function StepperManagerClosure() {
},
selectStepper: function selectStepper(pageIndex, selectPanel) {
if (selectPanel) {
this.manager.selectPanel(1);
this.manager.selectPanel(this);
}
for (var i = 0; i < steppers.length; ++i) {
var stepper = steppers[i];
Expand Down Expand Up @@ -259,6 +267,33 @@ var Stepper = (function StepperClosure() {
'nextLineSetSpacingShowText': 2
};

function simplifyArgs(args) {
if (typeof args === 'string') {
var MAX_STRING_LENGTH = 75;
return args.length <= MAX_STRING_LENGTH ? args :
args.substr(0, MAX_STRING_LENGTH) + '...';
}
if (typeof args !== 'object' || args === null) {
return args;
}
if ('length' in args) { // array
var simpleArgs = [], i, ii;
var MAX_ITEMS = 10;
for (i = 0, ii = Math.min(MAX_ITEMS, args.length); i < ii; i++) {
simpleArgs.push(simplifyArgs(args[i]));
}
if (i < args.length) {
simpleArgs.push('...');
}
return simpleArgs;
}
var simpleObj = {};
for (var key in args) {
simpleObj[key] = simplifyArgs(args[key]);
}
return simpleObj;
}

function Stepper(panel, pageIndex, initialBreakPoints) {
this.panel = panel;
this.breakPoint = 0;
Expand Down Expand Up @@ -291,30 +326,40 @@ var Stepper = (function StepperClosure() {
}
},
updateOperatorList: function updateOperatorList(operatorList) {
function cboxOnClick() {
var x = +this.dataset.idx;
if (this.checked) {
self.breakPoints.push(x);
} else {
self.breakPoints.splice(self.breakPoints.indexOf(x), 1);
}
StepperManager.saveBreakPoints(self.pageIndex, self.breakPoints);
}

var MAX_OPERATORS_COUNT = 15000;
if (this.operatorListIdx > MAX_OPERATORS_COUNT) {
return;
}

var self = this;
for (var i = this.operatorListIdx; i < operatorList.fnArray.length; i++) {
var chunk = document.createDocumentFragment();
var operatorsToDisplay = Math.min(MAX_OPERATORS_COUNT,
operatorList.fnArray.length);
for (var i = this.operatorListIdx; i < operatorsToDisplay; i++) {
var line = c('tr');
line.className = 'line';
line.dataset.idx = i;
this.table.appendChild(line);
chunk.appendChild(line);
var checked = this.breakPoints.indexOf(i) != -1;
var args = operatorList.argsArray[i] ? operatorList.argsArray[i] : [];
var args = operatorList.argsArray[i] || [];

var breakCell = c('td');
var cbox = c('input');
cbox.type = 'checkbox';
cbox.className = 'points';
cbox.checked = checked;
cbox.onclick = (function(x) {
return function() {
if (this.checked) {
self.breakPoints.push(x);
} else {
self.breakPoints.splice(self.breakPoints.indexOf(x), 1);
}
StepperManager.saveBreakPoints(self.pageIndex, self.breakPoints);
};
})(i);
cbox.dataset.idx = i;
cbox.onclick = cboxOnClick;

breakCell.appendChild(cbox);
line.appendChild(breakCell);
Expand All @@ -341,8 +386,16 @@ var Stepper = (function StepperClosure() {
decArgs[glyphIndex] = newArg;
}
line.appendChild(c('td', fn));
line.appendChild(c('td', JSON.stringify(decArgs)));
line.appendChild(c('td', JSON.stringify(simplifyArgs(decArgs))));
}
if (operatorsToDisplay < operatorList.fnArray.length) {
line = c('tr');
var lastCell = c('td', '...');
lastCell.colspan = 4;
chunk.appendChild(lastCell);
}
this.operatorListIdx = operatorList.fnArray.length;
this.table.appendChild(chunk);
},
getNextBreakPoint: function getNextBreakPoint() {
this.breakPoints.sort(function(a, b) { return a - b; });
Expand Down Expand Up @@ -447,6 +500,10 @@ var Stats = (function Stats() {
for (var i = 0, ii = stats.length; i < ii; ++i) {
this.panel.appendChild(stats[i].div);
}
},
cleanup: function () {
stats = [];
clear(this.panel);
}
};
})();
Expand Down Expand Up @@ -539,7 +596,17 @@ var PDFBug = (function PDFBugClosure() {
}
this.selectPanel(0);
},
cleanup: function cleanup() {
for (var i = 0, ii = this.tools.length; i < ii; i++) {
if (this.tools[i].enabled) {
this.tools[i].cleanup();
}
}
},
selectPanel: function selectPanel(index) {
if (typeof index !== 'number') {
index = this.tools.indexOf(index);
}
if (index === activePanel) {
return;
}
Expand Down
60 changes: 36 additions & 24 deletions web/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,43 @@ var PDFView = {
//#endif
},

close: function pdfViewClose() {
if (!this.pdfDocument) {
return;
}

this.pdfDocument.destroy();
this.pdfDocument = null;

var errorWrapper = document.getElementById('errorWrapper');
errorWrapper.setAttribute('hidden', 'true');

var thumbsView = document.getElementById('thumbnailView');
while (thumbsView.hasChildNodes()) {
thumbsView.removeChild(thumbsView.lastChild);
}

if ('_loadingInterval' in thumbsView) {
clearInterval(thumbsView._loadingInterval);
}

var container = document.getElementById('viewer');
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}

if (typeof PDFBug !== 'undefined') {
PDFBug.cleanup();
}
},

// TODO(mack): This function signature should really be pdfViewOpen(url, args)
open: function pdfViewOpen(url, scale, password,
pdfDataRangeTransport, args) {
if (this.pdfDocument) {
this.close();
}

var parameters = {password: password};
if (typeof url === 'string') { // URL
this.setTitleUsingUrl(url);
Expand All @@ -562,11 +596,6 @@ var PDFView = {
}
}

// Terminate worker of the previous document if any.
if (this.pdfDocument) {
this.pdfDocument.destroy();
}
this.pdfDocument = null;
var self = this;
self.loading = true;
var passwordNeeded = function passwordNeeded(updatePassword, reason) {
Expand Down Expand Up @@ -858,31 +887,12 @@ var PDFView = {

this.pdfDocument = pdfDocument;

var errorWrapper = document.getElementById('errorWrapper');
errorWrapper.setAttribute('hidden', 'true');

pdfDocument.getDownloadInfo().then(function() {
PDFView.loadingBar.hide();
var outerContainer = document.getElementById('outerContainer');
outerContainer.classList.remove('loadingInProgress');
});

var thumbsView = document.getElementById('thumbnailView');
thumbsView.parentNode.scrollTop = 0;

while (thumbsView.hasChildNodes()) {
thumbsView.removeChild(thumbsView.lastChild);
}

if ('_loadingInterval' in thumbsView) {
clearInterval(thumbsView._loadingInterval);
}

var container = document.getElementById('viewer');
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}

var pagesCount = pdfDocument.numPages;

var id = pdfDocument.fingerprint;
Expand All @@ -907,6 +917,8 @@ var PDFView = {
this.pagesPromise = pagesPromise;

var firstPagePromise = pdfDocument.getPage(1);
var container = document.getElementById('viewer');
var thumbsView = document.getElementById('thumbnailView');

// Fetch a single page so we can get a viewport that will be the default
// viewport for all pages
Expand Down

0 comments on commit a06fca1

Please sign in to comment.