Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow units for page size #2773

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 54 additions & 24 deletions packages/layout/src/page/getSize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import isLandscape from './isLandscape';

// page sizes for 72dpi. 72dpi is used internally by pdfkit
const PAGE_SIZES = {
'4A0': [4767.87, 6740.79],
'2A0': [3370.39, 4767.87],
Expand Down Expand Up @@ -54,11 +55,56 @@ const PAGE_SIZES = {
ID1: [153, 243],
};

/**
* Parses scalar value in value and unit pairs
*
* @param {string} value scalar value
* @returns {Object} parsed value
*/
const parseValue = (value) => {
const match = /^(-?\d*\.?\d+)(in|mm|cm|pt|px)?$/g.exec(value);

return match
? { value: parseFloat(match[1]), unit: match[2] || 'pt' }
: { value, unit: undefined };
};

/**
* Transform given scalar value to 72dpi equivalent of size
*
* @param {string} value styles value
* @param {number} inputDpi user defined dpi
* @returns {Object} transformed value
*/
const transformUnit = (value, inputDpi) => {
const scalar = parseValue(value);

const outputDpi = 72;
const mmFactor = (1 / 25.4) * outputDpi;
const cmFactor = (1 / 2.54) * outputDpi;

switch (scalar.unit) {
case 'in':
return scalar.value * outputDpi;
case 'mm':
return scalar.value * mmFactor;
case 'cm':
return scalar.value * cmFactor;
default:
return scalar.value * (outputDpi / inputDpi);
}
};

const transformUnits = ({ width, height }, dpi) => ({
width: transformUnit(width, dpi),
height: transformUnit(height, dpi),
});

/**
* Transforms array into size object
*
* @param {number[]} v array
* @returns {{ width: number, height: number }} size object with width and height
* @param {number[] | string[]} v array
* @returns {{ width: number | string, height: number | string }} size object with width and height
*/
const toSizeObject = (v) => ({ width: v[0], height: v[1] });

Expand All @@ -70,18 +116,6 @@ const toSizeObject = (v) => ({ width: v[0], height: v[1] });
*/
const flipSizeObject = (v) => ({ width: v.height, height: v.width });

/**
* Adjust page size to passed DPI
*
* @param {{ width: number, height: number }} v size object
* @param {number} dpi DPI
* @returns {{ width: number, height: number }} adjusted size object
*/
const adjustDpi = (v, dpi) => ({
width: v.width ? v.width * (72 / dpi) : v.width,
height: v.height ? v.height * (72 / dpi) : v.height,
});

/**
* Returns size object from a given string
*
Expand All @@ -95,10 +129,10 @@ const getStringSize = (v) => {
/**
* Returns size object from a single number
*
* @param {number} n page size number
* @returns {{ width: number, height: number }} size object with width and height
* @param {number|string} n page size number
* @returns {{ width: number|string, height: number|string }} size object with width and height
*/
const getNumberSize = (n) => toSizeObject([n]);
const getNumberSize = (n) => toSizeObject([n, n]);

/**
* Return page size in an object { width, height }
Expand All @@ -116,18 +150,14 @@ const getSize = (page) => {
* @type {{ width: number, height: number }}
*/
let size;

if (type === 'string') {
size = getStringSize(value);
} else if (Array.isArray(value)) {
size = toSizeObject(value);
size = adjustDpi(size, dpi);
size = transformUnits(toSizeObject(value), dpi);
} else if (type === 'number') {
size = getNumberSize(value);
size = adjustDpi(size, dpi);
size = transformUnits(getNumberSize(value), dpi);
} else {
size = value;
size = adjustDpi(size, dpi);
size = transformUnits(value, dpi);
}

return isLandscape(page) ? flipSizeObject(size) : size;
Expand Down