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,7 +199,7 @@ 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;
|
||||||
@ -225,8 +222,7 @@ var convertImgDataToPng = (function convertImgDataToPngClosure() {
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
@ -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);
|
||||||
@ -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;
|
||||||
@ -751,10 +737,8 @@ var SVGGraphics = (function SVGGraphicsClosure(ctx) {
|
|||||||
|
|
||||||
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++) {
|
||||||
@ -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