Miscellaneous code improvements for svg.js
This commit is contained in:
parent
f36bfccedf
commit
44fbf0ce14
@ -60,7 +60,6 @@ var convertImgDataToPng = (function convertImgDataToPngClosure() {
|
|||||||
|
|
||||||
function writePngChunk(type, body, data, offset) {
|
function writePngChunk(type, body, data, offset) {
|
||||||
var p = offset;
|
var p = offset;
|
||||||
|
|
||||||
var len = body.length;
|
var len = body.length;
|
||||||
|
|
||||||
data[p] = len >> 24 & 0xff;
|
data[p] = len >> 24 & 0xff;
|
||||||
@ -99,11 +98,9 @@ var convertImgDataToPng = (function convertImgDataToPngClosure() {
|
|||||||
function encode(imgData, kind) {
|
function encode(imgData, kind) {
|
||||||
var width = imgData.width;
|
var width = imgData.width;
|
||||||
var height = imgData.height;
|
var height = imgData.height;
|
||||||
var bitDepth;
|
var bitDepth, colorType, lineSize;
|
||||||
var colorType;
|
|
||||||
|
|
||||||
var bytes = imgData.data;
|
var bytes = imgData.data;
|
||||||
var lineSize;
|
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
case ImageKind.GRAYSCALE_1BPP:
|
case ImageKind.GRAYSCALE_1BPP:
|
||||||
colorType = 0;
|
colorType = 0;
|
||||||
@ -202,8 +199,8 @@ var convertImgDataToPng = (function convertImgDataToPngClosure() {
|
|||||||
idat[pi++] = adler & 0xff;
|
idat[pi++] = adler & 0xff;
|
||||||
|
|
||||||
// PNG will consists: header, IHDR+data, IDAT+data, and IEND.
|
// PNG will consists: header, IHDR+data, IDAT+data, and IEND.
|
||||||
var pngLength = PNG_HEADER.length + CHUNK_WRAPPER_SIZE * 3 +
|
var pngLength = PNG_HEADER.length + (CHUNK_WRAPPER_SIZE * 3) +
|
||||||
ihdr.length + idat.length;
|
ihdr.length + idat.length;
|
||||||
var data = new Uint8Array(pngLength);
|
var data = new Uint8Array(pngLength);
|
||||||
var offset = 0;
|
var offset = 0;
|
||||||
data.set(PNG_HEADER, offset);
|
data.set(PNG_HEADER, offset);
|
||||||
@ -219,14 +216,13 @@ var convertImgDataToPng = (function convertImgDataToPngClosure() {
|
|||||||
|
|
||||||
return function convertImgDataToPng(imgData) {
|
return function convertImgDataToPng(imgData) {
|
||||||
var kind = (imgData.kind === undefined ?
|
var kind = (imgData.kind === undefined ?
|
||||||
ImageKind.GRAYSCALE_1BPP : imgData.kind);
|
ImageKind.GRAYSCALE_1BPP : imgData.kind);
|
||||||
return encode(imgData, kind);
|
return encode(imgData, kind);
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
var SVGExtraState = (function SVGExtraStateClosure() {
|
var SVGExtraState = (function SVGExtraStateClosure() {
|
||||||
function SVGExtraState(old) {
|
function SVGExtraState() {
|
||||||
// Are soft masks and alpha values shapes or opacities?
|
|
||||||
this.fontSizeScale = 1;
|
this.fontSizeScale = 1;
|
||||||
this.fontWeight = 'normal';
|
this.fontWeight = 'normal';
|
||||||
this.fontSize = 0;
|
this.fontSize = 0;
|
||||||
@ -234,18 +230,22 @@ var SVGExtraState = (function SVGExtraStateClosure() {
|
|||||||
this.textMatrix = IDENTITY_MATRIX;
|
this.textMatrix = IDENTITY_MATRIX;
|
||||||
this.fontMatrix = FONT_IDENTITY_MATRIX;
|
this.fontMatrix = FONT_IDENTITY_MATRIX;
|
||||||
this.leading = 0;
|
this.leading = 0;
|
||||||
|
|
||||||
// Current point (in user coordinates)
|
// Current point (in user coordinates)
|
||||||
this.x = 0;
|
this.x = 0;
|
||||||
this.y = 0;
|
this.y = 0;
|
||||||
|
|
||||||
// Start of text line (in text coordinates)
|
// Start of text line (in text coordinates)
|
||||||
this.lineX = 0;
|
this.lineX = 0;
|
||||||
this.lineY = 0;
|
this.lineY = 0;
|
||||||
|
|
||||||
// Character and word spacing
|
// Character and word spacing
|
||||||
this.charSpacing = 0;
|
this.charSpacing = 0;
|
||||||
this.wordSpacing = 0;
|
this.wordSpacing = 0;
|
||||||
this.textHScale = 1;
|
this.textHScale = 1;
|
||||||
this.textRise = 0;
|
this.textRise = 0;
|
||||||
// Default fore and background colors
|
|
||||||
|
// Default foreground and background colors
|
||||||
this.fillColor = '#000000';
|
this.fillColor = '#000000';
|
||||||
this.strokeColor = '#000000';
|
this.strokeColor = '#000000';
|
||||||
|
|
||||||
@ -259,7 +259,6 @@ var SVGExtraState = (function SVGExtraStateClosure() {
|
|||||||
this.dashArray = [];
|
this.dashArray = [];
|
||||||
this.dashPhase = 0;
|
this.dashPhase = 0;
|
||||||
|
|
||||||
// Dependency
|
|
||||||
this.dependencies = [];
|
this.dependencies = [];
|
||||||
|
|
||||||
// Clipping
|
// Clipping
|
||||||
@ -281,13 +280,10 @@ var SVGExtraState = (function SVGExtraStateClosure() {
|
|||||||
return SVGExtraState;
|
return SVGExtraState;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
var SVGGraphics = (function SVGGraphicsClosure() {
|
||||||
function opListToTree(opList) {
|
function opListToTree(opList) {
|
||||||
var opTree = [];
|
var opTree = [];
|
||||||
var saveIdx = [];
|
|
||||||
var restIdx = [];
|
|
||||||
var tmp = [];
|
var tmp = [];
|
||||||
var items = [];
|
|
||||||
var opListLen = opList.length;
|
var opListLen = opList.length;
|
||||||
|
|
||||||
for (var x = 0; x < opListLen; x++) {
|
for (var x = 0; x < opListLen; x++) {
|
||||||
@ -357,7 +353,6 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function SVGGraphics(commonObjs, objs) {
|
function SVGGraphics(commonObjs, objs) {
|
||||||
|
|
||||||
this.current = new SVGExtraState();
|
this.current = new SVGExtraState();
|
||||||
this.transformMatrix = IDENTITY_MATRIX; // Graphics state matrix
|
this.transformMatrix = IDENTITY_MATRIX; // Graphics state matrix
|
||||||
this.transformStack = [];
|
this.transformStack = [];
|
||||||
@ -372,8 +367,6 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
var XLINK_NS = 'http://www.w3.org/1999/xlink';
|
var XLINK_NS = 'http://www.w3.org/1999/xlink';
|
||||||
var LINE_CAP_STYLES = ['butt', 'round', 'square'];
|
var LINE_CAP_STYLES = ['butt', 'round', 'square'];
|
||||||
var LINE_JOIN_STYLES = ['miter', 'round', 'bevel'];
|
var LINE_JOIN_STYLES = ['miter', 'round', 'bevel'];
|
||||||
var NORMAL_CLIP = {};
|
|
||||||
var EO_CLIP = {};
|
|
||||||
var clipCount = 0;
|
var clipCount = 0;
|
||||||
var maskCount = 0;
|
var maskCount = 0;
|
||||||
|
|
||||||
@ -432,14 +425,14 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
transform: function SVGGraphics_transform(a, b, c, d, e, f) {
|
transform: function SVGGraphics_transform(a, b, c, d, e, f) {
|
||||||
var transformMatrix = [a, b, c, d, e, f];
|
var transformMatrix = [a, b, c, d, e, f];
|
||||||
this.transformMatrix = PDFJS.Util.transform(this.transformMatrix,
|
this.transformMatrix = PDFJS.Util.transform(this.transformMatrix,
|
||||||
transformMatrix);
|
transformMatrix);
|
||||||
|
|
||||||
this.tgrp = document.createElementNS(NS, 'svg:g');
|
this.tgrp = document.createElementNS(NS, 'svg:g');
|
||||||
this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
|
this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
|
||||||
},
|
},
|
||||||
|
|
||||||
beginDrawing: function SVGGraphics_beginDrawing(viewport, pageNum,
|
beginDrawing: function SVGGraphics_beginDrawing(viewport, pageNum,
|
||||||
container, operatorList) {
|
container, operatorList) {
|
||||||
this.svg = createScratchSVG(viewport.width, viewport.height);
|
this.svg = createScratchSVG(viewport.width, viewport.height);
|
||||||
this.container = container;
|
this.container = container;
|
||||||
this.viewport = viewport;
|
this.viewport = viewport;
|
||||||
@ -461,22 +454,18 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
var argsArray = operatorList.argsArray;
|
var argsArray = operatorList.argsArray;
|
||||||
var fnArray = operatorList.fnArray;
|
var fnArray = operatorList.fnArray;
|
||||||
var fnArrayLen = fnArray.length;
|
var fnArrayLen = fnArray.length;
|
||||||
var argsArrayLen = argsArray.length;
|
|
||||||
var opTree = [];
|
|
||||||
|
|
||||||
var REVOPS = [];
|
var REVOPS = [];
|
||||||
|
var opList = [];
|
||||||
|
|
||||||
for (var op in OPS) {
|
for (var op in OPS) {
|
||||||
REVOPS[OPS[op]] = op;
|
REVOPS[OPS[op]] = op;
|
||||||
}
|
}
|
||||||
|
|
||||||
var opList = [];
|
|
||||||
for (var x = 0; x < fnArrayLen; x++) {
|
for (var x = 0; x < fnArrayLen; x++) {
|
||||||
var fnId = fnArray[x];
|
var fnId = fnArray[x];
|
||||||
opList.push({'fnId' : fnId, 'fn': REVOPS[fnId], 'args': argsArray[x]});
|
opList.push({'fnId' : fnId, 'fn': REVOPS[fnId], 'args': argsArray[x]});
|
||||||
}
|
}
|
||||||
opTree = opListToTree(opList);
|
return opListToTree(opList);
|
||||||
return opTree;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
executeOpTree: function SVGGraphics_executeOpTree(opTree) {
|
executeOpTree: function SVGGraphics_executeOpTree(opTree) {
|
||||||
@ -488,7 +477,7 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
|
|
||||||
switch (fnId | 0) {
|
switch (fnId | 0) {
|
||||||
case OPS.beginText:
|
case OPS.beginText:
|
||||||
this.beginText(args);
|
this.beginText();
|
||||||
break;
|
break;
|
||||||
case OPS.setLeading:
|
case OPS.setLeading:
|
||||||
this.setLeading(args);
|
this.setLeading(args);
|
||||||
@ -519,7 +508,7 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
break;
|
break;
|
||||||
case OPS.setTextMatrix:
|
case OPS.setTextMatrix:
|
||||||
this.setTextMatrix(args[0], args[1], args[2],
|
this.setTextMatrix(args[0], args[1], args[2],
|
||||||
args[3], args[4], args[5]);
|
args[3], args[4], args[5]);
|
||||||
break;
|
break;
|
||||||
case OPS.setLineWidth:
|
case OPS.setLineWidth:
|
||||||
this.setLineWidth(args[0]);
|
this.setLineWidth(args[0]);
|
||||||
@ -595,7 +584,7 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
break;
|
break;
|
||||||
case OPS.transform:
|
case OPS.transform:
|
||||||
this.transform(args[0], args[1], args[2], args[3],
|
this.transform(args[0], args[1], args[2], args[3],
|
||||||
args[4], args[5]);
|
args[4], args[5]);
|
||||||
break;
|
break;
|
||||||
case OPS.constructPath:
|
case OPS.constructPath:
|
||||||
this.constructPath(args[0], args[1]);
|
this.constructPath(args[0], args[1]);
|
||||||
@ -607,7 +596,7 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
this.group(opTree[x].items);
|
this.group(opTree[x].items);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
warn('Unimplemented Method '+ fn);
|
warn('Unimplemented method '+ fn);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -627,8 +616,7 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
|
|
||||||
setTextMatrix: function SVGGraphics_setTextMatrix(a, b, c, d, e, f) {
|
setTextMatrix: function SVGGraphics_setTextMatrix(a, b, c, d, e, f) {
|
||||||
var current = this.current;
|
var current = this.current;
|
||||||
this.current.textMatrix = [a, b, c, d, e, f];
|
this.current.textMatrix = this.current.lineMatrix = [a, b, c, d, e, f];
|
||||||
this.current.lineMatrix = [a, b, c, d, e, f];
|
|
||||||
|
|
||||||
this.current.x = this.current.lineX = 0;
|
this.current.x = this.current.lineX = 0;
|
||||||
this.current.y = this.current.lineY = 0;
|
this.current.y = this.current.lineY = 0;
|
||||||
@ -644,7 +632,7 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
current.txtElement.appendChild(current.tspan);
|
current.txtElement.appendChild(current.tspan);
|
||||||
},
|
},
|
||||||
|
|
||||||
beginText: function SVGGraphics_beginText(args) {
|
beginText: function SVGGraphics_beginText() {
|
||||||
this.current.x = this.current.lineX = 0;
|
this.current.x = this.current.lineX = 0;
|
||||||
this.current.y = this.current.lineY = 0;
|
this.current.y = this.current.lineY = 0;
|
||||||
this.current.textMatrix = IDENTITY_MATRIX;
|
this.current.textMatrix = IDENTITY_MATRIX;
|
||||||
@ -677,14 +665,12 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var fontSizeScale = current.fontSizeScale;
|
|
||||||
var charSpacing = current.charSpacing;
|
var charSpacing = current.charSpacing;
|
||||||
var wordSpacing = current.wordSpacing;
|
var wordSpacing = current.wordSpacing;
|
||||||
var fontDirection = current.fontDirection;
|
var fontDirection = current.fontDirection;
|
||||||
var textHScale = current.textHScale * fontDirection;
|
var textHScale = current.textHScale * fontDirection;
|
||||||
var glyphsLength = glyphs.length;
|
var glyphsLength = glyphs.length;
|
||||||
var vertical = font.vertical;
|
var vertical = font.vertical;
|
||||||
var defaultVMetrics = font.defaultVMetrics;
|
|
||||||
var widthAdvanceScale = fontSize * current.fontMatrix[0];
|
var widthAdvanceScale = fontSize * current.fontMatrix[0];
|
||||||
|
|
||||||
var x = 0, i;
|
var x = 0, i;
|
||||||
@ -747,14 +733,12 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
this.current.font = fontObj;
|
this.current.font = fontObj;
|
||||||
|
|
||||||
current.fontMatrix = (fontObj.fontMatrix ?
|
current.fontMatrix = (fontObj.fontMatrix ?
|
||||||
fontObj.fontMatrix : FONT_IDENTITY_MATRIX);
|
fontObj.fontMatrix : FONT_IDENTITY_MATRIX);
|
||||||
|
|
||||||
var bold = fontObj.black ? (fontObj.bold ? 'bolder' : 'bold') :
|
var bold = fontObj.black ? (fontObj.bold ? 'bolder' : 'bold') :
|
||||||
(fontObj.bold ? 'bold' : 'normal');
|
(fontObj.bold ? 'bold' : 'normal');
|
||||||
|
|
||||||
var italic = fontObj.italic ? 'italic' : 'normal';
|
var italic = fontObj.italic ? 'italic' : 'normal';
|
||||||
|
|
||||||
|
|
||||||
if (size < 0) {
|
if (size < 0) {
|
||||||
size = -size;
|
size = -size;
|
||||||
current.fontDirection = -1;
|
current.fontDirection = -1;
|
||||||
@ -771,7 +755,7 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
current.xcoords = [];
|
current.xcoords = [];
|
||||||
},
|
},
|
||||||
|
|
||||||
endText: function SVGGraphics_endText(args) {
|
endText: function SVGGraphics_endText() {
|
||||||
if (this.current.pendingClip) {
|
if (this.current.pendingClip) {
|
||||||
this.pgrp.appendChild(this.cgrp);
|
this.pgrp.appendChild(this.cgrp);
|
||||||
} else {
|
} else {
|
||||||
@ -814,7 +798,6 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
var x = current.x, y = current.y;
|
var x = current.x, y = current.y;
|
||||||
current.path = document.createElementNS(NS, 'svg:path');
|
current.path = document.createElementNS(NS, 'svg:path');
|
||||||
var d = [];
|
var d = [];
|
||||||
var arr = [];
|
|
||||||
var opLength = ops.length;
|
var opLength = ops.length;
|
||||||
|
|
||||||
for (var i = 0, j = 0; i < opLength; i++) {
|
for (var i = 0, j = 0; i < opLength; i++) {
|
||||||
@ -924,7 +907,7 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
current.pendingClip = true;
|
current.pendingClip = true;
|
||||||
this.cgrp = document.createElementNS(NS, 'svg:g');
|
this.cgrp = document.createElementNS(NS, 'svg:g');
|
||||||
this.cgrp.setAttributeNS(null, 'clip-path',
|
this.cgrp.setAttributeNS(null, 'clip-path',
|
||||||
'url(#' + current.clipId + ')');
|
'url(#' + current.clipId + ')');
|
||||||
this.pgrp.appendChild(this.cgrp);
|
this.pgrp.appendChild(this.cgrp);
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -1066,7 +1049,6 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
warn('Dependent image isn\'t ready yet');
|
warn('Dependent image isn\'t ready yet');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.paintInlineImageXObject(imgData);
|
this.paintInlineImageXObject(imgData);
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -1109,11 +1091,8 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
paintImageMaskXObject:
|
paintImageMaskXObject:
|
||||||
function SVGGraphics_paintImageMaskXObject(imgData) {
|
function SVGGraphics_paintImageMaskXObject(imgData) {
|
||||||
var current = this.current;
|
var current = this.current;
|
||||||
|
|
||||||
var width = imgData.width;
|
var width = imgData.width;
|
||||||
var height = imgData.height;
|
var height = imgData.height;
|
||||||
|
|
||||||
var img = convertImgDataToPng(imgData);
|
|
||||||
var fillColor = current.fillColor;
|
var fillColor = current.fillColor;
|
||||||
|
|
||||||
current.maskId = 'mask' + maskCount++;
|
current.maskId = 'mask' + maskCount++;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user