From 9673adfd599746a22cea0b838966ef7b0c1baba5 Mon Sep 17 00:00:00 2001 From: MoyuScript Date: Sat, 9 Dec 2017 00:12:29 +0100 Subject: [PATCH] added support for background-origin: content-box, fixed background-origin related background sizes --- src/Renderer.js | 38 +++++++++++++++++++++------ src/parsing/padding.js | 7 +++++ tests/reftests/background/origin.html | 33 +++++++++++++++++++++++ 3 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 tests/reftests/background/origin.html diff --git a/src/Renderer.js b/src/Renderer.js index 923e095..dd65acd 100644 --- a/src/Renderer.js +++ b/src/Renderer.js @@ -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); } } diff --git a/src/parsing/padding.js b/src/parsing/padding.js index b6e46d5..8616f74 100644 --- a/src/parsing/padding.js +++ b/src/parsing/padding.js @@ -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; diff --git a/tests/reftests/background/origin.html b/tests/reftests/background/origin.html new file mode 100644 index 0000000..d7dcb34 --- /dev/null +++ b/tests/reftests/background/origin.html @@ -0,0 +1,33 @@ + + + + + + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ + \ No newline at end of file