added support for background-origin: content-box, fixed background-origin related background sizes

This commit is contained in:
Matthias Christen 2017-12-09 00:12:29 +01:00
parent 63377d47a4
commit 850338a76a
3 changed files with 70 additions and 8 deletions

View File

@ -27,6 +27,7 @@ import {
calculatePaddingBox,
calculatePaddingBoxPath
} from './Bounds';
import {PADDING_SIDES} from './parsing/padding';
import {FontMetrics} from './Font';
import {parseGradient} from './Gradient';
import TextContainer from './TextContainer';
@ -223,14 +224,35 @@ export default class Renderer {
if (image) {
const bounds = container.bounds;
const paddingBox = calculatePaddingBox(bounds, container.style.border);
const backgroundImageSize = calculateBackgroundSize(background, image, bounds);
// TODO support CONTENT_BOX
const backgroundPositioningArea =
container.style.background.backgroundOrigin === BACKGROUND_ORIGIN.BORDER_BOX
? bounds
: paddingBox;
let backgroundPositioningArea;
switch (container.style.background.backgroundOrigin) {
case BACKGROUND_ORIGIN.BORDER_BOX:
backgroundPositioningArea = bounds;
break;
case BACKGROUND_ORIGIN.CONTENT_BOX:
const paddingLeft = container.style.padding[PADDING_SIDES.LEFT].value;
const paddingRight = container.style.padding[PADDING_SIDES.RIGHT].value;
const paddingTop = container.style.padding[PADDING_SIDES.TOP].value;
const paddingBottom = container.style.padding[PADDING_SIDES.BOTTOM].value;
backgroundPositioningArea = new Bounds(
paddingBox.left + paddingLeft,
paddingBox.top + paddingTop,
paddingBox.width - paddingLeft - paddingRight,
paddingBox.height - paddingTop - paddingBottom
);
break;
case BACKGROUND_ORIGIN.PADDING_BOX:
default:
backgroundPositioningArea = paddingBox;
break;
}
const backgroundImageSize = calculateBackgroundSize(
background,
image,
backgroundPositioningArea
);
const position = calculateBackgroundPosition(
background.position,
backgroundImageSize,
@ -244,8 +266,8 @@ export default class Renderer {
bounds
);
const offsetX = Math.round(paddingBox.left + position.x);
const offsetY = Math.round(paddingBox.top + position.y);
const offsetX = Math.round(backgroundPositioningArea.left + position.x);
const offsetY = Math.round(backgroundPositioningArea.top + position.y);
this.target.renderRepeat(path, image, backgroundImageSize, offsetX, offsetY);
}
}

View File

@ -2,6 +2,13 @@
'use strict';
import Length from '../Length';
export const PADDING_SIDES = {
TOP: 0,
RIGHT: 1,
BOTTOM: 2,
LEFT: 3
};
const SIDES = ['top', 'right', 'bottom', 'left'];
export type Padding = Array<Length>;

View File

@ -0,0 +1,33 @@
<!doctype html>
<html>
<head>
<style>
div {
display: inline-block;
width: 100px;
height: 100px;
border: 15px solid blue;
padding: 10px;
margin: 5px;
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAqxQTFRFAAAAAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAADAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACAAACA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AAJXRFWHRkYXRlOmNyZWF0ZQAyMDE3LTA0LTE4VDA5OjU0OjU4KzAwOjAwhlt+sgAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxNy0wNC0xOFQwOTo1NDo1OCswMDowMPcGxg4AAAAydEVYdExpY2Vuc2UAaHR0cDovL2VuLndpa2lwZWRpYS5vcmcvd2lraS9QdWJsaWNfZG9tYWluP/3qzwAAAE10RVh0c29mdHdhcmUASW1hZ2VNYWdpY2sgNy4wLjEtNiBRMTYgeDg2XzY0IDIwMTYtMDktMTcgaHR0cDovL3d3dy5pbWFnZW1hZ2ljay5vcmfd2aVOAAAAGHRFWHRTb3VyY2UAV2lraW1lZGlhIENvbW1vbnPSwlOaAAAANnRFWHRTb3VyY2VfVVJMAGh0dHA6Ly9jb21tb25zLndpa2ltZWRpYS5vcmcvd2lraS9NYWluX1BhZ2US/BctAAAAGHRFWHRUaHVtYjo6RG9jdW1lbnQ6OlBhZ2VzADGn/7svAAAAGHRFWHRUaHVtYjo6SW1hZ2U6OkhlaWdodAAyNTbpw0QZAAAAF3RFWHRUaHVtYjo6SW1hZ2U6OldpZHRoADI1NnoyFEQAAAAZdEVYdFRodW1iOjpNaW1ldHlwZQBpbWFnZS9wbmc/slZOAAAAF3RFWHRUaHVtYjo6TVRpbWUAMTMwNDA2NDg0MNaK6HQAAAASdEVYdFRodW1iOjpTaXplADEwLjhLQr49AxAAAABddEVYdFRodW1iOjpVUkkAZmlsZTovLy9ob21lL3d3d3Jvb3Qvc2l0ZS93d3cuZWFzeWljb24ubmV0L2Nkbi1pbWcuZWFzeWljb24uY24vc3JjLzU0MzEvNTQzMTAxLnBuZ/nLz6kAAAAASUVORK5CYII=');
}
</style>
</head>
<body>
<div style="background-repeat: no-repeat; background-origin: content-box"></div>
<div style="background-repeat: no-repeat; background-origin: padding-box"></div>
<div style="background-repeat: no-repeat; background-origin: border-box"></div>
<div style="background-repeat: no-repeat; background-origin: content-box; background-size: cover"></div>
<div style="background-repeat: no-repeat; background-origin: padding-box; background-size: cover"></div>
<div style="background-repeat: no-repeat; background-origin: border-box; background-size: cover"></div>
<div style="background-repeat: no-repeat; background-origin: content-box; background-size: contain"></div>
<div style="background-repeat: no-repeat; background-origin: padding-box; background-size: contain"></div>
<div style="background-repeat: no-repeat; background-origin: border-box; background-size: contain"></div>
<div style="background-repeat: no-repeat; background-origin: content-box; background-position: 15px 20px"></div>
<div style="background-repeat: no-repeat; background-origin: padding-box; background-position: 15px 20px"></div>
<div style="background-repeat: no-repeat; background-origin: border-box; background-position: 15px 20px"></div>
<div style="background-repeat: repeat; background-origin: content-box"></div>
<div style="background-repeat: repeat; background-origin: padding-box"></div>
<div style="background-repeat: repeat; background-origin: border-box"></div>
</body>
</html>