2014-09-14 21:14:38 +04:00
|
|
|
function Renderer(width, height, images, options, document) {
|
2014-01-21 23:41:00 +04:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
this.images = images;
|
2014-09-04 19:47:06 +04:00
|
|
|
this.options = options;
|
2014-09-14 21:14:38 +04:00
|
|
|
this.document = document;
|
2014-01-19 20:04:27 +04:00
|
|
|
}
|
|
|
|
|
2014-09-04 19:47:06 +04:00
|
|
|
Renderer.prototype.renderImage = function(container, bounds, borderData, imageContainer) {
|
2014-01-26 20:10:04 +04:00
|
|
|
var paddingLeft = container.cssInt('paddingLeft'),
|
|
|
|
paddingTop = container.cssInt('paddingTop'),
|
|
|
|
paddingRight = container.cssInt('paddingRight'),
|
|
|
|
paddingBottom = container.cssInt('paddingBottom'),
|
|
|
|
borders = borderData.borders;
|
|
|
|
|
2014-09-08 22:25:21 +04:00
|
|
|
var width = bounds.width - (borders[1].width + borders[3].width + paddingLeft + paddingRight);
|
|
|
|
var height = bounds.height - (borders[0].width + borders[2].width + paddingTop + paddingBottom);
|
2014-01-26 20:10:04 +04:00
|
|
|
this.drawImage(
|
2014-09-04 19:47:06 +04:00
|
|
|
imageContainer,
|
2014-01-26 20:10:04 +04:00
|
|
|
0,
|
|
|
|
0,
|
2014-09-08 22:25:21 +04:00
|
|
|
imageContainer.image.width || width,
|
|
|
|
imageContainer.image.height || height,
|
2014-01-26 20:10:04 +04:00
|
|
|
bounds.left + paddingLeft + borders[3].width,
|
|
|
|
bounds.top + paddingTop + borders[0].width,
|
2014-09-08 22:25:21 +04:00
|
|
|
width,
|
|
|
|
height
|
2014-01-26 20:10:04 +04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2014-02-15 02:31:48 +04:00
|
|
|
Renderer.prototype.renderBackground = function(container, bounds, borderData) {
|
2014-01-19 20:04:27 +04:00
|
|
|
if (bounds.height > 0 && bounds.width > 0) {
|
|
|
|
this.renderBackgroundColor(container, bounds);
|
2014-02-15 02:31:48 +04:00
|
|
|
this.renderBackgroundImage(container, bounds, borderData);
|
2014-01-19 20:04:27 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Renderer.prototype.renderBackgroundColor = function(container, bounds) {
|
|
|
|
var color = container.css("backgroundColor");
|
|
|
|
if (!this.isTransparent(color)) {
|
2014-01-21 23:41:00 +04:00
|
|
|
this.rectangle(bounds.left, bounds.top, bounds.width, bounds.height, container.css("backgroundColor"));
|
2014-01-19 20:04:27 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Renderer.prototype.renderBorders = function(borders) {
|
|
|
|
borders.forEach(this.renderBorder, this);
|
|
|
|
};
|
|
|
|
|
|
|
|
Renderer.prototype.renderBorder = function(data) {
|
|
|
|
if (!this.isTransparent(data.color) && data.args !== null) {
|
|
|
|
this.drawShape(data.args, data.color);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-15 02:31:48 +04:00
|
|
|
Renderer.prototype.renderBackgroundImage = function(container, bounds, borderData) {
|
2014-01-21 23:41:00 +04:00
|
|
|
var backgroundImages = container.parseBackgroundImages();
|
2014-01-26 18:06:16 +04:00
|
|
|
backgroundImages.reverse().forEach(function(backgroundImage, index, arr) {
|
2014-02-01 23:48:30 +04:00
|
|
|
switch(backgroundImage.method) {
|
|
|
|
case "url":
|
|
|
|
var image = this.images.get(backgroundImage.args[0]);
|
|
|
|
if (image) {
|
2014-02-15 02:31:48 +04:00
|
|
|
this.renderBackgroundRepeating(container, bounds, image, arr.length - (index+1), borderData);
|
2014-02-01 23:48:30 +04:00
|
|
|
} else {
|
|
|
|
log("Error loading background-image", backgroundImage.args[0]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "linear-gradient":
|
|
|
|
case "gradient":
|
|
|
|
var gradientImage = this.images.get(backgroundImage.value);
|
|
|
|
if (gradientImage) {
|
2014-02-15 02:31:48 +04:00
|
|
|
this.renderBackgroundGradient(gradientImage, bounds, borderData);
|
2014-02-01 23:48:30 +04:00
|
|
|
} else {
|
|
|
|
log("Error loading background-image", backgroundImage.args[0]);
|
|
|
|
}
|
|
|
|
break;
|
2014-02-08 16:07:20 +04:00
|
|
|
case "none":
|
|
|
|
break;
|
2014-02-01 23:48:30 +04:00
|
|
|
default:
|
|
|
|
log("Unknown background-image type", backgroundImage.args[0]);
|
2014-01-21 23:41:00 +04:00
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
};
|
2014-01-19 20:04:27 +04:00
|
|
|
|
2014-02-15 02:31:48 +04:00
|
|
|
Renderer.prototype.renderBackgroundRepeating = function(container, bounds, imageContainer, index, borderData) {
|
2014-01-21 23:41:00 +04:00
|
|
|
var size = container.parseBackgroundSize(bounds, imageContainer.image, index);
|
|
|
|
var position = container.parseBackgroundPosition(bounds, imageContainer.image, index, size);
|
|
|
|
var repeat = container.parseBackgroundRepeat(index);
|
|
|
|
switch (repeat) {
|
2014-09-27 19:03:18 +04:00
|
|
|
case "repeat-x":
|
|
|
|
case "repeat no-repeat":
|
|
|
|
this.backgroundRepeatShape(imageContainer, position, size, bounds, bounds.left + borderData[3], bounds.top + position.top + borderData[0], 99999, size.height, borderData);
|
|
|
|
break;
|
|
|
|
case "repeat-y":
|
|
|
|
case "no-repeat repeat":
|
|
|
|
this.backgroundRepeatShape(imageContainer, position, size, bounds, bounds.left + position.left + borderData[3], bounds.top + borderData[0], size.width, 99999, borderData);
|
|
|
|
break;
|
|
|
|
case "no-repeat":
|
|
|
|
this.backgroundRepeatShape(imageContainer, position, size, bounds, bounds.left + position.left + borderData[3], bounds.top + position.top + borderData[0], size.width, size.height, borderData);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.renderBackgroundRepeat(imageContainer, position, size, {top: bounds.top, left: bounds.left}, borderData[3], borderData[0]);
|
|
|
|
break;
|
2014-01-21 23:41:00 +04:00
|
|
|
}
|
2014-01-19 20:04:27 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
Renderer.prototype.isTransparent = function(color) {
|
|
|
|
return (!color || color === "transparent" || color === "rgba(0, 0, 0, 0)");
|
|
|
|
};
|