fix(svg) adjust strategy for decoding JPEG images

This commit is contained in:
巴里切罗 2017-05-08 11:32:44 +08:00
parent 0dbc68a6d6
commit 8d5d97264e
9 changed files with 66 additions and 15 deletions

View File

@ -147,3 +147,21 @@ global.document = {
return []; return [];
} }
}; };
function Image () {
this._src = null;
this.onload = null;
}
Image.prototype = {
get src () {
return this._src;
},
set src (value) {
this._src = value;
if (this.onload) {
this.onload();
}
}
}
global.Image = Image;

View File

@ -46,7 +46,8 @@ function getFileNameFromPath(path) {
// callback. // callback.
pdfjsLib.getDocument({ pdfjsLib.getDocument({
data: data, data: data,
disableNativeImageDecoder: true, // Try to export JPEG images directly if they don't need any further processing.
nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY
}).then(function (doc) { }).then(function (doc) {
var numPages = doc.numPages; var numPages = doc.numPages;
console.log('# Document Loaded'); console.log('# Document Loaded');

View File

@ -52,6 +52,7 @@ var IDENTITY_MATRIX = sharedUtil.IDENTITY_MATRIX;
var UNSUPPORTED_FEATURES = sharedUtil.UNSUPPORTED_FEATURES; var UNSUPPORTED_FEATURES = sharedUtil.UNSUPPORTED_FEATURES;
var ImageKind = sharedUtil.ImageKind; var ImageKind = sharedUtil.ImageKind;
var OPS = sharedUtil.OPS; var OPS = sharedUtil.OPS;
var NativeImageDecoding = sharedUtil.NativeImageDecoding;
var TextRenderingMode = sharedUtil.TextRenderingMode; var TextRenderingMode = sharedUtil.TextRenderingMode;
var CMapCompressionType = sharedUtil.CMapCompressionType; var CMapCompressionType = sharedUtil.CMapCompressionType;
var Util = sharedUtil.Util; var Util = sharedUtil.Util;
@ -113,7 +114,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
forceDataSchema: false, forceDataSchema: false,
maxImageSize: -1, maxImageSize: -1,
disableFontFace: false, disableFontFace: false,
disableNativeImageDecoder: false, nativeImageDecoderSupport: NativeImageDecoding.DECODE,
ignoreErrors: false, ignoreErrors: false,
}; };
@ -461,14 +462,14 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
return; return;
} }
var useNativeImageDecoder = !this.options.disableNativeImageDecoder; var nativeImageDecoderSupport = this.options.nativeImageDecoderSupport;
// If there is no imageMask, create the PDFImage and a lot // If there is no imageMask, create the PDFImage and a lot
// of image processing can be done here. // of image processing can be done here.
var objId = 'img_' + this.idFactory.createObjId(); var objId = 'img_' + this.idFactory.createObjId();
operatorList.addDependency(objId); operatorList.addDependency(objId);
args = [objId, w, h]; args = [objId, w, h];
if (useNativeImageDecoder && if (nativeImageDecoderSupport !== NativeImageDecoding.NONE &&
!softMask && !mask && image instanceof JpegStream && !softMask && !mask && image instanceof JpegStream &&
NativeImageDecoder.isSupported(image, this.xref, resources)) { NativeImageDecoder.isSupported(image, this.xref, resources)) {
// These JPEGs don't need any more processing so we can just send it. // These JPEGs don't need any more processing so we can just send it.
@ -481,7 +482,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
// Creates native image decoder only if a JPEG image or mask is present. // Creates native image decoder only if a JPEG image or mask is present.
var nativeImageDecoder = null; var nativeImageDecoder = null;
if (useNativeImageDecoder && if (nativeImageDecoderSupport === NativeImageDecoding.DECODE &&
(image instanceof JpegStream || mask instanceof JpegStream || (image instanceof JpegStream || mask instanceof JpegStream ||
softMask instanceof JpegStream)) { softMask instanceof JpegStream)) {
nativeImageDecoder = new NativeImageDecoder(this.xref, resources, nativeImageDecoder = new NativeImageDecoder(this.xref, resources,

View File

@ -731,7 +731,7 @@ var WorkerMessageHandler = {
forceDataSchema: data.disableCreateObjectURL, forceDataSchema: data.disableCreateObjectURL,
maxImageSize: data.maxImageSize === undefined ? -1 : data.maxImageSize, maxImageSize: data.maxImageSize === undefined ? -1 : data.maxImageSize,
disableFontFace: data.disableFontFace, disableFontFace: data.disableFontFace,
disableNativeImageDecoder: data.disableNativeImageDecoder, nativeImageDecoderSupport: data.nativeImageDecoderSupport,
ignoreErrors: data.ignoreErrors, ignoreErrors: data.ignoreErrors,
}; };

View File

@ -17,9 +17,9 @@
import { import {
createPromiseCapability, deprecated, error, getVerbosityLevel, globalScope, createPromiseCapability, deprecated, error, getVerbosityLevel, globalScope,
info, InvalidPDFException, isArray, isArrayBuffer, isInt, isSameOrigin, info, InvalidPDFException, isArray, isArrayBuffer, isInt, isSameOrigin,
loadJpegStream, MessageHandler, MissingPDFException, PageViewport, loadJpegStream, MessageHandler, MissingPDFException, NativeImageDecoding,
PasswordException, StatTimer, stringToBytes, UnexpectedResponseException, PageViewport, PasswordException, StatTimer, stringToBytes,
UnknownErrorException, Util, warn UnexpectedResponseException, UnknownErrorException, Util, warn
} from '../shared/util'; } from '../shared/util';
import { import {
DOMCanvasFactory, DOMCMapReaderFactory, getDefaultSetting, DOMCanvasFactory, DOMCMapReaderFactory, getDefaultSetting,
@ -104,10 +104,18 @@ if (typeof PDFJSDev !== 'undefined' &&
* @property {string} docBaseUrl - (optional) The base URL of the document, * @property {string} docBaseUrl - (optional) The base URL of the document,
* used when attempting to recover valid absolute URLs for annotations, and * used when attempting to recover valid absolute URLs for annotations, and
* outline items, that (incorrectly) only specify relative URLs. * outline items, that (incorrectly) only specify relative URLs.
* @property {boolean} disableNativeImageDecoder - (optional) Disable decoding * @property {boolean} disableNativeImageDecoder - (deprecated) Disable decoding
* of certain (simple) JPEG images in the browser. This is useful for * of certain (simple) JPEG images in the browser. This is useful for
* environments without DOM image support, such as e.g. Node.js. * environments without DOM image support, such as e.g. Node.js.
* The default value is `false`. * The default value is `false`.
* @property {string} nativeImageDecoderSupport - (optional) Strategy for
* decoding certain (simple) JPEG images in the browser. This is useful for
* environments without DOM image and canvas support, such as e.g. Node.js.
* Valid values are 'decode', 'display' or 'none'; where 'decode' is intended
* for browsers with full image/canvas support, 'display' for environments
* with limited image support through stubs (useful for SVG conversion),
* and 'none' where JPEG images will be decoded entirely by PDF.js.
* The default value is 'decode'.
* @property {Object} CMapReaderFactory - (optional) The factory that will be * @property {Object} CMapReaderFactory - (optional) The factory that will be
* used when reading built-in CMap files. Providing a custom factory is useful * used when reading built-in CMap files. Providing a custom factory is useful
* for environments without `XMLHttpRequest` support, such as e.g. Node.js. * for environments without `XMLHttpRequest` support, such as e.g. Node.js.
@ -229,10 +237,24 @@ function getDocument(src, pdfDataRangeTransport,
} }
params.rangeChunkSize = params.rangeChunkSize || DEFAULT_RANGE_CHUNK_SIZE; params.rangeChunkSize = params.rangeChunkSize || DEFAULT_RANGE_CHUNK_SIZE;
params.disableNativeImageDecoder = params.disableNativeImageDecoder === true;
params.ignoreErrors = params.stopAtErrors !== true; params.ignoreErrors = params.stopAtErrors !== true;
var CMapReaderFactory = params.CMapReaderFactory || DOMCMapReaderFactory; var CMapReaderFactory = params.CMapReaderFactory || DOMCMapReaderFactory;
if (params.disableNativeImageDecoder !== undefined) {
deprecated('parameter disableNativeImageDecoder, ' +
'use nativeImageDecoderSupport instead');
}
params.nativeImageDecoderSupport = params.nativeImageDecoderSupport ||
(params.disableNativeImageDecoder === true ? NativeImageDecoding.NONE :
NativeImageDecoding.DECODE);
if (params.nativeImageDecoderSupport !== NativeImageDecoding.DECODE &&
params.nativeImageDecoderSupport !== NativeImageDecoding.NONE &&
params.nativeImageDecoderSupport !== NativeImageDecoding.DISPLAY) {
warn('Invalid parameter nativeImageDecoderSupport: ' +
'need a state of enum {NativeImageDecoding}');
params.nativeImageDecoderSupport = NativeImageDecoding.DECODE;
}
if (!worker) { if (!worker) {
// Worker was not provided -- creating and owning our own. If message port // Worker was not provided -- creating and owning our own. If message port
// is specified in global settings, using it. // is specified in global settings, using it.
@ -293,7 +315,7 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
postMessageTransfers: getDefaultSetting('postMessageTransfers') && postMessageTransfers: getDefaultSetting('postMessageTransfers') &&
!isPostMessageTransfersDisabled, !isPostMessageTransfersDisabled,
docBaseUrl: source.docBaseUrl, docBaseUrl: source.docBaseUrl,
disableNativeImageDecoder: source.disableNativeImageDecoder, nativeImageDecoderSupport: source.nativeImageDecoderSupport,
ignoreErrors: source.ignoreErrors, ignoreErrors: source.ignoreErrors,
}).then(function (workerId) { }).then(function (workerId) {
if (worker.destroyed) { if (worker.destroyed) {

View File

@ -1029,8 +1029,8 @@ SVGGraphics = (function SVGGraphicsClosure() {
var imgObj = this.objs.get(objId); var imgObj = this.objs.get(objId);
var imgEl = document.createElementNS(NS, 'svg:image'); var imgEl = document.createElementNS(NS, 'svg:image');
imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgObj.src); imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgObj.src);
imgEl.setAttributeNS(null, 'width', imgObj.width + 'px'); imgEl.setAttributeNS(null, 'width', pf(w));
imgEl.setAttributeNS(null, 'height', imgObj.height + 'px'); imgEl.setAttributeNS(null, 'height', pf(h));
imgEl.setAttributeNS(null, 'x', '0'); imgEl.setAttributeNS(null, 'x', '0');
imgEl.setAttributeNS(null, 'y', pf(-h)); imgEl.setAttributeNS(null, 'y', pf(-h));
imgEl.setAttributeNS(null, 'transform', imgEl.setAttributeNS(null, 'transform',

View File

@ -54,6 +54,7 @@
exports.InvalidPDFException = sharedUtil.InvalidPDFException; exports.InvalidPDFException = sharedUtil.InvalidPDFException;
exports.MissingPDFException = sharedUtil.MissingPDFException; exports.MissingPDFException = sharedUtil.MissingPDFException;
exports.SVGGraphics = displaySVG.SVGGraphics; exports.SVGGraphics = displaySVG.SVGGraphics;
exports.NativeImageDecoding = sharedUtil.NativeImageDecoding;
exports.UnexpectedResponseException = sharedUtil.UnexpectedResponseException; exports.UnexpectedResponseException = sharedUtil.UnexpectedResponseException;
exports.OPS = sharedUtil.OPS; exports.OPS = sharedUtil.OPS;
exports.UNSUPPORTED_FEATURES = sharedUtil.UNSUPPORTED_FEATURES; exports.UNSUPPORTED_FEATURES = sharedUtil.UNSUPPORTED_FEATURES;

View File

@ -42,6 +42,7 @@ exports.PasswordResponses = pdfjsSharedUtil.PasswordResponses;
exports.InvalidPDFException = pdfjsSharedUtil.InvalidPDFException; exports.InvalidPDFException = pdfjsSharedUtil.InvalidPDFException;
exports.MissingPDFException = pdfjsSharedUtil.MissingPDFException; exports.MissingPDFException = pdfjsSharedUtil.MissingPDFException;
exports.SVGGraphics = pdfjsDisplaySVG.SVGGraphics; exports.SVGGraphics = pdfjsDisplaySVG.SVGGraphics;
exports.NativeImageDecoding = pdfjsSharedUtil.NativeImageDecoding;
exports.UnexpectedResponseException = exports.UnexpectedResponseException =
pdfjsSharedUtil.UnexpectedResponseException; pdfjsSharedUtil.UnexpectedResponseException;
exports.OPS = pdfjsSharedUtil.OPS; exports.OPS = pdfjsSharedUtil.OPS;

View File

@ -22,6 +22,12 @@ var globalScope = (typeof window !== 'undefined') ? window :
var FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0]; var FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
const NativeImageDecoding = {
NONE: 'none',
DECODE: 'decode',
DISPLAY: 'display'
};
var TextRenderingMode = { var TextRenderingMode = {
FILL: 0, FILL: 0,
STROKE: 1, STROKE: 1,
@ -1369,6 +1375,7 @@ export {
MessageHandler, MessageHandler,
MissingDataException, MissingDataException,
MissingPDFException, MissingPDFException,
NativeImageDecoding,
NotImplementedException, NotImplementedException,
PageViewport, PageViewport,
PasswordException, PasswordException,