Merge branch 'master' of git://github.com/mozilla/pdf.js into thumb-smooth
Conflicts: web/viewer.js
This commit is contained in:
commit
5aad7cb7b5
1
Makefile
1
Makefile
@ -33,6 +33,7 @@ PDF_JS_FILES = \
|
|||||||
pattern.js \
|
pattern.js \
|
||||||
stream.js \
|
stream.js \
|
||||||
worker.js \
|
worker.js \
|
||||||
|
../external/jpgjs/jpg.js \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
|
|
||||||
# make server
|
# make server
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
<script type="text/javascript" src="../../src/pattern.js"></script>
|
<script type="text/javascript" src="../../src/pattern.js"></script>
|
||||||
<script type="text/javascript" src="../../src/stream.js"></script>
|
<script type="text/javascript" src="../../src/stream.js"></script>
|
||||||
<script type="text/javascript" src="../../src/worker.js"></script>
|
<script type="text/javascript" src="../../src/worker.js"></script>
|
||||||
|
<script type="text/javascript" src="../../external/jpgjs/jpg.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
// Specify the main script used to create a new PDF.JS web worker.
|
// Specify the main script used to create a new PDF.JS web worker.
|
||||||
|
@ -6,13 +6,13 @@
|
|||||||
<Description about="urn:mozilla:install-manifest">
|
<Description about="urn:mozilla:install-manifest">
|
||||||
<em:id>uriloader@pdf.js</em:id>
|
<em:id>uriloader@pdf.js</em:id>
|
||||||
<em:name>pdf.js</em:name>
|
<em:name>pdf.js</em:name>
|
||||||
<em:version>0.1</em:version>
|
<em:version>0.1.0</em:version>
|
||||||
<em:iconURL>chrome://pdf.js/skin/logo.png</em:iconURL>
|
<em:iconURL>chrome://pdf.js/skin/logo.png</em:iconURL>
|
||||||
<em:targetApplication>
|
<em:targetApplication>
|
||||||
<Description>
|
<Description>
|
||||||
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
|
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
|
||||||
<em:minVersion>6.0</em:minVersion>
|
<em:minVersion>6.0</em:minVersion>
|
||||||
<em:maxVersion>11.0.*</em:maxVersion>
|
<em:maxVersion>11.0a1</em:maxVersion>
|
||||||
</Description>
|
</Description>
|
||||||
</em:targetApplication>
|
</em:targetApplication>
|
||||||
<em:bootstrap>true</em:bootstrap>
|
<em:bootstrap>true</em:bootstrap>
|
||||||
@ -20,5 +20,6 @@
|
|||||||
<em:creator>Vivien Nicolas</em:creator>
|
<em:creator>Vivien Nicolas</em:creator>
|
||||||
<em:description>pdf.js uri loader</em:description>
|
<em:description>pdf.js uri loader</em:description>
|
||||||
<em:homepageURL>https://github.com/mozilla/pdf.js/</em:homepageURL>
|
<em:homepageURL>https://github.com/mozilla/pdf.js/</em:homepageURL>
|
||||||
|
<em:type>2</em:type>
|
||||||
</Description>
|
</Description>
|
||||||
</RDF>
|
</RDF>
|
||||||
|
20
external/jasmine/MIT.LICENSE
vendored
Normal file
20
external/jasmine/MIT.LICENSE
vendored
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
Copyright (c) 2008-2011 Pivotal Labs
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
676
external/jasmine/jasmine-html.js
vendored
Normal file
676
external/jasmine/jasmine-html.js
vendored
Normal file
@ -0,0 +1,676 @@
|
|||||||
|
jasmine.HtmlReporterHelpers = {};
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.createDom = function(type, attrs, childrenVarArgs) {
|
||||||
|
var el = document.createElement(type);
|
||||||
|
|
||||||
|
for (var i = 2; i < arguments.length; i++) {
|
||||||
|
var child = arguments[i];
|
||||||
|
|
||||||
|
if (typeof child === 'string') {
|
||||||
|
el.appendChild(document.createTextNode(child));
|
||||||
|
} else {
|
||||||
|
if (child) {
|
||||||
|
el.appendChild(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var attr in attrs) {
|
||||||
|
if (attr == "className") {
|
||||||
|
el[attr] = attrs[attr];
|
||||||
|
} else {
|
||||||
|
el.setAttribute(attr, attrs[attr]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return el;
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.getSpecStatus = function(child) {
|
||||||
|
var results = child.results();
|
||||||
|
var status = results.passed() ? 'passed' : 'failed';
|
||||||
|
if (results.skipped) {
|
||||||
|
status = 'skipped';
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.appendToSummary = function(child, childElement) {
|
||||||
|
var parentDiv = this.dom.summary;
|
||||||
|
var parentSuite = (typeof child.parentSuite == 'undefined') ? 'suite' : 'parentSuite';
|
||||||
|
var parent = child[parentSuite];
|
||||||
|
|
||||||
|
if (parent) {
|
||||||
|
if (typeof this.views.suites[parent.id] == 'undefined') {
|
||||||
|
this.views.suites[parent.id] = new jasmine.HtmlReporter.SuiteView(parent, this.dom, this.views);
|
||||||
|
}
|
||||||
|
parentDiv = this.views.suites[parent.id].element;
|
||||||
|
}
|
||||||
|
|
||||||
|
parentDiv.appendChild(childElement);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.addHelpers = function(ctor) {
|
||||||
|
for(var fn in jasmine.HtmlReporterHelpers) {
|
||||||
|
ctor.prototype[fn] = jasmine.HtmlReporterHelpers[fn];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporter = function(_doc) {
|
||||||
|
var self = this;
|
||||||
|
var doc = _doc || window.document;
|
||||||
|
|
||||||
|
var reporterView;
|
||||||
|
|
||||||
|
var dom = {};
|
||||||
|
|
||||||
|
// Jasmine Reporter Public Interface
|
||||||
|
self.logRunningSpecs = false;
|
||||||
|
|
||||||
|
self.reportRunnerStarting = function(runner) {
|
||||||
|
var specs = runner.specs() || [];
|
||||||
|
|
||||||
|
if (specs.length == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
createReporterDom(runner.env.versionString());
|
||||||
|
doc.body.appendChild(dom.reporter);
|
||||||
|
|
||||||
|
reporterView = new jasmine.HtmlReporter.ReporterView(dom);
|
||||||
|
reporterView.addSpecs(specs, self.specFilter);
|
||||||
|
};
|
||||||
|
|
||||||
|
self.reportRunnerResults = function(runner) {
|
||||||
|
reporterView.complete();
|
||||||
|
};
|
||||||
|
|
||||||
|
self.reportSuiteResults = function(suite) {
|
||||||
|
reporterView.suiteComplete(suite);
|
||||||
|
};
|
||||||
|
|
||||||
|
self.reportSpecStarting = function(spec) {
|
||||||
|
if (self.logRunningSpecs) {
|
||||||
|
self.log('>> Jasmine Running ' + spec.suite.description + ' ' + spec.description + '...');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.reportSpecResults = function(spec) {
|
||||||
|
reporterView.specComplete(spec);
|
||||||
|
};
|
||||||
|
|
||||||
|
self.log = function() {
|
||||||
|
var console = jasmine.getGlobal().console;
|
||||||
|
if (console && console.log) {
|
||||||
|
if (console.log.apply) {
|
||||||
|
console.log.apply(console, arguments);
|
||||||
|
} else {
|
||||||
|
console.log(arguments); // ie fix: console.log.apply doesn't exist on ie
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
self.specFilter = function(spec) {
|
||||||
|
if (!focusedSpecName()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return spec.getFullName().indexOf(focusedSpecName()) === 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
return self;
|
||||||
|
|
||||||
|
function focusedSpecName() {
|
||||||
|
var specName;
|
||||||
|
|
||||||
|
(function memoizeFocusedSpec() {
|
||||||
|
if (specName) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var paramMap = [];
|
||||||
|
var params = doc.location.search.substring(1).split('&');
|
||||||
|
|
||||||
|
for (var i = 0; i < params.length; i++) {
|
||||||
|
var p = params[i].split('=');
|
||||||
|
paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
specName = paramMap.spec;
|
||||||
|
})();
|
||||||
|
|
||||||
|
return specName;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createReporterDom(version) {
|
||||||
|
dom.reporter = self.createDom('div', { id: 'HTMLReporter', className: 'jasmine_reporter' },
|
||||||
|
dom.banner = self.createDom('div', { className: 'banner' },
|
||||||
|
self.createDom('span', { className: 'title' }, "Jasmine "),
|
||||||
|
self.createDom('span', { className: 'version' }, version)),
|
||||||
|
|
||||||
|
dom.symbolSummary = self.createDom('ul', {className: 'symbolSummary'}),
|
||||||
|
dom.alert = self.createDom('div', {className: 'alert'}),
|
||||||
|
dom.results = self.createDom('div', {className: 'results'},
|
||||||
|
dom.summary = self.createDom('div', { className: 'summary' }),
|
||||||
|
dom.details = self.createDom('div', { id: 'details' }))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
jasmine.HtmlReporterHelpers.addHelpers(jasmine.HtmlReporter);jasmine.HtmlReporterHelpers = {};
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.createDom = function(type, attrs, childrenVarArgs) {
|
||||||
|
var el = document.createElement(type);
|
||||||
|
|
||||||
|
for (var i = 2; i < arguments.length; i++) {
|
||||||
|
var child = arguments[i];
|
||||||
|
|
||||||
|
if (typeof child === 'string') {
|
||||||
|
el.appendChild(document.createTextNode(child));
|
||||||
|
} else {
|
||||||
|
if (child) {
|
||||||
|
el.appendChild(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var attr in attrs) {
|
||||||
|
if (attr == "className") {
|
||||||
|
el[attr] = attrs[attr];
|
||||||
|
} else {
|
||||||
|
el.setAttribute(attr, attrs[attr]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return el;
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.getSpecStatus = function(child) {
|
||||||
|
var results = child.results();
|
||||||
|
var status = results.passed() ? 'passed' : 'failed';
|
||||||
|
if (results.skipped) {
|
||||||
|
status = 'skipped';
|
||||||
|
}
|
||||||
|
|
||||||
|
return status;
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.appendToSummary = function(child, childElement) {
|
||||||
|
var parentDiv = this.dom.summary;
|
||||||
|
var parentSuite = (typeof child.parentSuite == 'undefined') ? 'suite' : 'parentSuite';
|
||||||
|
var parent = child[parentSuite];
|
||||||
|
|
||||||
|
if (parent) {
|
||||||
|
if (typeof this.views.suites[parent.id] == 'undefined') {
|
||||||
|
this.views.suites[parent.id] = new jasmine.HtmlReporter.SuiteView(parent, this.dom, this.views);
|
||||||
|
}
|
||||||
|
parentDiv = this.views.suites[parent.id].element;
|
||||||
|
}
|
||||||
|
|
||||||
|
parentDiv.appendChild(childElement);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.addHelpers = function(ctor) {
|
||||||
|
for(var fn in jasmine.HtmlReporterHelpers) {
|
||||||
|
ctor.prototype[fn] = jasmine.HtmlReporterHelpers[fn];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporter.ReporterView = function(dom) {
|
||||||
|
this.startedAt = new Date();
|
||||||
|
this.runningSpecCount = 0;
|
||||||
|
this.completeSpecCount = 0;
|
||||||
|
this.passedCount = 0;
|
||||||
|
this.failedCount = 0;
|
||||||
|
this.skippedCount = 0;
|
||||||
|
|
||||||
|
this.createResultsMenu = function() {
|
||||||
|
this.resultsMenu = this.createDom('span', {className: 'resultsMenu bar'},
|
||||||
|
this.summaryMenuItem = this.createDom('a', {className: 'summaryMenuItem', href: "#"}, '0 specs'),
|
||||||
|
' | ',
|
||||||
|
this.detailsMenuItem = this.createDom('a', {className: 'detailsMenuItem', href: "#"}, '0 failing'));
|
||||||
|
|
||||||
|
this.summaryMenuItem.onclick = function() {
|
||||||
|
dom.reporter.className = dom.reporter.className.replace(/ showDetails/g, '');
|
||||||
|
};
|
||||||
|
|
||||||
|
this.detailsMenuItem.onclick = function() {
|
||||||
|
showDetails();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
this.addSpecs = function(specs, specFilter) {
|
||||||
|
this.totalSpecCount = specs.length;
|
||||||
|
|
||||||
|
this.views = {
|
||||||
|
specs: {},
|
||||||
|
suites: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (var i = 0; i < specs.length; i++) {
|
||||||
|
var spec = specs[i];
|
||||||
|
this.views.specs[spec.id] = new jasmine.HtmlReporter.SpecView(spec, dom, this.views);
|
||||||
|
if (specFilter(spec)) {
|
||||||
|
this.runningSpecCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.specComplete = function(spec) {
|
||||||
|
this.completeSpecCount++;
|
||||||
|
|
||||||
|
if (isUndefined(this.views.specs[spec.id])) {
|
||||||
|
this.views.specs[spec.id] = new jasmine.HtmlReporter.SpecView(spec, dom);
|
||||||
|
}
|
||||||
|
|
||||||
|
var specView = this.views.specs[spec.id];
|
||||||
|
|
||||||
|
switch (specView.status()) {
|
||||||
|
case 'passed':
|
||||||
|
this.passedCount++;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'failed':
|
||||||
|
this.failedCount++;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'skipped':
|
||||||
|
this.skippedCount++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
specView.refresh();
|
||||||
|
this.refresh();
|
||||||
|
};
|
||||||
|
|
||||||
|
this.suiteComplete = function(suite) {
|
||||||
|
var suiteView = this.views.suites[suite.id];
|
||||||
|
if (isUndefined(suiteView)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
suiteView.refresh();
|
||||||
|
};
|
||||||
|
|
||||||
|
this.refresh = function() {
|
||||||
|
|
||||||
|
if (isUndefined(this.resultsMenu)) {
|
||||||
|
this.createResultsMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
// currently running UI
|
||||||
|
if (isUndefined(this.runningAlert)) {
|
||||||
|
this.runningAlert = this.createDom('a', {href: "?", className: "runningAlert bar"});
|
||||||
|
dom.alert.appendChild(this.runningAlert);
|
||||||
|
}
|
||||||
|
this.runningAlert.innerHTML = "Running " + this.completeSpecCount + " of " + specPluralizedFor(this.totalSpecCount);
|
||||||
|
|
||||||
|
// skipped specs UI
|
||||||
|
if (isUndefined(this.skippedAlert)) {
|
||||||
|
this.skippedAlert = this.createDom('a', {href: "?", className: "skippedAlert bar"});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.skippedAlert.innerHTML = "Skipping " + this.skippedCount + " of " + specPluralizedFor(this.totalSpecCount) + " - run all";
|
||||||
|
|
||||||
|
if (this.skippedCount === 1 && isDefined(dom.alert)) {
|
||||||
|
dom.alert.appendChild(this.skippedAlert);
|
||||||
|
}
|
||||||
|
|
||||||
|
// passing specs UI
|
||||||
|
if (isUndefined(this.passedAlert)) {
|
||||||
|
this.passedAlert = this.createDom('span', {href: "?", className: "passingAlert bar"});
|
||||||
|
}
|
||||||
|
this.passedAlert.innerHTML = "Passing " + specPluralizedFor(this.passedCount);
|
||||||
|
|
||||||
|
// failing specs UI
|
||||||
|
if (isUndefined(this.failedAlert)) {
|
||||||
|
this.failedAlert = this.createDom('span', {href: "?", className: "failingAlert bar"});
|
||||||
|
}
|
||||||
|
this.failedAlert.innerHTML = "Failing " + specPluralizedFor(this.failedCount);
|
||||||
|
|
||||||
|
if (this.failedCount === 1 && isDefined(dom.alert)) {
|
||||||
|
dom.alert.appendChild(this.failedAlert);
|
||||||
|
dom.alert.appendChild(this.resultsMenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
// summary info
|
||||||
|
this.summaryMenuItem.innerHTML = "" + specPluralizedFor(this.runningSpecCount);
|
||||||
|
this.detailsMenuItem.innerHTML = "" + this.failedCount + " failing";
|
||||||
|
};
|
||||||
|
|
||||||
|
this.complete = function() {
|
||||||
|
dom.alert.removeChild(this.runningAlert);
|
||||||
|
|
||||||
|
this.skippedAlert.innerHTML = "Ran " + this.runningSpecCount + " of " + specPluralizedFor(this.totalSpecCount) + " - run all";
|
||||||
|
|
||||||
|
if (this.failedCount === 0) {
|
||||||
|
dom.alert.appendChild(this.createDom('span', {className: 'passingAlert bar'}, "Passing " + specPluralizedFor(this.passedCount)));
|
||||||
|
} else {
|
||||||
|
showDetails();
|
||||||
|
}
|
||||||
|
|
||||||
|
dom.banner.appendChild(this.createDom('span', {className: 'duration'}, "finished in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s"));
|
||||||
|
};
|
||||||
|
|
||||||
|
return this;
|
||||||
|
|
||||||
|
function showDetails() {
|
||||||
|
if (dom.reporter.className.search(/showDetails/) === -1) {
|
||||||
|
dom.reporter.className += " showDetails";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isUndefined(obj) {
|
||||||
|
return typeof obj === 'undefined';
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDefined(obj) {
|
||||||
|
return !isUndefined(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
function specPluralizedFor(count) {
|
||||||
|
var str = count + " spec";
|
||||||
|
if (count > 1) {
|
||||||
|
str += "s"
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.addHelpers(jasmine.HtmlReporter.ReporterView);
|
||||||
|
|
||||||
|
|
||||||
|
jasmine.HtmlReporter.SpecView = function(spec, dom, views) {
|
||||||
|
this.spec = spec;
|
||||||
|
this.dom = dom;
|
||||||
|
this.views = views;
|
||||||
|
|
||||||
|
this.symbol = this.createDom('li', { className: 'pending' });
|
||||||
|
this.dom.symbolSummary.appendChild(this.symbol);
|
||||||
|
|
||||||
|
this.summary = this.createDom('div', { className: 'specSummary' },
|
||||||
|
this.createDom('a', {
|
||||||
|
className: 'description',
|
||||||
|
href: '?spec=' + encodeURIComponent(this.spec.getFullName()),
|
||||||
|
title: this.spec.getFullName()
|
||||||
|
}, this.spec.description)
|
||||||
|
);
|
||||||
|
|
||||||
|
this.detail = this.createDom('div', { className: 'specDetail' },
|
||||||
|
this.createDom('a', {
|
||||||
|
className: 'description',
|
||||||
|
href: '?spec=' + encodeURIComponent(this.spec.getFullName()),
|
||||||
|
title: this.spec.getFullName()
|
||||||
|
}, this.spec.getFullName())
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporter.SpecView.prototype.status = function() {
|
||||||
|
return this.getSpecStatus(this.spec);
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporter.SpecView.prototype.refresh = function() {
|
||||||
|
this.symbol.className = this.status();
|
||||||
|
|
||||||
|
switch (this.status()) {
|
||||||
|
case 'skipped':
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'passed':
|
||||||
|
this.appendSummaryToSuiteDiv();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'failed':
|
||||||
|
this.appendSummaryToSuiteDiv();
|
||||||
|
this.appendFailureDetail();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporter.SpecView.prototype.appendSummaryToSuiteDiv = function() {
|
||||||
|
this.summary.className += ' ' + this.status();
|
||||||
|
this.appendToSummary(this.spec, this.summary);
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporter.SpecView.prototype.appendFailureDetail = function() {
|
||||||
|
this.detail.className += ' ' + this.status();
|
||||||
|
|
||||||
|
var resultItems = this.spec.results().getItems();
|
||||||
|
var messagesDiv = this.createDom('div', { className: 'messages' });
|
||||||
|
|
||||||
|
for (var i = 0; i < resultItems.length; i++) {
|
||||||
|
var result = resultItems[i];
|
||||||
|
|
||||||
|
if (result.type == 'log') {
|
||||||
|
messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage log'}, result.toString()));
|
||||||
|
} else if (result.type == 'expect' && result.passed && !result.passed()) {
|
||||||
|
messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message));
|
||||||
|
|
||||||
|
if (result.trace.stack) {
|
||||||
|
messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (messagesDiv.childNodes.length > 0) {
|
||||||
|
this.detail.appendChild(messagesDiv);
|
||||||
|
this.dom.details.appendChild(this.detail);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.addHelpers(jasmine.HtmlReporter.SpecView);jasmine.HtmlReporter.SuiteView = function(suite, dom, views) {
|
||||||
|
this.suite = suite;
|
||||||
|
this.dom = dom;
|
||||||
|
this.views = views;
|
||||||
|
|
||||||
|
this.element = this.createDom('div', { className: 'suite' },
|
||||||
|
this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(this.suite.getFullName()) }, this.suite.description)
|
||||||
|
);
|
||||||
|
|
||||||
|
this.appendToSummary(this.suite, this.element);
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporter.SuiteView.prototype.status = function() {
|
||||||
|
return this.getSpecStatus(this.suite);
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporter.SuiteView.prototype.refresh = function() {
|
||||||
|
this.element.className += " " + this.status();
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.HtmlReporterHelpers.addHelpers(jasmine.HtmlReporter.SuiteView);
|
||||||
|
|
||||||
|
/* @deprecated Use jasmine.HtmlReporter instead
|
||||||
|
*/
|
||||||
|
jasmine.TrivialReporter = function(doc) {
|
||||||
|
this.document = doc || document;
|
||||||
|
this.suiteDivs = {};
|
||||||
|
this.logRunningSpecs = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.TrivialReporter.prototype.createDom = function(type, attrs, childrenVarArgs) {
|
||||||
|
var el = document.createElement(type);
|
||||||
|
|
||||||
|
for (var i = 2; i < arguments.length; i++) {
|
||||||
|
var child = arguments[i];
|
||||||
|
|
||||||
|
if (typeof child === 'string') {
|
||||||
|
el.appendChild(document.createTextNode(child));
|
||||||
|
} else {
|
||||||
|
if (child) { el.appendChild(child); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var attr in attrs) {
|
||||||
|
if (attr == "className") {
|
||||||
|
el[attr] = attrs[attr];
|
||||||
|
} else {
|
||||||
|
el.setAttribute(attr, attrs[attr]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return el;
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) {
|
||||||
|
var showPassed, showSkipped;
|
||||||
|
|
||||||
|
this.outerDiv = this.createDom('div', { id: 'TrivialReporter', className: 'jasmine_reporter' },
|
||||||
|
this.createDom('div', { className: 'banner' },
|
||||||
|
this.createDom('div', { className: 'logo' },
|
||||||
|
this.createDom('span', { className: 'title' }, "Jasmine"),
|
||||||
|
this.createDom('span', { className: 'version' }, runner.env.versionString())),
|
||||||
|
this.createDom('div', { className: 'options' },
|
||||||
|
"Show ",
|
||||||
|
showPassed = this.createDom('input', { id: "__jasmine_TrivialReporter_showPassed__", type: 'checkbox' }),
|
||||||
|
this.createDom('label', { "for": "__jasmine_TrivialReporter_showPassed__" }, " passed "),
|
||||||
|
showSkipped = this.createDom('input', { id: "__jasmine_TrivialReporter_showSkipped__", type: 'checkbox' }),
|
||||||
|
this.createDom('label', { "for": "__jasmine_TrivialReporter_showSkipped__" }, " skipped")
|
||||||
|
)
|
||||||
|
),
|
||||||
|
|
||||||
|
this.runnerDiv = this.createDom('div', { className: 'runner running' },
|
||||||
|
this.createDom('a', { className: 'run_spec', href: '?' }, "run all"),
|
||||||
|
this.runnerMessageSpan = this.createDom('span', {}, "Running..."),
|
||||||
|
this.finishedAtSpan = this.createDom('span', { className: 'finished-at' }, ""))
|
||||||
|
);
|
||||||
|
|
||||||
|
this.document.body.appendChild(this.outerDiv);
|
||||||
|
|
||||||
|
var suites = runner.suites();
|
||||||
|
for (var i = 0; i < suites.length; i++) {
|
||||||
|
var suite = suites[i];
|
||||||
|
var suiteDiv = this.createDom('div', { className: 'suite' },
|
||||||
|
this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"),
|
||||||
|
this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, suite.description));
|
||||||
|
this.suiteDivs[suite.id] = suiteDiv;
|
||||||
|
var parentDiv = this.outerDiv;
|
||||||
|
if (suite.parentSuite) {
|
||||||
|
parentDiv = this.suiteDivs[suite.parentSuite.id];
|
||||||
|
}
|
||||||
|
parentDiv.appendChild(suiteDiv);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.startedAt = new Date();
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
showPassed.onclick = function(evt) {
|
||||||
|
if (showPassed.checked) {
|
||||||
|
self.outerDiv.className += ' show-passed';
|
||||||
|
} else {
|
||||||
|
self.outerDiv.className = self.outerDiv.className.replace(/ show-passed/, '');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
showSkipped.onclick = function(evt) {
|
||||||
|
if (showSkipped.checked) {
|
||||||
|
self.outerDiv.className += ' show-skipped';
|
||||||
|
} else {
|
||||||
|
self.outerDiv.className = self.outerDiv.className.replace(/ show-skipped/, '');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) {
|
||||||
|
var results = runner.results();
|
||||||
|
var className = (results.failedCount > 0) ? "runner failed" : "runner passed";
|
||||||
|
this.runnerDiv.setAttribute("class", className);
|
||||||
|
//do it twice for IE
|
||||||
|
this.runnerDiv.setAttribute("className", className);
|
||||||
|
var specs = runner.specs();
|
||||||
|
var specCount = 0;
|
||||||
|
for (var i = 0; i < specs.length; i++) {
|
||||||
|
if (this.specFilter(specs[i])) {
|
||||||
|
specCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var message = "" + specCount + " spec" + (specCount == 1 ? "" : "s" ) + ", " + results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s");
|
||||||
|
message += " in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s";
|
||||||
|
this.runnerMessageSpan.replaceChild(this.createDom('a', { className: 'description', href: '?'}, message), this.runnerMessageSpan.firstChild);
|
||||||
|
|
||||||
|
this.finishedAtSpan.appendChild(document.createTextNode("Finished at " + new Date().toString()));
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) {
|
||||||
|
var results = suite.results();
|
||||||
|
var status = results.passed() ? 'passed' : 'failed';
|
||||||
|
if (results.totalCount === 0) { // todo: change this to check results.skipped
|
||||||
|
status = 'skipped';
|
||||||
|
}
|
||||||
|
this.suiteDivs[suite.id].className += " " + status;
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.TrivialReporter.prototype.reportSpecStarting = function(spec) {
|
||||||
|
if (this.logRunningSpecs) {
|
||||||
|
this.log('>> Jasmine Running ' + spec.suite.description + ' ' + spec.description + '...');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) {
|
||||||
|
var results = spec.results();
|
||||||
|
var status = results.passed() ? 'passed' : 'failed';
|
||||||
|
if (results.skipped) {
|
||||||
|
status = 'skipped';
|
||||||
|
}
|
||||||
|
var specDiv = this.createDom('div', { className: 'spec ' + status },
|
||||||
|
this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"),
|
||||||
|
this.createDom('a', {
|
||||||
|
className: 'description',
|
||||||
|
href: '?spec=' + encodeURIComponent(spec.getFullName()),
|
||||||
|
title: spec.getFullName()
|
||||||
|
}, spec.description));
|
||||||
|
|
||||||
|
|
||||||
|
var resultItems = results.getItems();
|
||||||
|
var messagesDiv = this.createDom('div', { className: 'messages' });
|
||||||
|
for (var i = 0; i < resultItems.length; i++) {
|
||||||
|
var result = resultItems[i];
|
||||||
|
|
||||||
|
if (result.type == 'log') {
|
||||||
|
messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage log'}, result.toString()));
|
||||||
|
} else if (result.type == 'expect' && result.passed && !result.passed()) {
|
||||||
|
messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message));
|
||||||
|
|
||||||
|
if (result.trace.stack) {
|
||||||
|
messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (messagesDiv.childNodes.length > 0) {
|
||||||
|
specDiv.appendChild(messagesDiv);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.suiteDivs[spec.suite.id].appendChild(specDiv);
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.TrivialReporter.prototype.log = function() {
|
||||||
|
var console = jasmine.getGlobal().console;
|
||||||
|
if (console && console.log) {
|
||||||
|
if (console.log.apply) {
|
||||||
|
console.log.apply(console, arguments);
|
||||||
|
} else {
|
||||||
|
console.log(arguments); // ie fix: console.log.apply doesn't exist on ie
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.TrivialReporter.prototype.getLocation = function() {
|
||||||
|
return this.document.location;
|
||||||
|
};
|
||||||
|
|
||||||
|
jasmine.TrivialReporter.prototype.specFilter = function(spec) {
|
||||||
|
var paramMap = {};
|
||||||
|
var params = this.getLocation().search.substring(1).split('&');
|
||||||
|
for (var i = 0; i < params.length; i++) {
|
||||||
|
var p = params[i].split('=');
|
||||||
|
paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!paramMap.spec) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return spec.getFullName().indexOf(paramMap.spec) === 0;
|
||||||
|
};
|
81
external/jasmine/jasmine.css
vendored
Normal file
81
external/jasmine/jasmine.css
vendored
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
body { background-color: #eeeeee; padding: 0; margin: 5px; overflow-y: scroll; }
|
||||||
|
|
||||||
|
#HTMLReporter { font-size: 11px; font-family: Monaco, "Lucida Console", monospace; line-height: 14px; color: #333333; }
|
||||||
|
#HTMLReporter a { text-decoration: none; }
|
||||||
|
#HTMLReporter a:hover { text-decoration: underline; }
|
||||||
|
#HTMLReporter p, #HTMLReporter h1, #HTMLReporter h2, #HTMLReporter h3, #HTMLReporter h4, #HTMLReporter h5, #HTMLReporter h6 { margin: 0; line-height: 14px; }
|
||||||
|
#HTMLReporter .banner, #HTMLReporter .symbolSummary, #HTMLReporter .summary, #HTMLReporter .resultMessage, #HTMLReporter .specDetail .description, #HTMLReporter .alert .bar, #HTMLReporter .stackTrace { padding-left: 9px; padding-right: 9px; }
|
||||||
|
#HTMLReporter #jasmine_content { position: fixed; right: 100%; }
|
||||||
|
#HTMLReporter .version { color: #aaaaaa; }
|
||||||
|
#HTMLReporter .banner { margin-top: 14px; }
|
||||||
|
#HTMLReporter .duration { color: #aaaaaa; float: right; }
|
||||||
|
#HTMLReporter .symbolSummary { overflow: hidden; *zoom: 1; margin: 14px 0; }
|
||||||
|
#HTMLReporter .symbolSummary li { display: block; float: left; height: 7px; width: 14px; margin-bottom: 7px; font-size: 16px; }
|
||||||
|
#HTMLReporter .symbolSummary li.passed { font-size: 14px; }
|
||||||
|
#HTMLReporter .symbolSummary li.passed:before { color: #5e7d00; content: "\02022"; }
|
||||||
|
#HTMLReporter .symbolSummary li.failed { line-height: 9px; }
|
||||||
|
#HTMLReporter .symbolSummary li.failed:before { color: #b03911; content: "x"; font-weight: bold; margin-left: -1px; }
|
||||||
|
#HTMLReporter .symbolSummary li.skipped { font-size: 14px; }
|
||||||
|
#HTMLReporter .symbolSummary li.skipped:before { color: #bababa; content: "\02022"; }
|
||||||
|
#HTMLReporter .symbolSummary li.pending { line-height: 11px; }
|
||||||
|
#HTMLReporter .symbolSummary li.pending:before { color: #aaaaaa; content: "-"; }
|
||||||
|
#HTMLReporter .bar { line-height: 28px; font-size: 14px; display: block; color: #eee; }
|
||||||
|
#HTMLReporter .runningAlert { background-color: #666666; }
|
||||||
|
#HTMLReporter .skippedAlert { background-color: #aaaaaa; }
|
||||||
|
#HTMLReporter .skippedAlert:first-child { background-color: #333333; }
|
||||||
|
#HTMLReporter .skippedAlert:hover { text-decoration: none; color: white; text-decoration: underline; }
|
||||||
|
#HTMLReporter .passingAlert { background-color: #a6b779; }
|
||||||
|
#HTMLReporter .passingAlert:first-child { background-color: #5e7d00; }
|
||||||
|
#HTMLReporter .failingAlert { background-color: #cf867e; }
|
||||||
|
#HTMLReporter .failingAlert:first-child { background-color: #b03911; }
|
||||||
|
#HTMLReporter .results { margin-top: 14px; }
|
||||||
|
#HTMLReporter #details { display: none; }
|
||||||
|
#HTMLReporter .resultsMenu, #HTMLReporter .resultsMenu a { background-color: #fff; color: #333333; }
|
||||||
|
#HTMLReporter.showDetails .summaryMenuItem { font-weight: normal; text-decoration: inherit; }
|
||||||
|
#HTMLReporter.showDetails .summaryMenuItem:hover { text-decoration: underline; }
|
||||||
|
#HTMLReporter.showDetails .detailsMenuItem { font-weight: bold; text-decoration: underline; }
|
||||||
|
#HTMLReporter.showDetails .summary { display: none; }
|
||||||
|
#HTMLReporter.showDetails #details { display: block; }
|
||||||
|
#HTMLReporter .summaryMenuItem { font-weight: bold; text-decoration: underline; }
|
||||||
|
#HTMLReporter .summary { margin-top: 14px; }
|
||||||
|
#HTMLReporter .summary .suite .suite, #HTMLReporter .summary .specSummary { margin-left: 14px; }
|
||||||
|
#HTMLReporter .summary .specSummary.passed a { color: #5e7d00; }
|
||||||
|
#HTMLReporter .summary .specSummary.failed a { color: #b03911; }
|
||||||
|
#HTMLReporter .description + .suite { margin-top: 0; }
|
||||||
|
#HTMLReporter .suite { margin-top: 14px; }
|
||||||
|
#HTMLReporter .suite a { color: #333333; }
|
||||||
|
#HTMLReporter #details .specDetail { margin-bottom: 28px; }
|
||||||
|
#HTMLReporter #details .specDetail .description { display: block; color: white; background-color: #b03911; }
|
||||||
|
#HTMLReporter .resultMessage { padding-top: 14px; color: #333333; }
|
||||||
|
#HTMLReporter .resultMessage span.result { display: block; }
|
||||||
|
#HTMLReporter .stackTrace { margin: 5px 0 0 0; max-height: 224px; overflow: auto; line-height: 18px; color: #666666; border: 1px solid #ddd; background: white; white-space: pre; }
|
||||||
|
|
||||||
|
#TrivialReporter { padding: 8px 13px; position: absolute; top: 0; bottom: 0; left: 0; right: 0; overflow-y: scroll; background-color: white; font-family: "Helvetica Neue Light", "Lucida Grande", "Calibri", "Arial", sans-serif; /*.resultMessage {*/ /*white-space: pre;*/ /*}*/ }
|
||||||
|
#TrivialReporter a:visited, #TrivialReporter a { color: #303; }
|
||||||
|
#TrivialReporter a:hover, #TrivialReporter a:active { color: blue; }
|
||||||
|
#TrivialReporter .run_spec { float: right; padding-right: 5px; font-size: .8em; text-decoration: none; }
|
||||||
|
#TrivialReporter .banner { color: #303; background-color: #fef; padding: 5px; }
|
||||||
|
#TrivialReporter .logo { float: left; font-size: 1.1em; padding-left: 5px; }
|
||||||
|
#TrivialReporter .logo .version { font-size: .6em; padding-left: 1em; }
|
||||||
|
#TrivialReporter .runner.running { background-color: yellow; }
|
||||||
|
#TrivialReporter .options { text-align: right; font-size: .8em; }
|
||||||
|
#TrivialReporter .suite { border: 1px outset gray; margin: 5px 0; padding-left: 1em; }
|
||||||
|
#TrivialReporter .suite .suite { margin: 5px; }
|
||||||
|
#TrivialReporter .suite.passed { background-color: #dfd; }
|
||||||
|
#TrivialReporter .suite.failed { background-color: #fdd; }
|
||||||
|
#TrivialReporter .spec { margin: 5px; padding-left: 1em; clear: both; }
|
||||||
|
#TrivialReporter .spec.failed, #TrivialReporter .spec.passed, #TrivialReporter .spec.skipped { padding-bottom: 5px; border: 1px solid gray; }
|
||||||
|
#TrivialReporter .spec.failed { background-color: #fbb; border-color: red; }
|
||||||
|
#TrivialReporter .spec.passed { background-color: #bfb; border-color: green; }
|
||||||
|
#TrivialReporter .spec.skipped { background-color: #bbb; }
|
||||||
|
#TrivialReporter .messages { border-left: 1px dashed gray; padding-left: 1em; padding-right: 1em; }
|
||||||
|
#TrivialReporter .passed { background-color: #cfc; display: none; }
|
||||||
|
#TrivialReporter .failed { background-color: #fbb; }
|
||||||
|
#TrivialReporter .skipped { color: #777; background-color: #eee; display: none; }
|
||||||
|
#TrivialReporter .resultMessage span.result { display: block; line-height: 2em; color: black; }
|
||||||
|
#TrivialReporter .resultMessage .mismatch { color: black; }
|
||||||
|
#TrivialReporter .stackTrace { white-space: pre; font-size: .8em; margin-left: 10px; max-height: 5em; overflow: auto; border: 1px inset red; padding: 1em; background: #eef; }
|
||||||
|
#TrivialReporter .finished-at { padding-left: 1em; font-size: .6em; }
|
||||||
|
#TrivialReporter.show-passed .passed, #TrivialReporter.show-skipped .skipped { display: block; }
|
||||||
|
#TrivialReporter #jasmine_content { position: fixed; right: 100%; }
|
||||||
|
#TrivialReporter .runner { border: 1px solid gray; display: block; margin: 5px 0; padding: 2px 0 2px 10px; }
|
2476
external/jasmine/jasmine.js
vendored
Normal file
2476
external/jasmine/jasmine.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
BIN
external/jasmine/jasmine_favicon.png
vendored
Normal file
BIN
external/jasmine/jasmine_favicon.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 905 B |
24
external/jpgjs/LICENSE
vendored
Normal file
24
external/jpgjs/LICENSE
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
Copyright (C) 2011 by notmasteryet
|
||||||
|
|
||||||
|
Contributors: Yury Delendik
|
||||||
|
Brendan Dahl <bdahl@mozilla.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
911
external/jpgjs/jpg.js
vendored
Normal file
911
external/jpgjs/jpg.js
vendored
Normal file
@ -0,0 +1,911 @@
|
|||||||
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
|
||||||
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
||||||
|
|
||||||
|
// - The JPEG specification can be found in the ITU CCITT Recommendation T.81
|
||||||
|
// (www.w3.org/Graphics/JPEG/itu-t81.pdf)
|
||||||
|
// - The JFIF specification can be found in the JPEG File Interchange Format
|
||||||
|
// (www.w3.org/Graphics/JPEG/jfif3.pdf)
|
||||||
|
// - The Adobe Application-Specific JPEG markers in the Supporting the DCT Filters
|
||||||
|
// in PostScript Level 2, Technical Note #5116
|
||||||
|
// (partners.adobe.com/public/developer/en/ps/sdk/5116.DCT_Filter.pdf)
|
||||||
|
|
||||||
|
var JpegImage = (function jpegImage() {
|
||||||
|
"use strict";
|
||||||
|
var dctZigZag = new Int32Array([
|
||||||
|
0,
|
||||||
|
1, 8,
|
||||||
|
16, 9, 2,
|
||||||
|
3, 10, 17, 24,
|
||||||
|
32, 25, 18, 11, 4,
|
||||||
|
5, 12, 19, 26, 33, 40,
|
||||||
|
48, 41, 34, 27, 20, 13, 6,
|
||||||
|
7, 14, 21, 28, 35, 42, 49, 56,
|
||||||
|
57, 50, 43, 36, 29, 22, 15,
|
||||||
|
23, 30, 37, 44, 51, 58,
|
||||||
|
59, 52, 45, 38, 31,
|
||||||
|
39, 46, 53, 60,
|
||||||
|
61, 54, 47,
|
||||||
|
55, 62,
|
||||||
|
63
|
||||||
|
]);
|
||||||
|
|
||||||
|
var dctCos1 = 4017 // cos(pi/16)
|
||||||
|
var dctSin1 = 799 // sin(pi/16)
|
||||||
|
var dctCos3 = 3406 // cos(3*pi/16)
|
||||||
|
var dctSin3 = 2276 // sin(3*pi/16)
|
||||||
|
var dctCos6 = 1567 // cos(6*pi/16)
|
||||||
|
var dctSin6 = 3784 // sin(6*pi/16)
|
||||||
|
var dctSqrt2 = 5793 // sqrt(2)
|
||||||
|
var dctSqrt1d2 = 2896 // sqrt(2) / 2
|
||||||
|
|
||||||
|
function constructor() {
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildHuffmanTable(codeLengths, values) {
|
||||||
|
var k = 0, code = [], i, j, length = 16;
|
||||||
|
while (length > 0 && !codeLengths[length - 1])
|
||||||
|
length--;
|
||||||
|
code.push({children: [], index: 0});
|
||||||
|
var p = code[0], q;
|
||||||
|
for (i = 0; i < length; i++) {
|
||||||
|
for (j = 0; j < codeLengths[i]; j++) {
|
||||||
|
p = code.pop();
|
||||||
|
p.children[p.index] = values[k];
|
||||||
|
while (p.index > 0) {
|
||||||
|
p = code.pop();
|
||||||
|
}
|
||||||
|
p.index++;
|
||||||
|
code.push(p);
|
||||||
|
while (code.length <= i) {
|
||||||
|
code.push(q = {children: [], index: 0});
|
||||||
|
p.children[p.index] = q.children;
|
||||||
|
p = q;
|
||||||
|
}
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
if (i + 1 < length) {
|
||||||
|
// p here points to last code
|
||||||
|
code.push(q = {children: [], index: 0});
|
||||||
|
p.children[p.index] = q.children;
|
||||||
|
p = q;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return code[0].children;
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeScan(data, offset,
|
||||||
|
frame, components, resetInterval,
|
||||||
|
spectralStart, spectralEnd,
|
||||||
|
successivePrev, successive) {
|
||||||
|
var precision = frame.precision;
|
||||||
|
var samplesPerLine = frame.samplesPerLine;
|
||||||
|
var scanLines = frame.scanLines;
|
||||||
|
var mcusPerLine = frame.mcusPerLine;
|
||||||
|
var progressive = frame.progressive;
|
||||||
|
var maxH = frame.maxH, maxV = frame.maxV;
|
||||||
|
|
||||||
|
var startOffset = offset, bitsData = 0, bitsCount = 0;
|
||||||
|
function readBit() {
|
||||||
|
if (bitsCount > 0) {
|
||||||
|
bitsCount--;
|
||||||
|
return (bitsData >> bitsCount) & 1;
|
||||||
|
}
|
||||||
|
bitsData = data[offset++];
|
||||||
|
if (bitsData == 0xFF) {
|
||||||
|
var nextByte = data[offset++];
|
||||||
|
if (nextByte) {
|
||||||
|
throw "unexpected marker: " + ((bitsData << 8) | nextByte).toString(16);
|
||||||
|
}
|
||||||
|
// unstuff 0
|
||||||
|
}
|
||||||
|
bitsCount = 7;
|
||||||
|
return bitsData >>> 7;
|
||||||
|
}
|
||||||
|
function decodeHuffman(tree) {
|
||||||
|
var node = tree, bit;
|
||||||
|
while ((bit = readBit()) !== null) {
|
||||||
|
node = node[bit];
|
||||||
|
if (typeof node === 'number')
|
||||||
|
return node;
|
||||||
|
if (typeof node !== 'object')
|
||||||
|
throw "invalid huffman sequence";
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
function receive(length) {
|
||||||
|
var n = 0;
|
||||||
|
while (length > 0) {
|
||||||
|
var bit = readBit();
|
||||||
|
if (bit === null) return;
|
||||||
|
n = (n << 1) | bit;
|
||||||
|
length--;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
function receiveAndExtend(length) {
|
||||||
|
var n = receive(length);
|
||||||
|
if (n >= 1 << (length - 1))
|
||||||
|
return n;
|
||||||
|
return n + (-1 << length) + 1;
|
||||||
|
}
|
||||||
|
function decodeBaseline(component, zz) {
|
||||||
|
var t = decodeHuffman(component.huffmanTableDC);
|
||||||
|
var diff = t === 0 ? 0 : receiveAndExtend(t);
|
||||||
|
zz[0]= (component.pred += diff);
|
||||||
|
var k = 1;
|
||||||
|
while (k < 64) {
|
||||||
|
var rs = decodeHuffman(component.huffmanTableAC);
|
||||||
|
var s = rs & 15, r = rs >> 4;
|
||||||
|
if (s === 0) {
|
||||||
|
if (r < 15)
|
||||||
|
break;
|
||||||
|
k += 16;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
k += r;
|
||||||
|
var z = dctZigZag[k];
|
||||||
|
zz[z] = receiveAndExtend(s);
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function decodeDCFirst(component, zz) {
|
||||||
|
var t = decodeHuffman(component.huffmanTableDC);
|
||||||
|
var diff = t === 0 ? 0 : (receiveAndExtend(t) << successive);
|
||||||
|
zz[0] = (component.pred += diff);
|
||||||
|
}
|
||||||
|
function decodeDCSuccessive(component, zz) {
|
||||||
|
zz[0] |= readBit() << successive;
|
||||||
|
}
|
||||||
|
var eobrun = 0;
|
||||||
|
function decodeACFirst(component, zz) {
|
||||||
|
if (eobrun > 0) {
|
||||||
|
eobrun--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var k = spectralStart, e = spectralEnd;
|
||||||
|
while (k <= e) {
|
||||||
|
var rs = decodeHuffman(component.huffmanTableAC);
|
||||||
|
var s = rs & 15, r = rs >> 4;
|
||||||
|
if (s === 0) {
|
||||||
|
if (r < 15) {
|
||||||
|
eobrun = receive(r) + (1 << r) - 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
k += 16;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
k += r;
|
||||||
|
var z = dctZigZag[k];
|
||||||
|
zz[z] = receiveAndExtend(s) * (1 << successive);
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var successiveACState = 0, successiveACNextValue;
|
||||||
|
function decodeACSuccessive(component, zz) {
|
||||||
|
var k = spectralStart, e = spectralEnd, r = 0;
|
||||||
|
while (k <= e) {
|
||||||
|
var z = dctZigZag[k];
|
||||||
|
switch (successiveACState) {
|
||||||
|
case 0: // initial state
|
||||||
|
var rs = decodeHuffman(component.huffmanTableAC);
|
||||||
|
var s = rs & 15, r = rs >> 4;
|
||||||
|
if (s === 0) {
|
||||||
|
if (r < 15) {
|
||||||
|
eobrun = receive(r) + (1 << r);
|
||||||
|
successiveACState = 4;
|
||||||
|
} else {
|
||||||
|
r = 16;
|
||||||
|
successiveACState = 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (s !== 1)
|
||||||
|
throw "invalid ACn encoding";
|
||||||
|
successiveACNextValue = receiveAndExtend(s);
|
||||||
|
successiveACState = r ? 2 : 3;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
case 1: // skipping r zero items
|
||||||
|
case 2:
|
||||||
|
if (zz[z])
|
||||||
|
zz[z] += (readBit() << successive);
|
||||||
|
else {
|
||||||
|
r--;
|
||||||
|
if (r === 0)
|
||||||
|
successiveACState = successiveACState == 2 ? 3 : 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3: // set value for a zero item
|
||||||
|
if (zz[z])
|
||||||
|
zz[z] += (readBit() << successive);
|
||||||
|
else {
|
||||||
|
zz[z] = successiveACNextValue << successive;
|
||||||
|
successiveACState = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 4: // eob
|
||||||
|
if (zz[z])
|
||||||
|
zz[z] += (readBit() << successive);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
if (successiveACState === 4) {
|
||||||
|
eobrun--;
|
||||||
|
if (eobrun === 0)
|
||||||
|
successiveACState = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function decodeMcu(component, decode, mcu, row, col) {
|
||||||
|
var mcuRow = (mcu / mcusPerLine) | 0;
|
||||||
|
var mcuCol = mcu % mcusPerLine;
|
||||||
|
var blockRow = mcuRow * component.v + row;
|
||||||
|
var blockCol = mcuCol * component.h + col;
|
||||||
|
decode(component, component.blocks[blockRow][blockCol]);
|
||||||
|
}
|
||||||
|
function decodeBlock(component, decode, mcu) {
|
||||||
|
var blockRow = (mcu / component.blocksPerLine) | 0;
|
||||||
|
var blockCol = mcu % component.blocksPerLine;
|
||||||
|
decode(component, component.blocks[blockRow][blockCol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
var componentsLength = components.length;
|
||||||
|
var component, i, j, k, n;
|
||||||
|
var decodeFn;
|
||||||
|
if (progressive) {
|
||||||
|
if (spectralStart === 0)
|
||||||
|
decodeFn = successivePrev === 0 ? decodeDCFirst : decodeDCSuccessive;
|
||||||
|
else
|
||||||
|
decodeFn = successivePrev === 0 ? decodeACFirst : decodeACSuccessive;
|
||||||
|
} else {
|
||||||
|
decodeFn = decodeBaseline;
|
||||||
|
}
|
||||||
|
|
||||||
|
var mcu = 0, marker;
|
||||||
|
var mcuExpected;
|
||||||
|
if (componentsLength == 1) {
|
||||||
|
mcuExpected = components[0].blocksPerLine * components[0].blocksPerColumn;
|
||||||
|
} else {
|
||||||
|
mcuExpected = mcusPerLine * frame.mcusPerColumn;
|
||||||
|
}
|
||||||
|
if (!resetInterval) resetInterval = mcuExpected;
|
||||||
|
|
||||||
|
var h, v;
|
||||||
|
while (mcu < mcuExpected) {
|
||||||
|
// reset interval stuff
|
||||||
|
for (i = 0; i < componentsLength; i++)
|
||||||
|
components[i].pred = 0;
|
||||||
|
eobrun = 0;
|
||||||
|
|
||||||
|
if (componentsLength == 1) {
|
||||||
|
component = components[0];
|
||||||
|
for (n = 0; n < resetInterval; n++) {
|
||||||
|
decodeBlock(component, decodeFn, mcu);
|
||||||
|
mcu++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (n = 0; n < resetInterval; n++) {
|
||||||
|
for (i = 0; i < componentsLength; i++) {
|
||||||
|
component = components[i];
|
||||||
|
h = component.h;
|
||||||
|
v = component.v;
|
||||||
|
for (j = 0; j < v; j++) {
|
||||||
|
for (k = 0; k < h; k++) {
|
||||||
|
decodeMcu(component, decodeFn, mcu, j, k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mcu++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// find marker
|
||||||
|
bitsCount = 0;
|
||||||
|
marker = (data[offset] << 8) | data[offset + 1];
|
||||||
|
if (marker <= 0xFF00) {
|
||||||
|
throw "marker was not found";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (marker >= 0xFFD0 && marker <= 0xFFD7) { // RSTx
|
||||||
|
offset += 2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return offset - startOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildComponentData(frame, component) {
|
||||||
|
var lines = [];
|
||||||
|
var blocksPerLine = component.blocksPerLine;
|
||||||
|
var blocksPerColumn = component.blocksPerColumn;
|
||||||
|
var samplesPerLine = blocksPerLine << 3;
|
||||||
|
var R = new Int32Array(64), r = new Uint8Array(64);
|
||||||
|
|
||||||
|
// A port of poppler's IDCT method which in turn is taken from:
|
||||||
|
// Christoph Loeffler, Adriaan Ligtenberg, George S. Moschytz,
|
||||||
|
// "Practical Fast 1-D DCT Algorithms with 11 Multiplications",
|
||||||
|
// IEEE Intl. Conf. on Acoustics, Speech & Signal Processing, 1989,
|
||||||
|
// 988-991.
|
||||||
|
function quantizeAndInverse(zz, dataOut, dataIn) {
|
||||||
|
var qt = component.quantizationTable;
|
||||||
|
var v0, v1, v2, v3, v4, v5, v6, v7, t;
|
||||||
|
var p = dataIn;
|
||||||
|
var i;
|
||||||
|
|
||||||
|
// dequant
|
||||||
|
for (i = 0; i < 64; i++)
|
||||||
|
p[i] = zz[i] * qt[i];
|
||||||
|
|
||||||
|
// inverse DCT on rows
|
||||||
|
for (i = 0; i < 8; ++i) {
|
||||||
|
var row = 8 * i;
|
||||||
|
|
||||||
|
// check for all-zero AC coefficients
|
||||||
|
if (p[1 + row] == 0 && p[2 + row] == 0 && p[3 + row] == 0 &&
|
||||||
|
p[4 + row] == 0 && p[5 + row] == 0 && p[6 + row] == 0 &&
|
||||||
|
p[7 + row] == 0) {
|
||||||
|
t = (dctSqrt2 * p[0 + row] + 512) >> 10;
|
||||||
|
p[0 + row] = t;
|
||||||
|
p[1 + row] = t;
|
||||||
|
p[2 + row] = t;
|
||||||
|
p[3 + row] = t;
|
||||||
|
p[4 + row] = t;
|
||||||
|
p[5 + row] = t;
|
||||||
|
p[6 + row] = t;
|
||||||
|
p[7 + row] = t;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// stage 4
|
||||||
|
v0 = (dctSqrt2 * p[0 + row] + 128) >> 8;
|
||||||
|
v1 = (dctSqrt2 * p[4 + row] + 128) >> 8;
|
||||||
|
v2 = p[2 + row];
|
||||||
|
v3 = p[6 + row];
|
||||||
|
v4 = (dctSqrt1d2 * (p[1 + row] - p[7 + row]) + 128) >> 8;
|
||||||
|
v7 = (dctSqrt1d2 * (p[1 + row] + p[7 + row]) + 128) >> 8;
|
||||||
|
v5 = p[3 + row] << 4;
|
||||||
|
v6 = p[5 + row] << 4;
|
||||||
|
|
||||||
|
// stage 3
|
||||||
|
t = (v0 - v1+ 1) >> 1;
|
||||||
|
v0 = (v0 + v1 + 1) >> 1;
|
||||||
|
v1 = t;
|
||||||
|
t = (v2 * dctSin6 + v3 * dctCos6 + 128) >> 8;
|
||||||
|
v2 = (v2 * dctCos6 - v3 * dctSin6 + 128) >> 8;
|
||||||
|
v3 = t;
|
||||||
|
t = (v4 - v6 + 1) >> 1;
|
||||||
|
v4 = (v4 + v6 + 1) >> 1;
|
||||||
|
v6 = t;
|
||||||
|
t = (v7 + v5 + 1) >> 1;
|
||||||
|
v5 = (v7 - v5 + 1) >> 1;
|
||||||
|
v7 = t;
|
||||||
|
|
||||||
|
// stage 2
|
||||||
|
t = (v0 - v3 + 1) >> 1;
|
||||||
|
v0 = (v0 + v3 + 1) >> 1;
|
||||||
|
v3 = t;
|
||||||
|
t = (v1 - v2 + 1) >> 1;
|
||||||
|
v1 = (v1 + v2 + 1) >> 1;
|
||||||
|
v2 = t;
|
||||||
|
t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
|
||||||
|
v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
|
||||||
|
v7 = t;
|
||||||
|
t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
|
||||||
|
v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
|
||||||
|
v6 = t;
|
||||||
|
|
||||||
|
// stage 1
|
||||||
|
p[0 + row] = v0 + v7;
|
||||||
|
p[7 + row] = v0 - v7;
|
||||||
|
p[1 + row] = v1 + v6;
|
||||||
|
p[6 + row] = v1 - v6;
|
||||||
|
p[2 + row] = v2 + v5;
|
||||||
|
p[5 + row] = v2 - v5;
|
||||||
|
p[3 + row] = v3 + v4;
|
||||||
|
p[4 + row] = v3 - v4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// inverse DCT on columns
|
||||||
|
for (i = 0; i < 8; ++i) {
|
||||||
|
var col = i;
|
||||||
|
|
||||||
|
// check for all-zero AC coefficients
|
||||||
|
if (p[1*8 + col] == 0 && p[2*8 + col] == 0 && p[3*8 + col] == 0 &&
|
||||||
|
p[4*8 + col] == 0 && p[5*8 + col] == 0 && p[6*8 + col] == 0 &&
|
||||||
|
p[7*8 + col] == 0) {
|
||||||
|
t = (dctSqrt2 * dataIn[i+0] + 8192) >> 14;
|
||||||
|
p[0*8 + col] = t;
|
||||||
|
p[1*8 + col] = t;
|
||||||
|
p[2*8 + col] = t;
|
||||||
|
p[3*8 + col] = t;
|
||||||
|
p[4*8 + col] = t;
|
||||||
|
p[5*8 + col] = t;
|
||||||
|
p[6*8 + col] = t;
|
||||||
|
p[7*8 + col] = t;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// stage 4
|
||||||
|
v0 = (dctSqrt2 * p[0*8 + col] + 2048) >> 12;
|
||||||
|
v1 = (dctSqrt2 * p[4*8 + col] + 2048) >> 12;
|
||||||
|
v2 = p[2*8 + col];
|
||||||
|
v3 = p[6*8 + col];
|
||||||
|
v4 = (dctSqrt1d2 * (p[1*8 + col] - p[7*8 + col]) + 2048) >> 12;
|
||||||
|
v7 = (dctSqrt1d2 * (p[1*8 + col] + p[7*8 + col]) + 2048) >> 12;
|
||||||
|
v5 = p[3*8 + col];
|
||||||
|
v6 = p[5*8 + col];
|
||||||
|
|
||||||
|
// stage 3
|
||||||
|
t = (v0 - v1 + 1) >> 1;
|
||||||
|
v0 = (v0 + v1 + 1) >> 1;
|
||||||
|
v1 = t;
|
||||||
|
t = (v2 * dctSin6 + v3 * dctCos6 + 2048) >> 12;
|
||||||
|
v2 = (v2 * dctCos6 - v3 * dctSin6 + 2048) >> 12;
|
||||||
|
v3 = t;
|
||||||
|
t = (v4 - v6 + 1) >> 1;
|
||||||
|
v4 = (v4 + v6 + 1) >> 1;
|
||||||
|
v6 = t;
|
||||||
|
t = (v7 + v5 + 1) >> 1;
|
||||||
|
v5 = (v7 - v5 + 1) >> 1;
|
||||||
|
v7 = t;
|
||||||
|
|
||||||
|
// stage 2
|
||||||
|
t = (v0 - v3 + 1) >> 1;
|
||||||
|
v0 = (v0 + v3 + 1) >> 1;
|
||||||
|
v3 = t;
|
||||||
|
t = (v1 - v2 + 1) >> 1;
|
||||||
|
v1 = (v1 + v2 + 1) >> 1;
|
||||||
|
v2 = t;
|
||||||
|
t = (v4 * dctSin3 + v7 * dctCos3 + 2048) >> 12;
|
||||||
|
v4 = (v4 * dctCos3 - v7 * dctSin3 + 2048) >> 12;
|
||||||
|
v7 = t;
|
||||||
|
t = (v5 * dctSin1 + v6 * dctCos1 + 2048) >> 12;
|
||||||
|
v5 = (v5 * dctCos1 - v6 * dctSin1 + 2048) >> 12;
|
||||||
|
v6 = t;
|
||||||
|
|
||||||
|
// stage 1
|
||||||
|
p[0*8 + col] = v0 + v7;
|
||||||
|
p[7*8 + col] = v0 - v7;
|
||||||
|
p[1*8 + col] = v1 + v6;
|
||||||
|
p[6*8 + col] = v1 - v6;
|
||||||
|
p[2*8 + col] = v2 + v5;
|
||||||
|
p[5*8 + col] = v2 - v5;
|
||||||
|
p[3*8 + col] = v3 + v4;
|
||||||
|
p[4*8 + col] = v3 - v4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert to 8-bit integers
|
||||||
|
for (i = 0; i < 64; ++i) {
|
||||||
|
var sample = 128 + ((p[i] + 8) >> 4);
|
||||||
|
dataOut[i] = sample < 0 ? 0 : sample > 0xFF ? 0xFF : sample;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var i, j;
|
||||||
|
for (var blockRow = 0; blockRow < blocksPerColumn; blockRow++) {
|
||||||
|
var scanLine = blockRow << 3;
|
||||||
|
for (i = 0; i < 8; i++)
|
||||||
|
lines.push(new Uint8Array(samplesPerLine));
|
||||||
|
for (var blockCol = 0; blockCol < blocksPerLine; blockCol++) {
|
||||||
|
quantizeAndInverse(component.blocks[blockRow][blockCol], r, R);
|
||||||
|
|
||||||
|
var offset = 0, sample = blockCol << 3;
|
||||||
|
for (j = 0; j < 8; j++) {
|
||||||
|
var line = lines[scanLine + j];
|
||||||
|
for (i = 0; i < 8; i++)
|
||||||
|
line[sample + i] = r[offset++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor.prototype = {
|
||||||
|
load: function load(path) {
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.open("GET", path, true);
|
||||||
|
xhr.responseType = "arraybuffer";
|
||||||
|
xhr.onload = (function() {
|
||||||
|
// TODO catch parse error
|
||||||
|
var data = new Uint8Array(xhr.response || xhr.mozResponseArrayBuffer);
|
||||||
|
this.parse(data);
|
||||||
|
if (this.onload)
|
||||||
|
this.onload();
|
||||||
|
}).bind(this);
|
||||||
|
xhr.send(null);
|
||||||
|
},
|
||||||
|
parse: function parse(data) {
|
||||||
|
var offset = 0, length = data.length;
|
||||||
|
function readUint16() {
|
||||||
|
var value = (data[offset] << 8) | data[offset + 1];
|
||||||
|
offset += 2;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
function readDataBlock() {
|
||||||
|
var length = readUint16();
|
||||||
|
var array = data.subarray(offset, offset + length - 2);
|
||||||
|
offset += array.length;
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
function prepareComponents(frame) {
|
||||||
|
var maxH = 0, maxV = 0;
|
||||||
|
var component, componentId;
|
||||||
|
for (componentId in frame.components) {
|
||||||
|
if (frame.components.hasOwnProperty(componentId)) {
|
||||||
|
component = frame.components[componentId];
|
||||||
|
if (maxH < component.h) maxH = component.h;
|
||||||
|
if (maxV < component.v) maxV = component.v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var mcusPerLine = Math.ceil(frame.samplesPerLine / 8 / maxH);
|
||||||
|
var mcusPerColumn = Math.ceil(frame.scanLines / 8 / maxV);
|
||||||
|
for (componentId in frame.components) {
|
||||||
|
if (frame.components.hasOwnProperty(componentId)) {
|
||||||
|
component = frame.components[componentId];
|
||||||
|
var blocksPerLine = Math.ceil(Math.ceil(frame.samplesPerLine / 8) * component.h / maxH);
|
||||||
|
var blocksPerColumn = Math.ceil(Math.ceil(frame.scanLines / 8) * component.v / maxV);
|
||||||
|
var blocksPerLineForMcu = mcusPerLine * component.h;
|
||||||
|
var blocksPerColumnForMcu = mcusPerColumn * component.v;
|
||||||
|
var blocks = [];
|
||||||
|
for (var i = 0; i < blocksPerColumnForMcu; i++) {
|
||||||
|
var row = [];
|
||||||
|
for (var j = 0; j < blocksPerLineForMcu; j++)
|
||||||
|
row.push(new Int32Array(64));
|
||||||
|
blocks.push(row);
|
||||||
|
}
|
||||||
|
component.blocksPerLine = blocksPerLine;
|
||||||
|
component.blocksPerColumn = blocksPerColumn;
|
||||||
|
component.blocks = blocks;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
frame.maxH = maxH;
|
||||||
|
frame.maxV = maxV;
|
||||||
|
frame.mcusPerLine = mcusPerLine;
|
||||||
|
frame.mcusPerColumn = mcusPerColumn;
|
||||||
|
}
|
||||||
|
var jfif = null;
|
||||||
|
var adobe = null;
|
||||||
|
var pixels = null;
|
||||||
|
var frame, resetInterval;
|
||||||
|
var quantizationTables = [], frames = [];
|
||||||
|
var huffmanTablesAC = [], huffmanTablesDC = [];
|
||||||
|
var fileMarker = readUint16();
|
||||||
|
if (fileMarker != 0xFFD8) { // SOI (Start of Image)
|
||||||
|
throw "SOI not found";
|
||||||
|
}
|
||||||
|
|
||||||
|
fileMarker = readUint16();
|
||||||
|
while (fileMarker != 0xFFD9) { // EOI (End of image)
|
||||||
|
var i, j, l;
|
||||||
|
switch(fileMarker) {
|
||||||
|
case 0xFFE0: // APP0 (Application Specific)
|
||||||
|
case 0xFFE1: // APP1
|
||||||
|
case 0xFFE2: // APP2
|
||||||
|
case 0xFFE3: // APP3
|
||||||
|
case 0xFFE4: // APP4
|
||||||
|
case 0xFFE5: // APP5
|
||||||
|
case 0xFFE6: // APP6
|
||||||
|
case 0xFFE7: // APP7
|
||||||
|
case 0xFFE8: // APP8
|
||||||
|
case 0xFFE9: // APP9
|
||||||
|
case 0xFFEA: // APP10
|
||||||
|
case 0xFFEB: // APP11
|
||||||
|
case 0xFFEC: // APP12
|
||||||
|
case 0xFFED: // APP13
|
||||||
|
case 0xFFEE: // APP14
|
||||||
|
case 0xFFEF: // APP15
|
||||||
|
case 0xFFFE: // COM (Comment)
|
||||||
|
var appData = readDataBlock();
|
||||||
|
|
||||||
|
if (fileMarker === 0xFFE0) {
|
||||||
|
if (appData[0] === 0x4A && appData[1] === 0x46 && appData[2] === 0x49 &&
|
||||||
|
appData[3] === 0x46 && appData[4] === 0) { // 'JFIF\x00'
|
||||||
|
jfif = {
|
||||||
|
version: { major: appData[5], minor: appData[6] },
|
||||||
|
densityUnits: appData[7],
|
||||||
|
xDensity: (appData[8] << 8) | appData[9],
|
||||||
|
yDensity: (appData[10] << 8) | appData[11],
|
||||||
|
thumbWidth: appData[12],
|
||||||
|
thumbHeight: appData[13],
|
||||||
|
thumbData: appData.subarray(14, 14 + 3 * appData[12] * appData[13])
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO APP1 - Exif
|
||||||
|
if (fileMarker === 0xFFEE) {
|
||||||
|
if (appData[0] === 0x41 && appData[1] === 0x64 && appData[2] === 0x6F &&
|
||||||
|
appData[3] === 0x62 && appData[4] === 0x65 && appData[5] === 0) { // 'Adobe\x00'
|
||||||
|
adobe = {
|
||||||
|
version: appData[6],
|
||||||
|
flags0: (appData[7] << 8) | appData[8],
|
||||||
|
flags1: (appData[9] << 8) | appData[10],
|
||||||
|
transformCode: appData[11]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0xFFDB: // DQT (Define Quantization Tables)
|
||||||
|
var quantizationTableCount = Math.floor((readUint16() - 2) / 65);
|
||||||
|
for (i = 0; i < quantizationTableCount; i++) {
|
||||||
|
var quantizationTableSpec = data[offset++];
|
||||||
|
var tableData = new Int32Array(64);
|
||||||
|
if ((quantizationTableSpec >> 4) === 0) { // 8 bit values
|
||||||
|
for (j = 0; j < 64; j++) {
|
||||||
|
var z = dctZigZag[j];
|
||||||
|
tableData[z] = data[offset++];
|
||||||
|
}
|
||||||
|
} else if ((quantizationTableSpec >> 4) === 1) { //16 bit
|
||||||
|
tableData[j] = readUint16();
|
||||||
|
} else
|
||||||
|
throw "DQT: invalid table spec";
|
||||||
|
quantizationTables[quantizationTableSpec & 15] = tableData;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0xFFC0: // SOF0 (Start of Frame, Baseline DCT)
|
||||||
|
case 0xFFC2: // SOF2 (Start of Frame, Progressive DCT)
|
||||||
|
readUint16(); // skip data length
|
||||||
|
frame = {};
|
||||||
|
frame.progressive = (fileMarker === 0xFFC2);
|
||||||
|
frame.precision = data[offset++];
|
||||||
|
frame.scanLines = readUint16();
|
||||||
|
frame.samplesPerLine = readUint16();
|
||||||
|
frame.components = [];
|
||||||
|
var componentsCount = data[offset++], componentId;
|
||||||
|
var maxH = 0, maxV = 0;
|
||||||
|
for (i = 0; i < componentsCount; i++) {
|
||||||
|
componentId = data[offset];
|
||||||
|
var h = data[offset + 1] >> 4;
|
||||||
|
var v = data[offset + 1] & 15;
|
||||||
|
var qId = data[offset + 2];
|
||||||
|
frame.components[componentId] = {
|
||||||
|
h: h,
|
||||||
|
v: v,
|
||||||
|
quantizationTable: quantizationTables[qId]
|
||||||
|
};
|
||||||
|
offset += 3;
|
||||||
|
}
|
||||||
|
prepareComponents(frame);
|
||||||
|
frames.push(frame);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0xFFC4: // DHT (Define Huffman Tables)
|
||||||
|
var huffmanLength = readUint16();
|
||||||
|
for (i = 2; i < huffmanLength;) {
|
||||||
|
var huffmanTableSpec = data[offset++];
|
||||||
|
var codeLengths = new Uint8Array(16);
|
||||||
|
var codeLengthSum = 0;
|
||||||
|
for (j = 0; j < 16; j++, offset++)
|
||||||
|
codeLengthSum += (codeLengths[j] = data[offset]);
|
||||||
|
var huffmanValues = new Uint8Array(codeLengthSum);
|
||||||
|
for (j = 0; j < codeLengthSum; j++, offset++)
|
||||||
|
huffmanValues[j] = data[offset];
|
||||||
|
i += 17 + codeLengthSum;
|
||||||
|
|
||||||
|
((huffmanTableSpec >> 4) === 0 ?
|
||||||
|
huffmanTablesDC : huffmanTablesAC)[huffmanTableSpec & 15] =
|
||||||
|
buildHuffmanTable(codeLengths, huffmanValues);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0xFFDD: // DRI (Define Restart Interval)
|
||||||
|
readUint16(); // skip data length
|
||||||
|
resetInterval = readUint16();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0xFFDA: // SOS (Start of Scan)
|
||||||
|
var scanLength = readUint16();
|
||||||
|
var selectorsCount = data[offset++];
|
||||||
|
var components = [], component;
|
||||||
|
for (i = 0; i < selectorsCount; i++) {
|
||||||
|
component = frame.components[data[offset++]];
|
||||||
|
var tableSpec = data[offset++];
|
||||||
|
component.huffmanTableDC = huffmanTablesDC[tableSpec >> 4];
|
||||||
|
component.huffmanTableAC = huffmanTablesAC[tableSpec & 15];
|
||||||
|
components.push(component);
|
||||||
|
}
|
||||||
|
var spectralStart = data[offset++];
|
||||||
|
var spectralEnd = data[offset++];
|
||||||
|
var successiveApproximation = data[offset++];
|
||||||
|
var processed = decodeScan(data, offset,
|
||||||
|
frame, components, resetInterval,
|
||||||
|
spectralStart, spectralEnd,
|
||||||
|
successiveApproximation >> 4, successiveApproximation & 15);
|
||||||
|
offset += processed;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw "unknown JPEG marker " + fileMarker.toString(16);
|
||||||
|
}
|
||||||
|
fileMarker = readUint16();
|
||||||
|
}
|
||||||
|
if (frames.length != 1)
|
||||||
|
throw "only single frame JPEGs supported";
|
||||||
|
|
||||||
|
this.width = frame.samplesPerLine;
|
||||||
|
this.height = frame.scanLines;
|
||||||
|
this.jfif = jfif;
|
||||||
|
this.adobe = adobe;
|
||||||
|
this.components = [];
|
||||||
|
for (var id in frame.components) {
|
||||||
|
if (frame.components.hasOwnProperty(id)) {
|
||||||
|
this.components.push({
|
||||||
|
lines: buildComponentData(frame, frame.components[id]),
|
||||||
|
scaleX: frame.components[id].h / frame.maxH,
|
||||||
|
scaleY: frame.components[id].v / frame.maxV
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getData: function getData(width, height) {
|
||||||
|
function clampTo8bit(a) {
|
||||||
|
return a < 0 ? 0 : a > 255 ? 255 : a;
|
||||||
|
}
|
||||||
|
var scaleX = this.width / width, scaleY = this.height / height;
|
||||||
|
|
||||||
|
var component1, component2, component3, component4;
|
||||||
|
var component1Line, component2Line, component3Line, component4Line;
|
||||||
|
var x, y;
|
||||||
|
var offset = 0;
|
||||||
|
var Y, Cb, Cr, K, C, M, Ye, R, G, B;
|
||||||
|
var colorTransform;
|
||||||
|
var dataLength = width * height * this.components.length;
|
||||||
|
var data = new Uint8Array(dataLength);
|
||||||
|
switch (this.components.length) {
|
||||||
|
case 1:
|
||||||
|
component1 = this.components[0];
|
||||||
|
for (y = 0; y < height; y++) {
|
||||||
|
component1Line = component1.lines[0 | (y * component1.scaleY * scaleY)];
|
||||||
|
for (x = 0; x < width; x++) {
|
||||||
|
Y = component1Line[0 | (x * component1.scaleX * scaleX)];
|
||||||
|
|
||||||
|
data[offset++] = Y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
// The default transform for three components is true
|
||||||
|
colorTransform = true;
|
||||||
|
// The adobe transform marker overrides any previous setting
|
||||||
|
if (this.adobe && this.adobe.transformCode)
|
||||||
|
colorTransform = true;
|
||||||
|
else if (typeof this.colorTransform !== 'undefined')
|
||||||
|
colorTransform = !!this.colorTransform;
|
||||||
|
|
||||||
|
component1 = this.components[0];
|
||||||
|
component2 = this.components[1];
|
||||||
|
component3 = this.components[2];
|
||||||
|
for (y = 0; y < height; y++) {
|
||||||
|
component1Line = component1.lines[0 | (y * component1.scaleY * scaleY)];
|
||||||
|
component2Line = component2.lines[0 | (y * component2.scaleY * scaleY)];
|
||||||
|
component3Line = component3.lines[0 | (y * component3.scaleY * scaleY)];
|
||||||
|
for (x = 0; x < width; x++) {
|
||||||
|
if (!colorTransform) {
|
||||||
|
R = component1Line[0 | (x * component1.scaleX * scaleX)];
|
||||||
|
G = component2Line[0 | (x * component2.scaleX * scaleX)];
|
||||||
|
B = component3Line[0 | (x * component3.scaleX * scaleX)];
|
||||||
|
} else {
|
||||||
|
Y = component1Line[0 | (x * component1.scaleX * scaleX)];
|
||||||
|
Cb = component2Line[0 | (x * component2.scaleX * scaleX)];
|
||||||
|
Cr = component3Line[0 | (x * component3.scaleX * scaleX)];
|
||||||
|
|
||||||
|
R = clampTo8bit(Y + 1.402 * (Cr - 128));
|
||||||
|
G = clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
|
||||||
|
B = clampTo8bit(Y + 1.772 * (Cb - 128));
|
||||||
|
}
|
||||||
|
|
||||||
|
data[offset++] = R;
|
||||||
|
data[offset++] = G;
|
||||||
|
data[offset++] = B;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
if (!this.adobe)
|
||||||
|
throw 'Unsupported color mode (4 components)';
|
||||||
|
// The default transform for four components is false
|
||||||
|
colorTransform = false;
|
||||||
|
// The adobe transform marker overrides any previous setting
|
||||||
|
if (this.adobe && this.adobe.transformCode)
|
||||||
|
colorTransform = true;
|
||||||
|
else if (typeof this.colorTransform !== 'undefined')
|
||||||
|
colorTransform = !!this.colorTransform;
|
||||||
|
|
||||||
|
component1 = this.components[0];
|
||||||
|
component2 = this.components[1];
|
||||||
|
component3 = this.components[2];
|
||||||
|
component4 = this.components[3];
|
||||||
|
for (y = 0; y < height; y++) {
|
||||||
|
component1Line = component1.lines[0 | (y * component1.scaleY * scaleY)];
|
||||||
|
component2Line = component2.lines[0 | (y * component2.scaleY * scaleY)];
|
||||||
|
component3Line = component3.lines[0 | (y * component3.scaleY * scaleY)];
|
||||||
|
component4Line = component4.lines[0 | (y * component4.scaleY * scaleY)];
|
||||||
|
for (x = 0; x < width; x++) {
|
||||||
|
if (!colorTransform) {
|
||||||
|
C = component1Line[0 | (x * component1.scaleX * scaleX)];
|
||||||
|
M = component2Line[0 | (x * component2.scaleX * scaleX)];
|
||||||
|
Ye = component3Line[0 | (x * component3.scaleX * scaleX)];
|
||||||
|
K = component4Line[0 | (x * component4.scaleX * scaleX)];
|
||||||
|
} else {
|
||||||
|
Y = component1Line[0 | (x * component1.scaleX * scaleX)];
|
||||||
|
Cb = component2Line[0 | (x * component2.scaleX * scaleX)];
|
||||||
|
Cr = component3Line[0 | (x * component3.scaleX * scaleX)];
|
||||||
|
K = component4Line[0 | (x * component4.scaleX * scaleX)];
|
||||||
|
|
||||||
|
C = 255 - clampTo8bit(Y + 1.402 * (Cr - 128));
|
||||||
|
M = 255 - clampTo8bit(Y - 0.3441363 * (Cb - 128) - 0.71413636 * (Cr - 128));
|
||||||
|
Ye = 255 - clampTo8bit(Y + 1.772 * (Cb - 128));
|
||||||
|
}
|
||||||
|
data[offset++] = C;
|
||||||
|
data[offset++] = M;
|
||||||
|
data[offset++] = Ye;
|
||||||
|
data[offset++] = K;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw 'Unsupported color mode';
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
copyToImageData: function copyToImageData(imageData) {
|
||||||
|
var width = imageData.width, height = imageData.height;
|
||||||
|
var imageDataArray = imageData.data;
|
||||||
|
var data = this.getData(width, height);
|
||||||
|
var i = 0, j = 0, x, y;
|
||||||
|
var Y, K, C, M, R, G, B;
|
||||||
|
switch (this.components.length) {
|
||||||
|
case 1:
|
||||||
|
for (y = 0; y < height; y++) {
|
||||||
|
for (x = 0; x < width; x++) {
|
||||||
|
Y = data[i++];
|
||||||
|
|
||||||
|
imageDataArray[j++] = Y;
|
||||||
|
imageDataArray[j++] = Y;
|
||||||
|
imageDataArray[j++] = Y;
|
||||||
|
imageDataArray[j++] = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
for (y = 0; y < height; y++) {
|
||||||
|
for (x = 0; x < width; x++) {
|
||||||
|
R = data[i++];
|
||||||
|
G = data[i++];
|
||||||
|
B = data[i++];
|
||||||
|
|
||||||
|
imageDataArray[j++] = R;
|
||||||
|
imageDataArray[j++] = G;
|
||||||
|
imageDataArray[j++] = B;
|
||||||
|
imageDataArray[j++] = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
for (y = 0; y < height; y++) {
|
||||||
|
for (x = 0; x < width; x++) {
|
||||||
|
C = data[i++];
|
||||||
|
M = data[i++];
|
||||||
|
Y = data[i++];
|
||||||
|
K = data[i++];
|
||||||
|
|
||||||
|
R = 255 - clampTo8bit(C * (1 - K / 255) + K);
|
||||||
|
G = 255 - clampTo8bit(M * (1 - K / 255) + K);
|
||||||
|
B = 255 - clampTo8bit(Y * (1 - K / 255) + K);
|
||||||
|
|
||||||
|
imageDataArray[j++] = R;
|
||||||
|
imageDataArray[j++] = G;
|
||||||
|
imageDataArray[j++] = B;
|
||||||
|
imageDataArray[j++] = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw 'Unsupported color mode';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return constructor;
|
||||||
|
})();
|
176
src/canvas.js
176
src/canvas.js
@ -59,15 +59,121 @@ function ScratchCanvas(width, height) {
|
|||||||
return canvas;
|
return canvas;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function addContextCurrentTransform(ctx) {
|
||||||
|
// If the context doesn't expose a `mozCurrentTransform`, add a JS based on.
|
||||||
|
if (!ctx.mozCurrentTransform) {
|
||||||
|
// Store the original context
|
||||||
|
ctx._originalSave = ctx.save;
|
||||||
|
ctx._originalRestore = ctx.restore;
|
||||||
|
ctx._originalRotate = ctx.rotate;
|
||||||
|
ctx._originalScale = ctx.scale;
|
||||||
|
ctx._originalTranslate = ctx.translate;
|
||||||
|
ctx._originalTransform = ctx.transform;
|
||||||
|
|
||||||
|
ctx._transformMatrix = [1, 0, 0, 1, 0, 0];
|
||||||
|
ctx._transformStack = [];
|
||||||
|
|
||||||
|
Object.defineProperty(ctx, 'mozCurrentTransform', {
|
||||||
|
get: function getCurrentTransform() {
|
||||||
|
return this._transformMatrix;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(ctx, 'mozCurrentTransformInverse', {
|
||||||
|
get: function getCurrentTransformInverse() {
|
||||||
|
// Calculation done using WolframAlpha:
|
||||||
|
// http://www.wolframalpha.com/input/?
|
||||||
|
// i=Inverse+{{a%2C+c%2C+e}%2C+{b%2C+d%2C+f}%2C+{0%2C+0%2C+1}}
|
||||||
|
|
||||||
|
var m = this._transformMatrix;
|
||||||
|
var a = m[0], b = m[1], c = m[2], d = m[3], e = m[4], f = m[5];
|
||||||
|
|
||||||
|
var ad_bc = a * d - b * c;
|
||||||
|
var bc_ad = b * c - a * d;
|
||||||
|
|
||||||
|
return [
|
||||||
|
d / ad_bc,
|
||||||
|
b / bc_ad,
|
||||||
|
c / bc_ad,
|
||||||
|
a / ad_bc,
|
||||||
|
(d * e - c * f) / bc_ad,
|
||||||
|
(b * e - a * f) / ad_bc
|
||||||
|
];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.save = function ctxSave() {
|
||||||
|
var old = this._transformMatrix;
|
||||||
|
this._transformStack.push(old);
|
||||||
|
this._transformMatrix = old.slice(0, 6);
|
||||||
|
|
||||||
|
this._originalSave();
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.restore = function ctxRestore() {
|
||||||
|
var prev = this._transformStack.pop();
|
||||||
|
if (prev) {
|
||||||
|
this._transformMatrix = prev;
|
||||||
|
this._originalRestore();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.translate = function ctxTranslate(x, y) {
|
||||||
|
var m = this._transformMatrix;
|
||||||
|
m[4] = m[0] * x + m[2] * y + m[4];
|
||||||
|
m[5] = m[1] * x + m[3] * y + m[5];
|
||||||
|
|
||||||
|
this._originalTranslate(x, y);
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.scale = function ctxScale(x, y) {
|
||||||
|
var m = this._transformMatrix;
|
||||||
|
m[0] = m[0] * x;
|
||||||
|
m[1] = m[1] * x;
|
||||||
|
m[2] = m[2] * y;
|
||||||
|
m[3] = m[3] * y;
|
||||||
|
|
||||||
|
this._originalScale(x, y);
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.transform = function ctxTransform(a, b, c, d, e, f) {
|
||||||
|
var m = this._transformMatrix;
|
||||||
|
this._transformMatrix = [
|
||||||
|
m[0] * a + m[2] * b,
|
||||||
|
m[1] * a + m[3] * b,
|
||||||
|
m[0] * c + m[2] * d,
|
||||||
|
m[1] * c + m[3] * d,
|
||||||
|
m[0] * e + m[2] * f + m[4],
|
||||||
|
m[1] * e + m[3] * f + m[5]
|
||||||
|
];
|
||||||
|
|
||||||
|
ctx._originalTransform(a, b, c, d, e, f);
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.rotate = function ctxRotate(angle) {
|
||||||
|
var cosValue = Math.cos(angle);
|
||||||
|
var sinValue = Math.sin(angle);
|
||||||
|
|
||||||
|
var m = this._transformMatrix;
|
||||||
|
this._transformMatrix = [
|
||||||
|
m[0] * cosValue + m[2] * sinValue,
|
||||||
|
m[1] * cosValue + m[3] * sinValue,
|
||||||
|
m[0] * (-sinValue) + m[2] * cosValue,
|
||||||
|
m[1] * (-sinValue) + m[3] * cosValue,
|
||||||
|
m[4],
|
||||||
|
m[5]
|
||||||
|
];
|
||||||
|
|
||||||
|
this._originalRotate(angle);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var CanvasGraphics = (function canvasGraphics() {
|
var CanvasGraphics = (function canvasGraphics() {
|
||||||
// Defines the time the executeIRQueue is going to be executing
|
// Defines the time the executeIRQueue is going to be executing
|
||||||
// before it stops and shedules a continue of execution.
|
// before it stops and shedules a continue of execution.
|
||||||
var kExecutionTime = 50;
|
var kExecutionTime = 50;
|
||||||
|
|
||||||
// Number of IR commands to execute before checking
|
|
||||||
// if we execute longer then `kExecutionTime`.
|
|
||||||
var kExecutionTimeCheck = 500;
|
|
||||||
|
|
||||||
function constructor(canvasCtx, objs) {
|
function constructor(canvasCtx, objs) {
|
||||||
this.ctx = canvasCtx;
|
this.ctx = canvasCtx;
|
||||||
this.current = new CanvasExtraState();
|
this.current = new CanvasExtraState();
|
||||||
@ -77,6 +183,10 @@ var CanvasGraphics = (function canvasGraphics() {
|
|||||||
this.xobjs = null;
|
this.xobjs = null;
|
||||||
this.ScratchCanvas = ScratchCanvas;
|
this.ScratchCanvas = ScratchCanvas;
|
||||||
this.objs = objs;
|
this.objs = objs;
|
||||||
|
|
||||||
|
if (canvasCtx) {
|
||||||
|
addContextCurrentTransform(canvasCtx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var LINE_CAP_STYLES = ['butt', 'round', 'square'];
|
var LINE_CAP_STYLES = ['butt', 'round', 'square'];
|
||||||
@ -112,31 +222,33 @@ var CanvasGraphics = (function canvasGraphics() {
|
|||||||
var i = executionStartIdx || 0;
|
var i = executionStartIdx || 0;
|
||||||
var argsArrayLen = argsArray.length;
|
var argsArrayLen = argsArray.length;
|
||||||
|
|
||||||
|
// Sometimes the IRQueue to execute is empty.
|
||||||
|
if (argsArrayLen == i) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
var executionEndIdx;
|
var executionEndIdx;
|
||||||
var startTime = Date.now();
|
var startTime = Date.now();
|
||||||
|
|
||||||
var objs = this.objs;
|
var objs = this.objs;
|
||||||
|
|
||||||
do {
|
while (true) {
|
||||||
executionEndIdx = Math.min(argsArrayLen, i + kExecutionTimeCheck);
|
if (fnArray[i] !== 'dependency') {
|
||||||
|
this[fnArray[i]].apply(this, argsArray[i]);
|
||||||
|
} else {
|
||||||
|
var deps = argsArray[i];
|
||||||
|
for (var n = 0, nn = deps.length; n < nn; n++) {
|
||||||
|
var depObjId = deps[n];
|
||||||
|
|
||||||
for (i; i < executionEndIdx; i++) {
|
// If the promise isn't resolved yet, add the continueCallback
|
||||||
if (fnArray[i] !== 'dependency') {
|
// to the promise and bail out.
|
||||||
this[fnArray[i]].apply(this, argsArray[i]);
|
if (!objs.isResolved(depObjId)) {
|
||||||
} else {
|
objs.get(depObjId, continueCallback);
|
||||||
var deps = argsArray[i];
|
return i;
|
||||||
for (var n = 0, nn = deps.length; n < nn; n++) {
|
|
||||||
var depObjId = deps[n];
|
|
||||||
|
|
||||||
// If the promise isn't resolved yet, add the continueCallback
|
|
||||||
// to the promise and bail out.
|
|
||||||
if (!objs.isResolved(depObjId)) {
|
|
||||||
objs.get(depObjId, continueCallback);
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
|
|
||||||
// If the entire IRQueue was executed, stop as were done.
|
// If the entire IRQueue was executed, stop as were done.
|
||||||
if (i == argsArrayLen) {
|
if (i == argsArrayLen) {
|
||||||
@ -153,7 +265,7 @@ var CanvasGraphics = (function canvasGraphics() {
|
|||||||
|
|
||||||
// If the IRQueue isn't executed completly yet OR the execution time
|
// If the IRQueue isn't executed completly yet OR the execution time
|
||||||
// was short enough, do another execution round.
|
// was short enough, do another execution round.
|
||||||
} while (true);
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
endDrawing: function canvasGraphicsEndDrawing() {
|
endDrawing: function canvasGraphicsEndDrawing() {
|
||||||
@ -425,13 +537,14 @@ var CanvasGraphics = (function canvasGraphics() {
|
|||||||
var charSpacing = current.charSpacing;
|
var charSpacing = current.charSpacing;
|
||||||
var wordSpacing = current.wordSpacing;
|
var wordSpacing = current.wordSpacing;
|
||||||
var textHScale = current.textHScale;
|
var textHScale = current.textHScale;
|
||||||
|
var fontMatrix = font.fontMatrix || IDENTITY_MATRIX;
|
||||||
|
var textHScale2 = textHScale * fontMatrix[0];
|
||||||
var glyphsLength = glyphs.length;
|
var glyphsLength = glyphs.length;
|
||||||
if (font.coded) {
|
if (font.coded) {
|
||||||
ctx.save();
|
ctx.save();
|
||||||
ctx.transform.apply(ctx, current.textMatrix);
|
ctx.transform.apply(ctx, current.textMatrix);
|
||||||
ctx.translate(current.x, current.y);
|
ctx.translate(current.x, current.y);
|
||||||
|
|
||||||
var fontMatrix = font.fontMatrix || IDENTITY_MATRIX;
|
|
||||||
ctx.scale(1 / textHScale, 1);
|
ctx.scale(1 / textHScale, 1);
|
||||||
for (var i = 0; i < glyphsLength; ++i) {
|
for (var i = 0; i < glyphsLength; ++i) {
|
||||||
|
|
||||||
@ -452,7 +565,7 @@ var CanvasGraphics = (function canvasGraphics() {
|
|||||||
var width = transformed[0] * fontSize + charSpacing;
|
var width = transformed[0] * fontSize + charSpacing;
|
||||||
|
|
||||||
ctx.translate(width, 0);
|
ctx.translate(width, 0);
|
||||||
current.x += width;
|
current.x += width * textHScale2;
|
||||||
|
|
||||||
}
|
}
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
@ -461,7 +574,7 @@ var CanvasGraphics = (function canvasGraphics() {
|
|||||||
ctx.transform.apply(ctx, current.textMatrix);
|
ctx.transform.apply(ctx, current.textMatrix);
|
||||||
ctx.scale(1, -1);
|
ctx.scale(1, -1);
|
||||||
ctx.translate(current.x, -1 * current.y);
|
ctx.translate(current.x, -1 * current.y);
|
||||||
ctx.transform.apply(ctx, font.fontMatrix || IDENTITY_MATRIX);
|
ctx.transform.apply(ctx, fontMatrix);
|
||||||
|
|
||||||
ctx.scale(1 / textHScale, 1);
|
ctx.scale(1 / textHScale, 1);
|
||||||
|
|
||||||
@ -474,15 +587,13 @@ var CanvasGraphics = (function canvasGraphics() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var unicode = glyph.unicode;
|
var char = glyph.fontChar;
|
||||||
var char = (unicode >= 0x10000) ?
|
|
||||||
String.fromCharCode(0xD800 | ((unicode - 0x10000) >> 10),
|
|
||||||
0xDC00 | (unicode & 0x3FF)) : String.fromCharCode(unicode);
|
|
||||||
|
|
||||||
ctx.fillText(char, width, 0);
|
ctx.fillText(char, width, 0);
|
||||||
width += glyph.width * fontSize * 0.001 + charSpacing;
|
width += glyph.width * fontSize * 0.001 + charSpacing;
|
||||||
|
|
||||||
|
// TODO actual characters can be extracted from the glyph.unicode
|
||||||
}
|
}
|
||||||
current.x += width;
|
current.x += width * textHScale2;
|
||||||
|
|
||||||
ctx.restore();
|
ctx.restore();
|
||||||
}
|
}
|
||||||
@ -492,12 +603,13 @@ var CanvasGraphics = (function canvasGraphics() {
|
|||||||
var ctx = this.ctx;
|
var ctx = this.ctx;
|
||||||
var current = this.current;
|
var current = this.current;
|
||||||
var fontSize = current.fontSize;
|
var fontSize = current.fontSize;
|
||||||
var textHScale = current.textHScale;
|
var textHScale2 = current.textHScale *
|
||||||
|
(current.font.fontMatrix || IDENTITY_MATRIX)[0];
|
||||||
var arrLength = arr.length;
|
var arrLength = arr.length;
|
||||||
for (var i = 0; i < arrLength; ++i) {
|
for (var i = 0; i < arrLength; ++i) {
|
||||||
var e = arr[i];
|
var e = arr[i];
|
||||||
if (isNum(e)) {
|
if (isNum(e)) {
|
||||||
current.x -= e * 0.001 * fontSize * textHScale;
|
current.x -= e * 0.001 * fontSize * textHScale2;
|
||||||
} else if (isString(e)) {
|
} else if (isString(e)) {
|
||||||
this.showText(e);
|
this.showText(e);
|
||||||
} else {
|
} else {
|
||||||
|
@ -24,7 +24,7 @@ var ColorSpace = (function colorSpaceColorSpace() {
|
|||||||
|
|
||||||
constructor.parse = function colorSpaceParse(cs, xref, res) {
|
constructor.parse = function colorSpaceParse(cs, xref, res) {
|
||||||
var IR = constructor.parseToIR(cs, xref, res);
|
var IR = constructor.parseToIR(cs, xref, res);
|
||||||
if (IR instanceof SeparationCS)
|
if (IR instanceof AlternateCS)
|
||||||
return IR;
|
return IR;
|
||||||
|
|
||||||
return constructor.fromIR(IR);
|
return constructor.fromIR(IR);
|
||||||
@ -50,11 +50,12 @@ var ColorSpace = (function colorSpaceColorSpace() {
|
|||||||
var hiVal = IR[2];
|
var hiVal = IR[2];
|
||||||
var lookup = IR[3];
|
var lookup = IR[3];
|
||||||
return new IndexedCS(ColorSpace.fromIR(baseIndexedCS), hiVal, lookup);
|
return new IndexedCS(ColorSpace.fromIR(baseIndexedCS), hiVal, lookup);
|
||||||
case 'SeparationCS':
|
case 'AlternateCS':
|
||||||
var alt = IR[1];
|
var numComps = IR[1];
|
||||||
var tintFnIR = IR[2];
|
var alt = IR[2];
|
||||||
|
var tintFnIR = IR[3];
|
||||||
|
|
||||||
return new SeparationCS(ColorSpace.fromIR(alt),
|
return new AlternateCS(numComps, ColorSpace.fromIR(alt),
|
||||||
PDFFunction.fromIR(tintFnIR));
|
PDFFunction.fromIR(tintFnIR));
|
||||||
default:
|
default:
|
||||||
error('Unkown name ' + name);
|
error('Unkown name ' + name);
|
||||||
@ -134,11 +135,17 @@ var ColorSpace = (function colorSpaceColorSpace() {
|
|||||||
var lookup = xref.fetchIfRef(cs[3]);
|
var lookup = xref.fetchIfRef(cs[3]);
|
||||||
return ['IndexedCS', baseIndexedCS, hiVal, lookup];
|
return ['IndexedCS', baseIndexedCS, hiVal, lookup];
|
||||||
case 'Separation':
|
case 'Separation':
|
||||||
|
case 'DeviceN':
|
||||||
|
var name = cs[1];
|
||||||
|
var numComps = 1;
|
||||||
|
if (isName(name))
|
||||||
|
numComps = 1;
|
||||||
|
else if (isArray(name))
|
||||||
|
numComps = name.length;
|
||||||
var alt = ColorSpace.parseToIR(cs[2], xref, res);
|
var alt = ColorSpace.parseToIR(cs[2], xref, res);
|
||||||
var tintFnIR = PDFFunction.getIR(xref, xref.fetchIfRef(cs[3]));
|
var tintFnIR = PDFFunction.getIR(xref, xref.fetchIfRef(cs[3]));
|
||||||
return ['SeparationCS', alt, tintFnIR];
|
return ['AlternateCS', numComps, alt, tintFnIR];
|
||||||
case 'Lab':
|
case 'Lab':
|
||||||
case 'DeviceN':
|
|
||||||
default:
|
default:
|
||||||
error('unimplemented color space object "' + mode + '"');
|
error('unimplemented color space object "' + mode + '"');
|
||||||
}
|
}
|
||||||
@ -151,33 +158,45 @@ var ColorSpace = (function colorSpaceColorSpace() {
|
|||||||
return constructor;
|
return constructor;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
var SeparationCS = (function separationCS() {
|
/**
|
||||||
function constructor(base, tintFn) {
|
* Alternate color space handles both Separation and DeviceN color spaces. A
|
||||||
this.name = 'Separation';
|
* Separation color space is actually just a DeviceN with one color component.
|
||||||
this.numComps = 1;
|
* Both color spaces use a tinting function to convert colors to a base color
|
||||||
this.defaultColor = [1];
|
* space.
|
||||||
|
*/
|
||||||
|
var AlternateCS = (function alternateCS() {
|
||||||
|
function constructor(numComps, base, tintFn) {
|
||||||
|
this.name = 'Alternate';
|
||||||
|
this.numComps = numComps;
|
||||||
|
this.defaultColor = [];
|
||||||
|
for (var i = 0; i < numComps; ++i)
|
||||||
|
this.defaultColor.push(1);
|
||||||
this.base = base;
|
this.base = base;
|
||||||
this.tintFn = tintFn;
|
this.tintFn = tintFn;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor.prototype = {
|
constructor.prototype = {
|
||||||
getRgb: function sepcs_getRgb(color) {
|
getRgb: function altcs_getRgb(color) {
|
||||||
var tinted = this.tintFn(color);
|
var tinted = this.tintFn(color);
|
||||||
return this.base.getRgb(tinted);
|
return this.base.getRgb(tinted);
|
||||||
},
|
},
|
||||||
getRgbBuffer: function sepcs_getRgbBuffer(input, bits) {
|
getRgbBuffer: function altcs_getRgbBuffer(input, bits) {
|
||||||
var tintFn = this.tintFn;
|
var tintFn = this.tintFn;
|
||||||
var base = this.base;
|
var base = this.base;
|
||||||
var scale = 1 / ((1 << bits) - 1);
|
var scale = 1 / ((1 << bits) - 1);
|
||||||
var length = input.length;
|
var length = input.length;
|
||||||
var pos = 0;
|
var pos = 0;
|
||||||
var numComps = base.numComps;
|
var baseNumComps = base.numComps;
|
||||||
var baseBuf = new Uint8Array(numComps * length);
|
var baseBuf = new Uint8Array(baseNumComps * length);
|
||||||
|
var numComps = this.numComps;
|
||||||
|
var scaled = new Array(numComps);
|
||||||
|
|
||||||
for (var i = 0; i < length; ++i) {
|
for (var i = 0; i < length; i += numComps) {
|
||||||
var scaled = input[i] * scale;
|
for (var z = 0; z < numComps; ++z)
|
||||||
var tinted = tintFn([scaled]);
|
scaled[z] = input[i + z] * scale;
|
||||||
for (var j = 0; j < numComps; ++j)
|
|
||||||
|
var tinted = tintFn(scaled);
|
||||||
|
for (var j = 0; j < baseNumComps; ++j)
|
||||||
baseBuf[pos++] = 255 * tinted[j];
|
baseBuf[pos++] = 255 * tinted[j];
|
||||||
}
|
}
|
||||||
return base.getRgbBuffer(baseBuf, 8);
|
return base.getRgbBuffer(baseBuf, 8);
|
||||||
|
17
src/core.js
17
src/core.js
@ -550,7 +550,7 @@ var PDFDoc = (function pdfDoc() {
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case 'JpegStream':
|
case 'JpegStream':
|
||||||
var IR = data[2];
|
var IR = data[2];
|
||||||
new JpegImage(id, IR, this.objs);
|
new JpegImageLoader(id, IR, this.objs);
|
||||||
break;
|
break;
|
||||||
case 'Font':
|
case 'Font':
|
||||||
var name = data[2];
|
var name = data[2];
|
||||||
@ -558,20 +558,9 @@ var PDFDoc = (function pdfDoc() {
|
|||||||
var properties = data[4];
|
var properties = data[4];
|
||||||
|
|
||||||
if (file) {
|
if (file) {
|
||||||
|
// Rewrap the ArrayBuffer in a stream.
|
||||||
var fontFileDict = new Dict();
|
var fontFileDict = new Dict();
|
||||||
fontFileDict.map = file.dict.map;
|
file = new Stream(file, 0, file.length, fontFileDict);
|
||||||
|
|
||||||
var fontFile = new Stream(file.bytes, file.start,
|
|
||||||
file.end - file.start, fontFileDict);
|
|
||||||
|
|
||||||
// Check if this is a FlateStream. Otherwise just use the created
|
|
||||||
// Stream one. This makes complex_ttf_font.pdf work.
|
|
||||||
var cmf = file.bytes[0];
|
|
||||||
if ((cmf & 0x0f) == 0x08) {
|
|
||||||
file = new FlateStream(fontFile);
|
|
||||||
} else {
|
|
||||||
file = fontFile;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// For now, resolve the font object here direclty. The real font
|
// For now, resolve the font object here direclty. The real font
|
||||||
|
@ -155,6 +155,11 @@ var PartialEvaluator = (function partialEvaluator() {
|
|||||||
font.loadedName = loadedName;
|
font.loadedName = loadedName;
|
||||||
|
|
||||||
var translated = font.translated;
|
var translated = font.translated;
|
||||||
|
// Convert the file to an ArrayBuffer which will be turned back into
|
||||||
|
// a Stream in the main thread.
|
||||||
|
if (translated.file)
|
||||||
|
translated.file = translated.file.getBytes();
|
||||||
|
|
||||||
handler.send('obj', [
|
handler.send('obj', [
|
||||||
loadedName,
|
loadedName,
|
||||||
'Font',
|
'Font',
|
||||||
@ -179,7 +184,7 @@ var PartialEvaluator = (function partialEvaluator() {
|
|||||||
var w = dict.get('Width', 'W');
|
var w = dict.get('Width', 'W');
|
||||||
var h = dict.get('Height', 'H');
|
var h = dict.get('Height', 'H');
|
||||||
|
|
||||||
if (image instanceof JpegStream) {
|
if (image instanceof JpegStream && image.isNative) {
|
||||||
var objId = 'img_' + uniquePrefix + (++self.objIdCounter);
|
var objId = 'img_' + uniquePrefix + (++self.objIdCounter);
|
||||||
handler.send('obj', [objId, 'JpegStream', image.getIR()]);
|
handler.send('obj', [objId, 'JpegStream', image.getIR()]);
|
||||||
|
|
||||||
@ -493,6 +498,8 @@ var PartialEvaluator = (function partialEvaluator() {
|
|||||||
var baseName = encoding.get('BaseEncoding');
|
var baseName = encoding.get('BaseEncoding');
|
||||||
if (baseName)
|
if (baseName)
|
||||||
baseEncoding = Encodings[baseName.name];
|
baseEncoding = Encodings[baseName.name];
|
||||||
|
else
|
||||||
|
hasEncoding = false; // base encoding was not provided
|
||||||
|
|
||||||
// Load the differences between the base and original
|
// Load the differences between the base and original
|
||||||
if (encoding.has('Differences')) {
|
if (encoding.has('Differences')) {
|
||||||
@ -512,6 +519,7 @@ var PartialEvaluator = (function partialEvaluator() {
|
|||||||
error('Encoding is not a Name nor a Dict');
|
error('Encoding is not a Name nor a Dict');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
properties.differences = differences;
|
properties.differences = differences;
|
||||||
properties.baseEncoding = baseEncoding;
|
properties.baseEncoding = baseEncoding;
|
||||||
properties.hasEncoding = hasEncoding;
|
properties.hasEncoding = hasEncoding;
|
||||||
@ -554,9 +562,21 @@ var PartialEvaluator = (function partialEvaluator() {
|
|||||||
var startRange = tokens[j];
|
var startRange = tokens[j];
|
||||||
var endRange = tokens[j + 1];
|
var endRange = tokens[j + 1];
|
||||||
var code = tokens[j + 2];
|
var code = tokens[j + 2];
|
||||||
while (startRange <= endRange) {
|
if (code == 0xFFFF) {
|
||||||
charToUnicode[startRange] = code++;
|
// CMap is broken, assuming code == startRange
|
||||||
++startRange;
|
code = startRange;
|
||||||
|
}
|
||||||
|
if (isArray(code)) {
|
||||||
|
var codeindex = 0;
|
||||||
|
while (startRange <= endRange) {
|
||||||
|
charToUnicode[startRange] = code[codeindex++];
|
||||||
|
++startRange;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (startRange <= endRange) {
|
||||||
|
charToUnicode[startRange] = code++;
|
||||||
|
++startRange;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -595,9 +615,18 @@ var PartialEvaluator = (function partialEvaluator() {
|
|||||||
}
|
}
|
||||||
} else if (byte == 0x3E) {
|
} else if (byte == 0x3E) {
|
||||||
if (token.length) {
|
if (token.length) {
|
||||||
// parsing hex number
|
if (token.length <= 4) {
|
||||||
tokens.push(parseInt(token, 16));
|
// parsing hex number
|
||||||
token = '';
|
tokens.push(parseInt(token, 16));
|
||||||
|
token = '';
|
||||||
|
} else {
|
||||||
|
// parsing hex UTF-16BE numbers
|
||||||
|
var str = [];
|
||||||
|
for (var i = 0, ii = token.length; i < ii; i += 4)
|
||||||
|
str.push(parseInt(token.substr(i, 4), 16));
|
||||||
|
tokens.push(String.fromCharCode.apply(String, str));
|
||||||
|
token = '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
token += String.fromCharCode(byte);
|
token += String.fromCharCode(byte);
|
||||||
|
319
src/fonts.js
319
src/fonts.js
@ -719,20 +719,10 @@ function getUnicodeRangeFor(value) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
function adaptUnicode(unicode) {
|
|
||||||
return (unicode <= 0x1F || (unicode >= 127 && unicode < kSizeOfGlyphArea)) ?
|
|
||||||
unicode + kCmapGlyphOffset : unicode;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isAdaptedUnicode(unicode) {
|
|
||||||
return unicode >= kCmapGlyphOffset &&
|
|
||||||
unicode < kCmapGlyphOffset + kSizeOfGlyphArea;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isSpecialUnicode(unicode) {
|
function isSpecialUnicode(unicode) {
|
||||||
return (unicode <= 0x1F || (unicode >= 127 && unicode < kSizeOfGlyphArea)) ||
|
return (unicode <= 0x1F || (unicode >= 127 && unicode < kSizeOfGlyphArea)) ||
|
||||||
unicode >= kCmapGlyphOffset &&
|
(unicode >= kCmapGlyphOffset &&
|
||||||
unicode < kCmapGlyphOffset + kSizeOfGlyphArea;
|
unicode < kCmapGlyphOffset + kSizeOfGlyphArea);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -771,16 +761,21 @@ var Font = (function Font() {
|
|||||||
this.widths = properties.widths;
|
this.widths = properties.widths;
|
||||||
this.defaultWidth = properties.defaultWidth;
|
this.defaultWidth = properties.defaultWidth;
|
||||||
this.composite = properties.composite;
|
this.composite = properties.composite;
|
||||||
this.toUnicode = properties.toUnicode;
|
|
||||||
this.hasEncoding = properties.hasEncoding;
|
this.hasEncoding = properties.hasEncoding;
|
||||||
|
|
||||||
this.fontMatrix = properties.fontMatrix;
|
this.fontMatrix = properties.fontMatrix;
|
||||||
|
this.widthMultiplier = 1.0;
|
||||||
if (properties.type == 'Type3')
|
if (properties.type == 'Type3')
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Trying to fix encoding using glyph CIDSystemInfo.
|
// Trying to fix encoding using glyph CIDSystemInfo.
|
||||||
this.loadCidToUnicode(properties);
|
this.loadCidToUnicode(properties);
|
||||||
|
|
||||||
|
if (properties.toUnicode)
|
||||||
|
this.toUnicode = properties.toUnicode;
|
||||||
|
else
|
||||||
|
this.rebuildToUnicode(properties);
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
// The file data is not specified. Trying to fix the font name
|
// The file data is not specified. Trying to fix the font name
|
||||||
// to be used with the canvas.font.
|
// to be used with the canvas.font.
|
||||||
@ -832,6 +827,8 @@ var Font = (function Font() {
|
|||||||
|
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.fontMatrix = properties.fontMatrix;
|
this.fontMatrix = properties.fontMatrix;
|
||||||
|
this.widthMultiplier = !properties.fontMatrix ? 1.0 :
|
||||||
|
1.0 / properties.fontMatrix[0];
|
||||||
this.encoding = properties.baseEncoding;
|
this.encoding = properties.baseEncoding;
|
||||||
this.hasShortCmap = properties.hasShortCmap;
|
this.hasShortCmap = properties.hasShortCmap;
|
||||||
this.loadedName = getUniqueName();
|
this.loadedName = getUniqueName();
|
||||||
@ -961,15 +958,15 @@ var Font = (function Font() {
|
|||||||
var ranges = [];
|
var ranges = [];
|
||||||
for (var n = 0; n < length; ) {
|
for (var n = 0; n < length; ) {
|
||||||
var start = codes[n].unicode;
|
var start = codes[n].unicode;
|
||||||
var startCode = codes[n].code;
|
var codeIndices = [codes[n].code];
|
||||||
++n;
|
++n;
|
||||||
var end = start;
|
var end = start;
|
||||||
while (n < length && end + 1 == codes[n].unicode) {
|
while (n < length && end + 1 == codes[n].unicode) {
|
||||||
|
codeIndices.push(codes[n].code);
|
||||||
++end;
|
++end;
|
||||||
++n;
|
++n;
|
||||||
}
|
}
|
||||||
var endCode = codes[n - 1].code;
|
ranges.push([start, end, codeIndices]);
|
||||||
ranges.push([start, end, startCode, endCode]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ranges;
|
return ranges;
|
||||||
@ -1012,17 +1009,16 @@ var Font = (function Font() {
|
|||||||
idDeltas += string16(0);
|
idDeltas += string16(0);
|
||||||
idRangeOffsets += string16(offset);
|
idRangeOffsets += string16(offset);
|
||||||
|
|
||||||
var startCode = range[2];
|
var codes = range[2];
|
||||||
var endCode = range[3];
|
for (var j = 0, jj = codes.length; j < jj; ++j)
|
||||||
for (var j = startCode; j <= endCode; ++j)
|
glyphsIds += string16(deltas[codes[j]]);
|
||||||
glyphsIds += string16(deltas[j]);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (var i = 0; i < segCount - 1; i++) {
|
for (var i = 0; i < segCount - 1; i++) {
|
||||||
var range = ranges[i];
|
var range = ranges[i];
|
||||||
var start = range[0];
|
var start = range[0];
|
||||||
var end = range[1];
|
var end = range[1];
|
||||||
var startCode = range[2];
|
var startCode = range[2][0];
|
||||||
|
|
||||||
startCount += string16(start);
|
startCount += string16(start);
|
||||||
endCount += string16(end);
|
endCount += string16(end);
|
||||||
@ -1299,7 +1295,7 @@ var Font = (function Font() {
|
|||||||
properties.baseEncoding = encoding;
|
properties.baseEncoding = encoding;
|
||||||
}
|
}
|
||||||
|
|
||||||
function replaceCMapTable(cmap, font, properties) {
|
function readCMapTable(cmap, font) {
|
||||||
var start = (font.start ? font.start : 0) + cmap.offset;
|
var start = (font.start ? font.start : 0) + cmap.offset;
|
||||||
font.pos = start;
|
font.pos = start;
|
||||||
|
|
||||||
@ -1316,7 +1312,7 @@ var Font = (function Font() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check that table are sorted by platformID then encodingID,
|
// Check that table are sorted by platformID then encodingID,
|
||||||
records.sort(function fontReplaceCMapTableSort(a, b) {
|
records.sort(function fontReadCMapTableSort(a, b) {
|
||||||
return ((a.platformID << 16) + a.encodingID) -
|
return ((a.platformID << 16) + a.encodingID) -
|
||||||
((b.platformID << 16) + b.encodingID);
|
((b.platformID << 16) + b.encodingID);
|
||||||
});
|
});
|
||||||
@ -1371,16 +1367,15 @@ var Font = (function Font() {
|
|||||||
for (var j = 0; j < 256; j++) {
|
for (var j = 0; j < 256; j++) {
|
||||||
var index = font.getByte();
|
var index = font.getByte();
|
||||||
if (index) {
|
if (index) {
|
||||||
var unicode = adaptUnicode(j);
|
glyphs.push({ unicode: j, code: j });
|
||||||
glyphs.push({ unicode: unicode, code: j });
|
|
||||||
ids.push(index);
|
ids.push(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
properties.hasShortCmap = true;
|
glyphs: glyphs,
|
||||||
|
ids: ids,
|
||||||
createGlyphNameMap(glyphs, ids, properties);
|
hasShortCmap: true
|
||||||
return cmap.data = createCMapTable(glyphs, ids);
|
};
|
||||||
} else if (format == 4) {
|
} else if (format == 4) {
|
||||||
// re-creating the table in format 4 since the encoding
|
// re-creating the table in format 4 since the encoding
|
||||||
// might be changed
|
// might be changed
|
||||||
@ -1432,17 +1427,18 @@ var Font = (function Font() {
|
|||||||
var glyphCode = offsetIndex < 0 ? j :
|
var glyphCode = offsetIndex < 0 ? j :
|
||||||
offsets[offsetIndex + j - start];
|
offsets[offsetIndex + j - start];
|
||||||
glyphCode = (glyphCode + delta) & 0xFFFF;
|
glyphCode = (glyphCode + delta) & 0xFFFF;
|
||||||
if (glyphCode == 0 || isAdaptedUnicode(j))
|
if (glyphCode == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var unicode = adaptUnicode(j);
|
glyphs.push({ unicode: j, code: j });
|
||||||
glyphs.push({ unicode: unicode, code: j });
|
|
||||||
ids.push(glyphCode);
|
ids.push(glyphCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
createGlyphNameMap(glyphs, ids, properties);
|
return {
|
||||||
return cmap.data = createCMapTable(glyphs, ids);
|
glyphs: glyphs,
|
||||||
|
ids: ids
|
||||||
|
};
|
||||||
} else if (format == 6) {
|
} else if (format == 6) {
|
||||||
// Format 6 is a 2-bytes dense mapping, which means the font data
|
// Format 6 is a 2-bytes dense mapping, which means the font data
|
||||||
// lives glue together even if they are pretty far in the unicode
|
// lives glue together even if they are pretty far in the unicode
|
||||||
@ -1457,19 +1453,18 @@ var Font = (function Font() {
|
|||||||
for (var j = 0; j < entryCount; j++) {
|
for (var j = 0; j < entryCount; j++) {
|
||||||
var glyphCode = int16(font.getBytes(2));
|
var glyphCode = int16(font.getBytes(2));
|
||||||
var code = firstCode + j;
|
var code = firstCode + j;
|
||||||
if (isAdaptedUnicode(glyphCode))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
var unicode = adaptUnicode(code);
|
glyphs.push({ unicode: code, code: code });
|
||||||
glyphs.push({ unicode: unicode, code: code });
|
|
||||||
ids.push(glyphCode);
|
ids.push(glyphCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
createGlyphNameMap(glyphs, ids, properties);
|
return {
|
||||||
return cmap.data = createCMapTable(glyphs, ids);
|
glyphs: glyphs,
|
||||||
|
ids: ids
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cmap.data;
|
error('Unsupported cmap table format');
|
||||||
};
|
};
|
||||||
|
|
||||||
function sanitizeMetrics(font, header, metrics, numGlyphs) {
|
function sanitizeMetrics(font, header, metrics, numGlyphs) {
|
||||||
@ -1708,17 +1703,77 @@ var Font = (function Font() {
|
|||||||
tables.push(cmap);
|
tables.push(cmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
var glyphs = [];
|
var cidToGidMap = properties.cidToGidMap || [];
|
||||||
for (i = 1; i < numGlyphs; i++) {
|
var gidToCidMap = [0];
|
||||||
if (isAdaptedUnicode(i))
|
for (var j = cidToGidMap.length - 1; j >= 0; j--) {
|
||||||
continue;
|
var gid = cidToGidMap[j];
|
||||||
|
if (gid)
|
||||||
glyphs.push({ unicode: adaptUnicode(i) });
|
gidToCidMap[gid] = j;
|
||||||
}
|
}
|
||||||
cmap.data = createCMapTable(glyphs);
|
|
||||||
|
var glyphs = [], ids = [];
|
||||||
|
var usedUnicodes = [];
|
||||||
|
var unassignedUnicodeItems = [];
|
||||||
|
for (var i = 1; i < numGlyphs; i++) {
|
||||||
|
var cid = gidToCidMap[i] || i;
|
||||||
|
var unicode = this.toUnicode[cid];
|
||||||
|
if (!unicode || isSpecialUnicode(unicode) ||
|
||||||
|
unicode in usedUnicodes) {
|
||||||
|
unassignedUnicodeItems.push(i);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
usedUnicodes[unicode] = true;
|
||||||
|
glyphs.push({ unicode: unicode, code: cid });
|
||||||
|
ids.push(i);
|
||||||
|
}
|
||||||
|
// trying to fit as many unassigned symbols as we can
|
||||||
|
// in the range allocated for the user defined symbols
|
||||||
|
var unusedUnicode = kCmapGlyphOffset;
|
||||||
|
for (var j = 0, jj = unassignedUnicodeItems.length; j < jj; j++) {
|
||||||
|
var i = unassignedUnicodeItems[j];
|
||||||
|
var cid = gidToCidMap[i] || i;
|
||||||
|
while (unusedUnicode in usedUnicodes)
|
||||||
|
unusedUnicode++;
|
||||||
|
if (unusedUnicode >= kCmapGlyphOffset + kSizeOfGlyphArea)
|
||||||
|
break;
|
||||||
|
var unicode = unusedUnicode++;
|
||||||
|
this.toUnicode[cid] = unicode;
|
||||||
|
usedUnicodes[unicode] = true;
|
||||||
|
glyphs.push({ unicode: unicode, code: cid });
|
||||||
|
ids.push(i);
|
||||||
|
}
|
||||||
|
cmap.data = createCMapTable(glyphs, ids);
|
||||||
} else {
|
} else {
|
||||||
replaceCMapTable(cmap, font, properties);
|
var cmapTable = readCMapTable(cmap, font);
|
||||||
|
var glyphs = cmapTable.glyphs;
|
||||||
|
var ids = cmapTable.ids;
|
||||||
|
var hasShortCmap = !!cmapTable.hasShortCmap;
|
||||||
|
var toUnicode = this.toUnicode;
|
||||||
|
|
||||||
|
if (hasShortCmap && toUnicode) {
|
||||||
|
// checking if cmap is just identity map
|
||||||
|
var isIdentity = true;
|
||||||
|
for (var i = 0, ii = glyphs.length; i < ii; i++) {
|
||||||
|
if (glyphs[i].unicode != i + 1) {
|
||||||
|
isIdentity = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if it is, replacing with meaningful toUnicode values
|
||||||
|
if (isIdentity) {
|
||||||
|
for (var i = 0, ii = glyphs.length; i < ii; i++) {
|
||||||
|
var unicode = toUnicode[i + 1] || i + 1;
|
||||||
|
glyphs[i].unicode = unicode;
|
||||||
|
}
|
||||||
|
this.useToUnicode = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
properties.hasShortCmap = hasShortCmap;
|
||||||
|
|
||||||
|
createGlyphNameMap(glyphs, ids, properties);
|
||||||
this.glyphNameMap = properties.glyphNameMap;
|
this.glyphNameMap = properties.glyphNameMap;
|
||||||
|
|
||||||
|
cmap.data = createCMapTable(glyphs, ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rewrite the 'post' table if needed
|
// Rewrite the 'post' table if needed
|
||||||
@ -1808,6 +1863,14 @@ var Font = (function Font() {
|
|||||||
}
|
}
|
||||||
properties.baseEncoding = encoding;
|
properties.baseEncoding = encoding;
|
||||||
}
|
}
|
||||||
|
if (properties.subtype == 'CIDFontType0C') {
|
||||||
|
var toUnicode = [];
|
||||||
|
for (var i = 0; i < charstrings.length; ++i) {
|
||||||
|
var charstring = charstrings[i];
|
||||||
|
toUnicode[charstring.code] = charstring.unicode;
|
||||||
|
}
|
||||||
|
this.toUnicode = toUnicode;
|
||||||
|
}
|
||||||
|
|
||||||
var fields = {
|
var fields = {
|
||||||
// PostScript Font Program
|
// PostScript Font Program
|
||||||
@ -1868,8 +1931,11 @@ var Font = (function Font() {
|
|||||||
// Horizontal metrics
|
// Horizontal metrics
|
||||||
'hmtx': (function fontFieldsHmtx() {
|
'hmtx': (function fontFieldsHmtx() {
|
||||||
var hmtx = '\x00\x00\x00\x00'; // Fake .notdef
|
var hmtx = '\x00\x00\x00\x00'; // Fake .notdef
|
||||||
for (var i = 0, ii = charstrings.length; i < ii; i++)
|
for (var i = 0, ii = charstrings.length; i < ii; i++) {
|
||||||
hmtx += string16(charstrings[i].width) + string16(0);
|
var charstring = charstrings[i];
|
||||||
|
var width = 'width' in charstring ? charstring.width : 0;
|
||||||
|
hmtx += string16(width) + string16(0);
|
||||||
|
}
|
||||||
return stringToArray(hmtx);
|
return stringToArray(hmtx);
|
||||||
})(),
|
})(),
|
||||||
|
|
||||||
@ -1898,17 +1964,35 @@ var Font = (function Font() {
|
|||||||
return stringToArray(otf.file);
|
return stringToArray(otf.file);
|
||||||
},
|
},
|
||||||
|
|
||||||
loadCidToUnicode: function font_loadCidToUnicode(properties) {
|
rebuildToUnicode: function font_rebuildToUnicode(properties) {
|
||||||
if (properties.cidToGidMap) {
|
var firstChar = properties.firstChar, lastChar = properties.lastChar;
|
||||||
this.cidToUnicode = properties.cidToGidMap;
|
var map = [];
|
||||||
return;
|
if (properties.composite) {
|
||||||
|
var isIdentityMap = this.cidToUnicode.length == 0;
|
||||||
|
for (var i = firstChar, ii = lastChar; i <= ii; i++) {
|
||||||
|
// TODO missing map the character according font's CMap
|
||||||
|
var cid = i;
|
||||||
|
map[i] = isIdentityMap ? cid : this.cidToUnicode[cid];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (var i = firstChar, ii = lastChar; i <= ii; i++) {
|
||||||
|
var glyph = properties.differences[i];
|
||||||
|
if (!glyph)
|
||||||
|
glyph = properties.baseEncoding[i];
|
||||||
|
if (!!glyph && (glyph in GlyphsUnicode))
|
||||||
|
map[i] = GlyphsUnicode[glyph];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
this.toUnicode = map;
|
||||||
|
},
|
||||||
|
|
||||||
|
loadCidToUnicode: function font_loadCidToUnicode(properties) {
|
||||||
if (!properties.cidSystemInfo)
|
if (!properties.cidSystemInfo)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var cidToUnicodeMap = [];
|
var cidToUnicodeMap = [], unicodeToCIDMap = [];
|
||||||
this.cidToUnicode = cidToUnicodeMap;
|
this.cidToUnicode = cidToUnicodeMap;
|
||||||
|
this.unicodeToCID = unicodeToCIDMap;
|
||||||
|
|
||||||
var cidSystemInfo = properties.cidSystemInfo;
|
var cidSystemInfo = properties.cidSystemInfo;
|
||||||
var cidToUnicode;
|
var cidToUnicode;
|
||||||
@ -1920,28 +2004,34 @@ var Font = (function Font() {
|
|||||||
if (!cidToUnicode)
|
if (!cidToUnicode)
|
||||||
return; // identity encoding
|
return; // identity encoding
|
||||||
|
|
||||||
var glyph = 1, i, j, k, ii;
|
var cid = 1, i, j, k, ii;
|
||||||
for (i = 0, ii = cidToUnicode.length; i < ii; ++i) {
|
for (i = 0, ii = cidToUnicode.length; i < ii; ++i) {
|
||||||
var unicode = cidToUnicode[i];
|
var unicode = cidToUnicode[i];
|
||||||
if (isArray(unicode)) {
|
if (isArray(unicode)) {
|
||||||
var length = unicode.length;
|
var length = unicode.length;
|
||||||
for (j = 0; j < length; j++)
|
for (j = 0; j < length; j++) {
|
||||||
cidToUnicodeMap[unicode[j]] = glyph;
|
cidToUnicodeMap[cid] = unicode[j];
|
||||||
glyph++;
|
unicodeToCIDMap[unicode[j]] = cid;
|
||||||
|
}
|
||||||
|
cid++;
|
||||||
} else if (typeof unicode === 'object') {
|
} else if (typeof unicode === 'object') {
|
||||||
var fillLength = unicode.f;
|
var fillLength = unicode.f;
|
||||||
if (fillLength) {
|
if (fillLength) {
|
||||||
k = unicode.c;
|
k = unicode.c;
|
||||||
for (j = 0; j < fillLength; ++j) {
|
for (j = 0; j < fillLength; ++j) {
|
||||||
cidToUnicodeMap[k] = glyph++;
|
cidToUnicodeMap[cid] = k;
|
||||||
|
unicodeToCIDMap[k] = cid;
|
||||||
|
cid++;
|
||||||
k++;
|
k++;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
glyph += unicode.s;
|
cid += unicode.s;
|
||||||
} else if (unicode) {
|
} else if (unicode) {
|
||||||
cidToUnicodeMap[unicode] = glyph++;
|
cidToUnicodeMap[cid] = unicode;
|
||||||
|
unicodeToCIDMap[unicode] = cid;
|
||||||
|
cid++;
|
||||||
} else
|
} else
|
||||||
glyph++;
|
cid++;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -1981,19 +2071,19 @@ var Font = (function Font() {
|
|||||||
switch (this.type) {
|
switch (this.type) {
|
||||||
case 'CIDFontType0':
|
case 'CIDFontType0':
|
||||||
if (this.noUnicodeAdaptation) {
|
if (this.noUnicodeAdaptation) {
|
||||||
width = this.widths[this.cidToUnicode[charcode]];
|
width = this.widths[this.unicodeToCID[charcode] || charcode];
|
||||||
unicode = charcode;
|
unicode = charcode;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
unicode = adaptUnicode(this.cidToUnicode[charcode] || charcode);
|
unicode = this.toUnicode[charcode] || charcode;
|
||||||
break;
|
break;
|
||||||
case 'CIDFontType2':
|
case 'CIDFontType2':
|
||||||
if (this.noUnicodeAdaptation) {
|
if (this.noUnicodeAdaptation) {
|
||||||
width = this.widths[this.cidToUnicode[charcode]];
|
width = this.widths[this.unicodeToCID[charcode] || charcode];
|
||||||
unicode = charcode;
|
unicode = charcode;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
unicode = adaptUnicode(this.cidToUnicode[charcode] || charcode);
|
unicode = this.toUnicode[charcode] || charcode;
|
||||||
break;
|
break;
|
||||||
case 'Type1':
|
case 'Type1':
|
||||||
var glyphName = this.differences[charcode] || this.encoding[charcode];
|
var glyphName = this.differences[charcode] || this.encoding[charcode];
|
||||||
@ -2004,7 +2094,7 @@ var Font = (function Font() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
unicode = this.glyphNameMap[glyphName] ||
|
unicode = this.glyphNameMap[glyphName] ||
|
||||||
adaptUnicode(GlyphsUnicode[glyphName] || charcode);
|
GlyphsUnicode[glyphName] || charcode;
|
||||||
break;
|
break;
|
||||||
case 'Type3':
|
case 'Type3':
|
||||||
var glyphName = this.differences[charcode] || this.encoding[charcode];
|
var glyphName = this.differences[charcode] || this.encoding[charcode];
|
||||||
@ -2022,16 +2112,16 @@ var Font = (function Font() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!this.hasEncoding) {
|
if (!this.hasEncoding) {
|
||||||
unicode = adaptUnicode(charcode);
|
unicode = this.useToUnicode ? this.toUnicode[charcode] : charcode;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (this.hasShortCmap) {
|
if (this.hasShortCmap && false) {
|
||||||
var j = Encodings.MacRomanEncoding.indexOf(glyphName);
|
var j = Encodings.MacRomanEncoding.indexOf(glyphName);
|
||||||
unicode = j >= 0 && !isSpecialUnicode(j) ? j :
|
unicode = j >= 0 ? j :
|
||||||
this.glyphNameMap[glyphName];
|
this.glyphNameMap[glyphName];
|
||||||
} else {
|
} else {
|
||||||
unicode = glyphName in GlyphsUnicode ?
|
unicode = glyphName in GlyphsUnicode ?
|
||||||
adaptUnicode(GlyphsUnicode[glyphName]) :
|
GlyphsUnicode[glyphName] :
|
||||||
this.glyphNameMap[glyphName];
|
this.glyphNameMap[glyphName];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -2039,9 +2129,17 @@ var Font = (function Font() {
|
|||||||
warn('Unsupported font type: ' + this.type);
|
warn('Unsupported font type: ' + this.type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var unicodeChars = this.toUnicode ? this.toUnicode[charcode] : charcode;
|
||||||
|
if (typeof unicodeChars === 'number')
|
||||||
|
unicodeChars = String.fromCharCode(unicodeChars);
|
||||||
|
|
||||||
|
width = (isNum(width) ? width : this.defaultWidth) * this.widthMultiplier;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
unicode: unicode,
|
fontChar: String.fromCharCode(unicode),
|
||||||
width: isNum(width) ? width : this.defaultWidth,
|
unicode: unicodeChars,
|
||||||
|
width: width,
|
||||||
codeIRQueue: codeIRQueue
|
codeIRQueue: codeIRQueue
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@ -2753,22 +2851,13 @@ CFF.prototype = {
|
|||||||
getOrderedCharStrings: function cff_getOrderedCharStrings(glyphs,
|
getOrderedCharStrings: function cff_getOrderedCharStrings(glyphs,
|
||||||
properties) {
|
properties) {
|
||||||
var charstrings = [];
|
var charstrings = [];
|
||||||
var reverseMapping = {};
|
|
||||||
var encoding = properties.baseEncoding;
|
|
||||||
var i, length, glyphName;
|
var i, length, glyphName;
|
||||||
for (i = 0, length = encoding.length; i < length; ++i) {
|
|
||||||
glyphName = encoding[i];
|
|
||||||
if (!glyphName || isSpecialUnicode(i))
|
|
||||||
continue;
|
|
||||||
reverseMapping[glyphName] = i;
|
|
||||||
}
|
|
||||||
reverseMapping['.notdef'] = 0;
|
|
||||||
var unusedUnicode = kCmapGlyphOffset;
|
var unusedUnicode = kCmapGlyphOffset;
|
||||||
for (i = 0, length = glyphs.length; i < length; i++) {
|
for (i = 0, length = glyphs.length; i < length; i++) {
|
||||||
var item = glyphs[i];
|
var item = glyphs[i];
|
||||||
var glyphName = item.glyph;
|
var glyphName = item.glyph;
|
||||||
var unicode = glyphName in reverseMapping ?
|
var unicode = glyphName in GlyphsUnicode ?
|
||||||
reverseMapping[glyphName] : unusedUnicode++;
|
GlyphsUnicode[glyphName] : unusedUnicode++;
|
||||||
charstrings.push({
|
charstrings.push({
|
||||||
glyph: glyphName,
|
glyph: glyphName,
|
||||||
unicode: unicode,
|
unicode: unicode,
|
||||||
@ -3055,16 +3144,14 @@ var Type2CFF = (function type2CFF() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var charStrings = this.parseIndex(topDict.CharStrings);
|
var charStrings = this.parseIndex(topDict.CharStrings);
|
||||||
var charset = this.parseCharsets(topDict.charset,
|
|
||||||
charStrings.length, strings);
|
|
||||||
var encoding = this.parseEncoding(topDict.Encoding, properties,
|
|
||||||
strings, charset);
|
|
||||||
|
|
||||||
var charset, encoding;
|
var charset, encoding;
|
||||||
var isCIDFont = properties.subtype == 'CIDFontType0C';
|
var isCIDFont = properties.subtype == 'CIDFontType0C';
|
||||||
if (isCIDFont) {
|
if (isCIDFont) {
|
||||||
charset = [];
|
charset = ['.notdef'];
|
||||||
charset.length = charStrings.length;
|
for (var i = 1, ii = charStrings.length; i < ii; ++i)
|
||||||
|
charset.push('glyph' + i);
|
||||||
|
|
||||||
encoding = this.parseCidMap(topDict.charset,
|
encoding = this.parseCidMap(topDict.charset,
|
||||||
charStrings.length);
|
charStrings.length);
|
||||||
} else {
|
} else {
|
||||||
@ -3133,38 +3220,44 @@ var Type2CFF = (function type2CFF() {
|
|||||||
var charstrings = [];
|
var charstrings = [];
|
||||||
var unicodeUsed = [];
|
var unicodeUsed = [];
|
||||||
var unassignedUnicodeItems = [];
|
var unassignedUnicodeItems = [];
|
||||||
|
var inverseEncoding = [];
|
||||||
|
for (var charcode in encoding)
|
||||||
|
inverseEncoding[encoding[charcode]] = charcode | 0;
|
||||||
for (var i = 0, ii = charsets.length; i < ii; i++) {
|
for (var i = 0, ii = charsets.length; i < ii; i++) {
|
||||||
var glyph = charsets[i];
|
var glyph = charsets[i];
|
||||||
var encodingFound = false;
|
if (glyph == '.notdef') {
|
||||||
for (var charcode in encoding) {
|
charstrings.push({
|
||||||
if (encoding[charcode] == i) {
|
unicode: 0,
|
||||||
var code = charcode | 0;
|
code: 0,
|
||||||
charstrings.push({
|
gid: i,
|
||||||
unicode: adaptUnicode(code),
|
glyph: glyph
|
||||||
code: code,
|
});
|
||||||
gid: i,
|
continue;
|
||||||
glyph: glyph
|
|
||||||
});
|
|
||||||
unicodeUsed[code] = true;
|
|
||||||
encodingFound = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!encodingFound) {
|
var code = inverseEncoding[i];
|
||||||
|
if (!code || isSpecialUnicode(code)) {
|
||||||
unassignedUnicodeItems.push(i);
|
unassignedUnicodeItems.push(i);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
charstrings.push({
|
||||||
|
unicode: code,
|
||||||
|
code: code,
|
||||||
|
gid: i,
|
||||||
|
glyph: glyph
|
||||||
|
});
|
||||||
|
unicodeUsed[code] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var nextUnusedUnicode = 0x21;
|
var nextUnusedUnicode = kCmapGlyphOffset;
|
||||||
for (var j = 0, jj = unassignedUnicodeItems.length; j < jj; ++j) {
|
for (var j = 0, jj = unassignedUnicodeItems.length; j < jj; ++j) {
|
||||||
var i = unassignedUnicodeItems[j];
|
var i = unassignedUnicodeItems[j];
|
||||||
// giving unicode value anyway
|
// giving unicode value anyway
|
||||||
while (unicodeUsed[nextUnusedUnicode])
|
while (nextUnusedUnicode in unicodeUsed)
|
||||||
nextUnusedUnicode++;
|
nextUnusedUnicode++;
|
||||||
var code = nextUnusedUnicode++;
|
var unicode = nextUnusedUnicode++;
|
||||||
charstrings.push({
|
charstrings.push({
|
||||||
unicode: adaptUnicode(code),
|
unicode: unicode,
|
||||||
code: code,
|
code: inverseEncoding[i] || 0,
|
||||||
gid: i,
|
gid: i,
|
||||||
glyph: charsets[i]
|
glyph: charsets[i]
|
||||||
});
|
});
|
||||||
|
143
src/function.js
143
src/function.js
@ -20,6 +20,8 @@ var PDFFunction = (function pdfFunction() {
|
|||||||
var array = [];
|
var array = [];
|
||||||
var codeSize = 0;
|
var codeSize = 0;
|
||||||
var codeBuf = 0;
|
var codeBuf = 0;
|
||||||
|
// 32 is a valid bps so shifting won't work
|
||||||
|
var sampleMul = 1.0 / (Math.pow(2.0, bps) - 1);
|
||||||
|
|
||||||
var strBytes = str.getBytes((length * bps + 7) / 8);
|
var strBytes = str.getBytes((length * bps + 7) / 8);
|
||||||
var strIdx = 0;
|
var strIdx = 0;
|
||||||
@ -30,7 +32,7 @@ var PDFFunction = (function pdfFunction() {
|
|||||||
codeSize += 8;
|
codeSize += 8;
|
||||||
}
|
}
|
||||||
codeSize -= bps;
|
codeSize -= bps;
|
||||||
array.push(codeBuf >> codeSize);
|
array.push((codeBuf >> codeSize) * sampleMul);
|
||||||
codeBuf &= (1 << codeSize) - 1;
|
codeBuf &= (1 << codeSize) - 1;
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
@ -76,6 +78,17 @@ var PDFFunction = (function pdfFunction() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
constructSampled: function pdfFunctionConstructSampled(str, dict) {
|
constructSampled: function pdfFunctionConstructSampled(str, dict) {
|
||||||
|
function toMultiArray(arr) {
|
||||||
|
var inputLength = arr.length;
|
||||||
|
var outputLength = arr.length / 2;
|
||||||
|
var out = new Array(outputLength);
|
||||||
|
var index = 0;
|
||||||
|
for (var i = 0; i < inputLength; i += 2) {
|
||||||
|
out[index] = [arr[i], arr[i + 1]];
|
||||||
|
++index;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
var domain = dict.get('Domain');
|
var domain = dict.get('Domain');
|
||||||
var range = dict.get('Range');
|
var range = dict.get('Range');
|
||||||
|
|
||||||
@ -85,9 +98,8 @@ var PDFFunction = (function pdfFunction() {
|
|||||||
var inputSize = domain.length / 2;
|
var inputSize = domain.length / 2;
|
||||||
var outputSize = range.length / 2;
|
var outputSize = range.length / 2;
|
||||||
|
|
||||||
if (inputSize != 1)
|
domain = toMultiArray(domain);
|
||||||
error('No support for multi-variable inputs to functions: ' +
|
range = toMultiArray(range);
|
||||||
inputSize);
|
|
||||||
|
|
||||||
var size = dict.get('Size');
|
var size = dict.get('Size');
|
||||||
var bps = dict.get('BitsPerSample');
|
var bps = dict.get('BitsPerSample');
|
||||||
@ -105,15 +117,36 @@ var PDFFunction = (function pdfFunction() {
|
|||||||
encode.push(size[i] - 1);
|
encode.push(size[i] - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
encode = toMultiArray(encode);
|
||||||
|
|
||||||
var decode = dict.get('Decode');
|
var decode = dict.get('Decode');
|
||||||
if (!decode)
|
if (!decode)
|
||||||
decode = range;
|
decode = range;
|
||||||
|
else
|
||||||
|
decode = toMultiArray(decode);
|
||||||
|
|
||||||
|
// Precalc the multipliers
|
||||||
|
var inputMul = new Float64Array(inputSize);
|
||||||
|
for (var i = 0; i < inputSize; ++i) {
|
||||||
|
inputMul[i] = (encode[i][1] - encode[i][0]) /
|
||||||
|
(domain[i][1] - domain[i][0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
var idxMul = new Int32Array(inputSize);
|
||||||
|
idxMul[0] = outputSize;
|
||||||
|
for (i = 1; i < inputSize; ++i) {
|
||||||
|
idxMul[i] = idxMul[i - 1] * size[i - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
var nSamples = outputSize;
|
||||||
|
for (i = 0; i < inputSize; ++i)
|
||||||
|
nSamples *= size[i];
|
||||||
|
|
||||||
var samples = this.getSampleArray(size, outputSize, bps, str);
|
var samples = this.getSampleArray(size, outputSize, bps, str);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
CONSTRUCT_SAMPLED, inputSize, domain, encode, decode, samples, size,
|
CONSTRUCT_SAMPLED, inputSize, domain, encode, decode, samples, size,
|
||||||
outputSize, bps, range
|
outputSize, bps, range, inputMul, idxMul, nSamples
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -127,64 +160,74 @@ var PDFFunction = (function pdfFunction() {
|
|||||||
var outputSize = IR[7];
|
var outputSize = IR[7];
|
||||||
var bps = IR[8];
|
var bps = IR[8];
|
||||||
var range = IR[9];
|
var range = IR[9];
|
||||||
|
var inputMul = IR[10];
|
||||||
|
var idxMul = IR[11];
|
||||||
|
var nSamples = IR[12];
|
||||||
|
|
||||||
return function constructSampledFromIRResult(args) {
|
return function constructSampledFromIRResult(args) {
|
||||||
var clip = function constructSampledFromIRClip(v, min, max) {
|
|
||||||
if (v > max)
|
|
||||||
v = max;
|
|
||||||
else if (v < min)
|
|
||||||
v = min;
|
|
||||||
return v;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (inputSize != args.length)
|
if (inputSize != args.length)
|
||||||
error('Incorrect number of arguments: ' + inputSize + ' != ' +
|
error('Incorrect number of arguments: ' + inputSize + ' != ' +
|
||||||
args.length);
|
args.length);
|
||||||
|
// Most of the below is a port of Poppler's implementation.
|
||||||
|
// TODO: There's a few other ways to do multilinear interpolation such
|
||||||
|
// as piecewise, which is much faster but an approximation.
|
||||||
|
var out = new Float64Array(outputSize);
|
||||||
|
var x;
|
||||||
|
var e = new Array(inputSize);
|
||||||
|
var efrac0 = new Float64Array(inputSize);
|
||||||
|
var efrac1 = new Float64Array(inputSize);
|
||||||
|
var sBuf = new Float64Array(1 << inputSize);
|
||||||
|
var i, j, k, idx, t;
|
||||||
|
|
||||||
for (var i = 0; i < inputSize; i++) {
|
// map input values into sample array
|
||||||
var i2 = i * 2;
|
for (i = 0; i < inputSize; ++i) {
|
||||||
|
x = (args[i] - domain[i][0]) * inputMul[i] + encode[i][0];
|
||||||
// clip to the domain
|
if (x < 0) {
|
||||||
var v = clip(args[i], domain[i2], domain[i2 + 1]);
|
x = 0;
|
||||||
|
} else if (x > size[i] - 1) {
|
||||||
// encode
|
x = size[i] - 1;
|
||||||
v = encode[i2] + ((v - domain[i2]) *
|
}
|
||||||
(encode[i2 + 1] - encode[i2]) /
|
e[i] = [Math.floor(x), 0];
|
||||||
(domain[i2 + 1] - domain[i2]));
|
if ((e[i][1] = e[i][0] + 1) >= size[i]) {
|
||||||
|
// this happens if in[i] = domain[i][1]
|
||||||
// clip to the size
|
e[i][1] = e[i][0];
|
||||||
args[i] = clip(v, 0, size[i] - 1);
|
}
|
||||||
|
efrac1[i] = x - e[i][0];
|
||||||
|
efrac0[i] = 1 - efrac1[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// interpolate to table
|
// for each output, do m-linear interpolation
|
||||||
TODO('Multi-dimensional interpolation');
|
for (i = 0; i < outputSize; ++i) {
|
||||||
var floor = Math.floor(args[0]);
|
|
||||||
var ceil = Math.ceil(args[0]);
|
|
||||||
var scale = args[0] - floor;
|
|
||||||
|
|
||||||
floor *= outputSize;
|
// pull 2^m values out of the sample array
|
||||||
ceil *= outputSize;
|
for (j = 0; j < (1 << inputSize); ++j) {
|
||||||
|
idx = i;
|
||||||
var output = [], v = 0;
|
for (k = 0, t = j; k < inputSize; ++k, t >>= 1) {
|
||||||
for (var i = 0; i < outputSize; ++i) {
|
idx += idxMul[k] * (e[k][t & 1]);
|
||||||
if (ceil == floor) {
|
}
|
||||||
v = samples[ceil + i];
|
if (idx >= 0 && idx < nSamples) {
|
||||||
} else {
|
sBuf[j] = samples[idx];
|
||||||
var low = samples[floor + i];
|
} else {
|
||||||
var high = samples[ceil + i];
|
sBuf[j] = 0; // TODO Investigate if this is what Adobe does
|
||||||
v = low * scale + high * (1 - scale);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var i2 = i * 2;
|
// do m sets of interpolations
|
||||||
// decode
|
for (j = 0, t = (1 << inputSize); j < inputSize; ++j, t >>= 1) {
|
||||||
v = decode[i2] + (v * (decode[i2 + 1] - decode[i2]) /
|
for (k = 0; k < t; k += 2) {
|
||||||
((1 << bps) - 1));
|
sBuf[k >> 1] = efrac0[j] * sBuf[k] + efrac1[j] * sBuf[k + 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// clip to the domain
|
// map output value to range
|
||||||
output.push(clip(v, range[i2], range[i2 + 1]));
|
out[i] = (sBuf[0] * (decode[i][1] - decode[i][0]) + decode[i][0]);
|
||||||
|
if (out[i] < range[i][0]) {
|
||||||
|
out[i] = range[i][0];
|
||||||
|
} else if (out[i] > range[i][1]) {
|
||||||
|
out[i] = range[i][1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return out;
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -4287,6 +4287,7 @@ var GlyphsUnicode = {
|
|||||||
zretroflexhook: 0x0290,
|
zretroflexhook: 0x0290,
|
||||||
zstroke: 0x01B6,
|
zstroke: 0x01B6,
|
||||||
zuhiragana: 0x305A,
|
zuhiragana: 0x305A,
|
||||||
zukatakana: 0x30BA
|
zukatakana: 0x30BA,
|
||||||
|
'.notdef': 0x0000
|
||||||
};
|
};
|
||||||
|
|
||||||
|
12
src/image.js
12
src/image.js
@ -229,12 +229,12 @@ var PDFImage = (function pdfImage() {
|
|||||||
return constructor;
|
return constructor;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
var JpegImage = (function jpegImage() {
|
var JpegImageLoader = (function jpegImage() {
|
||||||
function JpegImage(objId, imageData, objs) {
|
function JpegImageLoader(objId, imageData, objs) {
|
||||||
var src = 'data:image/jpeg;base64,' + window.btoa(imageData);
|
var src = 'data:image/jpeg;base64,' + window.btoa(imageData);
|
||||||
|
|
||||||
var img = new Image();
|
var img = new Image();
|
||||||
img.onload = (function jpegImageOnload() {
|
img.onload = (function jpegImageLoaderOnload() {
|
||||||
this.loaded = true;
|
this.loaded = true;
|
||||||
|
|
||||||
objs.resolve(objId, this);
|
objs.resolve(objId, this);
|
||||||
@ -246,12 +246,12 @@ var JpegImage = (function jpegImage() {
|
|||||||
this.domImage = img;
|
this.domImage = img;
|
||||||
}
|
}
|
||||||
|
|
||||||
JpegImage.prototype = {
|
JpegImageLoader.prototype = {
|
||||||
getImage: function jpegImageGetImage() {
|
getImage: function jpegImageLoaderGetImage() {
|
||||||
return this.domImage;
|
return this.domImage;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return JpegImage;
|
return JpegImageLoader;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
@ -236,7 +236,7 @@ var Parser = (function parserParser() {
|
|||||||
return new LZWStream(stream, earlyChange);
|
return new LZWStream(stream, earlyChange);
|
||||||
} else if (name == 'DCTDecode' || name == 'DCT') {
|
} else if (name == 'DCTDecode' || name == 'DCT') {
|
||||||
var bytes = stream.getBytes(length);
|
var bytes = stream.getBytes(length);
|
||||||
return new JpegStream(bytes, stream.dict);
|
return new JpegStream(bytes, stream.dict, this.xref);
|
||||||
} else if (name == 'ASCII85Decode' || name == 'A85') {
|
} else if (name == 'ASCII85Decode' || name == 'A85') {
|
||||||
return new Ascii85Stream(stream);
|
return new Ascii85Stream(stream);
|
||||||
} else if (name == 'ASCIIHexDecode' || name == 'AHx') {
|
} else if (name == 'ASCIIHexDecode' || name == 'AHx') {
|
||||||
|
@ -756,8 +756,13 @@ var PredictorStream = (function predictorStream() {
|
|||||||
return constructor;
|
return constructor;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
// A JpegStream can't be read directly. We use the platform to render
|
/**
|
||||||
// the underlying JPEG data for us.
|
* Depending on the type of JPEG a JpegStream is handled in different ways. For
|
||||||
|
* JPEG's that are supported natively such as DeviceGray and DeviceRGB the image
|
||||||
|
* data is stored and then loaded by the browser. For unsupported JPEG's we use
|
||||||
|
* a library to decode these images and the stream behaves like all the other
|
||||||
|
* DecodeStreams.
|
||||||
|
*/
|
||||||
var JpegStream = (function jpegStream() {
|
var JpegStream = (function jpegStream() {
|
||||||
function isAdobeImage(bytes) {
|
function isAdobeImage(bytes) {
|
||||||
var maxBytesScanned = Math.max(bytes.length - 16, 1024);
|
var maxBytesScanned = Math.max(bytes.length - 16, 1024);
|
||||||
@ -789,24 +794,56 @@ var JpegStream = (function jpegStream() {
|
|||||||
return newBytes;
|
return newBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
function constructor(bytes, dict) {
|
function constructor(bytes, dict, xref) {
|
||||||
// TODO: per poppler, some images may have 'junk' before that
|
// TODO: per poppler, some images may have 'junk' before that
|
||||||
// need to be removed
|
// need to be removed
|
||||||
this.dict = dict;
|
this.dict = dict;
|
||||||
|
|
||||||
if (isAdobeImage(bytes))
|
// Flag indicating wether the image can be natively loaded.
|
||||||
bytes = fixAdobeImage(bytes);
|
this.isNative = true;
|
||||||
|
|
||||||
this.src = bytesToString(bytes);
|
this.colorTransform = -1;
|
||||||
|
|
||||||
|
if (isAdobeImage(bytes)) {
|
||||||
|
// when bug 674619 land, let's check if browser can do
|
||||||
|
// normal cmyk and then we won't have to the following
|
||||||
|
var cs = xref.fetchIfRef(dict.get('ColorSpace'));
|
||||||
|
|
||||||
|
// DeviceRGB and DeviceGray are the only Adobe images that work natively
|
||||||
|
if (isName(cs) && (cs.name === 'DeviceRGB' || cs.name === 'DeviceGray')) {
|
||||||
|
bytes = fixAdobeImage(bytes);
|
||||||
|
this.src = bytesToString(bytes);
|
||||||
|
} else {
|
||||||
|
this.colorTransform = dict.get('ColorTransform');
|
||||||
|
this.isNative = false;
|
||||||
|
this.bytes = bytes;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.src = bytesToString(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
DecodeStream.call(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor.prototype = {
|
constructor.prototype = Object.create(DecodeStream.prototype);
|
||||||
getIR: function jpegStreamGetIR() {
|
|
||||||
return this.src;
|
constructor.prototype.ensureBuffer = function jpegStreamEnsureBuffer(req) {
|
||||||
},
|
if (this.bufferLength)
|
||||||
getChar: function jpegStreamGetChar() {
|
return;
|
||||||
|
var jpegImage = new JpegImage();
|
||||||
|
jpegImage.colorTransform = this.colorTransform;
|
||||||
|
jpegImage.parse(this.bytes);
|
||||||
|
var width = jpegImage.width;
|
||||||
|
var height = jpegImage.height;
|
||||||
|
var data = jpegImage.getData(width, height);
|
||||||
|
this.buffer = data;
|
||||||
|
this.bufferLength = data.length;
|
||||||
|
};
|
||||||
|
constructor.prototype.getIR = function jpegStreamGetIR() {
|
||||||
|
return this.src;
|
||||||
|
};
|
||||||
|
constructor.prototype.getChar = function jpegStreamGetChar() {
|
||||||
error('internal error: getChar is not valid on JpegStream');
|
error('internal error: getChar is not valid on JpegStream');
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return constructor;
|
return constructor;
|
||||||
|
@ -40,7 +40,8 @@ function onMessageLoader(evt) {
|
|||||||
'parser.js',
|
'parser.js',
|
||||||
'pattern.js',
|
'pattern.js',
|
||||||
'stream.js',
|
'stream.js',
|
||||||
'worker.js'
|
'worker.js',
|
||||||
|
'../external/jpgjs/jpg.js'
|
||||||
];
|
];
|
||||||
|
|
||||||
// Load all the files.
|
// Load all the files.
|
||||||
|
3
test/pdfs/.gitignore
vendored
3
test/pdfs/.gitignore
vendored
@ -14,4 +14,5 @@
|
|||||||
!sizes.pdf
|
!sizes.pdf
|
||||||
!close-path-bug.pdf
|
!close-path-bug.pdf
|
||||||
!alphatrans.pdf
|
!alphatrans.pdf
|
||||||
|
!devicen.pdf
|
||||||
|
!cmykjpeg.pdf
|
||||||
|
2178
test/pdfs/cmykjpeg.pdf
Normal file
2178
test/pdfs/cmykjpeg.pdf
Normal file
File diff suppressed because one or more lines are too long
BIN
test/pdfs/devicen.pdf
Normal file
BIN
test/pdfs/devicen.pdf
Normal file
Binary file not shown.
@ -1 +0,0 @@
|
|||||||
http://openweb.flossmanuals.net/materials/openweb_tm-PRINT.pdf
|
|
1
test/pdfs/piperine.pdf.link
Normal file
1
test/pdfs/piperine.pdf.link
Normal file
@ -0,0 +1 @@
|
|||||||
|
http://www.erowid.org/archive/rhodium/chemistry/3base/piperonal.pepper/piperine.pepper/465e03piperine.pdf
|
1
test/pdfs/protectip.pdf.link
Normal file
1
test/pdfs/protectip.pdf.link
Normal file
@ -0,0 +1 @@
|
|||||||
|
http://leahy.senate.gov/imo/media/doc/BillText-PROTECTIPAct.pdf
|
@ -20,6 +20,7 @@
|
|||||||
{ "id": "intelisa-load",
|
{ "id": "intelisa-load",
|
||||||
"file": "pdfs/intelisa.pdf",
|
"file": "pdfs/intelisa.pdf",
|
||||||
"md5": "f5712097d29287a97f1278839814f682",
|
"md5": "f5712097d29287a97f1278839814f682",
|
||||||
|
"md5": "f3ed5487d1afa34d8b77c0c734a95c79",
|
||||||
"link": true,
|
"link": true,
|
||||||
"rounds": 1,
|
"rounds": 1,
|
||||||
"type": "load"
|
"type": "load"
|
||||||
@ -44,13 +45,6 @@
|
|||||||
"rounds": 1,
|
"rounds": 1,
|
||||||
"type": "eq"
|
"type": "eq"
|
||||||
},
|
},
|
||||||
{ "id": "openweb-cover",
|
|
||||||
"file": "pdfs/openweb_tm-PRINT.pdf",
|
|
||||||
"md5": "53f611dfc19ddfb50554c21c4af465c0",
|
|
||||||
"link": true,
|
|
||||||
"rounds": 1,
|
|
||||||
"type": "eq"
|
|
||||||
},
|
|
||||||
{ "id": "plusminus",
|
{ "id": "plusminus",
|
||||||
"file": "pdfs/Test-plusminus.pdf",
|
"file": "pdfs/Test-plusminus.pdf",
|
||||||
"md5": "1ec7ade5b95ac9aaba3a618af28d34c7",
|
"md5": "1ec7ade5b95ac9aaba3a618af28d34c7",
|
||||||
@ -268,5 +262,33 @@
|
|||||||
"link": false,
|
"link": false,
|
||||||
"rounds": 1,
|
"rounds": 1,
|
||||||
"type": "eq"
|
"type": "eq"
|
||||||
|
},
|
||||||
|
{ "id": "devicen",
|
||||||
|
"file": "pdfs/devicen.pdf",
|
||||||
|
"md5": "aac6a91725435d1376c6ff492dc5cb75",
|
||||||
|
"link": false,
|
||||||
|
"rounds": 1,
|
||||||
|
"type": "eq"
|
||||||
|
},
|
||||||
|
{ "id": "cmykjpeg",
|
||||||
|
"file": "pdfs/cmykjpeg.pdf",
|
||||||
|
"md5": "85d162b48ce98503a382d96f574f70a2",
|
||||||
|
"link": false,
|
||||||
|
"rounds": 1,
|
||||||
|
"type": "eq"
|
||||||
|
},
|
||||||
|
{ "id": "protectip",
|
||||||
|
"file": "pdfs/protectip.pdf",
|
||||||
|
"md5": "676e7a7b8f96d04825361832b1838a93",
|
||||||
|
"link": true,
|
||||||
|
"rounds": 1,
|
||||||
|
"type": "eq"
|
||||||
|
},
|
||||||
|
{ "id": "piperine",
|
||||||
|
"file": "pdfs/piperine.pdf",
|
||||||
|
"md5": "603ca43dc5732dbba1579f122958c0c2",
|
||||||
|
"link": true,
|
||||||
|
"rounds": 1,
|
||||||
|
"type": "eq"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
<script type="text/javascript" src="/src/pattern.js"></script>
|
<script type="text/javascript" src="/src/pattern.js"></script>
|
||||||
<script type="text/javascript" src="/src/stream.js"></script>
|
<script type="text/javascript" src="/src/stream.js"></script>
|
||||||
<script type="text/javascript" src="/src/worker.js"></script>
|
<script type="text/javascript" src="/src/worker.js"></script>
|
||||||
|
<script type="text/javascript" src="/external/jpgjs/jpg.js"></script>
|
||||||
<script type="text/javascript" src="driver.js"></script>
|
<script type="text/javascript" src="driver.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
16
test/unit/obj_spec.js
Normal file
16
test/unit/obj_spec.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||||
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
describe("obj", function() {
|
||||||
|
|
||||||
|
describe("Name", function() {
|
||||||
|
it("should retain the given name", function() {
|
||||||
|
var givenName = "Font";
|
||||||
|
var name = new Name(givenName);
|
||||||
|
expect(name.name).toEqual(givenName);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
51
test/unit/unit_test.html
Normal file
51
test/unit/unit_test.html
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>pdf.js unit test</title>
|
||||||
|
|
||||||
|
<link rel="shortcut icon" type="image/png" href="../../external/jasmine/jasmine_favicon.png">
|
||||||
|
<link rel="stylesheet" type="text/css" href="../../external/jasmine/jasmine.css">
|
||||||
|
|
||||||
|
<script type="text/javascript" src="../../external/jasmine/jasmine.js"></script>
|
||||||
|
<script type="text/javascript" src="../../external/jasmine/jasmine-html.js"></script>
|
||||||
|
|
||||||
|
<!-- include spec files here... -->
|
||||||
|
<script type="text/javascript" src="obj_spec.js"></script>
|
||||||
|
|
||||||
|
<!-- include source files here... -->
|
||||||
|
<script type="text/javascript" src="../../src/obj.js"></script>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
(function pdfJsUnitTest() {
|
||||||
|
var jasmineEnv = jasmine.getEnv();
|
||||||
|
jasmineEnv.updateInterval = 1000;
|
||||||
|
|
||||||
|
var trivialReporter = new jasmine.TrivialReporter();
|
||||||
|
|
||||||
|
jasmineEnv.addReporter(trivialReporter);
|
||||||
|
|
||||||
|
jasmineEnv.specFilter = function pdfJsUnitTestSpecFilter(spec) {
|
||||||
|
return trivialReporter.specFilter(spec);
|
||||||
|
};
|
||||||
|
|
||||||
|
var currentWindowOnload = window.onload;
|
||||||
|
|
||||||
|
window.onload = function pdfJsUnitTestOnload() {
|
||||||
|
if (currentWindowOnload) {
|
||||||
|
currentWindowOnload();
|
||||||
|
}
|
||||||
|
execJasmine();
|
||||||
|
};
|
||||||
|
|
||||||
|
function execJasmine() {
|
||||||
|
jasmineEnv.execute();
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
@ -25,7 +25,7 @@
|
|||||||
<script type="text/javascript" src="../src/pattern.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
<script type="text/javascript" src="../src/pattern.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
<script type="text/javascript" src="../src/stream.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
<script type="text/javascript" src="../src/stream.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
<script type="text/javascript" src="../src/worker.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
<script type="text/javascript" src="../src/worker.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
|
<script type="text/javascript" src="../external/jpgjs/jpg.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
<script type="text/javascript">PDFJS.workerSrc = '../src/worker_loader.js';</script> <!-- PDFJSSCRIPT_REMOVE -->
|
<script type="text/javascript">PDFJS.workerSrc = '../src/worker_loader.js';</script> <!-- PDFJSSCRIPT_REMOVE -->
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
|
@ -193,6 +193,13 @@ var PDFView = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
load: function pdfViewLoad(data, scale) {
|
load: function pdfViewLoad(data, scale) {
|
||||||
|
function bindOnAfterDraw(pageView, thumbnailView) {
|
||||||
|
// when page is painted, using the image as thumbnail base
|
||||||
|
pageView.onAfterDraw = function pdfViewLoadOnAfterDraw() {
|
||||||
|
thumbnailView.setImage(pageView.canvas);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
var loadingIndicator = document.getElementById('loading');
|
var loadingIndicator = document.getElementById('loading');
|
||||||
loadingIndicator.setAttribute('hidden', 'true');
|
loadingIndicator.setAttribute('hidden', 'true');
|
||||||
|
|
||||||
@ -219,10 +226,14 @@ var PDFView = {
|
|||||||
var thumbnails = this.thumbnails = [];
|
var thumbnails = this.thumbnails = [];
|
||||||
for (var i = 1; i <= pagesCount; i++) {
|
for (var i = 1; i <= pagesCount; i++) {
|
||||||
var page = pdf.getPage(i);
|
var page = pdf.getPage(i);
|
||||||
pages.push(new PageView(container, page, i, page.width, page.height,
|
var pageView = new PageView(container, page, i, page.width, page.height,
|
||||||
page.stats, this.navigateTo.bind(this)));
|
page.stats, this.navigateTo.bind(this));
|
||||||
thumbnails.push(new ThumbnailView(sidebar, page, i,
|
var thumbnailView = new ThumbnailView(sidebar, page, i,
|
||||||
page.width / page.height));
|
page.width / page.height);
|
||||||
|
bindOnAfterDraw(pageView, thumbnailView);
|
||||||
|
|
||||||
|
pages.push(pageView);
|
||||||
|
thumbnails.push(thumbnailView);
|
||||||
var pageRef = page.ref;
|
var pageRef = page.ref;
|
||||||
pagesRefMap[pageRef.num + ' ' + pageRef.gen + ' R'] = i;
|
pagesRefMap[pageRef.num + ' ' + pageRef.gen + ' R'] = i;
|
||||||
}
|
}
|
||||||
@ -389,6 +400,8 @@ var PageView = function pageView(container, content, id, pageWidth, pageHeight,
|
|||||||
while (div.hasChildNodes())
|
while (div.hasChildNodes())
|
||||||
div.removeChild(div.lastChild);
|
div.removeChild(div.lastChild);
|
||||||
div.removeAttribute('data-loaded');
|
div.removeAttribute('data-loaded');
|
||||||
|
|
||||||
|
delete this.canvas;
|
||||||
};
|
};
|
||||||
|
|
||||||
function setupLinks(content, scale) {
|
function setupLinks(content, scale) {
|
||||||
@ -503,6 +516,7 @@ var PageView = function pageView(container, content, id, pageWidth, pageHeight,
|
|||||||
canvas.id = 'page' + this.id;
|
canvas.id = 'page' + this.id;
|
||||||
canvas.mozOpaque = true;
|
canvas.mozOpaque = true;
|
||||||
div.appendChild(canvas);
|
div.appendChild(canvas);
|
||||||
|
this.canvas = canvas;
|
||||||
|
|
||||||
var scale = this.scale;
|
var scale = this.scale;
|
||||||
canvas.width = pageWidth * scale;
|
canvas.width = pageWidth * scale;
|
||||||
@ -516,7 +530,11 @@ var PageView = function pageView(container, content, id, pageWidth, pageHeight,
|
|||||||
ctx.translate(-this.x * scale, -this.y * scale);
|
ctx.translate(-this.x * scale, -this.y * scale);
|
||||||
|
|
||||||
stats.begin = Date.now();
|
stats.begin = Date.now();
|
||||||
this.content.startRendering(ctx, this.updateStats);
|
this.content.startRendering(ctx, (function pageViewDrawCallback() {
|
||||||
|
this.updateStats();
|
||||||
|
if (this.onAfterDraw)
|
||||||
|
this.onAfterDraw();
|
||||||
|
}).bind(this));
|
||||||
|
|
||||||
setupLinks(this.content, this.scale);
|
setupLinks(this.content, this.scale);
|
||||||
div.setAttribute('data-loaded', true);
|
div.setAttribute('data-loaded', true);
|
||||||
@ -560,10 +578,9 @@ var ThumbnailView = function thumbnailView(container, page, id, pageRatio) {
|
|||||||
anchor.appendChild(div);
|
anchor.appendChild(div);
|
||||||
container.appendChild(anchor);
|
container.appendChild(anchor);
|
||||||
|
|
||||||
this.draw = function thumbnailViewDraw() {
|
this.hasImage = false;
|
||||||
if (div.hasChildNodes())
|
|
||||||
return;
|
|
||||||
|
|
||||||
|
function getPageDrawContext() {
|
||||||
var canvas = document.createElement('canvas');
|
var canvas = document.createElement('canvas');
|
||||||
canvas.id = 'thumbnail' + id;
|
canvas.id = 'thumbnail' + id;
|
||||||
canvas.mozOpaque = true;
|
canvas.mozOpaque = true;
|
||||||
@ -588,7 +605,28 @@ var ThumbnailView = function thumbnailView(container, page, id, pageRatio) {
|
|||||||
div.style.height = (view.height * scaleY) + 'px';
|
div.style.height = (view.height * scaleY) + 'px';
|
||||||
div.style.lineHeight = (view.height * scaleY) + 'px';
|
div.style.lineHeight = (view.height * scaleY) + 'px';
|
||||||
|
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.draw = function thumbnailViewDraw() {
|
||||||
|
if (this.hasImage)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var ctx = getPageDrawContext();
|
||||||
page.startRendering(ctx, function thumbnailViewDrawStartRendering() {});
|
page.startRendering(ctx, function thumbnailViewDrawStartRendering() {});
|
||||||
|
|
||||||
|
this.hasImage = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.setImage = function thumbnailViewSetImage(img) {
|
||||||
|
if (this.hasImage)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var ctx = getPageDrawContext();
|
||||||
|
ctx.drawImage(img, 0, 0, img.width, img.height,
|
||||||
|
0, 0, ctx.canvas.width, ctx.canvas.height);
|
||||||
|
|
||||||
|
this.hasImage = true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user