Commit 211758fa authored by julien's avatar julien

Merge branch 'page-padding' into 'master'

add support for page padding

Closes #147

See merge request tools/pagedjs!76
parents 98ee0e31 cdb1c10a
......@@ -5,3 +5,4 @@ lib
__diff_output__
specs/**/*.pdf
issues
.vscode
\ No newline at end of file
......@@ -33,6 +33,12 @@ class AtPage extends Handler {
left: {},
bottom: {}
},
padding : {
top: {},
right: {},
left: {},
bottom: {}
},
block: {},
marks: undefined
};
......@@ -96,7 +102,7 @@ class AtPage extends Handler {
right: declarations.bleed[1],
bottom: declarations.bleed[2],
left: declarations.bleed[3]
}
};
break;
case 3: // top right bottom right
page.bleed = {
......@@ -104,7 +110,7 @@ class AtPage extends Handler {
right: declarations.bleed[1],
bottom: declarations.bleed[2],
left: declarations.bleed[1]
}
};
break;
case 2: // top right top right
page.bleed = {
......@@ -112,7 +118,7 @@ class AtPage extends Handler {
right: declarations.bleed[1],
bottom: declarations.bleed[0],
left: declarations.bleed[1]
}
};
break;
default:
page.bleed = {
......@@ -120,7 +126,7 @@ class AtPage extends Handler {
right: declarations.bleed[0],
bottom: declarations.bleed[0],
left: declarations.bleed[0]
}
};
}
}
......@@ -132,7 +138,7 @@ class AtPage extends Handler {
right: { value: 6, unit: "mm" },
bottom: { value: 6, unit: "mm" },
left: { value: 6, unit: "mm" }
}
};
}
page.marks = declarations.marks;
......@@ -141,6 +147,9 @@ class AtPage extends Handler {
if (declarations.margin) {
page.margin = declarations.margin;
}
if (declarations.padding) {
page.padding = declarations.padding;
}
if (declarations.marks) {
page.marks = declarations.marks;
......@@ -287,7 +296,7 @@ class AtPage extends Handler {
visit: "Declaration",
enter: (declaration, dItem, dList) => {
let prop = csstree.property(declaration.property).name;
let value = declaration.value;
// let value = declaration.value;
if (prop === "marks") {
parsed.marks = [];
......@@ -301,6 +310,7 @@ class AtPage extends Handler {
} else if (prop === "margin") {
parsed.margin = this.getMargins(declaration);
dList.remove(dItem);
} else if (prop.indexOf("margin-") === 0) {
let m = prop.substring("margin-".length);
if (!parsed.margin) {
......@@ -313,11 +323,29 @@ class AtPage extends Handler {
}
parsed.margin[m] = declaration.value.children.first();
dList.remove(dItem);
} else if (prop === "padding"){
parsed.padding = this.getPaddings(declaration.value);
dList.remove(dItem);
} else if (prop.indexOf("padding-") === 0) {
let p = prop.substring("padding-".length);
if (!parsed.padding) {
parsed.padding = {
top: {},
right: {},
left: {},
bottom: {}
};
}
parsed.padding[p] = declaration.value.children.first();
dList.remove(dItem);
} else if (prop === "size") {
parsed.size = this.getSize(declaration);
dList.remove(dItem);
} else if (prop === "bleed") {
parsed.bleed = []
parsed.bleed = [];
csstree.walk(declaration, {
enter: (subNode) => {
......@@ -450,6 +478,46 @@ class AtPage extends Handler {
return margin;
}
getPaddings(declaration) {
let paddings = [];
let padding = {
top: {},
right: {},
left: {},
bottom: {}
};
csstree.walk(declaration, {
visit: "Dimension",
enter: (node, item, list) => {
paddings.push(node);
}
});
if (paddings.length === 1) {
for (let p in padding) {
padding[p] = paddings[0];
}
} else if (paddings.length === 2) {
padding.top = paddings[0];
padding.right = paddings[1];
padding.bottom = paddings[0];
padding.left = paddings[1];
} else if (paddings.length === 3) {
padding.top = paddings[0];
padding.right = paddings[1];
padding.bottom = paddings[2];
padding.left = paddings[1];
} else if (paddings.length === 4) {
padding.top = paddings[0];
padding.right = paddings[1];
padding.bottom = paddings[2];
padding.left = paddings[3];
}
return padding;
}
addPageClasses(pages, ast, sheet) {
// First add * page
if ("*" in pages && !pages["*"].added) {
......@@ -512,6 +580,8 @@ class AtPage extends Handler {
this.addMarginVars(page.margin, children, children.first());
this.addPaddingVars(page.padding, children, children.first());
if (page.width) {
this.addDimensions(page.width, page.height, page.orientation, children, children.first());
}
......@@ -542,6 +612,26 @@ class AtPage extends Handler {
}
}
addPaddingVars(padding, list, item) {
// variables for padding
for (let p in padding) {
if (typeof padding[p].value !== "undefined") {
let value = padding[p].value + (padding[p].unit || "");
let pVar = list.createItem({
type: "Declaration",
property: "--pagedjs-padding-" + p,
value: {
type: "Raw",
value: value
}
});
list.append(pVar, item);
}
}
}
addDimensions(width, height, orientation, list, item) {
let widthString, heightString;
......@@ -817,7 +907,7 @@ class AtPage extends Handler {
marks.forEach((mark) => {
let markDisplay = this.createVariable("--pagedjs-mark-" + mark + "-display", "block");
rules.push(markDisplay);
})
});
}
// orientation variable
......@@ -1058,6 +1148,20 @@ class AtPage extends Handler {
}
});
children.appendData({
type: "Declaration",
property: "padding",
loc: null,
value: {
type: "Value",
children: [{
type: "Dimension",
unit: "px",
value: 0
}]
}
});
let rule = ast.children.createItem({
type: "Atrule",
prelude: null,
......
......@@ -12,6 +12,10 @@ export default `
--pagedjs-margin-right: 1in;
--pagedjs-margin-bottom: 1in;
--pagedjs-margin-left: 1in;
--pagedjs-padding-top: 0mm;
--pagedjs-padding-right: 0mm;
--pagedjs-padding-bottom: 0mm;
--pagedjs-padding-left: 0mm;
--pagedjs-bleed-top: 0mm;
--pagedjs-bleed-right: 0mm;
--pagedjs-bleed-bottom: 0mm;
......@@ -354,6 +358,8 @@ export default `
grid-row: page;
width: 100%;
height: 100%;
padding: var(--pagedjs-padding-top) var(--pagedjs-padding-right) var(--pagedjs-padding-bottom) var(--pagedjs-padding-left)
}
.pagedjs_pagebox > .pagedjs_area > .pagedjs_page_content {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment