123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836 |
- "use strict";
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
- return new (P || (P = Promise))(function (resolve, reject) {
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
- step((generator = generator.apply(thisArg, _arguments || [])).next());
- });
- };
- var __generator = (this && this.__generator) || function (thisArg, body) {
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
- function verb(n) { return function (v) { return step([n, v]); }; }
- function step(op) {
- if (f) throw new TypeError("Generator is already executing.");
- while (_) try {
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
- if (y = 0, t) op = [op[0] & 2, t.value];
- switch (op[0]) {
- case 0: case 1: t = op; break;
- case 4: _.label++; return { value: op[1], done: false };
- case 5: _.label++; y = op[1]; op = [0]; continue;
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
- default:
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
- if (t[2]) _.ops.pop();
- _.trys.pop(); continue;
- }
- op = body.call(thisArg, _);
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
- }
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- var stacking_context_1 = require("../stacking-context");
- var color_1 = require("../../css/types/color");
- var logger_1 = require("../../core/logger");
- var border_style_1 = require("../../css/property-descriptors/border-style");
- var path_1 = require("../path");
- var background_clip_1 = require("../../css/property-descriptors/background-clip");
- var bound_curves_1 = require("../bound-curves");
- var bezier_curve_1 = require("../bezier-curve");
- var vector_1 = require("../vector");
- var image_1 = require("../../css/types/image");
- var border_1 = require("../border");
- var background_1 = require("../background");
- var parser_1 = require("../../css/syntax/parser");
- var text_1 = require("../../css/layout/text");
- var css_line_break_1 = require("css-line-break");
- var image_element_container_1 = require("../../dom/replaced-elements/image-element-container");
- var box_sizing_1 = require("../box-sizing");
- var canvas_element_container_1 = require("../../dom/replaced-elements/canvas-element-container");
- var svg_element_container_1 = require("../../dom/replaced-elements/svg-element-container");
- var effects_1 = require("../effects");
- var bitwise_1 = require("../../core/bitwise");
- var gradient_1 = require("../../css/types/functions/gradient");
- var length_percentage_1 = require("../../css/types/length-percentage");
- var font_metrics_1 = require("../font-metrics");
- var bounds_1 = require("../../css/layout/bounds");
- var list_style_type_1 = require("../../css/property-descriptors/list-style-type");
- var line_height_1 = require("../../css/property-descriptors/line-height");
- var input_element_container_1 = require("../../dom/replaced-elements/input-element-container");
- var text_align_1 = require("../../css/property-descriptors/text-align");
- var textarea_element_container_1 = require("../../dom/elements/textarea-element-container");
- var select_element_container_1 = require("../../dom/elements/select-element-container");
- var iframe_element_container_1 = require("../../dom/replaced-elements/iframe-element-container");
- var MASK_OFFSET = 10000;
- var CanvasRenderer = /** @class */ (function () {
- function CanvasRenderer(options) {
- this._activeEffects = [];
- this.canvas = options.canvas ? options.canvas : document.createElement('canvas');
- this.ctx = this.canvas.getContext('2d');
- this.options = options;
- if (!options.canvas) {
- this.canvas.width = Math.floor(options.width * options.scale);
- this.canvas.height = Math.floor(options.height * options.scale);
- this.canvas.style.width = options.width + "px";
- this.canvas.style.height = options.height + "px";
- }
- this.fontMetrics = new font_metrics_1.FontMetrics(document);
- this.ctx.scale(this.options.scale, this.options.scale);
- this.ctx.translate(-options.x + options.scrollX, -options.y + options.scrollY);
- this.ctx.textBaseline = 'bottom';
- this._activeEffects = [];
- logger_1.Logger.getInstance(options.id).debug("Canvas renderer initialized (" + options.width + "x" + options.height + " at " + options.x + "," + options.y + ") with scale " + options.scale);
- }
- CanvasRenderer.prototype.applyEffects = function (effects, target) {
- var _this = this;
- while (this._activeEffects.length) {
- this.popEffect();
- }
- effects.filter(function (effect) { return bitwise_1.contains(effect.target, target); }).forEach(function (effect) { return _this.applyEffect(effect); });
- };
- CanvasRenderer.prototype.applyEffect = function (effect) {
- this.ctx.save();
- if (effects_1.isTransformEffect(effect)) {
- this.ctx.translate(effect.offsetX, effect.offsetY);
- this.ctx.transform(effect.matrix[0], effect.matrix[1], effect.matrix[2], effect.matrix[3], effect.matrix[4], effect.matrix[5]);
- this.ctx.translate(-effect.offsetX, -effect.offsetY);
- }
- if (effects_1.isClipEffect(effect)) {
- this.path(effect.path);
- this.ctx.clip();
- }
- this._activeEffects.push(effect);
- };
- CanvasRenderer.prototype.popEffect = function () {
- this._activeEffects.pop();
- this.ctx.restore();
- };
- CanvasRenderer.prototype.renderStack = function (stack) {
- return __awaiter(this, void 0, void 0, function () {
- var styles;
- return __generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- styles = stack.element.container.styles;
- if (!styles.isVisible()) return [3 /*break*/, 2];
- this.ctx.globalAlpha = styles.opacity;
- return [4 /*yield*/, this.renderStackContent(stack)];
- case 1:
- _a.sent();
- _a.label = 2;
- case 2: return [2 /*return*/];
- }
- });
- });
- };
- CanvasRenderer.prototype.renderNode = function (paint) {
- return __awaiter(this, void 0, void 0, function () {
- return __generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- if (!paint.container.styles.isVisible()) return [3 /*break*/, 3];
- return [4 /*yield*/, this.renderNodeBackgroundAndBorders(paint)];
- case 1:
- _a.sent();
- return [4 /*yield*/, this.renderNodeContent(paint)];
- case 2:
- _a.sent();
- _a.label = 3;
- case 3: return [2 /*return*/];
- }
- });
- });
- };
- CanvasRenderer.prototype.renderTextWithLetterSpacing = function (text, letterSpacing) {
- var _this = this;
- if (letterSpacing === 0) {
- this.ctx.fillText(text.text, text.bounds.left, text.bounds.top + text.bounds.height);
- }
- else {
- var letters = css_line_break_1.toCodePoints(text.text).map(function (i) { return css_line_break_1.fromCodePoint(i); });
- letters.reduce(function (left, letter) {
- _this.ctx.fillText(letter, left, text.bounds.top + text.bounds.height);
- return left + _this.ctx.measureText(letter).width;
- }, text.bounds.left);
- }
- };
- CanvasRenderer.prototype.createFontStyle = function (styles) {
- var fontVariant = styles.fontVariant
- .filter(function (variant) { return variant === 'normal' || variant === 'small-caps'; })
- .join('');
- var fontFamily = styles.fontFamily.join(', ');
- var fontSize = parser_1.isDimensionToken(styles.fontSize)
- ? "" + styles.fontSize.number + styles.fontSize.unit
- : styles.fontSize.number + "px";
- return [
- [styles.fontStyle, fontVariant, styles.fontWeight, fontSize, fontFamily].join(' '),
- fontFamily,
- fontSize
- ];
- };
- CanvasRenderer.prototype.renderTextNode = function (text, styles) {
- return __awaiter(this, void 0, void 0, function () {
- var _a, font, fontFamily, fontSize;
- var _this = this;
- return __generator(this, function (_b) {
- _a = this.createFontStyle(styles), font = _a[0], fontFamily = _a[1], fontSize = _a[2];
- this.ctx.font = font;
- text.textBounds.forEach(function (text) {
- _this.ctx.fillStyle = color_1.asString(styles.color);
- _this.renderTextWithLetterSpacing(text, styles.letterSpacing);
- var textShadows = styles.textShadow;
- if (textShadows.length && text.text.trim().length) {
- textShadows
- .slice(0)
- .reverse()
- .forEach(function (textShadow) {
- _this.ctx.shadowColor = color_1.asString(textShadow.color);
- _this.ctx.shadowOffsetX = textShadow.offsetX.number * _this.options.scale;
- _this.ctx.shadowOffsetY = textShadow.offsetY.number * _this.options.scale;
- _this.ctx.shadowBlur = textShadow.blur.number;
- _this.ctx.fillText(text.text, text.bounds.left, text.bounds.top + text.bounds.height);
- });
- _this.ctx.shadowColor = '';
- _this.ctx.shadowOffsetX = 0;
- _this.ctx.shadowOffsetY = 0;
- _this.ctx.shadowBlur = 0;
- }
- if (styles.textDecorationLine.length) {
- _this.ctx.fillStyle = color_1.asString(styles.textDecorationColor || styles.color);
- styles.textDecorationLine.forEach(function (textDecorationLine) {
- switch (textDecorationLine) {
- case 1 /* UNDERLINE */:
- // Draws a line at the baseline of the font
- // TODO As some browsers display the line as more than 1px if the font-size is big,
- // need to take that into account both in position and size
- var baseline = _this.fontMetrics.getMetrics(fontFamily, fontSize).baseline;
- _this.ctx.fillRect(text.bounds.left, Math.round(text.bounds.top + baseline), text.bounds.width, 1);
- break;
- case 2 /* OVERLINE */:
- _this.ctx.fillRect(text.bounds.left, Math.round(text.bounds.top), text.bounds.width, 1);
- break;
- case 3 /* LINE_THROUGH */:
- // TODO try and find exact position for line-through
- var middle = _this.fontMetrics.getMetrics(fontFamily, fontSize).middle;
- _this.ctx.fillRect(text.bounds.left, Math.ceil(text.bounds.top + middle), text.bounds.width, 1);
- break;
- }
- });
- }
- });
- return [2 /*return*/];
- });
- });
- };
- CanvasRenderer.prototype.renderReplacedElement = function (container, curves, image) {
- if (image && container.intrinsicWidth > 0 && container.intrinsicHeight > 0) {
- var box = box_sizing_1.contentBox(container);
- var path = bound_curves_1.calculatePaddingBoxPath(curves);
- this.path(path);
- this.ctx.save();
- this.ctx.clip();
- this.ctx.drawImage(image, 0, 0, container.intrinsicWidth, container.intrinsicHeight, box.left, box.top, box.width, box.height);
- this.ctx.restore();
- }
- };
- CanvasRenderer.prototype.renderNodeContent = function (paint) {
- return __awaiter(this, void 0, void 0, function () {
- var container, curves, styles, _i, _a, child, image, e_1, image, e_2, iframeRenderer, canvas, size, bounds, x, textBounds, img, image, url, e_3, bounds;
- return __generator(this, function (_b) {
- switch (_b.label) {
- case 0:
- this.applyEffects(paint.effects, 4 /* CONTENT */);
- container = paint.container;
- curves = paint.curves;
- styles = container.styles;
- _i = 0, _a = container.textNodes;
- _b.label = 1;
- case 1:
- if (!(_i < _a.length)) return [3 /*break*/, 4];
- child = _a[_i];
- return [4 /*yield*/, this.renderTextNode(child, styles)];
- case 2:
- _b.sent();
- _b.label = 3;
- case 3:
- _i++;
- return [3 /*break*/, 1];
- case 4:
- if (!(container instanceof image_element_container_1.ImageElementContainer)) return [3 /*break*/, 8];
- _b.label = 5;
- case 5:
- _b.trys.push([5, 7, , 8]);
- return [4 /*yield*/, this.options.cache.match(container.src)];
- case 6:
- image = _b.sent();
- this.renderReplacedElement(container, curves, image);
- return [3 /*break*/, 8];
- case 7:
- e_1 = _b.sent();
- logger_1.Logger.getInstance(this.options.id).error("Error loading image " + container.src);
- return [3 /*break*/, 8];
- case 8:
- if (container instanceof canvas_element_container_1.CanvasElementContainer) {
- this.renderReplacedElement(container, curves, container.canvas);
- }
- if (!(container instanceof svg_element_container_1.SVGElementContainer)) return [3 /*break*/, 12];
- _b.label = 9;
- case 9:
- _b.trys.push([9, 11, , 12]);
- return [4 /*yield*/, this.options.cache.match(container.svg)];
- case 10:
- image = _b.sent();
- this.renderReplacedElement(container, curves, image);
- return [3 /*break*/, 12];
- case 11:
- e_2 = _b.sent();
- logger_1.Logger.getInstance(this.options.id).error("Error loading svg " + container.svg.substring(0, 255));
- return [3 /*break*/, 12];
- case 12:
- if (!(container instanceof iframe_element_container_1.IFrameElementContainer && container.tree)) return [3 /*break*/, 14];
- iframeRenderer = new CanvasRenderer({
- id: this.options.id,
- scale: this.options.scale,
- backgroundColor: container.backgroundColor,
- x: 0,
- y: 0,
- scrollX: 0,
- scrollY: 0,
- width: container.width,
- height: container.height,
- cache: this.options.cache,
- windowWidth: container.width,
- windowHeight: container.height
- });
- return [4 /*yield*/, iframeRenderer.render(container.tree)];
- case 13:
- canvas = _b.sent();
- if (container.width && container.height) {
- this.ctx.drawImage(canvas, 0, 0, container.width, container.height, container.bounds.left, container.bounds.top, container.bounds.width, container.bounds.height);
- }
- _b.label = 14;
- case 14:
- if (container instanceof input_element_container_1.InputElementContainer) {
- size = Math.min(container.bounds.width, container.bounds.height);
- if (container.type === input_element_container_1.CHECKBOX) {
- if (container.checked) {
- this.ctx.save();
- this.path([
- new vector_1.Vector(container.bounds.left + size * 0.39363, container.bounds.top + size * 0.79),
- new vector_1.Vector(container.bounds.left + size * 0.16, container.bounds.top + size * 0.5549),
- new vector_1.Vector(container.bounds.left + size * 0.27347, container.bounds.top + size * 0.44071),
- new vector_1.Vector(container.bounds.left + size * 0.39694, container.bounds.top + size * 0.5649),
- new vector_1.Vector(container.bounds.left + size * 0.72983, container.bounds.top + size * 0.23),
- new vector_1.Vector(container.bounds.left + size * 0.84, container.bounds.top + size * 0.34085),
- new vector_1.Vector(container.bounds.left + size * 0.39363, container.bounds.top + size * 0.79)
- ]);
- this.ctx.fillStyle = color_1.asString(input_element_container_1.INPUT_COLOR);
- this.ctx.fill();
- this.ctx.restore();
- }
- }
- else if (container.type === input_element_container_1.RADIO) {
- if (container.checked) {
- this.ctx.save();
- this.ctx.beginPath();
- this.ctx.arc(container.bounds.left + size / 2, container.bounds.top + size / 2, size / 4, 0, Math.PI * 2, true);
- this.ctx.fillStyle = color_1.asString(input_element_container_1.INPUT_COLOR);
- this.ctx.fill();
- this.ctx.restore();
- }
- }
- }
- if (isTextInputElement(container) && container.value.length) {
- this.ctx.font = this.createFontStyle(styles)[0];
- this.ctx.fillStyle = color_1.asString(styles.color);
- this.ctx.textBaseline = 'middle';
- this.ctx.textAlign = canvasTextAlign(container.styles.textAlign);
- bounds = box_sizing_1.contentBox(container);
- x = 0;
- switch (container.styles.textAlign) {
- case text_align_1.TEXT_ALIGN.CENTER:
- x += bounds.width / 2;
- break;
- case text_align_1.TEXT_ALIGN.RIGHT:
- x += bounds.width;
- break;
- }
- textBounds = bounds.add(x, 0, 0, -bounds.height / 2 + 1);
- this.ctx.save();
- this.path([
- new vector_1.Vector(bounds.left, bounds.top),
- new vector_1.Vector(bounds.left + bounds.width, bounds.top),
- new vector_1.Vector(bounds.left + bounds.width, bounds.top + bounds.height),
- new vector_1.Vector(bounds.left, bounds.top + bounds.height)
- ]);
- this.ctx.clip();
- this.renderTextWithLetterSpacing(new text_1.TextBounds(container.value, textBounds), styles.letterSpacing);
- this.ctx.restore();
- this.ctx.textBaseline = 'bottom';
- this.ctx.textAlign = 'left';
- }
- if (!bitwise_1.contains(container.styles.display, 2048 /* LIST_ITEM */)) return [3 /*break*/, 20];
- if (!(container.styles.listStyleImage !== null)) return [3 /*break*/, 19];
- img = container.styles.listStyleImage;
- if (!(img.type === image_1.CSSImageType.URL)) return [3 /*break*/, 18];
- image = void 0;
- url = img.url;
- _b.label = 15;
- case 15:
- _b.trys.push([15, 17, , 18]);
- return [4 /*yield*/, this.options.cache.match(url)];
- case 16:
- image = _b.sent();
- this.ctx.drawImage(image, container.bounds.left - (image.width + 10), container.bounds.top);
- return [3 /*break*/, 18];
- case 17:
- e_3 = _b.sent();
- logger_1.Logger.getInstance(this.options.id).error("Error loading list-style-image " + url);
- return [3 /*break*/, 18];
- case 18: return [3 /*break*/, 20];
- case 19:
- if (paint.listValue && container.styles.listStyleType !== list_style_type_1.LIST_STYLE_TYPE.NONE) {
- this.ctx.font = this.createFontStyle(styles)[0];
- this.ctx.fillStyle = color_1.asString(styles.color);
- this.ctx.textBaseline = 'middle';
- this.ctx.textAlign = 'right';
- bounds = new bounds_1.Bounds(container.bounds.left, container.bounds.top + length_percentage_1.getAbsoluteValue(container.styles.paddingTop, container.bounds.width), container.bounds.width, line_height_1.computeLineHeight(styles.lineHeight, styles.fontSize.number) / 2 + 1);
- this.renderTextWithLetterSpacing(new text_1.TextBounds(paint.listValue, bounds), styles.letterSpacing);
- this.ctx.textBaseline = 'bottom';
- this.ctx.textAlign = 'left';
- }
- _b.label = 20;
- case 20: return [2 /*return*/];
- }
- });
- });
- };
- CanvasRenderer.prototype.renderStackContent = function (stack) {
- return __awaiter(this, void 0, void 0, function () {
- var _i, _a, child, _b, _c, child, _d, _e, child, _f, _g, child, _h, _j, child, _k, _l, child, _m, _o, child;
- return __generator(this, function (_p) {
- switch (_p.label) {
- case 0:
- // https://www.w3.org/TR/css-position-3/#painting-order
- // 1. the background and borders of the element forming the stacking context.
- return [4 /*yield*/, this.renderNodeBackgroundAndBorders(stack.element)];
- case 1:
- // https://www.w3.org/TR/css-position-3/#painting-order
- // 1. the background and borders of the element forming the stacking context.
- _p.sent();
- _i = 0, _a = stack.negativeZIndex;
- _p.label = 2;
- case 2:
- if (!(_i < _a.length)) return [3 /*break*/, 5];
- child = _a[_i];
- return [4 /*yield*/, this.renderStack(child)];
- case 3:
- _p.sent();
- _p.label = 4;
- case 4:
- _i++;
- return [3 /*break*/, 2];
- case 5:
- // 3. For all its in-flow, non-positioned, block-level descendants in tree order:
- return [4 /*yield*/, this.renderNodeContent(stack.element)];
- case 6:
- // 3. For all its in-flow, non-positioned, block-level descendants in tree order:
- _p.sent();
- _b = 0, _c = stack.nonInlineLevel;
- _p.label = 7;
- case 7:
- if (!(_b < _c.length)) return [3 /*break*/, 10];
- child = _c[_b];
- return [4 /*yield*/, this.renderNode(child)];
- case 8:
- _p.sent();
- _p.label = 9;
- case 9:
- _b++;
- return [3 /*break*/, 7];
- case 10:
- _d = 0, _e = stack.nonPositionedFloats;
- _p.label = 11;
- case 11:
- if (!(_d < _e.length)) return [3 /*break*/, 14];
- child = _e[_d];
- return [4 /*yield*/, this.renderStack(child)];
- case 12:
- _p.sent();
- _p.label = 13;
- case 13:
- _d++;
- return [3 /*break*/, 11];
- case 14:
- _f = 0, _g = stack.nonPositionedInlineLevel;
- _p.label = 15;
- case 15:
- if (!(_f < _g.length)) return [3 /*break*/, 18];
- child = _g[_f];
- return [4 /*yield*/, this.renderStack(child)];
- case 16:
- _p.sent();
- _p.label = 17;
- case 17:
- _f++;
- return [3 /*break*/, 15];
- case 18:
- _h = 0, _j = stack.inlineLevel;
- _p.label = 19;
- case 19:
- if (!(_h < _j.length)) return [3 /*break*/, 22];
- child = _j[_h];
- return [4 /*yield*/, this.renderNode(child)];
- case 20:
- _p.sent();
- _p.label = 21;
- case 21:
- _h++;
- return [3 /*break*/, 19];
- case 22:
- _k = 0, _l = stack.zeroOrAutoZIndexOrTransformedOrOpacity;
- _p.label = 23;
- case 23:
- if (!(_k < _l.length)) return [3 /*break*/, 26];
- child = _l[_k];
- return [4 /*yield*/, this.renderStack(child)];
- case 24:
- _p.sent();
- _p.label = 25;
- case 25:
- _k++;
- return [3 /*break*/, 23];
- case 26:
- _m = 0, _o = stack.positiveZIndex;
- _p.label = 27;
- case 27:
- if (!(_m < _o.length)) return [3 /*break*/, 30];
- child = _o[_m];
- return [4 /*yield*/, this.renderStack(child)];
- case 28:
- _p.sent();
- _p.label = 29;
- case 29:
- _m++;
- return [3 /*break*/, 27];
- case 30: return [2 /*return*/];
- }
- });
- });
- };
- CanvasRenderer.prototype.mask = function (paths) {
- this.ctx.beginPath();
- this.ctx.moveTo(0, 0);
- this.ctx.lineTo(this.canvas.width, 0);
- this.ctx.lineTo(this.canvas.width, this.canvas.height);
- this.ctx.lineTo(0, this.canvas.height);
- this.ctx.lineTo(0, 0);
- this.formatPath(paths.slice(0).reverse());
- this.ctx.closePath();
- };
- CanvasRenderer.prototype.path = function (paths) {
- this.ctx.beginPath();
- this.formatPath(paths);
- this.ctx.closePath();
- };
- CanvasRenderer.prototype.formatPath = function (paths) {
- var _this = this;
- paths.forEach(function (point, index) {
- var start = bezier_curve_1.isBezierCurve(point) ? point.start : point;
- if (index === 0) {
- _this.ctx.moveTo(start.x, start.y);
- }
- else {
- _this.ctx.lineTo(start.x, start.y);
- }
- if (bezier_curve_1.isBezierCurve(point)) {
- _this.ctx.bezierCurveTo(point.startControl.x, point.startControl.y, point.endControl.x, point.endControl.y, point.end.x, point.end.y);
- }
- });
- };
- CanvasRenderer.prototype.renderRepeat = function (path, pattern, offsetX, offsetY) {
- this.path(path);
- this.ctx.fillStyle = pattern;
- this.ctx.translate(offsetX, offsetY);
- this.ctx.fill();
- this.ctx.translate(-offsetX, -offsetY);
- };
- CanvasRenderer.prototype.resizeImage = function (image, width, height) {
- if (image.width === width && image.height === height) {
- return image;
- }
- var canvas = this.canvas.ownerDocument.createElement('canvas');
- canvas.width = width;
- canvas.height = height;
- var ctx = canvas.getContext('2d');
- ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, width, height);
- return canvas;
- };
- CanvasRenderer.prototype.renderBackgroundImage = function (container) {
- return __awaiter(this, void 0, void 0, function () {
- var index, _loop_1, this_1, _i, _a, backgroundImage;
- return __generator(this, function (_b) {
- switch (_b.label) {
- case 0:
- index = container.styles.backgroundImage.length - 1;
- _loop_1 = function (backgroundImage) {
- var image, url, e_4, _a, path, x, y, width, height, pattern, _b, path, x, y, width, height, _c, lineLength, x0, x1, y0, y1, canvas, ctx, gradient_2, pattern, _d, path, left, top_1, width, height, position, x, y, _e, rx, ry, radialGradient_1, midX, midY, f, invF;
- return __generator(this, function (_f) {
- switch (_f.label) {
- case 0:
- if (!(backgroundImage.type === image_1.CSSImageType.URL)) return [3 /*break*/, 5];
- image = void 0;
- url = backgroundImage.url;
- _f.label = 1;
- case 1:
- _f.trys.push([1, 3, , 4]);
- return [4 /*yield*/, this_1.options.cache.match(url)];
- case 2:
- image = _f.sent();
- return [3 /*break*/, 4];
- case 3:
- e_4 = _f.sent();
- logger_1.Logger.getInstance(this_1.options.id).error("Error loading background-image " + url);
- return [3 /*break*/, 4];
- case 4:
- if (image) {
- _a = background_1.calculateBackgroundRendering(container, index, [
- image.width,
- image.height,
- image.width / image.height
- ]), path = _a[0], x = _a[1], y = _a[2], width = _a[3], height = _a[4];
- pattern = this_1.ctx.createPattern(this_1.resizeImage(image, width, height), 'repeat');
- this_1.renderRepeat(path, pattern, x, y);
- }
- return [3 /*break*/, 6];
- case 5:
- if (image_1.isLinearGradient(backgroundImage)) {
- _b = background_1.calculateBackgroundRendering(container, index, [null, null, null]), path = _b[0], x = _b[1], y = _b[2], width = _b[3], height = _b[4];
- _c = gradient_1.calculateGradientDirection(backgroundImage.angle, width, height), lineLength = _c[0], x0 = _c[1], x1 = _c[2], y0 = _c[3], y1 = _c[4];
- canvas = document.createElement('canvas');
- canvas.width = width;
- canvas.height = height;
- ctx = canvas.getContext('2d');
- gradient_2 = ctx.createLinearGradient(x0, y0, x1, y1);
- gradient_1.processColorStops(backgroundImage.stops, lineLength).forEach(function (colorStop) {
- return gradient_2.addColorStop(colorStop.stop, color_1.asString(colorStop.color));
- });
- ctx.fillStyle = gradient_2;
- ctx.fillRect(0, 0, width, height);
- if (width > 0 && height > 0) {
- pattern = this_1.ctx.createPattern(canvas, 'repeat');
- this_1.renderRepeat(path, pattern, x, y);
- }
- }
- else if (image_1.isRadialGradient(backgroundImage)) {
- _d = background_1.calculateBackgroundRendering(container, index, [
- null,
- null,
- null
- ]), path = _d[0], left = _d[1], top_1 = _d[2], width = _d[3], height = _d[4];
- position = backgroundImage.position.length === 0 ? [length_percentage_1.FIFTY_PERCENT] : backgroundImage.position;
- x = length_percentage_1.getAbsoluteValue(position[0], width);
- y = length_percentage_1.getAbsoluteValue(position[position.length - 1], height);
- _e = gradient_1.calculateRadius(backgroundImage, x, y, width, height), rx = _e[0], ry = _e[1];
- if (rx > 0 && rx > 0) {
- radialGradient_1 = this_1.ctx.createRadialGradient(left + x, top_1 + y, 0, left + x, top_1 + y, rx);
- gradient_1.processColorStops(backgroundImage.stops, rx * 2).forEach(function (colorStop) {
- return radialGradient_1.addColorStop(colorStop.stop, color_1.asString(colorStop.color));
- });
- this_1.path(path);
- this_1.ctx.fillStyle = radialGradient_1;
- if (rx !== ry) {
- midX = container.bounds.left + 0.5 * container.bounds.width;
- midY = container.bounds.top + 0.5 * container.bounds.height;
- f = ry / rx;
- invF = 1 / f;
- this_1.ctx.save();
- this_1.ctx.translate(midX, midY);
- this_1.ctx.transform(1, 0, 0, f, 0, 0);
- this_1.ctx.translate(-midX, -midY);
- this_1.ctx.fillRect(left, invF * (top_1 - midY) + midY, width, height * invF);
- this_1.ctx.restore();
- }
- else {
- this_1.ctx.fill();
- }
- }
- }
- _f.label = 6;
- case 6:
- index--;
- return [2 /*return*/];
- }
- });
- };
- this_1 = this;
- _i = 0, _a = container.styles.backgroundImage.slice(0).reverse();
- _b.label = 1;
- case 1:
- if (!(_i < _a.length)) return [3 /*break*/, 4];
- backgroundImage = _a[_i];
- return [5 /*yield**/, _loop_1(backgroundImage)];
- case 2:
- _b.sent();
- _b.label = 3;
- case 3:
- _i++;
- return [3 /*break*/, 1];
- case 4: return [2 /*return*/];
- }
- });
- });
- };
- CanvasRenderer.prototype.renderBorder = function (color, side, curvePoints) {
- return __awaiter(this, void 0, void 0, function () {
- return __generator(this, function (_a) {
- this.path(border_1.parsePathForBorder(curvePoints, side));
- this.ctx.fillStyle = color_1.asString(color);
- this.ctx.fill();
- return [2 /*return*/];
- });
- });
- };
- CanvasRenderer.prototype.renderNodeBackgroundAndBorders = function (paint) {
- return __awaiter(this, void 0, void 0, function () {
- var styles, hasBackground, borders, backgroundPaintingArea, side, _i, borders_1, border;
- var _this = this;
- return __generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- this.applyEffects(paint.effects, 2 /* BACKGROUND_BORDERS */);
- styles = paint.container.styles;
- hasBackground = !color_1.isTransparent(styles.backgroundColor) || styles.backgroundImage.length;
- borders = [
- { style: styles.borderTopStyle, color: styles.borderTopColor },
- { style: styles.borderRightStyle, color: styles.borderRightColor },
- { style: styles.borderBottomStyle, color: styles.borderBottomColor },
- { style: styles.borderLeftStyle, color: styles.borderLeftColor }
- ];
- backgroundPaintingArea = calculateBackgroundCurvedPaintingArea(background_1.getBackgroundValueForIndex(styles.backgroundClip, 0), paint.curves);
- if (!(hasBackground || styles.boxShadow.length)) return [3 /*break*/, 2];
- this.ctx.save();
- this.path(backgroundPaintingArea);
- this.ctx.clip();
- if (!color_1.isTransparent(styles.backgroundColor)) {
- this.ctx.fillStyle = color_1.asString(styles.backgroundColor);
- this.ctx.fill();
- }
- return [4 /*yield*/, this.renderBackgroundImage(paint.container)];
- case 1:
- _a.sent();
- this.ctx.restore();
- styles.boxShadow
- .slice(0)
- .reverse()
- .forEach(function (shadow) {
- _this.ctx.save();
- var borderBoxArea = bound_curves_1.calculateBorderBoxPath(paint.curves);
- var maskOffset = shadow.inset ? 0 : MASK_OFFSET;
- var shadowPaintingArea = path_1.transformPath(borderBoxArea, -maskOffset + (shadow.inset ? 1 : -1) * shadow.spread.number, (shadow.inset ? 1 : -1) * shadow.spread.number, shadow.spread.number * (shadow.inset ? -2 : 2), shadow.spread.number * (shadow.inset ? -2 : 2));
- if (shadow.inset) {
- _this.path(borderBoxArea);
- _this.ctx.clip();
- _this.mask(shadowPaintingArea);
- }
- else {
- _this.mask(borderBoxArea);
- _this.ctx.clip();
- _this.path(shadowPaintingArea);
- }
- _this.ctx.shadowOffsetX = shadow.offsetX.number + maskOffset;
- _this.ctx.shadowOffsetY = shadow.offsetY.number;
- _this.ctx.shadowColor = color_1.asString(shadow.color);
- _this.ctx.shadowBlur = shadow.blur.number;
- _this.ctx.fillStyle = shadow.inset ? color_1.asString(shadow.color) : 'rgba(0,0,0,1)';
- _this.ctx.fill();
- _this.ctx.restore();
- });
- _a.label = 2;
- case 2:
- side = 0;
- _i = 0, borders_1 = borders;
- _a.label = 3;
- case 3:
- if (!(_i < borders_1.length)) return [3 /*break*/, 7];
- border = borders_1[_i];
- if (!(border.style !== border_style_1.BORDER_STYLE.NONE && !color_1.isTransparent(border.color))) return [3 /*break*/, 5];
- return [4 /*yield*/, this.renderBorder(border.color, side, paint.curves)];
- case 4:
- _a.sent();
- _a.label = 5;
- case 5:
- side++;
- _a.label = 6;
- case 6:
- _i++;
- return [3 /*break*/, 3];
- case 7: return [2 /*return*/];
- }
- });
- });
- };
- CanvasRenderer.prototype.render = function (element) {
- return __awaiter(this, void 0, void 0, function () {
- var stack;
- return __generator(this, function (_a) {
- switch (_a.label) {
- case 0:
- if (this.options.backgroundColor) {
- this.ctx.fillStyle = color_1.asString(this.options.backgroundColor);
- this.ctx.fillRect(this.options.x - this.options.scrollX, this.options.y - this.options.scrollY, this.options.width, this.options.height);
- }
- stack = stacking_context_1.parseStackingContexts(element);
- return [4 /*yield*/, this.renderStack(stack)];
- case 1:
- _a.sent();
- this.applyEffects([], 2 /* BACKGROUND_BORDERS */);
- return [2 /*return*/, this.canvas];
- }
- });
- });
- };
- return CanvasRenderer;
- }());
- exports.CanvasRenderer = CanvasRenderer;
- var isTextInputElement = function (container) {
- if (container instanceof textarea_element_container_1.TextareaElementContainer) {
- return true;
- }
- else if (container instanceof select_element_container_1.SelectElementContainer) {
- return true;
- }
- else if (container instanceof input_element_container_1.InputElementContainer && container.type !== input_element_container_1.RADIO && container.type !== input_element_container_1.CHECKBOX) {
- return true;
- }
- return false;
- };
- var calculateBackgroundCurvedPaintingArea = function (clip, curves) {
- switch (clip) {
- case background_clip_1.BACKGROUND_CLIP.BORDER_BOX:
- return bound_curves_1.calculateBorderBoxPath(curves);
- case background_clip_1.BACKGROUND_CLIP.CONTENT_BOX:
- return bound_curves_1.calculateContentBoxPath(curves);
- case background_clip_1.BACKGROUND_CLIP.PADDING_BOX:
- default:
- return bound_curves_1.calculatePaddingBoxPath(curves);
- }
- };
- var canvasTextAlign = function (textAlign) {
- switch (textAlign) {
- case text_align_1.TEXT_ALIGN.CENTER:
- return 'center';
- case text_align_1.TEXT_ALIGN.RIGHT:
- return 'right';
- case text_align_1.TEXT_ALIGN.LEFT:
- default:
- return 'left';
- }
- };
- //# sourceMappingURL=canvas-renderer.js.map
|