ucsc-xena-client
Version:
UCSC Xena Client. Functional genomics visualizations.
221 lines (199 loc) • 6.75 kB
JavaScript
;
// XXX Warning: opacity other than 1 is not interpreted correctly.
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var style = function style(c) {
return c.match(/^rgba?\(/) ? c.match(/rgba?\(([0-9]+), ([0-9]+), ([0-9]+)(, 1)?\)/).slice(1, 4) : c;
};
module.exports = function (doc, vgw, vgh) {
var fontFamily = 'Helvetica',
notImplemented = function notImplemented() {
return console.log('Not implemented');
},
// setting font is expensive, so cache it.
setfont = function setfont(family, size) {
return doc.font(family).fontSize(size);
},
scale = function scale(x, y, cb) {
doc.save();
doc.scale(x, y, {});
cb.apply(this);
doc.restore();
},
translate = function translate(x, y, cb) {
doc.save();
doc.translate(x, y);
cb.apply(this);
doc.restore();
},
alpha = notImplemented,
box = function box(x, y, w, h, c) {
doc.rect(x, y, w, h).fill(style(c));
},
clear = notImplemented,
circle = notImplemented,
clipRect = function clipRect(x, y, w, h) {
doc.save();
doc.rect(x, y, w, h).clip();
},
clipReset = function clipReset() {
doc.restore();
},
clip = function clip(x, y, w, h, fn) {
clipRect(x, y, w, h);
fn.apply(this);
clipReset();
},
width = notImplemented,
height = notImplemented,
element = notImplemented,
// getting a bit weird, here. Writing mock 2d canvas ctx in order to
// support img data in pdf. Should probably improve the vg API, instead.
context = function context() {
var el = document.createElement('canvas'),
ctx = el.getContext('2d');
el.width = vgw;
el.height = vgh;
return {
createImageData: function createImageData(w, h) {
el.width = w;
el.height = h;
return ctx.createImageData(w, h);
},
putImageData: function putImageData(img, x, y) {
ctx.putImageData(img, x, y);
doc.image(el.toDataURL(), 0, 0);
}
};
},
text = function text(x, cy, c, fontHeight, txt) {
// See https://github.com/devongovett/pdfkit/issues/351 for y correction.
var y = cy - doc._font.ascender / 1000 * fontHeight;
setfont(fontFamily, fontHeight);
doc.fill(style(c));
doc.text(txt, x, y, { lineBreak: false }); // lineBreak is broken; disable it.
// setfont(fontFamily, font);
// ctx.fillStyle = style(c);
// ctx.fillText(txt, x, y);
},
textWidth = function textWidth(font, txt) {
doc.font(fontFamily).fontSize(font);
return doc.widthOfString(txt);
},
textCentered = notImplemented,
// Center text if there's room. If the box is too narrow, push
// right so the left-most (first) characters are visible.
textCenteredPushRight = function textCenteredPushRight(x, y, w, h, c, fontHeight, val) {
// See https://github.com/devongovett/pdfkit/issues/351 for y correction.
setfont(fontFamily, fontHeight);
var opts = { width: w, height: h, align: 'center' },
// adj = doc._font.ascender / 1000 * fontHeight,
txt = String(val),
th = doc.heightOfString(txt, opts),
// pdfkit places text by upper-left corner, which is the same as
// the coord we are passed. Here we center it vertically, if there's
// space. Otherwise we use the passed y coordinate so the 1st line
// is visible. The +2 fudge is determined emperically, and should
// probably be based on line spacing, font height, or something.
ty = (th > h ? y : y + h / 2 - th / 2) + 2;
doc.fill(style(c));
doc.text(txt, x, ty, opts);
// There was an infinite loop with 'lineBreak' if text was placed
// outside the page margin. We disabled it for that reason. Enabling
// it now, because we want text to wrap. So far, the bug has not
// reappeared, but any lock-up with 100% cpu is likely due to
// hitting this bug, again.
// doc.text(txt, tx, ty, {lineBreak: false}); // lineBreak is broken; disable it.
},
textRight = notImplemented,
verticalTextRight = notImplemented,
makeColorGradient = notImplemented,
drawImage = notImplemented,
smoothing = function smoothing() {},
labels = function labels(cb) {
return cb();
},
drawPoly = function drawPoly(pts, _ref) {
var fillStyle = _ref.fillStyle,
strokeStyle = _ref.strokeStyle,
lineWidth = _ref.lineWidth;
pts.forEach(function (_ref2) {
var _ref3 = _slicedToArray(_ref2, 4),
mtx = _ref3[0],
mty = _ref3[1],
ltx = _ref3[2],
lty = _ref3[3];
if (ltx === undefined) {
doc.lineTo(mtx, mty);
} else {
doc.moveTo(mtx, mty);
doc.lineTo(ltx, lty);
}
});
if (fillStyle && strokeStyle) {
// This oddity is required by pdf.
doc.lineWidth(lineWidth);
doc.fillAndStroke(fillStyle, strokeStyle);
} else {
if (fillStyle) {
doc.fill(style(fillStyle));
}
if (strokeStyle) {
doc.lineWidth(lineWidth);
doc.stroke(style(strokeStyle));
}
}
},
drawRectangles = function drawRectangles(rects, _ref4) {
var fillStyle = _ref4.fillStyle,
strokeStyle = _ref4.strokeStyle,
lineWidth = _ref4.lineWidth;
rects.forEach(function (_ref5) {
var _ref6 = _slicedToArray(_ref5, 4),
x = _ref6[0],
y = _ref6[1],
w = _ref6[2],
h = _ref6[3];
return doc.rect(x, y, w, h);
});
if (fillStyle && strokeStyle) {
// This oddity is required by pdf.
doc.lineWidth(lineWidth);
doc.fillAndStroke(fillStyle, strokeStyle);
} else {
if (fillStyle) {
doc.fill(style(fillStyle));
}
if (strokeStyle) {
doc.lineWidth(lineWidth);
doc.stroke(style(strokeStyle));
}
}
};
return {
alpha: alpha,
box: box,
circle: circle,
clear: clear,
clip: clip,
clipRect: clipRect,
clipReset: clipReset,
context: context,
drawImage: drawImage,
drawPoly: drawPoly,
drawRectangles: drawRectangles,
element: element,
height: height,
labels: labels,
makeColorGradient: makeColorGradient,
scale: scale,
smoothing: smoothing,
text: text,
textCentered: textCentered,
textCenteredPushRight: textCenteredPushRight,
textRight: textRight,
textWidth: textWidth,
translate: translate,
verticalTextRight: verticalTextRight,
width: width
};
};